3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
61 #include "internal/cryptlib.h"
62 #include <openssl/pkcs12.h>
65 ASN1_TYPE *PKCS12_SAFEBAG_get0_attr(PKCS12_SAFEBAG *bag, int attr_nid)
67 return PKCS12_get_attr_gen(bag->attrib, attr_nid);
70 ASN1_TYPE *PKCS8_get_attr(PKCS8_PRIV_KEY_INFO *p8, int attr_nid)
72 return PKCS12_get_attr_gen(p8->attributes, attr_nid);
75 PKCS8_PRIV_KEY_INFO *PKCS12_SAFEBAG_get0_p8inf(PKCS12_SAFEBAG *bag)
77 if (PKCS12_SAFEBAG_get_nid(bag) != NID_keyBag)
79 return bag->value.keybag;
82 X509_SIG *PKCS12_SAFEBAG_get0_pkcs8(PKCS12_SAFEBAG *bag)
84 if (OBJ_obj2nid(bag->type) != NID_pkcs8ShroudedKeyBag)
86 return bag->value.shkeybag;
89 STACK_OF(PKCS12_SAFEBAG) *PKCS12_SAFEBAG_get0_safes(PKCS12_SAFEBAG *bag)
91 if (OBJ_obj2nid(bag->type) != NID_safeContentsBag)
93 return bag->value.safes;
96 ASN1_OBJECT *PKCS12_SAFEBAG_get0_type(PKCS12_SAFEBAG *bag)
101 int PKCS12_SAFEBAG_get_nid(PKCS12_SAFEBAG *bag)
103 return OBJ_obj2nid(bag->type);
106 int PKCS12_SAFEBAG_get_bag_nid(PKCS12_SAFEBAG *bag)
108 int btype = PKCS12_SAFEBAG_get_nid(bag);
110 if (btype != NID_certBag && btype != NID_crlBag && btype != NID_secretBag)
112 return OBJ_obj2nid(bag->value.bag->type);
115 X509 *PKCS12_SAFEBAG_get1_cert(PKCS12_SAFEBAG *bag)
117 if (PKCS12_SAFEBAG_get_nid(bag) != NID_certBag)
119 if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate)
121 return ASN1_item_unpack(bag->value.bag->value.octet,
122 ASN1_ITEM_rptr(X509));
125 X509_CRL *PKCS12_SAFEBAG_get1_crl(PKCS12_SAFEBAG *bag)
127 if (PKCS12_SAFEBAG_get_nid(bag) != NID_crlBag)
129 if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Crl)
131 return ASN1_item_unpack(bag->value.bag->value.octet,
132 ASN1_ITEM_rptr(X509_CRL));
135 PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_cert(X509 *x509)
137 return PKCS12_item_pack_safebag(x509, ASN1_ITEM_rptr(X509),
138 NID_x509Certificate, NID_certBag);
141 PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_crl(X509_CRL *crl)
143 return PKCS12_item_pack_safebag(crl, ASN1_ITEM_rptr(X509_CRL),
144 NID_x509Crl, NID_crlBag);
147 /* Turn PKCS8 object into a keybag */
149 PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_p8inf(PKCS8_PRIV_KEY_INFO *p8)
151 PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new();
154 PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE0_P8INF, ERR_R_MALLOC_FAILURE);
157 bag->type = OBJ_nid2obj(NID_keyBag);
158 bag->value.keybag = p8;
162 /* Turn PKCS8 object into a shrouded keybag */
164 PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_pkcs8(X509_SIG *p8)
166 PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new();
168 /* Set up the safe bag */
170 PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE0_PKCS8, ERR_R_MALLOC_FAILURE);
173 bag->type = OBJ_nid2obj(NID_pkcs8ShroudedKeyBag);
174 bag->value.shkeybag = p8;
178 PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt(int pbe_nid,
182 int saltlen, int iter,
183 PKCS8_PRIV_KEY_INFO *p8inf)
186 const EVP_CIPHER *pbe_ciph;
189 pbe_ciph = EVP_get_cipherbynid(pbe_nid);
194 p8 = PKCS8_encrypt(pbe_nid, pbe_ciph, pass, passlen, salt, saltlen, iter,
198 PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE_PKCS8_ENCRYPT, ERR_R_MALLOC_FAILURE);
202 bag = PKCS12_SAFEBAG_create0_pkcs8(p8);
205 PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE_PKCS8_ENCRYPT, ERR_R_MALLOC_FAILURE);