1/*
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#include <stdio.h>
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
19static int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder,
20 int nid, const EVP_CIPHER *enc,
21 const char *kstr, int klen,
22 pem_password_cb *cb, void *u);
23
24#ifndef OPENSSL_NO_STDIO
25static int do_pk8pkey_fp(FILE *bp, const EVP_PKEY *x, int isder,
26 int nid, const EVP_CIPHER *enc,
27 const char *kstr, int klen,
28 pem_password_cb *cb, void *u);
29#endif
30/*
31 * These functions write a private key in PKCS#8 format: it is a "drop in"
32 * replacement for PEM_write_bio_PrivateKey() and friends. As usual if 'enc'
33 * is NULL then it uses the unencrypted private key form. The 'nid' versions
34 * uses PKCS#5 v1.5 PBE algorithms whereas the others use PKCS#5 v2.0.
35 */
36
37int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid,
38 const char *kstr, int klen,
39 pem_password_cb *cb, void *u)
40{
41 return do_pk8pkey(bp, x, 0, nid, NULL, kstr, klen, cb, u);
42}
43
44int PEM_write_bio_PKCS8PrivateKey(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
45 const char *kstr, int klen,
46 pem_password_cb *cb, void *u)
47{
48 return do_pk8pkey(bp, x, 0, -1, enc, kstr, klen, cb, u);
49}
50
51int i2d_PKCS8PrivateKey_bio(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
52 const char *kstr, int klen,
53 pem_password_cb *cb, void *u)
54{
55 return do_pk8pkey(bp, x, 1, -1, enc, kstr, klen, cb, u);
56}
57
58int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, const EVP_PKEY *x, int nid,
59 const char *kstr, int klen,
60 pem_password_cb *cb, void *u)
61{
62 return do_pk8pkey(bp, x, 1, nid, NULL, kstr, klen, cb, u);
63}
64
65static int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder, int nid,
66 const EVP_CIPHER *enc, const char *kstr, int klen,
67 pem_password_cb *cb, void *u)
68{
69 X509_SIG *p8;
70 PKCS8_PRIV_KEY_INFO *p8inf;
71 char buf[PEM_BUFSIZE];
72 int ret;
73
74 if ((p8inf = EVP_PKEY2PKCS8(x)) == NULL) {
75 PEMerr(PEM_F_DO_PK8PKEY, PEM_R_ERROR_CONVERTING_PRIVATE_KEY);
76 return 0;
77 }
78 if (enc || (nid != -1)) {
79 if (!kstr) {
80 if (!cb)
81 klen = PEM_def_callback(buf, PEM_BUFSIZE, 1, u);
82 else
83 klen = cb(buf, PEM_BUFSIZE, 1, u);
84 if (klen <= 0) {
85 PEMerr(PEM_F_DO_PK8PKEY, PEM_R_READ_KEY);
86 PKCS8_PRIV_KEY_INFO_free(p8inf);
87 return 0;
88 }
89
90 kstr = buf;
91 }
92 p8 = PKCS8_encrypt(nid, enc, kstr, klen, NULL, 0, 0, p8inf);
93 if (kstr == buf)
94 OPENSSL_cleanse(buf, klen);
95 PKCS8_PRIV_KEY_INFO_free(p8inf);
96 if (p8 == NULL)
97 return 0;
98 if (isder)
99 ret = i2d_PKCS8_bio(bp, p8);
100 else
101 ret = PEM_write_bio_PKCS8(bp, p8);
102 X509_SIG_free(p8);
103 return ret;
104 } else {
105 if (isder)
106 ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
107 else
108 ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(bp, p8inf);
109 PKCS8_PRIV_KEY_INFO_free(p8inf);
110 return ret;
111 }
112}
113
114EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
115 void *u)
116{
117 PKCS8_PRIV_KEY_INFO *p8inf = NULL;
118 X509_SIG *p8 = NULL;
119 int klen;
120 EVP_PKEY *ret;
121 char psbuf[PEM_BUFSIZE];
122
123 p8 = d2i_PKCS8_bio(bp, NULL);
124 if (p8 == NULL)
125 return NULL;
126 if (cb != NULL)
127 klen = cb(psbuf, PEM_BUFSIZE, 0, u);
128 else
129 klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
130 if (klen < 0) {
131 PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_BIO, PEM_R_BAD_PASSWORD_READ);
132 X509_SIG_free(p8);
133 return NULL;
134 }
135 p8inf = PKCS8_decrypt(p8, psbuf, klen);
136 X509_SIG_free(p8);
137 OPENSSL_cleanse(psbuf, klen);
138 if (p8inf == NULL)
139 return NULL;
140 ret = EVP_PKCS82PKEY(p8inf);
141 PKCS8_PRIV_KEY_INFO_free(p8inf);
142 if (!ret)
143 return NULL;
144 if (x != NULL) {
145 EVP_PKEY_free(*x);
146 *x = ret;
147 }
148 return ret;
149}
150
151#ifndef OPENSSL_NO_STDIO
152
153int i2d_PKCS8PrivateKey_fp(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
154 const char *kstr, int klen,
155 pem_password_cb *cb, void *u)
156{
157 return do_pk8pkey_fp(fp, x, 1, -1, enc, kstr, klen, cb, u);
158}
159
160int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, const EVP_PKEY *x, int nid,
161 const char *kstr, int klen,
162 pem_password_cb *cb, void *u)
163{
164 return do_pk8pkey_fp(fp, x, 1, nid, NULL, kstr, klen, cb, u);
165}
166
167int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid,
168 const char *kstr, int klen,
169 pem_password_cb *cb, void *u)
170{
171 return do_pk8pkey_fp(fp, x, 0, nid, NULL, kstr, klen, cb, u);
172}
173
174int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
175 const char *kstr, int klen,
176 pem_password_cb *cb, void *u)
177{
178 return do_pk8pkey_fp(fp, x, 0, -1, enc, kstr, klen, cb, u);
179}
180
181static int do_pk8pkey_fp(FILE *fp, const EVP_PKEY *x, int isder, int nid,
182 const EVP_CIPHER *enc, const char *kstr, int klen,
183 pem_password_cb *cb, void *u)
184{
185 BIO *bp;
186 int ret;
187
188 if ((bp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
189 PEMerr(PEM_F_DO_PK8PKEY_FP, ERR_R_BUF_LIB);
190 return 0;
191 }
192 ret = do_pk8pkey(bp, x, isder, nid, enc, kstr, klen, cb, u);
193 BIO_free(bp);
194 return ret;
195}
196
197EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
198 void *u)
199{
200 BIO *bp;
201 EVP_PKEY *ret;
202
203 if ((bp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
204 PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_FP, ERR_R_BUF_LIB);
205 return NULL;
206 }
207 ret = d2i_PKCS8PrivateKey_bio(bp, x, cb, u);
208 BIO_free(bp);
209 return ret;
210}
211
212#endif
213
214IMPLEMENT_PEM_rw(PKCS8, X509_SIG, PEM_STRING_PKCS8, X509_SIG)
215
216
217IMPLEMENT_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO, PEM_STRING_PKCS8INF,
218 PKCS8_PRIV_KEY_INFO)
219