/src/openssl/crypto/pkcs12/p12_mutl.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1999-2026 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 | | /* |
11 | | * HMAC low level APIs are deprecated for public use, but still ok for internal |
12 | | * use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include <stdio.h> |
17 | | #include "internal/cryptlib.h" |
18 | | #include "crypto/evp.h" |
19 | | #include <openssl/crypto.h> |
20 | | #include <openssl/hmac.h> |
21 | | #include <openssl/rand.h> |
22 | | #include <openssl/pkcs12.h> |
23 | | #include "p12_local.h" |
24 | | |
25 | | #include <crypto/asn1.h> |
26 | | |
27 | | static int pkcs12_pbmac1_pbkdf2_key_gen(const char *pass, int passlen, |
28 | | unsigned char *salt, int saltlen, |
29 | | int id, int iter, int keylen, |
30 | | unsigned char *out, |
31 | | const EVP_MD *md_type, |
32 | | OSSL_LIB_CTX *libctx, const char *propq); |
33 | | |
34 | | int PKCS12_mac_present(const PKCS12 *p12) |
35 | 0 | { |
36 | 0 | return p12->mac ? 1 : 0; |
37 | 0 | } |
38 | | |
39 | | void PKCS12_get0_mac(const ASN1_OCTET_STRING **pmac, |
40 | | const X509_ALGOR **pmacalg, |
41 | | const ASN1_OCTET_STRING **psalt, |
42 | | const ASN1_INTEGER **piter, |
43 | | const PKCS12 *p12) |
44 | 0 | { |
45 | 0 | if (p12->mac) { |
46 | 0 | X509_SIG_get0(p12->mac->dinfo, pmacalg, pmac); |
47 | 0 | if (psalt) |
48 | 0 | *psalt = p12->mac->salt; |
49 | 0 | if (piter) |
50 | 0 | *piter = p12->mac->iter; |
51 | 0 | } else { |
52 | 0 | if (pmac) |
53 | 0 | *pmac = NULL; |
54 | 0 | if (pmacalg) |
55 | 0 | *pmacalg = NULL; |
56 | 0 | if (psalt) |
57 | 0 | *psalt = NULL; |
58 | 0 | if (piter) |
59 | 0 | *piter = NULL; |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | 0 | #define TK26_MAC_KEY_LEN 32 |
64 | | |
65 | | static int pkcs12_gen_gost_mac_key(const char *pass, int passlen, |
66 | | const unsigned char *salt, int saltlen, |
67 | | int iter, int keylen, unsigned char *key, |
68 | | const EVP_MD *digest) |
69 | 0 | { |
70 | 0 | unsigned char out[96]; |
71 | |
|
72 | 0 | if (keylen != TK26_MAC_KEY_LEN) { |
73 | 0 | return 0; |
74 | 0 | } |
75 | | |
76 | 0 | if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, |
77 | 0 | digest, sizeof(out), out)) { |
78 | 0 | return 0; |
79 | 0 | } |
80 | 0 | memcpy(key, out + sizeof(out) - TK26_MAC_KEY_LEN, TK26_MAC_KEY_LEN); |
81 | 0 | OPENSSL_cleanse(out, sizeof(out)); |
82 | 0 | return 1; |
83 | 0 | } |
84 | | |
85 | | PBKDF2PARAM *PBMAC1_get1_pbkdf2_param(const X509_ALGOR *macalg) |
86 | 0 | { |
87 | 0 | PBMAC1PARAM *param = NULL; |
88 | 0 | PBKDF2PARAM *pbkdf2_param = NULL; |
89 | 0 | const ASN1_OBJECT *kdf_oid; |
90 | |
|
91 | 0 | param = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBMAC1PARAM), macalg->parameter); |
92 | 0 | if (param == NULL) { |
93 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_INVALID_ARGUMENT); |
94 | 0 | return NULL; |
95 | 0 | } |
96 | | |
97 | 0 | X509_ALGOR_get0(&kdf_oid, NULL, NULL, param->keyDerivationFunc); |
98 | 0 | if (OBJ_obj2nid(kdf_oid) != NID_id_pbkdf2) { |
99 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_INVALID_ARGUMENT); |
100 | 0 | PBMAC1PARAM_free(param); |
101 | 0 | return NULL; |
102 | 0 | } |
103 | | |
104 | 0 | pbkdf2_param = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), |
105 | 0 | param->keyDerivationFunc->parameter); |
106 | 0 | PBMAC1PARAM_free(param); |
107 | |
|
108 | 0 | return pbkdf2_param; |
109 | 0 | } |
110 | | |
111 | | static int PBMAC1_PBKDF2_HMAC(OSSL_LIB_CTX *ctx, const char *propq, |
112 | | const char *pass, int passlen, |
113 | | const X509_ALGOR *macalg, unsigned char *key) |
114 | 0 | { |
115 | 0 | PBKDF2PARAM *pbkdf2_param = NULL; |
116 | 0 | const ASN1_OBJECT *kdf_hmac_oid; |
117 | 0 | int kdf_hmac_nid; |
118 | 0 | int ret = -1; |
119 | 0 | int keylen = 0; |
120 | 0 | EVP_MD *kdf_md = NULL; |
121 | 0 | const ASN1_OCTET_STRING *pbkdf2_salt = NULL; |
122 | |
|
123 | 0 | pbkdf2_param = PBMAC1_get1_pbkdf2_param(macalg); |
124 | 0 | if (pbkdf2_param == NULL) { |
125 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_UNSUPPORTED); |
126 | 0 | goto err; |
127 | 0 | } |
128 | | |
129 | 0 | if (pbkdf2_param->prf == NULL) { |
130 | 0 | kdf_hmac_nid = NID_hmacWithSHA1; |
131 | 0 | } else { |
132 | 0 | X509_ALGOR_get0(&kdf_hmac_oid, NULL, NULL, pbkdf2_param->prf); |
133 | 0 | kdf_hmac_nid = OBJ_obj2nid(kdf_hmac_oid); |
134 | 0 | } |
135 | |
|
136 | 0 | kdf_md = EVP_MD_fetch(ctx, OBJ_nid2sn(ossl_hmac2mdnid(kdf_hmac_nid)), propq); |
137 | 0 | if (kdf_md == NULL) { |
138 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_FETCH_FAILED); |
139 | 0 | goto err; |
140 | 0 | } |
141 | | |
142 | | /* Validate salt is an OCTET STRING choice */ |
143 | 0 | if (pbkdf2_param->salt == NULL |
144 | 0 | || pbkdf2_param->salt->type != V_ASN1_OCTET_STRING) { |
145 | 0 | ERR_raise_data(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR, "Invalid Salt"); |
146 | 0 | goto err; |
147 | 0 | } |
148 | 0 | pbkdf2_salt = pbkdf2_param->salt->value.octet_string; |
149 | | |
150 | | /* RFC 9579 specifies missing key length as invalid */ |
151 | 0 | if (pbkdf2_param->keylength != NULL) |
152 | 0 | keylen = ASN1_INTEGER_get(pbkdf2_param->keylength); |
153 | 0 | if (keylen <= 0 || keylen > EVP_MAX_MD_SIZE) { |
154 | 0 | ERR_raise_data(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR, |
155 | 0 | "Invalid Key length (%d is not in the range 1..64)", keylen); |
156 | 0 | goto err; |
157 | 0 | } |
158 | | |
159 | 0 | if (PKCS5_PBKDF2_HMAC(pass, passlen, pbkdf2_salt->data, pbkdf2_salt->length, |
160 | 0 | ASN1_INTEGER_get(pbkdf2_param->iter), kdf_md, keylen, key) |
161 | 0 | <= 0) { |
162 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR); |
163 | 0 | goto err; |
164 | 0 | } |
165 | 0 | ret = keylen; |
166 | |
|
167 | 0 | err: |
168 | 0 | EVP_MD_free(kdf_md); |
169 | 0 | PBKDF2PARAM_free(pbkdf2_param); |
170 | |
|
171 | 0 | return ret; |
172 | 0 | } |
173 | | |
174 | | /* Generate a MAC, also used for verification */ |
175 | | static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen, |
176 | | unsigned char *mac, unsigned int *maclen, |
177 | | int pbmac1_md_nid, int pbmac1_kdf_nid, |
178 | | int (*pkcs12_key_gen)(const char *pass, int passlen, |
179 | | unsigned char *salt, int slen, |
180 | | int id, int iter, int n, |
181 | | unsigned char *out, |
182 | | const EVP_MD *md_type, |
183 | | OSSL_LIB_CTX *libctx, |
184 | | const char *propq)) |
185 | 0 | { |
186 | 0 | int ret = 0; |
187 | 0 | EVP_MD *md; |
188 | 0 | unsigned char key[EVP_MAX_MD_SIZE], *salt; |
189 | 0 | int saltlen, iter; |
190 | 0 | char md_name[80]; |
191 | 0 | int keylen = 0; |
192 | 0 | int md_nid = NID_undef; |
193 | 0 | const X509_ALGOR *macalg; |
194 | 0 | const ASN1_OBJECT *macoid; |
195 | 0 | OSSL_LIB_CTX *libctx; |
196 | 0 | const char *propq; |
197 | 0 | size_t md_sz, outlen; |
198 | |
|
199 | 0 | if (!PKCS7_type_is_data(p12->authsafes)) { |
200 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA); |
201 | 0 | return 0; |
202 | 0 | } |
203 | | |
204 | 0 | if (p12->authsafes->d.data == NULL) { |
205 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR); |
206 | 0 | return 0; |
207 | 0 | } |
208 | | |
209 | 0 | libctx = p12->authsafes->ctx.libctx; |
210 | 0 | propq = p12->authsafes->ctx.propq; |
211 | 0 | salt = p12->mac->salt->data; |
212 | 0 | saltlen = p12->mac->salt->length; |
213 | 0 | if (p12->mac->iter == NULL) |
214 | 0 | iter = 1; |
215 | 0 | else |
216 | 0 | iter = ASN1_INTEGER_get(p12->mac->iter); |
217 | 0 | X509_SIG_get0(p12->mac->dinfo, &macalg, NULL); |
218 | 0 | X509_ALGOR_get0(&macoid, NULL, NULL, macalg); |
219 | 0 | if (OBJ_obj2nid(macoid) == NID_pbmac1) { |
220 | 0 | if (OBJ_obj2txt(md_name, sizeof(md_name), OBJ_nid2obj(pbmac1_md_nid), 0) < 0) |
221 | 0 | return 0; |
222 | 0 | } else { |
223 | 0 | if (OBJ_obj2txt(md_name, sizeof(md_name), macoid, 0) < 0) |
224 | 0 | return 0; |
225 | 0 | } |
226 | 0 | md = EVP_MD_fetch(libctx, md_name, propq); |
227 | |
|
228 | 0 | if (md == NULL) { |
229 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM); |
230 | 0 | return 0; |
231 | 0 | } |
232 | | |
233 | 0 | keylen = EVP_MD_get_size(md); |
234 | 0 | md_nid = EVP_MD_get_type(md); |
235 | 0 | if (keylen <= 0) |
236 | 0 | goto err; |
237 | 0 | md_sz = keylen; |
238 | | |
239 | | /* For PBMAC1 we use a special keygen callback if not provided (e.g. on verification) */ |
240 | 0 | if (pbmac1_md_nid != NID_undef && pkcs12_key_gen == NULL) { |
241 | 0 | keylen = PBMAC1_PBKDF2_HMAC(libctx, propq, pass, passlen, macalg, key); |
242 | 0 | if (keylen < 0) |
243 | 0 | goto err; |
244 | 0 | } else if ((md_nid == NID_id_GostR3411_94 |
245 | 0 | || md_nid == NID_id_GostR3411_2012_256 |
246 | 0 | || md_nid == NID_id_GostR3411_2012_512) |
247 | 0 | && ossl_safe_getenv("LEGACY_GOST_PKCS12") == NULL) { |
248 | 0 | keylen = TK26_MAC_KEY_LEN; |
249 | 0 | if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter, |
250 | 0 | keylen, key, md)) { |
251 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); |
252 | 0 | goto err; |
253 | 0 | } |
254 | 0 | } else { |
255 | 0 | EVP_MD *hmac_md = md; |
256 | 0 | int fetched = 0; |
257 | |
|
258 | 0 | if (pbmac1_kdf_nid != NID_undef) { |
259 | 0 | char hmac_md_name[128]; |
260 | |
|
261 | 0 | if (OBJ_obj2txt(hmac_md_name, sizeof(hmac_md_name), OBJ_nid2obj(pbmac1_kdf_nid), 0) < 0) |
262 | 0 | goto err; |
263 | 0 | hmac_md = EVP_MD_fetch(libctx, hmac_md_name, propq); |
264 | 0 | if (hmac_md == NULL) |
265 | 0 | goto err; |
266 | 0 | fetched = 1; |
267 | 0 | } |
268 | 0 | if (pkcs12_key_gen != NULL) { |
269 | 0 | int res = (*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID, |
270 | 0 | iter, keylen, key, hmac_md, libctx, propq); |
271 | |
|
272 | 0 | if (fetched) |
273 | 0 | EVP_MD_free(hmac_md); |
274 | 0 | if (res != 1) { |
275 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); |
276 | 0 | goto err; |
277 | 0 | } |
278 | 0 | } else { |
279 | 0 | if (fetched) |
280 | 0 | EVP_MD_free(hmac_md); |
281 | | /* Default to UTF-8 password */ |
282 | 0 | if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_MAC_ID, |
283 | 0 | iter, keylen, key, md, libctx, propq)) { |
284 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); |
285 | 0 | goto err; |
286 | 0 | } |
287 | 0 | } |
288 | 0 | } |
289 | 0 | if (EVP_Q_mac(libctx, "HMAC", propq, md_name, NULL, key, keylen, |
290 | 0 | p12->authsafes->d.data->data, p12->authsafes->d.data->length, |
291 | 0 | mac, md_sz, &outlen) |
292 | 0 | == NULL) |
293 | 0 | goto err; |
294 | 0 | if (outlen > UINT_MAX) |
295 | 0 | goto err; |
296 | 0 | *maclen = (unsigned int)outlen; |
297 | 0 | ret = 1; |
298 | 0 | err: |
299 | 0 | OPENSSL_cleanse(key, sizeof(key)); |
300 | 0 | EVP_MD_free(md); |
301 | 0 | return ret; |
302 | 0 | } |
303 | | |
304 | | int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen, |
305 | | unsigned char *mac, unsigned int *maclen) |
306 | 0 | { |
307 | 0 | return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NID_undef, NID_undef, NULL); |
308 | 0 | } |
309 | | |
310 | | /* Verify the mac */ |
311 | | int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen) |
312 | 0 | { |
313 | 0 | unsigned char mac[EVP_MAX_MD_SIZE]; |
314 | 0 | unsigned int maclen; |
315 | 0 | const ASN1_OCTET_STRING *macoct; |
316 | 0 | const X509_ALGOR *macalg; |
317 | 0 | const ASN1_OBJECT *macoid; |
318 | |
|
319 | 0 | if (p12->mac == NULL) { |
320 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_ABSENT); |
321 | 0 | return 0; |
322 | 0 | } |
323 | | |
324 | 0 | X509_SIG_get0(p12->mac->dinfo, &macalg, NULL); |
325 | 0 | X509_ALGOR_get0(&macoid, NULL, NULL, macalg); |
326 | 0 | if (OBJ_obj2nid(macoid) == NID_pbmac1) { |
327 | 0 | PBMAC1PARAM *param = NULL; |
328 | 0 | const ASN1_OBJECT *hmac_oid; |
329 | 0 | int md_nid = NID_undef; |
330 | |
|
331 | 0 | param = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBMAC1PARAM), macalg->parameter); |
332 | 0 | if (param == NULL) { |
333 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_UNSUPPORTED); |
334 | 0 | return 0; |
335 | 0 | } |
336 | 0 | X509_ALGOR_get0(&hmac_oid, NULL, NULL, param->messageAuthScheme); |
337 | 0 | md_nid = ossl_hmac2mdnid(OBJ_obj2nid(hmac_oid)); |
338 | |
|
339 | 0 | if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, md_nid, NID_undef, NULL)) { |
340 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR); |
341 | 0 | PBMAC1PARAM_free(param); |
342 | 0 | return 0; |
343 | 0 | } |
344 | 0 | PBMAC1PARAM_free(param); |
345 | 0 | } else { |
346 | 0 | if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NID_undef, NID_undef, NULL)) { |
347 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR); |
348 | 0 | return 0; |
349 | 0 | } |
350 | 0 | } |
351 | 0 | X509_SIG_get0(p12->mac->dinfo, NULL, &macoct); |
352 | 0 | if ((maclen != (unsigned int)ASN1_STRING_length(macoct)) |
353 | 0 | || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0) |
354 | 0 | return 0; |
355 | | |
356 | 0 | return 1; |
357 | 0 | } |
358 | | |
359 | | /* Set a mac */ |
360 | | int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen, |
361 | | unsigned char *salt, int saltlen, int iter, |
362 | | const EVP_MD *md_type) |
363 | 0 | { |
364 | 0 | unsigned char mac[EVP_MAX_MD_SIZE]; |
365 | 0 | unsigned int maclen; |
366 | 0 | ASN1_OCTET_STRING *macoct; |
367 | |
|
368 | 0 | if (md_type == NULL) |
369 | | /* No need to do a fetch as the md_type is used only to get a NID */ |
370 | 0 | md_type = EVP_sha256(); |
371 | 0 | if (!iter) |
372 | 0 | iter = PKCS12_DEFAULT_ITER; |
373 | 0 | if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) { |
374 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR); |
375 | 0 | return 0; |
376 | 0 | } |
377 | | /* |
378 | | * Note that output mac is forced to UTF-8... |
379 | | */ |
380 | 0 | if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NID_undef, NID_undef, NULL)) { |
381 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR); |
382 | 0 | return 0; |
383 | 0 | } |
384 | 0 | X509_SIG_getm(p12->mac->dinfo, NULL, &macoct); |
385 | 0 | if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) { |
386 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_STRING_SET_ERROR); |
387 | 0 | return 0; |
388 | 0 | } |
389 | 0 | return 1; |
390 | 0 | } |
391 | | |
392 | | static int pkcs12_pbmac1_pbkdf2_key_gen(const char *pass, int passlen, |
393 | | unsigned char *salt, int saltlen, |
394 | | int id, int iter, int keylen, |
395 | | unsigned char *out, |
396 | | const EVP_MD *md_type, |
397 | | OSSL_LIB_CTX *libctx, const char *propq) |
398 | 0 | { |
399 | 0 | return ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, md_type, |
400 | 0 | keylen, out, libctx, propq); |
401 | 0 | } |
402 | | |
403 | | static int pkcs12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen, |
404 | | int nid) |
405 | 0 | { |
406 | 0 | X509_ALGOR *macalg; |
407 | |
|
408 | 0 | PKCS12_MAC_DATA_free(p12->mac); |
409 | 0 | p12->mac = NULL; |
410 | |
|
411 | 0 | if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL) |
412 | 0 | return PKCS12_ERROR; |
413 | 0 | if (iter > 1) { |
414 | 0 | if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) { |
415 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
416 | 0 | return 0; |
417 | 0 | } |
418 | 0 | if (!ASN1_INTEGER_set(p12->mac->iter, iter)) { |
419 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
420 | 0 | return 0; |
421 | 0 | } |
422 | 0 | } |
423 | 0 | if (saltlen == 0) |
424 | 0 | saltlen = PKCS12_SALT_LEN; |
425 | 0 | else if (saltlen < 0) |
426 | 0 | return 0; |
427 | 0 | if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL) |
428 | 0 | return 0; |
429 | 0 | p12->mac->salt->length = saltlen; |
430 | 0 | if (salt == NULL) { |
431 | 0 | if (RAND_bytes_ex(p12->authsafes->ctx.libctx, p12->mac->salt->data, |
432 | 0 | (size_t)saltlen, 0) |
433 | 0 | <= 0) |
434 | 0 | return 0; |
435 | 0 | } else { |
436 | 0 | memcpy(p12->mac->salt->data, salt, saltlen); |
437 | 0 | } |
438 | 0 | X509_SIG_getm(p12->mac->dinfo, &macalg, NULL); |
439 | 0 | if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(nid), V_ASN1_NULL, NULL)) { |
440 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
441 | 0 | return 0; |
442 | 0 | } |
443 | | |
444 | 0 | return 1; |
445 | 0 | } |
446 | | |
447 | | /* Set up a mac structure */ |
448 | | int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen, |
449 | | const EVP_MD *md_type) |
450 | 0 | { |
451 | 0 | return pkcs12_setup_mac(p12, iter, salt, saltlen, EVP_MD_get_type(md_type)); |
452 | 0 | } |
453 | | |
454 | | int PKCS12_set_pbmac1_pbkdf2(PKCS12 *p12, const char *pass, int passlen, |
455 | | unsigned char *salt, int saltlen, int iter, |
456 | | const EVP_MD *md_type, const char *prf_md_name) |
457 | 0 | { |
458 | 0 | unsigned char mac[EVP_MAX_MD_SIZE]; |
459 | 0 | unsigned int maclen; |
460 | 0 | ASN1_OCTET_STRING *macoct; |
461 | 0 | X509_ALGOR *alg = NULL; |
462 | 0 | int ret = 0; |
463 | 0 | int prf_md_nid = NID_undef, prf_nid = NID_undef, hmac_nid; |
464 | 0 | unsigned char *known_salt = NULL; |
465 | 0 | int keylen = 0; |
466 | 0 | PBMAC1PARAM *param = NULL; |
467 | 0 | X509_ALGOR *hmac_alg = NULL, *macalg = NULL; |
468 | 0 | OSSL_LIB_CTX *libctx = p12->authsafes->ctx.libctx; |
469 | |
|
470 | 0 | if (md_type == NULL) |
471 | | /* No need to do a fetch as the md_type is used only to get a NID */ |
472 | 0 | md_type = EVP_sha256(); |
473 | |
|
474 | 0 | if (prf_md_name == NULL) |
475 | 0 | prf_md_nid = EVP_MD_get_type(md_type); |
476 | 0 | else |
477 | 0 | prf_md_nid = OBJ_txt2nid(prf_md_name); |
478 | |
|
479 | 0 | if (iter == 0) |
480 | 0 | iter = PKCS12_DEFAULT_ITER; |
481 | |
|
482 | 0 | keylen = EVP_MD_get_size(md_type); |
483 | |
|
484 | 0 | prf_nid = ossl_md2hmacnid(prf_md_nid); |
485 | 0 | hmac_nid = ossl_md2hmacnid(EVP_MD_get_type(md_type)); |
486 | |
|
487 | 0 | if (prf_nid == NID_undef || hmac_nid == NID_undef) { |
488 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM); |
489 | 0 | goto err; |
490 | 0 | } |
491 | | |
492 | 0 | if (salt == NULL) { |
493 | 0 | if (saltlen < 0) { |
494 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_SALT_LENGTH); |
495 | 0 | goto err; |
496 | 0 | } |
497 | 0 | if (saltlen > 0) { |
498 | 0 | known_salt = OPENSSL_malloc(saltlen); |
499 | 0 | if (known_salt == NULL) |
500 | 0 | goto err; |
501 | | |
502 | 0 | if (RAND_bytes_ex(libctx, known_salt, saltlen, 0) <= 0) { |
503 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_RAND_LIB); |
504 | 0 | goto err; |
505 | 0 | } |
506 | 0 | } |
507 | 0 | } |
508 | | |
509 | 0 | param = PBMAC1PARAM_new(); |
510 | 0 | hmac_alg = X509_ALGOR_new(); |
511 | 0 | alg = PKCS5_pbkdf2_set_ex(iter, salt ? salt : known_salt, saltlen, |
512 | 0 | prf_nid, keylen, libctx); |
513 | 0 | if (param == NULL || hmac_alg == NULL || alg == NULL) |
514 | 0 | goto err; |
515 | | |
516 | 0 | if (pkcs12_setup_mac(p12, iter, salt ? salt : known_salt, saltlen, |
517 | 0 | NID_pbmac1) |
518 | 0 | == PKCS12_ERROR) { |
519 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR); |
520 | 0 | goto err; |
521 | 0 | } |
522 | | |
523 | 0 | if (!X509_ALGOR_set0(hmac_alg, OBJ_nid2obj(hmac_nid), V_ASN1_NULL, NULL)) { |
524 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR); |
525 | 0 | goto err; |
526 | 0 | } |
527 | | |
528 | 0 | X509_ALGOR_free(param->keyDerivationFunc); |
529 | 0 | X509_ALGOR_free(param->messageAuthScheme); |
530 | 0 | param->keyDerivationFunc = alg; |
531 | 0 | param->messageAuthScheme = hmac_alg; |
532 | 0 | alg = NULL; |
533 | 0 | hmac_alg = NULL; |
534 | |
|
535 | 0 | X509_SIG_getm(p12->mac->dinfo, &macalg, &macoct); |
536 | 0 | if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBMAC1PARAM), param, &macalg->parameter)) |
537 | 0 | goto err; |
538 | | |
539 | | /* |
540 | | * Note that output mac is forced to UTF-8... |
541 | | */ |
542 | 0 | if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, |
543 | 0 | EVP_MD_get_type(md_type), prf_md_nid, |
544 | 0 | pkcs12_pbmac1_pbkdf2_key_gen)) { |
545 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR); |
546 | 0 | goto err; |
547 | 0 | } |
548 | 0 | if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) { |
549 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_STRING_SET_ERROR); |
550 | 0 | goto err; |
551 | 0 | } |
552 | 0 | ret = 1; |
553 | |
|
554 | 0 | err: |
555 | 0 | X509_ALGOR_free(alg); |
556 | 0 | X509_ALGOR_free(hmac_alg); |
557 | 0 | PBMAC1PARAM_free(param); |
558 | 0 | OPENSSL_free(known_salt); |
559 | 0 | return ret; |
560 | 0 | } |