p12_mutl.c 8.8 KB
Newer Older
1 2 3
/*
 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
 * 1999.
4 5 6 7 8 9 10 11 12
 */
/* ====================================================================
 * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    licensing@OpenSSL.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */

59
# include <stdio.h>
60
# include "internal/cryptlib.h"
61
# include <openssl/crypto.h>
62 63 64
# include <openssl/hmac.h>
# include <openssl/rand.h>
# include <openssl/pkcs12.h>
D
Dr. Stephen Henson 已提交
65
# include "p12_lcl.h"
66

D
Dr. Stephen Henson 已提交
67 68
int PKCS12_mac_present(PKCS12 *p12)
{
69
    return p12->mac ? 1 : 0;
D
Dr. Stephen Henson 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}

void PKCS12_get0_mac(ASN1_OCTET_STRING **pmac, X509_ALGOR **pmacalg,
                     ASN1_OCTET_STRING **psalt, ASN1_INTEGER **piter,
                     PKCS12 *p12)
{
    if (p12->mac) {
        if (pmac)
            *pmac = p12->mac->dinfo->digest;
        if (pmacalg)
            *pmacalg = p12->mac->dinfo->algor;
        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 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
# 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;
}

119
/* Generate a MAC */
B
Bodo Möller 已提交
120
int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
121
                   unsigned char *mac, unsigned int *maclen)
122
{
123
    const EVP_MD *md_type;
124
    HMAC_CTX *hmac = NULL;
125 126
    unsigned char key[EVP_MAX_MD_SIZE], *salt;
    int saltlen, iter;
M
Matt Caswell 已提交
127 128
    int md_size = 0;
    int md_type_nid;
129

130 131 132 133
    if (!PKCS7_type_is_data(p12->authsafes)) {
        PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_CONTENT_TYPE_NOT_DATA);
        return 0;
    }
134

135 136 137 138 139 140
    salt = p12->mac->salt->data;
    saltlen = p12->mac->salt->length;
    if (!p12->mac->iter)
        iter = 1;
    else
        iter = ASN1_INTEGER_get(p12->mac->iter);
141 142
    if ((md_type = EVP_get_digestbyobj(p12->mac->dinfo->algor->algorithm))
            == NULL) {
143 144 145 146
        PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
        return 0;
    }
    md_size = EVP_MD_size(md_type);
M
Matt Caswell 已提交
147
    md_type_nid = EVP_MD_type(md_type);
148 149
    if (md_size < 0)
        return 0;
M
Matt Caswell 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162
    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);
            return 0;
        }
    } else
        if (!PKCS12_key_gen(pass, passlen, salt, saltlen, PKCS12_MAC_ID, iter,
                            md_size, key, md_type)) {
163 164 165
        PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
        return 0;
    }
166 167 168
    hmac = HMAC_CTX_new();
    if (!HMAC_Init_ex(hmac, key, md_size, md_type, NULL)
        || !HMAC_Update(hmac, p12->authsafes->d.data->data,
169
                        p12->authsafes->d.data->length)
170 171
        || !HMAC_Final(hmac, mac, maclen)) {
        HMAC_CTX_free(hmac);
172 173
        return 0;
    }
174
    HMAC_CTX_free(hmac);
175
    return 1;
176 177 178
}

/* Verify the mac */
B
Bodo Möller 已提交
179
int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
180
{
181 182 183 184 185 186 187 188 189 190 191
    unsigned char mac[EVP_MAX_MD_SIZE];
    unsigned int maclen;
    if (p12->mac == NULL) {
        PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_ABSENT);
        return 0;
    }
    if (!PKCS12_gen_mac(p12, pass, passlen, mac, &maclen)) {
        PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_GENERATION_ERROR);
        return 0;
    }
    if ((maclen != (unsigned int)p12->mac->dinfo->digest->length)
192
        || CRYPTO_memcmp(mac, p12->mac->dinfo->digest->data, maclen))
193 194
        return 0;
    return 1;
195 196 197 198
}

/* Set a mac */

B
Bodo Möller 已提交
199
int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
200 201
                   unsigned char *salt, int saltlen, int iter,
                   const EVP_MD *md_type)
202
{
203 204
    unsigned char mac[EVP_MAX_MD_SIZE];
    unsigned int maclen;
B
Ben Laurie 已提交
205

206 207 208 209 210 211 212 213 214 215
    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;
    }
    if (!PKCS12_gen_mac(p12, pass, passlen, mac, &maclen)) {
        PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_GENERATION_ERROR);
        return 0;
    }
D
Dr. Stephen Henson 已提交
216
    if (!(ASN1_OCTET_STRING_set(p12->mac->dinfo->digest, mac, maclen))) {
217 218 219 220
        PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_STRING_SET_ERROR);
        return 0;
    }
    return 1;
221 222 223
}

/* Set up a mac structure */
B
Bodo Möller 已提交
224
int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
225
                     const EVP_MD *md_type)
226
{
227
    if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
228 229
        return PKCS12_ERROR;
    if (iter > 1) {
230
        if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) {
231 232 233 234 235 236 237 238 239 240
            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;
241
    if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL) {
242 243 244
        PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
        return 0;
    }
245
    p12->mac->salt->length = saltlen;
246
    if (!salt) {
M
Matt Caswell 已提交
247
        if (RAND_bytes(p12->mac->salt->data, saltlen) <= 0)
248 249 250 251
            return 0;
    } else
        memcpy(p12->mac->salt->data, salt, saltlen);
    p12->mac->dinfo->algor->algorithm = OBJ_nid2obj(EVP_MD_type(md_type));
252
    if ((p12->mac->dinfo->algor->parameter = ASN1_TYPE_new()) == NULL) {
253 254 255 256 257 258
        PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
        return 0;
    }
    p12->mac->dinfo->algor->parameter->type = V_ASN1_NULL;

    return 1;
259
}