Back to index

lightning-sunbird  0.9+nobinonly
cmmfresp.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8 -*-*/
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape security libraries.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * This file will contain all routines dealing with creating a 
00040  * CMMFCertRepContent structure through Create/Set functions.
00041  */
00042 
00043 #include "cmmf.h"
00044 #include "cmmfi.h"
00045 #include "crmf.h"
00046 #include "crmfi.h"
00047 #include "secitem.h"
00048 #include "secder.h"
00049 
00050 CMMFCertRepContent*
00051 CMMF_CreateCertRepContent(void)
00052 {
00053     CMMFCertRepContent *retCertRep;
00054     PRArenaPool        *poolp;
00055 
00056     poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
00057     if (poolp == NULL) {
00058         goto loser;
00059     }
00060     retCertRep = PORT_ArenaZNew(poolp, CMMFCertRepContent);
00061     if (retCertRep == NULL) {
00062         goto loser;
00063     }
00064     retCertRep->poolp = poolp;
00065     return retCertRep;
00066  loser:
00067     if (poolp != NULL) {
00068         PORT_FreeArena(poolp, PR_FALSE);
00069     }
00070     return NULL;
00071 }
00072 
00073 SECStatus 
00074 cmmf_CertOrEncCertSetCertificate(CMMFCertOrEncCert *certOrEncCert,
00075                              PRArenaPool       *poolp,
00076                              CERTCertificate   *inCert)
00077 {
00078     SECItem               *derDest = NULL;
00079     SECStatus             rv = SECFailure;
00080 
00081     if (inCert->derCert.data == NULL) {
00082         derDest = SEC_ASN1EncodeItem(NULL, NULL, inCert, 
00083                                  CMMFCertOrEncCertCertificateTemplate);
00084        if (derDest == NULL) {
00085            goto loser;
00086        }
00087     } else {
00088         derDest = SECITEM_DupItem(&inCert->derCert);
00089        if (derDest == NULL) {
00090            goto loser;
00091        }
00092     }
00093     PORT_Assert(certOrEncCert->cert.certificate == NULL);
00094     certOrEncCert->cert.certificate = CERT_DupCertificate(inCert);
00095     certOrEncCert->choice = cmmfCertificate;
00096     if (poolp != NULL) {
00097         rv = SECITEM_CopyItem(poolp, &certOrEncCert->derValue, derDest);
00098        if (rv != SECSuccess) {
00099            goto loser;
00100        }
00101     } else {
00102         certOrEncCert->derValue = *derDest;
00103     }
00104     PORT_Free(derDest);
00105     return SECSuccess;
00106  loser:
00107     if (derDest != NULL) {
00108         SECITEM_FreeItem(derDest, PR_TRUE);
00109     }
00110     return rv;
00111 }
00112 
00113 SECStatus
00114 cmmf_ExtractCertsFromList(CERTCertList      *inCertList,
00115                        PRArenaPool       *poolp,
00116                        CERTCertificate ***certArray)
00117 {
00118     CERTCertificate  **arrayLocalCopy;
00119     CERTCertListNode  *node;
00120     int                numNodes = 0, i;
00121 
00122     for (node = CERT_LIST_HEAD(inCertList); !CERT_LIST_END(node, inCertList);
00123         node = CERT_LIST_NEXT(node)) {
00124         numNodes++;
00125     }
00126 
00127     arrayLocalCopy = *certArray = (poolp == NULL) ?
00128                     PORT_NewArray(CERTCertificate*, (numNodes+1)) :
00129                     PORT_ArenaNewArray(poolp, CERTCertificate*, (numNodes+1));
00130     if (arrayLocalCopy == NULL) {
00131         return SECFailure;
00132     }
00133     for (node = CERT_LIST_HEAD(inCertList), i=0; 
00134         !CERT_LIST_END(node, inCertList);
00135         node = CERT_LIST_NEXT(node), i++) {
00136         arrayLocalCopy[i] = CERT_DupCertificate(node->cert);
00137        if (arrayLocalCopy[i] == NULL) {
00138            int j;
00139            
00140            for (j=0; j<i; j++) {
00141                CERT_DestroyCertificate(arrayLocalCopy[j]);
00142            }
00143            if (poolp == NULL) {
00144                PORT_Free(arrayLocalCopy);
00145            }
00146            *certArray = NULL;
00147            return SECFailure;
00148        }
00149     }
00150     arrayLocalCopy[numNodes] = NULL;
00151     return SECSuccess;
00152 }
00153 
00154 SECStatus
00155 CMMF_CertRepContentSetCertResponses(CMMFCertRepContent *inCertRepContent,
00156                                 CMMFCertResponse  **inCertResponses,
00157                                 int                 inNumResponses)
00158 {
00159     PRArenaPool       *poolp;
00160     CMMFCertResponse **respArr, *newResp;
00161     void              *mark;
00162     SECStatus          rv;
00163     int                i;
00164 
00165     PORT_Assert (inCertRepContent != NULL &&
00166                inCertResponses  != NULL &&
00167                inNumResponses    > 0);
00168     if (inCertRepContent == NULL ||
00169        inCertResponses  == NULL ||
00170        inCertRepContent->response != NULL) {
00171         return SECFailure;
00172     }
00173     poolp = inCertRepContent->poolp;
00174     mark = PORT_ArenaMark(poolp);
00175     respArr = inCertRepContent->response = 
00176         PORT_ArenaZNewArray(poolp, CMMFCertResponse*, (inNumResponses+1));
00177     if (respArr == NULL) {
00178         goto loser;
00179     }
00180     for (i=0; i<inNumResponses; i++) {
00181         newResp = PORT_ArenaZNew(poolp, CMMFCertResponse);
00182        if (newResp == NULL) {
00183            goto loser;
00184        }
00185         rv = cmmf_CopyCertResponse(poolp, newResp, inCertResponses[i]);
00186        if (rv != SECSuccess) {
00187            goto loser;
00188        }
00189        respArr[i] = newResp;
00190     }
00191     respArr[inNumResponses] = NULL;
00192     PORT_ArenaUnmark(poolp, mark);
00193     return SECSuccess;
00194 
00195  loser:
00196     PORT_ArenaRelease(poolp, mark);
00197     return SECFailure;
00198 }
00199 
00200 CMMFCertResponse*
00201 CMMF_CreateCertResponse(long inCertReqId)
00202 {
00203     SECItem          *dummy;
00204     CMMFCertResponse *newResp;
00205     
00206     newResp = PORT_ZNew(CMMFCertResponse);
00207     if (newResp == NULL) {
00208         goto loser;
00209     }
00210     dummy = SEC_ASN1EncodeInteger(NULL, &newResp->certReqId, inCertReqId);
00211     if (dummy != &newResp->certReqId) {
00212         goto loser;
00213     }
00214     return newResp;
00215 
00216  loser:
00217     if (newResp != NULL) {
00218         CMMF_DestroyCertResponse(newResp);
00219     }
00220     return NULL;
00221 }
00222 
00223 SECStatus
00224 CMMF_CertResponseSetPKIStatusInfoStatus(CMMFCertResponse *inCertResp,
00225                                    CMMFPKIStatus     inPKIStatus)
00226 {
00227     PORT_Assert (inCertResp != NULL && inPKIStatus >= cmmfGranted
00228                && inPKIStatus < cmmfNumPKIStatus);
00229 
00230     if  (inCertResp == NULL) {
00231         return SECFailure;
00232     }
00233     return cmmf_PKIStatusInfoSetStatus(&inCertResp->status, NULL,
00234                                    inPKIStatus);
00235 }
00236 
00237 SECStatus
00238 CMMF_CertResponseSetCertificate (CMMFCertResponse *inCertResp,
00239                              CERTCertificate  *inCertificate)
00240 {
00241     CMMFCertifiedKeyPair *keyPair = NULL;
00242     SECStatus             rv = SECFailure;
00243 
00244     PORT_Assert(inCertResp != NULL && inCertificate != NULL);
00245     if (inCertResp == NULL || inCertificate == NULL) {
00246         return SECFailure;
00247     }
00248     if (inCertResp->certifiedKeyPair == NULL) {
00249         keyPair = inCertResp->certifiedKeyPair = 
00250            PORT_ZNew(CMMFCertifiedKeyPair);
00251     } else {
00252         keyPair = inCertResp->certifiedKeyPair;
00253     }
00254     if (keyPair == NULL) {
00255         goto loser;
00256     }
00257     rv = cmmf_CertOrEncCertSetCertificate(&keyPair->certOrEncCert, NULL,
00258                                      inCertificate);
00259     if (rv != SECSuccess) {
00260         goto loser;
00261     }
00262     return SECSuccess;
00263  loser:
00264     if (keyPair) {
00265         if (keyPair->certOrEncCert.derValue.data) {
00266            PORT_Free(keyPair->certOrEncCert.derValue.data);
00267        }
00268        PORT_Free(keyPair);
00269     }
00270     return rv;
00271 }
00272 
00273 
00274 SECStatus
00275 CMMF_CertRepContentSetCAPubs(CMMFCertRepContent *inCertRepContent,
00276                           CERTCertList       *inCAPubs)
00277 {
00278     PRArenaPool      *poolp;
00279     void             *mark;
00280     SECStatus         rv;
00281 
00282     PORT_Assert(inCertRepContent != NULL &&
00283               inCAPubs         != NULL &&
00284               inCertRepContent->caPubs == NULL);
00285     
00286     if (inCertRepContent == NULL ||
00287        inCAPubs == NULL || inCertRepContent == NULL) {
00288         return SECFailure;
00289     }
00290 
00291     poolp = inCertRepContent->poolp;
00292     mark = PORT_ArenaMark(poolp);
00293 
00294     rv = cmmf_ExtractCertsFromList(inCAPubs, poolp,
00295                                &inCertRepContent->caPubs);
00296 
00297     if (rv != SECSuccess) {
00298         PORT_ArenaRelease(poolp, mark);
00299     } else {
00300         PORT_ArenaUnmark(poolp, mark);
00301     }
00302     return rv;
00303 }
00304 
00305 CERTCertificate*
00306 CMMF_CertifiedKeyPairGetCertificate(CMMFCertifiedKeyPair *inCertKeyPair,
00307                                 CERTCertDBHandle     *inCertdb)
00308 {
00309     PORT_Assert(inCertKeyPair != NULL);
00310     if (inCertKeyPair == NULL) {
00311         return NULL;
00312     }
00313     return cmmf_CertOrEncCertGetCertificate(&inCertKeyPair->certOrEncCert,
00314                                        inCertdb);
00315 }