/src/boringssl/crypto/pkcs8/pkcs8.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
2 | | * project 1999. |
3 | | */ |
4 | | /* ==================================================================== |
5 | | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. |
6 | | * |
7 | | * Redistribution and use in source and binary forms, with or without |
8 | | * modification, are permitted provided that the following conditions |
9 | | * are met: |
10 | | * |
11 | | * 1. Redistributions of source code must retain the above copyright |
12 | | * notice, this list of conditions and the following disclaimer. |
13 | | * |
14 | | * 2. Redistributions in binary form must reproduce the above copyright |
15 | | * notice, this list of conditions and the following disclaimer in |
16 | | * the documentation and/or other materials provided with the |
17 | | * distribution. |
18 | | * |
19 | | * 3. All advertising materials mentioning features or use of this |
20 | | * software must display the following acknowledgment: |
21 | | * "This product includes software developed by the OpenSSL Project |
22 | | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
23 | | * |
24 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
25 | | * endorse or promote products derived from this software without |
26 | | * prior written permission. For written permission, please contact |
27 | | * licensing@OpenSSL.org. |
28 | | * |
29 | | * 5. Products derived from this software may not be called "OpenSSL" |
30 | | * nor may "OpenSSL" appear in their names without prior written |
31 | | * permission of the OpenSSL Project. |
32 | | * |
33 | | * 6. Redistributions of any form whatsoever must retain the following |
34 | | * acknowledgment: |
35 | | * "This product includes software developed by the OpenSSL Project |
36 | | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
37 | | * |
38 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
39 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
40 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
41 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
42 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
43 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
44 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
45 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
46 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
47 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
48 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
49 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
50 | | * ==================================================================== |
51 | | * |
52 | | * This product includes cryptographic software written by Eric Young |
53 | | * (eay@cryptsoft.com). This product includes software written by Tim |
54 | | * Hudson (tjh@cryptsoft.com). */ |
55 | | |
56 | | #include <openssl/pkcs8.h> |
57 | | |
58 | | #include <assert.h> |
59 | | #include <limits.h> |
60 | | #include <string.h> |
61 | | |
62 | | #include <openssl/bytestring.h> |
63 | | #include <openssl/cipher.h> |
64 | | #include <openssl/digest.h> |
65 | | #include <openssl/err.h> |
66 | | #include <openssl/mem.h> |
67 | | #include <openssl/nid.h> |
68 | | #include <openssl/rand.h> |
69 | | |
70 | | #include "internal.h" |
71 | | #include "../bytestring/internal.h" |
72 | | #include "../internal.h" |
73 | | |
74 | | |
75 | | static int pkcs12_encode_password(const char *in, size_t in_len, uint8_t **out, |
76 | 0 | size_t *out_len) { |
77 | 0 | CBB cbb; |
78 | 0 | if (!CBB_init(&cbb, in_len * 2)) { |
79 | 0 | return 0; |
80 | 0 | } |
81 | | |
82 | | // Convert the password to BMPString, or UCS-2. See |
83 | | // https://tools.ietf.org/html/rfc7292#appendix-B.1. |
84 | 0 | CBS cbs; |
85 | 0 | CBS_init(&cbs, (const uint8_t *)in, in_len); |
86 | 0 | while (CBS_len(&cbs) != 0) { |
87 | 0 | uint32_t c; |
88 | 0 | if (!cbs_get_utf8(&cbs, &c) || |
89 | 0 | !cbb_add_ucs2_be(&cbb, c)) { |
90 | 0 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_INVALID_CHARACTERS); |
91 | 0 | goto err; |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | | // Terminate the result with a UCS-2 NUL. |
96 | 0 | if (!cbb_add_ucs2_be(&cbb, 0) || |
97 | 0 | !CBB_finish(&cbb, out, out_len)) { |
98 | 0 | goto err; |
99 | 0 | } |
100 | | |
101 | 0 | return 1; |
102 | | |
103 | 0 | err: |
104 | 0 | CBB_cleanup(&cbb); |
105 | 0 | return 0; |
106 | 0 | } |
107 | | |
108 | | int pkcs12_key_gen(const char *pass, size_t pass_len, const uint8_t *salt, |
109 | | size_t salt_len, uint8_t id, unsigned iterations, |
110 | 0 | size_t out_len, uint8_t *out, const EVP_MD *md) { |
111 | | // See https://tools.ietf.org/html/rfc7292#appendix-B. Quoted parts of the |
112 | | // specification have errata applied and other typos fixed. |
113 | |
|
114 | 0 | if (iterations < 1) { |
115 | 0 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_ITERATION_COUNT); |
116 | 0 | return 0; |
117 | 0 | } |
118 | | |
119 | 0 | int ret = 0; |
120 | 0 | EVP_MD_CTX ctx; |
121 | 0 | EVP_MD_CTX_init(&ctx); |
122 | 0 | uint8_t *pass_raw = NULL, *I = NULL; |
123 | 0 | size_t pass_raw_len = 0, I_len = 0; |
124 | | // If |pass| is NULL, we use the empty string rather than {0, 0} as the raw |
125 | | // password. |
126 | 0 | if (pass != NULL && |
127 | 0 | !pkcs12_encode_password(pass, pass_len, &pass_raw, &pass_raw_len)) { |
128 | 0 | goto err; |
129 | 0 | } |
130 | | |
131 | | // In the spec, |block_size| is called "v", but measured in bits. |
132 | 0 | size_t block_size = EVP_MD_block_size(md); |
133 | | |
134 | | // 1. Construct a string, D (the "diversifier"), by concatenating v/8 copies |
135 | | // of ID. |
136 | 0 | uint8_t D[EVP_MAX_MD_BLOCK_SIZE]; |
137 | 0 | OPENSSL_memset(D, id, block_size); |
138 | | |
139 | | // 2. Concatenate copies of the salt together to create a string S of length |
140 | | // v(ceiling(s/v)) bits (the final copy of the salt may be truncated to |
141 | | // create S). Note that if the salt is the empty string, then so is S. |
142 | | // |
143 | | // 3. Concatenate copies of the password together to create a string P of |
144 | | // length v(ceiling(p/v)) bits (the final copy of the password may be |
145 | | // truncated to create P). Note that if the password is the empty string, |
146 | | // then so is P. |
147 | | // |
148 | | // 4. Set I=S||P to be the concatenation of S and P. |
149 | 0 | if (salt_len + block_size - 1 < salt_len || |
150 | 0 | pass_raw_len + block_size - 1 < pass_raw_len) { |
151 | 0 | OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW); |
152 | 0 | goto err; |
153 | 0 | } |
154 | 0 | size_t S_len = block_size * ((salt_len + block_size - 1) / block_size); |
155 | 0 | size_t P_len = block_size * ((pass_raw_len + block_size - 1) / block_size); |
156 | 0 | I_len = S_len + P_len; |
157 | 0 | if (I_len < S_len) { |
158 | 0 | OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW); |
159 | 0 | goto err; |
160 | 0 | } |
161 | | |
162 | 0 | I = OPENSSL_malloc(I_len); |
163 | 0 | if (I_len != 0 && I == NULL) { |
164 | 0 | goto err; |
165 | 0 | } |
166 | | |
167 | 0 | for (size_t i = 0; i < S_len; i++) { |
168 | 0 | I[i] = salt[i % salt_len]; |
169 | 0 | } |
170 | 0 | for (size_t i = 0; i < P_len; i++) { |
171 | 0 | I[i + S_len] = pass_raw[i % pass_raw_len]; |
172 | 0 | } |
173 | |
|
174 | 0 | while (out_len != 0) { |
175 | | // A. Set A_i=H^r(D||I). (i.e., the r-th hash of D||I, |
176 | | // H(H(H(... H(D||I)))) |
177 | 0 | uint8_t A[EVP_MAX_MD_SIZE]; |
178 | 0 | unsigned A_len; |
179 | 0 | if (!EVP_DigestInit_ex(&ctx, md, NULL) || |
180 | 0 | !EVP_DigestUpdate(&ctx, D, block_size) || |
181 | 0 | !EVP_DigestUpdate(&ctx, I, I_len) || |
182 | 0 | !EVP_DigestFinal_ex(&ctx, A, &A_len)) { |
183 | 0 | goto err; |
184 | 0 | } |
185 | 0 | for (unsigned iter = 1; iter < iterations; iter++) { |
186 | 0 | if (!EVP_DigestInit_ex(&ctx, md, NULL) || |
187 | 0 | !EVP_DigestUpdate(&ctx, A, A_len) || |
188 | 0 | !EVP_DigestFinal_ex(&ctx, A, &A_len)) { |
189 | 0 | goto err; |
190 | 0 | } |
191 | 0 | } |
192 | | |
193 | 0 | size_t todo = out_len < A_len ? out_len : A_len; |
194 | 0 | OPENSSL_memcpy(out, A, todo); |
195 | 0 | out += todo; |
196 | 0 | out_len -= todo; |
197 | 0 | if (out_len == 0) { |
198 | 0 | break; |
199 | 0 | } |
200 | | |
201 | | // B. Concatenate copies of A_i to create a string B of length v bits (the |
202 | | // final copy of A_i may be truncated to create B). |
203 | 0 | uint8_t B[EVP_MAX_MD_BLOCK_SIZE]; |
204 | 0 | for (size_t i = 0; i < block_size; i++) { |
205 | 0 | B[i] = A[i % A_len]; |
206 | 0 | } |
207 | | |
208 | | // C. Treating I as a concatenation I_0, I_1, ..., I_(k-1) of v-bit blocks, |
209 | | // where k=ceiling(s/v)+ceiling(p/v), modify I by setting I_j=(I_j+B+1) mod |
210 | | // 2^v for each j. |
211 | 0 | assert(I_len % block_size == 0); |
212 | 0 | for (size_t i = 0; i < I_len; i += block_size) { |
213 | 0 | unsigned carry = 1; |
214 | 0 | for (size_t j = block_size - 1; j < block_size; j--) { |
215 | 0 | carry += I[i + j] + B[j]; |
216 | 0 | I[i + j] = (uint8_t)carry; |
217 | 0 | carry >>= 8; |
218 | 0 | } |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | 0 | ret = 1; |
223 | |
|
224 | 0 | err: |
225 | 0 | OPENSSL_free(I); |
226 | 0 | OPENSSL_free(pass_raw); |
227 | 0 | EVP_MD_CTX_cleanup(&ctx); |
228 | 0 | return ret; |
229 | 0 | } |
230 | | |
231 | | static int pkcs12_pbe_cipher_init(const struct pbe_suite *suite, |
232 | | EVP_CIPHER_CTX *ctx, unsigned iterations, |
233 | | const char *pass, size_t pass_len, |
234 | | const uint8_t *salt, size_t salt_len, |
235 | 0 | int is_encrypt) { |
236 | 0 | const EVP_CIPHER *cipher = suite->cipher_func(); |
237 | 0 | const EVP_MD *md = suite->md_func(); |
238 | |
|
239 | 0 | uint8_t key[EVP_MAX_KEY_LENGTH]; |
240 | 0 | uint8_t iv[EVP_MAX_IV_LENGTH]; |
241 | 0 | if (!pkcs12_key_gen(pass, pass_len, salt, salt_len, PKCS12_KEY_ID, iterations, |
242 | 0 | EVP_CIPHER_key_length(cipher), key, md) || |
243 | 0 | !pkcs12_key_gen(pass, pass_len, salt, salt_len, PKCS12_IV_ID, iterations, |
244 | 0 | EVP_CIPHER_iv_length(cipher), iv, md)) { |
245 | 0 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_KEY_GEN_ERROR); |
246 | 0 | return 0; |
247 | 0 | } |
248 | | |
249 | 0 | int ret = EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, is_encrypt); |
250 | 0 | OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH); |
251 | 0 | OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH); |
252 | 0 | return ret; |
253 | 0 | } |
254 | | |
255 | | static int pkcs12_pbe_decrypt_init(const struct pbe_suite *suite, |
256 | | EVP_CIPHER_CTX *ctx, const char *pass, |
257 | 0 | size_t pass_len, CBS *param) { |
258 | 0 | CBS pbe_param, salt; |
259 | 0 | uint64_t iterations; |
260 | 0 | if (!CBS_get_asn1(param, &pbe_param, CBS_ASN1_SEQUENCE) || |
261 | 0 | !CBS_get_asn1(&pbe_param, &salt, CBS_ASN1_OCTETSTRING) || |
262 | 0 | !CBS_get_asn1_uint64(&pbe_param, &iterations) || |
263 | 0 | CBS_len(&pbe_param) != 0 || |
264 | 0 | CBS_len(param) != 0) { |
265 | 0 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
266 | 0 | return 0; |
267 | 0 | } |
268 | | |
269 | 0 | if (!pkcs12_iterations_acceptable(iterations)) { |
270 | 0 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_ITERATION_COUNT); |
271 | 0 | return 0; |
272 | 0 | } |
273 | | |
274 | 0 | return pkcs12_pbe_cipher_init(suite, ctx, (unsigned)iterations, pass, |
275 | 0 | pass_len, CBS_data(&salt), CBS_len(&salt), |
276 | 0 | 0 /* decrypt */); |
277 | 0 | } |
278 | | |
279 | | static const struct pbe_suite kBuiltinPBE[] = { |
280 | | { |
281 | | NID_pbe_WithSHA1And40BitRC2_CBC, |
282 | | // 1.2.840.113549.1.12.1.6 |
283 | | {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x06}, |
284 | | 10, |
285 | | EVP_rc2_40_cbc, |
286 | | EVP_sha1, |
287 | | pkcs12_pbe_decrypt_init, |
288 | | }, |
289 | | { |
290 | | NID_pbe_WithSHA1And128BitRC4, |
291 | | // 1.2.840.113549.1.12.1.1 |
292 | | {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x01}, |
293 | | 10, |
294 | | EVP_rc4, |
295 | | EVP_sha1, |
296 | | pkcs12_pbe_decrypt_init, |
297 | | }, |
298 | | { |
299 | | NID_pbe_WithSHA1And3_Key_TripleDES_CBC, |
300 | | // 1.2.840.113549.1.12.1.3 |
301 | | {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x03}, |
302 | | 10, |
303 | | EVP_des_ede3_cbc, |
304 | | EVP_sha1, |
305 | | pkcs12_pbe_decrypt_init, |
306 | | }, |
307 | | { |
308 | | NID_pbes2, |
309 | | // 1.2.840.113549.1.5.13 |
310 | | {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x05, 0x0d}, |
311 | | 9, |
312 | | NULL, |
313 | | NULL, |
314 | | PKCS5_pbe2_decrypt_init, |
315 | | }, |
316 | | }; |
317 | | |
318 | 0 | static const struct pbe_suite *get_pkcs12_pbe_suite(int pbe_nid) { |
319 | 0 | for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(kBuiltinPBE); i++) { |
320 | 0 | if (kBuiltinPBE[i].pbe_nid == pbe_nid && |
321 | | // If |cipher_func| or |md_func| are missing, this is a PBES2 scheme. |
322 | 0 | kBuiltinPBE[i].cipher_func != NULL && |
323 | 0 | kBuiltinPBE[i].md_func != NULL) { |
324 | 0 | return &kBuiltinPBE[i]; |
325 | 0 | } |
326 | 0 | } |
327 | | |
328 | 0 | return NULL; |
329 | 0 | } |
330 | | |
331 | | int pkcs12_pbe_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx, int alg, |
332 | | unsigned iterations, const char *pass, |
333 | | size_t pass_len, const uint8_t *salt, |
334 | 0 | size_t salt_len) { |
335 | 0 | const struct pbe_suite *suite = get_pkcs12_pbe_suite(alg); |
336 | 0 | if (suite == NULL) { |
337 | 0 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_ALGORITHM); |
338 | 0 | return 0; |
339 | 0 | } |
340 | | |
341 | | // See RFC 2898, appendix A.3. |
342 | 0 | CBB algorithm, oid, param, salt_cbb; |
343 | 0 | if (!CBB_add_asn1(out, &algorithm, CBS_ASN1_SEQUENCE) || |
344 | 0 | !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) || |
345 | 0 | !CBB_add_bytes(&oid, suite->oid, suite->oid_len) || |
346 | 0 | !CBB_add_asn1(&algorithm, ¶m, CBS_ASN1_SEQUENCE) || |
347 | 0 | !CBB_add_asn1(¶m, &salt_cbb, CBS_ASN1_OCTETSTRING) || |
348 | 0 | !CBB_add_bytes(&salt_cbb, salt, salt_len) || |
349 | 0 | !CBB_add_asn1_uint64(¶m, iterations) || |
350 | 0 | !CBB_flush(out)) { |
351 | 0 | return 0; |
352 | 0 | } |
353 | | |
354 | 0 | return pkcs12_pbe_cipher_init(suite, ctx, iterations, pass, pass_len, salt, |
355 | 0 | salt_len, 1 /* encrypt */); |
356 | 0 | } |
357 | | |
358 | | int pkcs8_pbe_decrypt(uint8_t **out, size_t *out_len, CBS *algorithm, |
359 | | const char *pass, size_t pass_len, const uint8_t *in, |
360 | 0 | size_t in_len) { |
361 | 0 | int ret = 0; |
362 | 0 | uint8_t *buf = NULL;; |
363 | 0 | EVP_CIPHER_CTX ctx; |
364 | 0 | EVP_CIPHER_CTX_init(&ctx); |
365 | |
|
366 | 0 | CBS obj; |
367 | 0 | if (!CBS_get_asn1(algorithm, &obj, CBS_ASN1_OBJECT)) { |
368 | 0 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
369 | 0 | goto err; |
370 | 0 | } |
371 | | |
372 | 0 | const struct pbe_suite *suite = NULL; |
373 | 0 | for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(kBuiltinPBE); i++) { |
374 | 0 | if (CBS_mem_equal(&obj, kBuiltinPBE[i].oid, kBuiltinPBE[i].oid_len)) { |
375 | 0 | suite = &kBuiltinPBE[i]; |
376 | 0 | break; |
377 | 0 | } |
378 | 0 | } |
379 | 0 | if (suite == NULL) { |
380 | 0 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_ALGORITHM); |
381 | 0 | goto err; |
382 | 0 | } |
383 | | |
384 | 0 | if (!suite->decrypt_init(suite, &ctx, pass, pass_len, algorithm)) { |
385 | 0 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_KEYGEN_FAILURE); |
386 | 0 | goto err; |
387 | 0 | } |
388 | | |
389 | 0 | buf = OPENSSL_malloc(in_len); |
390 | 0 | if (buf == NULL) { |
391 | 0 | goto err; |
392 | 0 | } |
393 | | |
394 | 0 | if (in_len > INT_MAX) { |
395 | 0 | OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW); |
396 | 0 | goto err; |
397 | 0 | } |
398 | | |
399 | 0 | int n1, n2; |
400 | 0 | if (!EVP_DecryptUpdate(&ctx, buf, &n1, in, (int)in_len) || |
401 | 0 | !EVP_DecryptFinal_ex(&ctx, buf + n1, &n2)) { |
402 | 0 | goto err; |
403 | 0 | } |
404 | | |
405 | 0 | *out = buf; |
406 | 0 | *out_len = n1 + n2; |
407 | 0 | ret = 1; |
408 | 0 | buf = NULL; |
409 | |
|
410 | 0 | err: |
411 | 0 | OPENSSL_free(buf); |
412 | 0 | EVP_CIPHER_CTX_cleanup(&ctx); |
413 | 0 | return ret; |
414 | 0 | } |
415 | | |
416 | | EVP_PKEY *PKCS8_parse_encrypted_private_key(CBS *cbs, const char *pass, |
417 | 0 | size_t pass_len) { |
418 | | // See RFC 5208, section 6. |
419 | 0 | CBS epki, algorithm, ciphertext; |
420 | 0 | if (!CBS_get_asn1(cbs, &epki, CBS_ASN1_SEQUENCE) || |
421 | 0 | !CBS_get_asn1(&epki, &algorithm, CBS_ASN1_SEQUENCE) || |
422 | 0 | !CBS_get_asn1(&epki, &ciphertext, CBS_ASN1_OCTETSTRING) || |
423 | 0 | CBS_len(&epki) != 0) { |
424 | 0 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
425 | 0 | return 0; |
426 | 0 | } |
427 | | |
428 | 0 | uint8_t *out; |
429 | 0 | size_t out_len; |
430 | 0 | if (!pkcs8_pbe_decrypt(&out, &out_len, &algorithm, pass, pass_len, |
431 | 0 | CBS_data(&ciphertext), CBS_len(&ciphertext))) { |
432 | 0 | return 0; |
433 | 0 | } |
434 | | |
435 | 0 | CBS pki; |
436 | 0 | CBS_init(&pki, out, out_len); |
437 | 0 | EVP_PKEY *ret = EVP_parse_private_key(&pki); |
438 | 0 | OPENSSL_free(out); |
439 | 0 | return ret; |
440 | 0 | } |
441 | | |
442 | | int PKCS8_marshal_encrypted_private_key(CBB *out, int pbe_nid, |
443 | | const EVP_CIPHER *cipher, |
444 | | const char *pass, size_t pass_len, |
445 | | const uint8_t *salt, size_t salt_len, |
446 | 0 | int iterations, const EVP_PKEY *pkey) { |
447 | 0 | int ret = 0; |
448 | 0 | uint8_t *plaintext = NULL, *salt_buf = NULL; |
449 | 0 | size_t plaintext_len = 0; |
450 | 0 | EVP_CIPHER_CTX ctx; |
451 | 0 | EVP_CIPHER_CTX_init(&ctx); |
452 | | |
453 | | // Generate a random salt if necessary. |
454 | 0 | if (salt == NULL) { |
455 | 0 | if (salt_len == 0) { |
456 | 0 | salt_len = PKCS5_SALT_LEN; |
457 | 0 | } |
458 | |
|
459 | 0 | salt_buf = OPENSSL_malloc(salt_len); |
460 | 0 | if (salt_buf == NULL || |
461 | 0 | !RAND_bytes(salt_buf, salt_len)) { |
462 | 0 | goto err; |
463 | 0 | } |
464 | | |
465 | 0 | salt = salt_buf; |
466 | 0 | } |
467 | | |
468 | 0 | if (iterations <= 0) { |
469 | 0 | iterations = PKCS12_DEFAULT_ITER; |
470 | 0 | } |
471 | | |
472 | | // Serialize the input key. |
473 | 0 | CBB plaintext_cbb; |
474 | 0 | if (!CBB_init(&plaintext_cbb, 128) || |
475 | 0 | !EVP_marshal_private_key(&plaintext_cbb, pkey) || |
476 | 0 | !CBB_finish(&plaintext_cbb, &plaintext, &plaintext_len)) { |
477 | 0 | CBB_cleanup(&plaintext_cbb); |
478 | 0 | goto err; |
479 | 0 | } |
480 | | |
481 | 0 | CBB epki; |
482 | 0 | if (!CBB_add_asn1(out, &epki, CBS_ASN1_SEQUENCE)) { |
483 | 0 | goto err; |
484 | 0 | } |
485 | | |
486 | | // TODO(davidben): OpenSSL has since extended |pbe_nid| to control either the |
487 | | // PBES1 scheme or the PBES2 PRF. E.g. passing |NID_hmacWithSHA256| will |
488 | | // select PBES2 with HMAC-SHA256 as the PRF. Implement this if anything uses |
489 | | // it. See 5693a30813a031d3921a016a870420e7eb93ec90 in OpenSSL. |
490 | 0 | int alg_ok; |
491 | 0 | if (pbe_nid == -1) { |
492 | 0 | alg_ok = PKCS5_pbe2_encrypt_init(&epki, &ctx, cipher, (unsigned)iterations, |
493 | 0 | pass, pass_len, salt, salt_len); |
494 | 0 | } else { |
495 | 0 | alg_ok = pkcs12_pbe_encrypt_init(&epki, &ctx, pbe_nid, (unsigned)iterations, |
496 | 0 | pass, pass_len, salt, salt_len); |
497 | 0 | } |
498 | 0 | if (!alg_ok) { |
499 | 0 | goto err; |
500 | 0 | } |
501 | | |
502 | 0 | size_t max_out = plaintext_len + EVP_CIPHER_CTX_block_size(&ctx); |
503 | 0 | if (max_out < plaintext_len) { |
504 | 0 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_TOO_LONG); |
505 | 0 | goto err; |
506 | 0 | } |
507 | | |
508 | 0 | CBB ciphertext; |
509 | 0 | uint8_t *ptr; |
510 | 0 | int n1, n2; |
511 | 0 | if (!CBB_add_asn1(&epki, &ciphertext, CBS_ASN1_OCTETSTRING) || |
512 | 0 | !CBB_reserve(&ciphertext, &ptr, max_out) || |
513 | 0 | !EVP_CipherUpdate(&ctx, ptr, &n1, plaintext, plaintext_len) || |
514 | 0 | !EVP_CipherFinal_ex(&ctx, ptr + n1, &n2) || |
515 | 0 | !CBB_did_write(&ciphertext, n1 + n2) || |
516 | 0 | !CBB_flush(out)) { |
517 | 0 | goto err; |
518 | 0 | } |
519 | | |
520 | 0 | ret = 1; |
521 | |
|
522 | 0 | err: |
523 | 0 | OPENSSL_free(plaintext); |
524 | 0 | OPENSSL_free(salt_buf); |
525 | 0 | EVP_CIPHER_CTX_cleanup(&ctx); |
526 | 0 | return ret; |
527 | 0 | } |