]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/pem/pvkfmt.c
New BN functions.
[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
123 static EVP_PKEY *b2i_rsa(const unsigned char **in, unsigned int length,
124 unsigned int bitlen, int ispub);
125 static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length,
126 unsigned int bitlen, int ispub);
127
128 static int do_blob_header(const unsigned char **in, unsigned int length,
129 unsigned int *pmagic, unsigned int *pbitlen,
130 int *pisdss, int *pispub)
131 {
132 const unsigned char *p = *in;
133 if (length < 16)
134 return 0;
135 /* bType */
136 if (*p == MS_PUBLICKEYBLOB) {
137 if (*pispub == 0) {
138 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
139 return 0;
140 }
141 *pispub = 1;
142 } else if (*p == MS_PRIVATEKEYBLOB) {
143 if (*pispub == 1) {
144 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
145 return 0;
146 }
147 *pispub = 0;
148 } else
149 return 0;
150 p++;
151 /* Version */
152 if (*p++ != 0x2) {
153 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_VERSION_NUMBER);
154 return 0;
155 }
156 /* Ignore reserved, aiKeyAlg */
157 p += 6;
158 *pmagic = read_ledword(&p);
159 *pbitlen = read_ledword(&p);
160 *pisdss = 0;
161 switch (*pmagic) {
162
163 case MS_DSS1MAGIC:
164 *pisdss = 1;
165 case MS_RSA1MAGIC:
166 if (*pispub == 0) {
167 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
168 return 0;
169 }
170 break;
171
172 case MS_DSS2MAGIC:
173 *pisdss = 1;
174 case MS_RSA2MAGIC:
175 if (*pispub == 1) {
176 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
177 return 0;
178 }
179 break;
180
181 default:
182 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_MAGIC_NUMBER);
183 return -1;
184 }
185 *in = p;
186 return 1;
187 }
188
189 static unsigned int blob_length(unsigned bitlen, int isdss, int ispub)
190 {
191 unsigned int nbyte, hnbyte;
192 nbyte = (bitlen + 7) >> 3;
193 hnbyte = (bitlen + 15) >> 4;
194 if (isdss) {
195
196 /*
197 * Expected length: 20 for q + 3 components bitlen each + 24 for seed
198 * structure.
199 */
200 if (ispub)
201 return 44 + 3 * nbyte;
202 /*
203 * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
204 * structure.
205 */
206 else
207 return 64 + 2 * nbyte;
208 } else {
209 /* Expected length: 4 for 'e' + 'n' */
210 if (ispub)
211 return 4 + nbyte;
212 else
213 /*
214 * Expected length: 4 for 'e' and 7 other components. 2
215 * components are bitlen size, 5 are bitlen/2
216 */
217 return 4 + 2 * nbyte + 5 * hnbyte;
218 }
219
220 }
221
222 static EVP_PKEY *do_b2i(const unsigned char **in, unsigned int length,
223 int ispub)
224 {
225 const unsigned char *p = *in;
226 unsigned int bitlen, magic;
227 int isdss;
228 if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) {
229 PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
230 return NULL;
231 }
232 length -= 16;
233 if (length < blob_length(bitlen, isdss, ispub)) {
234 PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_TOO_SHORT);
235 return NULL;
236 }
237 if (isdss)
238 return b2i_dss(&p, length, bitlen, ispub);
239 else
240 return b2i_rsa(&p, length, bitlen, ispub);
241 }
242
243 static EVP_PKEY *do_b2i_bio(BIO *in, int ispub)
244 {
245 const unsigned char *p;
246 unsigned char hdr_buf[16], *buf = NULL;
247 unsigned int bitlen, magic, length;
248 int isdss;
249 EVP_PKEY *ret = NULL;
250 if (BIO_read(in, hdr_buf, 16) != 16) {
251 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
252 return NULL;
253 }
254 p = hdr_buf;
255 if (do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0)
256 return NULL;
257
258 length = blob_length(bitlen, isdss, ispub);
259 buf = OPENSSL_malloc(length);
260 if (buf == NULL) {
261 PEMerr(PEM_F_DO_B2I_BIO, ERR_R_MALLOC_FAILURE);
262 goto err;
263 }
264 p = buf;
265 if (BIO_read(in, buf, length) != (int)length) {
266 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
267 goto err;
268 }
269
270 if (isdss)
271 ret = b2i_dss(&p, length, bitlen, ispub);
272 else
273 ret = b2i_rsa(&p, length, bitlen, ispub);
274
275 err:
276 OPENSSL_free(buf);
277 return ret;
278 }
279
280 static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length,
281 unsigned int bitlen, int ispub)
282 {
283 const unsigned char *p = *in;
284 EVP_PKEY *ret = NULL;
285 DSA *dsa = NULL;
286 BN_CTX *ctx = NULL;
287 unsigned int nbyte;
288 nbyte = (bitlen + 7) >> 3;
289
290 dsa = DSA_new();
291 ret = EVP_PKEY_new();
292 if (dsa == NULL || ret == NULL)
293 goto memerr;
294 if (!read_lebn(&p, nbyte, &dsa->p))
295 goto memerr;
296 if (!read_lebn(&p, 20, &dsa->q))
297 goto memerr;
298 if (!read_lebn(&p, nbyte, &dsa->g))
299 goto memerr;
300 if (ispub) {
301 if (!read_lebn(&p, nbyte, &dsa->pub_key))
302 goto memerr;
303 } else {
304 if (!read_lebn(&p, 20, &dsa->priv_key))
305 goto memerr;
306 /* Calculate public key */
307 if ((dsa->pub_key = BN_new()) == NULL)
308 goto memerr;
309 if ((ctx = BN_CTX_new()) == NULL)
310 goto memerr;
311
312 if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx))
313 goto memerr;
314 BN_CTX_free(ctx);
315 }
316
317 EVP_PKEY_set1_DSA(ret, dsa);
318 DSA_free(dsa);
319 *in = p;
320 return ret;
321
322 memerr:
323 PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);
324 DSA_free(dsa);
325 EVP_PKEY_free(ret);
326 BN_CTX_free(ctx);
327 return NULL;
328 }
329
330 static EVP_PKEY *b2i_rsa(const unsigned char **in, unsigned int length,
331 unsigned int bitlen, int ispub)
332 {
333 const unsigned char *p = *in;
334 EVP_PKEY *ret = NULL;
335 RSA *rsa = NULL;
336 unsigned int nbyte, hnbyte;
337 nbyte = (bitlen + 7) >> 3;
338 hnbyte = (bitlen + 15) >> 4;
339 rsa = RSA_new();
340 ret = EVP_PKEY_new();
341 if (rsa == NULL || ret == NULL)
342 goto memerr;
343 rsa->e = BN_new();
344 if (rsa->e == NULL)
345 goto memerr;
346 if (!BN_set_word(rsa->e, read_ledword(&p)))
347 goto memerr;
348 if (!read_lebn(&p, nbyte, &rsa->n))
349 goto memerr;
350 if (!ispub) {
351 if (!read_lebn(&p, hnbyte, &rsa->p))
352 goto memerr;
353 if (!read_lebn(&p, hnbyte, &rsa->q))
354 goto memerr;
355 if (!read_lebn(&p, hnbyte, &rsa->dmp1))
356 goto memerr;
357 if (!read_lebn(&p, hnbyte, &rsa->dmq1))
358 goto memerr;
359 if (!read_lebn(&p, hnbyte, &rsa->iqmp))
360 goto memerr;
361 if (!read_lebn(&p, nbyte, &rsa->d))
362 goto memerr;
363 }
364
365 EVP_PKEY_set1_RSA(ret, rsa);
366 RSA_free(rsa);
367 *in = p;
368 return ret;
369 memerr:
370 PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE);
371 RSA_free(rsa);
372 EVP_PKEY_free(ret);
373 return NULL;
374 }
375
376 EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
377 {
378 return do_b2i(in, length, 0);
379 }
380
381 EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
382 {
383 return do_b2i(in, length, 1);
384 }
385
386 EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
387 {
388 return do_b2i_bio(in, 0);
389 }
390
391 EVP_PKEY *b2i_PublicKey_bio(BIO *in)
392 {
393 return do_b2i_bio(in, 1);
394 }
395
396 static void write_ledword(unsigned char **out, unsigned int dw)
397 {
398 unsigned char *p = *out;
399 *p++ = dw & 0xff;
400 *p++ = (dw >> 8) & 0xff;
401 *p++ = (dw >> 16) & 0xff;
402 *p++ = (dw >> 24) & 0xff;
403 *out = p;
404 }
405
406 static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
407 {
408 BN_bn2lebinpad(bn, *out, len);
409 *out += len;
410 }
411
412 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic);
413 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
414
415 static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
416 static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
417
418 static int do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)
419 {
420 unsigned char *p;
421 unsigned int bitlen, magic = 0, keyalg;
422 int outlen, noinc = 0;
423 int pktype = EVP_PKEY_id(pk);
424 if (pktype == EVP_PKEY_DSA) {
425 bitlen = check_bitlen_dsa(EVP_PKEY_get0_DSA(pk), ispub, &magic);
426 keyalg = MS_KEYALG_DSS_SIGN;
427 } else if (pktype == EVP_PKEY_RSA) {
428 bitlen = check_bitlen_rsa(EVP_PKEY_get0_RSA(pk), ispub, &magic);
429 keyalg = MS_KEYALG_RSA_KEYX;
430 } else
431 return -1;
432 if (bitlen == 0)
433 return -1;
434 outlen = 16 + blob_length(bitlen,
435 keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
436 if (out == NULL)
437 return outlen;
438 if (*out)
439 p = *out;
440 else {
441 p = OPENSSL_malloc(outlen);
442 if (p == NULL)
443 return -1;
444 *out = p;
445 noinc = 1;
446 }
447 if (ispub)
448 *p++ = MS_PUBLICKEYBLOB;
449 else
450 *p++ = MS_PRIVATEKEYBLOB;
451 *p++ = 0x2;
452 *p++ = 0;
453 *p++ = 0;
454 write_ledword(&p, keyalg);
455 write_ledword(&p, magic);
456 write_ledword(&p, bitlen);
457 if (keyalg == MS_KEYALG_DSS_SIGN)
458 write_dsa(&p, EVP_PKEY_get0_DSA(pk), ispub);
459 else
460 write_rsa(&p, EVP_PKEY_get0_RSA(pk), ispub);
461 if (!noinc)
462 *out += outlen;
463 return outlen;
464 }
465
466 static int do_i2b_bio(BIO *out, EVP_PKEY *pk, int ispub)
467 {
468 unsigned char *tmp = NULL;
469 int outlen, wrlen;
470 outlen = do_i2b(&tmp, pk, ispub);
471 if (outlen < 0)
472 return -1;
473 wrlen = BIO_write(out, tmp, outlen);
474 OPENSSL_free(tmp);
475 if (wrlen == outlen)
476 return outlen;
477 return -1;
478 }
479
480 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic)
481 {
482 int bitlen;
483 bitlen = BN_num_bits(dsa->p);
484 if ((bitlen & 7) || (BN_num_bits(dsa->q) != 160)
485 || (BN_num_bits(dsa->g) > bitlen))
486 goto badkey;
487 if (ispub) {
488 if (BN_num_bits(dsa->pub_key) > bitlen)
489 goto badkey;
490 *pmagic = MS_DSS1MAGIC;
491 } else {
492 if (BN_num_bits(dsa->priv_key) > 160)
493 goto badkey;
494 *pmagic = MS_DSS2MAGIC;
495 }
496
497 return bitlen;
498 badkey:
499 PEMerr(PEM_F_CHECK_BITLEN_DSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
500 return 0;
501 }
502
503 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic)
504 {
505 int nbyte, hnbyte, bitlen;
506 if (BN_num_bits(rsa->e) > 32)
507 goto badkey;
508 bitlen = BN_num_bits(rsa->n);
509 nbyte = BN_num_bytes(rsa->n);
510 hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
511 if (ispub) {
512 *pmagic = MS_RSA1MAGIC;
513 return bitlen;
514 } else {
515 *pmagic = MS_RSA2MAGIC;
516 /*
517 * For private key each component must fit within nbyte or hnbyte.
518 */
519 if (BN_num_bytes(rsa->d) > nbyte)
520 goto badkey;
521 if ((BN_num_bytes(rsa->iqmp) > hnbyte)
522 || (BN_num_bytes(rsa->p) > hnbyte)
523 || (BN_num_bytes(rsa->q) > hnbyte)
524 || (BN_num_bytes(rsa->dmp1) > hnbyte)
525 || (BN_num_bytes(rsa->dmq1) > hnbyte))
526 goto badkey;
527 }
528 return bitlen;
529 badkey:
530 PEMerr(PEM_F_CHECK_BITLEN_RSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
531 return 0;
532 }
533
534 static void write_rsa(unsigned char **out, RSA *rsa, int ispub)
535 {
536 int nbyte, hnbyte;
537 nbyte = BN_num_bytes(rsa->n);
538 hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
539 write_lebn(out, rsa->e, 4);
540 write_lebn(out, rsa->n, -1);
541 if (ispub)
542 return;
543 write_lebn(out, rsa->p, hnbyte);
544 write_lebn(out, rsa->q, hnbyte);
545 write_lebn(out, rsa->dmp1, hnbyte);
546 write_lebn(out, rsa->dmq1, hnbyte);
547 write_lebn(out, rsa->iqmp, hnbyte);
548 write_lebn(out, rsa->d, nbyte);
549 }
550
551 static void write_dsa(unsigned char **out, DSA *dsa, int ispub)
552 {
553 int nbyte;
554 nbyte = BN_num_bytes(dsa->p);
555 write_lebn(out, dsa->p, nbyte);
556 write_lebn(out, dsa->q, 20);
557 write_lebn(out, dsa->g, nbyte);
558 if (ispub)
559 write_lebn(out, dsa->pub_key, nbyte);
560 else
561 write_lebn(out, dsa->priv_key, 20);
562 /* Set "invalid" for seed structure values */
563 memset(*out, 0xff, 24);
564 *out += 24;
565 return;
566 }
567
568 int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk)
569 {
570 return do_i2b_bio(out, pk, 0);
571 }
572
573 int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk)
574 {
575 return do_i2b_bio(out, pk, 1);
576 }
577
578 # ifndef OPENSSL_NO_RC4
579
580 static int do_PVK_header(const unsigned char **in, unsigned int length,
581 int skip_magic,
582 unsigned int *psaltlen, unsigned int *pkeylen)
583 {
584 const unsigned char *p = *in;
585 unsigned int pvk_magic, is_encrypted;
586 if (skip_magic) {
587 if (length < 20) {
588 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
589 return 0;
590 }
591 } else {
592 if (length < 24) {
593 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
594 return 0;
595 }
596 pvk_magic = read_ledword(&p);
597 if (pvk_magic != MS_PVKMAGIC) {
598 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_BAD_MAGIC_NUMBER);
599 return 0;
600 }
601 }
602 /* Skip reserved */
603 p += 4;
604 /*
605 * keytype =
606 */ read_ledword(&p);
607 is_encrypted = read_ledword(&p);
608 *psaltlen = read_ledword(&p);
609 *pkeylen = read_ledword(&p);
610
611 if (is_encrypted && !*psaltlen) {
612 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_INCONSISTENT_HEADER);
613 return 0;
614 }
615
616 *in = p;
617 return 1;
618 }
619
620 static int derive_pvk_key(unsigned char *key,
621 const unsigned char *salt, unsigned int saltlen,
622 const unsigned char *pass, int passlen)
623 {
624 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
625 int rv = 1;
626 if (mctx == NULL
627 || !EVP_DigestInit_ex(mctx, EVP_sha1(), NULL)
628 || !EVP_DigestUpdate(mctx, salt, saltlen)
629 || !EVP_DigestUpdate(mctx, pass, passlen)
630 || !EVP_DigestFinal_ex(mctx, key, NULL))
631 rv = 0;
632
633 EVP_MD_CTX_free(mctx);
634 return rv;
635 }
636
637 static EVP_PKEY *do_PVK_body(const unsigned char **in,
638 unsigned int saltlen, unsigned int keylen,
639 pem_password_cb *cb, void *u)
640 {
641 EVP_PKEY *ret = NULL;
642 const unsigned char *p = *in;
643 unsigned int magic;
644 unsigned char *enctmp = NULL, *q;
645
646 EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
647 if (saltlen) {
648 char psbuf[PEM_BUFSIZE];
649 unsigned char keybuf[20];
650 int enctmplen, inlen;
651 if (cb)
652 inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
653 else
654 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
655 if (inlen <= 0) {
656 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_PASSWORD_READ);
657 goto err;
658 }
659 enctmp = OPENSSL_malloc(keylen + 8);
660 if (enctmp == NULL) {
661 PEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE);
662 goto err;
663 }
664 if (!derive_pvk_key(keybuf, p, saltlen,
665 (unsigned char *)psbuf, inlen))
666 goto err;
667 p += saltlen;
668 /* Copy BLOBHEADER across, decrypt rest */
669 memcpy(enctmp, p, 8);
670 p += 8;
671 if (keylen < 8) {
672 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_PVK_TOO_SHORT);
673 goto err;
674 }
675 inlen = keylen - 8;
676 q = enctmp + 8;
677 if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
678 goto err;
679 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
680 goto err;
681 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
682 goto err;
683 magic = read_ledword((const unsigned char **)&q);
684 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
685 q = enctmp + 8;
686 memset(keybuf + 5, 0, 11);
687 if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
688 goto err;
689 OPENSSL_cleanse(keybuf, 20);
690 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
691 goto err;
692 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
693 goto err;
694 magic = read_ledword((const unsigned char **)&q);
695 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
696 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT);
697 goto err;
698 }
699 } else
700 OPENSSL_cleanse(keybuf, 20);
701 p = enctmp;
702 }
703
704 ret = b2i_PrivateKey(&p, keylen);
705 err:
706 EVP_CIPHER_CTX_free(cctx);
707 OPENSSL_free(enctmp);
708 return ret;
709 }
710
711 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
712 {
713 unsigned char pvk_hdr[24], *buf = NULL;
714 const unsigned char *p;
715 int buflen;
716 EVP_PKEY *ret = NULL;
717 unsigned int saltlen, keylen;
718 if (BIO_read(in, pvk_hdr, 24) != 24) {
719 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
720 return NULL;
721 }
722 p = pvk_hdr;
723
724 if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))
725 return 0;
726 buflen = (int)keylen + saltlen;
727 buf = OPENSSL_malloc(buflen);
728 if (buf == NULL) {
729 PEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE);
730 return 0;
731 }
732 p = buf;
733 if (BIO_read(in, buf, buflen) != buflen) {
734 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
735 goto err;
736 }
737 ret = do_PVK_body(&p, saltlen, keylen, cb, u);
738
739 err:
740 OPENSSL_clear_free(buf, buflen);
741 return ret;
742 }
743
744 static int i2b_PVK(unsigned char **out, EVP_PKEY *pk, int enclevel,
745 pem_password_cb *cb, void *u)
746 {
747 int outlen = 24, pklen;
748 unsigned char *p, *salt = NULL;
749 EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
750 if (enclevel)
751 outlen += PVK_SALTLEN;
752 pklen = do_i2b(NULL, pk, 0);
753 if (pklen < 0)
754 return -1;
755 outlen += pklen;
756 if (!out)
757 return outlen;
758 if (*out)
759 p = *out;
760 else {
761 p = OPENSSL_malloc(outlen);
762 if (p == NULL) {
763 PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE);
764 return -1;
765 }
766 *out = p;
767 }
768
769 write_ledword(&p, MS_PVKMAGIC);
770 write_ledword(&p, 0);
771 if (EVP_PKEY_id(pk) == EVP_PKEY_DSA)
772 write_ledword(&p, MS_KEYTYPE_SIGN);
773 else
774 write_ledword(&p, MS_KEYTYPE_KEYX);
775 write_ledword(&p, enclevel ? 1 : 0);
776 write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
777 write_ledword(&p, pklen);
778 if (enclevel) {
779 if (RAND_bytes(p, PVK_SALTLEN) <= 0)
780 goto error;
781 salt = p;
782 p += PVK_SALTLEN;
783 }
784 do_i2b(&p, pk, 0);
785 if (enclevel == 0)
786 return outlen;
787 else {
788 char psbuf[PEM_BUFSIZE];
789 unsigned char keybuf[20];
790 int enctmplen, inlen;
791 if (cb)
792 inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
793 else
794 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
795 if (inlen <= 0) {
796 PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ);
797 goto error;
798 }
799 if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
800 (unsigned char *)psbuf, inlen))
801 goto error;
802 if (enclevel == 1)
803 memset(keybuf + 5, 0, 11);
804 p = salt + PVK_SALTLEN + 8;
805 if (!EVP_EncryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
806 goto error;
807 OPENSSL_cleanse(keybuf, 20);
808 if (!EVP_DecryptUpdate(cctx, p, &enctmplen, p, pklen - 8))
809 goto error;
810 if (!EVP_DecryptFinal_ex(cctx, p + enctmplen, &enctmplen))
811 goto error;
812 }
813 EVP_CIPHER_CTX_free(cctx);
814 return outlen;
815
816 error:
817 EVP_CIPHER_CTX_free(cctx);
818 return -1;
819 }
820
821 int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel,
822 pem_password_cb *cb, void *u)
823 {
824 unsigned char *tmp = NULL;
825 int outlen, wrlen;
826 outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
827 if (outlen < 0)
828 return -1;
829 wrlen = BIO_write(out, tmp, outlen);
830 OPENSSL_free(tmp);
831 if (wrlen == outlen) {
832 PEMerr(PEM_F_I2B_PVK_BIO, PEM_R_BIO_WRITE_FAILURE);
833 return outlen;
834 }
835 return -1;
836 }
837
838 # endif
839
840 #endif