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