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