xref: /onnv-gate/usr/src/common/openssl/crypto/pkcs12/p12_mutl.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* p12_mutl.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
30Sstevel@tonic-gate  * project 1999.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    licensing@OpenSSL.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #ifndef OPENSSL_NO_HMAC
600Sstevel@tonic-gate #include <stdio.h>
610Sstevel@tonic-gate #include "cryptlib.h"
620Sstevel@tonic-gate #include <openssl/hmac.h>
630Sstevel@tonic-gate #include <openssl/rand.h>
640Sstevel@tonic-gate #include <openssl/pkcs12.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /* Generate a MAC */
PKCS12_gen_mac(PKCS12 * p12,const char * pass,int passlen,unsigned char * mac,unsigned int * maclen)67*2139Sjp161948 int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
68*2139Sjp161948 		   unsigned char *mac, unsigned int *maclen)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	const EVP_MD *md_type;
710Sstevel@tonic-gate 	HMAC_CTX hmac;
720Sstevel@tonic-gate 	unsigned char key[PKCS12_MAC_KEY_LENGTH], *salt;
730Sstevel@tonic-gate 	int saltlen, iter;
740Sstevel@tonic-gate 
75*2139Sjp161948 	if (!PKCS7_type_is_data(p12->authsafes))
76*2139Sjp161948 		{
77*2139Sjp161948 		PKCS12err(PKCS12_F_PKCS12_GEN_MAC,PKCS12_R_CONTENT_TYPE_NOT_DATA);
78*2139Sjp161948 		return 0;
79*2139Sjp161948 		}
80*2139Sjp161948 
810Sstevel@tonic-gate 	salt = p12->mac->salt->data;
820Sstevel@tonic-gate 	saltlen = p12->mac->salt->length;
830Sstevel@tonic-gate 	if (!p12->mac->iter) iter = 1;
840Sstevel@tonic-gate 	else iter = ASN1_INTEGER_get (p12->mac->iter);
850Sstevel@tonic-gate     	if(!(md_type =
860Sstevel@tonic-gate 		 EVP_get_digestbyobj (p12->mac->dinfo->algor->algorithm))) {
870Sstevel@tonic-gate 		PKCS12err(PKCS12_F_PKCS12_GEN_MAC,PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
880Sstevel@tonic-gate 		return 0;
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 	if(!PKCS12_key_gen (pass, passlen, salt, saltlen, PKCS12_MAC_ID, iter,
910Sstevel@tonic-gate 				 PKCS12_MAC_KEY_LENGTH, key, md_type)) {
920Sstevel@tonic-gate 		PKCS12err(PKCS12_F_PKCS12_GEN_MAC,PKCS12_R_KEY_GEN_ERROR);
930Sstevel@tonic-gate 		return 0;
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 	HMAC_CTX_init(&hmac);
960Sstevel@tonic-gate 	HMAC_Init_ex(&hmac, key, PKCS12_MAC_KEY_LENGTH, md_type, NULL);
970Sstevel@tonic-gate     	HMAC_Update(&hmac, p12->authsafes->d.data->data,
980Sstevel@tonic-gate 					 p12->authsafes->d.data->length);
990Sstevel@tonic-gate     	HMAC_Final(&hmac, mac, maclen);
1000Sstevel@tonic-gate     	HMAC_CTX_cleanup(&hmac);
1010Sstevel@tonic-gate 	return 1;
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /* Verify the mac */
PKCS12_verify_mac(PKCS12 * p12,const char * pass,int passlen)105*2139Sjp161948 int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	unsigned char mac[EVP_MAX_MD_SIZE];
1080Sstevel@tonic-gate 	unsigned int maclen;
1090Sstevel@tonic-gate 	if(p12->mac == NULL) {
110*2139Sjp161948 		PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC,PKCS12_R_MAC_ABSENT);
1110Sstevel@tonic-gate 		return 0;
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 	if (!PKCS12_gen_mac (p12, pass, passlen, mac, &maclen)) {
114*2139Sjp161948 		PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC,PKCS12_R_MAC_GENERATION_ERROR);
1150Sstevel@tonic-gate 		return 0;
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 	if ((maclen != (unsigned int)p12->mac->dinfo->digest->length)
1180Sstevel@tonic-gate 	|| memcmp (mac, p12->mac->dinfo->digest->data, maclen)) return 0;
1190Sstevel@tonic-gate 	return 1;
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /* Set a mac */
1230Sstevel@tonic-gate 
PKCS12_set_mac(PKCS12 * p12,const char * pass,int passlen,unsigned char * salt,int saltlen,int iter,const EVP_MD * md_type)124*2139Sjp161948 int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
1250Sstevel@tonic-gate 	     unsigned char *salt, int saltlen, int iter, const EVP_MD *md_type)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	unsigned char mac[EVP_MAX_MD_SIZE];
1280Sstevel@tonic-gate 	unsigned int maclen;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	if (!md_type) md_type = EVP_sha1();
1310Sstevel@tonic-gate 	if (PKCS12_setup_mac (p12, iter, salt, saltlen, md_type) ==
1320Sstevel@tonic-gate 				 	PKCS12_ERROR) {
1330Sstevel@tonic-gate 		PKCS12err(PKCS12_F_PKCS12_SET_MAC,PKCS12_R_MAC_SETUP_ERROR);
1340Sstevel@tonic-gate 		return 0;
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 	if (!PKCS12_gen_mac (p12, pass, passlen, mac, &maclen)) {
1370Sstevel@tonic-gate 		PKCS12err(PKCS12_F_PKCS12_SET_MAC,PKCS12_R_MAC_GENERATION_ERROR);
1380Sstevel@tonic-gate 		return 0;
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 	if (!(M_ASN1_OCTET_STRING_set (p12->mac->dinfo->digest, mac, maclen))) {
1410Sstevel@tonic-gate 		PKCS12err(PKCS12_F_PKCS12_SET_MAC,PKCS12_R_MAC_STRING_SET_ERROR);
1420Sstevel@tonic-gate 						return 0;
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 	return 1;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /* Set up a mac structure */
PKCS12_setup_mac(PKCS12 * p12,int iter,unsigned char * salt,int saltlen,const EVP_MD * md_type)148*2139Sjp161948 int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
1490Sstevel@tonic-gate 	     const EVP_MD *md_type)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	if (!(p12->mac = PKCS12_MAC_DATA_new())) return PKCS12_ERROR;
1520Sstevel@tonic-gate 	if (iter > 1) {
1530Sstevel@tonic-gate 		if(!(p12->mac->iter = M_ASN1_INTEGER_new())) {
1540Sstevel@tonic-gate 			PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
1550Sstevel@tonic-gate 			return 0;
1560Sstevel@tonic-gate 		}
157*2139Sjp161948 		if (!ASN1_INTEGER_set(p12->mac->iter, iter)) {
158*2139Sjp161948 			PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
159*2139Sjp161948 			return 0;
160*2139Sjp161948 		}
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 	if (!saltlen) saltlen = PKCS12_SALT_LEN;
1630Sstevel@tonic-gate 	p12->mac->salt->length = saltlen;
1640Sstevel@tonic-gate 	if (!(p12->mac->salt->data = OPENSSL_malloc (saltlen))) {
1650Sstevel@tonic-gate 		PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
1660Sstevel@tonic-gate 		return 0;
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 	if (!salt) {
1690Sstevel@tonic-gate 		if (RAND_pseudo_bytes (p12->mac->salt->data, saltlen) < 0)
1700Sstevel@tonic-gate 			return 0;
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 	else memcpy (p12->mac->salt->data, salt, saltlen);
1730Sstevel@tonic-gate 	p12->mac->dinfo->algor->algorithm = OBJ_nid2obj(EVP_MD_type(md_type));
1740Sstevel@tonic-gate 	if (!(p12->mac->dinfo->algor->parameter = ASN1_TYPE_new())) {
1750Sstevel@tonic-gate 		PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
1760Sstevel@tonic-gate 		return 0;
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 	p12->mac->dinfo->algor->parameter->type = V_ASN1_NULL;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	return 1;
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate #endif
183