Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / crmf / cmmfresp.c
blobc77c5774dda6882671877cfe02561e6a443aabf3
1 /* -*- Mode: C; tab-width: 8 -*-*/
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
15 * The Original Code is the Netscape security libraries.
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.
22 * Contributor(s):
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.
36 * ***** END LICENSE BLOCK ***** */
39 * This file will contain all routines dealing with creating a
40 * CMMFCertRepContent structure through Create/Set functions.
43 #include "cmmf.h"
44 #include "cmmfi.h"
45 #include "crmf.h"
46 #include "crmfi.h"
47 #include "secitem.h"
48 #include "secder.h"
50 CMMFCertRepContent*
51 CMMF_CreateCertRepContent(void)
53 CMMFCertRepContent *retCertRep;
54 PRArenaPool *poolp;
56 poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
57 if (poolp == NULL) {
58 goto loser;
60 retCertRep = PORT_ArenaZNew(poolp, CMMFCertRepContent);
61 if (retCertRep == NULL) {
62 goto loser;
64 retCertRep->poolp = poolp;
65 return retCertRep;
66 loser:
67 if (poolp != NULL) {
68 PORT_FreeArena(poolp, PR_FALSE);
70 return NULL;
73 SECStatus
74 cmmf_CertOrEncCertSetCertificate(CMMFCertOrEncCert *certOrEncCert,
75 PRArenaPool *poolp,
76 CERTCertificate *inCert)
78 SECItem *derDest = NULL;
79 SECStatus rv = SECFailure;
81 if (inCert->derCert.data == NULL) {
82 derDest = SEC_ASN1EncodeItem(NULL, NULL, inCert,
83 CMMFCertOrEncCertCertificateTemplate);
84 if (derDest == NULL) {
85 goto loser;
87 } else {
88 derDest = SECITEM_DupItem(&inCert->derCert);
89 if (derDest == NULL) {
90 goto loser;
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;
101 } else {
102 certOrEncCert->derValue = *derDest;
104 PORT_Free(derDest);
105 return SECSuccess;
106 loser:
107 if (derDest != NULL) {
108 SECITEM_FreeItem(derDest, PR_TRUE);
110 return rv;
113 SECStatus
114 cmmf_ExtractCertsFromList(CERTCertList *inCertList,
115 PRArenaPool *poolp,
116 CERTCertificate ***certArray)
118 CERTCertificate **arrayLocalCopy;
119 CERTCertListNode *node;
120 int numNodes = 0, i;
122 for (node = CERT_LIST_HEAD(inCertList); !CERT_LIST_END(node, inCertList);
123 node = CERT_LIST_NEXT(node)) {
124 numNodes++;
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;
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;
140 for (j=0; j<i; j++) {
141 CERT_DestroyCertificate(arrayLocalCopy[j]);
143 if (poolp == NULL) {
144 PORT_Free(arrayLocalCopy);
146 *certArray = NULL;
147 return SECFailure;
150 arrayLocalCopy[numNodes] = NULL;
151 return SECSuccess;
154 SECStatus
155 CMMF_CertRepContentSetCertResponses(CMMFCertRepContent *inCertRepContent,
156 CMMFCertResponse **inCertResponses,
157 int inNumResponses)
159 PRArenaPool *poolp;
160 CMMFCertResponse **respArr, *newResp;
161 void *mark;
162 SECStatus rv;
163 int i;
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;
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;
180 for (i=0; i<inNumResponses; i++) {
181 newResp = PORT_ArenaZNew(poolp, CMMFCertResponse);
182 if (newResp == NULL) {
183 goto loser;
185 rv = cmmf_CopyCertResponse(poolp, newResp, inCertResponses[i]);
186 if (rv != SECSuccess) {
187 goto loser;
189 respArr[i] = newResp;
191 respArr[inNumResponses] = NULL;
192 PORT_ArenaUnmark(poolp, mark);
193 return SECSuccess;
195 loser:
196 PORT_ArenaRelease(poolp, mark);
197 return SECFailure;
200 CMMFCertResponse*
201 CMMF_CreateCertResponse(long inCertReqId)
203 SECItem *dummy;
204 CMMFCertResponse *newResp;
206 newResp = PORT_ZNew(CMMFCertResponse);
207 if (newResp == NULL) {
208 goto loser;
210 dummy = SEC_ASN1EncodeInteger(NULL, &newResp->certReqId, inCertReqId);
211 if (dummy != &newResp->certReqId) {
212 goto loser;
214 return newResp;
216 loser:
217 if (newResp != NULL) {
218 CMMF_DestroyCertResponse(newResp);
220 return NULL;
223 SECStatus
224 CMMF_CertResponseSetPKIStatusInfoStatus(CMMFCertResponse *inCertResp,
225 CMMFPKIStatus inPKIStatus)
227 PORT_Assert (inCertResp != NULL && inPKIStatus >= cmmfGranted
228 && inPKIStatus < cmmfNumPKIStatus);
230 if (inCertResp == NULL) {
231 return SECFailure;
233 return cmmf_PKIStatusInfoSetStatus(&inCertResp->status, NULL,
234 inPKIStatus);
237 SECStatus
238 CMMF_CertResponseSetCertificate (CMMFCertResponse *inCertResp,
239 CERTCertificate *inCertificate)
241 CMMFCertifiedKeyPair *keyPair = NULL;
242 SECStatus rv = SECFailure;
244 PORT_Assert(inCertResp != NULL && inCertificate != NULL);
245 if (inCertResp == NULL || inCertificate == NULL) {
246 return SECFailure;
248 if (inCertResp->certifiedKeyPair == NULL) {
249 keyPair = inCertResp->certifiedKeyPair =
250 PORT_ZNew(CMMFCertifiedKeyPair);
251 } else {
252 keyPair = inCertResp->certifiedKeyPair;
254 if (keyPair == NULL) {
255 goto loser;
257 rv = cmmf_CertOrEncCertSetCertificate(&keyPair->certOrEncCert, NULL,
258 inCertificate);
259 if (rv != SECSuccess) {
260 goto loser;
262 return SECSuccess;
263 loser:
264 if (keyPair) {
265 if (keyPair->certOrEncCert.derValue.data) {
266 PORT_Free(keyPair->certOrEncCert.derValue.data);
268 PORT_Free(keyPair);
270 return rv;
274 SECStatus
275 CMMF_CertRepContentSetCAPubs(CMMFCertRepContent *inCertRepContent,
276 CERTCertList *inCAPubs)
278 PRArenaPool *poolp;
279 void *mark;
280 SECStatus rv;
282 PORT_Assert(inCertRepContent != NULL &&
283 inCAPubs != NULL &&
284 inCertRepContent->caPubs == NULL);
286 if (inCertRepContent == NULL ||
287 inCAPubs == NULL || inCertRepContent == NULL) {
288 return SECFailure;
291 poolp = inCertRepContent->poolp;
292 mark = PORT_ArenaMark(poolp);
294 rv = cmmf_ExtractCertsFromList(inCAPubs, poolp,
295 &inCertRepContent->caPubs);
297 if (rv != SECSuccess) {
298 PORT_ArenaRelease(poolp, mark);
299 } else {
300 PORT_ArenaUnmark(poolp, mark);
302 return rv;
305 CERTCertificate*
306 CMMF_CertifiedKeyPairGetCertificate(CMMFCertifiedKeyPair *inCertKeyPair,
307 CERTCertDBHandle *inCertdb)
309 PORT_Assert(inCertKeyPair != NULL);
310 if (inCertKeyPair == NULL) {
311 return NULL;
313 return cmmf_CertOrEncCertGetCertificate(&inCertKeyPair->certOrEncCert,
314 inCertdb);