Skip to content

Commit 8a9ba0a

Browse files
authored
Fix incompatibilities with OpenSSL 1.0.x (#1)
Fix incompatiblities with OpenSSL 1.0.x
1 parent f6b1996 commit 8a9ba0a

File tree

4 files changed

+63
-24
lines changed

4 files changed

+63
-24
lines changed

key.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ var (
3232
SHA512_Method Method = C.X_EVP_sha512()
3333
)
3434

35-
type KeyType int
36-
35+
// Constants for the various key types.
36+
// Mapping of name -> NID taken from openssl/evp.h
3737
const (
38-
KeyTypeNone KeyType = C.EVP_PKEY_NONE
39-
KeyTypeRSA KeyType = C.EVP_PKEY_RSA
40-
KeyTypeRSA2 KeyType = C.EVP_PKEY_RSA2
41-
KeyTypeDSA KeyType = C.EVP_PKEY_DSA
42-
KeyTypeDSA1 KeyType = C.EVP_PKEY_DSA1
43-
KeyTypeDSA2 KeyType = C.EVP_PKEY_DSA2
44-
KeyTypeDSA3 KeyType = C.EVP_PKEY_DSA3
45-
KeyTypeDSA4 KeyType = C.EVP_PKEY_DSA4
46-
KeyTypeDH KeyType = C.EVP_PKEY_DH
47-
KeyTypeDHX KeyType = C.EVP_PKEY_DHX
48-
KeyTypeEC KeyType = C.EVP_PKEY_EC
49-
KeyTypeHMAC KeyType = C.EVP_PKEY_HMAC
50-
KeyTypeCMAC KeyType = C.EVP_PKEY_CMAC
51-
KeyTypeTLS1PRF KeyType = C.EVP_PKEY_TLS1_PRF
52-
KeyTypeHKDF KeyType = C.EVP_PKEY_HKDF
38+
KeyTypeNone = NID_undef
39+
KeyTypeRSA = NID_rsaEncryption
40+
KeyTypeRSA2 = NID_rsa
41+
KeyTypeDSA = NID_dsa
42+
KeyTypeDSA1 = NID_dsa_2
43+
KeyTypeDSA2 = NID_dsaWithSHA
44+
KeyTypeDSA3 = NID_dsaWithSHA1
45+
KeyTypeDSA4 = NID_dsaWithSHA1_2
46+
KeyTypeDH = NID_dhKeyAgreement
47+
KeyTypeDHX = NID_dhpublicnumber
48+
KeyTypeEC = NID_x9_62_id_ecPublicKey
49+
KeyTypeHMAC = NID_hmac
50+
KeyTypeCMAC = NID_cmac
51+
KeyTypeTLS1PRF = NID_tls1_prf
52+
KeyTypeHKDF = NID_hdkf
5353
)
5454

5555
type PublicKey interface {
@@ -66,7 +66,7 @@ type PublicKey interface {
6666

6767
// KeyType returns an identifier for what kind of key is represented by this
6868
// object.
69-
KeyType() KeyType
69+
KeyType() NID
7070

7171
// BaseType returns an identifier for what kind of key is represented
7272
// by this object.
@@ -75,7 +75,7 @@ type PublicKey interface {
7575
//
7676
// For example, a key with a `KeyType() == KeyTypeRSA` and a key with a
7777
// `KeyType() == KeyTypeRSA2` would both have `BaseType() == KeyTypeRSA`.
78-
BaseType() KeyType
78+
BaseType() NID
7979

8080
evpPKey() *C.EVP_PKEY
8181
}
@@ -101,12 +101,12 @@ type pKey struct {
101101

102102
func (key *pKey) evpPKey() *C.EVP_PKEY { return key.key }
103103

104-
func (key *pKey) KeyType() KeyType {
105-
return KeyType(C.EVP_PKEY_id(key.key))
104+
func (key *pKey) KeyType() NID {
105+
return NID(C.EVP_PKEY_id(key.key))
106106
}
107107

108-
func (key *pKey) BaseType() KeyType {
109-
return KeyType(C.EVP_PKEY_base_id(key.key))
108+
func (key *pKey) BaseType() NID {
109+
return NID(C.EVP_PKEY_base_id(key.key))
110110
}
111111

112112
func (key *pKey) SignPKCS1v15(method Method, data []byte) ([]byte, error) {
@@ -162,7 +162,7 @@ func (key *pKey) MarshalPKCS1PrivateKeyPEM() (pem_block []byte,
162162
// PEM_write_bio_PrivateKey_traditional will use the key-specific PKCS1
163163
// format if one is available for that key type, otherwise it will encode
164164
// to a PKCS8 key.
165-
if int(C.PEM_write_bio_PrivateKey_traditional(bio, key.key, nil, nil,
165+
if int(C.X_PEM_write_bio_PrivateKey_traditional(bio, key.key, nil, nil,
166166
C.int(0), nil, nil)) != 1 {
167167
return nil, errors.New("failed dumping private key")
168168
}

nid.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package openssl
1717
type NID int
1818

1919
const (
20+
NID_undef NID = 0
2021
NID_rsadsi NID = 1
2122
NID_pkcs NID = 2
2223
NID_md2 NID = 3
@@ -196,4 +197,10 @@ const (
196197
NID_ad_OCSP NID = 178
197198
NID_ad_ca_issuers NID = 179
198199
NID_OCSP_sign NID = 180
200+
NID_x9_62_id_ecPublicKey NID = 408
201+
NID_hmac NID = 855
202+
NID_cmac NID = 894
203+
NID_dhpublicnumber NID = 920
204+
NID_tls1_prf NID = 1021
205+
NID_hdkf NID = 1036
199206
)

shim.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ void X_HMAC_CTX_free(HMAC_CTX *ctx) {
156156
HMAC_CTX_free(ctx);
157157
}
158158

159+
int X_PEM_write_bio_PrivateKey_traditional(BIO *bio, EVP_PKEY *key, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u) {
160+
return PEM_write_bio_PrivateKey_traditional(bio, key, enc, kstr, klen, cb, u);
161+
}
162+
159163
#endif
160164

161165

@@ -276,6 +280,32 @@ void X_HMAC_CTX_free(HMAC_CTX *ctx) {
276280
}
277281
}
278282

283+
int X_PEM_write_bio_PrivateKey_traditional(BIO *bio, EVP_PKEY *key, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u) {
284+
/* PEM_write_bio_PrivateKey always tries to use the PKCS8 format if it
285+
* is available, instead of using the "traditional" format as stated in the
286+
* OpenSSL man page.
287+
* i2d_PrivateKey should give us the correct DER encoding, so we'll just
288+
* use PEM_ASN1_write_bio directly to write the DER encoding with the correct
289+
* type header. */
290+
291+
int ppkey_id, pkey_base_id, ppkey_flags;
292+
const char *pinfo, *ppem_str;
293+
char pem_type_str[80];
294+
295+
// Lookup the ASN1 method information to get the pem type
296+
if (EVP_PKEY_asn1_get0_info(&ppkey_id, &pkey_base_id, &ppkey_flags, &pinfo, &ppem_str, key->ameth) != 1) {
297+
return 0;
298+
}
299+
// Set up the PEM type string
300+
if (BIO_snprintf(pem_type_str, 80, "%s PRIVATE KEY", ppem_str) <= 0) {
301+
// Failed to write out the pem type string, something is really wrong.
302+
return 0;
303+
}
304+
// Write out everything to the BIO
305+
return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
306+
pem_type_str, bio, key, enc, kstr, klen, cb, u);
307+
}
308+
279309
#endif
280310

281311

shim.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,5 @@ extern const ASN1_TIME *X_X509_get0_notAfter(const X509 *x);
158158
extern int X_sk_X509_num(STACK_OF(X509) *sk);
159159
extern X509 *X_sk_X509_value(STACK_OF(X509)* sk, int i);
160160

161+
/* PEM methods */
162+
extern int X_PEM_write_bio_PrivateKey_traditional(BIO *bio, EVP_PKEY *key, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u);

0 commit comments

Comments
 (0)