]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
8020d79b | 2 | * Copyright 2005-2021 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> | |
1ffac6ca P |
26 | #include <openssl/kdf.h> |
27 | #include <openssl/core_names.h> | |
e77c13f8 RL |
28 | #include "internal/cryptlib.h" |
29 | #include "crypto/pem.h" | |
30 | #include "crypto/evp.h" | |
a0156a92 | 31 | |
0f113f3e MC |
32 | /* |
33 | * Utility function: read a DWORD (4 byte unsigned integer) in little endian | |
a0156a92 DSH |
34 | * format |
35 | */ | |
36 | ||
37 | static unsigned int read_ledword(const unsigned char **in) | |
0f113f3e MC |
38 | { |
39 | const unsigned char *p = *in; | |
40 | unsigned int ret; | |
a158f8cf | 41 | |
cbeb0bfa TM |
42 | ret = (unsigned int)*p++; |
43 | ret |= (unsigned int)*p++ << 8; | |
44 | ret |= (unsigned int)*p++ << 16; | |
45 | ret |= (unsigned int)*p++ << 24; | |
0f113f3e MC |
46 | *in = p; |
47 | return ret; | |
48 | } | |
49 | ||
50 | /* | |
51 | * Read a BIGNUM in little endian format. The docs say that this should take | |
52 | * up bitlen/8 bytes. | |
a0156a92 DSH |
53 | */ |
54 | ||
55 | static int read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r) | |
0f113f3e | 56 | { |
85a4807f DSH |
57 | *r = BN_lebin2bn(*in, nbyte, NULL); |
58 | if (*r == NULL) | |
0f113f3e | 59 | return 0; |
85a4807f DSH |
60 | *in += nbyte; |
61 | return 1; | |
0f113f3e | 62 | } |
a0156a92 | 63 | |
f4e46b81 RL |
64 | /* |
65 | * Create an EVP_PKEY from a type specific key. | |
66 | * This takes ownership of |key|, as long as the |evp_type| is acceptable | |
67 | * (EVP_PKEY_RSA or EVP_PKEY_DSA), even if the resulting EVP_PKEY wasn't | |
68 | * created. | |
69 | */ | |
70 | #define isdss_to_evp_type(isdss) \ | |
71 | (isdss == 0 ? EVP_PKEY_RSA : isdss == 1 ? EVP_PKEY_DSA : EVP_PKEY_NONE) | |
72 | static EVP_PKEY *evp_pkey_new0_key(void *key, int evp_type) | |
73 | { | |
74 | EVP_PKEY *pkey = NULL; | |
75 | ||
76 | /* | |
77 | * It's assumed that if |key| is NULL, something went wrong elsewhere | |
78 | * and suitable errors are already reported. | |
79 | */ | |
80 | if (key == NULL) | |
81 | return NULL; | |
82 | ||
83 | if (!ossl_assert(evp_type == EVP_PKEY_RSA || evp_type == EVP_PKEY_DSA)) { | |
84 | ERR_raise(ERR_LIB_PEM, ERR_R_INTERNAL_ERROR); | |
85 | return NULL; | |
86 | } | |
87 | ||
88 | if ((pkey = EVP_PKEY_new()) != NULL) { | |
89 | switch (evp_type) { | |
90 | case EVP_PKEY_RSA: | |
91 | if (EVP_PKEY_set1_RSA(pkey, key)) | |
92 | break; | |
e077455e | 93 | ERR_raise(ERR_LIB_PEM, ERR_R_EVP_LIB); |
f4e46b81 RL |
94 | EVP_PKEY_free(pkey); |
95 | pkey = NULL; | |
96 | break; | |
97 | #ifndef OPENSSL_NO_DSA | |
98 | case EVP_PKEY_DSA: | |
99 | if (EVP_PKEY_set1_DSA(pkey, key)) | |
100 | break; | |
e077455e | 101 | ERR_raise(ERR_LIB_PEM, ERR_R_EVP_LIB); |
f4e46b81 RL |
102 | EVP_PKEY_free(pkey); |
103 | pkey = NULL; | |
104 | break; | |
105 | #endif | |
106 | } | |
e077455e RL |
107 | } else { |
108 | ERR_raise(ERR_LIB_PEM, ERR_R_EVP_LIB); | |
f4e46b81 RL |
109 | } |
110 | ||
111 | switch (evp_type) { | |
112 | case EVP_PKEY_RSA: | |
113 | RSA_free(key); | |
114 | break; | |
115 | #ifndef OPENSSL_NO_DSA | |
116 | case EVP_PKEY_DSA: | |
117 | DSA_free(key); | |
118 | break; | |
119 | #endif | |
120 | } | |
121 | ||
f4e46b81 RL |
122 | return pkey; |
123 | } | |
124 | ||
a0156a92 DSH |
125 | /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */ |
126 | ||
0f113f3e MC |
127 | # define MS_PUBLICKEYBLOB 0x6 |
128 | # define MS_PRIVATEKEYBLOB 0x7 | |
129 | # define MS_RSA1MAGIC 0x31415352L | |
130 | # define MS_RSA2MAGIC 0x32415352L | |
131 | # define MS_DSS1MAGIC 0x31535344L | |
132 | # define MS_DSS2MAGIC 0x32535344L | |
a0156a92 | 133 | |
0f113f3e MC |
134 | # define MS_KEYALG_RSA_KEYX 0xa400 |
135 | # define MS_KEYALG_DSS_SIGN 0x2200 | |
a0156a92 | 136 | |
0f113f3e MC |
137 | # define MS_KEYTYPE_KEYX 0x1 |
138 | # define MS_KEYTYPE_SIGN 0x2 | |
a0156a92 DSH |
139 | |
140 | /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */ | |
0f113f3e | 141 | # define MS_PVKMAGIC 0xb0b5f11eL |
a0156a92 | 142 | /* Salt length for PVK files */ |
0f113f3e | 143 | # define PVK_SALTLEN 0x10 |
5f57abe2 DSH |
144 | /* Maximum length in PVK header */ |
145 | # define PVK_MAX_KEYLEN 102400 | |
146 | /* Maximum salt length */ | |
147 | # define PVK_MAX_SALTLEN 10240 | |
a0156a92 | 148 | |
f4e46b81 RL |
149 | /* |
150 | * Read the MSBLOB header and get relevant data from it. | |
151 | * | |
152 | * |pisdss| and |pispub| have a double role, as they can be used for | |
153 | * discovery as well as to check the the blob meets expectations. | |
154 | * |*pisdss| is the indicator for whether the key is a DSA key or not. | |
155 | * |*pispub| is the indicator for whether the key is public or not. | |
156 | * In both cases, the following input values apply: | |
157 | * | |
158 | * 0 Expected to not be what the variable indicates. | |
159 | * 1 Expected to be what the variable indicates. | |
160 | * -1 No expectations, this function will assign 0 or 1 depending on | |
161 | * header data. | |
162 | */ | |
f55838f3 RL |
163 | int ossl_do_blob_header(const unsigned char **in, unsigned int length, |
164 | unsigned int *pmagic, unsigned int *pbitlen, | |
165 | int *pisdss, int *pispub) | |
0f113f3e MC |
166 | { |
167 | const unsigned char *p = *in; | |
a158f8cf | 168 | |
0f113f3e MC |
169 | if (length < 16) |
170 | return 0; | |
171 | /* bType */ | |
f4e46b81 RL |
172 | switch (*p) { |
173 | case MS_PUBLICKEYBLOB: | |
0f113f3e | 174 | if (*pispub == 0) { |
9311d0c4 | 175 | ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB); |
0f113f3e MC |
176 | return 0; |
177 | } | |
178 | *pispub = 1; | |
f4e46b81 RL |
179 | break; |
180 | ||
181 | case MS_PRIVATEKEYBLOB: | |
0f113f3e | 182 | if (*pispub == 1) { |
9311d0c4 | 183 | ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB); |
0f113f3e MC |
184 | return 0; |
185 | } | |
186 | *pispub = 0; | |
f4e46b81 RL |
187 | break; |
188 | ||
189 | default: | |
0f113f3e | 190 | return 0; |
a158f8cf | 191 | } |
0f113f3e MC |
192 | p++; |
193 | /* Version */ | |
194 | if (*p++ != 0x2) { | |
9311d0c4 | 195 | ERR_raise(ERR_LIB_PEM, PEM_R_BAD_VERSION_NUMBER); |
0f113f3e MC |
196 | return 0; |
197 | } | |
198 | /* Ignore reserved, aiKeyAlg */ | |
199 | p += 6; | |
200 | *pmagic = read_ledword(&p); | |
201 | *pbitlen = read_ledword(&p); | |
0f113f3e | 202 | |
f4e46b81 RL |
203 | /* Consistency check for private vs public */ |
204 | switch (*pmagic) { | |
0f113f3e | 205 | case MS_DSS1MAGIC: |
0f113f3e MC |
206 | case MS_RSA1MAGIC: |
207 | if (*pispub == 0) { | |
9311d0c4 | 208 | ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB); |
0f113f3e MC |
209 | return 0; |
210 | } | |
211 | break; | |
212 | ||
213 | case MS_DSS2MAGIC: | |
0f113f3e MC |
214 | case MS_RSA2MAGIC: |
215 | if (*pispub == 1) { | |
9311d0c4 | 216 | ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB); |
0f113f3e MC |
217 | return 0; |
218 | } | |
219 | break; | |
220 | ||
f4e46b81 RL |
221 | default: |
222 | ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER); | |
223 | return -1; | |
224 | } | |
225 | ||
226 | /* Check that we got the expected type */ | |
227 | switch (*pmagic) { | |
228 | case MS_DSS1MAGIC: | |
229 | case MS_DSS2MAGIC: | |
230 | if (*pisdss == 0) { | |
231 | ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_DSS_KEY_BLOB); | |
232 | return 0; | |
233 | } | |
234 | *pisdss = 1; | |
235 | break; | |
236 | case MS_RSA1MAGIC: | |
237 | case MS_RSA2MAGIC: | |
238 | if (*pisdss == 1) { | |
239 | ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_RSA_KEY_BLOB); | |
240 | return 0; | |
241 | } | |
242 | *pisdss = 0; | |
243 | break; | |
244 | ||
0f113f3e | 245 | default: |
9311d0c4 | 246 | ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER); |
0f113f3e MC |
247 | return -1; |
248 | } | |
249 | *in = p; | |
250 | return 1; | |
251 | } | |
a0156a92 | 252 | |
f4e46b81 | 253 | unsigned int ossl_blob_length(unsigned bitlen, int isdss, int ispub) |
0f113f3e | 254 | { |
a158f8cf RL |
255 | unsigned int nbyte = (bitlen + 7) >> 3; |
256 | unsigned int hnbyte = (bitlen + 15) >> 4; | |
257 | ||
0f113f3e MC |
258 | if (isdss) { |
259 | ||
260 | /* | |
261 | * Expected length: 20 for q + 3 components bitlen each + 24 for seed | |
262 | * structure. | |
263 | */ | |
264 | if (ispub) | |
265 | return 44 + 3 * nbyte; | |
266 | /* | |
267 | * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed | |
268 | * structure. | |
269 | */ | |
270 | else | |
271 | return 64 + 2 * nbyte; | |
272 | } else { | |
273 | /* Expected length: 4 for 'e' + 'n' */ | |
274 | if (ispub) | |
275 | return 4 + nbyte; | |
276 | else | |
277 | /* | |
278 | * Expected length: 4 for 'e' and 7 other components. 2 | |
279 | * components are bitlen size, 5 are bitlen/2 | |
280 | */ | |
281 | return 4 + 2 * nbyte + 5 * hnbyte; | |
282 | } | |
283 | ||
284 | } | |
a0156a92 | 285 | |
f4e46b81 RL |
286 | static void *do_b2i_key(const unsigned char **in, unsigned int length, |
287 | int *isdss, int *ispub) | |
0f113f3e MC |
288 | { |
289 | const unsigned char *p = *in; | |
290 | unsigned int bitlen, magic; | |
f4e46b81 | 291 | void *key = NULL; |
a158f8cf | 292 | |
f4e46b81 | 293 | if (ossl_do_blob_header(&p, length, &magic, &bitlen, isdss, ispub) <= 0) { |
9311d0c4 | 294 | ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_HEADER_PARSE_ERROR); |
0f113f3e MC |
295 | return NULL; |
296 | } | |
297 | length -= 16; | |
f4e46b81 | 298 | if (length < ossl_blob_length(bitlen, *isdss, *ispub)) { |
9311d0c4 | 299 | ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT); |
0f113f3e MC |
300 | return NULL; |
301 | } | |
f4e46b81 RL |
302 | if (!*isdss) |
303 | key = ossl_b2i_RSA_after_header(&p, bitlen, *ispub); | |
a158f8cf RL |
304 | #ifndef OPENSSL_NO_DSA |
305 | else | |
f4e46b81 | 306 | key = ossl_b2i_DSA_after_header(&p, bitlen, *ispub); |
a158f8cf RL |
307 | #endif |
308 | ||
f4e46b81 RL |
309 | if (key == NULL) { |
310 | ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE); | |
311 | return NULL; | |
312 | } | |
313 | ||
314 | return key; | |
315 | } | |
316 | ||
317 | EVP_PKEY *ossl_b2i(const unsigned char **in, unsigned int length, int *ispub) | |
318 | { | |
319 | int isdss = -1; | |
320 | void *key = do_b2i_key(in, length, &isdss, ispub); | |
321 | ||
322 | return evp_pkey_new0_key(key, isdss_to_evp_type(isdss)); | |
0f113f3e | 323 | } |
a0156a92 | 324 | |
413835f5 | 325 | EVP_PKEY *ossl_b2i_bio(BIO *in, int *ispub) |
0f113f3e MC |
326 | { |
327 | const unsigned char *p; | |
328 | unsigned char hdr_buf[16], *buf = NULL; | |
329 | unsigned int bitlen, magic, length; | |
711d7ca5 | 330 | int isdss = -1; |
f4e46b81 RL |
331 | void *key = NULL; |
332 | EVP_PKEY *pkey = NULL; | |
a158f8cf | 333 | |
0f113f3e | 334 | if (BIO_read(in, hdr_buf, 16) != 16) { |
9311d0c4 | 335 | ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT); |
0f113f3e MC |
336 | return NULL; |
337 | } | |
338 | p = hdr_buf; | |
413835f5 | 339 | if (ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, ispub) <= 0) |
0f113f3e MC |
340 | return NULL; |
341 | ||
f4e46b81 | 342 | length = ossl_blob_length(bitlen, isdss, *ispub); |
66bcba14 | 343 | if (length > BLOB_MAX_LENGTH) { |
9311d0c4 | 344 | ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG); |
66bcba14 DSH |
345 | return NULL; |
346 | } | |
0f113f3e | 347 | buf = OPENSSL_malloc(length); |
e077455e | 348 | if (buf == NULL) |
0f113f3e | 349 | goto err; |
0f113f3e MC |
350 | p = buf; |
351 | if (BIO_read(in, buf, length) != (int)length) { | |
9311d0c4 | 352 | ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT); |
0f113f3e MC |
353 | goto err; |
354 | } | |
355 | ||
a158f8cf | 356 | if (!isdss) |
f4e46b81 | 357 | key = ossl_b2i_RSA_after_header(&p, bitlen, *ispub); |
a158f8cf RL |
358 | #ifndef OPENSSL_NO_DSA |
359 | else | |
f4e46b81 | 360 | key = ossl_b2i_DSA_after_header(&p, bitlen, *ispub); |
a158f8cf RL |
361 | #endif |
362 | ||
f4e46b81 | 363 | if (key == NULL) { |
a158f8cf | 364 | ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE); |
f4e46b81 RL |
365 | goto err; |
366 | } | |
0f113f3e | 367 | |
f4e46b81 | 368 | pkey = evp_pkey_new0_key(key, isdss_to_evp_type(isdss)); |
0f113f3e | 369 | err: |
b548a1f1 | 370 | OPENSSL_free(buf); |
f4e46b81 | 371 | return pkey; |
0f113f3e | 372 | } |
a0156a92 | 373 | |
a158f8cf | 374 | #ifndef OPENSSL_NO_DSA |
f4e46b81 RL |
375 | DSA *ossl_b2i_DSA_after_header(const unsigned char **in, unsigned int bitlen, |
376 | int ispub) | |
0f113f3e MC |
377 | { |
378 | const unsigned char *p = *in; | |
0f113f3e MC |
379 | DSA *dsa = NULL; |
380 | BN_CTX *ctx = NULL; | |
1258396d MC |
381 | BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL; |
382 | BIGNUM *pub_key = NULL; | |
a158f8cf | 383 | unsigned int nbyte = (bitlen + 7) >> 3; |
0f113f3e MC |
384 | |
385 | dsa = DSA_new(); | |
f4e46b81 | 386 | if (dsa == NULL) |
e077455e | 387 | goto dsaerr; |
1258396d | 388 | if (!read_lebn(&p, nbyte, &pbn)) |
e077455e | 389 | goto bnerr; |
1258396d MC |
390 | |
391 | if (!read_lebn(&p, 20, &qbn)) | |
e077455e | 392 | goto bnerr; |
1258396d MC |
393 | |
394 | if (!read_lebn(&p, nbyte, &gbn)) | |
e077455e | 395 | goto bnerr; |
1258396d | 396 | |
0f113f3e | 397 | if (ispub) { |
1258396d | 398 | if (!read_lebn(&p, nbyte, &pub_key)) |
e077455e | 399 | goto bnerr; |
0f113f3e | 400 | } else { |
1258396d | 401 | if (!read_lebn(&p, 20, &priv_key)) |
e077455e | 402 | goto bnerr; |
1258396d | 403 | |
724339ff CPG |
404 | /* Set constant time flag before public key calculation */ |
405 | BN_set_flags(priv_key, BN_FLG_CONSTTIME); | |
406 | ||
0f113f3e | 407 | /* Calculate public key */ |
1258396d MC |
408 | pub_key = BN_new(); |
409 | if (pub_key == NULL) | |
e077455e | 410 | goto bnerr; |
75ebbd9a | 411 | if ((ctx = BN_CTX_new()) == NULL) |
e077455e | 412 | goto bnerr; |
0f113f3e | 413 | |
1258396d | 414 | if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx)) |
e077455e | 415 | goto bnerr; |
1258396d | 416 | |
0f113f3e | 417 | BN_CTX_free(ctx); |
d288d7fc | 418 | ctx = NULL; |
0f113f3e | 419 | } |
1258396d | 420 | if (!DSA_set0_pqg(dsa, pbn, qbn, gbn)) |
e077455e | 421 | goto dsaerr; |
1258396d MC |
422 | pbn = qbn = gbn = NULL; |
423 | if (!DSA_set0_key(dsa, pub_key, priv_key)) | |
e077455e | 424 | goto dsaerr; |
d288d7fc | 425 | pub_key = priv_key = NULL; |
0f113f3e | 426 | |
0f113f3e | 427 | *in = p; |
f4e46b81 | 428 | return dsa; |
0f113f3e | 429 | |
e077455e RL |
430 | dsaerr: |
431 | ERR_raise(ERR_LIB_PEM, ERR_R_DSA_LIB); | |
432 | goto err; | |
433 | bnerr: | |
434 | ERR_raise(ERR_LIB_PEM, ERR_R_BN_LIB); | |
435 | ||
436 | err: | |
d6407083 | 437 | DSA_free(dsa); |
1258396d MC |
438 | BN_free(pbn); |
439 | BN_free(qbn); | |
440 | BN_free(gbn); | |
441 | BN_free(pub_key); | |
442 | BN_free(priv_key); | |
23a1d5e9 | 443 | BN_CTX_free(ctx); |
0f113f3e MC |
444 | return NULL; |
445 | } | |
a158f8cf | 446 | #endif |
a0156a92 | 447 | |
f4e46b81 RL |
448 | RSA *ossl_b2i_RSA_after_header(const unsigned char **in, unsigned int bitlen, |
449 | int ispub) | |
0f113f3e | 450 | { |
9862e9aa | 451 | const unsigned char *pin = *in; |
9862e9aa | 452 | BIGNUM *e = NULL, *n = NULL, *d = NULL; |
204cf940 | 453 | BIGNUM *p = NULL, *q = NULL, *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL; |
0f113f3e | 454 | RSA *rsa = NULL; |
a158f8cf RL |
455 | unsigned int nbyte = (bitlen + 7) >> 3; |
456 | unsigned int hnbyte = (bitlen + 15) >> 4; | |
457 | ||
0f113f3e | 458 | rsa = RSA_new(); |
f4e46b81 | 459 | if (rsa == NULL) |
e077455e | 460 | goto rsaerr; |
9862e9aa RL |
461 | e = BN_new(); |
462 | if (e == NULL) | |
e077455e | 463 | goto bnerr; |
9862e9aa | 464 | if (!BN_set_word(e, read_ledword(&pin))) |
e077455e | 465 | goto bnerr; |
9862e9aa | 466 | if (!read_lebn(&pin, nbyte, &n)) |
e077455e | 467 | goto bnerr; |
0f113f3e | 468 | if (!ispub) { |
9862e9aa | 469 | if (!read_lebn(&pin, hnbyte, &p)) |
e077455e | 470 | goto bnerr; |
9862e9aa | 471 | if (!read_lebn(&pin, hnbyte, &q)) |
e077455e | 472 | goto bnerr; |
9862e9aa | 473 | if (!read_lebn(&pin, hnbyte, &dmp1)) |
e077455e | 474 | goto bnerr; |
9862e9aa | 475 | if (!read_lebn(&pin, hnbyte, &dmq1)) |
e077455e | 476 | goto bnerr; |
9862e9aa | 477 | if (!read_lebn(&pin, hnbyte, &iqmp)) |
e077455e | 478 | goto bnerr; |
9862e9aa | 479 | if (!read_lebn(&pin, nbyte, &d)) |
e077455e | 480 | goto bnerr; |
d288d7fc | 481 | if (!RSA_set0_factors(rsa, p, q)) |
e077455e | 482 | goto rsaerr; |
d288d7fc BE |
483 | p = q = NULL; |
484 | if (!RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp)) | |
e077455e | 485 | goto rsaerr; |
d288d7fc | 486 | dmp1 = dmq1 = iqmp = NULL; |
0f113f3e | 487 | } |
d288d7fc | 488 | if (!RSA_set0_key(rsa, n, e, d)) |
e077455e | 489 | goto rsaerr; |
d288d7fc | 490 | n = e = d = NULL; |
0f113f3e | 491 | |
9862e9aa | 492 | *in = pin; |
f4e46b81 | 493 | return rsa; |
e077455e RL |
494 | |
495 | rsaerr: | |
496 | ERR_raise(ERR_LIB_PEM, ERR_R_RSA_LIB); | |
497 | goto err; | |
498 | bnerr: | |
499 | ERR_raise(ERR_LIB_PEM, ERR_R_BN_LIB); | |
500 | ||
501 | err: | |
204cf940 MC |
502 | BN_free(e); |
503 | BN_free(n); | |
504 | BN_free(p); | |
505 | BN_free(q); | |
506 | BN_free(dmp1); | |
507 | BN_free(dmq1); | |
508 | BN_free(iqmp); | |
509 | BN_free(d); | |
d6407083 | 510 | RSA_free(rsa); |
0f113f3e MC |
511 | return NULL; |
512 | } | |
a0156a92 DSH |
513 | |
514 | EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length) | |
0f113f3e | 515 | { |
413835f5 RL |
516 | int ispub = 0; |
517 | ||
518 | return ossl_b2i(in, length, &ispub); | |
0f113f3e | 519 | } |
a0156a92 DSH |
520 | |
521 | EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length) | |
0f113f3e | 522 | { |
413835f5 RL |
523 | int ispub = 1; |
524 | ||
525 | return ossl_b2i(in, length, &ispub); | |
0f113f3e | 526 | } |
a0156a92 DSH |
527 | |
528 | EVP_PKEY *b2i_PrivateKey_bio(BIO *in) | |
0f113f3e | 529 | { |
413835f5 RL |
530 | int ispub = 0; |
531 | ||
532 | return ossl_b2i_bio(in, &ispub); | |
0f113f3e | 533 | } |
a0156a92 DSH |
534 | |
535 | EVP_PKEY *b2i_PublicKey_bio(BIO *in) | |
0f113f3e | 536 | { |
413835f5 RL |
537 | int ispub = 1; |
538 | ||
539 | return ossl_b2i_bio(in, &ispub); | |
0f113f3e | 540 | } |
a0156a92 DSH |
541 | |
542 | static void write_ledword(unsigned char **out, unsigned int dw) | |
0f113f3e MC |
543 | { |
544 | unsigned char *p = *out; | |
a158f8cf | 545 | |
0f113f3e MC |
546 | *p++ = dw & 0xff; |
547 | *p++ = (dw >> 8) & 0xff; | |
548 | *p++ = (dw >> 16) & 0xff; | |
549 | *p++ = (dw >> 24) & 0xff; | |
550 | *out = p; | |
551 | } | |
a0156a92 DSH |
552 | |
553 | static void write_lebn(unsigned char **out, const BIGNUM *bn, int len) | |
0f113f3e | 554 | { |
85a4807f DSH |
555 | BN_bn2lebinpad(bn, *out, len); |
556 | *out += len; | |
0f113f3e | 557 | } |
a0156a92 | 558 | |
7bc0fdd3 MC |
559 | static int check_bitlen_rsa(const RSA *rsa, int ispub, unsigned int *magic); |
560 | static void write_rsa(unsigned char **out, const RSA *rsa, int ispub); | |
a158f8cf RL |
561 | |
562 | #ifndef OPENSSL_NO_DSA | |
7bc0fdd3 MC |
563 | static int check_bitlen_dsa(const DSA *dsa, int ispub, unsigned int *magic); |
564 | static void write_dsa(unsigned char **out, const DSA *dsa, int ispub); | |
a158f8cf | 565 | #endif |
0f113f3e | 566 | |
de0799b0 | 567 | static int do_i2b(unsigned char **out, const EVP_PKEY *pk, int ispub) |
0f113f3e MC |
568 | { |
569 | unsigned char *p; | |
a158f8cf | 570 | unsigned int bitlen = 0, magic = 0, keyalg = 0; |
e77c13f8 | 571 | int outlen = -1, noinc = 0; |
a158f8cf | 572 | |
f4e46b81 | 573 | if (EVP_PKEY_is_a(pk, "RSA")) { |
3aeb9348 | 574 | bitlen = check_bitlen_rsa(EVP_PKEY_get0_RSA(pk), ispub, &magic); |
0f113f3e | 575 | keyalg = MS_KEYALG_RSA_KEYX; |
a158f8cf | 576 | #ifndef OPENSSL_NO_DSA |
f4e46b81 | 577 | } else if (EVP_PKEY_is_a(pk, "DSA")) { |
a158f8cf RL |
578 | bitlen = check_bitlen_dsa(EVP_PKEY_get0_DSA(pk), ispub, &magic); |
579 | keyalg = MS_KEYALG_DSS_SIGN; | |
580 | #endif | |
581 | } | |
e77c13f8 RL |
582 | if (bitlen == 0) { |
583 | goto end; | |
584 | } | |
f4e46b81 RL |
585 | outlen = 16 |
586 | + ossl_blob_length(bitlen, keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub); | |
0f113f3e | 587 | if (out == NULL) |
e77c13f8 | 588 | goto end; |
0f113f3e MC |
589 | if (*out) |
590 | p = *out; | |
591 | else { | |
cdb10bae | 592 | if ((p = OPENSSL_malloc(outlen)) == NULL) { |
e77c13f8 RL |
593 | outlen = -1; |
594 | goto end; | |
cdb10bae | 595 | } |
0f113f3e MC |
596 | *out = p; |
597 | noinc = 1; | |
598 | } | |
599 | if (ispub) | |
600 | *p++ = MS_PUBLICKEYBLOB; | |
601 | else | |
602 | *p++ = MS_PRIVATEKEYBLOB; | |
603 | *p++ = 0x2; | |
604 | *p++ = 0; | |
605 | *p++ = 0; | |
606 | write_ledword(&p, keyalg); | |
607 | write_ledword(&p, magic); | |
608 | write_ledword(&p, bitlen); | |
a158f8cf | 609 | if (keyalg == MS_KEYALG_RSA_KEYX) |
3aeb9348 | 610 | write_rsa(&p, EVP_PKEY_get0_RSA(pk), ispub); |
a158f8cf RL |
611 | #ifndef OPENSSL_NO_DSA |
612 | else | |
613 | write_dsa(&p, EVP_PKEY_get0_DSA(pk), ispub); | |
614 | #endif | |
0f113f3e MC |
615 | if (!noinc) |
616 | *out += outlen; | |
e77c13f8 | 617 | end: |
0f113f3e MC |
618 | return outlen; |
619 | } | |
a0156a92 | 620 | |
de0799b0 | 621 | static int do_i2b_bio(BIO *out, const EVP_PKEY *pk, int ispub) |
0f113f3e MC |
622 | { |
623 | unsigned char *tmp = NULL; | |
624 | int outlen, wrlen; | |
a158f8cf | 625 | |
0f113f3e MC |
626 | outlen = do_i2b(&tmp, pk, ispub); |
627 | if (outlen < 0) | |
628 | return -1; | |
629 | wrlen = BIO_write(out, tmp, outlen); | |
630 | OPENSSL_free(tmp); | |
631 | if (wrlen == outlen) | |
632 | return outlen; | |
633 | return -1; | |
634 | } | |
a0156a92 | 635 | |
7bc0fdd3 | 636 | static int check_bitlen_rsa(const RSA *rsa, int ispub, unsigned int *pmagic) |
0f113f3e MC |
637 | { |
638 | int nbyte, hnbyte, bitlen; | |
2ac6115d | 639 | const BIGNUM *e; |
9862e9aa | 640 | |
1e7c159d | 641 | RSA_get0_key(rsa, NULL, &e, NULL); |
9862e9aa | 642 | if (BN_num_bits(e) > 32) |
0f113f3e | 643 | goto badkey; |
9862e9aa RL |
644 | bitlen = RSA_bits(rsa); |
645 | nbyte = RSA_size(rsa); | |
646 | hnbyte = (bitlen + 15) >> 4; | |
0f113f3e MC |
647 | if (ispub) { |
648 | *pmagic = MS_RSA1MAGIC; | |
649 | return bitlen; | |
650 | } else { | |
2ac6115d | 651 | const BIGNUM *d, *p, *q, *iqmp, *dmp1, *dmq1; |
9862e9aa | 652 | |
0f113f3e | 653 | *pmagic = MS_RSA2MAGIC; |
9862e9aa | 654 | |
0f113f3e MC |
655 | /* |
656 | * For private key each component must fit within nbyte or hnbyte. | |
657 | */ | |
9862e9aa RL |
658 | RSA_get0_key(rsa, NULL, NULL, &d); |
659 | if (BN_num_bytes(d) > nbyte) | |
0f113f3e | 660 | goto badkey; |
9862e9aa RL |
661 | RSA_get0_factors(rsa, &p, &q); |
662 | RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp); | |
663 | if ((BN_num_bytes(iqmp) > hnbyte) | |
664 | || (BN_num_bytes(p) > hnbyte) | |
665 | || (BN_num_bytes(q) > hnbyte) | |
666 | || (BN_num_bytes(dmp1) > hnbyte) | |
667 | || (BN_num_bytes(dmq1) > hnbyte)) | |
0f113f3e MC |
668 | goto badkey; |
669 | } | |
670 | return bitlen; | |
671 | badkey: | |
9311d0c4 | 672 | ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS); |
0f113f3e MC |
673 | return 0; |
674 | } | |
a0156a92 | 675 | |
7bc0fdd3 | 676 | static void write_rsa(unsigned char **out, const RSA *rsa, int ispub) |
0f113f3e MC |
677 | { |
678 | int nbyte, hnbyte; | |
2ac6115d | 679 | const BIGNUM *n, *d, *e, *p, *q, *iqmp, *dmp1, *dmq1; |
9862e9aa RL |
680 | |
681 | nbyte = RSA_size(rsa); | |
682 | hnbyte = (RSA_bits(rsa) + 15) >> 4; | |
1e7c159d | 683 | RSA_get0_key(rsa, &n, &e, &d); |
9862e9aa | 684 | write_lebn(out, e, 4); |
159f6e7e | 685 | write_lebn(out, n, nbyte); |
0f113f3e MC |
686 | if (ispub) |
687 | return; | |
9862e9aa RL |
688 | RSA_get0_factors(rsa, &p, &q); |
689 | RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp); | |
690 | write_lebn(out, p, hnbyte); | |
691 | write_lebn(out, q, hnbyte); | |
692 | write_lebn(out, dmp1, hnbyte); | |
693 | write_lebn(out, dmq1, hnbyte); | |
694 | write_lebn(out, iqmp, hnbyte); | |
695 | write_lebn(out, d, nbyte); | |
0f113f3e MC |
696 | } |
697 | ||
a158f8cf | 698 | #ifndef OPENSSL_NO_DSA |
7bc0fdd3 | 699 | static int check_bitlen_dsa(const DSA *dsa, int ispub, unsigned int *pmagic) |
a158f8cf RL |
700 | { |
701 | int bitlen; | |
702 | const BIGNUM *p = NULL, *q = NULL, *g = NULL; | |
703 | const BIGNUM *pub_key = NULL, *priv_key = NULL; | |
704 | ||
705 | DSA_get0_pqg(dsa, &p, &q, &g); | |
706 | DSA_get0_key(dsa, &pub_key, &priv_key); | |
707 | bitlen = BN_num_bits(p); | |
708 | if ((bitlen & 7) || (BN_num_bits(q) != 160) | |
709 | || (BN_num_bits(g) > bitlen)) | |
710 | goto badkey; | |
711 | if (ispub) { | |
712 | if (BN_num_bits(pub_key) > bitlen) | |
713 | goto badkey; | |
714 | *pmagic = MS_DSS1MAGIC; | |
715 | } else { | |
716 | if (BN_num_bits(priv_key) > 160) | |
717 | goto badkey; | |
718 | *pmagic = MS_DSS2MAGIC; | |
719 | } | |
720 | ||
721 | return bitlen; | |
722 | badkey: | |
723 | ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS); | |
724 | return 0; | |
725 | } | |
726 | ||
7bc0fdd3 | 727 | static void write_dsa(unsigned char **out, const DSA *dsa, int ispub) |
0f113f3e MC |
728 | { |
729 | int nbyte; | |
2ac6115d RL |
730 | const BIGNUM *p = NULL, *q = NULL, *g = NULL; |
731 | const BIGNUM *pub_key = NULL, *priv_key = NULL; | |
6e9fa57c MC |
732 | |
733 | DSA_get0_pqg(dsa, &p, &q, &g); | |
734 | DSA_get0_key(dsa, &pub_key, &priv_key); | |
735 | nbyte = BN_num_bytes(p); | |
736 | write_lebn(out, p, nbyte); | |
737 | write_lebn(out, q, 20); | |
738 | write_lebn(out, g, nbyte); | |
0f113f3e | 739 | if (ispub) |
6e9fa57c | 740 | write_lebn(out, pub_key, nbyte); |
0f113f3e | 741 | else |
6e9fa57c | 742 | write_lebn(out, priv_key, 20); |
0f113f3e MC |
743 | /* Set "invalid" for seed structure values */ |
744 | memset(*out, 0xff, 24); | |
745 | *out += 24; | |
746 | return; | |
747 | } | |
a158f8cf | 748 | #endif |
a0156a92 | 749 | |
de0799b0 | 750 | int i2b_PrivateKey_bio(BIO *out, const EVP_PKEY *pk) |
0f113f3e MC |
751 | { |
752 | return do_i2b_bio(out, pk, 0); | |
753 | } | |
a0156a92 | 754 | |
de0799b0 | 755 | int i2b_PublicKey_bio(BIO *out, const EVP_PKEY *pk) |
0f113f3e MC |
756 | { |
757 | return do_i2b_bio(out, pk, 1); | |
758 | } | |
a0156a92 | 759 | |
f55838f3 RL |
760 | int ossl_do_PVK_header(const unsigned char **in, unsigned int length, |
761 | int skip_magic, | |
762 | unsigned int *psaltlen, unsigned int *pkeylen) | |
0f113f3e MC |
763 | { |
764 | const unsigned char *p = *in; | |
765 | unsigned int pvk_magic, is_encrypted; | |
12a765a5 | 766 | |
0f113f3e MC |
767 | if (skip_magic) { |
768 | if (length < 20) { | |
9311d0c4 | 769 | ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT); |
0f113f3e MC |
770 | return 0; |
771 | } | |
0f113f3e MC |
772 | } else { |
773 | if (length < 24) { | |
9311d0c4 | 774 | ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT); |
0f113f3e MC |
775 | return 0; |
776 | } | |
0f113f3e MC |
777 | pvk_magic = read_ledword(&p); |
778 | if (pvk_magic != MS_PVKMAGIC) { | |
9311d0c4 | 779 | ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER); |
0f113f3e MC |
780 | return 0; |
781 | } | |
782 | } | |
783 | /* Skip reserved */ | |
784 | p += 4; | |
785 | /* | |
786 | * keytype = | |
787 | */ read_ledword(&p); | |
788 | is_encrypted = read_ledword(&p); | |
789 | *psaltlen = read_ledword(&p); | |
790 | *pkeylen = read_ledword(&p); | |
791 | ||
5f57abe2 DSH |
792 | if (*pkeylen > PVK_MAX_KEYLEN || *psaltlen > PVK_MAX_SALTLEN) |
793 | return 0; | |
794 | ||
12a765a5 | 795 | if (is_encrypted && *psaltlen == 0) { |
9311d0c4 | 796 | ERR_raise(ERR_LIB_PEM, PEM_R_INCONSISTENT_HEADER); |
0f113f3e MC |
797 | return 0; |
798 | } | |
799 | ||
800 | *in = p; | |
801 | return 1; | |
802 | } | |
803 | ||
a158f8cf | 804 | #ifndef OPENSSL_NO_RC4 |
1ffac6ca | 805 | static int derive_pvk_key(unsigned char *key, size_t keylen, |
0f113f3e | 806 | const unsigned char *salt, unsigned int saltlen, |
169eca60 JS |
807 | const unsigned char *pass, int passlen, |
808 | OSSL_LIB_CTX *libctx, const char *propq) | |
0f113f3e | 809 | { |
1ffac6ca P |
810 | EVP_KDF *kdf; |
811 | EVP_KDF_CTX *ctx; | |
812 | OSSL_PARAM params[5], *p = params; | |
813 | int rv; | |
a158f8cf | 814 | |
1ffac6ca P |
815 | if ((kdf = EVP_KDF_fetch(libctx, "PVKKDF", propq)) == NULL) |
816 | return 0; | |
817 | ctx = EVP_KDF_CTX_new(kdf); | |
818 | EVP_KDF_free(kdf); | |
819 | if (ctx == NULL) | |
820 | return 0; | |
0f113f3e | 821 | |
1ffac6ca P |
822 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, |
823 | (void *)salt, saltlen); | |
824 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, | |
825 | (void *)pass, passlen); | |
826 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, SN_sha1, 0); | |
827 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_PROPERTIES, | |
828 | (char *)propq, 0); | |
829 | *p = OSSL_PARAM_construct_end(); | |
830 | ||
831 | rv = EVP_KDF_derive(ctx, key, keylen, params); | |
832 | EVP_KDF_CTX_free(ctx); | |
0f113f3e MC |
833 | return rv; |
834 | } | |
a158f8cf | 835 | #endif |
a0156a92 | 836 | |
f4e46b81 | 837 | static void *do_PVK_body_key(const unsigned char **in, |
0f113f3e | 838 | unsigned int saltlen, unsigned int keylen, |
f4e46b81 | 839 | pem_password_cb *cb, void *u, |
169eca60 JS |
840 | int *isdss, int *ispub, |
841 | OSSL_LIB_CTX *libctx, const char *propq) | |
0f113f3e | 842 | { |
0f113f3e | 843 | const unsigned char *p = *in; |
a158f8cf | 844 | unsigned char *enctmp = NULL; |
0239283d | 845 | unsigned char keybuf[20]; |
f4e46b81 | 846 | void *key = NULL; |
169eca60 JS |
847 | #ifndef OPENSSL_NO_RC4 |
848 | EVP_CIPHER *rc4 = NULL; | |
849 | #endif | |
846ec07d | 850 | EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new(); |
169eca60 | 851 | |
9dddcd90 | 852 | if (cctx == NULL) { |
e077455e | 853 | ERR_raise(ERR_LIB_PEM, ERR_R_EVP_LIB); |
9dddcd90 | 854 | goto err; |
855 | } | |
856 | ||
0f113f3e | 857 | if (saltlen) { |
a158f8cf RL |
858 | #ifndef OPENSSL_NO_RC4 |
859 | unsigned int magic; | |
0f113f3e | 860 | char psbuf[PEM_BUFSIZE]; |
0f113f3e | 861 | int enctmplen, inlen; |
a158f8cf RL |
862 | unsigned char *q; |
863 | ||
0f113f3e MC |
864 | if (cb) |
865 | inlen = cb(psbuf, PEM_BUFSIZE, 0, u); | |
866 | else | |
867 | inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u); | |
c82c3462 | 868 | if (inlen < 0) { |
9311d0c4 | 869 | ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ); |
3f6c7691 | 870 | goto err; |
0f113f3e MC |
871 | } |
872 | enctmp = OPENSSL_malloc(keylen + 8); | |
e077455e | 873 | if (enctmp == NULL) |
3f6c7691 | 874 | goto err; |
1ffac6ca | 875 | if (!derive_pvk_key(keybuf, sizeof(keybuf), p, saltlen, |
169eca60 | 876 | (unsigned char *)psbuf, inlen, libctx, propq)) |
3f6c7691 | 877 | goto err; |
0f113f3e MC |
878 | p += saltlen; |
879 | /* Copy BLOBHEADER across, decrypt rest */ | |
880 | memcpy(enctmp, p, 8); | |
881 | p += 8; | |
882 | if (keylen < 8) { | |
9311d0c4 | 883 | ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT); |
3f6c7691 | 884 | goto err; |
0f113f3e MC |
885 | } |
886 | inlen = keylen - 8; | |
887 | q = enctmp + 8; | |
169eca60 JS |
888 | if ((rc4 = EVP_CIPHER_fetch(libctx, "RC4", propq)) == NULL) |
889 | goto err; | |
890 | if (!EVP_DecryptInit_ex(cctx, rc4, NULL, keybuf, NULL)) | |
0f113f3e | 891 | goto err; |
846ec07d | 892 | if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen)) |
0f113f3e | 893 | goto err; |
846ec07d | 894 | if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen)) |
0f113f3e MC |
895 | goto err; |
896 | magic = read_ledword((const unsigned char **)&q); | |
897 | if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) { | |
898 | q = enctmp + 8; | |
899 | memset(keybuf + 5, 0, 11); | |
169eca60 | 900 | if (!EVP_DecryptInit_ex(cctx, rc4, NULL, keybuf, NULL)) |
0f113f3e | 901 | goto err; |
846ec07d | 902 | if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen)) |
0f113f3e | 903 | goto err; |
846ec07d | 904 | if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen)) |
0f113f3e MC |
905 | goto err; |
906 | magic = read_ledword((const unsigned char **)&q); | |
907 | if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) { | |
9311d0c4 | 908 | ERR_raise(ERR_LIB_PEM, PEM_R_BAD_DECRYPT); |
0f113f3e MC |
909 | goto err; |
910 | } | |
0239283d | 911 | } |
0f113f3e | 912 | p = enctmp; |
a158f8cf RL |
913 | #else |
914 | ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER); | |
915 | goto err; | |
916 | #endif | |
0f113f3e MC |
917 | } |
918 | ||
f4e46b81 | 919 | key = do_b2i_key(&p, keylen, isdss, ispub); |
0f113f3e | 920 | err: |
846ec07d | 921 | EVP_CIPHER_CTX_free(cctx); |
169eca60 JS |
922 | #ifndef OPENSSL_NO_RC4 |
923 | EVP_CIPHER_free(rc4); | |
924 | #endif | |
0239283d SL |
925 | if (enctmp != NULL) { |
926 | OPENSSL_cleanse(keybuf, sizeof(keybuf)); | |
927 | OPENSSL_free(enctmp); | |
928 | } | |
f4e46b81 | 929 | return key; |
0f113f3e | 930 | } |
a0156a92 | 931 | |
f4e46b81 | 932 | static void *do_PVK_key_bio(BIO *in, pem_password_cb *cb, void *u, |
169eca60 JS |
933 | int *isdss, int *ispub, |
934 | OSSL_LIB_CTX *libctx, const char *propq) | |
0f113f3e MC |
935 | { |
936 | unsigned char pvk_hdr[24], *buf = NULL; | |
937 | const unsigned char *p; | |
938 | int buflen; | |
f4e46b81 | 939 | void *key = NULL; |
0f113f3e | 940 | unsigned int saltlen, keylen; |
a158f8cf | 941 | |
0f113f3e | 942 | if (BIO_read(in, pvk_hdr, 24) != 24) { |
9311d0c4 | 943 | ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT); |
0f113f3e MC |
944 | return NULL; |
945 | } | |
946 | p = pvk_hdr; | |
947 | ||
f55838f3 | 948 | if (!ossl_do_PVK_header(&p, 24, 0, &saltlen, &keylen)) |
0f113f3e MC |
949 | return 0; |
950 | buflen = (int)keylen + saltlen; | |
951 | buf = OPENSSL_malloc(buflen); | |
e077455e | 952 | if (buf == NULL) |
0f113f3e | 953 | return 0; |
0f113f3e MC |
954 | p = buf; |
955 | if (BIO_read(in, buf, buflen) != buflen) { | |
9311d0c4 | 956 | ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT); |
0f113f3e MC |
957 | goto err; |
958 | } | |
169eca60 | 959 | key = do_PVK_body_key(&p, saltlen, keylen, cb, u, isdss, ispub, libctx, propq); |
0f113f3e MC |
960 | |
961 | err: | |
4b45c6e5 | 962 | OPENSSL_clear_free(buf, buflen); |
f4e46b81 RL |
963 | return key; |
964 | } | |
965 | ||
966 | #ifndef OPENSSL_NO_DSA | |
5e2d22d5 JS |
967 | DSA *b2i_DSA_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u, |
968 | OSSL_LIB_CTX *libctx, const char *propq) | |
f4e46b81 RL |
969 | { |
970 | int isdss = 1; | |
971 | int ispub = 0; /* PVK keys are always private */ | |
972 | ||
5e2d22d5 JS |
973 | return do_PVK_key_bio(in, cb, u, &isdss, &ispub, libctx, propq); |
974 | } | |
975 | ||
976 | DSA *b2i_DSA_PVK_bio(BIO *in, pem_password_cb *cb, void *u) | |
977 | { | |
978 | return b2i_DSA_PVK_bio_ex(in, cb, u, NULL, NULL); | |
f4e46b81 RL |
979 | } |
980 | #endif | |
981 | ||
5e2d22d5 JS |
982 | RSA *b2i_RSA_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u, |
983 | OSSL_LIB_CTX *libctx, const char *propq) | |
f4e46b81 RL |
984 | { |
985 | int isdss = 0; | |
986 | int ispub = 0; /* PVK keys are always private */ | |
987 | ||
5e2d22d5 JS |
988 | return do_PVK_key_bio(in, cb, u, &isdss, &ispub, libctx, propq); |
989 | } | |
990 | ||
991 | RSA *b2i_RSA_PVK_bio(BIO *in, pem_password_cb *cb, void *u) | |
992 | { | |
993 | return b2i_RSA_PVK_bio_ex(in, cb, u, NULL, NULL); | |
f4e46b81 RL |
994 | } |
995 | ||
169eca60 JS |
996 | EVP_PKEY *b2i_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u, |
997 | OSSL_LIB_CTX *libctx, const char *propq) | |
f4e46b81 RL |
998 | { |
999 | int isdss = -1; | |
1000 | int ispub = -1; | |
169eca60 | 1001 | void *key = do_PVK_key_bio(in, cb, u, &isdss, &ispub, NULL, NULL); |
f4e46b81 RL |
1002 | |
1003 | return evp_pkey_new0_key(key, isdss_to_evp_type(isdss)); | |
0f113f3e MC |
1004 | } |
1005 | ||
169eca60 JS |
1006 | EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u) |
1007 | { | |
1008 | return b2i_PVK_bio_ex(in, cb, u, NULL, NULL); | |
1009 | } | |
1010 | ||
de0799b0 | 1011 | static int i2b_PVK(unsigned char **out, const EVP_PKEY *pk, int enclevel, |
169eca60 JS |
1012 | pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx, |
1013 | const char *propq) | |
0f113f3e | 1014 | { |
169eca60 | 1015 | int ret = -1; |
0f113f3e | 1016 | int outlen = 24, pklen; |
a158f8cf | 1017 | unsigned char *p = NULL, *start = NULL; |
8e588e28 | 1018 | EVP_CIPHER_CTX *cctx = NULL; |
a158f8cf RL |
1019 | #ifndef OPENSSL_NO_RC4 |
1020 | unsigned char *salt = NULL; | |
169eca60 | 1021 | EVP_CIPHER *rc4 = NULL; |
a158f8cf RL |
1022 | #endif |
1023 | ||
0f113f3e MC |
1024 | if (enclevel) |
1025 | outlen += PVK_SALTLEN; | |
1026 | pklen = do_i2b(NULL, pk, 0); | |
1027 | if (pklen < 0) | |
1028 | return -1; | |
1029 | outlen += pklen; | |
8e588e28 | 1030 | if (out == NULL) |
0f113f3e | 1031 | return outlen; |
8e588e28 | 1032 | if (*out != NULL) { |
0f113f3e | 1033 | p = *out; |
8e588e28 | 1034 | } else { |
febb096c | 1035 | start = p = OPENSSL_malloc(outlen); |
e077455e | 1036 | if (p == NULL) |
0f113f3e | 1037 | return -1; |
0f113f3e MC |
1038 | } |
1039 | ||
8e588e28 MC |
1040 | cctx = EVP_CIPHER_CTX_new(); |
1041 | if (cctx == NULL) | |
098c1e3d | 1042 | goto error; |
8e588e28 | 1043 | |
0f113f3e MC |
1044 | write_ledword(&p, MS_PVKMAGIC); |
1045 | write_ledword(&p, 0); | |
ed576acd | 1046 | if (EVP_PKEY_get_id(pk) == EVP_PKEY_RSA) |
0f113f3e | 1047 | write_ledword(&p, MS_KEYTYPE_KEYX); |
a158f8cf RL |
1048 | #ifndef OPENSSL_NO_DSA |
1049 | else | |
1050 | write_ledword(&p, MS_KEYTYPE_SIGN); | |
1051 | #endif | |
0f113f3e MC |
1052 | write_ledword(&p, enclevel ? 1 : 0); |
1053 | write_ledword(&p, enclevel ? PVK_SALTLEN : 0); | |
1054 | write_ledword(&p, pklen); | |
1055 | if (enclevel) { | |
a158f8cf | 1056 | #ifndef OPENSSL_NO_RC4 |
169eca60 | 1057 | if (RAND_bytes_ex(libctx, p, PVK_SALTLEN, 0) <= 0) |
0f113f3e MC |
1058 | goto error; |
1059 | salt = p; | |
1060 | p += PVK_SALTLEN; | |
a158f8cf | 1061 | #endif |
0f113f3e MC |
1062 | } |
1063 | do_i2b(&p, pk, 0); | |
8e588e28 | 1064 | if (enclevel != 0) { |
a158f8cf | 1065 | #ifndef OPENSSL_NO_RC4 |
0f113f3e MC |
1066 | char psbuf[PEM_BUFSIZE]; |
1067 | unsigned char keybuf[20]; | |
1068 | int enctmplen, inlen; | |
1069 | if (cb) | |
1070 | inlen = cb(psbuf, PEM_BUFSIZE, 1, u); | |
1071 | else | |
1072 | inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u); | |
1073 | if (inlen <= 0) { | |
9311d0c4 | 1074 | ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ); |
0f113f3e MC |
1075 | goto error; |
1076 | } | |
1ffac6ca | 1077 | if (!derive_pvk_key(keybuf, sizeof(keybuf), salt, PVK_SALTLEN, |
169eca60 JS |
1078 | (unsigned char *)psbuf, inlen, libctx, propq)) |
1079 | goto error; | |
1080 | if ((rc4 = EVP_CIPHER_fetch(libctx, "RC4", propq)) == NULL) | |
0f113f3e MC |
1081 | goto error; |
1082 | if (enclevel == 1) | |
1083 | memset(keybuf + 5, 0, 11); | |
1084 | p = salt + PVK_SALTLEN + 8; | |
169eca60 | 1085 | if (!EVP_EncryptInit_ex(cctx, rc4, NULL, keybuf, NULL)) |
0f113f3e MC |
1086 | goto error; |
1087 | OPENSSL_cleanse(keybuf, 20); | |
dca51418 | 1088 | if (!EVP_EncryptUpdate(cctx, p, &enctmplen, p, pklen - 8)) |
0f113f3e | 1089 | goto error; |
dca51418 | 1090 | if (!EVP_EncryptFinal_ex(cctx, p + enctmplen, &enctmplen)) |
0f113f3e | 1091 | goto error; |
a158f8cf RL |
1092 | #else |
1093 | ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER); | |
1094 | goto error; | |
1095 | #endif | |
0f113f3e | 1096 | } |
8e588e28 | 1097 | |
8e588e28 | 1098 | if (*out == NULL) |
febb096c | 1099 | *out = start; |
169eca60 | 1100 | ret = outlen; |
0f113f3e | 1101 | error: |
846ec07d | 1102 | EVP_CIPHER_CTX_free(cctx); |
169eca60 JS |
1103 | #ifndef OPENSSL_NO_RC4 |
1104 | EVP_CIPHER_free(rc4); | |
1105 | #endif | |
098c1e3d | 1106 | if (*out == NULL) |
febb096c | 1107 | OPENSSL_free(start); |
169eca60 JS |
1108 | |
1109 | return ret; | |
0f113f3e | 1110 | } |
a0156a92 | 1111 | |
169eca60 JS |
1112 | int i2b_PVK_bio_ex(BIO *out, const EVP_PKEY *pk, int enclevel, |
1113 | pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx, | |
1114 | const char *propq) | |
0f113f3e MC |
1115 | { |
1116 | unsigned char *tmp = NULL; | |
1117 | int outlen, wrlen; | |
a158f8cf | 1118 | |
169eca60 | 1119 | outlen = i2b_PVK(&tmp, pk, enclevel, cb, u, libctx, propq); |
0f113f3e MC |
1120 | if (outlen < 0) |
1121 | return -1; | |
1122 | wrlen = BIO_write(out, tmp, outlen); | |
1123 | OPENSSL_free(tmp); | |
1124 | if (wrlen == outlen) { | |
0f113f3e MC |
1125 | return outlen; |
1126 | } | |
9311d0c4 | 1127 | ERR_raise(ERR_LIB_PEM, PEM_R_BIO_WRITE_FAILURE); |
0f113f3e MC |
1128 | return -1; |
1129 | } | |
169eca60 JS |
1130 | |
1131 | int i2b_PVK_bio(BIO *out, const EVP_PKEY *pk, int enclevel, | |
1132 | pem_password_cb *cb, void *u) | |
1133 | { | |
1134 | return i2b_PVK_bio_ex(out, pk, enclevel, cb, u, NULL, NULL); | |
1135 | } | |
1136 |