diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/p5_pbev2.c')
-rw-r--r-- | src/lib/libcrypto/asn1/p5_pbev2.c | 143 |
1 files changed, 94 insertions, 49 deletions
diff --git a/src/lib/libcrypto/asn1/p5_pbev2.c b/src/lib/libcrypto/asn1/p5_pbev2.c index cb49b6651d..4ea683036b 100644 --- a/src/lib/libcrypto/asn1/p5_pbev2.c +++ b/src/lib/libcrypto/asn1/p5_pbev2.c | |||
@@ -91,12 +91,10 @@ X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter, | |||
91 | unsigned char *aiv, int prf_nid) | 91 | unsigned char *aiv, int prf_nid) |
92 | { | 92 | { |
93 | X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL; | 93 | X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL; |
94 | int alg_nid; | 94 | int alg_nid, keylen; |
95 | EVP_CIPHER_CTX ctx; | 95 | EVP_CIPHER_CTX ctx; |
96 | unsigned char iv[EVP_MAX_IV_LENGTH]; | 96 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
97 | PBKDF2PARAM *kdf = NULL; | ||
98 | PBE2PARAM *pbe2 = NULL; | 97 | PBE2PARAM *pbe2 = NULL; |
99 | ASN1_OCTET_STRING *osalt = NULL; | ||
100 | ASN1_OBJECT *obj; | 98 | ASN1_OBJECT *obj; |
101 | 99 | ||
102 | alg_nid = EVP_CIPHER_type(cipher); | 100 | alg_nid = EVP_CIPHER_type(cipher); |
@@ -127,7 +125,8 @@ X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter, | |||
127 | EVP_CIPHER_CTX_init(&ctx); | 125 | EVP_CIPHER_CTX_init(&ctx); |
128 | 126 | ||
129 | /* Dummy cipherinit to just setup the IV, and PRF */ | 127 | /* Dummy cipherinit to just setup the IV, and PRF */ |
130 | EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0); | 128 | if (!EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0)) |
129 | goto err; | ||
131 | if(EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) { | 130 | if(EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) { |
132 | ASN1err(ASN1_F_PKCS5_PBE2_SET_IV, | 131 | ASN1err(ASN1_F_PKCS5_PBE2_SET_IV, |
133 | ASN1_R_ERROR_SETTING_CIPHER_PARAMS); | 132 | ASN1_R_ERROR_SETTING_CIPHER_PARAMS); |
@@ -145,55 +144,21 @@ X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter, | |||
145 | } | 144 | } |
146 | EVP_CIPHER_CTX_cleanup(&ctx); | 145 | EVP_CIPHER_CTX_cleanup(&ctx); |
147 | 146 | ||
148 | if(!(kdf = PBKDF2PARAM_new())) goto merr; | ||
149 | if(!(osalt = M_ASN1_OCTET_STRING_new())) goto merr; | ||
150 | |||
151 | if (!saltlen) saltlen = PKCS5_SALT_LEN; | ||
152 | if (!(osalt->data = OPENSSL_malloc (saltlen))) goto merr; | ||
153 | osalt->length = saltlen; | ||
154 | if (salt) memcpy (osalt->data, salt, saltlen); | ||
155 | else if (RAND_pseudo_bytes (osalt->data, saltlen) < 0) goto merr; | ||
156 | |||
157 | if(iter <= 0) iter = PKCS5_DEFAULT_ITER; | ||
158 | if(!ASN1_INTEGER_set(kdf->iter, iter)) goto merr; | ||
159 | |||
160 | /* Now include salt in kdf structure */ | ||
161 | kdf->salt->value.octet_string = osalt; | ||
162 | kdf->salt->type = V_ASN1_OCTET_STRING; | ||
163 | osalt = NULL; | ||
164 | |||
165 | /* If its RC2 then we'd better setup the key length */ | 147 | /* If its RC2 then we'd better setup the key length */ |
166 | 148 | ||
167 | if(alg_nid == NID_rc2_cbc) { | 149 | if(alg_nid == NID_rc2_cbc) |
168 | if(!(kdf->keylength = M_ASN1_INTEGER_new())) goto merr; | 150 | keylen = EVP_CIPHER_key_length(cipher); |
169 | if(!ASN1_INTEGER_set (kdf->keylength, | 151 | else |
170 | EVP_CIPHER_key_length(cipher))) goto merr; | 152 | keylen = -1; |
171 | } | ||
172 | |||
173 | /* prf can stay NULL if we are using hmacWithSHA1 */ | ||
174 | if (prf_nid != NID_hmacWithSHA1) | ||
175 | { | ||
176 | kdf->prf = X509_ALGOR_new(); | ||
177 | if (!kdf->prf) | ||
178 | goto merr; | ||
179 | X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid), | ||
180 | V_ASN1_NULL, NULL); | ||
181 | } | ||
182 | |||
183 | /* Now setup the PBE2PARAM keyfunc structure */ | ||
184 | 153 | ||
185 | pbe2->keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2); | 154 | /* Setup keyfunc */ |
186 | 155 | ||
187 | /* Encode PBKDF2PARAM into parameter of pbe2 */ | 156 | X509_ALGOR_free(pbe2->keyfunc); |
188 | 157 | ||
189 | if(!(pbe2->keyfunc->parameter = ASN1_TYPE_new())) goto merr; | 158 | pbe2->keyfunc = PKCS5_pbkdf2_set(iter, salt, saltlen, prf_nid, keylen); |
190 | 159 | ||
191 | if(!ASN1_item_pack(kdf, ASN1_ITEM_rptr(PBKDF2PARAM), | 160 | if (!pbe2->keyfunc) |
192 | &pbe2->keyfunc->parameter->value.sequence)) goto merr; | 161 | goto merr; |
193 | pbe2->keyfunc->parameter->type = V_ASN1_SEQUENCE; | ||
194 | |||
195 | PBKDF2PARAM_free(kdf); | ||
196 | kdf = NULL; | ||
197 | 162 | ||
198 | /* Now set up top level AlgorithmIdentifier */ | 163 | /* Now set up top level AlgorithmIdentifier */ |
199 | 164 | ||
@@ -219,8 +184,6 @@ X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter, | |||
219 | err: | 184 | err: |
220 | PBE2PARAM_free(pbe2); | 185 | PBE2PARAM_free(pbe2); |
221 | /* Note 'scheme' is freed as part of pbe2 */ | 186 | /* Note 'scheme' is freed as part of pbe2 */ |
222 | M_ASN1_OCTET_STRING_free(osalt); | ||
223 | PBKDF2PARAM_free(kdf); | ||
224 | X509_ALGOR_free(kalg); | 187 | X509_ALGOR_free(kalg); |
225 | X509_ALGOR_free(ret); | 188 | X509_ALGOR_free(ret); |
226 | 189 | ||
@@ -233,3 +196,85 @@ X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, | |||
233 | { | 196 | { |
234 | return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1); | 197 | return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1); |
235 | } | 198 | } |
199 | |||
200 | X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen, | ||
201 | int prf_nid, int keylen) | ||
202 | { | ||
203 | X509_ALGOR *keyfunc = NULL; | ||
204 | PBKDF2PARAM *kdf = NULL; | ||
205 | ASN1_OCTET_STRING *osalt = NULL; | ||
206 | |||
207 | if(!(kdf = PBKDF2PARAM_new())) | ||
208 | goto merr; | ||
209 | if(!(osalt = M_ASN1_OCTET_STRING_new())) | ||
210 | goto merr; | ||
211 | |||
212 | kdf->salt->value.octet_string = osalt; | ||
213 | kdf->salt->type = V_ASN1_OCTET_STRING; | ||
214 | |||
215 | if (!saltlen) | ||
216 | saltlen = PKCS5_SALT_LEN; | ||
217 | if (!(osalt->data = OPENSSL_malloc (saltlen))) | ||
218 | goto merr; | ||
219 | |||
220 | osalt->length = saltlen; | ||
221 | |||
222 | if (salt) | ||
223 | memcpy (osalt->data, salt, saltlen); | ||
224 | else if (RAND_pseudo_bytes (osalt->data, saltlen) < 0) | ||
225 | goto merr; | ||
226 | |||
227 | if(iter <= 0) | ||
228 | iter = PKCS5_DEFAULT_ITER; | ||
229 | |||
230 | if(!ASN1_INTEGER_set(kdf->iter, iter)) | ||
231 | goto merr; | ||
232 | |||
233 | /* If have a key len set it up */ | ||
234 | |||
235 | if(keylen > 0) | ||
236 | { | ||
237 | if(!(kdf->keylength = M_ASN1_INTEGER_new())) | ||
238 | goto merr; | ||
239 | if(!ASN1_INTEGER_set (kdf->keylength, keylen)) | ||
240 | goto merr; | ||
241 | } | ||
242 | |||
243 | /* prf can stay NULL if we are using hmacWithSHA1 */ | ||
244 | if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1) | ||
245 | { | ||
246 | kdf->prf = X509_ALGOR_new(); | ||
247 | if (!kdf->prf) | ||
248 | goto merr; | ||
249 | X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid), | ||
250 | V_ASN1_NULL, NULL); | ||
251 | } | ||
252 | |||
253 | /* Finally setup the keyfunc structure */ | ||
254 | |||
255 | keyfunc = X509_ALGOR_new(); | ||
256 | if (!keyfunc) | ||
257 | goto merr; | ||
258 | |||
259 | keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2); | ||
260 | |||
261 | /* Encode PBKDF2PARAM into parameter of pbe2 */ | ||
262 | |||
263 | if(!(keyfunc->parameter = ASN1_TYPE_new())) | ||
264 | goto merr; | ||
265 | |||
266 | if(!ASN1_item_pack(kdf, ASN1_ITEM_rptr(PBKDF2PARAM), | ||
267 | &keyfunc->parameter->value.sequence)) | ||
268 | goto merr; | ||
269 | keyfunc->parameter->type = V_ASN1_SEQUENCE; | ||
270 | |||
271 | PBKDF2PARAM_free(kdf); | ||
272 | return keyfunc; | ||
273 | |||
274 | merr: | ||
275 | ASN1err(ASN1_F_PKCS5_PBKDF2_SET,ERR_R_MALLOC_FAILURE); | ||
276 | PBKDF2PARAM_free(kdf); | ||
277 | X509_ALGOR_free(keyfunc); | ||
278 | return NULL; | ||
279 | } | ||
280 | |||