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