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(const PKCS12 *p12)
20 return p12->mac ? 1 : 0;
23 void PKCS12_get0_mac(const ASN1_OCTET_STRING **pmac,
24 const X509_ALGOR **pmacalg,
25 const ASN1_OCTET_STRING **psalt,
26 const ASN1_INTEGER **piter,
30 X509_SIG_get0(p12->mac->dinfo, pmacalg, pmac);
32 *psalt = p12->mac->salt;
34 *piter = p12->mac->iter;
47 # define TK26_MAC_KEY_LEN 32
49 static int pkcs12_gen_gost_mac_key(const char *pass, int passlen,
50 const unsigned char *salt, int saltlen,
51 int iter, int keylen, unsigned char *key,
54 unsigned char out[96];
56 if (keylen != TK26_MAC_KEY_LEN) {
60 if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter,
61 digest, sizeof(out), out)) {
64 memcpy(key, out + sizeof(out) - TK26_MAC_KEY_LEN, TK26_MAC_KEY_LEN);
65 OPENSSL_cleanse(out, sizeof(out));
71 * |PKCS12_key_gen| is used to convey information about old-style broken
72 * password being used to PKCS12_PBE_keyivgen in decrypt cases. Workflow
73 * is if PKCS12_verify_mac notes that password encoded with compliant
74 * PKCS12_key_gen_utf8 conversion subroutine isn't right, while encoded
75 * with legacy non-compliant one is, then it sets |PKCS12_key_gen| to
76 * legacy PKCS12_key_gen_asc conversion subroutine, which is then picked
77 * by PKCS12_PBE_keyivgen. This applies to reading data. Written data
78 * on the other hand is protected with standard-compliant encoding, i.e.
79 * in backward-incompatible manner. Note that formally the approach is
80 * not MT-safe. Rationale is that in order to access PKCS#12 files from
81 * MT or even production application, you would be required to convert
82 * data to correct interoperable format. In which case this variable
83 * won't have to change. Conversion would have to be done with pkcs12
84 * utility, which is not MT, and hence can tolerate it. In other words
85 * goal is not to make this heuristic approach work in general case,
86 * but in one specific one, apps/pkcs12.c.
88 int (*PKCS12_key_gen)(const char *pass, int passlen,
89 unsigned char *salt, int slen,
90 int id, int iter, int n,
92 const EVP_MD *md_type) = NULL;
96 static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
97 unsigned char *mac, unsigned int *maclen,
98 int (*pkcs12_key_gen)(const char *pass, int passlen,
99 unsigned char *salt, int slen,
100 int id, int iter, int n,
102 const EVP_MD *md_type))
104 const EVP_MD *md_type;
105 HMAC_CTX *hmac = NULL;
106 unsigned char key[EVP_MAX_MD_SIZE], *salt;
110 const X509_ALGOR *macalg;
111 const ASN1_OBJECT *macoid;
113 if (pkcs12_key_gen == NULL)
114 pkcs12_key_gen = PKCS12_key_gen;
115 if (pkcs12_key_gen == NULL)
116 pkcs12_key_gen = PKCS12_key_gen_utf8;
118 if (!PKCS7_type_is_data(p12->authsafes)) {
119 PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_CONTENT_TYPE_NOT_DATA);
123 salt = p12->mac->salt->data;
124 saltlen = p12->mac->salt->length;
128 iter = ASN1_INTEGER_get(p12->mac->iter);
129 X509_SIG_get0(p12->mac->dinfo, &macalg, NULL);
130 X509_ALGOR_get0(&macoid, NULL, NULL, macalg);
131 if ((md_type = EVP_get_digestbyobj(macoid)) == NULL) {
132 PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
135 md_size = EVP_MD_size(md_type);
136 md_type_nid = EVP_MD_type(md_type);
139 if ((md_type_nid == NID_id_GostR3411_94
140 || md_type_nid == NID_id_GostR3411_2012_256
141 || md_type_nid == NID_id_GostR3411_2012_512)
142 && !getenv("LEGACY_GOST_PKCS12")) {
143 md_size = TK26_MAC_KEY_LEN;
144 if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter,
145 md_size, key, md_type)) {
146 PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
150 if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID,
151 iter, md_size, key, md_type)) {
152 PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
155 hmac = HMAC_CTX_new();
156 if (!HMAC_Init_ex(hmac, key, md_size, md_type, NULL)
157 || !HMAC_Update(hmac, p12->authsafes->d.data->data,
158 p12->authsafes->d.data->length)
159 || !HMAC_Final(hmac, mac, maclen)) {
167 int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
168 unsigned char *mac, unsigned int *maclen)
170 return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NULL);
174 int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
176 unsigned char mac[EVP_MAX_MD_SIZE];
178 const ASN1_OCTET_STRING *macoct;
180 if (p12->mac == NULL) {
181 PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_ABSENT);
184 if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
185 PKCS12_key_gen_utf8)) {
186 PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_GENERATION_ERROR);
189 X509_SIG_get0(p12->mac->dinfo, NULL, &macoct);
190 if (maclen != (unsigned int)ASN1_STRING_length(macoct))
193 if (CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0) {
197 * In order to facilitate accessing old data retry with
198 * old-style broken password ...
200 if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
201 PKCS12_key_gen_asc)) {
202 PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_GENERATION_ERROR);
205 if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
206 || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
209 PKCS12_key_gen = PKCS12_key_gen_asc;
211 * ... and if suceeded, pass it on to PKCS12_PBE_keyivgen.
219 int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
220 unsigned char *salt, int saltlen, int iter,
221 const EVP_MD *md_type)
223 unsigned char mac[EVP_MAX_MD_SIZE];
225 ASN1_OCTET_STRING *macoct;
228 md_type = EVP_sha1();
229 if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) {
230 PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_SETUP_ERROR);
234 * Note that output mac is forced to UTF-8...
236 if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
237 PKCS12_key_gen_utf8)) {
238 PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_GENERATION_ERROR);
241 X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
242 if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) {
243 PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_STRING_SET_ERROR);
249 /* Set up a mac structure */
250 int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
251 const EVP_MD *md_type)
255 if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
258 if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) {
259 PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
262 if (!ASN1_INTEGER_set(p12->mac->iter, iter)) {
263 PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
268 saltlen = PKCS12_SALT_LEN;
269 if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL) {
270 PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
273 p12->mac->salt->length = saltlen;
275 if (RAND_bytes(p12->mac->salt->data, saltlen) <= 0)
278 memcpy(p12->mac->salt->data, salt, saltlen);
279 X509_SIG_getm(p12->mac->dinfo, &macalg, NULL);
280 if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(EVP_MD_type(md_type)),
281 V_ASN1_NULL, NULL)) {
282 PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);