]>
Commit | Line | Data |
---|---|---|
61f5b6f3 | 1 | /* evp_pbe.c */ |
0f113f3e MC |
2 | /* |
3 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project | |
4 | * 1999. | |
cfcefcbe DSH |
5 | */ |
6 | /* ==================================================================== | |
856640b5 | 7 | * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved. |
cfcefcbe DSH |
8 | * |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * | |
13 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 14 | * notice, this list of conditions and the following disclaimer. |
cfcefcbe DSH |
15 | * |
16 | * 2. Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in | |
18 | * the documentation and/or other materials provided with the | |
19 | * distribution. | |
20 | * | |
21 | * 3. All advertising materials mentioning features or use of this | |
22 | * software must display the following acknowledgment: | |
23 | * "This product includes software developed by the OpenSSL Project | |
24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
25 | * | |
26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
27 | * endorse or promote products derived from this software without | |
28 | * prior written permission. For written permission, please contact | |
29 | * licensing@OpenSSL.org. | |
30 | * | |
31 | * 5. Products derived from this software may not be called "OpenSSL" | |
32 | * nor may "OpenSSL" appear in their names without prior written | |
33 | * permission of the OpenSSL Project. | |
34 | * | |
35 | * 6. Redistributions of any form whatsoever must retain the following | |
36 | * acknowledgment: | |
37 | * "This product includes software developed by the OpenSSL Project | |
38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
39 | * | |
40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
52 | * ==================================================================== | |
53 | * | |
54 | * This product includes cryptographic software written by Eric Young | |
55 | * (eay@cryptsoft.com). This product includes software written by Tim | |
56 | * Hudson (tjh@cryptsoft.com). | |
57 | * | |
58 | */ | |
59 | ||
60 | #include <stdio.h> | |
b39fc560 | 61 | #include "internal/cryptlib.h" |
ec577822 | 62 | #include <openssl/evp.h> |
b8f702a0 | 63 | #include <openssl/pkcs12.h> |
ec577822 | 64 | #include <openssl/x509.h> |
3d63b396 | 65 | #include "evp_locl.h" |
cfcefcbe DSH |
66 | |
67 | /* Password based encryption (PBE) functions */ | |
68 | ||
d4cdbab9 | 69 | DECLARE_STACK_OF(EVP_PBE_CTL) |
5ce278a7 | 70 | static STACK_OF(EVP_PBE_CTL) *pbe_algs; |
cfcefcbe DSH |
71 | |
72 | /* Setup a cipher context from a PBE algorithm */ | |
73 | ||
0f113f3e MC |
74 | typedef struct { |
75 | int pbe_type; | |
76 | int pbe_nid; | |
77 | int cipher_nid; | |
78 | int md_nid; | |
79 | EVP_PBE_KEYGEN *keygen; | |
80 | } EVP_PBE_CTL; | |
81 | ||
82 | static const EVP_PBE_CTL builtin_pbe[] = { | |
83 | {EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndDES_CBC, | |
84 | NID_des_cbc, NID_md2, PKCS5_PBE_keyivgen}, | |
85 | {EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC, | |
86 | NID_des_cbc, NID_md5, PKCS5_PBE_keyivgen}, | |
87 | {EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndRC2_CBC, | |
88 | NID_rc2_64_cbc, NID_sha1, PKCS5_PBE_keyivgen}, | |
b8f702a0 | 89 | |
0f113f3e | 90 | {EVP_PBE_TYPE_OUTER, NID_id_pbkdf2, -1, -1, PKCS5_v2_PBKDF2_keyivgen}, |
3d63b396 | 91 | |
0f113f3e MC |
92 | {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC4, |
93 | NID_rc4, NID_sha1, PKCS12_PBE_keyivgen}, | |
94 | {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC4, | |
95 | NID_rc4_40, NID_sha1, PKCS12_PBE_keyivgen}, | |
96 | {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, | |
97 | NID_des_ede3_cbc, NID_sha1, PKCS12_PBE_keyivgen}, | |
98 | {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And2_Key_TripleDES_CBC, | |
99 | NID_des_ede_cbc, NID_sha1, PKCS12_PBE_keyivgen}, | |
100 | {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC2_CBC, | |
101 | NID_rc2_cbc, NID_sha1, PKCS12_PBE_keyivgen}, | |
102 | {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC2_CBC, | |
103 | NID_rc2_40_cbc, NID_sha1, PKCS12_PBE_keyivgen}, | |
b8f702a0 | 104 | |
0f113f3e | 105 | {EVP_PBE_TYPE_OUTER, NID_pbes2, -1, -1, PKCS5_v2_PBE_keyivgen}, |
a283d2a8 | 106 | |
0f113f3e MC |
107 | {EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndRC2_CBC, |
108 | NID_rc2_64_cbc, NID_md2, PKCS5_PBE_keyivgen}, | |
109 | {EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndRC2_CBC, | |
110 | NID_rc2_64_cbc, NID_md5, PKCS5_PBE_keyivgen}, | |
111 | {EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndDES_CBC, | |
112 | NID_des_cbc, NID_sha1, PKCS5_PBE_keyivgen}, | |
113 | ||
114 | {EVP_PBE_TYPE_PRF, NID_hmacWithSHA1, -1, NID_sha1, 0}, | |
115 | {EVP_PBE_TYPE_PRF, NID_hmacWithMD5, -1, NID_md5, 0}, | |
116 | {EVP_PBE_TYPE_PRF, NID_hmacWithSHA224, -1, NID_sha224, 0}, | |
117 | {EVP_PBE_TYPE_PRF, NID_hmacWithSHA256, -1, NID_sha256, 0}, | |
118 | {EVP_PBE_TYPE_PRF, NID_hmacWithSHA384, -1, NID_sha384, 0}, | |
119 | {EVP_PBE_TYPE_PRF, NID_hmacWithSHA512, -1, NID_sha512, 0}, | |
120 | {EVP_PBE_TYPE_PRF, NID_id_HMACGostR3411_94, -1, NID_id_GostR3411_94, 0}, | |
eeb1c397 MC |
121 | {EVP_PBE_TYPE_PRF, NID_id_tc26_hmac_gost_3411_2012_256, -1, |
122 | NID_id_GostR3411_2012_256, 0}, | |
123 | {EVP_PBE_TYPE_PRF, NID_id_tc26_hmac_gost_3411_2012_512, -1, | |
124 | NID_id_GostR3411_2012_512, 0}, | |
e98aa30d | 125 | {EVP_PBE_TYPE_KDF, NID_id_pbkdf2, -1, -1, PKCS5_v2_PBKDF2_keyivgen}, |
b0809bc8 | 126 | #ifndef OPENSSL_NO_SCRYPT |
e98aa30d | 127 | {EVP_PBE_TYPE_KDF, NID_id_scrypt, -1, -1, PKCS5_v2_scrypt_keyivgen} |
b0809bc8 | 128 | #endif |
0f113f3e | 129 | }; |
b8f702a0 | 130 | |
8afca8d9 | 131 | int EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen, |
0f113f3e MC |
132 | ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de) |
133 | { | |
134 | const EVP_CIPHER *cipher; | |
135 | const EVP_MD *md; | |
136 | int cipher_nid, md_nid; | |
137 | EVP_PBE_KEYGEN *keygen; | |
138 | ||
139 | if (!EVP_PBE_find(EVP_PBE_TYPE_OUTER, OBJ_obj2nid(pbe_obj), | |
140 | &cipher_nid, &md_nid, &keygen)) { | |
141 | char obj_tmp[80]; | |
142 | EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_UNKNOWN_PBE_ALGORITHM); | |
143 | if (!pbe_obj) | |
7644a9ae | 144 | OPENSSL_strlcpy(obj_tmp, "NULL", sizeof obj_tmp); |
0f113f3e MC |
145 | else |
146 | i2t_ASN1_OBJECT(obj_tmp, sizeof obj_tmp, pbe_obj); | |
147 | ERR_add_error_data(2, "TYPE=", obj_tmp); | |
148 | return 0; | |
149 | } | |
150 | ||
151 | if (!pass) | |
152 | passlen = 0; | |
153 | else if (passlen == -1) | |
154 | passlen = strlen(pass); | |
155 | ||
156 | if (cipher_nid == -1) | |
157 | cipher = NULL; | |
158 | else { | |
159 | cipher = EVP_get_cipherbynid(cipher_nid); | |
160 | if (!cipher) { | |
161 | EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_UNKNOWN_CIPHER); | |
162 | return 0; | |
163 | } | |
164 | } | |
165 | ||
166 | if (md_nid == -1) | |
167 | md = NULL; | |
168 | else { | |
169 | md = EVP_get_digestbynid(md_nid); | |
170 | if (!md) { | |
171 | EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_UNKNOWN_DIGEST); | |
172 | return 0; | |
173 | } | |
174 | } | |
175 | ||
176 | if (!keygen(ctx, pass, passlen, param, cipher, md, en_de)) { | |
177 | EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_KEYGEN_FAILURE); | |
178 | return 0; | |
179 | } | |
180 | return 1; | |
cfcefcbe DSH |
181 | } |
182 | ||
e19106f5 | 183 | DECLARE_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2); |
babb3798 | 184 | |
e19106f5 | 185 | static int pbe2_cmp(const EVP_PBE_CTL *pbe1, const EVP_PBE_CTL *pbe2) |
0f113f3e MC |
186 | { |
187 | int ret = pbe1->pbe_type - pbe2->pbe_type; | |
188 | if (ret) | |
189 | return ret; | |
190 | else | |
191 | return pbe1->pbe_nid - pbe2->pbe_nid; | |
192 | } | |
b8f702a0 | 193 | |
e19106f5 | 194 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2); |
babb3798 | 195 | |
0f113f3e MC |
196 | static int pbe_cmp(const EVP_PBE_CTL *const *a, const EVP_PBE_CTL *const *b) |
197 | { | |
198 | int ret = (*a)->pbe_type - (*b)->pbe_type; | |
199 | if (ret) | |
200 | return ret; | |
201 | else | |
202 | return (*a)->pbe_nid - (*b)->pbe_nid; | |
203 | } | |
cfcefcbe DSH |
204 | |
205 | /* Add a PBE algorithm */ | |
206 | ||
0f113f3e MC |
207 | int EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid, |
208 | int md_nid, EVP_PBE_KEYGEN *keygen) | |
209 | { | |
210 | EVP_PBE_CTL *pbe_tmp; | |
b4faea50 | 211 | |
8cf9d71a | 212 | if (pbe_algs == NULL) { |
0f113f3e | 213 | pbe_algs = sk_EVP_PBE_CTL_new(pbe_cmp); |
8cf9d71a AG |
214 | if (pbe_algs == NULL) |
215 | goto err; | |
0f113f3e | 216 | } |
8cf9d71a AG |
217 | |
218 | if ((pbe_tmp = OPENSSL_malloc(sizeof(*pbe_tmp))) == NULL) | |
219 | goto err; | |
220 | ||
0f113f3e MC |
221 | pbe_tmp->pbe_type = pbe_type; |
222 | pbe_tmp->pbe_nid = pbe_nid; | |
223 | pbe_tmp->cipher_nid = cipher_nid; | |
224 | pbe_tmp->md_nid = md_nid; | |
225 | pbe_tmp->keygen = keygen; | |
226 | ||
227 | sk_EVP_PBE_CTL_push(pbe_algs, pbe_tmp); | |
228 | return 1; | |
8cf9d71a AG |
229 | |
230 | err: | |
231 | EVPerr(EVP_F_EVP_PBE_ALG_ADD_TYPE, ERR_R_MALLOC_FAILURE); | |
232 | return 0; | |
0f113f3e | 233 | } |
856640b5 DSH |
234 | |
235 | int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md, | |
0f113f3e MC |
236 | EVP_PBE_KEYGEN *keygen) |
237 | { | |
238 | int cipher_nid, md_nid; | |
75ebbd9a | 239 | |
0f113f3e MC |
240 | if (cipher) |
241 | cipher_nid = EVP_CIPHER_nid(cipher); | |
242 | else | |
243 | cipher_nid = -1; | |
244 | if (md) | |
245 | md_nid = EVP_MD_type(md); | |
246 | else | |
247 | md_nid = -1; | |
248 | ||
249 | return EVP_PBE_alg_add_type(EVP_PBE_TYPE_OUTER, nid, | |
250 | cipher_nid, md_nid, keygen); | |
251 | } | |
856640b5 DSH |
252 | |
253 | int EVP_PBE_find(int type, int pbe_nid, | |
0f113f3e MC |
254 | int *pcnid, int *pmnid, EVP_PBE_KEYGEN **pkeygen) |
255 | { | |
256 | EVP_PBE_CTL *pbetmp = NULL, pbelu; | |
257 | int i; | |
258 | if (pbe_nid == NID_undef) | |
259 | return 0; | |
260 | ||
261 | pbelu.pbe_type = type; | |
262 | pbelu.pbe_nid = pbe_nid; | |
263 | ||
264 | if (pbe_algs) { | |
265 | i = sk_EVP_PBE_CTL_find(pbe_algs, &pbelu); | |
266 | if (i != -1) | |
267 | pbetmp = sk_EVP_PBE_CTL_value(pbe_algs, i); | |
268 | } | |
269 | if (pbetmp == NULL) { | |
b6eb9827 | 270 | pbetmp = OBJ_bsearch_pbe2(&pbelu, builtin_pbe, OSSL_NELEM(builtin_pbe)); |
0f113f3e MC |
271 | } |
272 | if (pbetmp == NULL) | |
273 | return 0; | |
274 | if (pcnid) | |
275 | *pcnid = pbetmp->cipher_nid; | |
276 | if (pmnid) | |
277 | *pmnid = pbetmp->md_nid; | |
278 | if (pkeygen) | |
279 | *pkeygen = pbetmp->keygen; | |
280 | return 1; | |
281 | } | |
856640b5 | 282 | |
5ce278a7 | 283 | static void free_evp_pbe_ctl(EVP_PBE_CTL *pbe) |
0f113f3e | 284 | { |
4b45c6e5 | 285 | OPENSSL_free(pbe); |
0f113f3e | 286 | } |
ee0508d4 | 287 | |
6b691a5c | 288 | void EVP_PBE_cleanup(void) |
0f113f3e MC |
289 | { |
290 | sk_EVP_PBE_CTL_pop_free(pbe_algs, free_evp_pbe_ctl); | |
291 | pbe_algs = NULL; | |
292 | } | |
d9546693 DSH |
293 | |
294 | int EVP_PBE_get(int *ptype, int *ppbe_nid, size_t num) | |
295 | { | |
296 | const EVP_PBE_CTL *tpbe; | |
297 | ||
298 | if (num >= OSSL_NELEM(builtin_pbe)) | |
299 | return 0; | |
300 | ||
301 | tpbe = builtin_pbe + num; | |
302 | if (ptype) | |
303 | *ptype = tpbe->pbe_type; | |
304 | if (ppbe_nid) | |
305 | *ppbe_nid = tpbe->pbe_nid; | |
306 | return 1; | |
307 | } |