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