2 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 * HMAC low level APIs are deprecated for public use, but still ok for internal
14 #include "internal/deprecated.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"
24 int PKCS12_mac_present(const PKCS12
*p12
)
26 return p12
->mac
? 1 : 0;
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
,
36 X509_SIG_get0(p12
->mac
->dinfo
, pmacalg
, pmac
);
38 *psalt
= p12
->mac
->salt
;
40 *piter
= p12
->mac
->iter
;
53 #define TK26_MAC_KEY_LEN 32
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
,
60 unsigned char out
[96];
62 if (keylen
!= TK26_MAC_KEY_LEN
) {
66 if (!PKCS5_PBKDF2_HMAC(pass
, passlen
, salt
, saltlen
, iter
,
67 digest
, sizeof(out
), out
)) {
70 memcpy(key
, out
+ sizeof(out
) - TK26_MAC_KEY_LEN
, TK26_MAC_KEY_LEN
);
71 OPENSSL_cleanse(out
, sizeof(out
));
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
,
82 const EVP_MD
*md_type
))
87 HMAC_CTX
*hmac
= NULL
;
88 unsigned char key
[EVP_MAX_MD_SIZE
], *salt
;
93 const X509_ALGOR
*macalg
;
94 const ASN1_OBJECT
*macoid
;
96 if (!PKCS7_type_is_data(p12
->authsafes
)) {
97 ERR_raise(ERR_LIB_PKCS12
, PKCS12_R_CONTENT_TYPE_NOT_DATA
);
101 salt
= p12
->mac
->salt
->data
;
102 saltlen
= p12
->mac
->salt
->length
;
103 if (p12
->mac
->iter
== NULL
)
106 iter
= ASN1_INTEGER_get(p12
->mac
->iter
);
107 X509_SIG_get0(p12
->mac
->dinfo
, &macalg
, NULL
);
108 X509_ALGOR_get0(&macoid
, NULL
, NULL
, macalg
);
109 if (OBJ_obj2txt(md_name
, sizeof(md_name
), macoid
, 0) < 0)
111 md
= md_fetch
= EVP_MD_fetch(p12
->authsafes
->ctx
.libctx
, md_name
,
112 p12
->authsafes
->ctx
.propq
);
114 md
= EVP_get_digestbynid(OBJ_obj2nid(macoid
));
117 ERR_raise(ERR_LIB_PKCS12
, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM
);
120 md_size
= EVP_MD_get_size(md
);
121 md_nid
= EVP_MD_get_type(md
);
124 if ((md_nid
== NID_id_GostR3411_94
125 || md_nid
== NID_id_GostR3411_2012_256
126 || md_nid
== NID_id_GostR3411_2012_512
)
127 && ossl_safe_getenv("LEGACY_GOST_PKCS12") == NULL
) {
128 md_size
= TK26_MAC_KEY_LEN
;
129 if (!pkcs12_gen_gost_mac_key(pass
, passlen
, salt
, saltlen
, iter
,
131 ERR_raise(ERR_LIB_PKCS12
, PKCS12_R_KEY_GEN_ERROR
);
135 if (pkcs12_key_gen
!= NULL
) {
136 if (!(*pkcs12_key_gen
)(pass
, passlen
, salt
, saltlen
, PKCS12_MAC_ID
,
137 iter
, md_size
, key
, md
)) {
138 ERR_raise(ERR_LIB_PKCS12
, PKCS12_R_KEY_GEN_ERROR
);
142 /* Default to UTF-8 password */
143 if (!PKCS12_key_gen_utf8_ex(pass
, passlen
, salt
, saltlen
, PKCS12_MAC_ID
,
144 iter
, md_size
, key
, md
,
145 p12
->authsafes
->ctx
.libctx
,
146 p12
->authsafes
->ctx
.propq
)) {
147 ERR_raise(ERR_LIB_PKCS12
, PKCS12_R_KEY_GEN_ERROR
);
152 if ((hmac
= HMAC_CTX_new()) == NULL
153 || !HMAC_Init_ex(hmac
, key
, md_size
, md
, NULL
)
154 || !HMAC_Update(hmac
, p12
->authsafes
->d
.data
->data
,
155 p12
->authsafes
->d
.data
->length
)
156 || !HMAC_Final(hmac
, mac
, maclen
)) {
162 OPENSSL_cleanse(key
, sizeof(key
));
164 EVP_MD_free(md_fetch
);
168 int PKCS12_gen_mac(PKCS12
*p12
, const char *pass
, int passlen
,
169 unsigned char *mac
, unsigned int *maclen
)
171 return pkcs12_gen_mac(p12
, pass
, passlen
, mac
, maclen
, NULL
);
175 int PKCS12_verify_mac(PKCS12
*p12
, const char *pass
, int passlen
)
177 unsigned char mac
[EVP_MAX_MD_SIZE
];
179 const ASN1_OCTET_STRING
*macoct
;
181 if (p12
->mac
== NULL
) {
182 ERR_raise(ERR_LIB_PKCS12
, PKCS12_R_MAC_ABSENT
);
185 if (!pkcs12_gen_mac(p12
, pass
, passlen
, mac
, &maclen
, NULL
)) {
186 ERR_raise(ERR_LIB_PKCS12
, PKCS12_R_MAC_GENERATION_ERROR
);
189 X509_SIG_get0(p12
->mac
->dinfo
, NULL
, &macoct
);
190 if ((maclen
!= (unsigned int)ASN1_STRING_length(macoct
))
191 || CRYPTO_memcmp(mac
, ASN1_STRING_get0_data(macoct
), maclen
) != 0)
199 int PKCS12_set_mac(PKCS12
*p12
, const char *pass
, int passlen
,
200 unsigned char *salt
, int saltlen
, int iter
,
201 const EVP_MD
*md_type
)
203 unsigned char mac
[EVP_MAX_MD_SIZE
];
205 ASN1_OCTET_STRING
*macoct
;
208 /* No need to do a fetch as the md_type is used only to get a NID */
209 md_type
= EVP_sha256();
211 iter
= PKCS12_DEFAULT_ITER
;
212 if (PKCS12_setup_mac(p12
, iter
, salt
, saltlen
, md_type
) == PKCS12_ERROR
) {
213 ERR_raise(ERR_LIB_PKCS12
, PKCS12_R_MAC_SETUP_ERROR
);
217 * Note that output mac is forced to UTF-8...
219 if (!pkcs12_gen_mac(p12
, pass
, passlen
, mac
, &maclen
, NULL
)) {
220 ERR_raise(ERR_LIB_PKCS12
, PKCS12_R_MAC_GENERATION_ERROR
);
223 X509_SIG_getm(p12
->mac
->dinfo
, NULL
, &macoct
);
224 if (!ASN1_OCTET_STRING_set(macoct
, mac
, maclen
)) {
225 ERR_raise(ERR_LIB_PKCS12
, PKCS12_R_MAC_STRING_SET_ERROR
);
231 /* Set up a mac structure */
232 int PKCS12_setup_mac(PKCS12
*p12
, int iter
, unsigned char *salt
, int saltlen
,
233 const EVP_MD
*md_type
)
237 PKCS12_MAC_DATA_free(p12
->mac
);
240 if ((p12
->mac
= PKCS12_MAC_DATA_new()) == NULL
)
243 if ((p12
->mac
->iter
= ASN1_INTEGER_new()) == NULL
) {
244 ERR_raise(ERR_LIB_PKCS12
, ERR_R_MALLOC_FAILURE
);
247 if (!ASN1_INTEGER_set(p12
->mac
->iter
, iter
)) {
248 ERR_raise(ERR_LIB_PKCS12
, ERR_R_MALLOC_FAILURE
);
253 saltlen
= PKCS12_SALT_LEN
;
256 if ((p12
->mac
->salt
->data
= OPENSSL_malloc(saltlen
)) == NULL
) {
257 ERR_raise(ERR_LIB_PKCS12
, ERR_R_MALLOC_FAILURE
);
260 p12
->mac
->salt
->length
= saltlen
;
264 if (RAND_bytes_ex(p12
->authsafes
->ctx
.libctx
, p12
->mac
->salt
->data
,
265 (size_t)saltlen
, 0) <= 0)
268 memcpy(p12
->mac
->salt
->data
, salt
, saltlen
);
269 X509_SIG_getm(p12
->mac
->dinfo
, &macalg
, NULL
);
270 if (!X509_ALGOR_set0(macalg
, OBJ_nid2obj(EVP_MD_get_type(md_type
)),
271 V_ASN1_NULL
, NULL
)) {
272 ERR_raise(ERR_LIB_PKCS12
, ERR_R_MALLOC_FAILURE
);