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