/src/openssl30/crypto/pkcs12/p12_mutl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1999-2023 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 <openssl/crypto.h> |
19 | | #include <openssl/hmac.h> |
20 | | #include <openssl/rand.h> |
21 | | #include <openssl/pkcs12.h> |
22 | | #include "p12_local.h" |
23 | | |
24 | | int PKCS12_mac_present(const PKCS12 *p12) |
25 | 0 | { |
26 | 0 | return p12->mac ? 1 : 0; |
27 | 0 | } |
28 | | |
29 | | void PKCS12_get0_mac(const ASN1_OCTET_STRING **pmac, |
30 | | const X509_ALGOR **pmacalg, |
31 | | const ASN1_OCTET_STRING **psalt, |
32 | | const ASN1_INTEGER **piter, |
33 | | const PKCS12 *p12) |
34 | 0 | { |
35 | 0 | if (p12->mac) { |
36 | 0 | X509_SIG_get0(p12->mac->dinfo, pmacalg, pmac); |
37 | 0 | if (psalt) |
38 | 0 | *psalt = p12->mac->salt; |
39 | 0 | if (piter) |
40 | 0 | *piter = p12->mac->iter; |
41 | 0 | } else { |
42 | 0 | if (pmac) |
43 | 0 | *pmac = NULL; |
44 | 0 | if (pmacalg) |
45 | 0 | *pmacalg = NULL; |
46 | 0 | if (psalt) |
47 | 0 | *psalt = NULL; |
48 | 0 | if (piter) |
49 | 0 | *piter = NULL; |
50 | 0 | } |
51 | 0 | } |
52 | | |
53 | 0 | #define TK26_MAC_KEY_LEN 32 |
54 | | |
55 | | static int pkcs12_gen_gost_mac_key(const char *pass, int passlen, |
56 | | const unsigned char *salt, int saltlen, |
57 | | int iter, int keylen, unsigned char *key, |
58 | | const EVP_MD *digest) |
59 | 0 | { |
60 | 0 | unsigned char out[96]; |
61 | |
|
62 | 0 | if (keylen != TK26_MAC_KEY_LEN) { |
63 | 0 | return 0; |
64 | 0 | } |
65 | | |
66 | 0 | if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, |
67 | 0 | digest, sizeof(out), out)) { |
68 | 0 | return 0; |
69 | 0 | } |
70 | 0 | memcpy(key, out + sizeof(out) - TK26_MAC_KEY_LEN, TK26_MAC_KEY_LEN); |
71 | 0 | OPENSSL_cleanse(out, sizeof(out)); |
72 | 0 | return 1; |
73 | 0 | } |
74 | | |
75 | | /* Generate a MAC */ |
76 | | static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen, |
77 | | unsigned char *mac, unsigned int *maclen, |
78 | | int (*pkcs12_key_gen)(const char *pass, int passlen, |
79 | | unsigned char *salt, int slen, |
80 | | int id, int iter, int n, |
81 | | unsigned char *out, |
82 | | const EVP_MD *md_type)) |
83 | 0 | { |
84 | 0 | int ret = 0; |
85 | 0 | const EVP_MD *md; |
86 | 0 | EVP_MD *md_fetch; |
87 | 0 | HMAC_CTX *hmac = NULL; |
88 | 0 | unsigned char key[EVP_MAX_MD_SIZE], *salt; |
89 | 0 | int saltlen, iter; |
90 | 0 | char md_name[80]; |
91 | 0 | int md_size = 0; |
92 | 0 | int md_nid; |
93 | 0 | const X509_ALGOR *macalg; |
94 | 0 | const ASN1_OBJECT *macoid; |
95 | |
|
96 | 0 | if (!PKCS7_type_is_data(p12->authsafes)) { |
97 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA); |
98 | 0 | return 0; |
99 | 0 | } |
100 | | |
101 | 0 | salt = p12->mac->salt->data; |
102 | 0 | saltlen = p12->mac->salt->length; |
103 | 0 | if (p12->mac->iter == NULL) |
104 | 0 | iter = 1; |
105 | 0 | else |
106 | 0 | iter = ASN1_INTEGER_get(p12->mac->iter); |
107 | 0 | X509_SIG_get0(p12->mac->dinfo, &macalg, NULL); |
108 | 0 | X509_ALGOR_get0(&macoid, NULL, NULL, macalg); |
109 | 0 | if (OBJ_obj2txt(md_name, sizeof(md_name), macoid, 0) < 0) |
110 | 0 | return 0; |
111 | | |
112 | 0 | (void)ERR_set_mark(); |
113 | 0 | md = md_fetch = EVP_MD_fetch(p12->authsafes->ctx.libctx, md_name, |
114 | 0 | p12->authsafes->ctx.propq); |
115 | 0 | if (md == NULL) |
116 | 0 | md = EVP_get_digestbynid(OBJ_obj2nid(macoid)); |
117 | |
|
118 | 0 | if (md == NULL) { |
119 | 0 | (void)ERR_clear_last_mark(); |
120 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM); |
121 | 0 | return 0; |
122 | 0 | } |
123 | 0 | (void)ERR_pop_to_mark(); |
124 | |
|
125 | 0 | md_size = EVP_MD_get_size(md); |
126 | 0 | md_nid = EVP_MD_get_type(md); |
127 | 0 | if (md_size < 0) |
128 | 0 | goto err; |
129 | 0 | if ((md_nid == NID_id_GostR3411_94 |
130 | 0 | || md_nid == NID_id_GostR3411_2012_256 |
131 | 0 | || md_nid == NID_id_GostR3411_2012_512) |
132 | 0 | && ossl_safe_getenv("LEGACY_GOST_PKCS12") == NULL) { |
133 | 0 | md_size = TK26_MAC_KEY_LEN; |
134 | 0 | if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter, |
135 | 0 | md_size, key, md)) { |
136 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); |
137 | 0 | goto err; |
138 | 0 | } |
139 | 0 | } else { |
140 | 0 | if (pkcs12_key_gen != NULL) { |
141 | 0 | if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID, |
142 | 0 | iter, md_size, key, md)) { |
143 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); |
144 | 0 | goto err; |
145 | 0 | } |
146 | 0 | } else { |
147 | | /* Default to UTF-8 password */ |
148 | 0 | if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_MAC_ID, |
149 | 0 | iter, md_size, key, md, |
150 | 0 | p12->authsafes->ctx.libctx, |
151 | 0 | p12->authsafes->ctx.propq)) { |
152 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); |
153 | 0 | goto err; |
154 | 0 | } |
155 | 0 | } |
156 | 0 | } |
157 | 0 | if ((hmac = HMAC_CTX_new()) == NULL |
158 | 0 | || !HMAC_Init_ex(hmac, key, md_size, md, NULL) |
159 | 0 | || !HMAC_Update(hmac, p12->authsafes->d.data->data, |
160 | 0 | p12->authsafes->d.data->length) |
161 | 0 | || !HMAC_Final(hmac, mac, maclen)) { |
162 | 0 | goto err; |
163 | 0 | } |
164 | 0 | ret = 1; |
165 | |
|
166 | 0 | err: |
167 | 0 | OPENSSL_cleanse(key, sizeof(key)); |
168 | 0 | HMAC_CTX_free(hmac); |
169 | 0 | EVP_MD_free(md_fetch); |
170 | 0 | return ret; |
171 | 0 | } |
172 | | |
173 | | int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen, |
174 | | unsigned char *mac, unsigned int *maclen) |
175 | 0 | { |
176 | 0 | return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NULL); |
177 | 0 | } |
178 | | |
179 | | /* Verify the mac */ |
180 | | int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen) |
181 | 0 | { |
182 | 0 | unsigned char mac[EVP_MAX_MD_SIZE]; |
183 | 0 | unsigned int maclen; |
184 | 0 | const ASN1_OCTET_STRING *macoct; |
185 | |
|
186 | 0 | if (p12->mac == NULL) { |
187 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_ABSENT); |
188 | 0 | return 0; |
189 | 0 | } |
190 | 0 | if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NULL)) { |
191 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR); |
192 | 0 | return 0; |
193 | 0 | } |
194 | 0 | X509_SIG_get0(p12->mac->dinfo, NULL, &macoct); |
195 | 0 | if ((maclen != (unsigned int)ASN1_STRING_length(macoct)) |
196 | 0 | || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0) |
197 | 0 | return 0; |
198 | | |
199 | 0 | return 1; |
200 | 0 | } |
201 | | |
202 | | /* Set a mac */ |
203 | | |
204 | | int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen, |
205 | | unsigned char *salt, int saltlen, int iter, |
206 | | const EVP_MD *md_type) |
207 | 0 | { |
208 | 0 | unsigned char mac[EVP_MAX_MD_SIZE]; |
209 | 0 | unsigned int maclen; |
210 | 0 | ASN1_OCTET_STRING *macoct; |
211 | |
|
212 | 0 | if (md_type == NULL) |
213 | | /* No need to do a fetch as the md_type is used only to get a NID */ |
214 | 0 | md_type = EVP_sha256(); |
215 | 0 | if (!iter) |
216 | 0 | iter = PKCS12_DEFAULT_ITER; |
217 | 0 | if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) { |
218 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR); |
219 | 0 | return 0; |
220 | 0 | } |
221 | | /* |
222 | | * Note that output mac is forced to UTF-8... |
223 | | */ |
224 | 0 | if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NULL)) { |
225 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR); |
226 | 0 | return 0; |
227 | 0 | } |
228 | 0 | X509_SIG_getm(p12->mac->dinfo, NULL, &macoct); |
229 | 0 | if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) { |
230 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_STRING_SET_ERROR); |
231 | 0 | return 0; |
232 | 0 | } |
233 | 0 | return 1; |
234 | 0 | } |
235 | | |
236 | | /* Set up a mac structure */ |
237 | | int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen, |
238 | | const EVP_MD *md_type) |
239 | 0 | { |
240 | 0 | X509_ALGOR *macalg; |
241 | |
|
242 | 0 | PKCS12_MAC_DATA_free(p12->mac); |
243 | 0 | p12->mac = NULL; |
244 | |
|
245 | 0 | if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL) |
246 | 0 | return PKCS12_ERROR; |
247 | 0 | if (iter > 1) { |
248 | 0 | if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) { |
249 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); |
250 | 0 | return 0; |
251 | 0 | } |
252 | 0 | if (!ASN1_INTEGER_set(p12->mac->iter, iter)) { |
253 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); |
254 | 0 | return 0; |
255 | 0 | } |
256 | 0 | } |
257 | 0 | if (saltlen == 0) |
258 | 0 | saltlen = PKCS12_SALT_LEN; |
259 | 0 | else if (saltlen < 0) |
260 | 0 | return 0; |
261 | 0 | if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL) { |
262 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); |
263 | 0 | return 0; |
264 | 0 | } |
265 | 0 | p12->mac->salt->length = saltlen; |
266 | 0 | if (salt == NULL) { |
267 | 0 | if (RAND_bytes_ex(p12->authsafes->ctx.libctx, p12->mac->salt->data, |
268 | 0 | (size_t)saltlen, 0) <= 0) |
269 | 0 | return 0; |
270 | 0 | } else { |
271 | 0 | memcpy(p12->mac->salt->data, salt, saltlen); |
272 | 0 | } |
273 | 0 | X509_SIG_getm(p12->mac->dinfo, &macalg, NULL); |
274 | 0 | if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(EVP_MD_get_type(md_type)), |
275 | 0 | V_ASN1_NULL, NULL)) { |
276 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); |
277 | 0 | return 0; |
278 | 0 | } |
279 | | |
280 | 0 | return 1; |
281 | 0 | } |