325da0c207808b257432c03fce4898707b230b59
[openssl.git] / crypto / pkcs12 / p12_mutl.c
1 /*
2  * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
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>
16 # include "p12_lcl.h"
17
18 int PKCS12_mac_present(const PKCS12 *p12)
19 {
20     return p12->mac ? 1 : 0;
21 }
22
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,
27                      const PKCS12 *p12)
28 {
29     if (p12->mac) {
30         X509_SIG_get0(p12->mac->dinfo, pmacalg, pmac);
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
47 # define TK26_MAC_KEY_LEN 32
48
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,
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
69 #undef PKCS12_key_gen
70 /*
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.
87  */
88 int (*PKCS12_key_gen)(const char *pass, int passlen,
89                       unsigned char *salt, int slen,
90                       int id, int iter, int n,
91                       unsigned char *out,
92                       const EVP_MD *md_type) = NULL;
93
94
95 /* Generate a MAC */
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,
101                                                 unsigned char *out,
102                                                 const EVP_MD *md_type))
103 {
104     const EVP_MD *md_type;
105     HMAC_CTX *hmac = NULL;
106     unsigned char key[EVP_MAX_MD_SIZE], *salt;
107     int saltlen, iter;
108     int md_size = 0;
109     int md_type_nid;
110     const X509_ALGOR *macalg;
111     const ASN1_OBJECT *macoid;
112
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;
117
118     if (!PKCS7_type_is_data(p12->authsafes)) {
119         PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_CONTENT_TYPE_NOT_DATA);
120         return 0;
121     }
122
123     salt = p12->mac->salt->data;
124     saltlen = p12->mac->salt->length;
125     if (!p12->mac->iter)
126         iter = 1;
127     else
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);
133         return 0;
134     }
135     md_size = EVP_MD_size(md_type);
136     md_type_nid = EVP_MD_type(md_type);
137     if (md_size < 0)
138         return 0;
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);
147             return 0;
148         }
149     } else
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);
153         return 0;
154     }
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)) {
160         HMAC_CTX_free(hmac);
161         return 0;
162     }
163     HMAC_CTX_free(hmac);
164     return 1;
165 }
166
167 int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
168                    unsigned char *mac, unsigned int *maclen)
169 {
170     return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NULL);
171 }
172
173 /* Verify the mac */
174 int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
175 {
176     unsigned char mac[EVP_MAX_MD_SIZE];
177     unsigned int maclen;
178     const ASN1_OCTET_STRING *macoct;
179
180     if (p12->mac == NULL) {
181         PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_ABSENT);
182         return 0;
183     }
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);
187         return 0;
188     }
189     X509_SIG_get0(p12->mac->dinfo, NULL, &macoct);
190     if (maclen != (unsigned int)ASN1_STRING_length(macoct))
191         return 0;
192
193     if (CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0) {
194         if (pass == NULL)
195             return 0;
196         /*
197          * In order to facilitate accessing old data retry with
198          * old-style broken password ...
199          */
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);
203             return 0;
204         }
205         if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
206             || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
207             return 0;
208         else
209             PKCS12_key_gen = PKCS12_key_gen_asc;
210         /*
211          * ... and if suceeded, pass it on to PKCS12_PBE_keyivgen.
212          */
213     }
214     return 1;
215 }
216
217 /* Set a mac */
218
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)
222 {
223     unsigned char mac[EVP_MAX_MD_SIZE];
224     unsigned int maclen;
225     ASN1_OCTET_STRING *macoct;
226
227     if (!md_type)
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);
231         return 0;
232     }
233     /*
234      * Note that output mac is forced to UTF-8...
235      */
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);
239         return 0;
240     }
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);
244         return 0;
245     }
246     return 1;
247 }
248
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)
252 {
253     X509_ALGOR *macalg;
254
255     if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
256         return PKCS12_ERROR;
257     if (iter > 1) {
258         if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) {
259             PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
260             return 0;
261         }
262         if (!ASN1_INTEGER_set(p12->mac->iter, iter)) {
263             PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
264             return 0;
265         }
266     }
267     if (!saltlen)
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);
271         return 0;
272     }
273     p12->mac->salt->length = saltlen;
274     if (!salt) {
275         if (RAND_bytes(p12->mac->salt->data, saltlen) <= 0)
276             return 0;
277     } else
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);
283         return 0;
284     }
285
286     return 1;
287 }