xref: /freebsd/crypto/openssl/crypto/evp/evp_pbe.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1 /*
2  * Copyright 1999-2023 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/evp.h>
13 #include <openssl/core.h>
14 #include <openssl/core_names.h>
15 #include <openssl/pkcs12.h>
16 #include <openssl/x509.h>
17 #include "crypto/evp.h"
18 #include "evp_local.h"
19 
20 /* Password based encryption (PBE) functions */
21 
22 /* Setup a cipher context from a PBE algorithm */
23 
24 struct evp_pbe_st {
25     int pbe_type;
26     int pbe_nid;
27     int cipher_nid;
28     int md_nid;
29     EVP_PBE_KEYGEN *keygen;
30     EVP_PBE_KEYGEN_EX *keygen_ex;
31 };
32 
STACK_OF(EVP_PBE_CTL)33 static STACK_OF(EVP_PBE_CTL) *pbe_algs;
34 
35 static const EVP_PBE_CTL builtin_pbe[] = {
36     {EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndDES_CBC,
37      NID_des_cbc, NID_md2, PKCS5_PBE_keyivgen, PKCS5_PBE_keyivgen_ex},
38     {EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC,
39      NID_des_cbc, NID_md5, PKCS5_PBE_keyivgen, PKCS5_PBE_keyivgen_ex},
40     {EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndRC2_CBC,
41      NID_rc2_64_cbc, NID_sha1, PKCS5_PBE_keyivgen, PKCS5_PBE_keyivgen_ex},
42 
43     {EVP_PBE_TYPE_OUTER, NID_id_pbkdf2, -1, -1, PKCS5_v2_PBKDF2_keyivgen},
44 
45     {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC4,
46      NID_rc4, NID_sha1, PKCS12_PBE_keyivgen, &PKCS12_PBE_keyivgen_ex},
47     {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC4,
48      NID_rc4_40, NID_sha1, PKCS12_PBE_keyivgen, &PKCS12_PBE_keyivgen_ex},
49     {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
50      NID_des_ede3_cbc, NID_sha1, PKCS12_PBE_keyivgen, &PKCS12_PBE_keyivgen_ex},
51     {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And2_Key_TripleDES_CBC,
52      NID_des_ede_cbc, NID_sha1, PKCS12_PBE_keyivgen, &PKCS12_PBE_keyivgen_ex},
53     {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC2_CBC,
54      NID_rc2_cbc, NID_sha1, PKCS12_PBE_keyivgen, &PKCS12_PBE_keyivgen_ex},
55     {EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC2_CBC,
56      NID_rc2_40_cbc, NID_sha1, PKCS12_PBE_keyivgen, &PKCS12_PBE_keyivgen_ex},
57 
58     {EVP_PBE_TYPE_OUTER, NID_pbes2, -1, -1, PKCS5_v2_PBE_keyivgen, &PKCS5_v2_PBE_keyivgen_ex},
59 
60     {EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndRC2_CBC,
61      NID_rc2_64_cbc, NID_md2, PKCS5_PBE_keyivgen, PKCS5_PBE_keyivgen_ex},
62     {EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndRC2_CBC,
63      NID_rc2_64_cbc, NID_md5, PKCS5_PBE_keyivgen, PKCS5_PBE_keyivgen_ex},
64     {EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndDES_CBC,
65      NID_des_cbc, NID_sha1, PKCS5_PBE_keyivgen, PKCS5_PBE_keyivgen_ex},
66 
67     {EVP_PBE_TYPE_PRF, NID_hmacWithSHA1, -1, NID_sha1, 0},
68     {EVP_PBE_TYPE_PRF, NID_hmac_md5, -1, NID_md5, 0},
69     {EVP_PBE_TYPE_PRF, NID_hmac_sha1, -1, NID_sha1, 0},
70     {EVP_PBE_TYPE_PRF, NID_hmacWithMD5, -1, NID_md5, 0},
71     {EVP_PBE_TYPE_PRF, NID_hmacWithSHA224, -1, NID_sha224, 0},
72     {EVP_PBE_TYPE_PRF, NID_hmacWithSHA256, -1, NID_sha256, 0},
73     {EVP_PBE_TYPE_PRF, NID_hmacWithSHA384, -1, NID_sha384, 0},
74     {EVP_PBE_TYPE_PRF, NID_hmacWithSHA512, -1, NID_sha512, 0},
75     {EVP_PBE_TYPE_PRF, NID_id_HMACGostR3411_94, -1, NID_id_GostR3411_94, 0},
76     {EVP_PBE_TYPE_PRF, NID_id_tc26_hmac_gost_3411_2012_256, -1,
77      NID_id_GostR3411_2012_256, 0},
78     {EVP_PBE_TYPE_PRF, NID_id_tc26_hmac_gost_3411_2012_512, -1,
79      NID_id_GostR3411_2012_512, 0},
80     {EVP_PBE_TYPE_PRF, NID_hmacWithSHA512_224, -1, NID_sha512_224, 0},
81     {EVP_PBE_TYPE_PRF, NID_hmacWithSHA512_256, -1, NID_sha512_256, 0},
82     {EVP_PBE_TYPE_KDF, NID_id_pbkdf2, -1, -1, PKCS5_v2_PBKDF2_keyivgen, &PKCS5_v2_PBKDF2_keyivgen_ex},
83 #ifndef OPENSSL_NO_SCRYPT
84     {EVP_PBE_TYPE_KDF, NID_id_scrypt, -1, -1, PKCS5_v2_scrypt_keyivgen, &PKCS5_v2_scrypt_keyivgen_ex}
85 #endif
86 };
87 
88 
EVP_PBE_CipherInit_ex(ASN1_OBJECT * pbe_obj,const char * pass,int passlen,ASN1_TYPE * param,EVP_CIPHER_CTX * ctx,int en_de,OSSL_LIB_CTX * libctx,const char * propq)89 int EVP_PBE_CipherInit_ex(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
90                           ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de,
91                           OSSL_LIB_CTX *libctx, const char *propq)
92 {
93     const EVP_CIPHER *cipher = NULL;
94     EVP_CIPHER *cipher_fetch = NULL;
95     const EVP_MD *md = NULL;
96     EVP_MD *md_fetch = NULL;
97     int ret = 0, cipher_nid, md_nid;
98     EVP_PBE_KEYGEN_EX *keygen_ex;
99     EVP_PBE_KEYGEN *keygen;
100 
101     if (!EVP_PBE_find_ex(EVP_PBE_TYPE_OUTER, OBJ_obj2nid(pbe_obj),
102                          &cipher_nid, &md_nid, &keygen, &keygen_ex)) {
103         char obj_tmp[80];
104 
105         if (pbe_obj == NULL)
106             OPENSSL_strlcpy(obj_tmp, "NULL", sizeof(obj_tmp));
107         else
108             i2t_ASN1_OBJECT(obj_tmp, sizeof(obj_tmp), pbe_obj);
109         ERR_raise_data(ERR_LIB_EVP, EVP_R_UNKNOWN_PBE_ALGORITHM,
110                        "TYPE=%s", obj_tmp);
111         goto err;
112     }
113 
114     if (pass == NULL)
115         passlen = 0;
116     else if (passlen == -1)
117         passlen = strlen(pass);
118 
119     if (cipher_nid != -1) {
120         (void)ERR_set_mark();
121         cipher = cipher_fetch = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(cipher_nid), propq);
122         /* Fallback to legacy method */
123         if (cipher == NULL)
124             cipher = EVP_get_cipherbynid(cipher_nid);
125         if (cipher == NULL) {
126             (void)ERR_clear_last_mark();
127             ERR_raise_data(ERR_LIB_EVP, EVP_R_UNKNOWN_CIPHER,
128                            OBJ_nid2sn(cipher_nid));
129             goto err;
130         }
131         (void)ERR_pop_to_mark();
132     }
133 
134     if (md_nid != -1) {
135         (void)ERR_set_mark();
136         md = md_fetch = EVP_MD_fetch(libctx, OBJ_nid2sn(md_nid), propq);
137         /* Fallback to legacy method */
138         if (md == NULL)
139             md = EVP_get_digestbynid(md_nid);
140 
141         if (md == NULL) {
142             (void)ERR_clear_last_mark();
143             ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_DIGEST);
144             goto err;
145         }
146         (void)ERR_pop_to_mark();
147     }
148 
149     /* Try extended keygen with libctx/propq first, fall back to legacy keygen */
150     if (keygen_ex != NULL)
151         ret = keygen_ex(ctx, pass, passlen, param, cipher, md, en_de, libctx, propq);
152     else
153         ret = keygen(ctx, pass, passlen, param, cipher, md, en_de);
154 
155 err:
156     EVP_CIPHER_free(cipher_fetch);
157     EVP_MD_free(md_fetch);
158 
159     return ret;
160 }
161 
EVP_PBE_CipherInit(ASN1_OBJECT * pbe_obj,const char * pass,int passlen,ASN1_TYPE * param,EVP_CIPHER_CTX * ctx,int en_de)162 int EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
163                        ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de)
164 {
165     return EVP_PBE_CipherInit_ex(pbe_obj, pass, passlen, param, ctx, en_de, NULL, NULL);
166 }
167 
168 DECLARE_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2);
169 
pbe2_cmp(const EVP_PBE_CTL * pbe1,const EVP_PBE_CTL * pbe2)170 static int pbe2_cmp(const EVP_PBE_CTL *pbe1, const EVP_PBE_CTL *pbe2)
171 {
172     int ret = pbe1->pbe_type - pbe2->pbe_type;
173     if (ret)
174         return ret;
175     else
176         return pbe1->pbe_nid - pbe2->pbe_nid;
177 }
178 
179 IMPLEMENT_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2);
180 
pbe_cmp(const EVP_PBE_CTL * const * a,const EVP_PBE_CTL * const * b)181 static int pbe_cmp(const EVP_PBE_CTL *const *a, const EVP_PBE_CTL *const *b)
182 {
183     int ret = (*a)->pbe_type - (*b)->pbe_type;
184     if (ret)
185         return ret;
186     else
187         return (*a)->pbe_nid - (*b)->pbe_nid;
188 }
189 
190 /* Add a PBE algorithm */
191 
EVP_PBE_alg_add_type(int pbe_type,int pbe_nid,int cipher_nid,int md_nid,EVP_PBE_KEYGEN * keygen)192 int EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid,
193                          int md_nid, EVP_PBE_KEYGEN *keygen)
194 {
195     EVP_PBE_CTL *pbe_tmp;
196 
197     if (pbe_algs == NULL) {
198         pbe_algs = sk_EVP_PBE_CTL_new(pbe_cmp);
199         if (pbe_algs == NULL)
200             goto err;
201     }
202 
203     if ((pbe_tmp = OPENSSL_zalloc(sizeof(*pbe_tmp))) == NULL)
204         goto err;
205 
206     pbe_tmp->pbe_type = pbe_type;
207     pbe_tmp->pbe_nid = pbe_nid;
208     pbe_tmp->cipher_nid = cipher_nid;
209     pbe_tmp->md_nid = md_nid;
210     pbe_tmp->keygen = keygen;
211 
212     if (!sk_EVP_PBE_CTL_push(pbe_algs, pbe_tmp)) {
213         OPENSSL_free(pbe_tmp);
214         goto err;
215     }
216     return 1;
217 
218  err:
219     ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
220     return 0;
221 }
222 
EVP_PBE_alg_add(int nid,const EVP_CIPHER * cipher,const EVP_MD * md,EVP_PBE_KEYGEN * keygen)223 int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,
224                     EVP_PBE_KEYGEN *keygen)
225 {
226     int cipher_nid, md_nid;
227 
228     if (cipher)
229         cipher_nid = EVP_CIPHER_get_nid(cipher);
230     else
231         cipher_nid = -1;
232     if (md)
233         md_nid = EVP_MD_get_type(md);
234     else
235         md_nid = -1;
236 
237     return EVP_PBE_alg_add_type(EVP_PBE_TYPE_OUTER, nid,
238                                 cipher_nid, md_nid, keygen);
239 }
240 
EVP_PBE_find_ex(int type,int pbe_nid,int * pcnid,int * pmnid,EVP_PBE_KEYGEN ** pkeygen,EVP_PBE_KEYGEN_EX ** pkeygen_ex)241 int EVP_PBE_find_ex(int type, int pbe_nid, int *pcnid, int *pmnid,
242                     EVP_PBE_KEYGEN **pkeygen, EVP_PBE_KEYGEN_EX **pkeygen_ex)
243 {
244     EVP_PBE_CTL *pbetmp = NULL, pbelu;
245     int i;
246     if (pbe_nid == NID_undef)
247         return 0;
248 
249     pbelu.pbe_type = type;
250     pbelu.pbe_nid = pbe_nid;
251 
252     if (pbe_algs != NULL) {
253         i = sk_EVP_PBE_CTL_find(pbe_algs, &pbelu);
254         pbetmp = sk_EVP_PBE_CTL_value(pbe_algs, i);
255     }
256     if (pbetmp == NULL) {
257         pbetmp = OBJ_bsearch_pbe2(&pbelu, builtin_pbe, OSSL_NELEM(builtin_pbe));
258     }
259     if (pbetmp == NULL)
260         return 0;
261     if (pcnid != NULL)
262         *pcnid = pbetmp->cipher_nid;
263     if (pmnid != NULL)
264         *pmnid = pbetmp->md_nid;
265     if (pkeygen != NULL)
266         *pkeygen = pbetmp->keygen;
267     if (pkeygen_ex != NULL)
268         *pkeygen_ex = pbetmp->keygen_ex;
269     return 1;
270 }
271 
EVP_PBE_find(int type,int pbe_nid,int * pcnid,int * pmnid,EVP_PBE_KEYGEN ** pkeygen)272 int EVP_PBE_find(int type, int pbe_nid,
273                  int *pcnid, int *pmnid, EVP_PBE_KEYGEN **pkeygen)
274 {
275     return EVP_PBE_find_ex(type, pbe_nid, pcnid, pmnid, pkeygen, NULL);
276 }
277 
free_evp_pbe_ctl(EVP_PBE_CTL * pbe)278 static void free_evp_pbe_ctl(EVP_PBE_CTL *pbe)
279 {
280     OPENSSL_free(pbe);
281 }
282 
EVP_PBE_cleanup(void)283 void EVP_PBE_cleanup(void)
284 {
285     sk_EVP_PBE_CTL_pop_free(pbe_algs, free_evp_pbe_ctl);
286     pbe_algs = NULL;
287 }
288 
EVP_PBE_get(int * ptype,int * ppbe_nid,size_t num)289 int EVP_PBE_get(int *ptype, int *ppbe_nid, size_t num)
290 {
291     const EVP_PBE_CTL *tpbe;
292 
293     if (num >= OSSL_NELEM(builtin_pbe))
294         return 0;
295 
296     tpbe = builtin_pbe + num;
297     if (ptype)
298         *ptype = tpbe->pbe_type;
299     if (ppbe_nid)
300         *ppbe_nid = tpbe->pbe_nid;
301     return 1;
302 }
303