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