Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkcs8 support #92

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ type PrivateKey interface {
// MarshalPKCS1PrivateKeyPEM converts the private key to PEM-encoded PKCS1
// format
MarshalPKCS1PrivateKeyPEM() (pem_block []byte, err error)

// MarshalPKCS1PrivateKeyPEM converts the private key to PEM-encoded PKCS8
// format
MarshalPKCS8PrivateKeyPEM() (pem_block []byte, err error)

// MarshalPKCS1PrivateKeyDER converts the private key to DER-encoded PKCS1
// format
Expand Down Expand Up @@ -170,6 +174,25 @@ func (key *pKey) MarshalPKCS1PrivateKeyPEM() (pem_block []byte,
return ioutil.ReadAll(asAnyBio(bio))
}

func (key *pKey) MarshalPKCS8PrivateKeyPEM() (pem_block []byte,
err error) {
bio := C.BIO_new(C.BIO_s_mem())
if bio == nil {
return nil, errors.New("failed to allocate memory BIO")
}
defer C.BIO_free(bio)

// PEM_write_bio_PrivateKey_traditional will use the key-specific PKCS1
// format if one is available for that key type, otherwise it will encode
// to a PKCS8 key.
if int(C.X_PEM_write_bio_PrivateKey_pkcs8(bio, key.key, nil, nil,
C.int(0), nil, nil)) != 1 {
return nil, errors.New("failed dumping private key")
}

return ioutil.ReadAll(asAnyBio(bio))
}

func (key *pKey) MarshalPKCS1PrivateKeyDER() (der_block []byte,
err error) {
bio := C.BIO_new(C.BIO_s_mem())
Expand Down
5 changes: 5 additions & 0 deletions shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ int X_PEM_write_bio_PrivateKey_traditional(BIO *bio, EVP_PKEY *key, const EVP_CI
************************************************
*/


int X_PEM_write_bio_PrivateKey_pkcs8(BIO *bio, EVP_PKEY *key, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u) {
return PEM_write_bio_PKCS8PrivateKey(bio, key, enc, kstr, klen, cb, u);
}

int X_shim_init() {
int rc = 0;

Expand Down
4 changes: 3 additions & 1 deletion shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,6 @@ extern int X_sk_X509_num(STACK_OF(X509) *sk);
extern X509 *X_sk_X509_value(STACK_OF(X509)* sk, int i);

/* PEM methods */
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);
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);
/* PEM methods */
extern int X_PEM_write_bio_PrivateKey_pkcs8(BIO *bio, EVP_PKEY *key, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u);