]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/pkcs12/p12_mutl.c
Reorganize local header files
[thirdparty/openssl.git] / crypto / pkcs12 / p12_mutl.c
CommitLineData
0f113f3e 1/*
1212818e 2 * Copyright 1999-2018 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
5c39a55d
P
10#include <stdio.h>
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>
706457b7 16#include "p12_local.h"
8d8c7266 17
28da1455 18int PKCS12_mac_present(const PKCS12 *p12)
293042c9 19{
c9018bdf 20 return p12->mac ? 1 : 0;
293042c9
DSH
21}
22
59b4da05
DSH
23void PKCS12_get0_mac(const ASN1_OCTET_STRING **pmac,
24 const X509_ALGOR **pmacalg,
25 const ASN1_OCTET_STRING **psalt,
26 const ASN1_INTEGER **piter,
27 const PKCS12 *p12)
293042c9
DSH
28{
29 if (p12->mac) {
59b4da05 30 X509_SIG_get0(p12->mac->dinfo, pmacalg, pmac);
293042c9
DSH
31 if (psalt)
32 *psalt = p12->mac->salt;
33 if (piter)
34 *piter = p12->mac->iter;
35 } else {
36 if (pmac)
37 *pmac = NULL;
38 if (pmacalg)
39 *pmacalg = NULL;
40 if (psalt)
41 *psalt = NULL;
42 if (piter)
43 *piter = NULL;
44 }
45}
46
5c39a55d 47#define TK26_MAC_KEY_LEN 32
77886387
MC
48
49static 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,
52 const EVP_MD *digest)
53{
54 unsigned char out[96];
55
56 if (keylen != TK26_MAC_KEY_LEN) {
57 return 0;
58 }
59
60 if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter,
61 digest, sizeof(out), out)) {
62 return 0;
63 }
64 memcpy(key, out + sizeof(out) - TK26_MAC_KEY_LEN, TK26_MAC_KEY_LEN);
65 OPENSSL_cleanse(out, sizeof(out));
66 return 1;
67}
68
8d8c7266 69/* Generate a MAC */
1194ea8d
AP
70static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
71 unsigned char *mac, unsigned int *maclen,
72 int (*pkcs12_key_gen)(const char *pass, int passlen,
73 unsigned char *salt, int slen,
74 int id, int iter, int n,
75 unsigned char *out,
76 const EVP_MD *md_type))
8d8c7266 77{
f5cee414 78 int ret = 0;
0f113f3e 79 const EVP_MD *md_type;
bf7c6817 80 HMAC_CTX *hmac = NULL;
0f113f3e
MC
81 unsigned char key[EVP_MAX_MD_SIZE], *salt;
82 int saltlen, iter;
77886387
MC
83 int md_size = 0;
84 int md_type_nid;
59b4da05 85 const X509_ALGOR *macalg;
ac4e2577 86 const ASN1_OBJECT *macoid;
dbad1690 87
1194ea8d
AP
88 if (pkcs12_key_gen == NULL)
89 pkcs12_key_gen = PKCS12_key_gen_utf8;
90
0f113f3e
MC
91 if (!PKCS7_type_is_data(p12->authsafes)) {
92 PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_CONTENT_TYPE_NOT_DATA);
93 return 0;
94 }
1c2f1fe5 95
0f113f3e
MC
96 salt = p12->mac->salt->data;
97 saltlen = p12->mac->salt->length;
98 if (!p12->mac->iter)
99 iter = 1;
100 else
101 iter = ASN1_INTEGER_get(p12->mac->iter);
59b4da05 102 X509_SIG_get0(p12->mac->dinfo, &macalg, NULL);
a6eb1ce6
DSH
103 X509_ALGOR_get0(&macoid, NULL, NULL, macalg);
104 if ((md_type = EVP_get_digestbyobj(macoid)) == NULL) {
0f113f3e
MC
105 PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
106 return 0;
107 }
108 md_size = EVP_MD_size(md_type);
77886387 109 md_type_nid = EVP_MD_type(md_type);
0f113f3e
MC
110 if (md_size < 0)
111 return 0;
77886387
MC
112 if ((md_type_nid == NID_id_GostR3411_94
113 || md_type_nid == NID_id_GostR3411_2012_256
114 || md_type_nid == NID_id_GostR3411_2012_512)
5c39a55d 115 && ossl_safe_getenv("LEGACY_GOST_PKCS12") == NULL) {
77886387
MC
116 md_size = TK26_MAC_KEY_LEN;
117 if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter,
118 md_size, key, md_type)) {
119 PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
f5cee414 120 goto err;
77886387
MC
121 }
122 } else
1194ea8d
AP
123 if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID,
124 iter, md_size, key, md_type)) {
0f113f3e 125 PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
f5cee414 126 goto err;
0f113f3e 127 }
d3c3dfc5
RS
128 if ((hmac = HMAC_CTX_new()) == NULL
129 || !HMAC_Init_ex(hmac, key, md_size, md_type, NULL)
bf7c6817 130 || !HMAC_Update(hmac, p12->authsafes->d.data->data,
0f113f3e 131 p12->authsafes->d.data->length)
bf7c6817 132 || !HMAC_Final(hmac, mac, maclen)) {
f5cee414 133 goto err;
0f113f3e 134 }
f5cee414
SL
135 ret = 1;
136
137err:
138 OPENSSL_cleanse(key, sizeof(key));
bf7c6817 139 HMAC_CTX_free(hmac);
f5cee414 140 return ret;
8d8c7266
DSH
141}
142
1194ea8d
AP
143int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
144 unsigned char *mac, unsigned int *maclen)
145{
146 return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NULL);
147}
148
8d8c7266 149/* Verify the mac */
8afca8d9 150int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
8d8c7266 151{
0f113f3e
MC
152 unsigned char mac[EVP_MAX_MD_SIZE];
153 unsigned int maclen;
59b4da05 154 const ASN1_OCTET_STRING *macoct;
a6eb1ce6 155
0f113f3e
MC
156 if (p12->mac == NULL) {
157 PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_ABSENT);
158 return 0;
159 }
1194ea8d
AP
160 if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
161 PKCS12_key_gen_utf8)) {
0f113f3e
MC
162 PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_GENERATION_ERROR);
163 return 0;
164 }
59b4da05 165 X509_SIG_get0(p12->mac->dinfo, NULL, &macoct);
0fe17491
AP
166 if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
167 || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
0f113f3e 168 return 0;
1194ea8d 169
0f113f3e 170 return 1;
8d8c7266
DSH
171}
172
173/* Set a mac */
174
8afca8d9 175int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
0f113f3e
MC
176 unsigned char *salt, int saltlen, int iter,
177 const EVP_MD *md_type)
8d8c7266 178{
0f113f3e
MC
179 unsigned char mac[EVP_MAX_MD_SIZE];
180 unsigned int maclen;
a6eb1ce6 181 ASN1_OCTET_STRING *macoct;
61f5b6f3 182
0f113f3e
MC
183 if (!md_type)
184 md_type = EVP_sha1();
185 if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) {
186 PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_SETUP_ERROR);
187 return 0;
188 }
1194ea8d
AP
189 /*
190 * Note that output mac is forced to UTF-8...
191 */
192 if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
193 PKCS12_key_gen_utf8)) {
0f113f3e
MC
194 PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_GENERATION_ERROR);
195 return 0;
196 }
0b7347ef 197 X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
a6eb1ce6 198 if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) {
0f113f3e
MC
199 PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_STRING_SET_ERROR);
200 return 0;
201 }
202 return 1;
8d8c7266
DSH
203}
204
205/* Set up a mac structure */
8afca8d9 206int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
0f113f3e 207 const EVP_MD *md_type)
8d8c7266 208{
a6eb1ce6
DSH
209 X509_ALGOR *macalg;
210
1b8f1937
DB
211 PKCS12_MAC_DATA_free(p12->mac);
212 p12->mac = NULL;
213
75ebbd9a 214 if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
0f113f3e
MC
215 return PKCS12_ERROR;
216 if (iter > 1) {
75ebbd9a 217 if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) {
0f113f3e
MC
218 PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
219 return 0;
220 }
221 if (!ASN1_INTEGER_set(p12->mac->iter, iter)) {
222 PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
223 return 0;
224 }
225 }
226 if (!saltlen)
227 saltlen = PKCS12_SALT_LEN;
75ebbd9a 228 if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL) {
0f113f3e
MC
229 PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
230 return 0;
231 }
c2319cf9 232 p12->mac->salt->length = saltlen;
0f113f3e 233 if (!salt) {
266483d2 234 if (RAND_bytes(p12->mac->salt->data, saltlen) <= 0)
0f113f3e
MC
235 return 0;
236 } else
237 memcpy(p12->mac->salt->data, salt, saltlen);
0b7347ef 238 X509_SIG_getm(p12->mac->dinfo, &macalg, NULL);
a6eb1ce6
DSH
239 if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(EVP_MD_type(md_type)),
240 V_ASN1_NULL, NULL)) {
0f113f3e
MC
241 PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
242 return 0;
243 }
0f113f3e
MC
244
245 return 1;
8d8c7266 246}