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
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.
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 ***** */
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.
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? */
60 pfx
= (SEC_PKCS12PFXItem
*)PORT_ArenaZAlloc(poolp
,
61 sizeof(SEC_PKCS12PFXItem
));
69 PORT_FreeArena(poolp
, PR_TRUE
);
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
;
83 mark
= PORT_ArenaMark(poolp
);
84 asafe
= (SEC_PKCS12AuthenticatedSafe
*)PORT_ArenaZAlloc(poolp
,
85 sizeof(SEC_PKCS12AuthenticatedSafe
));
89 PORT_Memset(&asafe
->old_baggage
, 0, sizeof(SEC_PKCS7ContentInfo
));
91 PORT_ArenaUnmark(poolp
, mark
);
95 PORT_ArenaRelease(poolp
, mark
);
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
;
111 /* allocate structure */
112 mark
= PORT_ArenaMark(poolp
);
113 safe
= (SEC_PKCS12SafeContents
*)PORT_ArenaZAlloc(poolp
,
114 sizeof(SEC_PKCS12SafeContents
));
117 PORT_SetError(SEC_ERROR_NO_MEMORY
);
118 PORT_ArenaRelease(poolp
, mark
);
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
);
130 safe
->contents
[0] = NULL
;
133 PORT_ArenaUnmark(poolp
, mark
);
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
141 SEC_PKCS12BaggageItem
*
142 sec_pkcs12_create_external_bag(SEC_PKCS12Baggage
*luggage
)
145 SEC_PKCS12BaggageItem
*bag
;
147 if(luggage
== 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
) {
160 luggage
->luggage_size
= 0;
164 dummy
= PORT_ArenaGrow(luggage
->poolp
, luggage
->bags
,
165 sizeof(SEC_PKCS12BaggageItem
*) * (luggage
->luggage_size
+ 1),
166 sizeof(SEC_PKCS12BaggageItem
*) * (luggage
->luggage_size
+ 2));
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
) {
179 /* create new bag and append it to the end */
180 bag
= luggage
->bags
[luggage
->luggage_size
];
181 bag
->espvks
= (SEC_PKCS12ESPVKItem
**)PORT_ArenaZAlloc(
183 sizeof(SEC_PKCS12ESPVKItem
*));
184 bag
->unencSecrets
= (SEC_PKCS12SafeBag
**)PORT_ArenaZAlloc(
186 sizeof(SEC_PKCS12SafeBag
*));
187 if((bag
->espvks
== NULL
) || (bag
->unencSecrets
== NULL
)) {
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
);
202 PORT_ArenaRelease(luggage
->poolp
, mark
);
203 PORT_SetError(SEC_ERROR_NO_MEMORY
);
207 /* creates a baggage witha NULL terminated 0 length list */
209 sec_pkcs12_create_baggage(PRArenaPool
*poolp
)
211 SEC_PKCS12Baggage
*luggage
;
217 mark
= PORT_ArenaMark(poolp
);
220 luggage
= (SEC_PKCS12Baggage
*)PORT_ArenaZAlloc(poolp
,
221 sizeof(SEC_PKCS12Baggage
));
224 PORT_SetError(SEC_ERROR_NO_MEMORY
);
225 PORT_ArenaRelease(poolp
, mark
);
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
);
238 luggage
->bags
[0] = NULL
;
239 luggage
->luggage_size
= 0;
240 luggage
->poolp
= poolp
;
242 PORT_ArenaUnmark(poolp
, mark
);
246 /* free pfx structure and associated items in the arena */
248 SEC_PKCS12DestroyPFX(SEC_PKCS12PFXItem
*pfx
)
250 if (pfx
!= NULL
&& pfx
->poolp
!= NULL
)
252 PORT_FreeArena(pfx
->poolp
, PR_TRUE
);