]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright 2005-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 | /* | |
11 | * Support for PVK format keys and related structures (such a PUBLICKEYBLOB | |
12 | * and PRIVATEKEYBLOB). | |
13 | */ | |
14 | ||
15 | /* | |
16 | * RSA and DSA low level APIs are deprecated for public use, but still ok for | |
17 | * internal use. | |
18 | */ | |
19 | #include "internal/deprecated.h" | |
20 | ||
21 | #include <openssl/pem.h> | |
22 | #include <openssl/rand.h> | |
23 | #include <openssl/bn.h> | |
24 | #include <openssl/dsa.h> | |
25 | #include <openssl/rsa.h> | |
26 | #include "internal/cryptlib.h" | |
27 | #include "crypto/pem.h" | |
28 | #include "crypto/evp.h" | |
29 | ||
30 | /* | |
31 | * Utility function: read a DWORD (4 byte unsigned integer) in little endian | |
32 | * format | |
33 | */ | |
34 | ||
35 | static unsigned int read_ledword(const unsigned char **in) | |
36 | { | |
37 | const unsigned char *p = *in; | |
38 | unsigned int ret; | |
39 | ||
40 | ret = (unsigned int)*p++; | |
41 | ret |= (unsigned int)*p++ << 8; | |
42 | ret |= (unsigned int)*p++ << 16; | |
43 | ret |= (unsigned int)*p++ << 24; | |
44 | *in = p; | |
45 | return ret; | |
46 | } | |
47 | ||
48 | /* | |
49 | * Read a BIGNUM in little endian format. The docs say that this should take | |
50 | * up bitlen/8 bytes. | |
51 | */ | |
52 | ||
53 | static int read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r) | |
54 | { | |
55 | *r = BN_lebin2bn(*in, nbyte, NULL); | |
56 | if (*r == NULL) | |
57 | return 0; | |
58 | *in += nbyte; | |
59 | return 1; | |
60 | } | |
61 | ||
62 | /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */ | |
63 | ||
64 | # define MS_PUBLICKEYBLOB 0x6 | |
65 | # define MS_PRIVATEKEYBLOB 0x7 | |
66 | # define MS_RSA1MAGIC 0x31415352L | |
67 | # define MS_RSA2MAGIC 0x32415352L | |
68 | # define MS_DSS1MAGIC 0x31535344L | |
69 | # define MS_DSS2MAGIC 0x32535344L | |
70 | ||
71 | # define MS_KEYALG_RSA_KEYX 0xa400 | |
72 | # define MS_KEYALG_DSS_SIGN 0x2200 | |
73 | ||
74 | # define MS_KEYTYPE_KEYX 0x1 | |
75 | # define MS_KEYTYPE_SIGN 0x2 | |
76 | ||
77 | /* Maximum length of a blob after header */ | |
78 | # define BLOB_MAX_LENGTH 102400 | |
79 | ||
80 | /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */ | |
81 | # define MS_PVKMAGIC 0xb0b5f11eL | |
82 | /* Salt length for PVK files */ | |
83 | # define PVK_SALTLEN 0x10 | |
84 | /* Maximum length in PVK header */ | |
85 | # define PVK_MAX_KEYLEN 102400 | |
86 | /* Maximum salt length */ | |
87 | # define PVK_MAX_SALTLEN 10240 | |
88 | ||
89 | static EVP_PKEY *b2i_rsa(const unsigned char **in, | |
90 | unsigned int bitlen, int ispub); | |
91 | #ifndef OPENSSL_NO_DSA | |
92 | static EVP_PKEY *b2i_dss(const unsigned char **in, | |
93 | unsigned int bitlen, int ispub); | |
94 | #endif | |
95 | ||
96 | int ossl_do_blob_header(const unsigned char **in, unsigned int length, | |
97 | unsigned int *pmagic, unsigned int *pbitlen, | |
98 | int *pisdss, int *pispub) | |
99 | { | |
100 | const unsigned char *p = *in; | |
101 | ||
102 | if (length < 16) | |
103 | return 0; | |
104 | /* bType */ | |
105 | if (*p == MS_PUBLICKEYBLOB) { | |
106 | if (*pispub == 0) { | |
107 | ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB); | |
108 | return 0; | |
109 | } | |
110 | *pispub = 1; | |
111 | } else if (*p == MS_PRIVATEKEYBLOB) { | |
112 | if (*pispub == 1) { | |
113 | ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB); | |
114 | return 0; | |
115 | } | |
116 | *pispub = 0; | |
117 | } else { | |
118 | return 0; | |
119 | } | |
120 | p++; | |
121 | /* Version */ | |
122 | if (*p++ != 0x2) { | |
123 | ERR_raise(ERR_LIB_PEM, PEM_R_BAD_VERSION_NUMBER); | |
124 | return 0; | |
125 | } | |
126 | /* Ignore reserved, aiKeyAlg */ | |
127 | p += 6; | |
128 | *pmagic = read_ledword(&p); | |
129 | *pbitlen = read_ledword(&p); | |
130 | *pisdss = 0; | |
131 | switch (*pmagic) { | |
132 | ||
133 | case MS_DSS1MAGIC: | |
134 | *pisdss = 1; | |
135 | /* fall thru */ | |
136 | case MS_RSA1MAGIC: | |
137 | if (*pispub == 0) { | |
138 | ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB); | |
139 | return 0; | |
140 | } | |
141 | break; | |
142 | ||
143 | case MS_DSS2MAGIC: | |
144 | *pisdss = 1; | |
145 | /* fall thru */ | |
146 | case MS_RSA2MAGIC: | |
147 | if (*pispub == 1) { | |
148 | ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB); | |
149 | return 0; | |
150 | } | |
151 | break; | |
152 | ||
153 | default: | |
154 | ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER); | |
155 | return -1; | |
156 | } | |
157 | *in = p; | |
158 | return 1; | |
159 | } | |
160 | ||
161 | static unsigned int blob_length(unsigned bitlen, int isdss, int ispub) | |
162 | { | |
163 | unsigned int nbyte = (bitlen + 7) >> 3; | |
164 | unsigned int hnbyte = (bitlen + 15) >> 4; | |
165 | ||
166 | if (isdss) { | |
167 | ||
168 | /* | |
169 | * Expected length: 20 for q + 3 components bitlen each + 24 for seed | |
170 | * structure. | |
171 | */ | |
172 | if (ispub) | |
173 | return 44 + 3 * nbyte; | |
174 | /* | |
175 | * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed | |
176 | * structure. | |
177 | */ | |
178 | else | |
179 | return 64 + 2 * nbyte; | |
180 | } else { | |
181 | /* Expected length: 4 for 'e' + 'n' */ | |
182 | if (ispub) | |
183 | return 4 + nbyte; | |
184 | else | |
185 | /* | |
186 | * Expected length: 4 for 'e' and 7 other components. 2 | |
187 | * components are bitlen size, 5 are bitlen/2 | |
188 | */ | |
189 | return 4 + 2 * nbyte + 5 * hnbyte; | |
190 | } | |
191 | ||
192 | } | |
193 | ||
194 | EVP_PKEY *ossl_b2i(const unsigned char **in, unsigned int length, int *ispub) | |
195 | { | |
196 | const unsigned char *p = *in; | |
197 | unsigned int bitlen, magic; | |
198 | int isdss; | |
199 | ||
200 | if (ossl_do_blob_header(&p, length, &magic, &bitlen, &isdss, ispub) <= 0) { | |
201 | ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_HEADER_PARSE_ERROR); | |
202 | return NULL; | |
203 | } | |
204 | length -= 16; | |
205 | if (length < blob_length(bitlen, isdss, *ispub)) { | |
206 | ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT); | |
207 | return NULL; | |
208 | } | |
209 | if (!isdss) | |
210 | return b2i_rsa(&p, bitlen, *ispub); | |
211 | #ifndef OPENSSL_NO_DSA | |
212 | else | |
213 | return b2i_dss(&p, bitlen, *ispub); | |
214 | #endif | |
215 | ||
216 | ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE); | |
217 | return NULL; | |
218 | } | |
219 | ||
220 | EVP_PKEY *ossl_b2i_bio(BIO *in, int *ispub) | |
221 | { | |
222 | const unsigned char *p; | |
223 | unsigned char hdr_buf[16], *buf = NULL; | |
224 | unsigned int bitlen, magic, length; | |
225 | int isdss; | |
226 | EVP_PKEY *ret = NULL; | |
227 | ||
228 | if (BIO_read(in, hdr_buf, 16) != 16) { | |
229 | ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT); | |
230 | return NULL; | |
231 | } | |
232 | p = hdr_buf; | |
233 | if (ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, ispub) <= 0) | |
234 | return NULL; | |
235 | ||
236 | length = blob_length(bitlen, isdss, *ispub); | |
237 | if (length > BLOB_MAX_LENGTH) { | |
238 | ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG); | |
239 | return NULL; | |
240 | } | |
241 | buf = OPENSSL_malloc(length); | |
242 | if (buf == NULL) { | |
243 | ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE); | |
244 | goto err; | |
245 | } | |
246 | p = buf; | |
247 | if (BIO_read(in, buf, length) != (int)length) { | |
248 | ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT); | |
249 | goto err; | |
250 | } | |
251 | ||
252 | if (!isdss) | |
253 | ret = b2i_rsa(&p, bitlen, *ispub); | |
254 | #ifndef OPENSSL_NO_DSA | |
255 | else | |
256 | ret = b2i_dss(&p, bitlen, *ispub); | |
257 | #endif | |
258 | ||
259 | if (ret == NULL) | |
260 | ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE); | |
261 | ||
262 | err: | |
263 | OPENSSL_free(buf); | |
264 | return ret; | |
265 | } | |
266 | ||
267 | #ifndef OPENSSL_NO_DSA | |
268 | static EVP_PKEY *b2i_dss(const unsigned char **in, | |
269 | unsigned int bitlen, int ispub) | |
270 | { | |
271 | const unsigned char *p = *in; | |
272 | EVP_PKEY *ret = NULL; | |
273 | DSA *dsa = NULL; | |
274 | BN_CTX *ctx = NULL; | |
275 | BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL; | |
276 | BIGNUM *pub_key = NULL; | |
277 | unsigned int nbyte = (bitlen + 7) >> 3; | |
278 | ||
279 | dsa = DSA_new(); | |
280 | ret = EVP_PKEY_new(); | |
281 | if (dsa == NULL || ret == NULL) | |
282 | goto memerr; | |
283 | if (!read_lebn(&p, nbyte, &pbn)) | |
284 | goto memerr; | |
285 | ||
286 | if (!read_lebn(&p, 20, &qbn)) | |
287 | goto memerr; | |
288 | ||
289 | if (!read_lebn(&p, nbyte, &gbn)) | |
290 | goto memerr; | |
291 | ||
292 | if (ispub) { | |
293 | if (!read_lebn(&p, nbyte, &pub_key)) | |
294 | goto memerr; | |
295 | } else { | |
296 | if (!read_lebn(&p, 20, &priv_key)) | |
297 | goto memerr; | |
298 | ||
299 | /* Set constant time flag before public key calculation */ | |
300 | BN_set_flags(priv_key, BN_FLG_CONSTTIME); | |
301 | ||
302 | /* Calculate public key */ | |
303 | pub_key = BN_new(); | |
304 | if (pub_key == NULL) | |
305 | goto memerr; | |
306 | if ((ctx = BN_CTX_new()) == NULL) | |
307 | goto memerr; | |
308 | ||
309 | if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx)) | |
310 | goto memerr; | |
311 | ||
312 | BN_CTX_free(ctx); | |
313 | ctx = NULL; | |
314 | } | |
315 | if (!DSA_set0_pqg(dsa, pbn, qbn, gbn)) | |
316 | goto memerr; | |
317 | pbn = qbn = gbn = NULL; | |
318 | if (!DSA_set0_key(dsa, pub_key, priv_key)) | |
319 | goto memerr; | |
320 | pub_key = priv_key = NULL; | |
321 | ||
322 | if (!EVP_PKEY_set1_DSA(ret, dsa)) | |
323 | goto memerr; | |
324 | DSA_free(dsa); | |
325 | *in = p; | |
326 | return ret; | |
327 | ||
328 | memerr: | |
329 | ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE); | |
330 | DSA_free(dsa); | |
331 | BN_free(pbn); | |
332 | BN_free(qbn); | |
333 | BN_free(gbn); | |
334 | BN_free(pub_key); | |
335 | BN_free(priv_key); | |
336 | EVP_PKEY_free(ret); | |
337 | BN_CTX_free(ctx); | |
338 | return NULL; | |
339 | } | |
340 | #endif | |
341 | ||
342 | static EVP_PKEY *b2i_rsa(const unsigned char **in, | |
343 | unsigned int bitlen, int ispub) | |
344 | { | |
345 | const unsigned char *pin = *in; | |
346 | EVP_PKEY *ret = NULL; | |
347 | BIGNUM *e = NULL, *n = NULL, *d = NULL; | |
348 | BIGNUM *p = NULL, *q = NULL, *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL; | |
349 | RSA *rsa = NULL; | |
350 | unsigned int nbyte = (bitlen + 7) >> 3; | |
351 | unsigned int hnbyte = (bitlen + 15) >> 4; | |
352 | ||
353 | rsa = RSA_new(); | |
354 | ret = EVP_PKEY_new(); | |
355 | if (rsa == NULL || ret == NULL) | |
356 | goto memerr; | |
357 | e = BN_new(); | |
358 | if (e == NULL) | |
359 | goto memerr; | |
360 | if (!BN_set_word(e, read_ledword(&pin))) | |
361 | goto memerr; | |
362 | if (!read_lebn(&pin, nbyte, &n)) | |
363 | goto memerr; | |
364 | if (!ispub) { | |
365 | if (!read_lebn(&pin, hnbyte, &p)) | |
366 | goto memerr; | |
367 | if (!read_lebn(&pin, hnbyte, &q)) | |
368 | goto memerr; | |
369 | if (!read_lebn(&pin, hnbyte, &dmp1)) | |
370 | goto memerr; | |
371 | if (!read_lebn(&pin, hnbyte, &dmq1)) | |
372 | goto memerr; | |
373 | if (!read_lebn(&pin, hnbyte, &iqmp)) | |
374 | goto memerr; | |
375 | if (!read_lebn(&pin, nbyte, &d)) | |
376 | goto memerr; | |
377 | if (!RSA_set0_factors(rsa, p, q)) | |
378 | goto memerr; | |
379 | p = q = NULL; | |
380 | if (!RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp)) | |
381 | goto memerr; | |
382 | dmp1 = dmq1 = iqmp = NULL; | |
383 | } | |
384 | if (!RSA_set0_key(rsa, n, e, d)) | |
385 | goto memerr; | |
386 | n = e = d = NULL; | |
387 | ||
388 | if (!EVP_PKEY_set1_RSA(ret, rsa)) | |
389 | goto memerr; | |
390 | RSA_free(rsa); | |
391 | *in = pin; | |
392 | return ret; | |
393 | memerr: | |
394 | ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE); | |
395 | BN_free(e); | |
396 | BN_free(n); | |
397 | BN_free(p); | |
398 | BN_free(q); | |
399 | BN_free(dmp1); | |
400 | BN_free(dmq1); | |
401 | BN_free(iqmp); | |
402 | BN_free(d); | |
403 | RSA_free(rsa); | |
404 | EVP_PKEY_free(ret); | |
405 | return NULL; | |
406 | } | |
407 | ||
408 | EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length) | |
409 | { | |
410 | int ispub = 0; | |
411 | ||
412 | return ossl_b2i(in, length, &ispub); | |
413 | } | |
414 | ||
415 | EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length) | |
416 | { | |
417 | int ispub = 1; | |
418 | ||
419 | return ossl_b2i(in, length, &ispub); | |
420 | } | |
421 | ||
422 | EVP_PKEY *b2i_PrivateKey_bio(BIO *in) | |
423 | { | |
424 | int ispub = 0; | |
425 | ||
426 | return ossl_b2i_bio(in, &ispub); | |
427 | } | |
428 | ||
429 | EVP_PKEY *b2i_PublicKey_bio(BIO *in) | |
430 | { | |
431 | int ispub = 1; | |
432 | ||
433 | return ossl_b2i_bio(in, &ispub); | |
434 | } | |
435 | ||
436 | static void write_ledword(unsigned char **out, unsigned int dw) | |
437 | { | |
438 | unsigned char *p = *out; | |
439 | ||
440 | *p++ = dw & 0xff; | |
441 | *p++ = (dw >> 8) & 0xff; | |
442 | *p++ = (dw >> 16) & 0xff; | |
443 | *p++ = (dw >> 24) & 0xff; | |
444 | *out = p; | |
445 | } | |
446 | ||
447 | static void write_lebn(unsigned char **out, const BIGNUM *bn, int len) | |
448 | { | |
449 | BN_bn2lebinpad(bn, *out, len); | |
450 | *out += len; | |
451 | } | |
452 | ||
453 | static int check_bitlen_rsa(const RSA *rsa, int ispub, unsigned int *magic); | |
454 | static void write_rsa(unsigned char **out, const RSA *rsa, int ispub); | |
455 | ||
456 | #ifndef OPENSSL_NO_DSA | |
457 | static int check_bitlen_dsa(const DSA *dsa, int ispub, unsigned int *magic); | |
458 | static void write_dsa(unsigned char **out, const DSA *dsa, int ispub); | |
459 | #endif | |
460 | ||
461 | static int do_i2b(unsigned char **out, const EVP_PKEY *pk, int ispub) | |
462 | { | |
463 | unsigned char *p; | |
464 | unsigned int bitlen = 0, magic = 0, keyalg = 0; | |
465 | int outlen = -1, noinc = 0; | |
466 | int pktype; | |
467 | #ifndef OPENSSL_NO_PROVIDER_CODE | |
468 | EVP_PKEY *pkcopy = NULL; | |
469 | ||
470 | if (evp_pkey_is_provided(pk)) { | |
471 | if (!evp_pkey_copy_downgraded(&pkcopy, pk)) | |
472 | goto end; | |
473 | pk = pkcopy; | |
474 | } | |
475 | #endif | |
476 | ||
477 | pktype = EVP_PKEY_id(pk); | |
478 | if (pktype == EVP_PKEY_RSA) { | |
479 | bitlen = check_bitlen_rsa(EVP_PKEY_get0_RSA(pk), ispub, &magic); | |
480 | keyalg = MS_KEYALG_RSA_KEYX; | |
481 | #ifndef OPENSSL_NO_DSA | |
482 | } else if (pktype == EVP_PKEY_DSA) { | |
483 | bitlen = check_bitlen_dsa(EVP_PKEY_get0_DSA(pk), ispub, &magic); | |
484 | keyalg = MS_KEYALG_DSS_SIGN; | |
485 | #endif | |
486 | } | |
487 | if (bitlen == 0) { | |
488 | goto end; | |
489 | } | |
490 | outlen = 16 + blob_length(bitlen, | |
491 | keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub); | |
492 | if (out == NULL) | |
493 | goto end; | |
494 | if (*out) | |
495 | p = *out; | |
496 | else { | |
497 | if ((p = OPENSSL_malloc(outlen)) == NULL) { | |
498 | ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE); | |
499 | outlen = -1; | |
500 | goto end; | |
501 | } | |
502 | *out = p; | |
503 | noinc = 1; | |
504 | } | |
505 | if (ispub) | |
506 | *p++ = MS_PUBLICKEYBLOB; | |
507 | else | |
508 | *p++ = MS_PRIVATEKEYBLOB; | |
509 | *p++ = 0x2; | |
510 | *p++ = 0; | |
511 | *p++ = 0; | |
512 | write_ledword(&p, keyalg); | |
513 | write_ledword(&p, magic); | |
514 | write_ledword(&p, bitlen); | |
515 | if (keyalg == MS_KEYALG_RSA_KEYX) | |
516 | write_rsa(&p, EVP_PKEY_get0_RSA(pk), ispub); | |
517 | #ifndef OPENSSL_NO_DSA | |
518 | else | |
519 | write_dsa(&p, EVP_PKEY_get0_DSA(pk), ispub); | |
520 | #endif | |
521 | if (!noinc) | |
522 | *out += outlen; | |
523 | end: | |
524 | #ifndef OPENSSL_NO_PROVIDER_CODE | |
525 | EVP_PKEY_free(pkcopy); | |
526 | #endif | |
527 | return outlen; | |
528 | } | |
529 | ||
530 | static int do_i2b_bio(BIO *out, const EVP_PKEY *pk, int ispub) | |
531 | { | |
532 | unsigned char *tmp = NULL; | |
533 | int outlen, wrlen; | |
534 | ||
535 | outlen = do_i2b(&tmp, pk, ispub); | |
536 | if (outlen < 0) | |
537 | return -1; | |
538 | wrlen = BIO_write(out, tmp, outlen); | |
539 | OPENSSL_free(tmp); | |
540 | if (wrlen == outlen) | |
541 | return outlen; | |
542 | return -1; | |
543 | } | |
544 | ||
545 | static int check_bitlen_rsa(const RSA *rsa, int ispub, unsigned int *pmagic) | |
546 | { | |
547 | int nbyte, hnbyte, bitlen; | |
548 | const BIGNUM *e; | |
549 | ||
550 | RSA_get0_key(rsa, NULL, &e, NULL); | |
551 | if (BN_num_bits(e) > 32) | |
552 | goto badkey; | |
553 | bitlen = RSA_bits(rsa); | |
554 | nbyte = RSA_size(rsa); | |
555 | hnbyte = (bitlen + 15) >> 4; | |
556 | if (ispub) { | |
557 | *pmagic = MS_RSA1MAGIC; | |
558 | return bitlen; | |
559 | } else { | |
560 | const BIGNUM *d, *p, *q, *iqmp, *dmp1, *dmq1; | |
561 | ||
562 | *pmagic = MS_RSA2MAGIC; | |
563 | ||
564 | /* | |
565 | * For private key each component must fit within nbyte or hnbyte. | |
566 | */ | |
567 | RSA_get0_key(rsa, NULL, NULL, &d); | |
568 | if (BN_num_bytes(d) > nbyte) | |
569 | goto badkey; | |
570 | RSA_get0_factors(rsa, &p, &q); | |
571 | RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp); | |
572 | if ((BN_num_bytes(iqmp) > hnbyte) | |
573 | || (BN_num_bytes(p) > hnbyte) | |
574 | || (BN_num_bytes(q) > hnbyte) | |
575 | || (BN_num_bytes(dmp1) > hnbyte) | |
576 | || (BN_num_bytes(dmq1) > hnbyte)) | |
577 | goto badkey; | |
578 | } | |
579 | return bitlen; | |
580 | badkey: | |
581 | ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS); | |
582 | return 0; | |
583 | } | |
584 | ||
585 | static void write_rsa(unsigned char **out, const RSA *rsa, int ispub) | |
586 | { | |
587 | int nbyte, hnbyte; | |
588 | const BIGNUM *n, *d, *e, *p, *q, *iqmp, *dmp1, *dmq1; | |
589 | ||
590 | nbyte = RSA_size(rsa); | |
591 | hnbyte = (RSA_bits(rsa) + 15) >> 4; | |
592 | RSA_get0_key(rsa, &n, &e, &d); | |
593 | write_lebn(out, e, 4); | |
594 | write_lebn(out, n, nbyte); | |
595 | if (ispub) | |
596 | return; | |
597 | RSA_get0_factors(rsa, &p, &q); | |
598 | RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp); | |
599 | write_lebn(out, p, hnbyte); | |
600 | write_lebn(out, q, hnbyte); | |
601 | write_lebn(out, dmp1, hnbyte); | |
602 | write_lebn(out, dmq1, hnbyte); | |
603 | write_lebn(out, iqmp, hnbyte); | |
604 | write_lebn(out, d, nbyte); | |
605 | } | |
606 | ||
607 | #ifndef OPENSSL_NO_DSA | |
608 | static int check_bitlen_dsa(const DSA *dsa, int ispub, unsigned int *pmagic) | |
609 | { | |
610 | int bitlen; | |
611 | const BIGNUM *p = NULL, *q = NULL, *g = NULL; | |
612 | const BIGNUM *pub_key = NULL, *priv_key = NULL; | |
613 | ||
614 | DSA_get0_pqg(dsa, &p, &q, &g); | |
615 | DSA_get0_key(dsa, &pub_key, &priv_key); | |
616 | bitlen = BN_num_bits(p); | |
617 | if ((bitlen & 7) || (BN_num_bits(q) != 160) | |
618 | || (BN_num_bits(g) > bitlen)) | |
619 | goto badkey; | |
620 | if (ispub) { | |
621 | if (BN_num_bits(pub_key) > bitlen) | |
622 | goto badkey; | |
623 | *pmagic = MS_DSS1MAGIC; | |
624 | } else { | |
625 | if (BN_num_bits(priv_key) > 160) | |
626 | goto badkey; | |
627 | *pmagic = MS_DSS2MAGIC; | |
628 | } | |
629 | ||
630 | return bitlen; | |
631 | badkey: | |
632 | ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS); | |
633 | return 0; | |
634 | } | |
635 | ||
636 | static void write_dsa(unsigned char **out, const DSA *dsa, int ispub) | |
637 | { | |
638 | int nbyte; | |
639 | const BIGNUM *p = NULL, *q = NULL, *g = NULL; | |
640 | const BIGNUM *pub_key = NULL, *priv_key = NULL; | |
641 | ||
642 | DSA_get0_pqg(dsa, &p, &q, &g); | |
643 | DSA_get0_key(dsa, &pub_key, &priv_key); | |
644 | nbyte = BN_num_bytes(p); | |
645 | write_lebn(out, p, nbyte); | |
646 | write_lebn(out, q, 20); | |
647 | write_lebn(out, g, nbyte); | |
648 | if (ispub) | |
649 | write_lebn(out, pub_key, nbyte); | |
650 | else | |
651 | write_lebn(out, priv_key, 20); | |
652 | /* Set "invalid" for seed structure values */ | |
653 | memset(*out, 0xff, 24); | |
654 | *out += 24; | |
655 | return; | |
656 | } | |
657 | #endif | |
658 | ||
659 | int i2b_PrivateKey_bio(BIO *out, const EVP_PKEY *pk) | |
660 | { | |
661 | return do_i2b_bio(out, pk, 0); | |
662 | } | |
663 | ||
664 | int i2b_PublicKey_bio(BIO *out, const EVP_PKEY *pk) | |
665 | { | |
666 | return do_i2b_bio(out, pk, 1); | |
667 | } | |
668 | ||
669 | int ossl_do_PVK_header(const unsigned char **in, unsigned int length, | |
670 | int skip_magic, | |
671 | unsigned int *psaltlen, unsigned int *pkeylen) | |
672 | { | |
673 | const unsigned char *p = *in; | |
674 | unsigned int pvk_magic, is_encrypted; | |
675 | ||
676 | if (skip_magic) { | |
677 | if (length < 20) { | |
678 | ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT); | |
679 | return 0; | |
680 | } | |
681 | } else { | |
682 | if (length < 24) { | |
683 | ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT); | |
684 | return 0; | |
685 | } | |
686 | pvk_magic = read_ledword(&p); | |
687 | if (pvk_magic != MS_PVKMAGIC) { | |
688 | ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER); | |
689 | return 0; | |
690 | } | |
691 | } | |
692 | /* Skip reserved */ | |
693 | p += 4; | |
694 | /* | |
695 | * keytype = | |
696 | */ read_ledword(&p); | |
697 | is_encrypted = read_ledword(&p); | |
698 | *psaltlen = read_ledword(&p); | |
699 | *pkeylen = read_ledword(&p); | |
700 | ||
701 | if (*pkeylen > PVK_MAX_KEYLEN || *psaltlen > PVK_MAX_SALTLEN) | |
702 | return 0; | |
703 | ||
704 | if (is_encrypted && *psaltlen == 0) { | |
705 | ERR_raise(ERR_LIB_PEM, PEM_R_INCONSISTENT_HEADER); | |
706 | return 0; | |
707 | } | |
708 | ||
709 | *in = p; | |
710 | return 1; | |
711 | } | |
712 | ||
713 | #ifndef OPENSSL_NO_RC4 | |
714 | static int derive_pvk_key(unsigned char *key, | |
715 | const unsigned char *salt, unsigned int saltlen, | |
716 | const unsigned char *pass, int passlen) | |
717 | { | |
718 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); | |
719 | int rv = 1; | |
720 | ||
721 | if (mctx == NULL | |
722 | || !EVP_DigestInit_ex(mctx, EVP_sha1(), NULL) | |
723 | || !EVP_DigestUpdate(mctx, salt, saltlen) | |
724 | || !EVP_DigestUpdate(mctx, pass, passlen) | |
725 | || !EVP_DigestFinal_ex(mctx, key, NULL)) | |
726 | rv = 0; | |
727 | ||
728 | EVP_MD_CTX_free(mctx); | |
729 | return rv; | |
730 | } | |
731 | #endif | |
732 | ||
733 | static EVP_PKEY *do_PVK_body(const unsigned char **in, | |
734 | unsigned int saltlen, unsigned int keylen, | |
735 | pem_password_cb *cb, void *u) | |
736 | { | |
737 | EVP_PKEY *ret = NULL; | |
738 | const unsigned char *p = *in; | |
739 | unsigned char *enctmp = NULL; | |
740 | unsigned char keybuf[20]; | |
741 | ||
742 | EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new(); | |
743 | if (saltlen) { | |
744 | #ifndef OPENSSL_NO_RC4 | |
745 | unsigned int magic; | |
746 | char psbuf[PEM_BUFSIZE]; | |
747 | int enctmplen, inlen; | |
748 | unsigned char *q; | |
749 | ||
750 | if (cb) | |
751 | inlen = cb(psbuf, PEM_BUFSIZE, 0, u); | |
752 | else | |
753 | inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u); | |
754 | if (inlen < 0) { | |
755 | ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ); | |
756 | goto err; | |
757 | } | |
758 | enctmp = OPENSSL_malloc(keylen + 8); | |
759 | if (enctmp == NULL) { | |
760 | ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE); | |
761 | goto err; | |
762 | } | |
763 | if (!derive_pvk_key(keybuf, p, saltlen, | |
764 | (unsigned char *)psbuf, inlen)) | |
765 | goto err; | |
766 | p += saltlen; | |
767 | /* Copy BLOBHEADER across, decrypt rest */ | |
768 | memcpy(enctmp, p, 8); | |
769 | p += 8; | |
770 | if (keylen < 8) { | |
771 | ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT); | |
772 | goto err; | |
773 | } | |
774 | inlen = keylen - 8; | |
775 | q = enctmp + 8; | |
776 | if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL)) | |
777 | goto err; | |
778 | if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen)) | |
779 | goto err; | |
780 | if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen)) | |
781 | goto err; | |
782 | magic = read_ledword((const unsigned char **)&q); | |
783 | if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) { | |
784 | q = enctmp + 8; | |
785 | memset(keybuf + 5, 0, 11); | |
786 | if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL)) | |
787 | goto err; | |
788 | if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen)) | |
789 | goto err; | |
790 | if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen)) | |
791 | goto err; | |
792 | magic = read_ledword((const unsigned char **)&q); | |
793 | if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) { | |
794 | ERR_raise(ERR_LIB_PEM, PEM_R_BAD_DECRYPT); | |
795 | goto err; | |
796 | } | |
797 | } | |
798 | p = enctmp; | |
799 | #else | |
800 | ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER); | |
801 | goto err; | |
802 | #endif | |
803 | } | |
804 | ||
805 | ret = b2i_PrivateKey(&p, keylen); | |
806 | err: | |
807 | EVP_CIPHER_CTX_free(cctx); | |
808 | if (enctmp != NULL) { | |
809 | OPENSSL_cleanse(keybuf, sizeof(keybuf)); | |
810 | OPENSSL_free(enctmp); | |
811 | } | |
812 | return ret; | |
813 | } | |
814 | ||
815 | EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u) | |
816 | { | |
817 | unsigned char pvk_hdr[24], *buf = NULL; | |
818 | const unsigned char *p; | |
819 | int buflen; | |
820 | EVP_PKEY *ret = NULL; | |
821 | unsigned int saltlen, keylen; | |
822 | ||
823 | if (BIO_read(in, pvk_hdr, 24) != 24) { | |
824 | ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT); | |
825 | return NULL; | |
826 | } | |
827 | p = pvk_hdr; | |
828 | ||
829 | if (!ossl_do_PVK_header(&p, 24, 0, &saltlen, &keylen)) | |
830 | return 0; | |
831 | buflen = (int)keylen + saltlen; | |
832 | buf = OPENSSL_malloc(buflen); | |
833 | if (buf == NULL) { | |
834 | ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE); | |
835 | return 0; | |
836 | } | |
837 | p = buf; | |
838 | if (BIO_read(in, buf, buflen) != buflen) { | |
839 | ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT); | |
840 | goto err; | |
841 | } | |
842 | ret = do_PVK_body(&p, saltlen, keylen, cb, u); | |
843 | ||
844 | err: | |
845 | OPENSSL_clear_free(buf, buflen); | |
846 | return ret; | |
847 | } | |
848 | ||
849 | static int i2b_PVK(unsigned char **out, const EVP_PKEY *pk, int enclevel, | |
850 | pem_password_cb *cb, void *u) | |
851 | { | |
852 | int outlen = 24, pklen; | |
853 | unsigned char *p = NULL, *start = NULL; | |
854 | EVP_CIPHER_CTX *cctx = NULL; | |
855 | #ifndef OPENSSL_NO_RC4 | |
856 | unsigned char *salt = NULL; | |
857 | #endif | |
858 | ||
859 | if (enclevel) | |
860 | outlen += PVK_SALTLEN; | |
861 | pklen = do_i2b(NULL, pk, 0); | |
862 | if (pklen < 0) | |
863 | return -1; | |
864 | outlen += pklen; | |
865 | if (out == NULL) | |
866 | return outlen; | |
867 | if (*out != NULL) { | |
868 | p = *out; | |
869 | } else { | |
870 | start = p = OPENSSL_malloc(outlen); | |
871 | if (p == NULL) { | |
872 | ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE); | |
873 | return -1; | |
874 | } | |
875 | } | |
876 | ||
877 | cctx = EVP_CIPHER_CTX_new(); | |
878 | if (cctx == NULL) | |
879 | goto error; | |
880 | ||
881 | write_ledword(&p, MS_PVKMAGIC); | |
882 | write_ledword(&p, 0); | |
883 | if (EVP_PKEY_id(pk) == EVP_PKEY_RSA) | |
884 | write_ledword(&p, MS_KEYTYPE_KEYX); | |
885 | #ifndef OPENSSL_NO_DSA | |
886 | else | |
887 | write_ledword(&p, MS_KEYTYPE_SIGN); | |
888 | #endif | |
889 | write_ledword(&p, enclevel ? 1 : 0); | |
890 | write_ledword(&p, enclevel ? PVK_SALTLEN : 0); | |
891 | write_ledword(&p, pklen); | |
892 | if (enclevel) { | |
893 | #ifndef OPENSSL_NO_RC4 | |
894 | if (RAND_bytes(p, PVK_SALTLEN) <= 0) | |
895 | goto error; | |
896 | salt = p; | |
897 | p += PVK_SALTLEN; | |
898 | #endif | |
899 | } | |
900 | do_i2b(&p, pk, 0); | |
901 | if (enclevel != 0) { | |
902 | #ifndef OPENSSL_NO_RC4 | |
903 | char psbuf[PEM_BUFSIZE]; | |
904 | unsigned char keybuf[20]; | |
905 | int enctmplen, inlen; | |
906 | ||
907 | if (cb) | |
908 | inlen = cb(psbuf, PEM_BUFSIZE, 1, u); | |
909 | else | |
910 | inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u); | |
911 | if (inlen <= 0) { | |
912 | ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ); | |
913 | goto error; | |
914 | } | |
915 | if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN, | |
916 | (unsigned char *)psbuf, inlen)) | |
917 | goto error; | |
918 | if (enclevel == 1) | |
919 | memset(keybuf + 5, 0, 11); | |
920 | p = salt + PVK_SALTLEN + 8; | |
921 | if (!EVP_EncryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL)) | |
922 | goto error; | |
923 | OPENSSL_cleanse(keybuf, 20); | |
924 | if (!EVP_EncryptUpdate(cctx, p, &enctmplen, p, pklen - 8)) | |
925 | goto error; | |
926 | if (!EVP_EncryptFinal_ex(cctx, p + enctmplen, &enctmplen)) | |
927 | goto error; | |
928 | #else | |
929 | ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER); | |
930 | goto error; | |
931 | #endif | |
932 | } | |
933 | ||
934 | EVP_CIPHER_CTX_free(cctx); | |
935 | ||
936 | if (*out == NULL) | |
937 | *out = start; | |
938 | ||
939 | return outlen; | |
940 | ||
941 | error: | |
942 | EVP_CIPHER_CTX_free(cctx); | |
943 | if (*out == NULL) | |
944 | OPENSSL_free(start); | |
945 | return -1; | |
946 | } | |
947 | ||
948 | int i2b_PVK_bio(BIO *out, const EVP_PKEY *pk, int enclevel, | |
949 | pem_password_cb *cb, void *u) | |
950 | { | |
951 | unsigned char *tmp = NULL; | |
952 | int outlen, wrlen; | |
953 | ||
954 | outlen = i2b_PVK(&tmp, pk, enclevel, cb, u); | |
955 | if (outlen < 0) | |
956 | return -1; | |
957 | wrlen = BIO_write(out, tmp, outlen); | |
958 | OPENSSL_free(tmp); | |
959 | if (wrlen == outlen) { | |
960 | return outlen; | |
961 | } | |
962 | ERR_raise(ERR_LIB_PEM, PEM_R_BIO_WRITE_FAILURE); | |
963 | return -1; | |
964 | } |