|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "pkcs12.h" |
|
6 #include "secitem.h" |
|
7 #include "secport.h" |
|
8 #include "secder.h" |
|
9 #include "secoid.h" |
|
10 #include "p12local.h" |
|
11 #include "secerr.h" |
|
12 |
|
13 |
|
14 /* allocate space for a PFX structure and set up initial |
|
15 * arena pool. pfx structure is cleared and a pointer to |
|
16 * the new structure is returned. |
|
17 */ |
|
18 SEC_PKCS12PFXItem * |
|
19 sec_pkcs12_new_pfx(void) |
|
20 { |
|
21 SEC_PKCS12PFXItem *pfx = NULL; |
|
22 PLArenaPool *poolp = NULL; |
|
23 |
|
24 poolp = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE); /* XXX Different size? */ |
|
25 if(poolp == NULL) |
|
26 goto loser; |
|
27 |
|
28 pfx = (SEC_PKCS12PFXItem *)PORT_ArenaZAlloc(poolp, |
|
29 sizeof(SEC_PKCS12PFXItem)); |
|
30 if(pfx == NULL) |
|
31 goto loser; |
|
32 pfx->poolp = poolp; |
|
33 |
|
34 return pfx; |
|
35 |
|
36 loser: |
|
37 PORT_FreeArena(poolp, PR_TRUE); |
|
38 return NULL; |
|
39 } |
|
40 |
|
41 /* allocate space for a PFX structure and set up initial |
|
42 * arena pool. pfx structure is cleared and a pointer to |
|
43 * the new structure is returned. |
|
44 */ |
|
45 SEC_PKCS12AuthenticatedSafe * |
|
46 sec_pkcs12_new_asafe(PLArenaPool *poolp) |
|
47 { |
|
48 SEC_PKCS12AuthenticatedSafe *asafe = NULL; |
|
49 void *mark; |
|
50 |
|
51 mark = PORT_ArenaMark(poolp); |
|
52 asafe = (SEC_PKCS12AuthenticatedSafe *)PORT_ArenaZAlloc(poolp, |
|
53 sizeof(SEC_PKCS12AuthenticatedSafe)); |
|
54 if(asafe == NULL) |
|
55 goto loser; |
|
56 asafe->poolp = poolp; |
|
57 PORT_Memset(&asafe->old_baggage, 0, sizeof(SEC_PKCS12Baggage_OLD)); |
|
58 |
|
59 PORT_ArenaUnmark(poolp, mark); |
|
60 return asafe; |
|
61 |
|
62 loser: |
|
63 PORT_ArenaRelease(poolp, mark); |
|
64 return NULL; |
|
65 } |
|
66 |
|
67 /* create a safe contents structure with a list of |
|
68 * length 0 with the first element being NULL |
|
69 */ |
|
70 SEC_PKCS12SafeContents * |
|
71 sec_pkcs12_create_safe_contents(PLArenaPool *poolp) |
|
72 { |
|
73 SEC_PKCS12SafeContents *safe; |
|
74 void *mark; |
|
75 |
|
76 if(poolp == NULL) |
|
77 return NULL; |
|
78 |
|
79 /* allocate structure */ |
|
80 mark = PORT_ArenaMark(poolp); |
|
81 safe = (SEC_PKCS12SafeContents *)PORT_ArenaZAlloc(poolp, |
|
82 sizeof(SEC_PKCS12SafeContents)); |
|
83 if(safe == NULL) |
|
84 { |
|
85 PORT_SetError(SEC_ERROR_NO_MEMORY); |
|
86 PORT_ArenaRelease(poolp, mark); |
|
87 return NULL; |
|
88 } |
|
89 |
|
90 /* init list */ |
|
91 safe->contents = (SEC_PKCS12SafeBag**)PORT_ArenaZAlloc(poolp, |
|
92 sizeof(SEC_PKCS12SafeBag *)); |
|
93 if(safe->contents == NULL) { |
|
94 PORT_SetError(SEC_ERROR_NO_MEMORY); |
|
95 PORT_ArenaRelease(poolp, mark); |
|
96 return NULL; |
|
97 } |
|
98 safe->contents[0] = NULL; |
|
99 safe->poolp = poolp; |
|
100 safe->safe_size = 0; |
|
101 PORT_ArenaUnmark(poolp, mark); |
|
102 return safe; |
|
103 } |
|
104 |
|
105 /* create a new external bag which is appended onto the list |
|
106 * of bags in baggage. the bag is created in the same arena |
|
107 * as baggage |
|
108 */ |
|
109 SEC_PKCS12BaggageItem * |
|
110 sec_pkcs12_create_external_bag(SEC_PKCS12Baggage *luggage) |
|
111 { |
|
112 void *dummy, *mark; |
|
113 SEC_PKCS12BaggageItem *bag; |
|
114 |
|
115 if(luggage == NULL) { |
|
116 return NULL; |
|
117 } |
|
118 |
|
119 mark = PORT_ArenaMark(luggage->poolp); |
|
120 |
|
121 /* allocate space for null terminated bag list */ |
|
122 if(luggage->bags == NULL) { |
|
123 luggage->bags=(SEC_PKCS12BaggageItem**)PORT_ArenaZAlloc(luggage->poolp, |
|
124 sizeof(SEC_PKCS12BaggageItem *)); |
|
125 if(luggage->bags == NULL) { |
|
126 goto loser; |
|
127 } |
|
128 luggage->luggage_size = 0; |
|
129 } |
|
130 |
|
131 /* grow the list */ |
|
132 dummy = PORT_ArenaGrow(luggage->poolp, luggage->bags, |
|
133 sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 1), |
|
134 sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 2)); |
|
135 if(dummy == NULL) { |
|
136 goto loser; |
|
137 } |
|
138 luggage->bags = (SEC_PKCS12BaggageItem**)dummy; |
|
139 |
|
140 luggage->bags[luggage->luggage_size] = |
|
141 (SEC_PKCS12BaggageItem *)PORT_ArenaZAlloc(luggage->poolp, |
|
142 sizeof(SEC_PKCS12BaggageItem)); |
|
143 if(luggage->bags[luggage->luggage_size] == NULL) { |
|
144 goto loser; |
|
145 } |
|
146 |
|
147 /* create new bag and append it to the end */ |
|
148 bag = luggage->bags[luggage->luggage_size]; |
|
149 bag->espvks = (SEC_PKCS12ESPVKItem **)PORT_ArenaZAlloc( |
|
150 luggage->poolp, |
|
151 sizeof(SEC_PKCS12ESPVKItem *)); |
|
152 bag->unencSecrets = (SEC_PKCS12SafeBag **)PORT_ArenaZAlloc( |
|
153 luggage->poolp, |
|
154 sizeof(SEC_PKCS12SafeBag *)); |
|
155 if((bag->espvks == NULL) || (bag->unencSecrets == NULL)) { |
|
156 goto loser; |
|
157 } |
|
158 |
|
159 bag->poolp = luggage->poolp; |
|
160 luggage->luggage_size++; |
|
161 luggage->bags[luggage->luggage_size] = NULL; |
|
162 bag->espvks[0] = NULL; |
|
163 bag->unencSecrets[0] = NULL; |
|
164 bag->nEspvks = bag->nSecrets = 0; |
|
165 |
|
166 PORT_ArenaUnmark(luggage->poolp, mark); |
|
167 return bag; |
|
168 |
|
169 loser: |
|
170 PORT_ArenaRelease(luggage->poolp, mark); |
|
171 PORT_SetError(SEC_ERROR_NO_MEMORY); |
|
172 return NULL; |
|
173 } |
|
174 |
|
175 /* creates a baggage witha NULL terminated 0 length list */ |
|
176 SEC_PKCS12Baggage * |
|
177 sec_pkcs12_create_baggage(PLArenaPool *poolp) |
|
178 { |
|
179 SEC_PKCS12Baggage *luggage; |
|
180 void *mark; |
|
181 |
|
182 if(poolp == NULL) |
|
183 return NULL; |
|
184 |
|
185 mark = PORT_ArenaMark(poolp); |
|
186 |
|
187 /* allocate bag */ |
|
188 luggage = (SEC_PKCS12Baggage *)PORT_ArenaZAlloc(poolp, |
|
189 sizeof(SEC_PKCS12Baggage)); |
|
190 if(luggage == NULL) |
|
191 { |
|
192 PORT_SetError(SEC_ERROR_NO_MEMORY); |
|
193 PORT_ArenaRelease(poolp, mark); |
|
194 return NULL; |
|
195 } |
|
196 |
|
197 /* init list */ |
|
198 luggage->bags = (SEC_PKCS12BaggageItem **)PORT_ArenaZAlloc(poolp, |
|
199 sizeof(SEC_PKCS12BaggageItem *)); |
|
200 if(luggage->bags == NULL) { |
|
201 PORT_SetError(SEC_ERROR_NO_MEMORY); |
|
202 PORT_ArenaRelease(poolp, mark); |
|
203 return NULL; |
|
204 } |
|
205 |
|
206 luggage->bags[0] = NULL; |
|
207 luggage->luggage_size = 0; |
|
208 luggage->poolp = poolp; |
|
209 |
|
210 PORT_ArenaUnmark(poolp, mark); |
|
211 return luggage; |
|
212 } |
|
213 |
|
214 /* free pfx structure and associated items in the arena */ |
|
215 void |
|
216 SEC_PKCS12DestroyPFX(SEC_PKCS12PFXItem *pfx) |
|
217 { |
|
218 if (pfx != NULL && pfx->poolp != NULL) |
|
219 { |
|
220 PORT_FreeArena(pfx->poolp, PR_TRUE); |
|
221 } |
|
222 } |