Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/pkcs12/p12_mutl.c
Line
Count
Source (jump to first uncovered line)
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
0
{
20
0
    return p12->mac ? 1 : 0;
21
0
}
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
0
{
29
0
    if (p12->mac) {
30
0
        X509_SIG_get0(p12->mac->dinfo, pmacalg, pmac);
31
0
        if (psalt)
32
0
            *psalt = p12->mac->salt;
33
0
        if (piter)
34
0
            *piter = p12->mac->iter;
35
0
    } else {
36
0
        if (pmac)
37
0
            *pmac = NULL;
38
0
        if (pmacalg)
39
0
            *pmacalg = NULL;
40
0
        if (psalt)
41
0
            *psalt = NULL;
42
0
        if (piter)
43
0
            *piter = NULL;
44
0
    }
45
0
}
46
47
0
# 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
0
{
54
0
    unsigned char out[96];
55
0
56
0
    if (keylen != TK26_MAC_KEY_LEN) {
57
0
        return 0;
58
0
    }
59
0
60
0
    if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter,
61
0
                           digest, sizeof(out), out)) {
62
0
        return 0;
63
0
    }
64
0
    memcpy(key, out + sizeof(out) - TK26_MAC_KEY_LEN, TK26_MAC_KEY_LEN);
65
0
    OPENSSL_cleanse(out, sizeof(out));
66
0
    return 1;
67
0
}
68
69
/* Generate a MAC */
70
static 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))
77
0
{
78
0
    const EVP_MD *md_type;
79
0
    HMAC_CTX *hmac = NULL;
80
0
    unsigned char key[EVP_MAX_MD_SIZE], *salt;
81
0
    int saltlen, iter;
82
0
    int md_size = 0;
83
0
    int md_type_nid;
84
0
    const X509_ALGOR *macalg;
85
0
    const ASN1_OBJECT *macoid;
86
0
87
0
    if (pkcs12_key_gen == NULL)
88
0
        pkcs12_key_gen = PKCS12_key_gen_utf8;
89
0
90
0
    if (!PKCS7_type_is_data(p12->authsafes)) {
91
0
        PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_CONTENT_TYPE_NOT_DATA);
92
0
        return 0;
93
0
    }
94
0
95
0
    salt = p12->mac->salt->data;
96
0
    saltlen = p12->mac->salt->length;
97
0
    if (!p12->mac->iter)
98
0
        iter = 1;
99
0
    else
100
0
        iter = ASN1_INTEGER_get(p12->mac->iter);
101
0
    X509_SIG_get0(p12->mac->dinfo, &macalg, NULL);
102
0
    X509_ALGOR_get0(&macoid, NULL, NULL, macalg);
103
0
    if ((md_type = EVP_get_digestbyobj(macoid)) == NULL) {
104
0
        PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
105
0
        return 0;
106
0
    }
107
0
    md_size = EVP_MD_size(md_type);
108
0
    md_type_nid = EVP_MD_type(md_type);
109
0
    if (md_size < 0)
110
0
        return 0;
111
0
    if ((md_type_nid == NID_id_GostR3411_94
112
0
         || md_type_nid == NID_id_GostR3411_2012_256
113
0
         || md_type_nid == NID_id_GostR3411_2012_512)
114
0
        && !getenv("LEGACY_GOST_PKCS12")) {
115
0
        md_size = TK26_MAC_KEY_LEN;
116
0
        if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter,
117
0
                                     md_size, key, md_type)) {
118
0
            PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
119
0
            return 0;
120
0
        }
121
0
    } else
122
0
        if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID,
123
0
                               iter, md_size, key, md_type)) {
124
0
        PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
125
0
        return 0;
126
0
    }
127
0
    if ((hmac = HMAC_CTX_new()) == NULL
128
0
        || !HMAC_Init_ex(hmac, key, md_size, md_type, NULL)
129
0
        || !HMAC_Update(hmac, p12->authsafes->d.data->data,
130
0
                        p12->authsafes->d.data->length)
131
0
        || !HMAC_Final(hmac, mac, maclen)) {
132
0
        HMAC_CTX_free(hmac);
133
0
        return 0;
134
0
    }
135
0
    HMAC_CTX_free(hmac);
136
0
    return 1;
137
0
}
138
139
int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
140
                   unsigned char *mac, unsigned int *maclen)
141
0
{
142
0
    return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NULL);
143
0
}
144
145
/* Verify the mac */
146
int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
147
0
{
148
0
    unsigned char mac[EVP_MAX_MD_SIZE];
149
0
    unsigned int maclen;
150
0
    const ASN1_OCTET_STRING *macoct;
151
0
152
0
    if (p12->mac == NULL) {
153
0
        PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_ABSENT);
154
0
        return 0;
155
0
    }
156
0
    if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
157
0
                        PKCS12_key_gen_utf8)) {
158
0
        PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_GENERATION_ERROR);
159
0
        return 0;
160
0
    }
161
0
    X509_SIG_get0(p12->mac->dinfo, NULL, &macoct);
162
0
    if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
163
0
        || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
164
0
        return 0;
165
0
166
0
    return 1;
167
0
}
168
169
/* Set a mac */
170
171
int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
172
                   unsigned char *salt, int saltlen, int iter,
173
                   const EVP_MD *md_type)
174
0
{
175
0
    unsigned char mac[EVP_MAX_MD_SIZE];
176
0
    unsigned int maclen;
177
0
    ASN1_OCTET_STRING *macoct;
178
0
179
0
    if (!md_type)
180
0
        md_type = EVP_sha1();
181
0
    if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) {
182
0
        PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_SETUP_ERROR);
183
0
        return 0;
184
0
    }
185
0
    /*
186
0
     * Note that output mac is forced to UTF-8...
187
0
     */
188
0
    if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
189
0
                        PKCS12_key_gen_utf8)) {
190
0
        PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_GENERATION_ERROR);
191
0
        return 0;
192
0
    }
193
0
    X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
194
0
    if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) {
195
0
        PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_STRING_SET_ERROR);
196
0
        return 0;
197
0
    }
198
0
    return 1;
199
0
}
200
201
/* Set up a mac structure */
202
int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
203
                     const EVP_MD *md_type)
204
0
{
205
0
    X509_ALGOR *macalg;
206
0
207
0
    PKCS12_MAC_DATA_free(p12->mac);
208
0
    p12->mac = NULL;
209
0
210
0
    if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
211
0
        return PKCS12_ERROR;
212
0
    if (iter > 1) {
213
0
        if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) {
214
0
            PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
215
0
            return 0;
216
0
        }
217
0
        if (!ASN1_INTEGER_set(p12->mac->iter, iter)) {
218
0
            PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
219
0
            return 0;
220
0
        }
221
0
    }
222
0
    if (!saltlen)
223
0
        saltlen = PKCS12_SALT_LEN;
224
0
    if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL) {
225
0
        PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
226
0
        return 0;
227
0
    }
228
0
    p12->mac->salt->length = saltlen;
229
0
    if (!salt) {
230
0
        if (RAND_bytes(p12->mac->salt->data, saltlen) <= 0)
231
0
            return 0;
232
0
    } else
233
0
        memcpy(p12->mac->salt->data, salt, saltlen);
234
0
    X509_SIG_getm(p12->mac->dinfo, &macalg, NULL);
235
0
    if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(EVP_MD_type(md_type)),
236
0
                         V_ASN1_NULL, NULL)) {
237
0
        PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
238
0
        return 0;
239
0
    }
240
0
241
0
    return 1;
242
0
}