2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/buffer.h>
13 #include <openssl/objects.h>
14 #include <openssl/evp.h>
15 #include <openssl/rand.h>
16 #include <openssl/x509.h>
17 #include <openssl/pkcs12.h>
18 #include <openssl/pem.h>
20 static int do_pk8pkey(BIO
*bp
, EVP_PKEY
*x
, int isder
,
21 int nid
, const EVP_CIPHER
*enc
,
22 char *kstr
, int klen
, pem_password_cb
*cb
, void *u
);
24 #ifndef OPENSSL_NO_STDIO
25 static int do_pk8pkey_fp(FILE *bp
, EVP_PKEY
*x
, int isder
,
26 int nid
, const EVP_CIPHER
*enc
,
27 char *kstr
, int klen
, pem_password_cb
*cb
, void *u
);
30 * These functions write a private key in PKCS#8 format: it is a "drop in"
31 * replacement for PEM_write_bio_PrivateKey() and friends. As usual if 'enc'
32 * is NULL then it uses the unencrypted private key form. The 'nid' versions
33 * uses PKCS#5 v1.5 PBE algorithms whereas the others use PKCS#5 v2.0.
36 int PEM_write_bio_PKCS8PrivateKey_nid(BIO
*bp
, EVP_PKEY
*x
, int nid
,
38 pem_password_cb
*cb
, void *u
)
40 return do_pk8pkey(bp
, x
, 0, nid
, NULL
, kstr
, klen
, cb
, u
);
43 int PEM_write_bio_PKCS8PrivateKey(BIO
*bp
, EVP_PKEY
*x
, const EVP_CIPHER
*enc
,
45 pem_password_cb
*cb
, void *u
)
47 return do_pk8pkey(bp
, x
, 0, -1, enc
, kstr
, klen
, cb
, u
);
50 int i2d_PKCS8PrivateKey_bio(BIO
*bp
, EVP_PKEY
*x
, const EVP_CIPHER
*enc
,
52 pem_password_cb
*cb
, void *u
)
54 return do_pk8pkey(bp
, x
, 1, -1, enc
, kstr
, klen
, cb
, u
);
57 int i2d_PKCS8PrivateKey_nid_bio(BIO
*bp
, EVP_PKEY
*x
, int nid
,
59 pem_password_cb
*cb
, void *u
)
61 return do_pk8pkey(bp
, x
, 1, nid
, NULL
, kstr
, klen
, cb
, u
);
64 static int do_pk8pkey(BIO
*bp
, EVP_PKEY
*x
, int isder
, int nid
,
65 const EVP_CIPHER
*enc
, char *kstr
, int klen
,
66 pem_password_cb
*cb
, void *u
)
69 PKCS8_PRIV_KEY_INFO
*p8inf
;
70 char buf
[PEM_BUFSIZE
];
73 if ((p8inf
= EVP_PKEY2PKCS8(x
)) == NULL
) {
74 PEMerr(PEM_F_DO_PK8PKEY
, PEM_R_ERROR_CONVERTING_PRIVATE_KEY
);
77 if (enc
|| (nid
!= -1)) {
80 klen
= PEM_def_callback(buf
, PEM_BUFSIZE
, 1, u
);
82 klen
= cb(buf
, PEM_BUFSIZE
, 1, u
);
84 PEMerr(PEM_F_DO_PK8PKEY
, PEM_R_READ_KEY
);
85 PKCS8_PRIV_KEY_INFO_free(p8inf
);
91 p8
= PKCS8_encrypt(nid
, enc
, kstr
, klen
, NULL
, 0, 0, p8inf
);
93 OPENSSL_cleanse(buf
, klen
);
94 PKCS8_PRIV_KEY_INFO_free(p8inf
);
98 ret
= i2d_PKCS8_bio(bp
, p8
);
100 ret
= PEM_write_bio_PKCS8(bp
, p8
);
105 ret
= i2d_PKCS8_PRIV_KEY_INFO_bio(bp
, p8inf
);
107 ret
= PEM_write_bio_PKCS8_PRIV_KEY_INFO(bp
, p8inf
);
108 PKCS8_PRIV_KEY_INFO_free(p8inf
);
113 EVP_PKEY
*d2i_PKCS8PrivateKey_bio(BIO
*bp
, EVP_PKEY
**x
, pem_password_cb
*cb
,
116 PKCS8_PRIV_KEY_INFO
*p8inf
= NULL
;
120 char psbuf
[PEM_BUFSIZE
];
121 p8
= d2i_PKCS8_bio(bp
, NULL
);
125 klen
= cb(psbuf
, PEM_BUFSIZE
, 0, u
);
127 klen
= PEM_def_callback(psbuf
, PEM_BUFSIZE
, 0, u
);
129 PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_BIO
, PEM_R_BAD_PASSWORD_READ
);
133 p8inf
= PKCS8_decrypt(p8
, psbuf
, klen
);
137 ret
= EVP_PKCS82PKEY(p8inf
);
138 PKCS8_PRIV_KEY_INFO_free(p8inf
);
148 #ifndef OPENSSL_NO_STDIO
150 int i2d_PKCS8PrivateKey_fp(FILE *fp
, EVP_PKEY
*x
, const EVP_CIPHER
*enc
,
151 char *kstr
, int klen
, pem_password_cb
*cb
, void *u
)
153 return do_pk8pkey_fp(fp
, x
, 1, -1, enc
, kstr
, klen
, cb
, u
);
156 int i2d_PKCS8PrivateKey_nid_fp(FILE *fp
, EVP_PKEY
*x
, int nid
,
157 char *kstr
, int klen
,
158 pem_password_cb
*cb
, void *u
)
160 return do_pk8pkey_fp(fp
, x
, 1, nid
, NULL
, kstr
, klen
, cb
, u
);
163 int PEM_write_PKCS8PrivateKey_nid(FILE *fp
, EVP_PKEY
*x
, int nid
,
164 char *kstr
, int klen
,
165 pem_password_cb
*cb
, void *u
)
167 return do_pk8pkey_fp(fp
, x
, 0, nid
, NULL
, kstr
, klen
, cb
, u
);
170 int PEM_write_PKCS8PrivateKey(FILE *fp
, EVP_PKEY
*x
, const EVP_CIPHER
*enc
,
171 char *kstr
, int klen
, pem_password_cb
*cb
,
174 return do_pk8pkey_fp(fp
, x
, 0, -1, enc
, kstr
, klen
, cb
, u
);
177 static int do_pk8pkey_fp(FILE *fp
, EVP_PKEY
*x
, int isder
, int nid
,
178 const EVP_CIPHER
*enc
, char *kstr
, int klen
,
179 pem_password_cb
*cb
, void *u
)
184 if ((bp
= BIO_new_fp(fp
, BIO_NOCLOSE
)) == NULL
) {
185 PEMerr(PEM_F_DO_PK8PKEY_FP
, ERR_R_BUF_LIB
);
188 ret
= do_pk8pkey(bp
, x
, isder
, nid
, enc
, kstr
, klen
, cb
, u
);
193 EVP_PKEY
*d2i_PKCS8PrivateKey_fp(FILE *fp
, EVP_PKEY
**x
, pem_password_cb
*cb
,
199 if ((bp
= BIO_new_fp(fp
, BIO_NOCLOSE
)) == NULL
) {
200 PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_FP
, ERR_R_BUF_LIB
);
203 ret
= d2i_PKCS8PrivateKey_bio(bp
, x
, cb
, u
);
210 IMPLEMENT_PEM_rw(PKCS8
, X509_SIG
, PEM_STRING_PKCS8
, X509_SIG
)
213 IMPLEMENT_PEM_rw(PKCS8_PRIV_KEY_INFO
, PKCS8_PRIV_KEY_INFO
, PEM_STRING_PKCS8INF
,