Coverage Report

Created: 2023-09-25 06:45

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