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