xref: /dragonfly/crypto/libressl/crypto/evp/evp_pbe.c (revision 8edacedf)
1 /* $OpenBSD: evp_pbe.c,v 1.26 2020/06/05 17:30:41 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2006 The OpenSSL Project.  All rights reserved.
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>
60 #include <string.h>
61 
62 #include <openssl/opensslconf.h>
63 
64 #include <openssl/err.h>
65 #include <openssl/evp.h>
66 #include <openssl/pkcs12.h>
67 #include <openssl/x509.h>
68 
69 #include "evp_locl.h"
70 
71 /* Password based encryption (PBE) functions */
72 
73 DECLARE_STACK_OF(EVP_PBE_CTL)
74 static STACK_OF(EVP_PBE_CTL) *pbe_algs;
75 
76 /* Setup a cipher context from a PBE algorithm */
77 
78 typedef struct {
79 	int pbe_type;
80 	int pbe_nid;
81 	int cipher_nid;
82 	int md_nid;
83 	EVP_PBE_KEYGEN *keygen;
84 } EVP_PBE_CTL;
85 
86 static const EVP_PBE_CTL builtin_pbe[] = {
87 	{EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndDES_CBC, NID_des_cbc, NID_md2, PKCS5_PBE_keyivgen},
88 	{EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC, NID_des_cbc, NID_md5, PKCS5_PBE_keyivgen},
89 	{EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndRC2_CBC, NID_rc2_64_cbc, NID_sha1, PKCS5_PBE_keyivgen},
90 
91 #ifndef OPENSSL_NO_HMAC
92 	{EVP_PBE_TYPE_OUTER, NID_id_pbkdf2, -1, -1, PKCS5_v2_PBKDF2_keyivgen},
93 #endif
94 
95 	{EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC4, NID_rc4, NID_sha1, PKCS12_PBE_keyivgen},
96 	{EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC4, NID_rc4_40, NID_sha1, PKCS12_PBE_keyivgen},
97 	{EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NID_des_ede3_cbc, NID_sha1, PKCS12_PBE_keyivgen},
98 	{EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And2_Key_TripleDES_CBC, NID_des_ede_cbc, NID_sha1, PKCS12_PBE_keyivgen},
99 	{EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC2_CBC, NID_rc2_cbc, NID_sha1, PKCS12_PBE_keyivgen},
100 	{EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC2_CBC, 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, NID_rc2_64_cbc, NID_md2, PKCS5_PBE_keyivgen},
106 	{EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndRC2_CBC, NID_rc2_64_cbc, NID_md5, PKCS5_PBE_keyivgen},
107 	{EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndDES_CBC, NID_des_cbc, NID_sha1, PKCS5_PBE_keyivgen},
108 
109 
110 	{EVP_PBE_TYPE_PRF, NID_hmacWithSHA1, -1, NID_sha1, 0},
111 	{EVP_PBE_TYPE_PRF, NID_hmacWithMD5, -1, NID_md5, 0},
112 	{EVP_PBE_TYPE_PRF, NID_hmacWithSHA224, -1, NID_sha224, 0},
113 	{EVP_PBE_TYPE_PRF, NID_hmacWithSHA256, -1, NID_sha256, 0},
114 	{EVP_PBE_TYPE_PRF, NID_hmacWithSHA384, -1, NID_sha384, 0},
115 	{EVP_PBE_TYPE_PRF, NID_hmacWithSHA512, -1, NID_sha512, 0},
116 	{EVP_PBE_TYPE_PRF, NID_id_HMACGostR3411_94, -1, NID_id_GostR3411_94, 0},
117 	{EVP_PBE_TYPE_PRF, NID_id_tc26_hmac_gost_3411_12_256, -1, NID_id_tc26_gost3411_2012_256, 0},
118 	{EVP_PBE_TYPE_PRF, NID_id_tc26_hmac_gost_3411_12_512, -1, NID_id_tc26_gost3411_2012_512, 0},
119 };
120 
121 int
EVP_PBE_CipherInit(ASN1_OBJECT * pbe_obj,const char * pass,int passlen,ASN1_TYPE * param,EVP_CIPHER_CTX * ctx,int en_de)122 EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
123     ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de)
124 {
125 	const EVP_CIPHER *cipher;
126 	const EVP_MD *md;
127 	int cipher_nid, md_nid;
128 	EVP_PBE_KEYGEN *keygen;
129 
130 	if (!EVP_PBE_find(EVP_PBE_TYPE_OUTER, OBJ_obj2nid(pbe_obj),
131 	    &cipher_nid, &md_nid, &keygen)) {
132 		char obj_tmp[80];
133 		EVPerror(EVP_R_UNKNOWN_PBE_ALGORITHM);
134 		if (!pbe_obj)
135 			strlcpy(obj_tmp, "NULL", sizeof obj_tmp);
136 		else
137 			i2t_ASN1_OBJECT(obj_tmp, sizeof obj_tmp, pbe_obj);
138 		ERR_asprintf_error_data("TYPE=%s", obj_tmp);
139 		return 0;
140 	}
141 
142 	if (!pass)
143 		passlen = 0;
144 	else if (passlen == -1)
145 		passlen = strlen(pass);
146 
147 	if (cipher_nid == -1)
148 		cipher = NULL;
149 	else {
150 		cipher = EVP_get_cipherbynid(cipher_nid);
151 		if (!cipher) {
152 			EVPerror(EVP_R_UNKNOWN_CIPHER);
153 			return 0;
154 		}
155 	}
156 
157 	if (md_nid == -1)
158 		md = NULL;
159 	else {
160 		md = EVP_get_digestbynid(md_nid);
161 		if (!md) {
162 			EVPerror(EVP_R_UNKNOWN_DIGEST);
163 			return 0;
164 		}
165 	}
166 
167 	if (!keygen(ctx, pass, passlen, param, cipher, md, en_de)) {
168 		EVPerror(EVP_R_KEYGEN_FAILURE);
169 		return 0;
170 	}
171 	return 1;
172 }
173 
174 static int pbe2_cmp_BSEARCH_CMP_FN(const void *, const void *);
175 static int pbe2_cmp(EVP_PBE_CTL const *, EVP_PBE_CTL const *);
176 static EVP_PBE_CTL *OBJ_bsearch_pbe2(EVP_PBE_CTL *key, EVP_PBE_CTL const *base, int num);
177 
178 static int
pbe2_cmp(const EVP_PBE_CTL * pbe1,const EVP_PBE_CTL * pbe2)179 pbe2_cmp(const EVP_PBE_CTL *pbe1, const EVP_PBE_CTL *pbe2)
180 {
181 	int ret = pbe1->pbe_type - pbe2->pbe_type;
182 
183 	if (ret)
184 		return ret;
185 	else
186 		return pbe1->pbe_nid - pbe2->pbe_nid;
187 }
188 
189 
190 static int
pbe2_cmp_BSEARCH_CMP_FN(const void * a_,const void * b_)191 pbe2_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
192 {
193 	EVP_PBE_CTL const *a = a_;
194 	EVP_PBE_CTL const *b = b_;
195 	return pbe2_cmp(a, b);
196 }
197 
198 static EVP_PBE_CTL *
OBJ_bsearch_pbe2(EVP_PBE_CTL * key,EVP_PBE_CTL const * base,int num)199 OBJ_bsearch_pbe2(EVP_PBE_CTL *key, EVP_PBE_CTL const *base, int num)
200 {
201 	return (EVP_PBE_CTL *)OBJ_bsearch_(key, base, num, sizeof(EVP_PBE_CTL),
202 	    pbe2_cmp_BSEARCH_CMP_FN);
203 }
204 
205 static int
pbe_cmp(const EVP_PBE_CTL * const * a,const EVP_PBE_CTL * const * b)206 pbe_cmp(const EVP_PBE_CTL * const *a, const EVP_PBE_CTL * const *b)
207 {
208 	int ret = (*a)->pbe_type - (*b)->pbe_type;
209 
210 	if (ret)
211 		return ret;
212 	else
213 		return (*a)->pbe_nid - (*b)->pbe_nid;
214 }
215 
216 /* Add a PBE algorithm */
217 
218 int
EVP_PBE_alg_add_type(int pbe_type,int pbe_nid,int cipher_nid,int md_nid,EVP_PBE_KEYGEN * keygen)219 EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid, int md_nid,
220     EVP_PBE_KEYGEN *keygen)
221 {
222 	EVP_PBE_CTL *pbe_tmp;
223 
224 	if (pbe_algs == NULL) {
225 		pbe_algs = sk_EVP_PBE_CTL_new(pbe_cmp);
226 		if (pbe_algs == NULL) {
227 			EVPerror(ERR_R_MALLOC_FAILURE);
228 			return 0;
229 		}
230 	}
231 	pbe_tmp = malloc(sizeof(EVP_PBE_CTL));
232 	if (pbe_tmp == NULL) {
233 		EVPerror(ERR_R_MALLOC_FAILURE);
234 		return 0;
235 	}
236 	pbe_tmp->pbe_type = pbe_type;
237 	pbe_tmp->pbe_nid = pbe_nid;
238 	pbe_tmp->cipher_nid = cipher_nid;
239 	pbe_tmp->md_nid = md_nid;
240 	pbe_tmp->keygen = keygen;
241 
242 	if (sk_EVP_PBE_CTL_push(pbe_algs, pbe_tmp) == 0) {
243 		free(pbe_tmp);
244 		EVPerror(ERR_R_MALLOC_FAILURE);
245 		return 0;
246 	}
247 	return 1;
248 }
249 
250 int
EVP_PBE_alg_add(int nid,const EVP_CIPHER * cipher,const EVP_MD * md,EVP_PBE_KEYGEN * keygen)251 EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,
252     EVP_PBE_KEYGEN *keygen)
253 {
254 	int cipher_nid, md_nid;
255 
256 	if (cipher)
257 		cipher_nid = EVP_CIPHER_nid(cipher);
258 	else
259 		cipher_nid = -1;
260 	if (md)
261 		md_nid = EVP_MD_type(md);
262 	else
263 		md_nid = -1;
264 
265 	return EVP_PBE_alg_add_type(EVP_PBE_TYPE_OUTER, nid,
266 	    cipher_nid, md_nid, keygen);
267 }
268 
269 int
EVP_PBE_find(int type,int pbe_nid,int * pcnid,int * pmnid,EVP_PBE_KEYGEN ** pkeygen)270 EVP_PBE_find(int type, int pbe_nid,
271     int *pcnid, int *pmnid, EVP_PBE_KEYGEN **pkeygen)
272 {
273 	EVP_PBE_CTL *pbetmp = NULL, pbelu;
274 	int i;
275 	if (pbe_nid == NID_undef)
276 		return 0;
277 
278 	pbelu.pbe_type = type;
279 	pbelu.pbe_nid = pbe_nid;
280 
281 	if (pbe_algs) {
282 		i = sk_EVP_PBE_CTL_find(pbe_algs, &pbelu);
283 		if (i != -1)
284 			pbetmp = sk_EVP_PBE_CTL_value (pbe_algs, i);
285 	}
286 	if (pbetmp == NULL) {
287 		pbetmp = OBJ_bsearch_pbe2(&pbelu, builtin_pbe,
288 		    sizeof(builtin_pbe)/sizeof(EVP_PBE_CTL));
289 	}
290 	if (pbetmp == NULL)
291 		return 0;
292 	if (pcnid)
293 		*pcnid = pbetmp->cipher_nid;
294 	if (pmnid)
295 		*pmnid = pbetmp->md_nid;
296 	if (pkeygen)
297 		*pkeygen = pbetmp->keygen;
298 	return 1;
299 }
300 
301 static void
free_evp_pbe_ctl(EVP_PBE_CTL * pbe)302 free_evp_pbe_ctl(EVP_PBE_CTL *pbe)
303 {
304 	free(pbe);
305 }
306 
307 void
EVP_PBE_cleanup(void)308 EVP_PBE_cleanup(void)
309 {
310 	sk_EVP_PBE_CTL_pop_free(pbe_algs, free_evp_pbe_ctl);
311 	pbe_algs = NULL;
312 }
313