]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/pem/pvkfmt.c
Remove the GOST engine
[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
b39fc560 64#include "internal/cryptlib.h"
a0156a92
DSH
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);
90945fa3 101 if (tmpbuf == NULL)
0f113f3e
MC
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);
90945fa3 272 if (buf == NULL) {
0f113f3e
MC
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();
90945fa3 304 if (dsa == NULL || ret == NULL)
0f113f3e
MC
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 */
75ebbd9a 319 if ((dsa->pub_key = BN_new()) == NULL)
0f113f3e 320 goto memerr;
75ebbd9a 321 if ((ctx = BN_CTX_new()) == NULL)
0f113f3e
MC
322 goto memerr;
323
324 if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx))
0f113f3e
MC
325 goto memerr;
326 BN_CTX_free(ctx);
327 }
328
329 EVP_PKEY_set1_DSA(ret, dsa);
330 DSA_free(dsa);
331 *in = p;
332 return ret;
333
334 memerr:
335 PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);
d6407083 336 DSA_free(dsa);
c5ba2d99 337 EVP_PKEY_free(ret);
23a1d5e9 338 BN_CTX_free(ctx);
0f113f3e
MC
339 return NULL;
340}
a0156a92
DSH
341
342static EVP_PKEY *b2i_rsa(const unsigned char **in, unsigned int length,
0f113f3e
MC
343 unsigned int bitlen, int ispub)
344{
345 const unsigned char *p = *in;
346 EVP_PKEY *ret = NULL;
347 RSA *rsa = NULL;
348 unsigned int nbyte, hnbyte;
349 nbyte = (bitlen + 7) >> 3;
350 hnbyte = (bitlen + 15) >> 4;
351 rsa = RSA_new();
352 ret = EVP_PKEY_new();
90945fa3 353 if (rsa == NULL || ret == NULL)
0f113f3e
MC
354 goto memerr;
355 rsa->e = BN_new();
90945fa3 356 if (rsa->e == NULL)
0f113f3e
MC
357 goto memerr;
358 if (!BN_set_word(rsa->e, read_ledword(&p)))
359 goto memerr;
360 if (!read_lebn(&p, nbyte, &rsa->n))
361 goto memerr;
362 if (!ispub) {
363 if (!read_lebn(&p, hnbyte, &rsa->p))
364 goto memerr;
365 if (!read_lebn(&p, hnbyte, &rsa->q))
366 goto memerr;
367 if (!read_lebn(&p, hnbyte, &rsa->dmp1))
368 goto memerr;
369 if (!read_lebn(&p, hnbyte, &rsa->dmq1))
370 goto memerr;
371 if (!read_lebn(&p, hnbyte, &rsa->iqmp))
372 goto memerr;
373 if (!read_lebn(&p, nbyte, &rsa->d))
374 goto memerr;
375 }
376
377 EVP_PKEY_set1_RSA(ret, rsa);
378 RSA_free(rsa);
379 *in = p;
380 return ret;
381 memerr:
382 PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE);
d6407083 383 RSA_free(rsa);
c5ba2d99 384 EVP_PKEY_free(ret);
0f113f3e
MC
385 return NULL;
386}
a0156a92
DSH
387
388EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
0f113f3e
MC
389{
390 return do_b2i(in, length, 0);
391}
a0156a92
DSH
392
393EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
0f113f3e
MC
394{
395 return do_b2i(in, length, 1);
396}
a0156a92
DSH
397
398EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
0f113f3e
MC
399{
400 return do_b2i_bio(in, 0);
401}
a0156a92
DSH
402
403EVP_PKEY *b2i_PublicKey_bio(BIO *in)
0f113f3e
MC
404{
405 return do_b2i_bio(in, 1);
406}
a0156a92
DSH
407
408static void write_ledword(unsigned char **out, unsigned int dw)
0f113f3e
MC
409{
410 unsigned char *p = *out;
411 *p++ = dw & 0xff;
412 *p++ = (dw >> 8) & 0xff;
413 *p++ = (dw >> 16) & 0xff;
414 *p++ = (dw >> 24) & 0xff;
415 *out = p;
416}
a0156a92
DSH
417
418static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
0f113f3e
MC
419{
420 int nb, i;
421 unsigned char *p = *out, *q, c;
422 nb = BN_num_bytes(bn);
423 BN_bn2bin(bn, p);
424 q = p + nb - 1;
425 /* In place byte order reversal */
426 for (i = 0; i < nb / 2; i++) {
427 c = *p;
428 *p++ = *q;
429 *q-- = c;
430 }
431 *out += nb;
432 /* Pad with zeroes if we have to */
433 if (len > 0) {
434 len -= nb;
435 if (len > 0) {
436 memset(*out, 0, len);
437 *out += len;
438 }
439 }
440}
a0156a92
DSH
441
442static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic);
443static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
444
445static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
446static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
0f113f3e 447
a0156a92 448static int do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)
0f113f3e
MC
449{
450 unsigned char *p;
451 unsigned int bitlen, magic = 0, keyalg;
452 int outlen, noinc = 0;
453 if (pk->type == EVP_PKEY_DSA) {
454 bitlen = check_bitlen_dsa(pk->pkey.dsa, ispub, &magic);
455 keyalg = MS_KEYALG_DSS_SIGN;
456 } else if (pk->type == EVP_PKEY_RSA) {
457 bitlen = check_bitlen_rsa(pk->pkey.rsa, ispub, &magic);
458 keyalg = MS_KEYALG_RSA_KEYX;
459 } else
460 return -1;
461 if (bitlen == 0)
462 return -1;
463 outlen = 16 + blob_length(bitlen,
464 keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
465 if (out == NULL)
466 return outlen;
467 if (*out)
468 p = *out;
469 else {
470 p = OPENSSL_malloc(outlen);
90945fa3 471 if (p == NULL)
0f113f3e
MC
472 return -1;
473 *out = p;
474 noinc = 1;
475 }
476 if (ispub)
477 *p++ = MS_PUBLICKEYBLOB;
478 else
479 *p++ = MS_PRIVATEKEYBLOB;
480 *p++ = 0x2;
481 *p++ = 0;
482 *p++ = 0;
483 write_ledword(&p, keyalg);
484 write_ledword(&p, magic);
485 write_ledword(&p, bitlen);
486 if (keyalg == MS_KEYALG_DSS_SIGN)
487 write_dsa(&p, pk->pkey.dsa, ispub);
488 else
489 write_rsa(&p, pk->pkey.rsa, ispub);
490 if (!noinc)
491 *out += outlen;
492 return outlen;
493}
a0156a92
DSH
494
495static int do_i2b_bio(BIO *out, EVP_PKEY *pk, int ispub)
0f113f3e
MC
496{
497 unsigned char *tmp = NULL;
498 int outlen, wrlen;
499 outlen = do_i2b(&tmp, pk, ispub);
500 if (outlen < 0)
501 return -1;
502 wrlen = BIO_write(out, tmp, outlen);
503 OPENSSL_free(tmp);
504 if (wrlen == outlen)
505 return outlen;
506 return -1;
507}
a0156a92
DSH
508
509static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic)
0f113f3e
MC
510{
511 int bitlen;
512 bitlen = BN_num_bits(dsa->p);
513 if ((bitlen & 7) || (BN_num_bits(dsa->q) != 160)
514 || (BN_num_bits(dsa->g) > bitlen))
515 goto badkey;
516 if (ispub) {
517 if (BN_num_bits(dsa->pub_key) > bitlen)
518 goto badkey;
519 *pmagic = MS_DSS1MAGIC;
520 } else {
521 if (BN_num_bits(dsa->priv_key) > 160)
522 goto badkey;
523 *pmagic = MS_DSS2MAGIC;
524 }
525
526 return bitlen;
527 badkey:
528 PEMerr(PEM_F_CHECK_BITLEN_DSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
529 return 0;
530}
a0156a92
DSH
531
532static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic)
0f113f3e
MC
533{
534 int nbyte, hnbyte, bitlen;
535 if (BN_num_bits(rsa->e) > 32)
536 goto badkey;
537 bitlen = BN_num_bits(rsa->n);
538 nbyte = BN_num_bytes(rsa->n);
539 hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
540 if (ispub) {
541 *pmagic = MS_RSA1MAGIC;
542 return bitlen;
543 } else {
544 *pmagic = MS_RSA2MAGIC;
545 /*
546 * For private key each component must fit within nbyte or hnbyte.
547 */
548 if (BN_num_bytes(rsa->d) > nbyte)
549 goto badkey;
550 if ((BN_num_bytes(rsa->iqmp) > hnbyte)
551 || (BN_num_bytes(rsa->p) > hnbyte)
552 || (BN_num_bytes(rsa->q) > hnbyte)
553 || (BN_num_bytes(rsa->dmp1) > hnbyte)
554 || (BN_num_bytes(rsa->dmq1) > hnbyte))
555 goto badkey;
556 }
557 return bitlen;
558 badkey:
559 PEMerr(PEM_F_CHECK_BITLEN_RSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
560 return 0;
561}
a0156a92
DSH
562
563static void write_rsa(unsigned char **out, RSA *rsa, int ispub)
0f113f3e
MC
564{
565 int nbyte, hnbyte;
566 nbyte = BN_num_bytes(rsa->n);
567 hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
568 write_lebn(out, rsa->e, 4);
569 write_lebn(out, rsa->n, -1);
570 if (ispub)
571 return;
572 write_lebn(out, rsa->p, hnbyte);
573 write_lebn(out, rsa->q, hnbyte);
574 write_lebn(out, rsa->dmp1, hnbyte);
575 write_lebn(out, rsa->dmq1, hnbyte);
576 write_lebn(out, rsa->iqmp, hnbyte);
577 write_lebn(out, rsa->d, nbyte);
578}
579
a0156a92 580static void write_dsa(unsigned char **out, DSA *dsa, int ispub)
0f113f3e
MC
581{
582 int nbyte;
583 nbyte = BN_num_bytes(dsa->p);
584 write_lebn(out, dsa->p, nbyte);
585 write_lebn(out, dsa->q, 20);
586 write_lebn(out, dsa->g, nbyte);
587 if (ispub)
588 write_lebn(out, dsa->pub_key, nbyte);
589 else
590 write_lebn(out, dsa->priv_key, 20);
591 /* Set "invalid" for seed structure values */
592 memset(*out, 0xff, 24);
593 *out += 24;
594 return;
595}
a0156a92
DSH
596
597int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk)
0f113f3e
MC
598{
599 return do_i2b_bio(out, pk, 0);
600}
a0156a92
DSH
601
602int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk)
0f113f3e
MC
603{
604 return do_i2b_bio(out, pk, 1);
605}
a0156a92 606
0f113f3e 607# ifndef OPENSSL_NO_RC4
00a37b5a 608
a0156a92 609static int do_PVK_header(const unsigned char **in, unsigned int length,
0f113f3e
MC
610 int skip_magic,
611 unsigned int *psaltlen, unsigned int *pkeylen)
612{
613 const unsigned char *p = *in;
614 unsigned int pvk_magic, is_encrypted;
615 if (skip_magic) {
616 if (length < 20) {
617 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
618 return 0;
619 }
0f113f3e
MC
620 } else {
621 if (length < 24) {
622 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
623 return 0;
624 }
0f113f3e
MC
625 pvk_magic = read_ledword(&p);
626 if (pvk_magic != MS_PVKMAGIC) {
627 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_BAD_MAGIC_NUMBER);
628 return 0;
629 }
630 }
631 /* Skip reserved */
632 p += 4;
633 /*
634 * keytype =
635 */ read_ledword(&p);
636 is_encrypted = read_ledword(&p);
637 *psaltlen = read_ledword(&p);
638 *pkeylen = read_ledword(&p);
639
640 if (is_encrypted && !*psaltlen) {
641 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_INCONSISTENT_HEADER);
642 return 0;
643 }
644
645 *in = p;
646 return 1;
647}
648
649static int derive_pvk_key(unsigned char *key,
650 const unsigned char *salt, unsigned int saltlen,
651 const unsigned char *pass, int passlen)
652{
3cb9fd97 653 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
0f113f3e 654 int rv = 1;
6e59a892
RL
655 if (mctx == NULL
656 || !EVP_DigestInit_ex(mctx, EVP_sha1(), NULL)
657 || !EVP_DigestUpdate(mctx, salt, saltlen)
658 || !EVP_DigestUpdate(mctx, pass, passlen)
659 || !EVP_DigestFinal_ex(mctx, key, NULL))
0f113f3e
MC
660 rv = 0;
661
bfb0641f 662 EVP_MD_CTX_free(mctx);
0f113f3e
MC
663 return rv;
664}
a0156a92
DSH
665
666static EVP_PKEY *do_PVK_body(const unsigned char **in,
0f113f3e
MC
667 unsigned int saltlen, unsigned int keylen,
668 pem_password_cb *cb, void *u)
669{
670 EVP_PKEY *ret = NULL;
671 const unsigned char *p = *in;
672 unsigned int magic;
673 unsigned char *enctmp = NULL, *q;
25aaa98a 674
846ec07d 675 EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
0f113f3e
MC
676 if (saltlen) {
677 char psbuf[PEM_BUFSIZE];
678 unsigned char keybuf[20];
679 int enctmplen, inlen;
680 if (cb)
681 inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
682 else
683 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
684 if (inlen <= 0) {
685 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_PASSWORD_READ);
3f6c7691 686 goto err;
0f113f3e
MC
687 }
688 enctmp = OPENSSL_malloc(keylen + 8);
90945fa3 689 if (enctmp == NULL) {
0f113f3e 690 PEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE);
3f6c7691 691 goto err;
0f113f3e
MC
692 }
693 if (!derive_pvk_key(keybuf, p, saltlen,
694 (unsigned char *)psbuf, inlen))
3f6c7691 695 goto err;
0f113f3e
MC
696 p += saltlen;
697 /* Copy BLOBHEADER across, decrypt rest */
698 memcpy(enctmp, p, 8);
699 p += 8;
700 if (keylen < 8) {
701 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_PVK_TOO_SHORT);
3f6c7691 702 goto err;
0f113f3e
MC
703 }
704 inlen = keylen - 8;
705 q = enctmp + 8;
846ec07d 706 if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
0f113f3e 707 goto err;
846ec07d 708 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
0f113f3e 709 goto err;
846ec07d 710 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
0f113f3e
MC
711 goto err;
712 magic = read_ledword((const unsigned char **)&q);
713 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
714 q = enctmp + 8;
715 memset(keybuf + 5, 0, 11);
846ec07d 716 if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
0f113f3e
MC
717 goto err;
718 OPENSSL_cleanse(keybuf, 20);
846ec07d 719 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
0f113f3e 720 goto err;
846ec07d 721 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
0f113f3e
MC
722 goto err;
723 magic = read_ledword((const unsigned char **)&q);
724 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
725 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT);
726 goto err;
727 }
728 } else
729 OPENSSL_cleanse(keybuf, 20);
730 p = enctmp;
731 }
732
733 ret = b2i_PrivateKey(&p, keylen);
734 err:
846ec07d 735 EVP_CIPHER_CTX_free(cctx);
25aaa98a 736 OPENSSL_free(enctmp);
0f113f3e
MC
737 return ret;
738}
a0156a92
DSH
739
740EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
0f113f3e
MC
741{
742 unsigned char pvk_hdr[24], *buf = NULL;
743 const unsigned char *p;
744 int buflen;
745 EVP_PKEY *ret = NULL;
746 unsigned int saltlen, keylen;
747 if (BIO_read(in, pvk_hdr, 24) != 24) {
748 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
749 return NULL;
750 }
751 p = pvk_hdr;
752
753 if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))
754 return 0;
755 buflen = (int)keylen + saltlen;
756 buf = OPENSSL_malloc(buflen);
90945fa3 757 if (buf == NULL) {
0f113f3e
MC
758 PEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE);
759 return 0;
760 }
761 p = buf;
762 if (BIO_read(in, buf, buflen) != buflen) {
763 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
764 goto err;
765 }
766 ret = do_PVK_body(&p, saltlen, keylen, cb, u);
767
768 err:
4b45c6e5 769 OPENSSL_clear_free(buf, buflen);
0f113f3e
MC
770 return ret;
771}
772
773static int i2b_PVK(unsigned char **out, EVP_PKEY *pk, int enclevel,
774 pem_password_cb *cb, void *u)
775{
776 int outlen = 24, pklen;
777 unsigned char *p, *salt = NULL;
846ec07d 778 EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
0f113f3e
MC
779 if (enclevel)
780 outlen += PVK_SALTLEN;
781 pklen = do_i2b(NULL, pk, 0);
782 if (pklen < 0)
783 return -1;
784 outlen += pklen;
785 if (!out)
786 return outlen;
787 if (*out)
788 p = *out;
789 else {
790 p = OPENSSL_malloc(outlen);
90945fa3 791 if (p == NULL) {
0f113f3e
MC
792 PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE);
793 return -1;
794 }
795 *out = p;
796 }
797
798 write_ledword(&p, MS_PVKMAGIC);
799 write_ledword(&p, 0);
800 if (pk->type == EVP_PKEY_DSA)
801 write_ledword(&p, MS_KEYTYPE_SIGN);
802 else
803 write_ledword(&p, MS_KEYTYPE_KEYX);
804 write_ledword(&p, enclevel ? 1 : 0);
805 write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
806 write_ledword(&p, pklen);
807 if (enclevel) {
808 if (RAND_bytes(p, PVK_SALTLEN) <= 0)
809 goto error;
810 salt = p;
811 p += PVK_SALTLEN;
812 }
813 do_i2b(&p, pk, 0);
814 if (enclevel == 0)
815 return outlen;
816 else {
817 char psbuf[PEM_BUFSIZE];
818 unsigned char keybuf[20];
819 int enctmplen, inlen;
820 if (cb)
821 inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
822 else
823 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
824 if (inlen <= 0) {
825 PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ);
826 goto error;
827 }
828 if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
829 (unsigned char *)psbuf, inlen))
830 goto error;
831 if (enclevel == 1)
832 memset(keybuf + 5, 0, 11);
833 p = salt + PVK_SALTLEN + 8;
846ec07d 834 if (!EVP_EncryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
0f113f3e
MC
835 goto error;
836 OPENSSL_cleanse(keybuf, 20);
846ec07d 837 if (!EVP_DecryptUpdate(cctx, p, &enctmplen, p, pklen - 8))
0f113f3e 838 goto error;
846ec07d 839 if (!EVP_DecryptFinal_ex(cctx, p + enctmplen, &enctmplen))
0f113f3e
MC
840 goto error;
841 }
846ec07d 842 EVP_CIPHER_CTX_free(cctx);
0f113f3e
MC
843 return outlen;
844
845 error:
846ec07d 846 EVP_CIPHER_CTX_free(cctx);
0f113f3e
MC
847 return -1;
848}
a0156a92
DSH
849
850int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel,
0f113f3e
MC
851 pem_password_cb *cb, void *u)
852{
853 unsigned char *tmp = NULL;
854 int outlen, wrlen;
855 outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
856 if (outlen < 0)
857 return -1;
858 wrlen = BIO_write(out, tmp, outlen);
859 OPENSSL_free(tmp);
860 if (wrlen == outlen) {
861 PEMerr(PEM_F_I2B_PVK_BIO, PEM_R_BIO_WRITE_FAILURE);
862 return outlen;
863 }
864 return -1;
865}
866
867# endif
00a37b5a 868
d4f0339c 869#endif