Debian Codegraph @

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

raw
1 // Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 1999-2004.
2 // Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
4
5 #include <openssl/pkcs8.h>
6
7 #include <limits.h>
8 #include <string.h>
9
10 #include <openssl/bytestring.h>
11 #include <openssl/cipher.h>
12 #include <openssl/err.h>
13 #include <openssl/mem.h>
14 #include <openssl/nid.h>
15 #include <openssl/rand.h>
16
17 #include "internal.h"
18 #include "../internal.h"
19
20
21 // 1.2.840.113549.1.5.12
22 static const uint8_t kPBKDF2[] = {0x2a, 0x86, 0x48, 0x86, 0xf7,
23 0x0d, 0x01, 0x05, 0x0c};
24
25 // 1.2.840.113549.1.5.13
26 static const uint8_t kPBES2[] = {0x2a, 0x86, 0x48, 0x86, 0xf7,
27 0x0d, 0x01, 0x05, 0x0d};
28
29 // 1.2.840.113549.2.7
30 static const uint8_t kHMACWithSHA1[] = {0x2a, 0x86, 0x48, 0x86,
31 0xf7, 0x0d, 0x02, 0x07};
32
33 // 1.2.840.113549.2.9
34 static const uint8_t kHMACWithSHA256[] = {0x2a, 0x86, 0x48, 0x86,
35 0xf7, 0x0d, 0x02, 0x09};
36
37 static const struct {
38 uint8_t oid[9];
39 uint8_t oid_len;
40 int nid;
41 const EVP_CIPHER *(*cipher_func)(void);
42 } kCipherOIDs[] = {
43 // 1.2.840.113549.3.2
44 {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x03, 0x02},
45 8,
46 NID_rc2_cbc,
47 &EVP_rc2_cbc},
48 // 1.2.840.113549.3.7
49 {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x03, 0x07},
50 8,
51 NID_des_ede3_cbc,
52 &EVP_des_ede3_cbc},
53 // 2.16.840.1.101.3.4.1.2
54 {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x02},
55 9,
56 NID_aes_128_cbc,
57 &EVP_aes_128_cbc},
58 // 2.16.840.1.101.3.4.1.22
59 {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x16},
60 9,
61 NID_aes_192_cbc,
62 &EVP_aes_192_cbc},
63 // 2.16.840.1.101.3.4.1.42
64 {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x2a},
65 9,
66 NID_aes_256_cbc,
67 &EVP_aes_256_cbc},
68 };
69
70 static const EVP_CIPHER *cbs_to_cipher(const CBS *cbs) {
71 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kCipherOIDs); i++) {
72 if (CBS_mem_equal(cbs, kCipherOIDs[i].oid, kCipherOIDs[i].oid_len)) {
73 return kCipherOIDs[i].cipher_func();
74 }
75 }
76
77 return NULL;
78 }
79
80 static int add_cipher_oid(CBB *out, int nid) {
81 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kCipherOIDs); i++) {
82 if (kCipherOIDs[i].nid == nid) {
83 CBB child;
84 return CBB_add_asn1(out, &child, CBS_ASN1_OBJECT) &&
85 CBB_add_bytes(&child, kCipherOIDs[i].oid,
86 kCipherOIDs[i].oid_len) &&
87 CBB_flush(out);
88 }
89 }
90
91 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_CIPHER);
92 return 0;
93 }
94
95 static int pkcs5_pbe2_cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
96 const EVP_MD *pbkdf2_md, uint32_t iterations,
97 const char *pass, size_t pass_len,
98 const uint8_t *salt, size_t salt_len,
99 const uint8_t *iv, size_t iv_len, int enc) {
100 if (iv_len != EVP_CIPHER_iv_length(cipher)) {
101 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ERROR_SETTING_CIPHER_PARAMS);
102 return 0;
103 }
104
105 uint8_t key[EVP_MAX_KEY_LENGTH];
106 int ret = PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iterations,
107 pbkdf2_md, EVP_CIPHER_key_length(cipher), key) &&
108 EVP_CipherInit_ex(ctx, cipher, NULL /* engine */, key, iv, enc);
109 OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
110 return ret;
111 }
112
113 int PKCS5_pbe2_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx,
114 const EVP_CIPHER *cipher, uint32_t iterations,
115 const char *pass, size_t pass_len,
116 const uint8_t *salt, size_t salt_len) {
117 int cipher_nid = EVP_CIPHER_nid(cipher);
118 if (cipher_nid == NID_undef) {
119 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
120 return 0;
121 }
122
123 // Generate a random IV.
124 uint8_t iv[EVP_MAX_IV_LENGTH];
125 AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)));
126
127 // See RFC 2898, appendix A.
128 CBB algorithm, oid, param, kdf, kdf_oid, kdf_param, salt_cbb, cipher_cbb,
129 iv_cbb;
130 if (!CBB_add_asn1(out, &algorithm, CBS_ASN1_SEQUENCE) ||
131 !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
132 !CBB_add_bytes(&oid, kPBES2, sizeof(kPBES2)) ||
133 !CBB_add_asn1(&algorithm, &param, CBS_ASN1_SEQUENCE) ||
134 !CBB_add_asn1(&param, &kdf, CBS_ASN1_SEQUENCE) ||
135 !CBB_add_asn1(&kdf, &kdf_oid, CBS_ASN1_OBJECT) ||
136 !CBB_add_bytes(&kdf_oid, kPBKDF2, sizeof(kPBKDF2)) ||
137 !CBB_add_asn1(&kdf, &kdf_param, CBS_ASN1_SEQUENCE) ||
138 !CBB_add_asn1(&kdf_param, &salt_cbb, CBS_ASN1_OCTETSTRING) ||
139 !CBB_add_bytes(&salt_cbb, salt, salt_len) ||
140 !CBB_add_asn1_uint64(&kdf_param, iterations) ||
141 // Specify a key length for RC2.
142 (cipher_nid == NID_rc2_cbc &&
143 !CBB_add_asn1_uint64(&kdf_param, EVP_CIPHER_key_length(cipher))) ||
144 // Omit the PRF. We use the default hmacWithSHA1.
145 !CBB_add_asn1(&param, &cipher_cbb, CBS_ASN1_SEQUENCE) ||
146 !add_cipher_oid(&cipher_cbb, cipher_nid) ||
147 // RFC 2898 says RC2-CBC and RC5-CBC-Pad use a SEQUENCE with version and
148 // IV, but OpenSSL always uses an OCTET STRING IV, so we do the same.
149 !CBB_add_asn1(&cipher_cbb, &iv_cbb, CBS_ASN1_OCTETSTRING) ||
150 !CBB_add_bytes(&iv_cbb, iv, EVP_CIPHER_iv_length(cipher)) ||
151 !CBB_flush(out)) {
152 return 0;
153 }
154
155 return pkcs5_pbe2_cipher_init(ctx, cipher, EVP_sha1(), iterations, pass,
156 pass_len, salt, salt_len, iv,
157 EVP_CIPHER_iv_length(cipher), 1 /* encrypt */);
158 }
159
160 int PKCS5_pbe2_decrypt_init(const struct pbe_suite *suite, EVP_CIPHER_CTX *ctx,
161 const char *pass, size_t pass_len, CBS *param) {
162 CBS pbe_param, kdf, kdf_obj, enc_scheme, enc_obj;
163 if (!CBS_get_asn1(param, &pbe_param, CBS_ASN1_SEQUENCE) ||
164 CBS_len(param) != 0 ||
165 !CBS_get_asn1(&pbe_param, &kdf, CBS_ASN1_SEQUENCE) ||
166 !CBS_get_asn1(&pbe_param, &enc_scheme, CBS_ASN1_SEQUENCE) ||
167 CBS_len(&pbe_param) != 0 ||
168 !CBS_get_asn1(&kdf, &kdf_obj, CBS_ASN1_OBJECT) ||
169 !CBS_get_asn1(&enc_scheme, &enc_obj, CBS_ASN1_OBJECT)) {
170 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
171 return 0;
172 }
173
174 // Only PBKDF2 is supported.
175 if (!CBS_mem_equal(&kdf_obj, kPBKDF2, sizeof(kPBKDF2))) {
176 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
177 return 0;
178 }
179
180 // See if we recognise the encryption algorithm.
181 const EVP_CIPHER *cipher = cbs_to_cipher(&enc_obj);
182 if (cipher == NULL) {
183 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_CIPHER);
184 return 0;
185 }
186
187 // Parse the KDF parameters. See RFC 8018, appendix A.2.
188 CBS pbkdf2_params, salt;
189 uint64_t iterations;
190 if (!CBS_get_asn1(&kdf, &pbkdf2_params, CBS_ASN1_SEQUENCE) ||
191 CBS_len(&kdf) != 0 ||
192 !CBS_get_asn1(&pbkdf2_params, &salt, CBS_ASN1_OCTETSTRING) ||
193 !CBS_get_asn1_uint64(&pbkdf2_params, &iterations)) {
194 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
195 return 0;
196 }
197
198 if (!pkcs12_iterations_acceptable(iterations)) {
199 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_ITERATION_COUNT);
200 return 0;
201 }
202
203 // The optional keyLength parameter, if present, must match the key length of
204 // the cipher.
205 if (CBS_peek_asn1_tag(&pbkdf2_params, CBS_ASN1_INTEGER)) {
206 uint64_t key_len;
207 if (!CBS_get_asn1_uint64(&pbkdf2_params, &key_len)) {
208 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
209 return 0;
210 }
211
212 if (key_len != EVP_CIPHER_key_length(cipher)) {
213 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_KEYLENGTH);
214 return 0;
215 }
216 }
217
218 const EVP_MD *md = EVP_sha1();
219 if (CBS_len(&pbkdf2_params) != 0) {
220 CBS alg_id, prf;
221 if (!CBS_get_asn1(&pbkdf2_params, &alg_id, CBS_ASN1_SEQUENCE) ||
222 !CBS_get_asn1(&alg_id, &prf, CBS_ASN1_OBJECT) ||
223 CBS_len(&pbkdf2_params) != 0) {
224 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
225 return 0;
226 }
227
228 if (CBS_mem_equal(&prf, kHMACWithSHA1, sizeof(kHMACWithSHA1))) {
229 // hmacWithSHA1 is the DEFAULT, so DER requires it be omitted, but we
230 // match OpenSSL in tolerating it being present.
231 md = EVP_sha1();
232 } else if (CBS_mem_equal(&prf, kHMACWithSHA256, sizeof(kHMACWithSHA256))) {
233 md = EVP_sha256();
234 } else {
235 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_PRF);
236 return 0;
237 }
238
239 // All supported PRFs use a NULL parameter.
240 CBS null;
241 if (!CBS_get_asn1(&alg_id, &null, CBS_ASN1_NULL) ||
242 CBS_len(&null) != 0 ||
243 CBS_len(&alg_id) != 0) {
244 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
245 return 0;
246 }
247 }
248
249 // Parse the encryption scheme parameters. Note OpenSSL does not match the
250 // specification. Per RFC 2898, this should depend on the encryption scheme.
251 // In particular, RC2-CBC uses a SEQUENCE with version and IV. We align with
252 // OpenSSL.
253 CBS iv;
254 if (!CBS_get_asn1(&enc_scheme, &iv, CBS_ASN1_OCTETSTRING) ||
255 CBS_len(&enc_scheme) != 0) {
256 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_PRF);
257 return 0;
258 }
259
260 return pkcs5_pbe2_cipher_init(ctx, cipher, md, (uint32_t)iterations, pass,
261 pass_len, CBS_data(&salt), CBS_len(&salt),
262 CBS_data(&iv), CBS_len(&iv), 0 /* decrypt */);
263 }