Back to index

lightning-sunbird  0.9+nobinonly
respcli.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 /*
00040  * This file will contain all routines needed by a client that has 
00041  * to parse a CMMFCertRepContent structure and retirieve the appropriate
00042  * data.
00043  */
00044 
00045 #include "cmmf.h"
00046 #include "cmmfi.h"
00047 #include "crmf.h"
00048 #include "crmfi.h"
00049 #include "secitem.h"
00050 #include "secder.h"
00051 #include "secasn1.h"
00052 
00053 CMMFCertRepContent*
00054 CMMF_CreateCertRepContentFromDER(CERTCertDBHandle *db, const char *buf, 
00055                              long len)
00056 {
00057     PRArenaPool        *poolp;
00058     CMMFCertRepContent *certRepContent;
00059     SECStatus           rv;
00060     int                 i;
00061 
00062     poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
00063     if (poolp == NULL) {
00064         return NULL;
00065     }
00066     certRepContent = PORT_ArenaZNew(poolp, CMMFCertRepContent);
00067     if (certRepContent == NULL) {
00068         goto loser;
00069     }
00070     certRepContent->poolp = poolp;
00071     rv = SEC_ASN1Decode(poolp, certRepContent, CMMFCertRepContentTemplate,
00072                      buf, len);
00073     if (rv != SECSuccess) {
00074         goto loser;
00075     }
00076     if (certRepContent->response != NULL) {
00077         for (i=0; certRepContent->response[i] != NULL; i++) {
00078            rv = cmmf_decode_process_cert_response(poolp, db,
00079                                           certRepContent->response[i]);
00080            if (rv != SECSuccess) {
00081                goto loser;
00082            }
00083        }
00084     }
00085     certRepContent->isDecoded = PR_TRUE;
00086     return certRepContent;
00087  loser:
00088     PORT_FreeArena(poolp, PR_FALSE);
00089     return NULL;
00090 }
00091 
00092 long
00093 CMMF_CertResponseGetCertReqId(CMMFCertResponse *inCertResp)
00094 {
00095     PORT_Assert(inCertResp != NULL);
00096     if (inCertResp == NULL) {
00097         return -1;
00098     }
00099     return DER_GetInteger(&inCertResp->certReqId);
00100 }
00101 
00102 PRBool
00103 cmmf_CertRepContentIsIndexValid(CMMFCertRepContent *inCertRepContent,
00104                                 int                 inIndex)
00105 {
00106     int numResponses;
00107 
00108     PORT_Assert(inCertRepContent != NULL);
00109     numResponses = CMMF_CertRepContentGetNumResponses(inCertRepContent);
00110     return (PRBool)(inIndex >= 0 && inIndex < numResponses);
00111 }
00112 
00113 CMMFCertResponse*
00114 CMMF_CertRepContentGetResponseAtIndex(CMMFCertRepContent *inCertRepContent,
00115                                   int                 inIndex)
00116 {
00117     CMMFCertResponse *certResponse;
00118     SECStatus         rv;
00119 
00120     PORT_Assert(inCertRepContent != NULL &&
00121               cmmf_CertRepContentIsIndexValid(inCertRepContent, inIndex));
00122     if (inCertRepContent == NULL ||
00123        !cmmf_CertRepContentIsIndexValid(inCertRepContent, inIndex)) {
00124         return NULL;
00125     }
00126     certResponse = PORT_ZNew(CMMFCertResponse);
00127     rv = cmmf_CopyCertResponse(NULL, certResponse, 
00128                             inCertRepContent->response[inIndex]);
00129     if (rv != SECSuccess) {
00130         CMMF_DestroyCertResponse(certResponse);
00131        certResponse = NULL;
00132     }
00133     return certResponse;
00134 }
00135 
00136 CMMFPKIStatus
00137 CMMF_CertResponseGetPKIStatusInfoStatus(CMMFCertResponse *inCertResp)
00138 {
00139     PORT_Assert(inCertResp != NULL);
00140     if (inCertResp == NULL) {
00141         return cmmfNoPKIStatus;
00142     }
00143     return cmmf_PKIStatusInfoGetStatus(&inCertResp->status);
00144 }
00145 
00146 CERTCertificate*
00147 CMMF_CertResponseGetCertificate(CMMFCertResponse *inCertResp,
00148                             CERTCertDBHandle *inCertdb)
00149 {
00150     PORT_Assert(inCertResp != NULL);
00151     if (inCertResp == NULL || inCertResp->certifiedKeyPair == NULL) {
00152         return NULL;
00153     }
00154     
00155     return cmmf_CertOrEncCertGetCertificate(
00156               &inCertResp->certifiedKeyPair->certOrEncCert, inCertdb);
00157                                
00158 }
00159 
00160 CERTCertList*
00161 CMMF_CertRepContentGetCAPubs (CMMFCertRepContent *inCertRepContent)
00162 {
00163     PORT_Assert (inCertRepContent != NULL);
00164     if (inCertRepContent == NULL || inCertRepContent->caPubs == NULL) {
00165         return NULL;
00166     }
00167     return cmmf_MakeCertList(inCertRepContent->caPubs);
00168 }
00169