1999-03-28 23:17:34 +00:00
|
|
|
/* p12_mutl.c */
|
|
|
|
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
|
|
|
|
* project 1999.
|
|
|
|
*/
|
|
|
|
/* ====================================================================
|
|
|
|
* 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
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 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).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1999-06-29 23:52:08 +00:00
|
|
|
#ifndef NO_HMAC
|
1999-03-28 23:17:34 +00:00
|
|
|
#include <stdio.h>
|
1999-03-29 17:50:26 +00:00
|
|
|
#include "cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/hmac.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/pkcs12.h>
|
1999-03-28 23:17:34 +00:00
|
|
|
|
|
|
|
/* Generate a MAC */
|
1999-04-23 15:01:15 +00:00
|
|
|
int PKCS12_gen_mac (PKCS12 *p12, const char *pass, int passlen,
|
|
|
|
unsigned char *mac, unsigned int *maclen)
|
1999-03-28 23:17:34 +00:00
|
|
|
{
|
1999-04-17 21:25:43 +00:00
|
|
|
const EVP_MD *md_type;
|
1999-03-28 23:17:34 +00:00
|
|
|
HMAC_CTX hmac;
|
|
|
|
unsigned char key[PKCS12_MAC_KEY_LENGTH], *salt;
|
|
|
|
int saltlen, iter;
|
|
|
|
salt = p12->mac->salt->data;
|
|
|
|
saltlen = p12->mac->salt->length;
|
|
|
|
if (!p12->mac->iter) iter = 1;
|
|
|
|
else iter = ASN1_INTEGER_get (p12->mac->iter);
|
|
|
|
if(!(md_type =
|
|
|
|
EVP_get_digestbyobj (p12->mac->dinfo->algor->algorithm))) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_GEN_MAC,PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(!PKCS12_key_gen (pass, passlen, salt, saltlen, PKCS12_MAC_ID, iter,
|
|
|
|
PKCS12_MAC_KEY_LENGTH, key, md_type)) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_GEN_MAC,PKCS12_R_KEY_GEN_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
HMAC_Init (&hmac, key, PKCS12_MAC_KEY_LENGTH, md_type);
|
|
|
|
HMAC_Update (&hmac, p12->authsafes->d.data->data,
|
|
|
|
p12->authsafes->d.data->length);
|
|
|
|
HMAC_Final (&hmac, mac, maclen);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify the mac */
|
1999-04-23 15:01:15 +00:00
|
|
|
int PKCS12_verify_mac (PKCS12 *p12, const char *pass, int passlen)
|
1999-03-28 23:17:34 +00:00
|
|
|
{
|
|
|
|
unsigned char mac[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int maclen;
|
|
|
|
if(p12->mac == NULL) {
|
|
|
|
PKCS12err(PKCS12_F_VERIFY_MAC,PKCS12_R_MAC_ABSENT);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!PKCS12_gen_mac (p12, pass, passlen, mac, &maclen)) {
|
|
|
|
PKCS12err(PKCS12_F_VERIFY_MAC,PKCS12_R_MAC_GENERATION_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((maclen != (unsigned int)p12->mac->dinfo->digest->length)
|
2000-05-04 00:08:35 +00:00
|
|
|
|| memcmp (mac, p12->mac->dinfo->digest->data, maclen)) return 0;
|
1999-03-28 23:17:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set a mac */
|
|
|
|
|
1999-04-23 15:01:15 +00:00
|
|
|
int PKCS12_set_mac (PKCS12 *p12, const char *pass, int passlen,
|
1999-04-19 21:31:43 +00:00
|
|
|
unsigned char *salt, int saltlen, int iter, EVP_MD *md_type)
|
1999-03-28 23:17:34 +00:00
|
|
|
{
|
|
|
|
unsigned char mac[EVP_MAX_MD_SIZE];
|
1999-04-23 15:01:15 +00:00
|
|
|
unsigned int maclen;
|
|
|
|
|
1999-03-28 23:17:34 +00:00
|
|
|
if (!md_type) md_type = EVP_sha1();
|
|
|
|
if (PKCS12_setup_mac (p12, iter, salt, saltlen, md_type) ==
|
|
|
|
PKCS12_ERROR) {
|
1999-04-24 13:28:57 +00:00
|
|
|
PKCS12err(PKCS12_F_PKCS12_SET_MAC,PKCS12_R_MAC_SETUP_ERROR);
|
1999-03-28 23:17:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!PKCS12_gen_mac (p12, pass, passlen, mac, &maclen)) {
|
1999-04-24 13:28:57 +00:00
|
|
|
PKCS12err(PKCS12_F_PKCS12_SET_MAC,PKCS12_R_MAC_GENERATION_ERROR);
|
1999-03-28 23:17:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1999-10-20 01:50:23 +00:00
|
|
|
if (!(M_ASN1_OCTET_STRING_set (p12->mac->dinfo->digest, mac, maclen))) {
|
1999-04-24 13:28:57 +00:00
|
|
|
PKCS12err(PKCS12_F_PKCS12_SET_MAC,PKCS12_R_MAC_STRING_SET_ERROR);
|
1999-03-28 23:17:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up a mac structure */
|
1999-04-19 21:31:43 +00:00
|
|
|
int PKCS12_setup_mac (PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
|
|
|
|
EVP_MD *md_type)
|
1999-03-28 23:17:34 +00:00
|
|
|
{
|
1999-10-20 01:50:23 +00:00
|
|
|
if (!(p12->mac = PKCS12_MAC_DATA_new())) return PKCS12_ERROR;
|
1999-03-28 23:17:34 +00:00
|
|
|
if (iter > 1) {
|
1999-10-20 01:50:23 +00:00
|
|
|
if(!(p12->mac->iter = M_ASN1_INTEGER_new())) {
|
1999-03-28 23:17:34 +00:00
|
|
|
PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
1999-10-20 01:50:23 +00:00
|
|
|
ASN1_INTEGER_set(p12->mac->iter, iter);
|
1999-03-28 23:17:34 +00:00
|
|
|
}
|
|
|
|
if (!saltlen) saltlen = PKCS12_SALT_LEN;
|
|
|
|
p12->mac->salt->length = saltlen;
|
2000-06-01 22:19:21 +00:00
|
|
|
if (!(p12->mac->salt->data = OPENSSL_malloc (saltlen))) {
|
1999-03-28 23:17:34 +00:00
|
|
|
PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
2000-01-21 01:15:56 +00:00
|
|
|
if (!salt) {
|
2000-03-03 07:51:25 +00:00
|
|
|
if (RAND_pseudo_bytes (p12->mac->salt->data, saltlen) < 0)
|
2000-01-21 01:15:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1999-03-28 23:17:34 +00:00
|
|
|
else memcpy (p12->mac->salt->data, salt, saltlen);
|
1999-03-29 22:18:54 +00:00
|
|
|
p12->mac->dinfo->algor->algorithm = OBJ_nid2obj(EVP_MD_type(md_type));
|
1999-03-28 23:17:34 +00:00
|
|
|
if (!(p12->mac->dinfo->algor->parameter = ASN1_TYPE_new())) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
p12->mac->dinfo->algor->parameter->type = V_ASN1_NULL;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
1999-06-29 23:52:08 +00:00
|
|
|
#endif
|