security/nss/lib/pkcs12/p12creat.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial