/src/openssl/crypto/asn1/p5_pbev2.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1999-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 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/asn1t.h> |
13 | | #include <openssl/x509.h> |
14 | | #include <openssl/rand.h> |
15 | | |
16 | | /* PKCS#5 v2.0 password based encryption structures */ |
17 | | |
18 | | ASN1_SEQUENCE(PBE2PARAM) = { |
19 | | ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR), |
20 | | ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR) |
21 | | } ASN1_SEQUENCE_END(PBE2PARAM) |
22 | | |
23 | | IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM) |
24 | | |
25 | | ASN1_SEQUENCE(PBKDF2PARAM) = { |
26 | | ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY), |
27 | | ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER), |
28 | | ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER), |
29 | | ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR) |
30 | | } ASN1_SEQUENCE_END(PBKDF2PARAM) |
31 | | |
32 | | IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM) |
33 | | |
34 | | /* |
35 | | * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm: yes I know |
36 | | * this is horrible! Extended version to allow application supplied PRF NID |
37 | | * and IV. |
38 | | */ |
39 | | |
40 | | X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter, |
41 | | unsigned char *salt, int saltlen, |
42 | | unsigned char *aiv, int prf_nid) |
43 | 0 | { |
44 | 0 | X509_ALGOR *scheme = NULL, *ret = NULL; |
45 | 0 | int alg_nid, keylen; |
46 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
47 | 0 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
48 | 0 | PBE2PARAM *pbe2 = NULL; |
49 | 0 |
|
50 | 0 | alg_nid = EVP_CIPHER_type(cipher); |
51 | 0 | if (alg_nid == NID_undef) { |
52 | 0 | ASN1err(ASN1_F_PKCS5_PBE2_SET_IV, |
53 | 0 | ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); |
54 | 0 | goto err; |
55 | 0 | } |
56 | 0 |
|
57 | 0 | if ((pbe2 = PBE2PARAM_new()) == NULL) |
58 | 0 | goto merr; |
59 | 0 | |
60 | 0 | /* Setup the AlgorithmIdentifier for the encryption scheme */ |
61 | 0 | scheme = pbe2->encryption; |
62 | 0 | scheme->algorithm = OBJ_nid2obj(alg_nid); |
63 | 0 | if ((scheme->parameter = ASN1_TYPE_new()) == NULL) |
64 | 0 | goto merr; |
65 | 0 | |
66 | 0 | /* Create random IV */ |
67 | 0 | if (EVP_CIPHER_iv_length(cipher)) { |
68 | 0 | if (aiv) |
69 | 0 | memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher)); |
70 | 0 | else if (RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)) <= 0) |
71 | 0 | goto err; |
72 | 0 | } |
73 | 0 | |
74 | 0 | ctx = EVP_CIPHER_CTX_new(); |
75 | 0 | if (ctx == NULL) |
76 | 0 | goto merr; |
77 | 0 | |
78 | 0 | /* Dummy cipherinit to just setup the IV, and PRF */ |
79 | 0 | if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0)) |
80 | 0 | goto err; |
81 | 0 | if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) <= 0) { |
82 | 0 | ASN1err(ASN1_F_PKCS5_PBE2_SET_IV, ASN1_R_ERROR_SETTING_CIPHER_PARAMS); |
83 | 0 | goto err; |
84 | 0 | } |
85 | 0 | /* |
86 | 0 | * If prf NID unspecified see if cipher has a preference. An error is OK |
87 | 0 | * here: just means use default PRF. |
88 | 0 | */ |
89 | 0 | if ((prf_nid == -1) && |
90 | 0 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0) { |
91 | 0 | ERR_clear_error(); |
92 | 0 | prf_nid = NID_hmacWithSHA256; |
93 | 0 | } |
94 | 0 | EVP_CIPHER_CTX_free(ctx); |
95 | 0 | ctx = NULL; |
96 | 0 |
|
97 | 0 | /* If its RC2 then we'd better setup the key length */ |
98 | 0 |
|
99 | 0 | if (alg_nid == NID_rc2_cbc) |
100 | 0 | keylen = EVP_CIPHER_key_length(cipher); |
101 | 0 | else |
102 | 0 | keylen = -1; |
103 | 0 |
|
104 | 0 | /* Setup keyfunc */ |
105 | 0 |
|
106 | 0 | X509_ALGOR_free(pbe2->keyfunc); |
107 | 0 |
|
108 | 0 | pbe2->keyfunc = PKCS5_pbkdf2_set(iter, salt, saltlen, prf_nid, keylen); |
109 | 0 |
|
110 | 0 | if (!pbe2->keyfunc) |
111 | 0 | goto merr; |
112 | 0 | |
113 | 0 | /* Now set up top level AlgorithmIdentifier */ |
114 | 0 | |
115 | 0 | if ((ret = X509_ALGOR_new()) == NULL) |
116 | 0 | goto merr; |
117 | 0 | |
118 | 0 | ret->algorithm = OBJ_nid2obj(NID_pbes2); |
119 | 0 |
|
120 | 0 | /* Encode PBE2PARAM into parameter */ |
121 | 0 |
|
122 | 0 | if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2, |
123 | 0 | &ret->parameter)) |
124 | 0 | goto merr; |
125 | 0 | |
126 | 0 | PBE2PARAM_free(pbe2); |
127 | 0 | pbe2 = NULL; |
128 | 0 |
|
129 | 0 | return ret; |
130 | 0 | |
131 | 0 | merr: |
132 | 0 | ASN1err(ASN1_F_PKCS5_PBE2_SET_IV, ERR_R_MALLOC_FAILURE); |
133 | 0 |
|
134 | 0 | err: |
135 | 0 | EVP_CIPHER_CTX_free(ctx); |
136 | 0 | PBE2PARAM_free(pbe2); |
137 | 0 | /* Note 'scheme' is freed as part of pbe2 */ |
138 | 0 | X509_ALGOR_free(ret); |
139 | 0 |
|
140 | 0 | return NULL; |
141 | 0 | } |
142 | | |
143 | | X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, |
144 | | unsigned char *salt, int saltlen) |
145 | 0 | { |
146 | 0 | return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1); |
147 | 0 | } |
148 | | |
149 | | X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen, |
150 | | int prf_nid, int keylen) |
151 | 0 | { |
152 | 0 | X509_ALGOR *keyfunc = NULL; |
153 | 0 | PBKDF2PARAM *kdf = NULL; |
154 | 0 | ASN1_OCTET_STRING *osalt = NULL; |
155 | 0 |
|
156 | 0 | if ((kdf = PBKDF2PARAM_new()) == NULL) |
157 | 0 | goto merr; |
158 | 0 | if ((osalt = ASN1_OCTET_STRING_new()) == NULL) |
159 | 0 | goto merr; |
160 | 0 | |
161 | 0 | kdf->salt->value.octet_string = osalt; |
162 | 0 | kdf->salt->type = V_ASN1_OCTET_STRING; |
163 | 0 |
|
164 | 0 | if (saltlen == 0) |
165 | 0 | saltlen = PKCS5_SALT_LEN; |
166 | 0 | if ((osalt->data = OPENSSL_malloc(saltlen)) == NULL) |
167 | 0 | goto merr; |
168 | 0 | |
169 | 0 | osalt->length = saltlen; |
170 | 0 |
|
171 | 0 | if (salt) |
172 | 0 | memcpy(osalt->data, salt, saltlen); |
173 | 0 | else if (RAND_bytes(osalt->data, saltlen) <= 0) |
174 | 0 | goto merr; |
175 | 0 | |
176 | 0 | if (iter <= 0) |
177 | 0 | iter = PKCS5_DEFAULT_ITER; |
178 | 0 |
|
179 | 0 | if (!ASN1_INTEGER_set(kdf->iter, iter)) |
180 | 0 | goto merr; |
181 | 0 | |
182 | 0 | /* If have a key len set it up */ |
183 | 0 | |
184 | 0 | if (keylen > 0) { |
185 | 0 | if ((kdf->keylength = ASN1_INTEGER_new()) == NULL) |
186 | 0 | goto merr; |
187 | 0 | if (!ASN1_INTEGER_set(kdf->keylength, keylen)) |
188 | 0 | goto merr; |
189 | 0 | } |
190 | 0 | |
191 | 0 | /* prf can stay NULL if we are using hmacWithSHA1 */ |
192 | 0 | if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1) { |
193 | 0 | kdf->prf = X509_ALGOR_new(); |
194 | 0 | if (kdf->prf == NULL) |
195 | 0 | goto merr; |
196 | 0 | X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid), V_ASN1_NULL, NULL); |
197 | 0 | } |
198 | 0 |
|
199 | 0 | /* Finally setup the keyfunc structure */ |
200 | 0 |
|
201 | 0 | keyfunc = X509_ALGOR_new(); |
202 | 0 | if (keyfunc == NULL) |
203 | 0 | goto merr; |
204 | 0 | |
205 | 0 | keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2); |
206 | 0 |
|
207 | 0 | /* Encode PBKDF2PARAM into parameter of pbe2 */ |
208 | 0 |
|
209 | 0 | if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), kdf, |
210 | 0 | &keyfunc->parameter)) |
211 | 0 | goto merr; |
212 | 0 | |
213 | 0 | PBKDF2PARAM_free(kdf); |
214 | 0 | return keyfunc; |
215 | 0 | |
216 | 0 | merr: |
217 | 0 | ASN1err(ASN1_F_PKCS5_PBKDF2_SET, ERR_R_MALLOC_FAILURE); |
218 | 0 | PBKDF2PARAM_free(kdf); |
219 | 0 | X509_ALGOR_free(keyfunc); |
220 | 0 | return NULL; |
221 | 0 | } |