]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/pkcs12/p12_mutl.c
Convert all {NAME}err() in crypto/ to their corresponding ERR_raise() call
[thirdparty/openssl.git] / crypto / pkcs12 / p12_mutl.c
1 /*
2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
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
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>
22 #include "p12_local.h"
23
24 int PKCS12_mac_present(const PKCS12 *p12)
25 {
26 return p12->mac ? 1 : 0;
27 }
28
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,
33 const PKCS12 *p12)
34 {
35 if (p12->mac) {
36 X509_SIG_get0(p12->mac->dinfo, pmacalg, pmac);
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
53 #define TK26_MAC_KEY_LEN 32
54
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,
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
75 /* Generate a MAC */
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,
81 unsigned char *out,
82 const EVP_MD *md_type))
83 {
84 int ret = 0;
85 const EVP_MD *md_type;
86 HMAC_CTX *hmac = NULL;
87 unsigned char key[EVP_MAX_MD_SIZE], *salt;
88 int saltlen, iter;
89 int md_size = 0;
90 int md_type_nid;
91 const X509_ALGOR *macalg;
92 const ASN1_OBJECT *macoid;
93
94 if (pkcs12_key_gen == NULL)
95 pkcs12_key_gen = PKCS12_key_gen_utf8;
96
97 if (!PKCS7_type_is_data(p12->authsafes)) {
98 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
99 return 0;
100 }
101
102 salt = p12->mac->salt->data;
103 saltlen = p12->mac->salt->length;
104 if (p12->mac->iter == NULL)
105 iter = 1;
106 else
107 iter = ASN1_INTEGER_get(p12->mac->iter);
108 X509_SIG_get0(p12->mac->dinfo, &macalg, NULL);
109 X509_ALGOR_get0(&macoid, NULL, NULL, macalg);
110 if ((md_type = EVP_get_digestbyobj(macoid)) == NULL) {
111 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
112 return 0;
113 }
114 md_size = EVP_MD_size(md_type);
115 md_type_nid = EVP_MD_type(md_type);
116 if (md_size < 0)
117 return 0;
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)
121 && ossl_safe_getenv("LEGACY_GOST_PKCS12") == NULL) {
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)) {
125 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR);
126 goto err;
127 }
128 } else
129 if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID,
130 iter, md_size, key, md_type)) {
131 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR);
132 goto err;
133 }
134 if ((hmac = HMAC_CTX_new()) == NULL
135 || !HMAC_Init_ex(hmac, key, md_size, md_type, NULL)
136 || !HMAC_Update(hmac, p12->authsafes->d.data->data,
137 p12->authsafes->d.data->length)
138 || !HMAC_Final(hmac, mac, maclen)) {
139 goto err;
140 }
141 ret = 1;
142
143 err:
144 OPENSSL_cleanse(key, sizeof(key));
145 HMAC_CTX_free(hmac);
146 return ret;
147 }
148
149 int 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
155 /* Verify the mac */
156 int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
157 {
158 unsigned char mac[EVP_MAX_MD_SIZE];
159 unsigned int maclen;
160 const ASN1_OCTET_STRING *macoct;
161
162 if (p12->mac == NULL) {
163 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_ABSENT);
164 return 0;
165 }
166 if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
167 PKCS12_key_gen_utf8)) {
168 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR);
169 return 0;
170 }
171 X509_SIG_get0(p12->mac->dinfo, NULL, &macoct);
172 if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
173 || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
174 return 0;
175
176 return 1;
177 }
178
179 /* Set a mac */
180
181 int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
182 unsigned char *salt, int saltlen, int iter,
183 const EVP_MD *md_type)
184 {
185 unsigned char mac[EVP_MAX_MD_SIZE];
186 unsigned int maclen;
187 ASN1_OCTET_STRING *macoct;
188
189 if (!md_type)
190 md_type = EVP_sha1();
191 if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) {
192 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR);
193 return 0;
194 }
195 /*
196 * Note that output mac is forced to UTF-8...
197 */
198 if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
199 PKCS12_key_gen_utf8)) {
200 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR);
201 return 0;
202 }
203 X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
204 if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) {
205 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_STRING_SET_ERROR);
206 return 0;
207 }
208 return 1;
209 }
210
211 /* Set up a mac structure */
212 int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
213 const EVP_MD *md_type)
214 {
215 X509_ALGOR *macalg;
216
217 PKCS12_MAC_DATA_free(p12->mac);
218 p12->mac = NULL;
219
220 if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
221 return PKCS12_ERROR;
222 if (iter > 1) {
223 if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) {
224 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
225 return 0;
226 }
227 if (!ASN1_INTEGER_set(p12->mac->iter, iter)) {
228 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
229 return 0;
230 }
231 }
232 if (!saltlen)
233 saltlen = PKCS12_SALT_LEN;
234 if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL) {
235 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
236 return 0;
237 }
238 p12->mac->salt->length = saltlen;
239 if (!salt) {
240 if (RAND_bytes(p12->mac->salt->data, saltlen) <= 0)
241 return 0;
242 } else
243 memcpy(p12->mac->salt->data, salt, saltlen);
244 X509_SIG_getm(p12->mac->dinfo, &macalg, NULL);
245 if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(EVP_MD_type(md_type)),
246 V_ASN1_NULL, NULL)) {
247 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
248 return 0;
249 }
250
251 return 1;
252 }