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