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