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