michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "pkcs12.h" michael@0: #include "secitem.h" michael@0: #include "secport.h" michael@0: #include "secder.h" michael@0: #include "secoid.h" michael@0: #include "p12local.h" michael@0: #include "secerr.h" michael@0: michael@0: michael@0: /* allocate space for a PFX structure and set up initial michael@0: * arena pool. pfx structure is cleared and a pointer to michael@0: * the new structure is returned. michael@0: */ michael@0: SEC_PKCS12PFXItem * michael@0: sec_pkcs12_new_pfx(void) michael@0: { michael@0: SEC_PKCS12PFXItem *pfx = NULL; michael@0: PLArenaPool *poolp = NULL; michael@0: michael@0: poolp = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE); /* XXX Different size? */ michael@0: if(poolp == NULL) michael@0: goto loser; michael@0: michael@0: pfx = (SEC_PKCS12PFXItem *)PORT_ArenaZAlloc(poolp, michael@0: sizeof(SEC_PKCS12PFXItem)); michael@0: if(pfx == NULL) michael@0: goto loser; michael@0: pfx->poolp = poolp; michael@0: michael@0: return pfx; michael@0: michael@0: loser: michael@0: PORT_FreeArena(poolp, PR_TRUE); michael@0: return NULL; michael@0: } michael@0: michael@0: /* allocate space for a PFX structure and set up initial michael@0: * arena pool. pfx structure is cleared and a pointer to michael@0: * the new structure is returned. michael@0: */ michael@0: SEC_PKCS12AuthenticatedSafe * michael@0: sec_pkcs12_new_asafe(PLArenaPool *poolp) michael@0: { michael@0: SEC_PKCS12AuthenticatedSafe *asafe = NULL; michael@0: void *mark; michael@0: michael@0: mark = PORT_ArenaMark(poolp); michael@0: asafe = (SEC_PKCS12AuthenticatedSafe *)PORT_ArenaZAlloc(poolp, michael@0: sizeof(SEC_PKCS12AuthenticatedSafe)); michael@0: if(asafe == NULL) michael@0: goto loser; michael@0: asafe->poolp = poolp; michael@0: PORT_Memset(&asafe->old_baggage, 0, sizeof(SEC_PKCS12Baggage_OLD)); michael@0: michael@0: PORT_ArenaUnmark(poolp, mark); michael@0: return asafe; michael@0: michael@0: loser: michael@0: PORT_ArenaRelease(poolp, mark); michael@0: return NULL; michael@0: } michael@0: michael@0: /* create a safe contents structure with a list of michael@0: * length 0 with the first element being NULL michael@0: */ michael@0: SEC_PKCS12SafeContents * michael@0: sec_pkcs12_create_safe_contents(PLArenaPool *poolp) michael@0: { michael@0: SEC_PKCS12SafeContents *safe; michael@0: void *mark; michael@0: michael@0: if(poolp == NULL) michael@0: return NULL; michael@0: michael@0: /* allocate structure */ michael@0: mark = PORT_ArenaMark(poolp); michael@0: safe = (SEC_PKCS12SafeContents *)PORT_ArenaZAlloc(poolp, michael@0: sizeof(SEC_PKCS12SafeContents)); michael@0: if(safe == NULL) michael@0: { michael@0: PORT_SetError(SEC_ERROR_NO_MEMORY); michael@0: PORT_ArenaRelease(poolp, mark); michael@0: return NULL; michael@0: } michael@0: michael@0: /* init list */ michael@0: safe->contents = (SEC_PKCS12SafeBag**)PORT_ArenaZAlloc(poolp, michael@0: sizeof(SEC_PKCS12SafeBag *)); michael@0: if(safe->contents == NULL) { michael@0: PORT_SetError(SEC_ERROR_NO_MEMORY); michael@0: PORT_ArenaRelease(poolp, mark); michael@0: return NULL; michael@0: } michael@0: safe->contents[0] = NULL; michael@0: safe->poolp = poolp; michael@0: safe->safe_size = 0; michael@0: PORT_ArenaUnmark(poolp, mark); michael@0: return safe; michael@0: } michael@0: michael@0: /* create a new external bag which is appended onto the list michael@0: * of bags in baggage. the bag is created in the same arena michael@0: * as baggage michael@0: */ michael@0: SEC_PKCS12BaggageItem * michael@0: sec_pkcs12_create_external_bag(SEC_PKCS12Baggage *luggage) michael@0: { michael@0: void *dummy, *mark; michael@0: SEC_PKCS12BaggageItem *bag; michael@0: michael@0: if(luggage == NULL) { michael@0: return NULL; michael@0: } michael@0: michael@0: mark = PORT_ArenaMark(luggage->poolp); michael@0: michael@0: /* allocate space for null terminated bag list */ michael@0: if(luggage->bags == NULL) { michael@0: luggage->bags=(SEC_PKCS12BaggageItem**)PORT_ArenaZAlloc(luggage->poolp, michael@0: sizeof(SEC_PKCS12BaggageItem *)); michael@0: if(luggage->bags == NULL) { michael@0: goto loser; michael@0: } michael@0: luggage->luggage_size = 0; michael@0: } michael@0: michael@0: /* grow the list */ michael@0: dummy = PORT_ArenaGrow(luggage->poolp, luggage->bags, michael@0: sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 1), michael@0: sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 2)); michael@0: if(dummy == NULL) { michael@0: goto loser; michael@0: } michael@0: luggage->bags = (SEC_PKCS12BaggageItem**)dummy; michael@0: michael@0: luggage->bags[luggage->luggage_size] = michael@0: (SEC_PKCS12BaggageItem *)PORT_ArenaZAlloc(luggage->poolp, michael@0: sizeof(SEC_PKCS12BaggageItem)); michael@0: if(luggage->bags[luggage->luggage_size] == NULL) { michael@0: goto loser; michael@0: } michael@0: michael@0: /* create new bag and append it to the end */ michael@0: bag = luggage->bags[luggage->luggage_size]; michael@0: bag->espvks = (SEC_PKCS12ESPVKItem **)PORT_ArenaZAlloc( michael@0: luggage->poolp, michael@0: sizeof(SEC_PKCS12ESPVKItem *)); michael@0: bag->unencSecrets = (SEC_PKCS12SafeBag **)PORT_ArenaZAlloc( michael@0: luggage->poolp, michael@0: sizeof(SEC_PKCS12SafeBag *)); michael@0: if((bag->espvks == NULL) || (bag->unencSecrets == NULL)) { michael@0: goto loser; michael@0: } michael@0: michael@0: bag->poolp = luggage->poolp; michael@0: luggage->luggage_size++; michael@0: luggage->bags[luggage->luggage_size] = NULL; michael@0: bag->espvks[0] = NULL; michael@0: bag->unencSecrets[0] = NULL; michael@0: bag->nEspvks = bag->nSecrets = 0; michael@0: michael@0: PORT_ArenaUnmark(luggage->poolp, mark); michael@0: return bag; michael@0: michael@0: loser: michael@0: PORT_ArenaRelease(luggage->poolp, mark); michael@0: PORT_SetError(SEC_ERROR_NO_MEMORY); michael@0: return NULL; michael@0: } michael@0: michael@0: /* creates a baggage witha NULL terminated 0 length list */ michael@0: SEC_PKCS12Baggage * michael@0: sec_pkcs12_create_baggage(PLArenaPool *poolp) michael@0: { michael@0: SEC_PKCS12Baggage *luggage; michael@0: void *mark; michael@0: michael@0: if(poolp == NULL) michael@0: return NULL; michael@0: michael@0: mark = PORT_ArenaMark(poolp); michael@0: michael@0: /* allocate bag */ michael@0: luggage = (SEC_PKCS12Baggage *)PORT_ArenaZAlloc(poolp, michael@0: sizeof(SEC_PKCS12Baggage)); michael@0: if(luggage == NULL) michael@0: { michael@0: PORT_SetError(SEC_ERROR_NO_MEMORY); michael@0: PORT_ArenaRelease(poolp, mark); michael@0: return NULL; michael@0: } michael@0: michael@0: /* init list */ michael@0: luggage->bags = (SEC_PKCS12BaggageItem **)PORT_ArenaZAlloc(poolp, michael@0: sizeof(SEC_PKCS12BaggageItem *)); michael@0: if(luggage->bags == NULL) { michael@0: PORT_SetError(SEC_ERROR_NO_MEMORY); michael@0: PORT_ArenaRelease(poolp, mark); michael@0: return NULL; michael@0: } michael@0: michael@0: luggage->bags[0] = NULL; michael@0: luggage->luggage_size = 0; michael@0: luggage->poolp = poolp; michael@0: michael@0: PORT_ArenaUnmark(poolp, mark); michael@0: return luggage; michael@0: } michael@0: michael@0: /* free pfx structure and associated items in the arena */ michael@0: void michael@0: SEC_PKCS12DestroyPFX(SEC_PKCS12PFXItem *pfx) michael@0: { michael@0: if (pfx != NULL && pfx->poolp != NULL) michael@0: { michael@0: PORT_FreeArena(pfx->poolp, PR_TRUE); michael@0: } michael@0: }