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