10Sstevel@tonic-gate /* p12_add.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 #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/pkcs12.h>
620Sstevel@tonic-gate
630Sstevel@tonic-gate /* Pack an object into an OCTET STRING and turn into a safebag */
640Sstevel@tonic-gate
PKCS12_item_pack_safebag(void * obj,const ASN1_ITEM * it,int nid1,int nid2)650Sstevel@tonic-gate PKCS12_SAFEBAG *PKCS12_item_pack_safebag(void *obj, const ASN1_ITEM *it, int nid1,
660Sstevel@tonic-gate int nid2)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate PKCS12_BAGS *bag;
690Sstevel@tonic-gate PKCS12_SAFEBAG *safebag;
700Sstevel@tonic-gate if (!(bag = PKCS12_BAGS_new())) {
71*2139Sjp161948 PKCS12err(PKCS12_F_PKCS12_ITEM_PACK_SAFEBAG, ERR_R_MALLOC_FAILURE);
720Sstevel@tonic-gate return NULL;
730Sstevel@tonic-gate }
740Sstevel@tonic-gate bag->type = OBJ_nid2obj(nid1);
750Sstevel@tonic-gate if (!ASN1_item_pack(obj, it, &bag->value.octet)) {
76*2139Sjp161948 PKCS12err(PKCS12_F_PKCS12_ITEM_PACK_SAFEBAG, ERR_R_MALLOC_FAILURE);
770Sstevel@tonic-gate return NULL;
780Sstevel@tonic-gate }
790Sstevel@tonic-gate if (!(safebag = PKCS12_SAFEBAG_new())) {
80*2139Sjp161948 PKCS12err(PKCS12_F_PKCS12_ITEM_PACK_SAFEBAG, ERR_R_MALLOC_FAILURE);
810Sstevel@tonic-gate return NULL;
820Sstevel@tonic-gate }
830Sstevel@tonic-gate safebag->value.bag = bag;
840Sstevel@tonic-gate safebag->type = OBJ_nid2obj(nid2);
850Sstevel@tonic-gate return safebag;
860Sstevel@tonic-gate }
870Sstevel@tonic-gate
880Sstevel@tonic-gate /* Turn PKCS8 object into a keybag */
890Sstevel@tonic-gate
PKCS12_MAKE_KEYBAG(PKCS8_PRIV_KEY_INFO * p8)900Sstevel@tonic-gate PKCS12_SAFEBAG *PKCS12_MAKE_KEYBAG(PKCS8_PRIV_KEY_INFO *p8)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate PKCS12_SAFEBAG *bag;
930Sstevel@tonic-gate if (!(bag = PKCS12_SAFEBAG_new())) {
940Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_MAKE_KEYBAG,ERR_R_MALLOC_FAILURE);
950Sstevel@tonic-gate return NULL;
960Sstevel@tonic-gate }
970Sstevel@tonic-gate bag->type = OBJ_nid2obj(NID_keyBag);
980Sstevel@tonic-gate bag->value.keybag = p8;
990Sstevel@tonic-gate return bag;
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /* Turn PKCS8 object into a shrouded keybag */
1030Sstevel@tonic-gate
PKCS12_MAKE_SHKEYBAG(int pbe_nid,const char * pass,int passlen,unsigned char * salt,int saltlen,int iter,PKCS8_PRIV_KEY_INFO * p8)1040Sstevel@tonic-gate PKCS12_SAFEBAG *PKCS12_MAKE_SHKEYBAG(int pbe_nid, const char *pass,
1050Sstevel@tonic-gate int passlen, unsigned char *salt, int saltlen, int iter,
1060Sstevel@tonic-gate PKCS8_PRIV_KEY_INFO *p8)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate PKCS12_SAFEBAG *bag;
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate /* Set up the safe bag */
1110Sstevel@tonic-gate if (!(bag = PKCS12_SAFEBAG_new())) {
1120Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_MAKE_SHKEYBAG, ERR_R_MALLOC_FAILURE);
1130Sstevel@tonic-gate return NULL;
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate bag->type = OBJ_nid2obj(NID_pkcs8ShroudedKeyBag);
1170Sstevel@tonic-gate if (!(bag->value.shkeybag =
1180Sstevel@tonic-gate PKCS8_encrypt(pbe_nid, NULL, pass, passlen, salt, saltlen, iter,
1190Sstevel@tonic-gate p8))) {
1200Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_MAKE_SHKEYBAG, ERR_R_MALLOC_FAILURE);
1210Sstevel@tonic-gate return NULL;
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate return bag;
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate /* Turn a stack of SAFEBAGS into a PKCS#7 data Contentinfo */
PKCS12_pack_p7data(STACK_OF (PKCS12_SAFEBAG)* sk)1280Sstevel@tonic-gate PKCS7 *PKCS12_pack_p7data(STACK_OF(PKCS12_SAFEBAG) *sk)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate PKCS7 *p7;
1310Sstevel@tonic-gate if (!(p7 = PKCS7_new())) {
1320Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_PACK_P7DATA, ERR_R_MALLOC_FAILURE);
1330Sstevel@tonic-gate return NULL;
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate p7->type = OBJ_nid2obj(NID_pkcs7_data);
1360Sstevel@tonic-gate if (!(p7->d.data = M_ASN1_OCTET_STRING_new())) {
1370Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_PACK_P7DATA, ERR_R_MALLOC_FAILURE);
1380Sstevel@tonic-gate return NULL;
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate if (!ASN1_item_pack(sk, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), &p7->d.data)) {
1420Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_PACK_P7DATA, PKCS12_R_CANT_PACK_STRUCTURE);
1430Sstevel@tonic-gate return NULL;
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate return p7;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate /* Unpack SAFEBAGS from PKCS#7 data ContentInfo */
STACK_OF(PKCS12_SAFEBAG)1490Sstevel@tonic-gate STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7data(PKCS7 *p7)
1500Sstevel@tonic-gate {
151*2139Sjp161948 if(!PKCS7_type_is_data(p7))
152*2139Sjp161948 {
153*2139Sjp161948 PKCS12err(PKCS12_F_PKCS12_UNPACK_P7DATA,PKCS12_R_CONTENT_TYPE_NOT_DATA);
154*2139Sjp161948 return NULL;
155*2139Sjp161948 }
1560Sstevel@tonic-gate return ASN1_item_unpack(p7->d.data, ASN1_ITEM_rptr(PKCS12_SAFEBAGS));
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate /* Turn a stack of SAFEBAGS into a PKCS#7 encrypted data ContentInfo */
1600Sstevel@tonic-gate
PKCS12_pack_p7encdata(int pbe_nid,const char * pass,int passlen,unsigned char * salt,int saltlen,int iter,STACK_OF (PKCS12_SAFEBAG)* bags)1610Sstevel@tonic-gate PKCS7 *PKCS12_pack_p7encdata(int pbe_nid, const char *pass, int passlen,
1620Sstevel@tonic-gate unsigned char *salt, int saltlen, int iter,
1630Sstevel@tonic-gate STACK_OF(PKCS12_SAFEBAG) *bags)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate PKCS7 *p7;
1660Sstevel@tonic-gate X509_ALGOR *pbe;
1670Sstevel@tonic-gate if (!(p7 = PKCS7_new())) {
1680Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_PACK_P7ENCDATA, ERR_R_MALLOC_FAILURE);
1690Sstevel@tonic-gate return NULL;
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate if(!PKCS7_set_type(p7, NID_pkcs7_encrypted)) {
1720Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_PACK_P7ENCDATA,
1730Sstevel@tonic-gate PKCS12_R_ERROR_SETTING_ENCRYPTED_DATA_TYPE);
1740Sstevel@tonic-gate return NULL;
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate if (!(pbe = PKCS5_pbe_set(pbe_nid, iter, salt, saltlen))) {
1770Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_PACK_P7ENCDATA, ERR_R_MALLOC_FAILURE);
1780Sstevel@tonic-gate return NULL;
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate X509_ALGOR_free(p7->d.encrypted->enc_data->algorithm);
1810Sstevel@tonic-gate p7->d.encrypted->enc_data->algorithm = pbe;
1820Sstevel@tonic-gate M_ASN1_OCTET_STRING_free(p7->d.encrypted->enc_data->enc_data);
1830Sstevel@tonic-gate if (!(p7->d.encrypted->enc_data->enc_data =
1840Sstevel@tonic-gate PKCS12_item_i2d_encrypt(pbe, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), pass, passlen,
1850Sstevel@tonic-gate bags, 1))) {
1860Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_PACK_P7ENCDATA, PKCS12_R_ENCRYPT_ERROR);
1870Sstevel@tonic-gate return NULL;
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate return p7;
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate
STACK_OF(PKCS12_SAFEBAG)1930Sstevel@tonic-gate STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass, int passlen)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate if(!PKCS7_type_is_encrypted(p7)) return NULL;
1960Sstevel@tonic-gate return PKCS12_item_decrypt_d2i(p7->d.encrypted->enc_data->algorithm,
1970Sstevel@tonic-gate ASN1_ITEM_rptr(PKCS12_SAFEBAGS),
1980Sstevel@tonic-gate pass, passlen,
1990Sstevel@tonic-gate p7->d.encrypted->enc_data->enc_data, 1);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate
PKCS12_decrypt_skey(PKCS12_SAFEBAG * bag,const char * pass,int passlen)2020Sstevel@tonic-gate PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey(PKCS12_SAFEBAG *bag, const char *pass,
2030Sstevel@tonic-gate int passlen)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate return PKCS8_decrypt(bag->value.shkeybag, pass, passlen);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate
PKCS12_pack_authsafes(PKCS12 * p12,STACK_OF (PKCS7)* safes)2080Sstevel@tonic-gate int PKCS12_pack_authsafes(PKCS12 *p12, STACK_OF(PKCS7) *safes)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate if(ASN1_item_pack(safes, ASN1_ITEM_rptr(PKCS12_AUTHSAFES),
2110Sstevel@tonic-gate &p12->authsafes->d.data))
2120Sstevel@tonic-gate return 1;
2130Sstevel@tonic-gate return 0;
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate
STACK_OF(PKCS7)2160Sstevel@tonic-gate STACK_OF(PKCS7) *PKCS12_unpack_authsafes(PKCS12 *p12)
2170Sstevel@tonic-gate {
218*2139Sjp161948 if (!PKCS7_type_is_data(p12->authsafes))
219*2139Sjp161948 {
220*2139Sjp161948 PKCS12err(PKCS12_F_PKCS12_UNPACK_AUTHSAFES,PKCS12_R_CONTENT_TYPE_NOT_DATA);
221*2139Sjp161948 return NULL;
222*2139Sjp161948 }
2230Sstevel@tonic-gate return ASN1_item_unpack(p12->authsafes->d.data, ASN1_ITEM_rptr(PKCS12_AUTHSAFES));
2240Sstevel@tonic-gate }
225