security/nss/lib/pkcs12/p12creat.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     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/. */
     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"
    14 /* allocate space for a PFX structure and set up initial
    15  * arena pool.  pfx structure is cleared and a pointer to
    16  * the new structure is returned.
    17  */
    18 SEC_PKCS12PFXItem *
    19 sec_pkcs12_new_pfx(void)
    20 {
    21     SEC_PKCS12PFXItem   *pfx = NULL;
    22     PLArenaPool     *poolp = NULL;
    24     poolp = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);	/* XXX Different size? */
    25     if(poolp == NULL)
    26 	goto loser;
    28     pfx = (SEC_PKCS12PFXItem *)PORT_ArenaZAlloc(poolp, 
    29 	sizeof(SEC_PKCS12PFXItem));
    30     if(pfx == NULL)
    31 	goto loser;
    32     pfx->poolp = poolp;
    34     return pfx;
    36 loser:
    37     PORT_FreeArena(poolp, PR_TRUE);
    38     return NULL;
    39 }
    41 /* allocate space for a PFX structure and set up initial
    42  * arena pool.  pfx structure is cleared and a pointer to
    43  * the new structure is returned.
    44  */
    45 SEC_PKCS12AuthenticatedSafe *
    46 sec_pkcs12_new_asafe(PLArenaPool *poolp)
    47 {
    48     SEC_PKCS12AuthenticatedSafe  *asafe = NULL;
    49     void *mark;
    51     mark = PORT_ArenaMark(poolp);
    52     asafe = (SEC_PKCS12AuthenticatedSafe *)PORT_ArenaZAlloc(poolp, 
    53 	sizeof(SEC_PKCS12AuthenticatedSafe));
    54     if(asafe == NULL)
    55 	goto loser;
    56     asafe->poolp = poolp;
    57     PORT_Memset(&asafe->old_baggage, 0, sizeof(SEC_PKCS12Baggage_OLD));
    59     PORT_ArenaUnmark(poolp, mark);
    60     return asafe;
    62 loser:
    63     PORT_ArenaRelease(poolp, mark);
    64     return NULL;
    65 }
    67 /* create a safe contents structure with a list of
    68  * length 0 with the first element being NULL 
    69  */
    70 SEC_PKCS12SafeContents *
    71 sec_pkcs12_create_safe_contents(PLArenaPool *poolp)
    72 {
    73     SEC_PKCS12SafeContents *safe;
    74     void *mark;
    76     if(poolp == NULL)
    77 	return NULL;
    79     /* allocate structure */
    80     mark = PORT_ArenaMark(poolp);
    81     safe = (SEC_PKCS12SafeContents *)PORT_ArenaZAlloc(poolp, 
    82 	sizeof(SEC_PKCS12SafeContents));
    83     if(safe == NULL)
    84     {
    85 	PORT_SetError(SEC_ERROR_NO_MEMORY);
    86 	PORT_ArenaRelease(poolp, mark);
    87 	return NULL;
    88     }
    90     /* init list */
    91     safe->contents = (SEC_PKCS12SafeBag**)PORT_ArenaZAlloc(poolp, 
    92 						  sizeof(SEC_PKCS12SafeBag *));
    93     if(safe->contents == NULL) {
    94 	PORT_SetError(SEC_ERROR_NO_MEMORY);
    95 	PORT_ArenaRelease(poolp, mark);
    96 	return NULL;
    97     }
    98     safe->contents[0] = NULL;
    99     safe->poolp       = poolp;
   100     safe->safe_size   = 0;
   101     PORT_ArenaUnmark(poolp, mark);
   102     return safe;
   103 }
   105 /* create a new external bag which is appended onto the list
   106  * of bags in baggage.  the bag is created in the same arena
   107  * as baggage
   108  */
   109 SEC_PKCS12BaggageItem *
   110 sec_pkcs12_create_external_bag(SEC_PKCS12Baggage *luggage)
   111 {
   112     void *dummy, *mark;
   113     SEC_PKCS12BaggageItem *bag;
   115     if(luggage == NULL) {
   116 	return NULL;
   117     }
   119     mark = PORT_ArenaMark(luggage->poolp);
   121     /* allocate space for null terminated bag list */
   122     if(luggage->bags == NULL) {
   123 	luggage->bags=(SEC_PKCS12BaggageItem**)PORT_ArenaZAlloc(luggage->poolp, 
   124 					sizeof(SEC_PKCS12BaggageItem *));
   125 	if(luggage->bags == NULL) {
   126 	    goto loser;
   127 	}
   128 	luggage->luggage_size = 0;
   129     }
   131     /* grow the list */    
   132     dummy = PORT_ArenaGrow(luggage->poolp, luggage->bags,
   133     			sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 1),
   134     			sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 2));
   135     if(dummy == NULL) {
   136 	goto loser;
   137     }
   138     luggage->bags = (SEC_PKCS12BaggageItem**)dummy;
   140     luggage->bags[luggage->luggage_size] = 
   141     		(SEC_PKCS12BaggageItem *)PORT_ArenaZAlloc(luggage->poolp,
   142     							sizeof(SEC_PKCS12BaggageItem));
   143     if(luggage->bags[luggage->luggage_size] == NULL) {
   144 	goto loser;
   145     }
   147     /* create new bag and append it to the end */
   148     bag = luggage->bags[luggage->luggage_size];
   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     }
   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;
   166     PORT_ArenaUnmark(luggage->poolp, mark);
   167     return bag;
   169 loser:
   170     PORT_ArenaRelease(luggage->poolp, mark);
   171     PORT_SetError(SEC_ERROR_NO_MEMORY);
   172     return NULL;
   173 }
   175 /* creates a baggage witha NULL terminated 0 length list */
   176 SEC_PKCS12Baggage *
   177 sec_pkcs12_create_baggage(PLArenaPool *poolp)
   178 {
   179     SEC_PKCS12Baggage *luggage;
   180     void *mark;
   182     if(poolp == NULL)
   183 	return NULL;
   185     mark = PORT_ArenaMark(poolp);
   187     /* allocate bag */
   188     luggage = (SEC_PKCS12Baggage *)PORT_ArenaZAlloc(poolp, 
   189 	sizeof(SEC_PKCS12Baggage));
   190     if(luggage == NULL)
   191     {
   192 	PORT_SetError(SEC_ERROR_NO_MEMORY);
   193 	PORT_ArenaRelease(poolp, mark);
   194 	return NULL;
   195     }
   197     /* init list */
   198     luggage->bags = (SEC_PKCS12BaggageItem **)PORT_ArenaZAlloc(poolp,
   199     					sizeof(SEC_PKCS12BaggageItem *));
   200     if(luggage->bags == NULL) {
   201 	PORT_SetError(SEC_ERROR_NO_MEMORY);
   202 	PORT_ArenaRelease(poolp, mark);
   203 	return NULL;
   204     }
   206     luggage->bags[0] = NULL;
   207     luggage->luggage_size = 0;
   208     luggage->poolp = poolp;
   210     PORT_ArenaUnmark(poolp, mark);
   211     return luggage;
   212 }
   214 /* free pfx structure and associated items in the arena */
   215 void 
   216 SEC_PKCS12DestroyPFX(SEC_PKCS12PFXItem *pfx)
   217 {
   218     if (pfx != NULL && pfx->poolp != NULL)
   219     {
   220 	PORT_FreeArena(pfx->poolp, PR_TRUE);
   221     }
   222 }

mercurial