fix various formatting issues
[openssl.git] / crypto / pkcs12 / p12_sbag.c
1 /* p12_sbag.c */
2 /*
3  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4  * 1999-2015.
5  */
6 /* ====================================================================
7  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
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
19  *    distribution.
20  *
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/)"
25  *
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.
30  *
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.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
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  * ====================================================================
53  *
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).
57  *
58  */
59
60 #include <stdio.h>
61 #include "internal/cryptlib.h"
62 #include <openssl/pkcs12.h>
63 #include "p12_lcl.h"
64
65 ASN1_TYPE *PKCS12_SAFEBAG_get0_attr(PKCS12_SAFEBAG *bag, int attr_nid)
66 {
67     return PKCS12_get_attr_gen(bag->attrib, attr_nid);
68 }
69
70 ASN1_TYPE *PKCS8_get_attr(PKCS8_PRIV_KEY_INFO *p8, int attr_nid)
71 {
72     return PKCS12_get_attr_gen(p8->attributes, attr_nid);
73 }
74
75 PKCS8_PRIV_KEY_INFO *PKCS12_SAFEBAG_get0_p8inf(PKCS12_SAFEBAG *bag)
76 {
77     if (PKCS12_SAFEBAG_get_nid(bag) != NID_keyBag)
78         return NULL;
79     return bag->value.keybag;
80 }
81
82 X509_SIG *PKCS12_SAFEBAG_get0_pkcs8(PKCS12_SAFEBAG *bag)
83 {
84     if (OBJ_obj2nid(bag->type) != NID_pkcs8ShroudedKeyBag)
85         return NULL;
86     return bag->value.shkeybag;
87 }
88
89 STACK_OF(PKCS12_SAFEBAG) *PKCS12_SAFEBAG_get0_safes(PKCS12_SAFEBAG *bag)
90 {
91     if (OBJ_obj2nid(bag->type) != NID_safeContentsBag)
92         return NULL;
93     return bag->value.safes;
94 }
95
96 ASN1_OBJECT *PKCS12_SAFEBAG_get0_type(PKCS12_SAFEBAG *bag)
97 {
98     return bag->type;
99 }
100
101 int PKCS12_SAFEBAG_get_nid(PKCS12_SAFEBAG *bag)
102 {
103     return OBJ_obj2nid(bag->type);
104 }
105
106 int PKCS12_SAFEBAG_get_bag_nid(PKCS12_SAFEBAG *bag)
107 {
108     int btype = PKCS12_SAFEBAG_get_nid(bag);
109
110     if (btype != NID_certBag || btype != NID_crlBag || btype != NID_secretBag)
111         return -1;
112     return OBJ_obj2nid(bag->value.bag->type);
113 }
114
115 X509 *PKCS12_SAFEBAG_get1_cert(PKCS12_SAFEBAG *bag)
116 {
117     if (PKCS12_SAFEBAG_get_nid(bag) != NID_certBag)
118         return NULL;
119     if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate)
120         return NULL;
121     return ASN1_item_unpack(bag->value.bag->value.octet,
122                             ASN1_ITEM_rptr(X509));
123 }
124
125 X509_CRL *PKCS12_SAFEBAG_get1_crl(PKCS12_SAFEBAG *bag)
126 {
127     if (PKCS12_SAFEBAG_get_nid(bag) != NID_crlBag)
128         return NULL;
129     if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Crl)
130         return NULL;
131     return ASN1_item_unpack(bag->value.bag->value.octet,
132                             ASN1_ITEM_rptr(X509_CRL));
133 }
134
135 PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_cert(X509 *x509)
136 {
137     return PKCS12_item_pack_safebag(x509, ASN1_ITEM_rptr(X509),
138                                     NID_x509Certificate, NID_certBag);
139 }
140
141 PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_crl(X509_CRL *crl)
142 {
143     return PKCS12_item_pack_safebag(crl, ASN1_ITEM_rptr(X509_CRL),
144                                     NID_x509Crl, NID_crlBag);
145 }
146
147 /* Turn PKCS8 object into a keybag */
148
149 PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_p8inf(PKCS8_PRIV_KEY_INFO *p8)
150 {
151     PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new();
152
153     if (bag == NULL) {
154         PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE0_P8INF, ERR_R_MALLOC_FAILURE);
155         return NULL;
156     }
157     bag->type = OBJ_nid2obj(NID_keyBag);
158     bag->value.keybag = p8;
159     return bag;
160 }
161
162 /* Turn PKCS8 object into a shrouded keybag */
163
164 PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_pkcs8(X509_SIG *p8)
165 {
166     PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new();
167
168     /* Set up the safe bag */
169     if (bag == NULL) {
170         PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE0_PKCS8, ERR_R_MALLOC_FAILURE);
171         return NULL;
172     }
173     bag->type = OBJ_nid2obj(NID_pkcs8ShroudedKeyBag);
174     bag->value.shkeybag = p8;
175     return bag;
176 }
177
178 PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt(int pbe_nid,
179                                                     const char *pass,
180                                                     int passlen,
181                                                     unsigned char *salt,
182                                                     int saltlen, int iter,
183                                                     PKCS8_PRIV_KEY_INFO *p8inf)
184 {
185     PKCS12_SAFEBAG *bag;
186     const EVP_CIPHER *pbe_ciph;
187     X509_SIG *p8;
188
189     pbe_ciph = EVP_get_cipherbynid(pbe_nid);
190
191     if (pbe_ciph)
192         pbe_nid = -1;
193
194     p8 = PKCS8_encrypt(pbe_nid, pbe_ciph, pass, passlen, salt, saltlen, iter,
195                        p8inf);
196
197     if (p8 == NULL) {
198         PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE_PKCS8_ENCRYPT, ERR_R_MALLOC_FAILURE);
199         return NULL;
200     }
201
202     bag = PKCS12_SAFEBAG_create0_pkcs8(p8);
203
204     if (bag == NULL) {
205         PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE_PKCS8_ENCRYPT, ERR_R_MALLOC_FAILURE);
206         X509_SIG_free(p8);
207         return NULL;
208     }
209
210     return bag;
211 }