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