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