Debian Codegraph @

external/boringssl/crypto/pkcs8/p5_pbev2.c

raw
1 /* Written by Dr Stephen N Henson ([email protected]) for the OpenSSL
2 * project 1999-2004.
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 * [email protected].
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 * ([email protected]). This product includes software written by Tim
54 * Hudson ([email protected]). */
55
56 #include <assert.h>
57 #include <limits.h>
58 #include <string.h>
59
60 #include <openssl/asn1t.h>
61 #include <openssl/cipher.h>
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64 #include <openssl/obj.h>
65 #include <openssl/pkcs8.h>
66 #include <openssl/rand.h>
67 #include <openssl/x509.h>
68
69 #include "internal.h"
70
71
72 /* PKCS#5 v2.0 password based encryption structures */
73
74 ASN1_SEQUENCE(PBE2PARAM) = {
75 ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR),
76 ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR)
77 } ASN1_SEQUENCE_END(PBE2PARAM)
78
79 IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM)
80
81 ASN1_SEQUENCE(PBKDF2PARAM) = {
82 ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY),
83 ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER),
84 ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER),
85 ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR)
86 } ASN1_SEQUENCE_END(PBKDF2PARAM)
87
88 IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM);
89
90 static int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len)
91 {
92 ASN1_STRING *os;
93
94 if ((os=M_ASN1_OCTET_STRING_new()) == NULL) return(0);
95 if (!M_ASN1_OCTET_STRING_set(os,data,len))
96 {
97 M_ASN1_OCTET_STRING_free(os);
98 return 0;
99 }
100 ASN1_TYPE_set(a,V_ASN1_OCTET_STRING,os);
101 return(1);
102 }
103
104 static int param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
105 {
106 unsigned iv_len;
107
108 iv_len = EVP_CIPHER_CTX_iv_length(c);
109 return ASN1_TYPE_set_octetstring(type, c->oiv, iv_len);
110 }
111
112 /* Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm:
113 * yes I know this is horrible!
114 *
115 * Extended version to allow application supplied PRF NID and IV. */
116
117 X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
118 unsigned char *salt, int saltlen,
119 unsigned char *aiv, int prf_nid)
120 {
121 X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL;
122 int alg_nid, keylen;
123 EVP_CIPHER_CTX ctx;
124 unsigned char iv[EVP_MAX_IV_LENGTH];
125 PBE2PARAM *pbe2 = NULL;
126 const ASN1_OBJECT *obj;
127
128 alg_nid = EVP_CIPHER_nid(cipher);
129 if(alg_nid == NID_undef) {
130 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
131 goto err;
132 }
133 obj = OBJ_nid2obj(alg_nid);
134
135 if(!(pbe2 = PBE2PARAM_new())) goto merr;
136
137 /* Setup the AlgorithmIdentifier for the encryption scheme */
138 scheme = pbe2->encryption;
139
140 scheme->algorithm = (ASN1_OBJECT*) obj;
141 if(!(scheme->parameter = ASN1_TYPE_new())) goto merr;
142
143 /* Create random IV */
144 if (EVP_CIPHER_iv_length(cipher))
145 {
146 if (aiv)
147 memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
148 else if (!RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)))
149 goto err;
150 }
151
152 EVP_CIPHER_CTX_init(&ctx);
153
154 /* Dummy cipherinit to just setup the IV, and PRF */
155 if (!EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0))
156 goto err;
157 if(param_to_asn1(&ctx, scheme->parameter) < 0) {
158 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ERROR_SETTING_CIPHER_PARAMS);
159 EVP_CIPHER_CTX_cleanup(&ctx);
160 goto err;
161 }
162 /* If prf NID unspecified see if cipher has a preference.
163 * An error is OK here: just means use default PRF.
164 */
165 if ((prf_nid == -1) &&
166 EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0)
167 {
168 ERR_clear_error();
169 prf_nid = NID_hmacWithSHA1;
170 }
171 EVP_CIPHER_CTX_cleanup(&ctx);
172
173 /* If its RC2 then we'd better setup the key length */
174
175 if(alg_nid == NID_rc2_cbc)
176 keylen = EVP_CIPHER_key_length(cipher);
177 else
178 keylen = -1;
179
180 /* Setup keyfunc */
181
182 X509_ALGOR_free(pbe2->keyfunc);
183
184 pbe2->keyfunc = PKCS5_pbkdf2_set(iter, salt, saltlen, prf_nid, keylen);
185
186 if (!pbe2->keyfunc)
187 goto merr;
188
189 /* Now set up top level AlgorithmIdentifier */
190
191 if(!(ret = X509_ALGOR_new())) goto merr;
192 if(!(ret->parameter = ASN1_TYPE_new())) goto merr;
193
194 ret->algorithm = (ASN1_OBJECT*) OBJ_nid2obj(NID_pbes2);
195
196 /* Encode PBE2PARAM into parameter */
197
198 if(!ASN1_item_pack(pbe2, ASN1_ITEM_rptr(PBE2PARAM),
199 &ret->parameter->value.sequence)) goto merr;
200 ret->parameter->type = V_ASN1_SEQUENCE;
201
202 PBE2PARAM_free(pbe2);
203 pbe2 = NULL;
204
205 return ret;
206
207 merr:
208 OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE);
209
210 err:
211 PBE2PARAM_free(pbe2);
212 /* Note 'scheme' is freed as part of pbe2 */
213 X509_ALGOR_free(kalg);
214 X509_ALGOR_free(ret);
215
216 return NULL;
217
218 }
219
220 X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter,
221 unsigned char *salt, int saltlen)
222 {
223 return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1);
224 }
225
226 X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen,
227 int prf_nid, int keylen)
228 {
229 X509_ALGOR *keyfunc = NULL;
230 PBKDF2PARAM *kdf = NULL;
231 ASN1_OCTET_STRING *osalt = NULL;
232
233 if(!(kdf = PBKDF2PARAM_new()))
234 goto merr;
235 if(!(osalt = M_ASN1_OCTET_STRING_new()))
236 goto merr;
237
238 kdf->salt->value.octet_string = osalt;
239 kdf->salt->type = V_ASN1_OCTET_STRING;
240
241 if (!saltlen)
242 saltlen = PKCS5_SALT_LEN;
243 if (!(osalt->data = OPENSSL_malloc (saltlen)))
244 goto merr;
245
246 osalt->length = saltlen;
247
248 if (salt)
249 memcpy (osalt->data, salt, saltlen);
250 else if (!RAND_bytes(osalt->data, saltlen))
251 goto merr;
252
253 if(iter <= 0)
254 iter = PKCS5_DEFAULT_ITERATIONS;
255
256 if(!ASN1_INTEGER_set(kdf->iter, iter))
257 goto merr;
258
259 /* If have a key len set it up */
260
261 if(keylen > 0)
262 {
263 if(!(kdf->keylength = M_ASN1_INTEGER_new()))
264 goto merr;
265 if(!ASN1_INTEGER_set (kdf->keylength, keylen))
266 goto merr;
267 }
268
269 /* prf can stay NULL if we are using hmacWithSHA1 */
270 if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1)
271 {
272 kdf->prf = X509_ALGOR_new();
273 if (!kdf->prf)
274 goto merr;
275 X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid),
276 V_ASN1_NULL, NULL);
277 }
278
279 /* Finally setup the keyfunc structure */
280
281 keyfunc = X509_ALGOR_new();
282 if (!keyfunc)
283 goto merr;
284
285 keyfunc->algorithm = (ASN1_OBJECT*) OBJ_nid2obj(NID_id_pbkdf2);
286
287 /* Encode PBKDF2PARAM into parameter of pbe2 */
288
289 if(!(keyfunc->parameter = ASN1_TYPE_new()))
290 goto merr;
291
292 if(!ASN1_item_pack(kdf, ASN1_ITEM_rptr(PBKDF2PARAM),
293 &keyfunc->parameter->value.sequence))
294 goto merr;
295 keyfunc->parameter->type = V_ASN1_SEQUENCE;
296
297 PBKDF2PARAM_free(kdf);
298 return keyfunc;
299
300 merr:
301 OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE);
302 PBKDF2PARAM_free(kdf);
303 X509_ALGOR_free(keyfunc);
304 return NULL;
305 }
306
307 static int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx,
308 const uint8_t *pass_raw,
309 size_t pass_raw_len, const ASN1_TYPE *param,
310 const ASN1_TYPE *iv, int enc) {
311 int rv = 0;
312 PBKDF2PARAM *pbkdf2param = NULL;
313
314 if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
315 OPENSSL_PUT_ERROR(PKCS8, CIPHER_R_NO_CIPHER_SET);
316 goto err;
317 }
318
319 /* Decode parameters. */
320 if (param == NULL || param->type != V_ASN1_SEQUENCE) {
321 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
322 goto err;
323 }
324
325 const uint8_t *pbuf = param->value.sequence->data;
326 int plen = param->value.sequence->length;
327 pbkdf2param = d2i_PBKDF2PARAM(NULL, &pbuf, plen);
328 if (pbkdf2param == NULL || pbuf != param->value.sequence->data + plen) {
329 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
330 goto err;
331 }
332
333 /* Now check the parameters. */
334 uint8_t key[EVP_MAX_KEY_LENGTH];
335 const size_t key_len = EVP_CIPHER_CTX_key_length(ctx);
336 assert(key_len <= sizeof(key));
337
338 if (pbkdf2param->keylength != NULL &&
339 ASN1_INTEGER_get(pbkdf2param->keylength) != (int) key_len) {
340 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_KEYLENGTH);
341 goto err;
342 }
343
344 if (pbkdf2param->prf != NULL &&
345 OBJ_obj2nid(pbkdf2param->prf->algorithm) != NID_hmacWithSHA1) {
346 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_PRF);
347 goto err;
348 }
349
350 if (pbkdf2param->salt->type != V_ASN1_OCTET_STRING) {
351 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_SALT_TYPE);
352 goto err;
353 }
354
355 if (pbkdf2param->iter->type != V_ASN1_INTEGER) {
356 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_ITERATION_COUNT);
357 goto err;
358 }
359 long iterations = ASN1_INTEGER_get(pbkdf2param->iter);
360 if (iterations <= 0 ||
361 (sizeof(long) > sizeof(unsigned) && iterations > (long)UINT_MAX)) {
362 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_ITERATION_COUNT);
363 goto err;
364 }
365
366 if (iv->type != V_ASN1_OCTET_STRING || iv->value.octet_string == NULL) {
367 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ERROR_SETTING_CIPHER_PARAMS);
368 goto err;
369 }
370
371 const size_t iv_len = EVP_CIPHER_CTX_iv_length(ctx);
372 if ((size_t) iv->value.octet_string->length != iv_len) {
373 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ERROR_SETTING_CIPHER_PARAMS);
374 goto err;
375 }
376
377 if (!PKCS5_PBKDF2_HMAC_SHA1((const char *) pass_raw, pass_raw_len,
378 pbkdf2param->salt->value.octet_string->data,
379 pbkdf2param->salt->value.octet_string->length,
380 iterations, key_len, key)) {
381 goto err;
382 }
383
384 rv = EVP_CipherInit_ex(ctx, NULL /* cipher */, NULL /* engine */, key,
385 iv->value.octet_string->data, enc);
386
387 err:
388 PBKDF2PARAM_free(pbkdf2param);
389 return rv;
390 }
391
392 int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const uint8_t *pass_raw,
393 size_t pass_raw_len, ASN1_TYPE *param,
394 const EVP_CIPHER *unused, const EVP_MD *unused2,
395 int enc) {
396 PBE2PARAM *pbe2param = NULL;
397 int rv = 0;
398
399 if (param == NULL ||
400 param->type != V_ASN1_SEQUENCE ||
401 param->value.sequence == NULL) {
402 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
403 goto err;
404 }
405
406 const uint8_t *pbuf = param->value.sequence->data;
407 int plen = param->value.sequence->length;
408 pbe2param = d2i_PBE2PARAM(NULL, &pbuf, plen);
409 if (pbe2param == NULL || pbuf != param->value.sequence->data + plen) {
410 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
411 goto err;
412 }
413
414 /* Check that the key derivation function is PBKDF2. */
415 if (OBJ_obj2nid(pbe2param->keyfunc->algorithm) != NID_id_pbkdf2) {
416 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
417 goto err;
418 }
419
420 /* See if we recognise the encryption algorithm. */
421 const EVP_CIPHER *cipher =
422 EVP_get_cipherbynid(OBJ_obj2nid(pbe2param->encryption->algorithm));
423 if (cipher == NULL) {
424 OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_CIPHER);
425 goto err;
426 }
427
428 /* Fixup cipher based on AlgorithmIdentifier. */
429 if (!EVP_CipherInit_ex(ctx, cipher, NULL /* engine */, NULL /* key */,
430 NULL /* iv */, enc)) {
431 goto err;
432 }
433
434 rv = PKCS5_v2_PBKDF2_keyivgen(ctx, pass_raw, pass_raw_len,
435 pbe2param->keyfunc->parameter,
436 pbe2param->encryption->parameter, enc);
437
438 err:
439 PBE2PARAM_free(pbe2param);
440 return rv;
441 }