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