/src/openssl/crypto/asn1/p5_pbev2.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1999-2024 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 "crypto/asn1.h" |
13 | | #include "crypto/evp.h" |
14 | | #include <openssl/asn1t.h> |
15 | | #include <openssl/core.h> |
16 | | #include <openssl/core_names.h> |
17 | | #include <openssl/x509.h> |
18 | | #include <openssl/rand.h> |
19 | | |
20 | | /* PKCS#5 v2.0 password based encryption structures */ |
21 | | |
22 | | ASN1_SEQUENCE(PBE2PARAM) = { |
23 | | ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR), |
24 | | ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR) |
25 | 0 | } ASN1_SEQUENCE_END(PBE2PARAM) |
26 | 0 |
|
27 | 0 | IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM) |
28 | 0 |
|
29 | 0 | ASN1_SEQUENCE(PBKDF2PARAM) = { |
30 | 0 | ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY), |
31 | 0 | ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER), |
32 | 0 | ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER), |
33 | 0 | ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR) |
34 | 0 | } ASN1_SEQUENCE_END(PBKDF2PARAM) |
35 | 0 |
|
36 | 0 | IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM) |
37 | 0 |
|
38 | 0 | ASN1_SEQUENCE(PBMAC1PARAM) = { |
39 | 0 | ASN1_SIMPLE(PBMAC1PARAM, keyDerivationFunc, X509_ALGOR), |
40 | 0 | ASN1_SIMPLE(PBMAC1PARAM, messageAuthScheme, X509_ALGOR) |
41 | 0 | } ASN1_SEQUENCE_END(PBMAC1PARAM) |
42 | 0 |
|
43 | 0 | IMPLEMENT_ASN1_FUNCTIONS(PBMAC1PARAM) |
44 | 0 |
|
45 | 0 | /* |
46 | 0 | * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm: yes I know |
47 | 0 | * this is horrible! Extended version to allow application supplied PRF NID |
48 | 0 | * and IV. |
49 | 0 | */ |
50 | 0 |
|
51 | 0 | X509_ALGOR *PKCS5_pbe2_set_iv_ex(const EVP_CIPHER *cipher, int iter, |
52 | 0 | unsigned char *salt, int saltlen, |
53 | 0 | unsigned char *aiv, int prf_nid, |
54 | 0 | OSSL_LIB_CTX *libctx) |
55 | 0 | { |
56 | 0 | X509_ALGOR *scheme = NULL, *ret = NULL; |
57 | 0 | int alg_nid, keylen, ivlen; |
58 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
59 | 0 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
60 | 0 | PBE2PARAM *pbe2 = NULL; |
61 | |
|
62 | 0 | alg_nid = EVP_CIPHER_get_type(cipher); |
63 | 0 | if (alg_nid == NID_undef) { |
64 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); |
65 | 0 | goto err; |
66 | 0 | } |
67 | | |
68 | 0 | if ((pbe2 = PBE2PARAM_new()) == NULL) { |
69 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
70 | 0 | goto err; |
71 | 0 | } |
72 | | |
73 | | /* Setup the AlgorithmIdentifier for the encryption scheme */ |
74 | 0 | scheme = pbe2->encryption; |
75 | 0 | scheme->algorithm = OBJ_nid2obj(alg_nid); |
76 | 0 | if ((scheme->parameter = ASN1_TYPE_new()) == NULL) { |
77 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
78 | 0 | goto err; |
79 | 0 | } |
80 | | |
81 | | /* Create random IV */ |
82 | 0 | ivlen = EVP_CIPHER_get_iv_length(cipher); |
83 | 0 | if (ivlen > 0) { |
84 | 0 | if (aiv) |
85 | 0 | memcpy(iv, aiv, ivlen); |
86 | 0 | else if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0) |
87 | 0 | goto err; |
88 | 0 | } |
89 | | |
90 | 0 | ctx = EVP_CIPHER_CTX_new(); |
91 | 0 | if (ctx == NULL) { |
92 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
93 | 0 | goto err; |
94 | 0 | } |
95 | | |
96 | | /* Dummy cipherinit to just setup the IV, and PRF */ |
97 | 0 | if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0)) |
98 | 0 | goto err; |
99 | 0 | if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) <= 0) { |
100 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_SETTING_CIPHER_PARAMS); |
101 | 0 | goto err; |
102 | 0 | } |
103 | | /* |
104 | | * If prf NID unspecified see if cipher has a preference. An error is OK |
105 | | * here: just means use default PRF. |
106 | | */ |
107 | 0 | ERR_set_mark(); |
108 | 0 | if ((prf_nid == -1) && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0) { |
109 | 0 | prf_nid = NID_hmacWithSHA256; |
110 | 0 | } |
111 | 0 | ERR_pop_to_mark(); |
112 | 0 | EVP_CIPHER_CTX_free(ctx); |
113 | 0 | ctx = NULL; |
114 | | |
115 | | /* If its RC2 then we'd better setup the key length */ |
116 | |
|
117 | 0 | if (alg_nid == NID_rc2_cbc) |
118 | 0 | keylen = EVP_CIPHER_get_key_length(cipher); |
119 | 0 | else |
120 | 0 | keylen = -1; |
121 | | |
122 | | /* Setup keyfunc */ |
123 | |
|
124 | 0 | X509_ALGOR_free(pbe2->keyfunc); |
125 | |
|
126 | 0 | pbe2->keyfunc = PKCS5_pbkdf2_set_ex(iter, salt, saltlen, prf_nid, keylen, |
127 | 0 | libctx); |
128 | |
|
129 | 0 | if (pbe2->keyfunc == NULL) { |
130 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
131 | 0 | goto err; |
132 | 0 | } |
133 | | |
134 | | /* Now set up top level AlgorithmIdentifier */ |
135 | | |
136 | 0 | if ((ret = X509_ALGOR_new()) == NULL) { |
137 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_X509_LIB); |
138 | 0 | goto err; |
139 | 0 | } |
140 | | |
141 | 0 | ret->algorithm = OBJ_nid2obj(NID_pbes2); |
142 | | |
143 | | /* Encode PBE2PARAM into parameter */ |
144 | |
|
145 | 0 | if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2, |
146 | 0 | &ret->parameter)) { |
147 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
148 | 0 | goto err; |
149 | 0 | } |
150 | | |
151 | 0 | PBE2PARAM_free(pbe2); |
152 | 0 | pbe2 = NULL; |
153 | |
|
154 | 0 | return ret; |
155 | | |
156 | 0 | err: |
157 | 0 | EVP_CIPHER_CTX_free(ctx); |
158 | 0 | PBE2PARAM_free(pbe2); |
159 | | /* Note 'scheme' is freed as part of pbe2 */ |
160 | 0 | X509_ALGOR_free(ret); |
161 | |
|
162 | 0 | return NULL; |
163 | 0 | } |
164 | | |
165 | | X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter, |
166 | | unsigned char *salt, int saltlen, |
167 | | unsigned char *aiv, int prf_nid) |
168 | 0 | { |
169 | 0 | return PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, aiv, prf_nid, |
170 | 0 | NULL); |
171 | 0 | } |
172 | | |
173 | | X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, |
174 | | unsigned char *salt, int saltlen) |
175 | 0 | { |
176 | 0 | return PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, NULL, -1, |
177 | 0 | NULL); |
178 | 0 | } |
179 | | |
180 | | X509_ALGOR *PKCS5_pbkdf2_set_ex(int iter, unsigned char *salt, int saltlen, |
181 | | int prf_nid, int keylen, |
182 | | OSSL_LIB_CTX *libctx) |
183 | 0 | { |
184 | 0 | X509_ALGOR *keyfunc = NULL; |
185 | 0 | PBKDF2PARAM *kdf = NULL; |
186 | 0 | ASN1_OCTET_STRING *osalt = NULL; |
187 | |
|
188 | 0 | if ((kdf = PBKDF2PARAM_new()) == NULL) { |
189 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
190 | 0 | goto err; |
191 | 0 | } |
192 | 0 | if ((osalt = ASN1_OCTET_STRING_new()) == NULL) { |
193 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
194 | 0 | goto err; |
195 | 0 | } |
196 | | |
197 | 0 | kdf->salt->value.octet_string = osalt; |
198 | 0 | kdf->salt->type = V_ASN1_OCTET_STRING; |
199 | |
|
200 | 0 | if (saltlen < 0) { |
201 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_INVALID_ARGUMENT); |
202 | 0 | goto err; |
203 | 0 | } |
204 | 0 | if (saltlen == 0) |
205 | 0 | saltlen = PKCS5_DEFAULT_PBE2_SALT_LEN; |
206 | 0 | if ((osalt->data = OPENSSL_malloc(saltlen)) == NULL) |
207 | 0 | goto err; |
208 | | |
209 | 0 | osalt->length = saltlen; |
210 | |
|
211 | 0 | if (salt) { |
212 | 0 | memcpy(osalt->data, salt, saltlen); |
213 | 0 | } else if (RAND_bytes_ex(libctx, osalt->data, saltlen, 0) <= 0) { |
214 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_RAND_LIB); |
215 | 0 | goto err; |
216 | 0 | } |
217 | | |
218 | 0 | if (iter <= 0) |
219 | 0 | iter = PKCS5_DEFAULT_ITER; |
220 | |
|
221 | 0 | if (!ASN1_INTEGER_set(kdf->iter, iter)) { |
222 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
223 | 0 | goto err; |
224 | 0 | } |
225 | | |
226 | | /* If have a key len set it up */ |
227 | | |
228 | 0 | if (keylen > 0) { |
229 | 0 | if ((kdf->keylength = ASN1_INTEGER_new()) == NULL) { |
230 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
231 | 0 | goto err; |
232 | 0 | } |
233 | 0 | if (!ASN1_INTEGER_set(kdf->keylength, keylen)) { |
234 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
235 | 0 | goto err; |
236 | 0 | } |
237 | 0 | } |
238 | | |
239 | | /* prf can stay NULL if we are using hmacWithSHA1 */ |
240 | 0 | if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1) { |
241 | 0 | kdf->prf = ossl_X509_ALGOR_from_nid(prf_nid, V_ASN1_NULL, NULL); |
242 | 0 | if (kdf->prf == NULL) { |
243 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_X509_LIB); |
244 | 0 | goto err; |
245 | 0 | } |
246 | 0 | } |
247 | | |
248 | | /* Finally setup the keyfunc structure */ |
249 | | |
250 | 0 | keyfunc = X509_ALGOR_new(); |
251 | 0 | if (keyfunc == NULL) { |
252 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_X509_LIB); |
253 | 0 | goto err; |
254 | 0 | } |
255 | | |
256 | 0 | keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2); |
257 | | |
258 | | /* Encode PBKDF2PARAM into parameter of pbe2 */ |
259 | |
|
260 | 0 | if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), kdf, |
261 | 0 | &keyfunc->parameter)) { |
262 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
263 | 0 | goto err; |
264 | 0 | } |
265 | | |
266 | 0 | PBKDF2PARAM_free(kdf); |
267 | 0 | return keyfunc; |
268 | | |
269 | 0 | err: |
270 | 0 | PBKDF2PARAM_free(kdf); |
271 | 0 | X509_ALGOR_free(keyfunc); |
272 | 0 | return NULL; |
273 | 0 | } |
274 | | |
275 | | X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen, |
276 | | int prf_nid, int keylen) |
277 | 0 | { |
278 | | return PKCS5_pbkdf2_set_ex(iter, salt, saltlen, prf_nid, keylen, NULL); |
279 | 0 | } |