2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 # include "internal/cryptlib.h"
12 # include <openssl/crypto.h>
13 # include <openssl/hmac.h>
14 # include <openssl/rand.h>
15 # include <openssl/pkcs12.h>
18 int PKCS12_mac_present(PKCS12
*p12
)
20 return p12
->mac
? 1 : 0;
23 void PKCS12_get0_mac(ASN1_OCTET_STRING
**pmac
, X509_ALGOR
**pmacalg
,
24 ASN1_OCTET_STRING
**psalt
, ASN1_INTEGER
**piter
,
28 X509_SIG_get0(pmacalg
, pmac
, p12
->mac
->dinfo
);
30 *psalt
= p12
->mac
->salt
;
32 *piter
= p12
->mac
->iter
;
45 # define TK26_MAC_KEY_LEN 32
47 static int pkcs12_gen_gost_mac_key(const char *pass
, int passlen
,
48 const unsigned char *salt
, int saltlen
,
49 int iter
, int keylen
, unsigned char *key
,
52 unsigned char out
[96];
54 if (keylen
!= TK26_MAC_KEY_LEN
) {
58 if (!PKCS5_PBKDF2_HMAC(pass
, passlen
, salt
, saltlen
, iter
,
59 digest
, sizeof(out
), out
)) {
62 memcpy(key
, out
+ sizeof(out
) - TK26_MAC_KEY_LEN
, TK26_MAC_KEY_LEN
);
63 OPENSSL_cleanse(out
, sizeof(out
));
68 int PKCS12_gen_mac(PKCS12
*p12
, const char *pass
, int passlen
,
69 unsigned char *mac
, unsigned int *maclen
)
71 const EVP_MD
*md_type
;
72 HMAC_CTX
*hmac
= NULL
;
73 unsigned char key
[EVP_MAX_MD_SIZE
], *salt
;
80 if (!PKCS7_type_is_data(p12
->authsafes
)) {
81 PKCS12err(PKCS12_F_PKCS12_GEN_MAC
, PKCS12_R_CONTENT_TYPE_NOT_DATA
);
85 salt
= p12
->mac
->salt
->data
;
86 saltlen
= p12
->mac
->salt
->length
;
90 iter
= ASN1_INTEGER_get(p12
->mac
->iter
);
91 X509_SIG_get0(&macalg
, NULL
, p12
->mac
->dinfo
);
92 X509_ALGOR_get0(&macoid
, NULL
, NULL
, macalg
);
93 if ((md_type
= EVP_get_digestbyobj(macoid
)) == NULL
) {
94 PKCS12err(PKCS12_F_PKCS12_GEN_MAC
, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM
);
97 md_size
= EVP_MD_size(md_type
);
98 md_type_nid
= EVP_MD_type(md_type
);
101 if ((md_type_nid
== NID_id_GostR3411_94
102 || md_type_nid
== NID_id_GostR3411_2012_256
103 || md_type_nid
== NID_id_GostR3411_2012_512
)
104 && !getenv("LEGACY_GOST_PKCS12")) {
105 md_size
= TK26_MAC_KEY_LEN
;
106 if (!pkcs12_gen_gost_mac_key(pass
, passlen
, salt
, saltlen
, iter
,
107 md_size
, key
, md_type
)) {
108 PKCS12err(PKCS12_F_PKCS12_GEN_MAC
, PKCS12_R_KEY_GEN_ERROR
);
112 if (!PKCS12_key_gen(pass
, passlen
, salt
, saltlen
, PKCS12_MAC_ID
, iter
,
113 md_size
, key
, md_type
)) {
114 PKCS12err(PKCS12_F_PKCS12_GEN_MAC
, PKCS12_R_KEY_GEN_ERROR
);
117 hmac
= HMAC_CTX_new();
118 if (!HMAC_Init_ex(hmac
, key
, md_size
, md_type
, NULL
)
119 || !HMAC_Update(hmac
, p12
->authsafes
->d
.data
->data
,
120 p12
->authsafes
->d
.data
->length
)
121 || !HMAC_Final(hmac
, mac
, maclen
)) {
130 int PKCS12_verify_mac(PKCS12
*p12
, const char *pass
, int passlen
)
132 unsigned char mac
[EVP_MAX_MD_SIZE
];
134 ASN1_OCTET_STRING
*macoct
;
136 if (p12
->mac
== NULL
) {
137 PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC
, PKCS12_R_MAC_ABSENT
);
140 if (!PKCS12_gen_mac(p12
, pass
, passlen
, mac
, &maclen
)) {
141 PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC
, PKCS12_R_MAC_GENERATION_ERROR
);
144 X509_SIG_get0(NULL
, &macoct
, p12
->mac
->dinfo
);
145 if ((maclen
!= (unsigned int)ASN1_STRING_length(macoct
))
146 || CRYPTO_memcmp(mac
, ASN1_STRING_data(macoct
), maclen
))
153 int PKCS12_set_mac(PKCS12
*p12
, const char *pass
, int passlen
,
154 unsigned char *salt
, int saltlen
, int iter
,
155 const EVP_MD
*md_type
)
157 unsigned char mac
[EVP_MAX_MD_SIZE
];
159 ASN1_OCTET_STRING
*macoct
;
162 md_type
= EVP_sha1();
163 if (PKCS12_setup_mac(p12
, iter
, salt
, saltlen
, md_type
) == PKCS12_ERROR
) {
164 PKCS12err(PKCS12_F_PKCS12_SET_MAC
, PKCS12_R_MAC_SETUP_ERROR
);
167 if (!PKCS12_gen_mac(p12
, pass
, passlen
, mac
, &maclen
)) {
168 PKCS12err(PKCS12_F_PKCS12_SET_MAC
, PKCS12_R_MAC_GENERATION_ERROR
);
171 X509_SIG_get0(NULL
, &macoct
, p12
->mac
->dinfo
);
172 if (!ASN1_OCTET_STRING_set(macoct
, mac
, maclen
)) {
173 PKCS12err(PKCS12_F_PKCS12_SET_MAC
, PKCS12_R_MAC_STRING_SET_ERROR
);
179 /* Set up a mac structure */
180 int PKCS12_setup_mac(PKCS12
*p12
, int iter
, unsigned char *salt
, int saltlen
,
181 const EVP_MD
*md_type
)
185 if ((p12
->mac
= PKCS12_MAC_DATA_new()) == NULL
)
188 if ((p12
->mac
->iter
= ASN1_INTEGER_new()) == NULL
) {
189 PKCS12err(PKCS12_F_PKCS12_SETUP_MAC
, ERR_R_MALLOC_FAILURE
);
192 if (!ASN1_INTEGER_set(p12
->mac
->iter
, iter
)) {
193 PKCS12err(PKCS12_F_PKCS12_SETUP_MAC
, ERR_R_MALLOC_FAILURE
);
198 saltlen
= PKCS12_SALT_LEN
;
199 if ((p12
->mac
->salt
->data
= OPENSSL_malloc(saltlen
)) == NULL
) {
200 PKCS12err(PKCS12_F_PKCS12_SETUP_MAC
, ERR_R_MALLOC_FAILURE
);
203 p12
->mac
->salt
->length
= saltlen
;
205 if (RAND_bytes(p12
->mac
->salt
->data
, saltlen
) <= 0)
208 memcpy(p12
->mac
->salt
->data
, salt
, saltlen
);
209 X509_SIG_get0(&macalg
, NULL
, p12
->mac
->dinfo
);
210 if (!X509_ALGOR_set0(macalg
, OBJ_nid2obj(EVP_MD_type(md_type
)),
211 V_ASN1_NULL
, NULL
)) {
212 PKCS12err(PKCS12_F_PKCS12_SETUP_MAC
, ERR_R_MALLOC_FAILURE
);