security/nss/lib/pkcs12/p12creat.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/pkcs12/p12creat.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,222 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "pkcs12.h"
     1.9 +#include "secitem.h"
    1.10 +#include "secport.h"
    1.11 +#include "secder.h"
    1.12 +#include "secoid.h"
    1.13 +#include "p12local.h"
    1.14 +#include "secerr.h"
    1.15 +
    1.16 +
    1.17 +/* allocate space for a PFX structure and set up initial
    1.18 + * arena pool.  pfx structure is cleared and a pointer to
    1.19 + * the new structure is returned.
    1.20 + */
    1.21 +SEC_PKCS12PFXItem *
    1.22 +sec_pkcs12_new_pfx(void)
    1.23 +{
    1.24 +    SEC_PKCS12PFXItem   *pfx = NULL;
    1.25 +    PLArenaPool     *poolp = NULL;
    1.26 +
    1.27 +    poolp = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);	/* XXX Different size? */
    1.28 +    if(poolp == NULL)
    1.29 +	goto loser;
    1.30 +
    1.31 +    pfx = (SEC_PKCS12PFXItem *)PORT_ArenaZAlloc(poolp, 
    1.32 +	sizeof(SEC_PKCS12PFXItem));
    1.33 +    if(pfx == NULL)
    1.34 +	goto loser;
    1.35 +    pfx->poolp = poolp;
    1.36 +
    1.37 +    return pfx;
    1.38 +
    1.39 +loser:
    1.40 +    PORT_FreeArena(poolp, PR_TRUE);
    1.41 +    return NULL;
    1.42 +}
    1.43 +
    1.44 +/* allocate space for a PFX structure and set up initial
    1.45 + * arena pool.  pfx structure is cleared and a pointer to
    1.46 + * the new structure is returned.
    1.47 + */
    1.48 +SEC_PKCS12AuthenticatedSafe *
    1.49 +sec_pkcs12_new_asafe(PLArenaPool *poolp)
    1.50 +{
    1.51 +    SEC_PKCS12AuthenticatedSafe  *asafe = NULL;
    1.52 +    void *mark;
    1.53 +
    1.54 +    mark = PORT_ArenaMark(poolp);
    1.55 +    asafe = (SEC_PKCS12AuthenticatedSafe *)PORT_ArenaZAlloc(poolp, 
    1.56 +	sizeof(SEC_PKCS12AuthenticatedSafe));
    1.57 +    if(asafe == NULL)
    1.58 +	goto loser;
    1.59 +    asafe->poolp = poolp;
    1.60 +    PORT_Memset(&asafe->old_baggage, 0, sizeof(SEC_PKCS12Baggage_OLD));
    1.61 +
    1.62 +    PORT_ArenaUnmark(poolp, mark);
    1.63 +    return asafe;
    1.64 +
    1.65 +loser:
    1.66 +    PORT_ArenaRelease(poolp, mark);
    1.67 +    return NULL;
    1.68 +}
    1.69 +
    1.70 +/* create a safe contents structure with a list of
    1.71 + * length 0 with the first element being NULL 
    1.72 + */
    1.73 +SEC_PKCS12SafeContents *
    1.74 +sec_pkcs12_create_safe_contents(PLArenaPool *poolp)
    1.75 +{
    1.76 +    SEC_PKCS12SafeContents *safe;
    1.77 +    void *mark;
    1.78 +
    1.79 +    if(poolp == NULL)
    1.80 +	return NULL;
    1.81 +
    1.82 +    /* allocate structure */
    1.83 +    mark = PORT_ArenaMark(poolp);
    1.84 +    safe = (SEC_PKCS12SafeContents *)PORT_ArenaZAlloc(poolp, 
    1.85 +	sizeof(SEC_PKCS12SafeContents));
    1.86 +    if(safe == NULL)
    1.87 +    {
    1.88 +	PORT_SetError(SEC_ERROR_NO_MEMORY);
    1.89 +	PORT_ArenaRelease(poolp, mark);
    1.90 +	return NULL;
    1.91 +    }
    1.92 +
    1.93 +    /* init list */
    1.94 +    safe->contents = (SEC_PKCS12SafeBag**)PORT_ArenaZAlloc(poolp, 
    1.95 +						  sizeof(SEC_PKCS12SafeBag *));
    1.96 +    if(safe->contents == NULL) {
    1.97 +	PORT_SetError(SEC_ERROR_NO_MEMORY);
    1.98 +	PORT_ArenaRelease(poolp, mark);
    1.99 +	return NULL;
   1.100 +    }
   1.101 +    safe->contents[0] = NULL;
   1.102 +    safe->poolp       = poolp;
   1.103 +    safe->safe_size   = 0;
   1.104 +    PORT_ArenaUnmark(poolp, mark);
   1.105 +    return safe;
   1.106 +}
   1.107 +
   1.108 +/* create a new external bag which is appended onto the list
   1.109 + * of bags in baggage.  the bag is created in the same arena
   1.110 + * as baggage
   1.111 + */
   1.112 +SEC_PKCS12BaggageItem *
   1.113 +sec_pkcs12_create_external_bag(SEC_PKCS12Baggage *luggage)
   1.114 +{
   1.115 +    void *dummy, *mark;
   1.116 +    SEC_PKCS12BaggageItem *bag;
   1.117 +
   1.118 +    if(luggage == NULL) {
   1.119 +	return NULL;
   1.120 +    }
   1.121 +
   1.122 +    mark = PORT_ArenaMark(luggage->poolp);
   1.123 +
   1.124 +    /* allocate space for null terminated bag list */
   1.125 +    if(luggage->bags == NULL) {
   1.126 +	luggage->bags=(SEC_PKCS12BaggageItem**)PORT_ArenaZAlloc(luggage->poolp, 
   1.127 +					sizeof(SEC_PKCS12BaggageItem *));
   1.128 +	if(luggage->bags == NULL) {
   1.129 +	    goto loser;
   1.130 +	}
   1.131 +	luggage->luggage_size = 0;
   1.132 +    }
   1.133 +
   1.134 +    /* grow the list */    
   1.135 +    dummy = PORT_ArenaGrow(luggage->poolp, luggage->bags,
   1.136 +    			sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 1),
   1.137 +    			sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 2));
   1.138 +    if(dummy == NULL) {
   1.139 +	goto loser;
   1.140 +    }
   1.141 +    luggage->bags = (SEC_PKCS12BaggageItem**)dummy;
   1.142 +
   1.143 +    luggage->bags[luggage->luggage_size] = 
   1.144 +    		(SEC_PKCS12BaggageItem *)PORT_ArenaZAlloc(luggage->poolp,
   1.145 +    							sizeof(SEC_PKCS12BaggageItem));
   1.146 +    if(luggage->bags[luggage->luggage_size] == NULL) {
   1.147 +	goto loser;
   1.148 +    }
   1.149 +
   1.150 +    /* create new bag and append it to the end */
   1.151 +    bag = luggage->bags[luggage->luggage_size];
   1.152 +    bag->espvks = (SEC_PKCS12ESPVKItem **)PORT_ArenaZAlloc(
   1.153 +    						luggage->poolp,
   1.154 +    						sizeof(SEC_PKCS12ESPVKItem *));
   1.155 +    bag->unencSecrets = (SEC_PKCS12SafeBag **)PORT_ArenaZAlloc(
   1.156 +    						luggage->poolp,
   1.157 +    						sizeof(SEC_PKCS12SafeBag *));
   1.158 +    if((bag->espvks == NULL) || (bag->unencSecrets == NULL)) {
   1.159 +	goto loser;
   1.160 +    }
   1.161 +
   1.162 +    bag->poolp = luggage->poolp;
   1.163 +    luggage->luggage_size++;
   1.164 +    luggage->bags[luggage->luggage_size] = NULL;
   1.165 +    bag->espvks[0] = NULL;
   1.166 +    bag->unencSecrets[0] = NULL;
   1.167 +    bag->nEspvks = bag->nSecrets = 0;
   1.168 +
   1.169 +    PORT_ArenaUnmark(luggage->poolp, mark);
   1.170 +    return bag;
   1.171 +
   1.172 +loser:
   1.173 +    PORT_ArenaRelease(luggage->poolp, mark);
   1.174 +    PORT_SetError(SEC_ERROR_NO_MEMORY);
   1.175 +    return NULL;
   1.176 +}
   1.177 +
   1.178 +/* creates a baggage witha NULL terminated 0 length list */
   1.179 +SEC_PKCS12Baggage *
   1.180 +sec_pkcs12_create_baggage(PLArenaPool *poolp)
   1.181 +{
   1.182 +    SEC_PKCS12Baggage *luggage;
   1.183 +    void *mark;
   1.184 +
   1.185 +    if(poolp == NULL)
   1.186 +	return NULL;
   1.187 +
   1.188 +    mark = PORT_ArenaMark(poolp);
   1.189 +
   1.190 +    /* allocate bag */
   1.191 +    luggage = (SEC_PKCS12Baggage *)PORT_ArenaZAlloc(poolp, 
   1.192 +	sizeof(SEC_PKCS12Baggage));
   1.193 +    if(luggage == NULL)
   1.194 +    {
   1.195 +	PORT_SetError(SEC_ERROR_NO_MEMORY);
   1.196 +	PORT_ArenaRelease(poolp, mark);
   1.197 +	return NULL;
   1.198 +    }
   1.199 +
   1.200 +    /* init list */
   1.201 +    luggage->bags = (SEC_PKCS12BaggageItem **)PORT_ArenaZAlloc(poolp,
   1.202 +    					sizeof(SEC_PKCS12BaggageItem *));
   1.203 +    if(luggage->bags == NULL) {
   1.204 +	PORT_SetError(SEC_ERROR_NO_MEMORY);
   1.205 +	PORT_ArenaRelease(poolp, mark);
   1.206 +	return NULL;
   1.207 +    }
   1.208 +
   1.209 +    luggage->bags[0] = NULL;
   1.210 +    luggage->luggage_size = 0;
   1.211 +    luggage->poolp = poolp;
   1.212 +
   1.213 +    PORT_ArenaUnmark(poolp, mark);
   1.214 +    return luggage;
   1.215 +}
   1.216 +
   1.217 +/* free pfx structure and associated items in the arena */
   1.218 +void 
   1.219 +SEC_PKCS12DestroyPFX(SEC_PKCS12PFXItem *pfx)
   1.220 +{
   1.221 +    if (pfx != NULL && pfx->poolp != NULL)
   1.222 +    {
   1.223 +	PORT_FreeArena(pfx->poolp, PR_TRUE);
   1.224 +    }
   1.225 +}

mercurial