Back to index

lightning-sunbird  0.9+nobinonly
p12creat.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "pkcs12.h"
00038 #include "secitem.h"
00039 #include "secport.h"
00040 #include "secder.h"
00041 #include "secoid.h"
00042 #include "p12local.h"
00043 #include "secerr.h"
00044 
00045 
00046 /* allocate space for a PFX structure and set up initial
00047  * arena pool.  pfx structure is cleared and a pointer to
00048  * the new structure is returned.
00049  */
00050 SEC_PKCS12PFXItem *
00051 sec_pkcs12_new_pfx(void)
00052 {
00053     SEC_PKCS12PFXItem   *pfx = NULL;
00054     PRArenaPool     *poolp = NULL;
00055 
00056     poolp = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE); /* XXX Different size? */
00057     if(poolp == NULL)
00058        goto loser;
00059 
00060     pfx = (SEC_PKCS12PFXItem *)PORT_ArenaZAlloc(poolp, 
00061        sizeof(SEC_PKCS12PFXItem));
00062     if(pfx == NULL)
00063        goto loser;
00064     pfx->poolp = poolp;
00065 
00066     return pfx;
00067 
00068 loser:
00069     PORT_FreeArena(poolp, PR_TRUE);
00070     return NULL;
00071 }
00072 
00073 /* allocate space for a PFX structure and set up initial
00074  * arena pool.  pfx structure is cleared and a pointer to
00075  * the new structure is returned.
00076  */
00077 SEC_PKCS12AuthenticatedSafe *
00078 sec_pkcs12_new_asafe(PRArenaPool *poolp)
00079 {
00080     SEC_PKCS12AuthenticatedSafe  *asafe = NULL;
00081     void *mark;
00082 
00083     mark = PORT_ArenaMark(poolp);
00084     asafe = (SEC_PKCS12AuthenticatedSafe *)PORT_ArenaZAlloc(poolp, 
00085        sizeof(SEC_PKCS12AuthenticatedSafe));
00086     if(asafe == NULL)
00087        goto loser;
00088     asafe->poolp = poolp;
00089     PORT_Memset(&asafe->old_baggage, 0, sizeof(SEC_PKCS7ContentInfo));
00090 
00091     PORT_ArenaUnmark(poolp, mark);
00092     return asafe;
00093 
00094 loser:
00095     PORT_ArenaRelease(poolp, mark);
00096     return NULL;
00097 }
00098 
00099 /* create a safe contents structure with a list of
00100  * length 0 with the first element being NULL 
00101  */
00102 SEC_PKCS12SafeContents *
00103 sec_pkcs12_create_safe_contents(PRArenaPool *poolp)
00104 {
00105     SEC_PKCS12SafeContents *safe;
00106     void *mark;
00107 
00108     if(poolp == NULL)
00109        return NULL;
00110 
00111     /* allocate structure */
00112     mark = PORT_ArenaMark(poolp);
00113     safe = (SEC_PKCS12SafeContents *)PORT_ArenaZAlloc(poolp, 
00114        sizeof(SEC_PKCS12SafeContents));
00115     if(safe == NULL)
00116     {
00117        PORT_SetError(SEC_ERROR_NO_MEMORY);
00118        PORT_ArenaRelease(poolp, mark);
00119        return NULL;
00120     }
00121 
00122     /* init list */
00123     safe->contents = (SEC_PKCS12SafeBag**)PORT_ArenaZAlloc(poolp, 
00124                                             sizeof(SEC_PKCS12SafeBag *));
00125     if(safe->contents == NULL) {
00126        PORT_SetError(SEC_ERROR_NO_MEMORY);
00127        PORT_ArenaRelease(poolp, mark);
00128        return NULL;
00129     }
00130     safe->contents[0] = NULL;
00131     safe->poolp       = poolp;
00132     safe->safe_size   = 0;
00133     PORT_ArenaUnmark(poolp, mark);
00134     return safe;
00135 }
00136 
00137 /* create a new external bag which is appended onto the list
00138  * of bags in baggage.  the bag is created in the same arena
00139  * as baggage
00140  */
00141 SEC_PKCS12BaggageItem *
00142 sec_pkcs12_create_external_bag(SEC_PKCS12Baggage *luggage)
00143 {
00144     void *dummy, *mark;
00145     SEC_PKCS12BaggageItem *bag;
00146 
00147     if(luggage == NULL) {
00148        return NULL;
00149     }
00150 
00151     mark = PORT_ArenaMark(luggage->poolp);
00152 
00153     /* allocate space for null terminated bag list */
00154     if(luggage->bags == NULL) {
00155        luggage->bags=(SEC_PKCS12BaggageItem**)PORT_ArenaZAlloc(luggage->poolp, 
00156                                    sizeof(SEC_PKCS12BaggageItem *));
00157        if(luggage->bags == NULL) {
00158            goto loser;
00159        }
00160        luggage->luggage_size = 0;
00161     }
00162 
00163     /* grow the list */    
00164     dummy = PORT_ArenaGrow(luggage->poolp, luggage->bags,
00165                      sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 1),
00166                      sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 2));
00167     if(dummy == NULL) {
00168        goto loser;
00169     }
00170     luggage->bags = (SEC_PKCS12BaggageItem**)dummy;
00171 
00172     luggage->bags[luggage->luggage_size] = 
00173               (SEC_PKCS12BaggageItem *)PORT_ArenaZAlloc(luggage->poolp,
00174                                                  sizeof(SEC_PKCS12BaggageItem));
00175     if(luggage->bags[luggage->luggage_size] == NULL) {
00176        goto loser;
00177     }
00178 
00179     /* create new bag and append it to the end */
00180     bag = luggage->bags[luggage->luggage_size];
00181     bag->espvks = (SEC_PKCS12ESPVKItem **)PORT_ArenaZAlloc(
00182                                           luggage->poolp,
00183                                           sizeof(SEC_PKCS12ESPVKItem *));
00184     bag->unencSecrets = (SEC_PKCS12SafeBag **)PORT_ArenaZAlloc(
00185                                           luggage->poolp,
00186                                           sizeof(SEC_PKCS12SafeBag *));
00187     if((bag->espvks == NULL) || (bag->unencSecrets == NULL)) {
00188        goto loser;
00189     }
00190 
00191     bag->poolp = luggage->poolp;
00192     luggage->luggage_size++;
00193     luggage->bags[luggage->luggage_size] = NULL;
00194     bag->espvks[0] = NULL;
00195     bag->unencSecrets[0] = NULL;
00196     bag->nEspvks = bag->nSecrets = 0;
00197 
00198     PORT_ArenaUnmark(luggage->poolp, mark);
00199     return bag;
00200 
00201 loser:
00202     PORT_ArenaRelease(luggage->poolp, mark);
00203     PORT_SetError(SEC_ERROR_NO_MEMORY);
00204     return NULL;
00205 }
00206 
00207 /* creates a baggage witha NULL terminated 0 length list */
00208 SEC_PKCS12Baggage *
00209 sec_pkcs12_create_baggage(PRArenaPool *poolp)
00210 {
00211     SEC_PKCS12Baggage *luggage;
00212     void *mark;
00213 
00214     if(poolp == NULL)
00215        return NULL;
00216 
00217     mark = PORT_ArenaMark(poolp);
00218 
00219     /* allocate bag */
00220     luggage = (SEC_PKCS12Baggage *)PORT_ArenaZAlloc(poolp, 
00221        sizeof(SEC_PKCS12Baggage));
00222     if(luggage == NULL)
00223     {
00224        PORT_SetError(SEC_ERROR_NO_MEMORY);
00225        PORT_ArenaRelease(poolp, mark);
00226        return NULL;
00227     }
00228 
00229     /* init list */
00230     luggage->bags = (SEC_PKCS12BaggageItem **)PORT_ArenaZAlloc(poolp,
00231                                    sizeof(SEC_PKCS12BaggageItem *));
00232     if(luggage->bags == NULL) {
00233        PORT_SetError(SEC_ERROR_NO_MEMORY);
00234        PORT_ArenaRelease(poolp, mark);
00235        return NULL;
00236     }
00237 
00238     luggage->bags[0] = NULL;
00239     luggage->luggage_size = 0;
00240     luggage->poolp = poolp;
00241 
00242     PORT_ArenaUnmark(poolp, mark);
00243     return luggage;
00244 }
00245 
00246 /* free pfx structure and associated items in the arena */
00247 void 
00248 SEC_PKCS12DestroyPFX(SEC_PKCS12PFXItem *pfx)
00249 {
00250     if (pfx != NULL && pfx->poolp != NULL)
00251     {
00252        PORT_FreeArena(pfx->poolp, PR_TRUE);
00253     }
00254 }