~ubuntu-branches/ubuntu/lucid/seamonkey/lucid-security

« back to all changes in this revision

Viewing changes to security/nss-fips/lib/crmf/cmmfresp.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabien Tassin
  • Date: 2008-07-29 21:29:02 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080729212902-spm9kpvchp9udwbw
Tags: 1.1.11+nobinonly-0ubuntu1
* New security upstream release: 1.1.11 (LP: #218534)
  Fixes USN-602-1, USN-619-1, USN-623-1 and USN-629-1
* Refresh diverged patch:
  - update debian/patches/80_security_build.patch
* Fix FTBFS with missing -lfontconfig
  - add debian/patches/11_fix_ftbfs_with_fontconfig.patch
  - update debian/patches/series
* Build with default gcc (hardy: 4.2, intrepid: 4.3)
  - update debian/rules
  - update debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8 -*-*/
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Mozilla Public License Version
 
6
 * 1.1 (the "License"); you may not use this file except in compliance with
 
7
 * the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/MPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is the Netscape security libraries.
 
16
 *
 
17
 * The Initial Developer of the Original Code is
 
18
 * Netscape Communications Corporation.
 
19
 * Portions created by the Initial Developer are Copyright (C) 1994-2000
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the terms of
 
25
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
26
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
27
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
28
 * of those above. If you wish to allow use of your version of this file only
 
29
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
30
 * use your version of this file under the terms of the MPL, indicate your
 
31
 * decision by deleting the provisions above and replace them with the notice
 
32
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
33
 * the provisions above, a recipient may use your version of this file under
 
34
 * the terms of any one of the MPL, the GPL or the LGPL.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
/*
 
39
 * This file will contain all routines dealing with creating a 
 
40
 * CMMFCertRepContent structure through Create/Set functions.
 
41
 */
 
42
 
 
43
#include "cmmf.h"
 
44
#include "cmmfi.h"
 
45
#include "crmf.h"
 
46
#include "crmfi.h"
 
47
#include "secitem.h"
 
48
#include "secder.h"
 
49
 
 
50
CMMFCertRepContent*
 
51
CMMF_CreateCertRepContent(void)
 
52
{
 
53
    CMMFCertRepContent *retCertRep;
 
54
    PRArenaPool        *poolp;
 
55
 
 
56
    poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
 
57
    if (poolp == NULL) {
 
58
        goto loser;
 
59
    }
 
60
    retCertRep = PORT_ArenaZNew(poolp, CMMFCertRepContent);
 
61
    if (retCertRep == NULL) {
 
62
        goto loser;
 
63
    }
 
64
    retCertRep->poolp = poolp;
 
65
    return retCertRep;
 
66
 loser:
 
67
    if (poolp != NULL) {
 
68
        PORT_FreeArena(poolp, PR_FALSE);
 
69
    }
 
70
    return NULL;
 
71
}
 
72
 
 
73
SECStatus 
 
74
cmmf_CertOrEncCertSetCertificate(CMMFCertOrEncCert *certOrEncCert,
 
75
                                 PRArenaPool       *poolp,
 
76
                                 CERTCertificate   *inCert)
 
77
{
 
78
    SECItem               *derDest = NULL;
 
79
    SECStatus             rv = SECFailure;
 
80
 
 
81
    if (inCert->derCert.data == NULL) {
 
82
        derDest = SEC_ASN1EncodeItem(NULL, NULL, inCert, 
 
83
                                     CMMFCertOrEncCertCertificateTemplate);
 
84
        if (derDest == NULL) {
 
85
            goto loser;
 
86
        }
 
87
    } else {
 
88
        derDest = SECITEM_DupItem(&inCert->derCert);
 
89
        if (derDest == NULL) {
 
90
            goto loser;
 
91
        }
 
92
    }
 
93
    PORT_Assert(certOrEncCert->cert.certificate == NULL);
 
94
    certOrEncCert->cert.certificate = CERT_DupCertificate(inCert);
 
95
    certOrEncCert->choice = cmmfCertificate;
 
96
    if (poolp != NULL) {
 
97
        rv = SECITEM_CopyItem(poolp, &certOrEncCert->derValue, derDest);
 
98
        if (rv != SECSuccess) {
 
99
            goto loser;
 
100
        }
 
101
    } else {
 
102
        certOrEncCert->derValue = *derDest;
 
103
    }
 
104
    PORT_Free(derDest);
 
105
    return SECSuccess;
 
106
 loser:
 
107
    if (derDest != NULL) {
 
108
        SECITEM_FreeItem(derDest, PR_TRUE);
 
109
    }
 
110
    return rv;
 
111
}
 
112
 
 
113
SECStatus
 
114
cmmf_ExtractCertsFromList(CERTCertList      *inCertList,
 
115
                          PRArenaPool       *poolp,
 
116
                          CERTCertificate ***certArray)
 
117
{
 
118
    CERTCertificate  **arrayLocalCopy;
 
119
    CERTCertListNode  *node;
 
120
    int                numNodes = 0, i;
 
121
 
 
122
    for (node = CERT_LIST_HEAD(inCertList); !CERT_LIST_END(node, inCertList);
 
123
         node = CERT_LIST_NEXT(node)) {
 
124
        numNodes++;
 
125
    }
 
126
 
 
127
    arrayLocalCopy = *certArray = (poolp == NULL) ?
 
128
                    PORT_NewArray(CERTCertificate*, (numNodes+1)) :
 
129
                    PORT_ArenaNewArray(poolp, CERTCertificate*, (numNodes+1));
 
130
    if (arrayLocalCopy == NULL) {
 
131
        return SECFailure;
 
132
    }
 
133
    for (node = CERT_LIST_HEAD(inCertList), i=0; 
 
134
         !CERT_LIST_END(node, inCertList);
 
135
         node = CERT_LIST_NEXT(node), i++) {
 
136
        arrayLocalCopy[i] = CERT_DupCertificate(node->cert);
 
137
        if (arrayLocalCopy[i] == NULL) {
 
138
            int j;
 
139
            
 
140
            for (j=0; j<i; j++) {
 
141
                CERT_DestroyCertificate(arrayLocalCopy[j]);
 
142
            }
 
143
            if (poolp == NULL) {
 
144
                PORT_Free(arrayLocalCopy);
 
145
            }
 
146
            *certArray = NULL;
 
147
            return SECFailure;
 
148
        }
 
149
    }
 
150
    arrayLocalCopy[numNodes] = NULL;
 
151
    return SECSuccess;
 
152
}
 
153
 
 
154
SECStatus
 
155
CMMF_CertRepContentSetCertResponses(CMMFCertRepContent *inCertRepContent,
 
156
                                    CMMFCertResponse  **inCertResponses,
 
157
                                    int                 inNumResponses)
 
158
{
 
159
    PRArenaPool       *poolp;
 
160
    CMMFCertResponse **respArr, *newResp;
 
161
    void              *mark;
 
162
    SECStatus          rv;
 
163
    int                i;
 
164
 
 
165
    PORT_Assert (inCertRepContent != NULL &&
 
166
                 inCertResponses  != NULL &&
 
167
                 inNumResponses    > 0);
 
168
    if (inCertRepContent == NULL ||
 
169
        inCertResponses  == NULL ||
 
170
        inCertRepContent->response != NULL) {
 
171
        return SECFailure;
 
172
    }
 
173
    poolp = inCertRepContent->poolp;
 
174
    mark = PORT_ArenaMark(poolp);
 
175
    respArr = inCertRepContent->response = 
 
176
        PORT_ArenaZNewArray(poolp, CMMFCertResponse*, (inNumResponses+1));
 
177
    if (respArr == NULL) {
 
178
        goto loser;
 
179
    }
 
180
    for (i=0; i<inNumResponses; i++) {
 
181
        newResp = PORT_ArenaZNew(poolp, CMMFCertResponse);
 
182
        if (newResp == NULL) {
 
183
            goto loser;
 
184
        }
 
185
        rv = cmmf_CopyCertResponse(poolp, newResp, inCertResponses[i]);
 
186
        if (rv != SECSuccess) {
 
187
            goto loser;
 
188
        }
 
189
        respArr[i] = newResp;
 
190
    }
 
191
    respArr[inNumResponses] = NULL;
 
192
    PORT_ArenaUnmark(poolp, mark);
 
193
    return SECSuccess;
 
194
 
 
195
 loser:
 
196
    PORT_ArenaRelease(poolp, mark);
 
197
    return SECFailure;
 
198
}
 
199
 
 
200
CMMFCertResponse*
 
201
CMMF_CreateCertResponse(long inCertReqId)
 
202
{
 
203
    SECItem          *dummy;
 
204
    CMMFCertResponse *newResp;
 
205
    
 
206
    newResp = PORT_ZNew(CMMFCertResponse);
 
207
    if (newResp == NULL) {
 
208
        goto loser;
 
209
    }
 
210
    dummy = SEC_ASN1EncodeInteger(NULL, &newResp->certReqId, inCertReqId);
 
211
    if (dummy != &newResp->certReqId) {
 
212
        goto loser;
 
213
    }
 
214
    return newResp;
 
215
 
 
216
 loser:
 
217
    if (newResp != NULL) {
 
218
        CMMF_DestroyCertResponse(newResp);
 
219
    }
 
220
    return NULL;
 
221
}
 
222
 
 
223
SECStatus
 
224
CMMF_CertResponseSetPKIStatusInfoStatus(CMMFCertResponse *inCertResp,
 
225
                                        CMMFPKIStatus     inPKIStatus)
 
226
{
 
227
    PORT_Assert (inCertResp != NULL && inPKIStatus >= cmmfGranted
 
228
                 && inPKIStatus < cmmfNumPKIStatus);
 
229
 
 
230
    if  (inCertResp == NULL) {
 
231
        return SECFailure;
 
232
    }
 
233
    return cmmf_PKIStatusInfoSetStatus(&inCertResp->status, NULL,
 
234
                                       inPKIStatus);
 
235
}
 
236
 
 
237
SECStatus
 
238
CMMF_CertResponseSetCertificate (CMMFCertResponse *inCertResp,
 
239
                                 CERTCertificate  *inCertificate)
 
240
{
 
241
    CMMFCertifiedKeyPair *keyPair = NULL;
 
242
    SECStatus             rv = SECFailure;
 
243
 
 
244
    PORT_Assert(inCertResp != NULL && inCertificate != NULL);
 
245
    if (inCertResp == NULL || inCertificate == NULL) {
 
246
        return SECFailure;
 
247
    }
 
248
    if (inCertResp->certifiedKeyPair == NULL) {
 
249
        keyPair = inCertResp->certifiedKeyPair = 
 
250
            PORT_ZNew(CMMFCertifiedKeyPair);
 
251
    } else {
 
252
        keyPair = inCertResp->certifiedKeyPair;
 
253
    }
 
254
    if (keyPair == NULL) {
 
255
        goto loser;
 
256
    }
 
257
    rv = cmmf_CertOrEncCertSetCertificate(&keyPair->certOrEncCert, NULL,
 
258
                                          inCertificate);
 
259
    if (rv != SECSuccess) {
 
260
        goto loser;
 
261
    }
 
262
    return SECSuccess;
 
263
 loser:
 
264
    if (keyPair) {
 
265
        if (keyPair->certOrEncCert.derValue.data) {
 
266
            PORT_Free(keyPair->certOrEncCert.derValue.data);
 
267
        }
 
268
        PORT_Free(keyPair);
 
269
    }
 
270
    return rv;
 
271
}
 
272
 
 
273
 
 
274
SECStatus
 
275
CMMF_CertRepContentSetCAPubs(CMMFCertRepContent *inCertRepContent,
 
276
                             CERTCertList       *inCAPubs)
 
277
{
 
278
    PRArenaPool      *poolp;
 
279
    void             *mark;
 
280
    SECStatus         rv;
 
281
 
 
282
    PORT_Assert(inCertRepContent != NULL &&
 
283
                inCAPubs         != NULL &&
 
284
                inCertRepContent->caPubs == NULL);
 
285
    
 
286
    if (inCertRepContent == NULL ||
 
287
        inCAPubs == NULL || inCertRepContent == NULL) {
 
288
        return SECFailure;
 
289
    }
 
290
 
 
291
    poolp = inCertRepContent->poolp;
 
292
    mark = PORT_ArenaMark(poolp);
 
293
 
 
294
    rv = cmmf_ExtractCertsFromList(inCAPubs, poolp,
 
295
                                   &inCertRepContent->caPubs);
 
296
 
 
297
    if (rv != SECSuccess) {
 
298
        PORT_ArenaRelease(poolp, mark);
 
299
    } else {
 
300
        PORT_ArenaUnmark(poolp, mark);
 
301
    }
 
302
    return rv;
 
303
}
 
304
 
 
305
CERTCertificate*
 
306
CMMF_CertifiedKeyPairGetCertificate(CMMFCertifiedKeyPair *inCertKeyPair,
 
307
                                    CERTCertDBHandle     *inCertdb)
 
308
{
 
309
    PORT_Assert(inCertKeyPair != NULL);
 
310
    if (inCertKeyPair == NULL) {
 
311
        return NULL;
 
312
    }
 
313
    return cmmf_CertOrEncCertGetCertificate(&inCertKeyPair->certOrEncCert,
 
314
                                            inCertdb);
 
315
}