]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/evp_pbe.c
Avoid name clash.
[thirdparty/openssl.git] / crypto / evp / evp_pbe.c
CommitLineData
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
67static STACK *pbe_algs;
68
69/* Setup a cipher context from a PBE algorithm */
70
856640b5
DSH
71typedef struct
72 {
73 int pbe_type;
74 int pbe_nid;
75 int cipher_nid;
76 int md_nid;
77 EVP_PBE_KEYGEN *keygen;
78 } EVP_PBE_CTL;
cfcefcbe 79
560b79cb 80static const EVP_PBE_CTL builtin_pbe[] =
b8f702a0
DSH
81 {
82 {EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndDES_CBC,
83 NID_des_cbc, NID_md2, PKCS5_PBE_keyivgen},
84 {EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC,
85 NID_des_cbc, NID_md5, PKCS5_PBE_keyivgen},
86 {EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndRC2_CBC,
87 NID_rc2_64_cbc, NID_sha1, PKCS5_PBE_keyivgen},
88
89 {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC4,
90 NID_rc4, NID_sha1, PKCS12_PBE_keyivgen},
91 {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC4,
92 NID_rc4_40, NID_sha1, PKCS12_PBE_keyivgen},
93 {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
94 NID_des_ede3_cbc, NID_sha1, PKCS12_PBE_keyivgen},
95 {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And2_Key_TripleDES_CBC,
96 NID_des_ede_cbc, NID_sha1, PKCS12_PBE_keyivgen},
97 {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC2_CBC,
98 NID_rc2_cbc, NID_sha1, PKCS12_PBE_keyivgen},
99 {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC2_CBC,
100 NID_rc2_40_cbc, NID_sha1, PKCS12_PBE_keyivgen},
101
102#ifndef OPENSSL_NO_HMAC
103 {EVP_PBE_TYPE_OUTER, NID_pbes2, -1, -1, PKCS5_v2_PBE_keyivgen},
104#endif
105 {EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndRC2_CBC,
106 NID_rc2_64_cbc, NID_md2, PKCS5_PBE_keyivgen},
107 {EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndRC2_CBC,
108 NID_rc2_64_cbc, NID_md5, PKCS5_PBE_keyivgen},
109 {EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndDES_CBC,
110 NID_des_cbc, NID_sha1, PKCS5_PBE_keyivgen},
111
112
113 {EVP_PBE_TYPE_PRF, NID_hmacWithSHA1, -1, NID_sha1, 0},
b8f702a0 114 {EVP_PBE_TYPE_PRF, NID_hmacWithMD5, -1, NID_md5, 0},
1631d5f9
DSH
115 {EVP_PBE_TYPE_PRF, NID_hmacWithSHA224, -1, NID_sha224, 0},
116 {EVP_PBE_TYPE_PRF, NID_hmacWithSHA256, -1, NID_sha256, 0},
117 {EVP_PBE_TYPE_PRF, NID_hmacWithSHA384, -1, NID_sha384, 0},
118 {EVP_PBE_TYPE_PRF, NID_hmacWithSHA512, -1, NID_sha512, 0},
b0ec1146 119 {EVP_PBE_TYPE_PRF, NID_id_HMACGostR3411_94, -1, NID_id_GostR3411_94, 0},
b8f702a0
DSH
120 };
121
122#ifdef TEST
123int main(int argc, char **argv)
124 {
125 int i, nid_md, nid_cipher;
126 EVP_PBE_CTL *tpbe, *tpbe2;
127 /*OpenSSL_add_all_algorithms();*/
128
129 for (i = 0; i < sizeof(builtin_pbe)/sizeof(EVP_PBE_CTL); i++)
130 {
131 tpbe = builtin_pbe + i;
132 fprintf(stderr, "%d %d %s ", tpbe->pbe_type, tpbe->pbe_nid,
133 OBJ_nid2sn(tpbe->pbe_nid));
134 if (EVP_PBE_find(tpbe->pbe_type, tpbe->pbe_nid,
135 &nid_cipher ,&nid_md,0))
136 fprintf(stderr, "Found %s %s\n",
137 OBJ_nid2sn(nid_cipher),
138 OBJ_nid2sn(nid_md));
139 else
140 fprintf(stderr, "Find ERROR!!\n");
141 }
142
143 return 0;
144 }
145#endif
146
147
148
8afca8d9 149int EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
69cbf468 150 ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de)
856640b5
DSH
151 {
152 const EVP_CIPHER *cipher;
153 const EVP_MD *md;
154 int cipher_nid, md_nid;
155 EVP_PBE_KEYGEN *keygen;
cfcefcbe 156
856640b5
DSH
157 if (!EVP_PBE_find(EVP_PBE_TYPE_OUTER, OBJ_obj2nid(pbe_obj),
158 &cipher_nid, &md_nid, &keygen))
159 {
cfcefcbe
DSH
160 char obj_tmp[80];
161 EVPerr(EVP_F_EVP_PBE_CIPHERINIT,EVP_R_UNKNOWN_PBE_ALGORITHM);
d420ac2c 162 if (!pbe_obj) BUF_strlcpy (obj_tmp, "NULL", sizeof obj_tmp);
54a656ef 163 else i2t_ASN1_OBJECT(obj_tmp, sizeof obj_tmp, pbe_obj);
cfcefcbe
DSH
164 ERR_add_error_data(2, "TYPE=", obj_tmp);
165 return 0;
856640b5
DSH
166 }
167
168 if(!pass)
169 passlen = 0;
170 else if (passlen == -1)
171 passlen = strlen(pass);
172
173 if (cipher_nid == -1)
174 cipher = NULL;
175 else
176 cipher = EVP_get_cipherbynid(cipher_nid);
177
178 if (md_nid == -1)
179 md = NULL;
180 else
181 md = EVP_get_digestbynid(md_nid);
182
183 if (!keygen(ctx, pass, passlen, param, cipher, md, en_de))
184 {
cfcefcbe
DSH
185 EVPerr(EVP_F_EVP_PBE_CIPHERINIT,EVP_R_KEYGEN_FAILURE);
186 return 0;
856640b5 187 }
cfcefcbe
DSH
188 return 1;
189}
190
b8f702a0
DSH
191static int pbe_cmp2(const void *a, const void *b)
192 {
193 const EVP_PBE_CTL *pbe1 = a;
194 const EVP_PBE_CTL *pbe2 = b;
195 int ret = pbe1->pbe_type - pbe2->pbe_type;
196 if (ret)
197 return ret;
198 else
199 return pbe1->pbe_nid - pbe2->pbe_nid;
200 }
201
13083215 202static int pbe_cmp(const char * const *a, const char * const *b)
856640b5 203 {
70f34a58
NL
204 const EVP_PBE_CTL * const *pbe1 = (const EVP_PBE_CTL * const *) a,
205 * const *pbe2 = (const EVP_PBE_CTL * const *)b;
856640b5
DSH
206 int ret = (*pbe1)->pbe_type - (*pbe2)->pbe_type;
207 if (ret)
208 return ret;
209 else
210 return (*pbe1)->pbe_nid - (*pbe2)->pbe_nid;
211 }
cfcefcbe
DSH
212
213/* Add a PBE algorithm */
214
856640b5 215int EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid, int md_nid,
6b691a5c 216 EVP_PBE_KEYGEN *keygen)
856640b5 217 {
cfcefcbe 218 EVP_PBE_CTL *pbe_tmp;
856640b5
DSH
219 if (!pbe_algs)
220 pbe_algs = sk_new(pbe_cmp);
221 if (!(pbe_tmp = (EVP_PBE_CTL*) OPENSSL_malloc (sizeof(EVP_PBE_CTL))))
222 {
5c95c2ac 223 EVPerr(EVP_F_EVP_PBE_ALG_ADD_TYPE,ERR_R_MALLOC_FAILURE);
cfcefcbe 224 return 0;
856640b5
DSH
225 }
226 pbe_tmp->pbe_type = pbe_type;
227 pbe_tmp->pbe_nid = pbe_nid;
228 pbe_tmp->cipher_nid = cipher_nid;
229 pbe_tmp->md_nid = md_nid;
cfcefcbe 230 pbe_tmp->keygen = keygen;
856640b5
DSH
231
232
cfcefcbe
DSH
233 sk_push (pbe_algs, (char *)pbe_tmp);
234 return 1;
856640b5
DSH
235 }
236
237int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,
238 EVP_PBE_KEYGEN *keygen)
239 {
240 int cipher_nid, md_nid;
241 if (cipher)
242 cipher_nid = EVP_CIPHER_type(cipher);
243 else
244 cipher_nid = -1;
245 if (md)
246 md_nid = EVP_MD_type(md);
247 else
248 md_nid = -1;
249
250 return EVP_PBE_alg_add_type(EVP_PBE_TYPE_OUTER, nid,
251 cipher_nid, md_nid, keygen);
252 }
253
254int EVP_PBE_find(int type, int pbe_nid,
255 int *pcnid, int *pmnid, EVP_PBE_KEYGEN **pkeygen)
256 {
b8f702a0 257 EVP_PBE_CTL *pbetmp = NULL, pbelu;
856640b5
DSH
258 int i;
259 if (pbe_nid == NID_undef)
260 return 0;
b8f702a0 261
856640b5
DSH
262 pbelu.pbe_type = type;
263 pbelu.pbe_nid = pbe_nid;
b8f702a0
DSH
264
265 if (pbe_algs)
266 {
267 i = sk_find(pbe_algs, (char *)&pbelu);
268 if (i != -1)
269 pbetmp = (EVP_PBE_CTL *)sk_value (pbe_algs, i);
270 }
271 if (pbetmp == NULL)
272 {
273 pbetmp = (EVP_PBE_CTL *) OBJ_bsearch((char *)&pbelu,
274 (char *)builtin_pbe,
275 sizeof(builtin_pbe)/sizeof(EVP_PBE_CTL),
276 sizeof(EVP_PBE_CTL),
277 pbe_cmp2);
278 }
279 if (pbetmp == NULL)
856640b5 280 return 0;
856640b5
DSH
281 if (pcnid)
282 *pcnid = pbetmp->cipher_nid;
283 if (pmnid)
284 *pmnid = pbetmp->md_nid;
285 if (pkeygen)
286 *pkeygen = pbetmp->keygen;
287 return 1;
288 }
289
290
ee0508d4 291
6b691a5c 292void EVP_PBE_cleanup(void)
856640b5 293 {
26a3a48d 294 sk_pop_free(pbe_algs, OPENSSL_freeFunc);
b64f8256 295 pbe_algs = NULL;
856640b5 296 }