Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / pkcs12 / p12creat.c
blob29b0892f39096e336f7dd25df8e9eab2330447b7
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #include "pkcs12.h"
38 #include "secitem.h"
39 #include "secport.h"
40 #include "secder.h"
41 #include "secoid.h"
42 #include "p12local.h"
43 #include "secerr.h"
46 /* allocate space for a PFX structure and set up initial
47 * arena pool. pfx structure is cleared and a pointer to
48 * the new structure is returned.
50 SEC_PKCS12PFXItem *
51 sec_pkcs12_new_pfx(void)
53 SEC_PKCS12PFXItem *pfx = NULL;
54 PRArenaPool *poolp = NULL;
56 poolp = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE); /* XXX Different size? */
57 if(poolp == NULL)
58 goto loser;
60 pfx = (SEC_PKCS12PFXItem *)PORT_ArenaZAlloc(poolp,
61 sizeof(SEC_PKCS12PFXItem));
62 if(pfx == NULL)
63 goto loser;
64 pfx->poolp = poolp;
66 return pfx;
68 loser:
69 PORT_FreeArena(poolp, PR_TRUE);
70 return NULL;
73 /* allocate space for a PFX structure and set up initial
74 * arena pool. pfx structure is cleared and a pointer to
75 * the new structure is returned.
77 SEC_PKCS12AuthenticatedSafe *
78 sec_pkcs12_new_asafe(PRArenaPool *poolp)
80 SEC_PKCS12AuthenticatedSafe *asafe = NULL;
81 void *mark;
83 mark = PORT_ArenaMark(poolp);
84 asafe = (SEC_PKCS12AuthenticatedSafe *)PORT_ArenaZAlloc(poolp,
85 sizeof(SEC_PKCS12AuthenticatedSafe));
86 if(asafe == NULL)
87 goto loser;
88 asafe->poolp = poolp;
89 PORT_Memset(&asafe->old_baggage, 0, sizeof(SEC_PKCS7ContentInfo));
91 PORT_ArenaUnmark(poolp, mark);
92 return asafe;
94 loser:
95 PORT_ArenaRelease(poolp, mark);
96 return NULL;
99 /* create a safe contents structure with a list of
100 * length 0 with the first element being NULL
102 SEC_PKCS12SafeContents *
103 sec_pkcs12_create_safe_contents(PRArenaPool *poolp)
105 SEC_PKCS12SafeContents *safe;
106 void *mark;
108 if(poolp == NULL)
109 return NULL;
111 /* allocate structure */
112 mark = PORT_ArenaMark(poolp);
113 safe = (SEC_PKCS12SafeContents *)PORT_ArenaZAlloc(poolp,
114 sizeof(SEC_PKCS12SafeContents));
115 if(safe == NULL)
117 PORT_SetError(SEC_ERROR_NO_MEMORY);
118 PORT_ArenaRelease(poolp, mark);
119 return NULL;
122 /* init list */
123 safe->contents = (SEC_PKCS12SafeBag**)PORT_ArenaZAlloc(poolp,
124 sizeof(SEC_PKCS12SafeBag *));
125 if(safe->contents == NULL) {
126 PORT_SetError(SEC_ERROR_NO_MEMORY);
127 PORT_ArenaRelease(poolp, mark);
128 return NULL;
130 safe->contents[0] = NULL;
131 safe->poolp = poolp;
132 safe->safe_size = 0;
133 PORT_ArenaUnmark(poolp, mark);
134 return safe;
137 /* create a new external bag which is appended onto the list
138 * of bags in baggage. the bag is created in the same arena
139 * as baggage
141 SEC_PKCS12BaggageItem *
142 sec_pkcs12_create_external_bag(SEC_PKCS12Baggage *luggage)
144 void *dummy, *mark;
145 SEC_PKCS12BaggageItem *bag;
147 if(luggage == NULL) {
148 return NULL;
151 mark = PORT_ArenaMark(luggage->poolp);
153 /* allocate space for null terminated bag list */
154 if(luggage->bags == NULL) {
155 luggage->bags=(SEC_PKCS12BaggageItem**)PORT_ArenaZAlloc(luggage->poolp,
156 sizeof(SEC_PKCS12BaggageItem *));
157 if(luggage->bags == NULL) {
158 goto loser;
160 luggage->luggage_size = 0;
163 /* grow the list */
164 dummy = PORT_ArenaGrow(luggage->poolp, luggage->bags,
165 sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 1),
166 sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 2));
167 if(dummy == NULL) {
168 goto loser;
170 luggage->bags = (SEC_PKCS12BaggageItem**)dummy;
172 luggage->bags[luggage->luggage_size] =
173 (SEC_PKCS12BaggageItem *)PORT_ArenaZAlloc(luggage->poolp,
174 sizeof(SEC_PKCS12BaggageItem));
175 if(luggage->bags[luggage->luggage_size] == NULL) {
176 goto loser;
179 /* create new bag and append it to the end */
180 bag = luggage->bags[luggage->luggage_size];
181 bag->espvks = (SEC_PKCS12ESPVKItem **)PORT_ArenaZAlloc(
182 luggage->poolp,
183 sizeof(SEC_PKCS12ESPVKItem *));
184 bag->unencSecrets = (SEC_PKCS12SafeBag **)PORT_ArenaZAlloc(
185 luggage->poolp,
186 sizeof(SEC_PKCS12SafeBag *));
187 if((bag->espvks == NULL) || (bag->unencSecrets == NULL)) {
188 goto loser;
191 bag->poolp = luggage->poolp;
192 luggage->luggage_size++;
193 luggage->bags[luggage->luggage_size] = NULL;
194 bag->espvks[0] = NULL;
195 bag->unencSecrets[0] = NULL;
196 bag->nEspvks = bag->nSecrets = 0;
198 PORT_ArenaUnmark(luggage->poolp, mark);
199 return bag;
201 loser:
202 PORT_ArenaRelease(luggage->poolp, mark);
203 PORT_SetError(SEC_ERROR_NO_MEMORY);
204 return NULL;
207 /* creates a baggage witha NULL terminated 0 length list */
208 SEC_PKCS12Baggage *
209 sec_pkcs12_create_baggage(PRArenaPool *poolp)
211 SEC_PKCS12Baggage *luggage;
212 void *mark;
214 if(poolp == NULL)
215 return NULL;
217 mark = PORT_ArenaMark(poolp);
219 /* allocate bag */
220 luggage = (SEC_PKCS12Baggage *)PORT_ArenaZAlloc(poolp,
221 sizeof(SEC_PKCS12Baggage));
222 if(luggage == NULL)
224 PORT_SetError(SEC_ERROR_NO_MEMORY);
225 PORT_ArenaRelease(poolp, mark);
226 return NULL;
229 /* init list */
230 luggage->bags = (SEC_PKCS12BaggageItem **)PORT_ArenaZAlloc(poolp,
231 sizeof(SEC_PKCS12BaggageItem *));
232 if(luggage->bags == NULL) {
233 PORT_SetError(SEC_ERROR_NO_MEMORY);
234 PORT_ArenaRelease(poolp, mark);
235 return NULL;
238 luggage->bags[0] = NULL;
239 luggage->luggage_size = 0;
240 luggage->poolp = poolp;
242 PORT_ArenaUnmark(poolp, mark);
243 return luggage;
246 /* free pfx structure and associated items in the arena */
247 void
248 SEC_PKCS12DestroyPFX(SEC_PKCS12PFXItem *pfx)
250 if (pfx != NULL && pfx->poolp != NULL)
252 PORT_FreeArena(pfx->poolp, PR_TRUE);