PageRenderTime 58ms CodeModel.GetById 18ms app.highlight 34ms RepoModel.GetById 2ms app.codeStats 0ms

/security/nss/lib/pkcs12/p12creat.c

http://github.com/zpao/v8monkey
C | 254 lines | 162 code | 33 blank | 59 comment | 36 complexity | 28f93ddb508bdd4b0d31d3b1821e17ec MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  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/
  8 *
  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.
 13 *
 14 * The Original Code is the Netscape security libraries.
 15 *
 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.
 20 *
 21 * Contributor(s):
 22 *
 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.
 34 *
 35 * ***** END LICENSE BLOCK ***** */
 36
 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"
 44
 45
 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.
 49 */
 50SEC_PKCS12PFXItem *
 51sec_pkcs12_new_pfx(void)
 52{
 53    SEC_PKCS12PFXItem   *pfx = NULL;
 54    PRArenaPool     *poolp = NULL;
 55
 56    poolp = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);	/* XXX Different size? */
 57    if(poolp == NULL)
 58	goto loser;
 59
 60    pfx = (SEC_PKCS12PFXItem *)PORT_ArenaZAlloc(poolp, 
 61	sizeof(SEC_PKCS12PFXItem));
 62    if(pfx == NULL)
 63	goto loser;
 64    pfx->poolp = poolp;
 65
 66    return pfx;
 67
 68loser:
 69    PORT_FreeArena(poolp, PR_TRUE);
 70    return NULL;
 71}
 72
 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.
 76 */
 77SEC_PKCS12AuthenticatedSafe *
 78sec_pkcs12_new_asafe(PRArenaPool *poolp)
 79{
 80    SEC_PKCS12AuthenticatedSafe  *asafe = NULL;
 81    void *mark;
 82
 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));
 90
 91    PORT_ArenaUnmark(poolp, mark);
 92    return asafe;
 93
 94loser:
 95    PORT_ArenaRelease(poolp, mark);
 96    return NULL;
 97}
 98
 99/* create a safe contents structure with a list of
100 * length 0 with the first element being NULL 
101 */
102SEC_PKCS12SafeContents *
103sec_pkcs12_create_safe_contents(PRArenaPool *poolp)
104{
105    SEC_PKCS12SafeContents *safe;
106    void *mark;
107
108    if(poolp == NULL)
109	return NULL;
110
111    /* allocate structure */
112    mark = PORT_ArenaMark(poolp);
113    safe = (SEC_PKCS12SafeContents *)PORT_ArenaZAlloc(poolp, 
114	sizeof(SEC_PKCS12SafeContents));
115    if(safe == NULL)
116    {
117	PORT_SetError(SEC_ERROR_NO_MEMORY);
118	PORT_ArenaRelease(poolp, mark);
119	return NULL;
120    }
121
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;
129    }
130    safe->contents[0] = NULL;
131    safe->poolp       = poolp;
132    safe->safe_size   = 0;
133    PORT_ArenaUnmark(poolp, mark);
134    return safe;
135}
136
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
140 */
141SEC_PKCS12BaggageItem *
142sec_pkcs12_create_external_bag(SEC_PKCS12Baggage *luggage)
143{
144    void *dummy, *mark;
145    SEC_PKCS12BaggageItem *bag;
146
147    if(luggage == NULL) {
148	return NULL;
149    }
150
151    mark = PORT_ArenaMark(luggage->poolp);
152
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;
159	}
160	luggage->luggage_size = 0;
161    }
162
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;
169    }
170    luggage->bags = (SEC_PKCS12BaggageItem**)dummy;
171
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;
177    }
178
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;
189    }
190
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;
197
198    PORT_ArenaUnmark(luggage->poolp, mark);
199    return bag;
200
201loser:
202    PORT_ArenaRelease(luggage->poolp, mark);
203    PORT_SetError(SEC_ERROR_NO_MEMORY);
204    return NULL;
205}
206
207/* creates a baggage witha NULL terminated 0 length list */
208SEC_PKCS12Baggage *
209sec_pkcs12_create_baggage(PRArenaPool *poolp)
210{
211    SEC_PKCS12Baggage *luggage;
212    void *mark;
213
214    if(poolp == NULL)
215	return NULL;
216
217    mark = PORT_ArenaMark(poolp);
218
219    /* allocate bag */
220    luggage = (SEC_PKCS12Baggage *)PORT_ArenaZAlloc(poolp, 
221	sizeof(SEC_PKCS12Baggage));
222    if(luggage == NULL)
223    {
224	PORT_SetError(SEC_ERROR_NO_MEMORY);
225	PORT_ArenaRelease(poolp, mark);
226	return NULL;
227    }
228
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;
236    }
237
238    luggage->bags[0] = NULL;
239    luggage->luggage_size = 0;
240    luggage->poolp = poolp;
241
242    PORT_ArenaUnmark(poolp, mark);
243    return luggage;
244}
245
246/* free pfx structure and associated items in the arena */
247void 
248SEC_PKCS12DestroyPFX(SEC_PKCS12PFXItem *pfx)
249{
250    if (pfx != NULL && pfx->poolp != NULL)
251    {
252	PORT_FreeArena(pfx->poolp, PR_TRUE);
253    }
254}