]>
Commit | Line | Data |
---|---|---|
62867571 | 1 | /* |
a28d06f3 | 2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
1241126a | 3 | * |
16742672 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
62867571 RS |
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 | |
1241126a DSH |
8 | */ |
9 | ||
b4c4a2c6 | 10 | /* We need to use some deprecated APIs */ |
a1447076 RL |
11 | #define OPENSSL_SUPPRESS_DEPRECATED |
12 | ||
1241126a | 13 | #include <stdio.h> |
b39fc560 | 14 | #include "internal/cryptlib.h" |
1241126a DSH |
15 | #include <openssl/buffer.h> |
16 | #include <openssl/objects.h> | |
17 | #include <openssl/evp.h> | |
1241126a DSH |
18 | #include <openssl/x509.h> |
19 | #include <openssl/pkcs12.h> | |
20 | #include <openssl/pem.h> | |
3c27208f RS |
21 | #include <openssl/engine.h> |
22 | #include <openssl/dh.h> | |
b4c4a2c6 | 23 | #include <openssl/decoder.h> |
1427d33c | 24 | #include <openssl/ui.h> |
25f2138b DMSP |
25 | #include "crypto/asn1.h" |
26 | #include "crypto/evp.h" | |
f864a939 | 27 | #include "pem_local.h" |
1241126a | 28 | |
b78c0166 | 29 | int ossl_pem_check_suffix(const char *pem_str, const char *suffix); |
1241126a | 30 | |
7bc027d7 TM |
31 | static EVP_PKEY *pem_read_bio_key_decoder(BIO *bp, EVP_PKEY **x, |
32 | pem_password_cb *cb, void *u, | |
33 | OSSL_LIB_CTX *libctx, | |
34 | const char *propq, | |
35 | int selection) | |
0f113f3e | 36 | { |
b4c4a2c6 TM |
37 | EVP_PKEY *pkey = NULL; |
38 | OSSL_DECODER_CTX *dctx = NULL; | |
39 | ||
40 | dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "PEM", NULL, NULL, | |
41 | selection, libctx, propq); | |
42 | ||
43 | if (dctx == NULL) | |
6e5ccd58 | 44 | return NULL; |
6e5ccd58 | 45 | |
6e5ccd58 | 46 | if (cb == NULL) |
00eae3f9 | 47 | cb = PEM_def_callback; |
1427d33c | 48 | |
b4c4a2c6 | 49 | if (!OSSL_DECODER_CTX_set_pem_password_cb(dctx, cb, u)) |
1427d33c | 50 | goto err; |
b4c4a2c6 TM |
51 | |
52 | while (!OSSL_DECODER_from_bio(dctx, bp) || pkey == NULL) | |
53 | if (BIO_eof(bp) != 0) | |
0f113f3e | 54 | goto err; |
1427d33c | 55 | |
b4c4a2c6 TM |
56 | if (!evp_keymgmt_util_has(pkey, selection)) { |
57 | EVP_PKEY_free(pkey); | |
58 | pkey = NULL; | |
59 | ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS); | |
8d8fee64 | 60 | goto err; |
0f113f3e | 61 | } |
1427d33c | 62 | |
b4c4a2c6 TM |
63 | if (x != NULL) { |
64 | EVP_PKEY_free(*x); | |
65 | *x = pkey; | |
66 | } | |
1427d33c | 67 | |
0f113f3e | 68 | err: |
b4c4a2c6 TM |
69 | OSSL_DECODER_CTX_free(dctx); |
70 | return pkey; | |
0f113f3e | 71 | } |
1241126a | 72 | |
7bc027d7 TM |
73 | static EVP_PKEY *pem_read_bio_key_legacy(BIO *bp, EVP_PKEY **x, |
74 | pem_password_cb *cb, void *u, | |
75 | OSSL_LIB_CTX *libctx, | |
76 | const char *propq, | |
77 | int selection) | |
78 | { | |
79 | char *nm = NULL; | |
80 | const unsigned char *p = NULL; | |
81 | unsigned char *data = NULL; | |
82 | long len; | |
83 | int slen; | |
84 | EVP_PKEY *ret = NULL; | |
85 | ||
86 | ERR_set_mark(); /* not interested in PEM read errors */ | |
87 | if (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) { | |
88 | if (!PEM_bytes_read_bio_secmem(&data, &len, &nm, | |
89 | PEM_STRING_EVP_PKEY, | |
90 | bp, cb, u)) { | |
91 | ERR_pop_to_mark(); | |
92 | return NULL; | |
93 | } | |
94 | } else { | |
95 | const char *pem_string = PEM_STRING_PARAMETERS; | |
96 | ||
97 | if (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) | |
98 | pem_string = PEM_STRING_PUBLIC; | |
99 | if (!PEM_bytes_read_bio(&data, &len, &nm, | |
100 | pem_string, | |
101 | bp, cb, u)) { | |
102 | ERR_pop_to_mark(); | |
103 | return NULL; | |
104 | } | |
105 | } | |
106 | ERR_clear_last_mark(); | |
107 | p = data; | |
108 | ||
109 | if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) { | |
110 | PKCS8_PRIV_KEY_INFO *p8inf; | |
111 | ||
112 | if ((p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len)) == NULL) | |
113 | goto p8err; | |
114 | ret = evp_pkcs82pkey_legacy(p8inf, libctx, propq); | |
115 | if (x != NULL) { | |
116 | EVP_PKEY_free(*x); | |
117 | *x = ret; | |
118 | } | |
119 | PKCS8_PRIV_KEY_INFO_free(p8inf); | |
120 | } else if (strcmp(nm, PEM_STRING_PKCS8) == 0) { | |
121 | PKCS8_PRIV_KEY_INFO *p8inf; | |
122 | X509_SIG *p8; | |
123 | int klen; | |
124 | char psbuf[PEM_BUFSIZE]; | |
125 | ||
126 | if ((p8 = d2i_X509_SIG(NULL, &p, len)) == NULL) | |
127 | goto p8err; | |
128 | if (cb != NULL) | |
129 | klen = cb(psbuf, PEM_BUFSIZE, 0, u); | |
130 | else | |
131 | klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u); | |
132 | if (klen < 0) { | |
133 | ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ); | |
134 | X509_SIG_free(p8); | |
135 | goto err; | |
136 | } | |
137 | p8inf = PKCS8_decrypt(p8, psbuf, klen); | |
138 | X509_SIG_free(p8); | |
139 | OPENSSL_cleanse(psbuf, klen); | |
140 | if (p8inf == NULL) | |
141 | goto p8err; | |
142 | ret = evp_pkcs82pkey_legacy(p8inf, libctx, propq); | |
143 | if (x != NULL) { | |
144 | EVP_PKEY_free(*x); | |
145 | *x = ret; | |
146 | } | |
147 | PKCS8_PRIV_KEY_INFO_free(p8inf); | |
148 | } else if ((slen = ossl_pem_check_suffix(nm, "PRIVATE KEY")) > 0) { | |
149 | const EVP_PKEY_ASN1_METHOD *ameth; | |
150 | ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen); | |
151 | if (ameth == NULL || ameth->old_priv_decode == NULL) | |
152 | goto p8err; | |
153 | ret = d2i_PrivateKey(ameth->pkey_id, x, &p, len); | |
154 | } else if (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) { | |
155 | ret = d2i_PUBKEY(x, &p, len); | |
156 | } else if ((slen = ossl_pem_check_suffix(nm, "PARAMETERS")) > 0) { | |
157 | ret = EVP_PKEY_new(); | |
158 | if (ret == NULL) | |
159 | goto err; | |
160 | if (!EVP_PKEY_set_type_str(ret, nm, slen) | |
161 | || !ret->ameth->param_decode | |
162 | || !ret->ameth->param_decode(ret, &p, len)) { | |
163 | EVP_PKEY_free(ret); | |
164 | ret = NULL; | |
165 | goto err; | |
166 | } | |
167 | if (x) { | |
168 | EVP_PKEY_free(*x); | |
169 | *x = ret; | |
170 | } | |
171 | } | |
172 | ||
173 | p8err: | |
8a709c5e TM |
174 | if (ret == NULL && ERR_peek_last_error() == 0) |
175 | /* ensure some error is reported but do not hide the real one */ | |
7bc027d7 TM |
176 | ERR_raise(ERR_LIB_PEM, ERR_R_ASN1_LIB); |
177 | err: | |
178 | OPENSSL_secure_free(nm); | |
179 | OPENSSL_secure_clear_free(data, len); | |
180 | return ret; | |
181 | } | |
182 | ||
183 | static EVP_PKEY *pem_read_bio_key(BIO *bp, EVP_PKEY **x, | |
184 | pem_password_cb *cb, void *u, | |
185 | OSSL_LIB_CTX *libctx, | |
186 | const char *propq, | |
187 | int selection) | |
188 | { | |
189 | EVP_PKEY *ret; | |
190 | BIO *new_bio = NULL; | |
191 | int pos; | |
192 | ||
193 | if ((pos = BIO_tell(bp)) < 0) { | |
194 | new_bio = BIO_new(BIO_f_readbuffer()); | |
195 | if (new_bio == NULL) | |
196 | return NULL; | |
197 | bp = BIO_push(new_bio, bp); | |
198 | pos = BIO_tell(bp); | |
199 | } | |
200 | ||
201 | ERR_set_mark(); | |
202 | ret = pem_read_bio_key_decoder(bp, x, cb, u, libctx, propq, selection); | |
203 | if (ret == NULL | |
204 | && (BIO_seek(bp, pos) < 0 | |
205 | || (ret = pem_read_bio_key_legacy(bp, x, cb, u, | |
206 | libctx, propq, | |
207 | selection)) == NULL)) | |
208 | ERR_clear_last_mark(); | |
209 | else | |
210 | ERR_pop_to_mark(); | |
211 | ||
212 | if (new_bio != NULL) { | |
213 | BIO_pop(new_bio); | |
214 | BIO_free(new_bio); | |
215 | } | |
216 | return ret; | |
217 | } | |
218 | ||
6e5ccd58 RL |
219 | EVP_PKEY *PEM_read_bio_PUBKEY_ex(BIO *bp, EVP_PKEY **x, |
220 | pem_password_cb *cb, void *u, | |
b4250010 | 221 | OSSL_LIB_CTX *libctx, const char *propq) |
6e5ccd58 RL |
222 | { |
223 | return pem_read_bio_key(bp, x, cb, u, libctx, propq, | |
b4c4a2c6 | 224 | EVP_PKEY_PUBLIC_KEY); |
6e5ccd58 RL |
225 | } |
226 | ||
227 | EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, | |
228 | void *u) | |
229 | { | |
230 | return PEM_read_bio_PUBKEY_ex(bp, x, cb, u, NULL, NULL); | |
231 | } | |
232 | ||
233 | #ifndef OPENSSL_NO_STDIO | |
234 | EVP_PKEY *PEM_read_PUBKEY_ex(FILE *fp, EVP_PKEY **x, | |
235 | pem_password_cb *cb, void *u, | |
b4250010 | 236 | OSSL_LIB_CTX *libctx, const char *propq) |
6e5ccd58 RL |
237 | { |
238 | BIO *b; | |
239 | EVP_PKEY *ret; | |
240 | ||
241 | if ((b = BIO_new(BIO_s_file())) == NULL) { | |
9311d0c4 | 242 | ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB); |
6e5ccd58 RL |
243 | return 0; |
244 | } | |
245 | BIO_set_fp(b, fp, BIO_NOCLOSE); | |
246 | ret = PEM_read_bio_PUBKEY_ex(b, x, cb, u, libctx, propq); | |
247 | BIO_free(b); | |
248 | return ret; | |
249 | } | |
250 | ||
251 | EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u) | |
252 | { | |
253 | return PEM_read_PUBKEY_ex(fp, x, cb, u, NULL, NULL); | |
254 | } | |
255 | #endif | |
256 | ||
257 | EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x, | |
258 | pem_password_cb *cb, void *u, | |
b4250010 | 259 | OSSL_LIB_CTX *libctx, const char *propq) |
6e5ccd58 RL |
260 | { |
261 | return pem_read_bio_key(bp, x, cb, u, libctx, propq, | |
b4c4a2c6 | 262 | EVP_PKEY_KEYPAIR); |
6e5ccd58 RL |
263 | } |
264 | ||
1531241c MC |
265 | EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, |
266 | void *u) | |
267 | { | |
268 | return PEM_read_bio_PrivateKey_ex(bp, x, cb, u, NULL, NULL); | |
269 | } | |
270 | ||
9256e8a2 | 271 | PEM_write_cb_ex_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio) |
0f113f3e | 272 | { |
fe75766c | 273 | IMPLEMENT_PEM_provided_write_body_vars(pkey, PrivateKey, propq); |
f864a939 RL |
274 | |
275 | IMPLEMENT_PEM_provided_write_body_pass(); | |
fe75766c | 276 | IMPLEMENT_PEM_provided_write_body_main(pkey, bio); |
f864a939 RL |
277 | |
278 | legacy: | |
05dba815 | 279 | if (x->ameth == NULL || x->ameth->priv_encode != NULL) |
f864a939 | 280 | return PEM_write_bio_PKCS8PrivateKey(out, x, enc, |
de0799b0 | 281 | (const char *)kstr, klen, cb, u); |
f864a939 | 282 | return PEM_write_bio_PrivateKey_traditional(out, x, enc, kstr, klen, cb, u); |
05dba815 | 283 | } |
e4263314 | 284 | |
9256e8a2 RL |
285 | PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio) |
286 | { | |
287 | return PEM_write_bio_PrivateKey_ex(out, x, enc, kstr, klen, cb, u, | |
288 | NULL, NULL); | |
289 | } | |
290 | ||
4ce1025a RL |
291 | /* |
292 | * Note: there is no way to tell a provided pkey encoder to use "traditional" | |
293 | * encoding. Therefore, if the pkey is provided, we try to take a copy | |
294 | * TODO: when #legacy keys are gone, this function will not be possible any | |
295 | * more and should be removed. | |
296 | */ | |
de0799b0 | 297 | int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x, |
05dba815 | 298 | const EVP_CIPHER *enc, |
de0799b0 | 299 | const unsigned char *kstr, int klen, |
05dba815 DSH |
300 | pem_password_cb *cb, void *u) |
301 | { | |
302 | char pem_str[80]; | |
4ce1025a RL |
303 | EVP_PKEY *copy = NULL; |
304 | int ret; | |
305 | ||
306 | if (evp_pkey_is_assigned(x) | |
307 | && evp_pkey_is_provided(x) | |
308 | && evp_pkey_copy_downgraded(©, x)) | |
309 | x = copy; | |
87d91d22 RL |
310 | |
311 | if (x->ameth == NULL || x->ameth->old_priv_encode == NULL) { | |
312 | ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE); | |
313 | return 0; | |
314 | } | |
0f113f3e | 315 | BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str); |
4ce1025a RL |
316 | ret = PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey, |
317 | pem_str, bp, x, enc, kstr, klen, cb, u); | |
318 | ||
319 | EVP_PKEY_free(copy); | |
320 | return ret; | |
0f113f3e | 321 | } |
e4263314 | 322 | |
6e5ccd58 | 323 | EVP_PKEY *PEM_read_bio_Parameters_ex(BIO *bp, EVP_PKEY **x, |
b4250010 | 324 | OSSL_LIB_CTX *libctx, const char *propq) |
0f113f3e | 325 | { |
6e5ccd58 | 326 | return pem_read_bio_key(bp, x, NULL, NULL, libctx, propq, |
b4c4a2c6 | 327 | EVP_PKEY_KEY_PARAMETERS); |
6e5ccd58 | 328 | } |
1427d33c | 329 | |
6e5ccd58 RL |
330 | EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x) |
331 | { | |
332 | return PEM_read_bio_Parameters_ex(bp, x, NULL, NULL); | |
0f113f3e | 333 | } |
db98bbc1 | 334 | |
f864a939 | 335 | PEM_write_fnsig(Parameters, EVP_PKEY, BIO, write_bio) |
0f113f3e MC |
336 | { |
337 | char pem_str[80]; | |
fe75766c | 338 | IMPLEMENT_PEM_provided_write_body_vars(pkey, Parameters, NULL); |
f864a939 | 339 | |
fe75766c | 340 | IMPLEMENT_PEM_provided_write_body_main(pkey, bio); |
f864a939 RL |
341 | |
342 | legacy: | |
0f113f3e MC |
343 | if (!x->ameth || !x->ameth->param_encode) |
344 | return 0; | |
db98bbc1 | 345 | |
0f113f3e MC |
346 | BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str); |
347 | return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode, | |
f864a939 | 348 | pem_str, out, x, NULL, NULL, 0, 0, NULL); |
0f113f3e | 349 | } |
e4263314 | 350 | |
4b618848 | 351 | #ifndef OPENSSL_NO_STDIO |
1531241c | 352 | EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, |
b4250010 | 353 | void *u, OSSL_LIB_CTX *libctx, |
1531241c | 354 | const char *propq) |
0f113f3e MC |
355 | { |
356 | BIO *b; | |
357 | EVP_PKEY *ret; | |
1241126a | 358 | |
0f113f3e | 359 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
9311d0c4 | 360 | ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB); |
26a7d938 | 361 | return 0; |
0f113f3e MC |
362 | } |
363 | BIO_set_fp(b, fp, BIO_NOCLOSE); | |
1531241c | 364 | ret = PEM_read_bio_PrivateKey_ex(b, x, cb, u, libctx, propq); |
0f113f3e | 365 | BIO_free(b); |
26a7d938 | 366 | return ret; |
0f113f3e | 367 | } |
e4263314 | 368 | |
1531241c MC |
369 | EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, |
370 | void *u) | |
371 | { | |
372 | return PEM_read_PrivateKey_ex(fp, x, cb, u, NULL, NULL); | |
373 | } | |
374 | ||
9256e8a2 | 375 | PEM_write_cb_ex_fnsig(PrivateKey, EVP_PKEY, FILE, write) |
0f113f3e MC |
376 | { |
377 | BIO *b; | |
378 | int ret; | |
e4263314 | 379 | |
9256e8a2 | 380 | if ((b = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) { |
9311d0c4 | 381 | ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB); |
0f113f3e MC |
382 | return 0; |
383 | } | |
9256e8a2 RL |
384 | ret = PEM_write_bio_PrivateKey_ex(b, x, enc, kstr, klen, cb, u, |
385 | libctx, propq); | |
0f113f3e MC |
386 | BIO_free(b); |
387 | return ret; | |
388 | } | |
e4263314 | 389 | |
9256e8a2 RL |
390 | PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, FILE, write) |
391 | { | |
392 | return PEM_write_PrivateKey_ex(out, x, enc, kstr, klen, cb, u, NULL, NULL); | |
393 | } | |
1241126a | 394 | #endif |