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