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.c83
1 files changed, 44 insertions, 39 deletions
diff --git a/src/lib/libcrypto/asn1/p5_pbev2.c b/src/lib/libcrypto/asn1/p5_pbev2.c
index 01563a68fd..8917cc4ccf 100644
--- a/src/lib/libcrypto/asn1/p5_pbev2.c
+++ b/src/lib/libcrypto/asn1/p5_pbev2.c
@@ -10,7 +10,7 @@
10 * are met: 10 * are met:
11 * 11 *
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 14 *
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in 16 * notice, this list of conditions and the following disclaimer in
@@ -86,9 +86,9 @@ IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM)
86 * Extended version to allow application supplied PRF NID and IV. 86 * Extended version to allow application supplied PRF NID and IV.
87 */ 87 */
88 88
89X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter, 89X509_ALGOR *
90 unsigned char *salt, int saltlen, 90PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter, unsigned char *salt,
91 unsigned char *aiv, int prf_nid) 91 int saltlen, 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, keylen;
@@ -98,27 +98,30 @@ X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
98 ASN1_OBJECT *obj; 98 ASN1_OBJECT *obj;
99 99
100 alg_nid = EVP_CIPHER_type(cipher); 100 alg_nid = EVP_CIPHER_type(cipher);
101 if(alg_nid == NID_undef) { 101 if (alg_nid == NID_undef) {
102 ASN1err(ASN1_F_PKCS5_PBE2_SET_IV, 102 ASN1err(ASN1_F_PKCS5_PBE2_SET_IV,
103 ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); 103 ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
104 goto err; 104 goto err;
105 } 105 }
106 obj = OBJ_nid2obj(alg_nid); 106 obj = OBJ_nid2obj(alg_nid);
107 107
108 if(!(pbe2 = PBE2PARAM_new())) goto merr; 108 if (!(pbe2 = PBE2PARAM_new()))
109 goto merr;
109 110
110 /* Setup the AlgorithmIdentifier for the encryption scheme */ 111 /* Setup the AlgorithmIdentifier for the encryption scheme */
111 scheme = pbe2->encryption; 112 scheme = pbe2->encryption;
112 113
113 scheme->algorithm = obj; 114 scheme->algorithm = obj;
114 if(!(scheme->parameter = ASN1_TYPE_new())) goto merr; 115 if (!(scheme->parameter = ASN1_TYPE_new()))
116 goto merr;
115 117
116 /* Create random IV */ 118 /* Create random IV */
117 if (EVP_CIPHER_iv_length(cipher)) { 119 if (EVP_CIPHER_iv_length(cipher)) {
118 if (aiv) 120 if (aiv)
119 memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher)); 121 memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
120 else if (RAND_pseudo_bytes(iv, EVP_CIPHER_iv_length(cipher)) < 0) 122 else if (RAND_pseudo_bytes(iv,
121 goto err; 123 EVP_CIPHER_iv_length(cipher)) < 0)
124 goto err;
122 } 125 }
123 126
124 EVP_CIPHER_CTX_init(&ctx); 127 EVP_CIPHER_CTX_init(&ctx);
@@ -126,16 +129,16 @@ X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
126 /* Dummy cipherinit to just setup the IV, and PRF */ 129 /* Dummy cipherinit to just setup the IV, and PRF */
127 if (!EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0)) 130 if (!EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0))
128 goto err; 131 goto err;
129 if(EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) { 132 if (EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) {
130 ASN1err(ASN1_F_PKCS5_PBE2_SET_IV, 133 ASN1err(ASN1_F_PKCS5_PBE2_SET_IV,
131 ASN1_R_ERROR_SETTING_CIPHER_PARAMS); 134 ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
132 EVP_CIPHER_CTX_cleanup(&ctx); 135 EVP_CIPHER_CTX_cleanup(&ctx);
133 goto err; 136 goto err;
134 } 137 }
135 /* If prf NID unspecified see if cipher has a preference. 138 /* If prf NID unspecified see if cipher has a preference.
136 * An error is OK here: just means use default PRF. 139 * An error is OK here: just means use default PRF.
137 */ 140 */
138 if ((prf_nid == -1) && 141 if ((prf_nid == -1) &&
139 EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0) { 142 EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0) {
140 ERR_clear_error(); 143 ERR_clear_error();
141 prf_nid = NID_hmacWithSHA1; 144 prf_nid = NID_hmacWithSHA1;
@@ -144,7 +147,7 @@ X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
144 147
145 /* If its RC2 then we'd better setup the key length */ 148 /* If its RC2 then we'd better setup the key length */
146 149
147 if(alg_nid == NID_rc2_cbc) 150 if (alg_nid == NID_rc2_cbc)
148 keylen = EVP_CIPHER_key_length(cipher); 151 keylen = EVP_CIPHER_key_length(cipher);
149 else 152 else
150 keylen = -1; 153 keylen = -1;
@@ -160,15 +163,17 @@ X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
160 163
161 /* Now set up top level AlgorithmIdentifier */ 164 /* Now set up top level AlgorithmIdentifier */
162 165
163 if(!(ret = X509_ALGOR_new())) goto merr; 166 if (!(ret = X509_ALGOR_new()))
164 if(!(ret->parameter = ASN1_TYPE_new())) goto merr; 167 goto merr;
168 if (!(ret->parameter = ASN1_TYPE_new()))
169 goto merr;
165 170
166 ret->algorithm = OBJ_nid2obj(NID_pbes2); 171 ret->algorithm = OBJ_nid2obj(NID_pbes2);
167 172
168 /* Encode PBE2PARAM into parameter */ 173 /* Encode PBE2PARAM into parameter */
169 174
170 if(!ASN1_item_pack(pbe2, ASN1_ITEM_rptr(PBE2PARAM), 175 if (!ASN1_item_pack(pbe2, ASN1_ITEM_rptr(PBE2PARAM),
171 &ret->parameter->value.sequence)) goto merr; 176 &ret->parameter->value.sequence)) goto merr;
172 ret->parameter->type = V_ASN1_SEQUENCE; 177 ret->parameter->type = V_ASN1_SEQUENCE;
173 178
174 PBE2PARAM_free(pbe2); 179 PBE2PARAM_free(pbe2);
@@ -177,34 +182,35 @@ X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
177 return ret; 182 return ret;
178 183
179merr: 184merr:
180 ASN1err(ASN1_F_PKCS5_PBE2_SET_IV,ERR_R_MALLOC_FAILURE); 185 ASN1err(ASN1_F_PKCS5_PBE2_SET_IV, ERR_R_MALLOC_FAILURE);
181 186
182 err: 187err:
183 PBE2PARAM_free(pbe2); 188 PBE2PARAM_free(pbe2);
184 /* Note 'scheme' is freed as part of pbe2 */ 189 /* Note 'scheme' is freed as part of pbe2 */
185 X509_ALGOR_free(kalg); 190 X509_ALGOR_free(kalg);
186 X509_ALGOR_free(ret); 191 X509_ALGOR_free(ret);
187 192
188 return NULL; 193 return NULL;
189
190} 194}
191 195
192X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, 196X509_ALGOR *
193 unsigned char *salt, int saltlen) 197PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, unsigned char *salt,
198 int saltlen)
194{ 199{
195 return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1); 200 return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1);
196} 201}
197 202
198X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen, 203X509_ALGOR *
199 int prf_nid, int keylen) 204PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen, int prf_nid,
205 int keylen)
200{ 206{
201 X509_ALGOR *keyfunc = NULL; 207 X509_ALGOR *keyfunc = NULL;
202 PBKDF2PARAM *kdf = NULL; 208 PBKDF2PARAM *kdf = NULL;
203 ASN1_OCTET_STRING *osalt = NULL; 209 ASN1_OCTET_STRING *osalt = NULL;
204 210
205 if(!(kdf = PBKDF2PARAM_new())) 211 if (!(kdf = PBKDF2PARAM_new()))
206 goto merr; 212 goto merr;
207 if(!(osalt = M_ASN1_OCTET_STRING_new())) 213 if (!(osalt = M_ASN1_OCTET_STRING_new()))
208 goto merr; 214 goto merr;
209 215
210 kdf->salt->value.octet_string = osalt; 216 kdf->salt->value.octet_string = osalt;
@@ -222,20 +228,20 @@ X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen,
222 else if (RAND_pseudo_bytes (osalt->data, saltlen) < 0) 228 else if (RAND_pseudo_bytes (osalt->data, saltlen) < 0)
223 goto merr; 229 goto merr;
224 230
225 if(iter <= 0) 231 if (iter <= 0)
226 iter = PKCS5_DEFAULT_ITER; 232 iter = PKCS5_DEFAULT_ITER;
227 233
228 if(!ASN1_INTEGER_set(kdf->iter, iter)) 234 if (!ASN1_INTEGER_set(kdf->iter, iter))
229 goto merr; 235 goto merr;
230 236
231 /* If have a key len set it up */ 237 /* If have a key len set it up */
232 238
233 if(keylen > 0) { 239 if (keylen > 0) {
234 if(!(kdf->keylength = M_ASN1_INTEGER_new())) 240 if (!(kdf->keylength = M_ASN1_INTEGER_new()))
235 goto merr; 241 goto merr;
236 if(!ASN1_INTEGER_set (kdf->keylength, keylen)) 242 if (!ASN1_INTEGER_set (kdf->keylength, keylen))
237 goto merr; 243 goto merr;
238 } 244 }
239 245
240 /* prf can stay NULL if we are using hmacWithSHA1 */ 246 /* prf can stay NULL if we are using hmacWithSHA1 */
241 if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1) { 247 if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1) {
@@ -243,7 +249,7 @@ X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen,
243 if (!kdf->prf) 249 if (!kdf->prf)
244 goto merr; 250 goto merr;
245 X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid), 251 X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid),
246 V_ASN1_NULL, NULL); 252 V_ASN1_NULL, NULL);
247 } 253 }
248 254
249 /* Finally setup the keyfunc structure */ 255 /* Finally setup the keyfunc structure */
@@ -256,11 +262,11 @@ X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen,
256 262
257 /* Encode PBKDF2PARAM into parameter of pbe2 */ 263 /* Encode PBKDF2PARAM into parameter of pbe2 */
258 264
259 if(!(keyfunc->parameter = ASN1_TYPE_new())) 265 if (!(keyfunc->parameter = ASN1_TYPE_new()))
260 goto merr; 266 goto merr;
261 267
262 if(!ASN1_item_pack(kdf, ASN1_ITEM_rptr(PBKDF2PARAM), 268 if (!ASN1_item_pack(kdf, ASN1_ITEM_rptr(PBKDF2PARAM),
263 &keyfunc->parameter->value.sequence)) 269 &keyfunc->parameter->value.sequence))
264 goto merr; 270 goto merr;
265 keyfunc->parameter->type = V_ASN1_SEQUENCE; 271 keyfunc->parameter->type = V_ASN1_SEQUENCE;
266 272
@@ -268,9 +274,8 @@ X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen,
268 return keyfunc; 274 return keyfunc;
269 275
270merr: 276merr:
271 ASN1err(ASN1_F_PKCS5_PBKDF2_SET,ERR_R_MALLOC_FAILURE); 277 ASN1err(ASN1_F_PKCS5_PBKDF2_SET, ERR_R_MALLOC_FAILURE);
272 PBKDF2PARAM_free(kdf); 278 PBKDF2PARAM_free(kdf);
273 X509_ALGOR_free(keyfunc); 279 X509_ALGOR_free(keyfunc);
274 return NULL; 280 return NULL;
275} 281}
276