Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/pkcs8/pkcs8.c

raw
1 // Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 1999.
2 // Copyright (c) 1999 The OpenSSL Project. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
4
5 #include <openssl/pkcs8.h>
6
7 #include <assert.h>
8 #include <limits.h>
9 #include <string.h>
10
11 #include <openssl/bytestring.h>
12 #include <openssl/cipher.h>
13 #include <openssl/digest.h>
14 #include <openssl/err.h>
15 #include <openssl/mem.h>
16 #include <openssl/nid.h>
17 #include <openssl/rand.h>
18
19 #include "internal.h"
20 #include "../bytestring/internal.h"
21 #include "../internal.h"
22
23
24 static int pkcs12_encode_password(const char *in, size_t in_len, uint8_t **out,
25 size_t *out_len) {
26 CBB cbb;
27 if (!CBB_init(&cbb, in_len * 2)) {
28 return 0;
29 }
30
31 // Convert the password to BMPString, or UCS-2. See
32 // https://tools.ietf.org/html/rfc7292#appendix-B.1.
33 CBS cbs;
34 CBS_init(&cbs, (const uint8_t *)in, in_len);
35 while (CBS_len(&cbs) != 0) {
36 uint32_t c;
37 if (!cbs_get_utf8(&cbs, &c) ||
38 !cbb_add_ucs2_be(&cbb, c)) {
39 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_INVALID_CHARACTERS);
40 goto err;
41 }
42 }
43
44 // Terminate the result with a UCS-2 NUL.
45 if (!cbb_add_ucs2_be(&cbb, 0) ||
46 !CBB_finish(&cbb, out, out_len)) {
47 goto err;
48 }
49
50 return 1;
51
52 err:
53 CBB_cleanup(&cbb);
54 return 0;
55 }
56
57 int pkcs12_key_gen(const char *pass, size_t pass_len, const uint8_t *salt,
58 size_t salt_len, uint8_t id, uint32_t iterations,
59 size_t out_len, uint8_t *out, const EVP_MD *md) {
60 // See https://tools.ietf.org/html/rfc7292#appendix-B. Quoted parts of the
61 // specification have errata applied and other typos fixed.
62
63 if (iterations < 1) {
64 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_ITERATION_COUNT);
65 return 0;
66 }
67
68 int ret = 0;
69 EVP_MD_CTX ctx;
70 EVP_MD_CTX_init(&ctx);
71 uint8_t *pass_raw = NULL, *I = NULL;
72 size_t pass_raw_len = 0, I_len = 0;
73 // If |pass| is NULL, we use the empty string rather than {0, 0} as the raw
74 // password.
75 if (pass != NULL &&
76 !pkcs12_encode_password(pass, pass_len, &pass_raw, &pass_raw_len)) {
77 goto err;
78 }
79
80 // In the spec, |block_size| is called "v", but measured in bits.
81 size_t block_size = EVP_MD_block_size(md);
82
83 // 1. Construct a string, D (the "diversifier"), by concatenating v/8 copies
84 // of ID.
85 uint8_t D[EVP_MAX_MD_BLOCK_SIZE];
86 OPENSSL_memset(D, id, block_size);
87
88 // 2. Concatenate copies of the salt together to create a string S of length
89 // v(ceiling(s/v)) bits (the final copy of the salt may be truncated to
90 // create S). Note that if the salt is the empty string, then so is S.
91 //
92 // 3. Concatenate copies of the password together to create a string P of
93 // length v(ceiling(p/v)) bits (the final copy of the password may be
94 // truncated to create P). Note that if the password is the empty string,
95 // then so is P.
96 //
97 // 4. Set I=S||P to be the concatenation of S and P.
98 if (salt_len + block_size - 1 < salt_len ||
99 pass_raw_len + block_size - 1 < pass_raw_len) {
100 OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW);
101 goto err;
102 }
103 size_t S_len = block_size * ((salt_len + block_size - 1) / block_size);
104 size_t P_len = block_size * ((pass_raw_len + block_size - 1) / block_size);
105 I_len = S_len + P_len;
106 if (I_len < S_len) {
107 OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW);
108 goto err;
109 }
110
111 I = OPENSSL_malloc(I_len);
112 if (I_len != 0 && I == NULL) {
113 goto err;
114 }
115
116 for (size_t i = 0; i < S_len; i++) {
117 I[i] = salt[i % salt_len];
118 }
119 // P_len would be 0 in this case, but static analyzers don't always see that
120 if(pass_raw_len > 0) {
121 for (size_t i = 0; i < P_len; i++) {
122 I[i + S_len] = pass_raw[i % pass_raw_len];
123 }
124 }
125
126 uint8_t A[EVP_MAX_MD_SIZE];
127 unsigned A_len = 0;
128 while (out_len != 0) {
129 // A. Set A_i=H^r(D||I). (i.e., the r-th hash of D||I,
130 // H(H(H(... H(D||I))))
131 if (!EVP_DigestInit_ex(&ctx, md, NULL) ||
132 !EVP_DigestUpdate(&ctx, D, block_size) ||
133 !EVP_DigestUpdate(&ctx, I, I_len) ||
134 !EVP_DigestFinal_ex(&ctx, A, &A_len)) {
135 goto err;
136 }
137 for (uint32_t iter = 1; iter < iterations; iter++) {
138 if (!EVP_DigestInit_ex(&ctx, md, NULL) ||
139 !EVP_DigestUpdate(&ctx, A, A_len) ||
140 !EVP_DigestFinal_ex(&ctx, A, &A_len)) {
141 goto err;
142 }
143 }
144
145 size_t todo = out_len < A_len ? out_len : A_len;
146 OPENSSL_memcpy(out, A, todo);
147 out += todo;
148 out_len -= todo;
149 if (out_len == 0) {
150 break;
151 }
152
153 // B. Concatenate copies of A_i to create a string B of length v bits (the
154 // final copy of A_i may be truncated to create B).
155 uint8_t B[EVP_MAX_MD_BLOCK_SIZE];
156 for (size_t i = 0; i < block_size; i++) {
157 B[i] = A[i % A_len];
158 }
159
160 // C. Treating I as a concatenation I_0, I_1, ..., I_(k-1) of v-bit blocks,
161 // where k=ceiling(s/v)+ceiling(p/v), modify I by setting I_j=(I_j+B+1) mod
162 // 2^v for each j.
163 assert(I_len % block_size == 0);
164 for (size_t i = 0; i < I_len; i += block_size) {
165 unsigned carry = 1;
166 for (size_t j = block_size - 1; j < block_size; j--) {
167 carry += I[i + j] + B[j];
168 I[i + j] = (uint8_t)carry;
169 carry >>= 8;
170 }
171 }
172 }
173
174 ret = 1;
175
176 err:
177 OPENSSL_cleanse(A, sizeof(A));
178 OPENSSL_free(I);
179 OPENSSL_free(pass_raw);
180 EVP_MD_CTX_cleanup(&ctx);
181 return ret;
182 }
183
184 static int pkcs12_pbe_cipher_init(const struct pbe_suite *suite,
185 EVP_CIPHER_CTX *ctx, uint32_t iterations,
186 const char *pass, size_t pass_len,
187 const uint8_t *salt, size_t salt_len,
188 int is_encrypt) {
189 const EVP_CIPHER *cipher = suite->cipher_func();
190 const EVP_MD *md = suite->md_func();
191
192 uint8_t key[EVP_MAX_KEY_LENGTH];
193 uint8_t iv[EVP_MAX_IV_LENGTH];
194 int ret = 0;
195 if (!pkcs12_key_gen(pass, pass_len, salt, salt_len, PKCS12_KEY_ID, iterations,
196 EVP_CIPHER_key_length(cipher), key, md) ||
197 !pkcs12_key_gen(pass, pass_len, salt, salt_len, PKCS12_IV_ID, iterations,
198 EVP_CIPHER_iv_length(cipher), iv, md)) {
199 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_KEY_GEN_ERROR);
200 goto err;
201 }
202 ret = EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, is_encrypt);
203
204 err:
205 OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
206 OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
207 return ret;
208 }
209
210 static int pkcs12_pbe_decrypt_init(const struct pbe_suite *suite,
211 EVP_CIPHER_CTX *ctx, const char *pass,
212 size_t pass_len, CBS *param) {
213 CBS pbe_param, salt;
214 uint64_t iterations;
215 if (!CBS_get_asn1(param, &pbe_param, CBS_ASN1_SEQUENCE) ||
216 !CBS_get_asn1(&pbe_param, &salt, CBS_ASN1_OCTETSTRING) ||
217 !CBS_get_asn1_uint64(&pbe_param, &iterations) ||
218 CBS_len(&pbe_param) != 0 ||
219 CBS_len(param) != 0) {
220 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
221 return 0;
222 }
223
224 if (!pkcs12_iterations_acceptable(iterations)) {
225 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_ITERATION_COUNT);
226 return 0;
227 }
228
229 return pkcs12_pbe_cipher_init(suite, ctx, (uint32_t)iterations, pass,
230 pass_len, CBS_data(&salt), CBS_len(&salt),
231 0 /* decrypt */);
232 }
233
234 static const struct pbe_suite kBuiltinPBE[] = {
235 {
236 NID_pbe_WithSHA1And40BitRC2_CBC,
237 // 1.2.840.113549.1.12.1.6
238 {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x06},
239 10,
240 EVP_rc2_40_cbc,
241 EVP_sha1,
242 pkcs12_pbe_decrypt_init,
243 },
244 {
245 NID_pbe_WithSHA1And128BitRC4,
246 // 1.2.840.113549.1.12.1.1
247 {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x01},
248 10,
249 EVP_rc4,
250 EVP_sha1,
251 pkcs12_pbe_decrypt_init,
252 },
253 {
254 NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
255 // 1.2.840.113549.1.12.1.3
256 {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x03},
257 10,
258 EVP_des_ede3_cbc,
259 EVP_sha1,
260 pkcs12_pbe_decrypt_init,
261 },
262 {
263 NID_pbes2,
264 // 1.2.840.113549.1.5.13
265 {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x05, 0x0d},
266 9,
267 NULL,
268 NULL,
269 PKCS5_pbe2_decrypt_init,
270 },
271 };
272
273 static const struct pbe_suite *get_pkcs12_pbe_suite(int pbe_nid) {
274 for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(kBuiltinPBE); i++) {
275 if (kBuiltinPBE[i].pbe_nid == pbe_nid &&
276 // If |cipher_func| or |md_func| are missing, this is a PBES2 scheme.
277 kBuiltinPBE[i].cipher_func != NULL &&
278 kBuiltinPBE[i].md_func != NULL) {
279 return &kBuiltinPBE[i];
280 }
281 }
282
283 return NULL;
284 }
285
286 int pkcs12_pbe_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx, int alg,
287 uint32_t iterations, const char *pass,
288 size_t pass_len, const uint8_t *salt,
289 size_t salt_len) {
290 const struct pbe_suite *suite = get_pkcs12_pbe_suite(alg);
291 if (suite == NULL) {
292 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_ALGORITHM);
293 return 0;
294 }
295
296 // See RFC 2898, appendix A.3.
297 CBB algorithm, oid, param, salt_cbb;
298 if (!CBB_add_asn1(out, &algorithm, CBS_ASN1_SEQUENCE) ||
299 !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
300 !CBB_add_bytes(&oid, suite->oid, suite->oid_len) ||
301 !CBB_add_asn1(&algorithm, &param, CBS_ASN1_SEQUENCE) ||
302 !CBB_add_asn1(&param, &salt_cbb, CBS_ASN1_OCTETSTRING) ||
303 !CBB_add_bytes(&salt_cbb, salt, salt_len) ||
304 !CBB_add_asn1_uint64(&param, iterations) ||
305 !CBB_flush(out)) {
306 return 0;
307 }
308
309 return pkcs12_pbe_cipher_init(suite, ctx, iterations, pass, pass_len, salt,
310 salt_len, 1 /* encrypt */);
311 }
312
313 int pkcs8_pbe_decrypt(uint8_t **out, size_t *out_len, CBS *algorithm,
314 const char *pass, size_t pass_len, const uint8_t *in,
315 size_t in_len) {
316 int ret = 0;
317 uint8_t *buf = NULL;
318 EVP_CIPHER_CTX ctx;
319 EVP_CIPHER_CTX_init(&ctx);
320
321 CBS obj;
322 if (!CBS_get_asn1(algorithm, &obj, CBS_ASN1_OBJECT)) {
323 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
324 goto err;
325 }
326
327 const struct pbe_suite *suite = NULL;
328 for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(kBuiltinPBE); i++) {
329 if (CBS_mem_equal(&obj, kBuiltinPBE[i].oid, kBuiltinPBE[i].oid_len)) {
330 suite = &kBuiltinPBE[i];
331 break;
332 }
333 }
334 if (suite == NULL) {
335 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_ALGORITHM);
336 goto err;
337 }
338
339 if (!suite->decrypt_init(suite, &ctx, pass, pass_len, algorithm)) {
340 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_KEYGEN_FAILURE);
341 goto err;
342 }
343
344 if (in_len > INT_MAX) {
345 OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW);
346 goto err;
347 }
348
349 buf = OPENSSL_malloc(in_len);
350 if (buf == NULL) {
351 goto err;
352 }
353
354 int n1, n2;
355 if (!EVP_DecryptUpdate(&ctx, buf, &n1, in, (int)in_len) ||
356 !EVP_DecryptFinal_ex(&ctx, buf + n1, &n2)) {
357 goto err;
358 }
359
360 *out = buf;
361 *out_len = n1 + n2;
362 ret = 1;
363 buf = NULL;
364
365 err:
366 OPENSSL_free(buf);
367 EVP_CIPHER_CTX_cleanup(&ctx);
368 return ret;
369 }
370
371 EVP_PKEY *PKCS8_parse_encrypted_private_key(CBS *cbs, const char *pass,
372 size_t pass_len) {
373 // See RFC 5208, section 6.
374 CBS epki, algorithm, ciphertext;
375 if (!CBS_get_asn1(cbs, &epki, CBS_ASN1_SEQUENCE) ||
376 !CBS_get_asn1(&epki, &algorithm, CBS_ASN1_SEQUENCE) ||
377 !CBS_get_asn1(&epki, &ciphertext, CBS_ASN1_OCTETSTRING) ||
378 CBS_len(&epki) != 0) {
379 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
380 return 0;
381 }
382
383 uint8_t *out;
384 size_t out_len;
385 if (!pkcs8_pbe_decrypt(&out, &out_len, &algorithm, pass, pass_len,
386 CBS_data(&ciphertext), CBS_len(&ciphertext))) {
387 return 0;
388 }
389
390 CBS pki;
391 CBS_init(&pki, out, out_len);
392 EVP_PKEY *ret = EVP_parse_private_key(&pki);
393 OPENSSL_free(out);
394 return ret;
395 }
396
397 int PKCS8_marshal_encrypted_private_key(CBB *out, int pbe_nid,
398 const EVP_CIPHER *cipher,
399 const char *pass, size_t pass_len,
400 const uint8_t *salt, size_t salt_len,
401 int iterations, const EVP_PKEY *pkey) {
402 int ret = 0;
403 uint8_t *plaintext = NULL, *salt_buf = NULL;
404 size_t plaintext_len = 0;
405 EVP_CIPHER_CTX ctx;
406 EVP_CIPHER_CTX_init(&ctx);
407
408 // Generate a random salt if necessary.
409 if (salt == NULL) {
410 if (salt_len == 0) {
411 salt_len = PKCS12_SALT_LEN;
412 }
413
414 salt_buf = OPENSSL_malloc(salt_len);
415 if (salt_buf == NULL) {
416 goto err;
417 }
418 AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(salt_buf, salt_len));
419
420 salt = salt_buf;
421 }
422
423 if (iterations <= 0) {
424 iterations = PKCS12_DEFAULT_ITER;
425 }
426
427 // Serialize the input key.
428 CBB plaintext_cbb;
429 if (!CBB_init(&plaintext_cbb, 128) ||
430 !EVP_marshal_private_key(&plaintext_cbb, pkey) ||
431 !CBB_finish(&plaintext_cbb, &plaintext, &plaintext_len)) {
432 CBB_cleanup(&plaintext_cbb);
433 goto err;
434 }
435
436 CBB epki;
437 if (!CBB_add_asn1(out, &epki, CBS_ASN1_SEQUENCE)) {
438 goto err;
439 }
440
441 // TODO(davidben): OpenSSL has since extended |pbe_nid| to control either the
442 // PBES1 scheme or the PBES2 PRF. E.g. passing |NID_hmacWithSHA256| will
443 // select PBES2 with HMAC-SHA256 as the PRF. Implement this if anything uses
444 // it. See 5693a30813a031d3921a016a870420e7eb93ec90 in OpenSSL.
445 int alg_ok;
446 if (pbe_nid == -1) {
447 alg_ok = PKCS5_pbe2_encrypt_init(&epki, &ctx, cipher, (uint32_t)iterations,
448 pass, pass_len, salt, salt_len);
449 } else {
450 alg_ok = pkcs12_pbe_encrypt_init(&epki, &ctx, pbe_nid, (uint32_t)iterations,
451 pass, pass_len, salt, salt_len);
452 }
453 if (!alg_ok) {
454 goto err;
455 }
456
457 // |EVP_CipherUpdate| takes |int| for the input length and may output up to
458 // |plaintext_len + block_size| bytes. Ensure the total fits in |int| to avoid
459 // truncation and cannot overflow.
460 size_t block_size = EVP_CIPHER_CTX_block_size(&ctx);
461 if (plaintext_len > INT_MAX - block_size) {
462 OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW);
463 goto err;
464 }
465
466 size_t max_out = plaintext_len + block_size;
467
468 CBB ciphertext;
469 uint8_t *ptr;
470 int n1, n2;
471 if (!CBB_add_asn1(&epki, &ciphertext, CBS_ASN1_OCTETSTRING) ||
472 !CBB_reserve(&ciphertext, &ptr, max_out) ||
473 !EVP_CipherUpdate(&ctx, ptr, &n1, plaintext, (int)plaintext_len) ||
474 !EVP_CipherFinal_ex(&ctx, ptr + n1, &n2) ||
475 !CBB_did_write(&ciphertext, n1 + n2) ||
476 !CBB_flush(out)) {
477 goto err;
478 }
479
480 ret = 1;
481
482 err:
483 OPENSSL_free(plaintext);
484 OPENSSL_free(salt_buf);
485 EVP_CIPHER_CTX_cleanup(&ctx);
486 return ret;
487 }