~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/security/nss/lib/pkcs12/p12creat.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contents of this file are subject to the Mozilla Public
 
3
 * License Version 1.1 (the "License"); you may not use this file
 
4
 * except in compliance with the License. You may obtain a copy of
 
5
 * the License at http://www.mozilla.org/MPL/
 
6
 * 
 
7
 * Software distributed under the License is distributed on an "AS
 
8
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
9
 * implied. See the License for the specific language governing
 
10
 * rights and limitations under the License.
 
11
 * 
 
12
 * The Original Code is the Netscape security libraries.
 
13
 * 
 
14
 * The Initial Developer of the Original Code is Netscape
 
15
 * Communications Corporation.  Portions created by Netscape are 
 
16
 * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
 
17
 * Rights Reserved.
 
18
 * 
 
19
 * Contributor(s):
 
20
 * 
 
21
 * Alternatively, the contents of this file may be used under the
 
22
 * terms of the GNU General Public License Version 2 or later (the
 
23
 * "GPL"), in which case the provisions of the GPL are applicable 
 
24
 * instead of those above.  If you wish to allow use of your 
 
25
 * version of this file only under the terms of the GPL and not to
 
26
 * allow others to use your version of this file under the MPL,
 
27
 * indicate your decision by deleting the provisions above and
 
28
 * replace them with the notice and other provisions required by
 
29
 * the GPL.  If you do not delete the provisions above, a recipient
 
30
 * may use your version of this file under either the MPL or the
 
31
 * GPL.
 
32
 */
 
33
 
 
34
#include "pkcs12.h"
 
35
#include "secitem.h"
 
36
#include "secport.h"
 
37
#include "secder.h"
 
38
#include "secoid.h"
 
39
#include "p12local.h"
 
40
#include "secerr.h"
 
41
 
 
42
 
 
43
/* allocate space for a PFX structure and set up initial
 
44
 * arena pool.  pfx structure is cleared and a pointer to
 
45
 * the new structure is returned.
 
46
 */
 
47
SEC_PKCS12PFXItem *
 
48
sec_pkcs12_new_pfx(void)
 
49
{
 
50
    SEC_PKCS12PFXItem   *pfx = NULL;
 
51
    PRArenaPool     *poolp = NULL;
 
52
 
 
53
    poolp = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE); /* XXX Different size? */
 
54
    if(poolp == NULL)
 
55
        goto loser;
 
56
 
 
57
    pfx = (SEC_PKCS12PFXItem *)PORT_ArenaZAlloc(poolp, 
 
58
        sizeof(SEC_PKCS12PFXItem));
 
59
    if(pfx == NULL)
 
60
        goto loser;
 
61
    pfx->poolp = poolp;
 
62
 
 
63
    return pfx;
 
64
 
 
65
loser:
 
66
    PORT_FreeArena(poolp, PR_TRUE);
 
67
    return NULL;
 
68
}
 
69
 
 
70
/* allocate space for a PFX structure and set up initial
 
71
 * arena pool.  pfx structure is cleared and a pointer to
 
72
 * the new structure is returned.
 
73
 */
 
74
SEC_PKCS12AuthenticatedSafe *
 
75
sec_pkcs12_new_asafe(PRArenaPool *poolp)
 
76
{
 
77
    SEC_PKCS12AuthenticatedSafe  *asafe = NULL;
 
78
    void *mark;
 
79
 
 
80
    mark = PORT_ArenaMark(poolp);
 
81
    asafe = (SEC_PKCS12AuthenticatedSafe *)PORT_ArenaZAlloc(poolp, 
 
82
        sizeof(SEC_PKCS12AuthenticatedSafe));
 
83
    if(asafe == NULL)
 
84
        goto loser;
 
85
    asafe->poolp = poolp;
 
86
    PORT_Memset(&asafe->old_baggage, 0, sizeof(SEC_PKCS7ContentInfo));
 
87
 
 
88
    PORT_ArenaUnmark(poolp, mark);
 
89
    return asafe;
 
90
 
 
91
loser:
 
92
    PORT_ArenaRelease(poolp, mark);
 
93
    return NULL;
 
94
}
 
95
 
 
96
/* create a safe contents structure with a list of
 
97
 * length 0 with the first element being NULL 
 
98
 */
 
99
SEC_PKCS12SafeContents *
 
100
sec_pkcs12_create_safe_contents(PRArenaPool *poolp)
 
101
{
 
102
    SEC_PKCS12SafeContents *safe;
 
103
    void *mark;
 
104
 
 
105
    if(poolp == NULL)
 
106
        return NULL;
 
107
 
 
108
    /* allocate structure */
 
109
    mark = PORT_ArenaMark(poolp);
 
110
    safe = (SEC_PKCS12SafeContents *)PORT_ArenaZAlloc(poolp, 
 
111
        sizeof(SEC_PKCS12SafeContents));
 
112
    if(safe == NULL)
 
113
    {
 
114
        PORT_SetError(SEC_ERROR_NO_MEMORY);
 
115
        PORT_ArenaRelease(poolp, mark);
 
116
        return NULL;
 
117
    }
 
118
 
 
119
    /* init list */
 
120
    safe->contents = (SEC_PKCS12SafeBag**)PORT_ArenaZAlloc(poolp, 
 
121
                                                  sizeof(SEC_PKCS12SafeBag *));
 
122
    if(safe->contents == NULL) {
 
123
        PORT_SetError(SEC_ERROR_NO_MEMORY);
 
124
        PORT_ArenaRelease(poolp, mark);
 
125
        return NULL;
 
126
    }
 
127
    safe->contents[0] = NULL;
 
128
    safe->poolp       = poolp;
 
129
    safe->safe_size   = 0;
 
130
    PORT_ArenaUnmark(poolp, mark);
 
131
    return safe;
 
132
}
 
133
 
 
134
/* create a new external bag which is appended onto the list
 
135
 * of bags in baggage.  the bag is created in the same arena
 
136
 * as baggage
 
137
 */
 
138
SEC_PKCS12BaggageItem *
 
139
sec_pkcs12_create_external_bag(SEC_PKCS12Baggage *luggage)
 
140
{
 
141
    void *dummy, *mark;
 
142
    SEC_PKCS12BaggageItem *bag;
 
143
 
 
144
    if(luggage == NULL) {
 
145
        return NULL;
 
146
    }
 
147
 
 
148
    mark = PORT_ArenaMark(luggage->poolp);
 
149
 
 
150
    /* allocate space for null terminated bag list */
 
151
    if(luggage->bags == NULL) {
 
152
        luggage->bags=(SEC_PKCS12BaggageItem**)PORT_ArenaZAlloc(luggage->poolp, 
 
153
                                        sizeof(SEC_PKCS12BaggageItem *));
 
154
        if(luggage->bags == NULL) {
 
155
            goto loser;
 
156
        }
 
157
        luggage->luggage_size = 0;
 
158
    }
 
159
 
 
160
    /* grow the list */    
 
161
    dummy = PORT_ArenaGrow(luggage->poolp, luggage->bags,
 
162
                        sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 1),
 
163
                        sizeof(SEC_PKCS12BaggageItem *) * (luggage->luggage_size + 2));
 
164
    if(dummy == NULL) {
 
165
        goto loser;
 
166
    }
 
167
    luggage->bags = (SEC_PKCS12BaggageItem**)dummy;
 
168
 
 
169
    luggage->bags[luggage->luggage_size] = 
 
170
                (SEC_PKCS12BaggageItem *)PORT_ArenaZAlloc(luggage->poolp,
 
171
                                                        sizeof(SEC_PKCS12BaggageItem));
 
172
    if(luggage->bags[luggage->luggage_size] == NULL) {
 
173
        goto loser;
 
174
    }
 
175
 
 
176
    /* create new bag and append it to the end */
 
177
    bag = luggage->bags[luggage->luggage_size];
 
178
    bag->espvks = (SEC_PKCS12ESPVKItem **)PORT_ArenaZAlloc(
 
179
                                                luggage->poolp,
 
180
                                                sizeof(SEC_PKCS12ESPVKItem *));
 
181
    bag->unencSecrets = (SEC_PKCS12SafeBag **)PORT_ArenaZAlloc(
 
182
                                                luggage->poolp,
 
183
                                                sizeof(SEC_PKCS12SafeBag *));
 
184
    if((bag->espvks == NULL) || (bag->unencSecrets == NULL)) {
 
185
        goto loser;
 
186
    }
 
187
 
 
188
    bag->poolp = luggage->poolp;
 
189
    luggage->luggage_size++;
 
190
    luggage->bags[luggage->luggage_size] = NULL;
 
191
    bag->espvks[0] = NULL;
 
192
    bag->unencSecrets[0] = NULL;
 
193
    bag->nEspvks = bag->nSecrets = 0;
 
194
 
 
195
    PORT_ArenaUnmark(luggage->poolp, mark);
 
196
    return bag;
 
197
 
 
198
loser:
 
199
    PORT_ArenaRelease(luggage->poolp, mark);
 
200
    PORT_SetError(SEC_ERROR_NO_MEMORY);
 
201
    return NULL;
 
202
}
 
203
 
 
204
/* creates a baggage witha NULL terminated 0 length list */
 
205
SEC_PKCS12Baggage *
 
206
sec_pkcs12_create_baggage(PRArenaPool *poolp)
 
207
{
 
208
    SEC_PKCS12Baggage *luggage;
 
209
    void *mark;
 
210
 
 
211
    if(poolp == NULL)
 
212
        return NULL;
 
213
 
 
214
    mark = PORT_ArenaMark(poolp);
 
215
 
 
216
    /* allocate bag */
 
217
    luggage = (SEC_PKCS12Baggage *)PORT_ArenaZAlloc(poolp, 
 
218
        sizeof(SEC_PKCS12Baggage));
 
219
    if(luggage == NULL)
 
220
    {
 
221
        PORT_SetError(SEC_ERROR_NO_MEMORY);
 
222
        PORT_ArenaRelease(poolp, mark);
 
223
        return NULL;
 
224
    }
 
225
 
 
226
    /* init list */
 
227
    luggage->bags = (SEC_PKCS12BaggageItem **)PORT_ArenaZAlloc(poolp,
 
228
                                        sizeof(SEC_PKCS12BaggageItem *));
 
229
    if(luggage->bags == NULL) {
 
230
        PORT_SetError(SEC_ERROR_NO_MEMORY);
 
231
        PORT_ArenaRelease(poolp, mark);
 
232
        return NULL;
 
233
    }
 
234
 
 
235
    luggage->bags[0] = NULL;
 
236
    luggage->luggage_size = 0;
 
237
    luggage->poolp = poolp;
 
238
 
 
239
    PORT_ArenaUnmark(poolp, mark);
 
240
    return luggage;
 
241
}
 
242
 
 
243
/* free pfx structure and associated items in the arena */
 
244
void 
 
245
SEC_PKCS12DestroyPFX(SEC_PKCS12PFXItem *pfx)
 
246
{
 
247
    if (pfx != NULL && pfx->poolp != NULL)
 
248
    {
 
249
        PORT_FreeArena(pfx->poolp, PR_TRUE);
 
250
    }
 
251
}