p12_mutl.c 7.7 KB
Newer Older
1
/*
M
Matt Caswell 已提交
2
 * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
R
Rich Salz 已提交
4 5 6 7
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
8 9
 */

10
# include <stdio.h>
11
# include "internal/cryptlib.h"
12
# include <openssl/crypto.h>
13 14 15
# include <openssl/hmac.h>
# include <openssl/rand.h>
# include <openssl/pkcs12.h>
D
Dr. Stephen Henson 已提交
16
# include "p12_lcl.h"
17

18
int PKCS12_mac_present(const PKCS12 *p12)
D
Dr. Stephen Henson 已提交
19
{
20
    return p12->mac ? 1 : 0;
D
Dr. Stephen Henson 已提交
21 22
}

D
Dr. Stephen Henson 已提交
23 24 25 26 27
void PKCS12_get0_mac(const ASN1_OCTET_STRING **pmac,
                     const X509_ALGOR **pmacalg,
                     const ASN1_OCTET_STRING **psalt,
                     const ASN1_INTEGER **piter,
                     const PKCS12 *p12)
D
Dr. Stephen Henson 已提交
28 29
{
    if (p12->mac) {
D
Dr. Stephen Henson 已提交
30
        X509_SIG_get0(p12->mac->dinfo, pmacalg, pmac);
D
Dr. Stephen Henson 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        if (psalt)
            *psalt = p12->mac->salt;
        if (piter)
            *piter = p12->mac->iter;
    } else {
        if (pmac)
            *pmac = NULL;
        if (pmacalg)
            *pmacalg = NULL;
        if (psalt)
            *psalt = NULL;
        if (piter)
            *piter = NULL;
    }
}

M
Matt Caswell 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
# define TK26_MAC_KEY_LEN 32

static int pkcs12_gen_gost_mac_key(const char *pass, int passlen,
                                   const unsigned char *salt, int saltlen,
                                   int iter, int keylen, unsigned char *key,
                                   const EVP_MD *digest)
{
    unsigned char out[96];

    if (keylen != TK26_MAC_KEY_LEN) {
        return 0;
    }

    if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter,
                           digest, sizeof(out), out)) {
        return 0;
    }
    memcpy(key, out + sizeof(out) - TK26_MAC_KEY_LEN, TK26_MAC_KEY_LEN);
    OPENSSL_cleanse(out, sizeof(out));
    return 1;
}

69
/* Generate a MAC */
70 71 72 73 74 75 76
static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
                          unsigned char *mac, unsigned int *maclen,
                          int (*pkcs12_key_gen)(const char *pass, int passlen,
                                                unsigned char *salt, int slen,
                                                int id, int iter, int n,
                                                unsigned char *out,
                                                const EVP_MD *md_type))
77
{
S
Shane Lontis 已提交
78
    int ret = 0;
79
    const EVP_MD *md_type;
80
    HMAC_CTX *hmac = NULL;
81 82
    unsigned char key[EVP_MAX_MD_SIZE], *salt;
    int saltlen, iter;
M
Matt Caswell 已提交
83 84
    int md_size = 0;
    int md_type_nid;
D
Dr. Stephen Henson 已提交
85
    const X509_ALGOR *macalg;
D
Dr. Stephen Henson 已提交
86
    const ASN1_OBJECT *macoid;
87

88 89 90
    if (pkcs12_key_gen == NULL)
        pkcs12_key_gen = PKCS12_key_gen_utf8;

91 92 93 94
    if (!PKCS7_type_is_data(p12->authsafes)) {
        PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_CONTENT_TYPE_NOT_DATA);
        return 0;
    }
95

96 97 98 99 100 101
    salt = p12->mac->salt->data;
    saltlen = p12->mac->salt->length;
    if (!p12->mac->iter)
        iter = 1;
    else
        iter = ASN1_INTEGER_get(p12->mac->iter);
D
Dr. Stephen Henson 已提交
102
    X509_SIG_get0(p12->mac->dinfo, &macalg, NULL);
D
Dr. Stephen Henson 已提交
103 104
    X509_ALGOR_get0(&macoid, NULL, NULL, macalg);
    if ((md_type = EVP_get_digestbyobj(macoid)) == NULL) {
105 106 107 108
        PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
        return 0;
    }
    md_size = EVP_MD_size(md_type);
M
Matt Caswell 已提交
109
    md_type_nid = EVP_MD_type(md_type);
110 111
    if (md_size < 0)
        return 0;
M
Matt Caswell 已提交
112 113 114 115 116 117 118 119
    if ((md_type_nid == NID_id_GostR3411_94
         || md_type_nid == NID_id_GostR3411_2012_256
         || md_type_nid == NID_id_GostR3411_2012_512)
        && !getenv("LEGACY_GOST_PKCS12")) {
        md_size = TK26_MAC_KEY_LEN;
        if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter,
                                     md_size, key, md_type)) {
            PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
S
Shane Lontis 已提交
120
            goto err;
M
Matt Caswell 已提交
121 122
        }
    } else
123 124
        if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID,
                               iter, md_size, key, md_type)) {
125
        PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
S
Shane Lontis 已提交
126
        goto err;
127
    }
R
Rich Salz 已提交
128 129
    if ((hmac = HMAC_CTX_new()) == NULL
        || !HMAC_Init_ex(hmac, key, md_size, md_type, NULL)
130
        || !HMAC_Update(hmac, p12->authsafes->d.data->data,
131
                        p12->authsafes->d.data->length)
132
        || !HMAC_Final(hmac, mac, maclen)) {
S
Shane Lontis 已提交
133
        goto err;
134
    }
S
Shane Lontis 已提交
135 136 137 138
    ret = 1;

err:
    OPENSSL_cleanse(key, sizeof(key));
139
    HMAC_CTX_free(hmac);
S
Shane Lontis 已提交
140
    return ret;
141 142
}

143 144 145 146 147 148
int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
                   unsigned char *mac, unsigned int *maclen)
{
    return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NULL);
}

149
/* Verify the mac */
B
Bodo Möller 已提交
150
int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
151
{
152 153
    unsigned char mac[EVP_MAX_MD_SIZE];
    unsigned int maclen;
D
Dr. Stephen Henson 已提交
154
    const ASN1_OCTET_STRING *macoct;
D
Dr. Stephen Henson 已提交
155

156 157 158 159
    if (p12->mac == NULL) {
        PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_ABSENT);
        return 0;
    }
160 161
    if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
                        PKCS12_key_gen_utf8)) {
162 163 164
        PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_GENERATION_ERROR);
        return 0;
    }
D
Dr. Stephen Henson 已提交
165
    X509_SIG_get0(p12->mac->dinfo, NULL, &macoct);
166 167
    if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
        || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
168
        return 0;
169

170
    return 1;
171 172 173 174
}

/* Set a mac */

B
Bodo Möller 已提交
175
int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
176 177
                   unsigned char *salt, int saltlen, int iter,
                   const EVP_MD *md_type)
178
{
179 180
    unsigned char mac[EVP_MAX_MD_SIZE];
    unsigned int maclen;
D
Dr. Stephen Henson 已提交
181
    ASN1_OCTET_STRING *macoct;
B
Ben Laurie 已提交
182

183 184 185 186 187 188
    if (!md_type)
        md_type = EVP_sha1();
    if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) {
        PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_SETUP_ERROR);
        return 0;
    }
189 190 191 192 193
    /*
     * Note that output mac is forced to UTF-8...
     */
    if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
                        PKCS12_key_gen_utf8)) {
194 195 196
        PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_GENERATION_ERROR);
        return 0;
    }
197
    X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
D
Dr. Stephen Henson 已提交
198
    if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) {
199 200 201 202
        PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_STRING_SET_ERROR);
        return 0;
    }
    return 1;
203 204 205
}

/* Set up a mac structure */
B
Bodo Möller 已提交
206
int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
207
                     const EVP_MD *md_type)
208
{
D
Dr. Stephen Henson 已提交
209 210
    X509_ALGOR *macalg;

211 212 213
    PKCS12_MAC_DATA_free(p12->mac);
    p12->mac = NULL;

214
    if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
215 216
        return PKCS12_ERROR;
    if (iter > 1) {
217
        if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) {
218 219 220 221 222 223 224 225 226 227
            PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
            return 0;
        }
        if (!ASN1_INTEGER_set(p12->mac->iter, iter)) {
            PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
            return 0;
        }
    }
    if (!saltlen)
        saltlen = PKCS12_SALT_LEN;
228
    if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL) {
229 230 231
        PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
        return 0;
    }
232
    p12->mac->salt->length = saltlen;
233
    if (!salt) {
M
Matt Caswell 已提交
234
        if (RAND_bytes(p12->mac->salt->data, saltlen) <= 0)
235 236 237
            return 0;
    } else
        memcpy(p12->mac->salt->data, salt, saltlen);
238
    X509_SIG_getm(p12->mac->dinfo, &macalg, NULL);
D
Dr. Stephen Henson 已提交
239 240
    if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(EVP_MD_type(md_type)),
                         V_ASN1_NULL, NULL)) {
241 242 243 244 245
        PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
        return 0;
    }

    return 1;
246
}