Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/evp/p5_crpt2.c

raw
1 /*
2 * Copyright 1999-2025 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 <stdlib.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/x509.h>
14 #include <openssl/evp.h>
15 #include <openssl/kdf.h>
16 #include <openssl/hmac.h>
17 #include <openssl/trace.h>
18 #include <openssl/core_names.h>
19 #include "crypto/evp.h"
20 #include "evp_local.h"
21
22 int ossl_pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen,
23 const unsigned char *salt, int saltlen, int iter,
24 const EVP_MD *digest, int keylen, unsigned char *out,
25 OSSL_LIB_CTX *libctx, const char *propq)
26 {
27 const char *empty = "";
28 int rv = 1, mode = 1;
29 EVP_KDF *kdf;
30 EVP_KDF_CTX *kctx;
31 const char *mdname = EVP_MD_get0_name(digest);
32 OSSL_PARAM params[6], *p = params;
33
34 /* Keep documented behaviour. */
35 if (pass == NULL) {
36 pass = empty;
37 passlen = 0;
38 } else if (passlen == -1) {
39 passlen = (int)strlen(pass);
40 }
41 if (salt == NULL && saltlen == 0)
42 salt = (unsigned char *)empty;
43
44 kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF2, propq);
45 if (kdf == NULL)
46 return 0;
47 kctx = EVP_KDF_CTX_new(kdf);
48 EVP_KDF_free(kdf);
49 if (kctx == NULL)
50 return 0;
51 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
52 (char *)pass, (size_t)passlen);
53 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
54 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
55 (unsigned char *)salt, saltlen);
56 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
57 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
58 (char *)mdname, 0);
59 *p = OSSL_PARAM_construct_end();
60 if (EVP_KDF_derive(kctx, out, keylen, params) != 1)
61 rv = 0;
62
63 EVP_KDF_CTX_free(kctx);
64
65 OSSL_TRACE_BEGIN(PKCS5V2)
66 {
67 BIO_printf(trc_out, "Password:\n");
68 BIO_hex_string(trc_out,
69 0, passlen, pass, passlen);
70 BIO_printf(trc_out, "\n");
71 BIO_printf(trc_out, "Salt:\n");
72 BIO_hex_string(trc_out,
73 0, saltlen, salt, saltlen);
74 BIO_printf(trc_out, "\n");
75 BIO_printf(trc_out, "Iteration count %d\n", iter);
76 BIO_printf(trc_out, "Key:\n");
77 BIO_hex_string(trc_out,
78 0, keylen, out, keylen);
79 BIO_printf(trc_out, "\n");
80 }
81 OSSL_TRACE_END(PKCS5V2);
82 return rv;
83 }
84
85 int PKCS5_PBKDF2_HMAC(const char *pass, int passlen, const unsigned char *salt,
86 int saltlen, int iter, const EVP_MD *digest, int keylen,
87 unsigned char *out)
88 {
89 return ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, digest,
90 keylen, out, NULL, NULL);
91 }
92
93 int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
94 const unsigned char *salt, int saltlen, int iter,
95 int keylen, unsigned char *out)
96 {
97 EVP_MD *digest;
98 int r = 0;
99
100 if ((digest = EVP_MD_fetch(NULL, SN_sha1, NULL)) != NULL)
101 r = ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter,
102 digest, keylen, out, NULL, NULL);
103 EVP_MD_free(digest);
104 return r;
105 }
106
107 /*
108 * Now the key derivation function itself. This is a bit evil because it has
109 * to check the ASN1 parameters are valid: and there are quite a few of
110 * them...
111 */
112
113 int PKCS5_v2_PBE_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
114 ASN1_TYPE *param, const EVP_CIPHER *c,
115 const EVP_MD *md, int en_de,
116 OSSL_LIB_CTX *libctx, const char *propq)
117 {
118 PBE2PARAM *pbe2 = NULL;
119 char ciph_name[80];
120 const EVP_CIPHER *cipher = NULL;
121 EVP_CIPHER *cipher_fetch = NULL;
122 EVP_PBE_KEYGEN_EX *kdf;
123
124 int rv = 0;
125
126 pbe2 = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBE2PARAM), param);
127 if (pbe2 == NULL) {
128 ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
129 goto err;
130 }
131
132 /* See if we recognise the key derivation function */
133 if (!EVP_PBE_find_ex(EVP_PBE_TYPE_KDF, OBJ_obj2nid(pbe2->keyfunc->algorithm),
134 NULL, NULL, NULL, &kdf)) {
135 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
136 goto err;
137 }
138
139 /*
140 * lets see if we recognise the encryption algorithm.
141 */
142 if (OBJ_obj2txt(ciph_name, sizeof(ciph_name), pbe2->encryption->algorithm, 0) <= 0) {
143 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
144 goto err;
145 }
146
147 (void)ERR_set_mark();
148 cipher = cipher_fetch = EVP_CIPHER_fetch(libctx, ciph_name, propq);
149 /* Fallback to legacy method */
150 if (cipher == NULL)
151 cipher = EVP_get_cipherbyname(ciph_name);
152
153 if (cipher == NULL) {
154 (void)ERR_clear_last_mark();
155 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
156 goto err;
157 }
158 (void)ERR_pop_to_mark();
159
160 /* Fixup cipher based on AlgorithmIdentifier */
161 if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de))
162 goto err;
163 if (EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) <= 0) {
164 ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
165 goto err;
166 }
167 rv = kdf(ctx, pass, passlen, pbe2->keyfunc->parameter, NULL, NULL, en_de, libctx, propq);
168 err:
169 EVP_CIPHER_free(cipher_fetch);
170 PBE2PARAM_free(pbe2);
171 return rv;
172 }
173
174 int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
175 ASN1_TYPE *param, const EVP_CIPHER *c,
176 const EVP_MD *md, int en_de)
177 {
178 return PKCS5_v2_PBE_keyivgen_ex(ctx, pass, passlen, param, c, md, en_de, NULL, NULL);
179 }
180
181 int PKCS5_v2_PBKDF2_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass,
182 int passlen, ASN1_TYPE *param,
183 const EVP_CIPHER *c, const EVP_MD *md, int en_de,
184 OSSL_LIB_CTX *libctx, const char *propq)
185 {
186 unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
187 int saltlen, iter, t;
188 int rv = 0;
189 unsigned int keylen = 0;
190 int prf_nid, hmac_md_nid;
191 PBKDF2PARAM *kdf = NULL;
192 const EVP_MD *prfmd = NULL;
193 EVP_MD *prfmd_fetch = NULL;
194
195 if (EVP_CIPHER_CTX_get0_cipher(ctx) == NULL) {
196 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
197 goto err;
198 }
199 keylen = EVP_CIPHER_CTX_get_key_length(ctx);
200 OPENSSL_assert(keylen <= sizeof(key));
201
202 /* Decode parameter */
203
204 kdf = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), param);
205
206 if (kdf == NULL) {
207 ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
208 goto err;
209 }
210
211 t = EVP_CIPHER_CTX_get_key_length(ctx);
212 if (t < 0) {
213 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
214 goto err;
215 }
216 keylen = t;
217
218 /* Now check the parameters of the kdf */
219
220 if (kdf->keylength && (ASN1_INTEGER_get(kdf->keylength) != (int)keylen)) {
221 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEYLENGTH);
222 goto err;
223 }
224
225 if (kdf->prf)
226 prf_nid = OBJ_obj2nid(kdf->prf->algorithm);
227 else
228 prf_nid = NID_hmacWithSHA1;
229
230 if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, prf_nid, NULL, &hmac_md_nid, 0)) {
231 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRF);
232 goto err;
233 }
234
235 (void)ERR_set_mark();
236 prfmd = prfmd_fetch = EVP_MD_fetch(libctx, OBJ_nid2sn(hmac_md_nid), propq);
237 if (prfmd == NULL)
238 prfmd = EVP_get_digestbynid(hmac_md_nid);
239 if (prfmd == NULL) {
240 (void)ERR_clear_last_mark();
241 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRF);
242 goto err;
243 }
244 (void)ERR_pop_to_mark();
245
246 if (kdf->salt->type != V_ASN1_OCTET_STRING) {
247 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_SALT_TYPE);
248 goto err;
249 }
250
251 /* it seems that its all OK */
252 salt = kdf->salt->value.octet_string->data;
253 saltlen = kdf->salt->value.octet_string->length;
254 iter = ASN1_INTEGER_get(kdf->iter);
255 if (!ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, prfmd,
256 keylen, key, libctx, propq))
257 goto err;
258 rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
259 err:
260 OPENSSL_cleanse(key, keylen);
261 PBKDF2PARAM_free(kdf);
262 EVP_MD_free(prfmd_fetch);
263 return rv;
264 }
265
266 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
267 int passlen, ASN1_TYPE *param,
268 const EVP_CIPHER *c, const EVP_MD *md, int en_de)
269 {
270 return PKCS5_v2_PBKDF2_keyivgen_ex(ctx, pass, passlen, param, c, md, en_de,
271 NULL, NULL);
272 }