security/nss/lib/crmf/cmmfresp.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/crmf/cmmfresp.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,283 @@
     1.4 +/* -*- Mode: C; tab-width: 8 -*-*/
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * This file will contain all routines dealing with creating a 
    1.11 + * CMMFCertRepContent structure through Create/Set functions.
    1.12 + */
    1.13 +
    1.14 +#include "cmmf.h"
    1.15 +#include "cmmfi.h"
    1.16 +#include "crmf.h"
    1.17 +#include "crmfi.h"
    1.18 +#include "secitem.h"
    1.19 +#include "secder.h"
    1.20 +
    1.21 +CMMFCertRepContent*
    1.22 +CMMF_CreateCertRepContent(void)
    1.23 +{
    1.24 +    CMMFCertRepContent *retCertRep;
    1.25 +    PLArenaPool        *poolp;
    1.26 +
    1.27 +    poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
    1.28 +    if (poolp == NULL) {
    1.29 +        goto loser;
    1.30 +    }
    1.31 +    retCertRep = PORT_ArenaZNew(poolp, CMMFCertRepContent);
    1.32 +    if (retCertRep == NULL) {
    1.33 +        goto loser;
    1.34 +    }
    1.35 +    retCertRep->poolp = poolp;
    1.36 +    return retCertRep;
    1.37 + loser:
    1.38 +    if (poolp != NULL) {
    1.39 +        PORT_FreeArena(poolp, PR_FALSE);
    1.40 +    }
    1.41 +    return NULL;
    1.42 +}
    1.43 +
    1.44 +SECStatus 
    1.45 +cmmf_CertOrEncCertSetCertificate(CMMFCertOrEncCert *certOrEncCert,
    1.46 +				 PLArenaPool       *poolp,
    1.47 +				 CERTCertificate   *inCert)
    1.48 +{
    1.49 +    SECItem               *derDest = NULL;
    1.50 +    SECStatus             rv = SECFailure;
    1.51 +
    1.52 +    if (inCert->derCert.data == NULL) {
    1.53 +        derDest = SEC_ASN1EncodeItem(NULL, NULL, inCert, 
    1.54 +				     CMMFCertOrEncCertCertificateTemplate);
    1.55 +	if (derDest == NULL) {
    1.56 +	    goto loser;
    1.57 +	}
    1.58 +    } else {
    1.59 +        derDest = SECITEM_DupItem(&inCert->derCert);
    1.60 +	if (derDest == NULL) {
    1.61 +	    goto loser;
    1.62 +	}
    1.63 +    }
    1.64 +    PORT_Assert(certOrEncCert->cert.certificate == NULL);
    1.65 +    certOrEncCert->cert.certificate = CERT_DupCertificate(inCert);
    1.66 +    certOrEncCert->choice = cmmfCertificate;
    1.67 +    if (poolp != NULL) {
    1.68 +        rv = SECITEM_CopyItem(poolp, &certOrEncCert->derValue, derDest);
    1.69 +	if (rv != SECSuccess) {
    1.70 +	    goto loser;
    1.71 +	}
    1.72 +    } else {
    1.73 +        certOrEncCert->derValue = *derDest;
    1.74 +    }
    1.75 +    PORT_Free(derDest);
    1.76 +    return SECSuccess;
    1.77 + loser:
    1.78 +    if (derDest != NULL) {
    1.79 +        SECITEM_FreeItem(derDest, PR_TRUE);
    1.80 +    }
    1.81 +    return rv;
    1.82 +}
    1.83 +
    1.84 +SECStatus
    1.85 +cmmf_ExtractCertsFromList(CERTCertList      *inCertList,
    1.86 +			  PLArenaPool       *poolp,
    1.87 +			  CERTCertificate ***certArray)
    1.88 +{
    1.89 +    CERTCertificate  **arrayLocalCopy;
    1.90 +    CERTCertListNode  *node;
    1.91 +    int                numNodes = 0, i;
    1.92 +
    1.93 +    for (node = CERT_LIST_HEAD(inCertList); !CERT_LIST_END(node, inCertList);
    1.94 +	 node = CERT_LIST_NEXT(node)) {
    1.95 +        numNodes++;
    1.96 +    }
    1.97 +
    1.98 +    arrayLocalCopy = *certArray = (poolp == NULL) ?
    1.99 +                    PORT_NewArray(CERTCertificate*, (numNodes+1)) :
   1.100 +                    PORT_ArenaNewArray(poolp, CERTCertificate*, (numNodes+1));
   1.101 +    if (arrayLocalCopy == NULL) {
   1.102 +        return SECFailure;
   1.103 +    }
   1.104 +    for (node = CERT_LIST_HEAD(inCertList), i=0; 
   1.105 +	 !CERT_LIST_END(node, inCertList);
   1.106 +	 node = CERT_LIST_NEXT(node), i++) {
   1.107 +        arrayLocalCopy[i] = CERT_DupCertificate(node->cert);
   1.108 +	if (arrayLocalCopy[i] == NULL) {
   1.109 +	    int j;
   1.110 +	    
   1.111 +	    for (j=0; j<i; j++) {
   1.112 +	        CERT_DestroyCertificate(arrayLocalCopy[j]);
   1.113 +	    }
   1.114 +	    if (poolp == NULL) {
   1.115 +	        PORT_Free(arrayLocalCopy);
   1.116 +	    }
   1.117 +	    *certArray = NULL;
   1.118 +	    return SECFailure;
   1.119 +	}
   1.120 +    }
   1.121 +    arrayLocalCopy[numNodes] = NULL;
   1.122 +    return SECSuccess;
   1.123 +}
   1.124 +
   1.125 +SECStatus
   1.126 +CMMF_CertRepContentSetCertResponses(CMMFCertRepContent *inCertRepContent,
   1.127 +				    CMMFCertResponse  **inCertResponses,
   1.128 +				    int                 inNumResponses)
   1.129 +{
   1.130 +    PLArenaPool       *poolp;
   1.131 +    CMMFCertResponse **respArr, *newResp;
   1.132 +    void              *mark;
   1.133 +    SECStatus          rv;
   1.134 +    int                i;
   1.135 +
   1.136 +    PORT_Assert (inCertRepContent != NULL &&
   1.137 +		 inCertResponses  != NULL &&
   1.138 +		 inNumResponses    > 0);
   1.139 +    if (inCertRepContent == NULL ||
   1.140 +	inCertResponses  == NULL ||
   1.141 +	inCertRepContent->response != NULL) {
   1.142 +        return SECFailure;
   1.143 +    }
   1.144 +    poolp = inCertRepContent->poolp;
   1.145 +    mark = PORT_ArenaMark(poolp);
   1.146 +    respArr = inCertRepContent->response = 
   1.147 +        PORT_ArenaZNewArray(poolp, CMMFCertResponse*, (inNumResponses+1));
   1.148 +    if (respArr == NULL) {
   1.149 +        goto loser;
   1.150 +    }
   1.151 +    for (i=0; i<inNumResponses; i++) {
   1.152 +        newResp = PORT_ArenaZNew(poolp, CMMFCertResponse);
   1.153 +	if (newResp == NULL) {
   1.154 +	    goto loser;
   1.155 +	}
   1.156 +        rv = cmmf_CopyCertResponse(poolp, newResp, inCertResponses[i]);
   1.157 +	if (rv != SECSuccess) {
   1.158 +	    goto loser;
   1.159 +	}
   1.160 +	respArr[i] = newResp;
   1.161 +    }
   1.162 +    respArr[inNumResponses] = NULL;
   1.163 +    PORT_ArenaUnmark(poolp, mark);
   1.164 +    return SECSuccess;
   1.165 +
   1.166 + loser:
   1.167 +    PORT_ArenaRelease(poolp, mark);
   1.168 +    return SECFailure;
   1.169 +}
   1.170 +
   1.171 +CMMFCertResponse*
   1.172 +CMMF_CreateCertResponse(long inCertReqId)
   1.173 +{
   1.174 +    SECItem          *dummy;
   1.175 +    CMMFCertResponse *newResp;
   1.176 +    
   1.177 +    newResp = PORT_ZNew(CMMFCertResponse);
   1.178 +    if (newResp == NULL) {
   1.179 +        goto loser;
   1.180 +    }
   1.181 +    dummy = SEC_ASN1EncodeInteger(NULL, &newResp->certReqId, inCertReqId);
   1.182 +    if (dummy != &newResp->certReqId) {
   1.183 +        goto loser;
   1.184 +    }
   1.185 +    return newResp;
   1.186 +
   1.187 + loser:
   1.188 +    if (newResp != NULL) {
   1.189 +        CMMF_DestroyCertResponse(newResp);
   1.190 +    }
   1.191 +    return NULL;
   1.192 +}
   1.193 +
   1.194 +SECStatus
   1.195 +CMMF_CertResponseSetPKIStatusInfoStatus(CMMFCertResponse *inCertResp,
   1.196 +					CMMFPKIStatus     inPKIStatus)
   1.197 +{
   1.198 +    PORT_Assert (inCertResp != NULL && inPKIStatus >= cmmfGranted
   1.199 +		 && inPKIStatus < cmmfNumPKIStatus);
   1.200 +
   1.201 +    if  (inCertResp == NULL) {
   1.202 +        return SECFailure;
   1.203 +    }
   1.204 +    return cmmf_PKIStatusInfoSetStatus(&inCertResp->status, NULL,
   1.205 +				       inPKIStatus);
   1.206 +}
   1.207 +
   1.208 +SECStatus
   1.209 +CMMF_CertResponseSetCertificate (CMMFCertResponse *inCertResp,
   1.210 +				 CERTCertificate  *inCertificate)
   1.211 +{
   1.212 +    CMMFCertifiedKeyPair *keyPair = NULL;
   1.213 +    SECStatus             rv = SECFailure;
   1.214 +
   1.215 +    PORT_Assert(inCertResp != NULL && inCertificate != NULL);
   1.216 +    if (inCertResp == NULL || inCertificate == NULL) {
   1.217 +        return SECFailure;
   1.218 +    }
   1.219 +    if (inCertResp->certifiedKeyPair == NULL) {
   1.220 +        keyPair = inCertResp->certifiedKeyPair = 
   1.221 +	    PORT_ZNew(CMMFCertifiedKeyPair);
   1.222 +    } else {
   1.223 +        keyPair = inCertResp->certifiedKeyPair;
   1.224 +    }
   1.225 +    if (keyPair == NULL) {
   1.226 +        goto loser;
   1.227 +    }
   1.228 +    rv = cmmf_CertOrEncCertSetCertificate(&keyPair->certOrEncCert, NULL,
   1.229 +					  inCertificate);
   1.230 +    if (rv != SECSuccess) {
   1.231 +        goto loser;
   1.232 +    }
   1.233 +    return SECSuccess;
   1.234 + loser:
   1.235 +    if (keyPair) {
   1.236 +        if (keyPair->certOrEncCert.derValue.data) {
   1.237 +	    PORT_Free(keyPair->certOrEncCert.derValue.data);
   1.238 +	}
   1.239 +	PORT_Free(keyPair);
   1.240 +    }
   1.241 +    return rv;
   1.242 +}
   1.243 +
   1.244 +
   1.245 +SECStatus
   1.246 +CMMF_CertRepContentSetCAPubs(CMMFCertRepContent *inCertRepContent,
   1.247 +			     CERTCertList       *inCAPubs)
   1.248 +{
   1.249 +    PLArenaPool      *poolp;
   1.250 +    void             *mark;
   1.251 +    SECStatus         rv;
   1.252 +
   1.253 +    PORT_Assert(inCertRepContent != NULL &&
   1.254 +		inCAPubs         != NULL &&
   1.255 +		inCertRepContent->caPubs == NULL);
   1.256 +    
   1.257 +    if (inCertRepContent == NULL ||
   1.258 +	inCAPubs == NULL || inCertRepContent == NULL) {
   1.259 +        return SECFailure;
   1.260 +    }
   1.261 +
   1.262 +    poolp = inCertRepContent->poolp;
   1.263 +    mark = PORT_ArenaMark(poolp);
   1.264 +
   1.265 +    rv = cmmf_ExtractCertsFromList(inCAPubs, poolp,
   1.266 +				   &inCertRepContent->caPubs);
   1.267 +
   1.268 +    if (rv != SECSuccess) {
   1.269 +        PORT_ArenaRelease(poolp, mark);
   1.270 +    } else {
   1.271 +        PORT_ArenaUnmark(poolp, mark);
   1.272 +    }
   1.273 +    return rv;
   1.274 +}
   1.275 +
   1.276 +CERTCertificate*
   1.277 +CMMF_CertifiedKeyPairGetCertificate(CMMFCertifiedKeyPair *inCertKeyPair,
   1.278 +				    CERTCertDBHandle     *inCertdb)
   1.279 +{
   1.280 +    PORT_Assert(inCertKeyPair != NULL);
   1.281 +    if (inCertKeyPair == NULL) {
   1.282 +        return NULL;
   1.283 +    }
   1.284 +    return cmmf_CertOrEncCertGetCertificate(&inCertKeyPair->certOrEncCert,
   1.285 +					    inCertdb);
   1.286 +}

mercurial