2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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/x509.h>
16 #include <openssl/pkcs12.h>
17 #include <openssl/pem.h>
18 #include <openssl/serializer.h>
20 static int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder,
21 int nid, const EVP_CIPHER *enc,
22 const char *kstr, int klen,
23 pem_password_cb *cb, void *u);
25 #ifndef OPENSSL_NO_STDIO
26 static int do_pk8pkey_fp(FILE *bp, const EVP_PKEY *x, int isder,
27 int nid, const EVP_CIPHER *enc,
28 const char *kstr, int klen,
29 pem_password_cb *cb, void *u);
32 * These functions write a private key in PKCS#8 format: it is a "drop in"
33 * replacement for PEM_write_bio_PrivateKey() and friends. As usual if 'enc'
34 * is NULL then it uses the unencrypted private key form. The 'nid' versions
35 * uses PKCS#5 v1.5 PBE algorithms whereas the others use PKCS#5 v2.0.
38 int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid,
39 const char *kstr, int klen,
40 pem_password_cb *cb, void *u)
42 return do_pk8pkey(bp, x, 0, nid, NULL, kstr, klen, cb, u);
45 int PEM_write_bio_PKCS8PrivateKey(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
46 const char *kstr, int klen,
47 pem_password_cb *cb, void *u)
49 return do_pk8pkey(bp, x, 0, -1, enc, kstr, klen, cb, u);
52 int i2d_PKCS8PrivateKey_bio(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
53 const char *kstr, int klen,
54 pem_password_cb *cb, void *u)
56 return do_pk8pkey(bp, x, 1, -1, enc, kstr, klen, cb, u);
59 int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, const EVP_PKEY *x, int nid,
60 const char *kstr, int klen,
61 pem_password_cb *cb, void *u)
63 return do_pk8pkey(bp, x, 1, nid, NULL, kstr, klen, cb, u);
66 static int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder, int nid,
67 const EVP_CIPHER *enc, const char *kstr, int klen,
68 pem_password_cb *cb, void *u)
71 const char *pq = isder
72 ? OSSL_SERIALIZER_PrivateKey_TO_DER_PQ
73 : OSSL_SERIALIZER_PrivateKey_TO_PEM_PQ;
74 OSSL_SERIALIZER_CTX *ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(x, pq);
80 * If no keystring or callback is set, OpenSSL traditionally uses the
81 * user's cb argument as a password string, or if that's NULL, it falls
82 * back on PEM_def_callback().
84 if (kstr == NULL && cb == NULL) {
89 cb = PEM_def_callback;
93 if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL) {
97 if (OSSL_SERIALIZER_CTX_set_cipher(ctx, EVP_CIPHER_name(enc),
99 const unsigned char *ukstr = (const unsigned char *)kstr;
102 * Try to pass the passphrase if one was given, or the
103 * passphrase callback if one was given. If none of them
104 * are given and that's wrong, we rely on the _to_bio()
105 * call to generate errors.
109 && !OSSL_SERIALIZER_CTX_set_passphrase(ctx, ukstr, klen))
112 && !OSSL_SERIALIZER_CTX_set_passphrase_cb(ctx, 1,
117 ret = ret && OSSL_SERIALIZER_to_bio(ctx, bp);
120 PKCS8_PRIV_KEY_INFO *p8inf;
121 char buf[PEM_BUFSIZE];
124 if ((p8inf = EVP_PKEY2PKCS8(x)) == NULL) {
125 PEMerr(PEM_F_DO_PK8PKEY, PEM_R_ERROR_CONVERTING_PRIVATE_KEY);
128 if (enc || (nid != -1)) {
130 klen = cb(buf, PEM_BUFSIZE, 1, u);
132 PEMerr(PEM_F_DO_PK8PKEY, PEM_R_READ_KEY);
138 p8 = PKCS8_encrypt(nid, enc, kstr, klen, NULL, 0, 0, p8inf);
140 OPENSSL_cleanse(buf, klen);
144 ret = i2d_PKCS8_bio(bp, p8);
146 ret = PEM_write_bio_PKCS8(bp, p8);
150 ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
152 ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(bp, p8inf);
155 PKCS8_PRIV_KEY_INFO_free(p8inf);
157 OSSL_SERIALIZER_CTX_free(ctx);
161 EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
164 PKCS8_PRIV_KEY_INFO *p8inf = NULL;
168 char psbuf[PEM_BUFSIZE];
170 p8 = d2i_PKCS8_bio(bp, NULL);
174 klen = cb(psbuf, PEM_BUFSIZE, 0, u);
176 klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
178 PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_BIO, PEM_R_BAD_PASSWORD_READ);
182 p8inf = PKCS8_decrypt(p8, psbuf, klen);
184 OPENSSL_cleanse(psbuf, klen);
187 ret = EVP_PKCS82PKEY(p8inf);
188 PKCS8_PRIV_KEY_INFO_free(p8inf);
198 #ifndef OPENSSL_NO_STDIO
200 int i2d_PKCS8PrivateKey_fp(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
201 const char *kstr, int klen,
202 pem_password_cb *cb, void *u)
204 return do_pk8pkey_fp(fp, x, 1, -1, enc, kstr, klen, cb, u);
207 int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, const EVP_PKEY *x, int nid,
208 const char *kstr, int klen,
209 pem_password_cb *cb, void *u)
211 return do_pk8pkey_fp(fp, x, 1, nid, NULL, kstr, klen, cb, u);
214 int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid,
215 const char *kstr, int klen,
216 pem_password_cb *cb, void *u)
218 return do_pk8pkey_fp(fp, x, 0, nid, NULL, kstr, klen, cb, u);
221 int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
222 const char *kstr, int klen,
223 pem_password_cb *cb, void *u)
225 return do_pk8pkey_fp(fp, x, 0, -1, enc, kstr, klen, cb, u);
228 static int do_pk8pkey_fp(FILE *fp, const EVP_PKEY *x, int isder, int nid,
229 const EVP_CIPHER *enc, const char *kstr, int klen,
230 pem_password_cb *cb, void *u)
235 if ((bp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
236 PEMerr(PEM_F_DO_PK8PKEY_FP, ERR_R_BUF_LIB);
239 ret = do_pk8pkey(bp, x, isder, nid, enc, kstr, klen, cb, u);
244 EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
250 if ((bp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
251 PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_FP, ERR_R_BUF_LIB);
254 ret = d2i_PKCS8PrivateKey_bio(bp, x, cb, u);
261 IMPLEMENT_PEM_rw(PKCS8, X509_SIG, PEM_STRING_PKCS8, X509_SIG)
264 IMPLEMENT_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO, PEM_STRING_PKCS8INF,