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