]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
b6461792 | 2 | * Copyright 1999-2024 The OpenSSL Project Authors. All Rights Reserved. |
8d8c7266 | 3 | * |
54fffdf4 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 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 | |
8d8c7266 DSH |
8 | */ |
9 | ||
dbde4726 P |
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 | ||
5c39a55d P |
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> | |
706457b7 | 22 | #include "p12_local.h" |
8d8c7266 | 23 | |
28da1455 | 24 | int PKCS12_mac_present(const PKCS12 *p12) |
293042c9 | 25 | { |
c9018bdf | 26 | return p12->mac ? 1 : 0; |
293042c9 DSH |
27 | } |
28 | ||
59b4da05 DSH |
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) | |
293042c9 DSH |
34 | { |
35 | if (p12->mac) { | |
59b4da05 | 36 | X509_SIG_get0(p12->mac->dinfo, pmacalg, pmac); |
293042c9 DSH |
37 | if (psalt) |
38 | *psalt = p12->mac->salt; | |
39 | if (piter) | |
40 | *piter = p12->mac->iter; | |
41 | } else { | |
42 | if (pmac) | |
43 | *pmac = NULL; | |
44 | if (pmacalg) | |
45 | *pmacalg = NULL; | |
46 | if (psalt) | |
47 | *psalt = NULL; | |
48 | if (piter) | |
49 | *piter = NULL; | |
50 | } | |
51 | } | |
52 | ||
5c39a55d | 53 | #define TK26_MAC_KEY_LEN 32 |
77886387 MC |
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 | { | |
60 | unsigned char out[96]; | |
61 | ||
62 | if (keylen != TK26_MAC_KEY_LEN) { | |
63 | return 0; | |
64 | } | |
65 | ||
66 | if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, | |
67 | digest, sizeof(out), out)) { | |
68 | return 0; | |
69 | } | |
70 | memcpy(key, out + sizeof(out) - TK26_MAC_KEY_LEN, TK26_MAC_KEY_LEN); | |
71 | OPENSSL_cleanse(out, sizeof(out)); | |
72 | return 1; | |
73 | } | |
74 | ||
8d8c7266 | 75 | /* Generate a MAC */ |
1194ea8d AP |
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)) | |
8d8c7266 | 83 | { |
f5cee414 | 84 | int ret = 0; |
b536880c JS |
85 | const EVP_MD *md; |
86 | EVP_MD *md_fetch; | |
bf7c6817 | 87 | HMAC_CTX *hmac = NULL; |
0f113f3e MC |
88 | unsigned char key[EVP_MAX_MD_SIZE], *salt; |
89 | int saltlen, iter; | |
b536880c | 90 | char md_name[80]; |
77886387 | 91 | int md_size = 0; |
b536880c | 92 | int md_nid; |
59b4da05 | 93 | const X509_ALGOR *macalg; |
ac4e2577 | 94 | const ASN1_OBJECT *macoid; |
dbad1690 | 95 | |
0f113f3e | 96 | if (!PKCS7_type_is_data(p12->authsafes)) { |
9311d0c4 | 97 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA); |
0f113f3e MC |
98 | return 0; |
99 | } | |
1c2f1fe5 | 100 | |
041962b4 MC |
101 | if (p12->authsafes->d.data == NULL) { |
102 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR); | |
103 | return 0; | |
104 | } | |
105 | ||
0f113f3e MC |
106 | salt = p12->mac->salt->data; |
107 | saltlen = p12->mac->salt->length; | |
12a765a5 | 108 | if (p12->mac->iter == NULL) |
0f113f3e MC |
109 | iter = 1; |
110 | else | |
111 | iter = ASN1_INTEGER_get(p12->mac->iter); | |
59b4da05 | 112 | X509_SIG_get0(p12->mac->dinfo, &macalg, NULL); |
a6eb1ce6 | 113 | X509_ALGOR_get0(&macoid, NULL, NULL, macalg); |
b536880c JS |
114 | if (OBJ_obj2txt(md_name, sizeof(md_name), macoid, 0) < 0) |
115 | return 0; | |
dc4ccc70 YP |
116 | |
117 | (void)ERR_set_mark(); | |
b536880c JS |
118 | md = md_fetch = EVP_MD_fetch(p12->authsafes->ctx.libctx, md_name, |
119 | p12->authsafes->ctx.propq); | |
120 | if (md == NULL) | |
121 | md = EVP_get_digestbynid(OBJ_obj2nid(macoid)); | |
122 | ||
123 | if (md == NULL) { | |
dc4ccc70 | 124 | (void)ERR_clear_last_mark(); |
9311d0c4 | 125 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM); |
0f113f3e MC |
126 | return 0; |
127 | } | |
dc4ccc70 YP |
128 | (void)ERR_pop_to_mark(); |
129 | ||
ed576acd TM |
130 | md_size = EVP_MD_get_size(md); |
131 | md_nid = EVP_MD_get_type(md); | |
0f113f3e | 132 | if (md_size < 0) |
b536880c JS |
133 | goto err; |
134 | if ((md_nid == NID_id_GostR3411_94 | |
135 | || md_nid == NID_id_GostR3411_2012_256 | |
136 | || md_nid == NID_id_GostR3411_2012_512) | |
5c39a55d | 137 | && ossl_safe_getenv("LEGACY_GOST_PKCS12") == NULL) { |
77886387 MC |
138 | md_size = TK26_MAC_KEY_LEN; |
139 | if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter, | |
b536880c | 140 | md_size, key, md)) { |
9311d0c4 | 141 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); |
f5cee414 | 142 | goto err; |
77886387 | 143 | } |
b536880c JS |
144 | } else { |
145 | if (pkcs12_key_gen != NULL) { | |
146 | if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID, | |
147 | iter, md_size, key, md)) { | |
148 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); | |
149 | goto err; | |
150 | } | |
151 | } else { | |
152 | /* Default to UTF-8 password */ | |
153 | if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_MAC_ID, | |
154 | iter, md_size, key, md, | |
155 | p12->authsafes->ctx.libctx, | |
156 | p12->authsafes->ctx.propq)) { | |
157 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); | |
158 | goto err; | |
159 | } | |
160 | } | |
0f113f3e | 161 | } |
d3c3dfc5 | 162 | if ((hmac = HMAC_CTX_new()) == NULL |
b536880c | 163 | || !HMAC_Init_ex(hmac, key, md_size, md, NULL) |
bf7c6817 | 164 | || !HMAC_Update(hmac, p12->authsafes->d.data->data, |
0f113f3e | 165 | p12->authsafes->d.data->length) |
bf7c6817 | 166 | || !HMAC_Final(hmac, mac, maclen)) { |
f5cee414 | 167 | goto err; |
0f113f3e | 168 | } |
f5cee414 SL |
169 | ret = 1; |
170 | ||
171 | err: | |
172 | OPENSSL_cleanse(key, sizeof(key)); | |
bf7c6817 | 173 | HMAC_CTX_free(hmac); |
b536880c | 174 | EVP_MD_free(md_fetch); |
f5cee414 | 175 | return ret; |
8d8c7266 DSH |
176 | } |
177 | ||
1194ea8d AP |
178 | int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen, |
179 | unsigned char *mac, unsigned int *maclen) | |
180 | { | |
181 | return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NULL); | |
182 | } | |
183 | ||
8d8c7266 | 184 | /* Verify the mac */ |
8afca8d9 | 185 | int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen) |
8d8c7266 | 186 | { |
0f113f3e MC |
187 | unsigned char mac[EVP_MAX_MD_SIZE]; |
188 | unsigned int maclen; | |
59b4da05 | 189 | const ASN1_OCTET_STRING *macoct; |
a6eb1ce6 | 190 | |
0f113f3e | 191 | if (p12->mac == NULL) { |
9311d0c4 | 192 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_ABSENT); |
0f113f3e MC |
193 | return 0; |
194 | } | |
b536880c | 195 | if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NULL)) { |
9311d0c4 | 196 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR); |
0f113f3e MC |
197 | return 0; |
198 | } | |
59b4da05 | 199 | X509_SIG_get0(p12->mac->dinfo, NULL, &macoct); |
0fe17491 AP |
200 | if ((maclen != (unsigned int)ASN1_STRING_length(macoct)) |
201 | || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0) | |
0f113f3e | 202 | return 0; |
1194ea8d | 203 | |
0f113f3e | 204 | return 1; |
8d8c7266 DSH |
205 | } |
206 | ||
207 | /* Set a mac */ | |
208 | ||
8afca8d9 | 209 | int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen, |
0f113f3e MC |
210 | unsigned char *salt, int saltlen, int iter, |
211 | const EVP_MD *md_type) | |
8d8c7266 | 212 | { |
0f113f3e MC |
213 | unsigned char mac[EVP_MAX_MD_SIZE]; |
214 | unsigned int maclen; | |
a6eb1ce6 | 215 | ASN1_OCTET_STRING *macoct; |
61f5b6f3 | 216 | |
762970bd TM |
217 | if (md_type == NULL) |
218 | /* No need to do a fetch as the md_type is used only to get a NID */ | |
219 | md_type = EVP_sha256(); | |
220 | if (!iter) | |
221 | iter = PKCS12_DEFAULT_ITER; | |
0f113f3e | 222 | if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) { |
9311d0c4 | 223 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR); |
0f113f3e MC |
224 | return 0; |
225 | } | |
1194ea8d AP |
226 | /* |
227 | * Note that output mac is forced to UTF-8... | |
228 | */ | |
b536880c | 229 | if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NULL)) { |
9311d0c4 | 230 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR); |
0f113f3e MC |
231 | return 0; |
232 | } | |
0b7347ef | 233 | X509_SIG_getm(p12->mac->dinfo, NULL, &macoct); |
a6eb1ce6 | 234 | if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) { |
9311d0c4 | 235 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_STRING_SET_ERROR); |
0f113f3e MC |
236 | return 0; |
237 | } | |
238 | return 1; | |
8d8c7266 DSH |
239 | } |
240 | ||
241 | /* Set up a mac structure */ | |
8afca8d9 | 242 | int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen, |
0f113f3e | 243 | const EVP_MD *md_type) |
8d8c7266 | 244 | { |
a6eb1ce6 DSH |
245 | X509_ALGOR *macalg; |
246 | ||
1b8f1937 DB |
247 | PKCS12_MAC_DATA_free(p12->mac); |
248 | p12->mac = NULL; | |
249 | ||
75ebbd9a | 250 | if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL) |
0f113f3e MC |
251 | return PKCS12_ERROR; |
252 | if (iter > 1) { | |
75ebbd9a | 253 | if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) { |
e077455e | 254 | ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
0f113f3e MC |
255 | return 0; |
256 | } | |
257 | if (!ASN1_INTEGER_set(p12->mac->iter, iter)) { | |
e077455e | 258 | ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
0f113f3e MC |
259 | return 0; |
260 | } | |
261 | } | |
94289779 | 262 | if (saltlen == 0) |
0f113f3e | 263 | saltlen = PKCS12_SALT_LEN; |
94289779 | 264 | else if (saltlen < 0) |
42e7d2f1 | 265 | return 0; |
e077455e | 266 | if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL) |
0f113f3e | 267 | return 0; |
c2319cf9 | 268 | p12->mac->salt->length = saltlen; |
94289779 | 269 | if (salt == NULL) { |
b536880c | 270 | if (RAND_bytes_ex(p12->authsafes->ctx.libctx, p12->mac->salt->data, |
28cab209 | 271 | (size_t)saltlen, 0) <= 0) |
0f113f3e | 272 | return 0; |
94289779 | 273 | } else { |
0f113f3e | 274 | memcpy(p12->mac->salt->data, salt, saltlen); |
94289779 | 275 | } |
0b7347ef | 276 | X509_SIG_getm(p12->mac->dinfo, &macalg, NULL); |
ed576acd | 277 | if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(EVP_MD_get_type(md_type)), |
a6eb1ce6 | 278 | V_ASN1_NULL, NULL)) { |
e077455e | 279 | ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
0f113f3e MC |
280 | return 0; |
281 | } | |
0f113f3e MC |
282 | |
283 | return 1; | |
8d8c7266 | 284 | } |