diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/p5_pbev2.c')
-rw-r--r-- | src/lib/libcrypto/asn1/p5_pbev2.c | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/src/lib/libcrypto/asn1/p5_pbev2.c b/src/lib/libcrypto/asn1/p5_pbev2.c index 09f4bf6112..4ce06a94ab 100644 --- a/src/lib/libcrypto/asn1/p5_pbev2.c +++ b/src/lib/libcrypto/asn1/p5_pbev2.c | |||
@@ -104,7 +104,7 @@ void PBE2PARAM_free (PBE2PARAM *a) | |||
104 | if(a==NULL) return; | 104 | if(a==NULL) return; |
105 | X509_ALGOR_free(a->keyfunc); | 105 | X509_ALGOR_free(a->keyfunc); |
106 | X509_ALGOR_free(a->encryption); | 106 | X509_ALGOR_free(a->encryption); |
107 | Free ((char *)a); | 107 | Free (a); |
108 | } | 108 | } |
109 | 109 | ||
110 | int i2d_PBKDF2PARAM(PBKDF2PARAM *a, unsigned char **pp) | 110 | int i2d_PBKDF2PARAM(PBKDF2PARAM *a, unsigned char **pp) |
@@ -131,7 +131,7 @@ PBKDF2PARAM *PBKDF2PARAM_new(void) | |||
131 | ASN1_CTX c; | 131 | ASN1_CTX c; |
132 | M_ASN1_New_Malloc(ret, PBKDF2PARAM); | 132 | M_ASN1_New_Malloc(ret, PBKDF2PARAM); |
133 | M_ASN1_New(ret->salt, ASN1_TYPE_new); | 133 | M_ASN1_New(ret->salt, ASN1_TYPE_new); |
134 | M_ASN1_New(ret->iter, ASN1_INTEGER_new); | 134 | M_ASN1_New(ret->iter, M_ASN1_INTEGER_new); |
135 | ret->keylength = NULL; | 135 | ret->keylength = NULL; |
136 | ret->prf = NULL; | 136 | ret->prf = NULL; |
137 | return (ret); | 137 | return (ret); |
@@ -155,10 +155,10 @@ void PBKDF2PARAM_free (PBKDF2PARAM *a) | |||
155 | { | 155 | { |
156 | if(a==NULL) return; | 156 | if(a==NULL) return; |
157 | ASN1_TYPE_free(a->salt); | 157 | ASN1_TYPE_free(a->salt); |
158 | ASN1_INTEGER_free(a->iter); | 158 | M_ASN1_INTEGER_free(a->iter); |
159 | ASN1_INTEGER_free(a->keylength); | 159 | M_ASN1_INTEGER_free(a->keylength); |
160 | X509_ALGOR_free(a->prf); | 160 | X509_ALGOR_free(a->prf); |
161 | Free ((char *)a); | 161 | Free (a); |
162 | } | 162 | } |
163 | 163 | ||
164 | /* Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm: | 164 | /* Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm: |
@@ -175,19 +175,26 @@ X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, | |||
175 | PBKDF2PARAM *kdf = NULL; | 175 | PBKDF2PARAM *kdf = NULL; |
176 | PBE2PARAM *pbe2 = NULL; | 176 | PBE2PARAM *pbe2 = NULL; |
177 | ASN1_OCTET_STRING *osalt = NULL; | 177 | ASN1_OCTET_STRING *osalt = NULL; |
178 | ASN1_OBJECT *obj; | ||
179 | |||
180 | alg_nid = EVP_CIPHER_type(cipher); | ||
181 | if(alg_nid == NID_undef) { | ||
182 | ASN1err(ASN1_F_PKCS5_PBE2_SET, | ||
183 | ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); | ||
184 | goto err; | ||
185 | } | ||
186 | obj = OBJ_nid2obj(alg_nid); | ||
178 | 187 | ||
179 | if(!(pbe2 = PBE2PARAM_new())) goto merr; | 188 | if(!(pbe2 = PBE2PARAM_new())) goto merr; |
180 | 189 | ||
181 | /* Setup the AlgorithmIdentifier for the encryption scheme */ | 190 | /* Setup the AlgorithmIdentifier for the encryption scheme */ |
182 | scheme = pbe2->encryption; | 191 | scheme = pbe2->encryption; |
183 | 192 | ||
184 | alg_nid = EVP_CIPHER_type(cipher); | 193 | scheme->algorithm = obj; |
185 | |||
186 | scheme->algorithm = OBJ_nid2obj(alg_nid); | ||
187 | if(!(scheme->parameter = ASN1_TYPE_new())) goto merr; | 194 | if(!(scheme->parameter = ASN1_TYPE_new())) goto merr; |
188 | 195 | ||
189 | /* Create random IV */ | 196 | /* Create random IV */ |
190 | RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)); | 197 | RAND_pseudo_bytes(iv, EVP_CIPHER_iv_length(cipher)); |
191 | 198 | ||
192 | /* Dummy cipherinit to just setup the IV */ | 199 | /* Dummy cipherinit to just setup the IV */ |
193 | EVP_CipherInit(&ctx, cipher, NULL, iv, 0); | 200 | EVP_CipherInit(&ctx, cipher, NULL, iv, 0); |
@@ -199,13 +206,13 @@ X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, | |||
199 | EVP_CIPHER_CTX_cleanup(&ctx); | 206 | EVP_CIPHER_CTX_cleanup(&ctx); |
200 | 207 | ||
201 | if(!(kdf = PBKDF2PARAM_new())) goto merr; | 208 | if(!(kdf = PBKDF2PARAM_new())) goto merr; |
202 | if(!(osalt = ASN1_OCTET_STRING_new())) goto merr; | 209 | if(!(osalt = M_ASN1_OCTET_STRING_new())) goto merr; |
203 | 210 | ||
204 | if (!saltlen) saltlen = PKCS5_SALT_LEN; | 211 | if (!saltlen) saltlen = PKCS5_SALT_LEN; |
205 | if (!(osalt->data = Malloc (saltlen))) goto merr; | 212 | if (!(osalt->data = Malloc (saltlen))) goto merr; |
206 | osalt->length = saltlen; | 213 | osalt->length = saltlen; |
207 | if (salt) memcpy (osalt->data, salt, saltlen); | 214 | if (salt) memcpy (osalt->data, salt, saltlen); |
208 | else RAND_bytes (osalt->data, saltlen); | 215 | else if (RAND_bytes (osalt->data, saltlen) <= 0) goto merr; |
209 | 216 | ||
210 | if(iter <= 0) iter = PKCS5_DEFAULT_ITER; | 217 | if(iter <= 0) iter = PKCS5_DEFAULT_ITER; |
211 | if(!ASN1_INTEGER_set(kdf->iter, iter)) goto merr; | 218 | if(!ASN1_INTEGER_set(kdf->iter, iter)) goto merr; |
@@ -218,7 +225,7 @@ X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, | |||
218 | /* If its RC2 then we'd better setup the key length */ | 225 | /* If its RC2 then we'd better setup the key length */ |
219 | 226 | ||
220 | if(alg_nid == NID_rc2_cbc) { | 227 | if(alg_nid == NID_rc2_cbc) { |
221 | if(!(kdf->keylength = ASN1_INTEGER_new())) goto merr; | 228 | if(!(kdf->keylength = M_ASN1_INTEGER_new())) goto merr; |
222 | if(!ASN1_INTEGER_set (kdf->keylength, | 229 | if(!ASN1_INTEGER_set (kdf->keylength, |
223 | EVP_CIPHER_key_length(cipher))) goto merr; | 230 | EVP_CIPHER_key_length(cipher))) goto merr; |
224 | } | 231 | } |
@@ -264,7 +271,7 @@ X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, | |||
264 | err: | 271 | err: |
265 | PBE2PARAM_free(pbe2); | 272 | PBE2PARAM_free(pbe2); |
266 | /* Note 'scheme' is freed as part of pbe2 */ | 273 | /* Note 'scheme' is freed as part of pbe2 */ |
267 | ASN1_OCTET_STRING_free(osalt); | 274 | M_ASN1_OCTET_STRING_free(osalt); |
268 | PBKDF2PARAM_free(kdf); | 275 | PBKDF2PARAM_free(kdf); |
269 | X509_ALGOR_free(kalg); | 276 | X509_ALGOR_free(kalg); |
270 | X509_ALGOR_free(ret); | 277 | X509_ALGOR_free(ret); |