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