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