]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/pkcs12/p12_mutl.c
Change default algorithms in PKCS12_create() and PKCS12_set_mac()
[thirdparty/openssl.git] / crypto / pkcs12 / p12_mutl.c
CommitLineData
0f113f3e 1/*
33388b44 2 * Copyright 1999-2020 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 24int PKCS12_mac_present(const PKCS12 *p12)
293042c9 25{
c9018bdf 26 return p12->mac ? 1 : 0;
293042c9
DSH
27}
28
59b4da05
DSH
29void 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
55static 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
76static 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;
0f113f3e 85 const EVP_MD *md_type;
bf7c6817 86 HMAC_CTX *hmac = NULL;
0f113f3e
MC
87 unsigned char key[EVP_MAX_MD_SIZE], *salt;
88 int saltlen, iter;
77886387
MC
89 int md_size = 0;
90 int md_type_nid;
59b4da05 91 const X509_ALGOR *macalg;
ac4e2577 92 const ASN1_OBJECT *macoid;
dbad1690 93
1194ea8d
AP
94 if (pkcs12_key_gen == NULL)
95 pkcs12_key_gen = PKCS12_key_gen_utf8;
96
0f113f3e 97 if (!PKCS7_type_is_data(p12->authsafes)) {
9311d0c4 98 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
0f113f3e
MC
99 return 0;
100 }
1c2f1fe5 101
0f113f3e
MC
102 salt = p12->mac->salt->data;
103 saltlen = p12->mac->salt->length;
12a765a5 104 if (p12->mac->iter == NULL)
0f113f3e
MC
105 iter = 1;
106 else
107 iter = ASN1_INTEGER_get(p12->mac->iter);
59b4da05 108 X509_SIG_get0(p12->mac->dinfo, &macalg, NULL);
a6eb1ce6
DSH
109 X509_ALGOR_get0(&macoid, NULL, NULL, macalg);
110 if ((md_type = EVP_get_digestbyobj(macoid)) == NULL) {
9311d0c4 111 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
0f113f3e
MC
112 return 0;
113 }
114 md_size = EVP_MD_size(md_type);
77886387 115 md_type_nid = EVP_MD_type(md_type);
0f113f3e
MC
116 if (md_size < 0)
117 return 0;
77886387
MC
118 if ((md_type_nid == NID_id_GostR3411_94
119 || md_type_nid == NID_id_GostR3411_2012_256
120 || md_type_nid == NID_id_GostR3411_2012_512)
5c39a55d 121 && ossl_safe_getenv("LEGACY_GOST_PKCS12") == NULL) {
77886387
MC
122 md_size = TK26_MAC_KEY_LEN;
123 if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter,
124 md_size, key, md_type)) {
9311d0c4 125 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR);
f5cee414 126 goto err;
77886387
MC
127 }
128 } else
1194ea8d
AP
129 if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID,
130 iter, md_size, key, md_type)) {
9311d0c4 131 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR);
f5cee414 132 goto err;
0f113f3e 133 }
d3c3dfc5
RS
134 if ((hmac = HMAC_CTX_new()) == NULL
135 || !HMAC_Init_ex(hmac, key, md_size, md_type, NULL)
bf7c6817 136 || !HMAC_Update(hmac, p12->authsafes->d.data->data,
0f113f3e 137 p12->authsafes->d.data->length)
bf7c6817 138 || !HMAC_Final(hmac, mac, maclen)) {
f5cee414 139 goto err;
0f113f3e 140 }
f5cee414
SL
141 ret = 1;
142
143err:
144 OPENSSL_cleanse(key, sizeof(key));
bf7c6817 145 HMAC_CTX_free(hmac);
f5cee414 146 return ret;
8d8c7266
DSH
147}
148
1194ea8d
AP
149int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
150 unsigned char *mac, unsigned int *maclen)
151{
152 return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NULL);
153}
154
8d8c7266 155/* Verify the mac */
8afca8d9 156int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
8d8c7266 157{
0f113f3e
MC
158 unsigned char mac[EVP_MAX_MD_SIZE];
159 unsigned int maclen;
59b4da05 160 const ASN1_OCTET_STRING *macoct;
a6eb1ce6 161
0f113f3e 162 if (p12->mac == NULL) {
9311d0c4 163 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_ABSENT);
0f113f3e
MC
164 return 0;
165 }
1194ea8d
AP
166 if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
167 PKCS12_key_gen_utf8)) {
9311d0c4 168 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR);
0f113f3e
MC
169 return 0;
170 }
59b4da05 171 X509_SIG_get0(p12->mac->dinfo, NULL, &macoct);
0fe17491
AP
172 if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
173 || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
0f113f3e 174 return 0;
1194ea8d 175
0f113f3e 176 return 1;
8d8c7266
DSH
177}
178
179/* Set a mac */
180
8afca8d9 181int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
0f113f3e
MC
182 unsigned char *salt, int saltlen, int iter,
183 const EVP_MD *md_type)
8d8c7266 184{
0f113f3e
MC
185 unsigned char mac[EVP_MAX_MD_SIZE];
186 unsigned int maclen;
a6eb1ce6 187 ASN1_OCTET_STRING *macoct;
61f5b6f3 188
762970bd
TM
189 if (md_type == NULL)
190 /* No need to do a fetch as the md_type is used only to get a NID */
191 md_type = EVP_sha256();
192 if (!iter)
193 iter = PKCS12_DEFAULT_ITER;
0f113f3e 194 if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) {
9311d0c4 195 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR);
0f113f3e
MC
196 return 0;
197 }
1194ea8d
AP
198 /*
199 * Note that output mac is forced to UTF-8...
200 */
201 if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
202 PKCS12_key_gen_utf8)) {
9311d0c4 203 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR);
0f113f3e
MC
204 return 0;
205 }
0b7347ef 206 X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
a6eb1ce6 207 if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) {
9311d0c4 208 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_STRING_SET_ERROR);
0f113f3e
MC
209 return 0;
210 }
211 return 1;
8d8c7266
DSH
212}
213
214/* Set up a mac structure */
8afca8d9 215int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
0f113f3e 216 const EVP_MD *md_type)
8d8c7266 217{
a6eb1ce6
DSH
218 X509_ALGOR *macalg;
219
1b8f1937
DB
220 PKCS12_MAC_DATA_free(p12->mac);
221 p12->mac = NULL;
222
75ebbd9a 223 if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
0f113f3e
MC
224 return PKCS12_ERROR;
225 if (iter > 1) {
75ebbd9a 226 if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) {
9311d0c4 227 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
228 return 0;
229 }
230 if (!ASN1_INTEGER_set(p12->mac->iter, iter)) {
9311d0c4 231 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
232 return 0;
233 }
234 }
235 if (!saltlen)
236 saltlen = PKCS12_SALT_LEN;
75ebbd9a 237 if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL) {
9311d0c4 238 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
239 return 0;
240 }
c2319cf9 241 p12->mac->salt->length = saltlen;
0f113f3e 242 if (!salt) {
266483d2 243 if (RAND_bytes(p12->mac->salt->data, saltlen) <= 0)
0f113f3e
MC
244 return 0;
245 } else
246 memcpy(p12->mac->salt->data, salt, saltlen);
0b7347ef 247 X509_SIG_getm(p12->mac->dinfo, &macalg, NULL);
a6eb1ce6
DSH
248 if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(EVP_MD_type(md_type)),
249 V_ASN1_NULL, NULL)) {
9311d0c4 250 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
251 return 0;
252 }
0f113f3e
MC
253
254 return 1;
8d8c7266 255}