summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/p5_pbev2.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/p5_pbev2.c')
-rw-r--r--src/lib/libcrypto/asn1/p5_pbev2.c143
1 files changed, 49 insertions, 94 deletions
diff --git a/src/lib/libcrypto/asn1/p5_pbev2.c b/src/lib/libcrypto/asn1/p5_pbev2.c
index 4ea683036b..cb49b6651d 100644
--- a/src/lib/libcrypto/asn1/p5_pbev2.c
+++ b/src/lib/libcrypto/asn1/p5_pbev2.c
@@ -91,10 +91,12 @@ 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, keylen; 94 int alg_nid;
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;
97 PBE2PARAM *pbe2 = NULL; 98 PBE2PARAM *pbe2 = NULL;
99 ASN1_OCTET_STRING *osalt = NULL;
98 ASN1_OBJECT *obj; 100 ASN1_OBJECT *obj;
99 101
100 alg_nid = EVP_CIPHER_type(cipher); 102 alg_nid = EVP_CIPHER_type(cipher);
@@ -125,8 +127,7 @@ X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
125 EVP_CIPHER_CTX_init(&ctx); 127 EVP_CIPHER_CTX_init(&ctx);
126 128
127 /* Dummy cipherinit to just setup the IV, and PRF */ 129 /* Dummy cipherinit to just setup the IV, and PRF */
128 if (!EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0)) 130 EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0);
129 goto err;
130 if(EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) { 131 if(EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) {
131 ASN1err(ASN1_F_PKCS5_PBE2_SET_IV, 132 ASN1err(ASN1_F_PKCS5_PBE2_SET_IV,
132 ASN1_R_ERROR_SETTING_CIPHER_PARAMS); 133 ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
@@ -144,21 +145,55 @@ X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
144 } 145 }
145 EVP_CIPHER_CTX_cleanup(&ctx); 146 EVP_CIPHER_CTX_cleanup(&ctx);
146 147
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
147 /* If its RC2 then we'd better setup the key length */ 165 /* If its RC2 then we'd better setup the key length */
148 166
149 if(alg_nid == NID_rc2_cbc) 167 if(alg_nid == NID_rc2_cbc) {
150 keylen = EVP_CIPHER_key_length(cipher); 168 if(!(kdf->keylength = M_ASN1_INTEGER_new())) goto merr;
151 else 169 if(!ASN1_INTEGER_set (kdf->keylength,
152 keylen = -1; 170 EVP_CIPHER_key_length(cipher))) goto merr;
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 */
153 184
154 /* Setup keyfunc */ 185 pbe2->keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2);
155 186
156 X509_ALGOR_free(pbe2->keyfunc); 187 /* Encode PBKDF2PARAM into parameter of pbe2 */
157 188
158 pbe2->keyfunc = PKCS5_pbkdf2_set(iter, salt, saltlen, prf_nid, keylen); 189 if(!(pbe2->keyfunc->parameter = ASN1_TYPE_new())) goto merr;
159 190
160 if (!pbe2->keyfunc) 191 if(!ASN1_item_pack(kdf, ASN1_ITEM_rptr(PBKDF2PARAM),
161 goto merr; 192 &pbe2->keyfunc->parameter->value.sequence)) goto merr;
193 pbe2->keyfunc->parameter->type = V_ASN1_SEQUENCE;
194
195 PBKDF2PARAM_free(kdf);
196 kdf = NULL;
162 197
163 /* Now set up top level AlgorithmIdentifier */ 198 /* Now set up top level AlgorithmIdentifier */
164 199
@@ -184,6 +219,8 @@ X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
184 err: 219 err:
185 PBE2PARAM_free(pbe2); 220 PBE2PARAM_free(pbe2);
186 /* Note 'scheme' is freed as part of pbe2 */ 221 /* Note 'scheme' is freed as part of pbe2 */
222 M_ASN1_OCTET_STRING_free(osalt);
223 PBKDF2PARAM_free(kdf);
187 X509_ALGOR_free(kalg); 224 X509_ALGOR_free(kalg);
188 X509_ALGOR_free(ret); 225 X509_ALGOR_free(ret);
189 226
@@ -196,85 +233,3 @@ X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter,
196 { 233 {
197 return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1); 234 return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1);
198 } 235 }
199
200X509_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