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