michael@0: /* -*- Mode: C; tab-width: 8 -*-*/ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * This file will contain all routines dealing with creating a michael@0: * CMMFCertRepContent structure through Create/Set functions. michael@0: */ michael@0: michael@0: #include "cmmf.h" michael@0: #include "cmmfi.h" michael@0: #include "crmf.h" michael@0: #include "crmfi.h" michael@0: #include "secitem.h" michael@0: #include "secder.h" michael@0: michael@0: CMMFCertRepContent* michael@0: CMMF_CreateCertRepContent(void) michael@0: { michael@0: CMMFCertRepContent *retCertRep; michael@0: PLArenaPool *poolp; michael@0: michael@0: poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE); michael@0: if (poolp == NULL) { michael@0: goto loser; michael@0: } michael@0: retCertRep = PORT_ArenaZNew(poolp, CMMFCertRepContent); michael@0: if (retCertRep == NULL) { michael@0: goto loser; michael@0: } michael@0: retCertRep->poolp = poolp; michael@0: return retCertRep; michael@0: loser: michael@0: if (poolp != NULL) { michael@0: PORT_FreeArena(poolp, PR_FALSE); michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: SECStatus michael@0: cmmf_CertOrEncCertSetCertificate(CMMFCertOrEncCert *certOrEncCert, michael@0: PLArenaPool *poolp, michael@0: CERTCertificate *inCert) michael@0: { michael@0: SECItem *derDest = NULL; michael@0: SECStatus rv = SECFailure; michael@0: michael@0: if (inCert->derCert.data == NULL) { michael@0: derDest = SEC_ASN1EncodeItem(NULL, NULL, inCert, michael@0: CMMFCertOrEncCertCertificateTemplate); michael@0: if (derDest == NULL) { michael@0: goto loser; michael@0: } michael@0: } else { michael@0: derDest = SECITEM_DupItem(&inCert->derCert); michael@0: if (derDest == NULL) { michael@0: goto loser; michael@0: } michael@0: } michael@0: PORT_Assert(certOrEncCert->cert.certificate == NULL); michael@0: certOrEncCert->cert.certificate = CERT_DupCertificate(inCert); michael@0: certOrEncCert->choice = cmmfCertificate; michael@0: if (poolp != NULL) { michael@0: rv = SECITEM_CopyItem(poolp, &certOrEncCert->derValue, derDest); michael@0: if (rv != SECSuccess) { michael@0: goto loser; michael@0: } michael@0: } else { michael@0: certOrEncCert->derValue = *derDest; michael@0: } michael@0: PORT_Free(derDest); michael@0: return SECSuccess; michael@0: loser: michael@0: if (derDest != NULL) { michael@0: SECITEM_FreeItem(derDest, PR_TRUE); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: SECStatus michael@0: cmmf_ExtractCertsFromList(CERTCertList *inCertList, michael@0: PLArenaPool *poolp, michael@0: CERTCertificate ***certArray) michael@0: { michael@0: CERTCertificate **arrayLocalCopy; michael@0: CERTCertListNode *node; michael@0: int numNodes = 0, i; michael@0: michael@0: for (node = CERT_LIST_HEAD(inCertList); !CERT_LIST_END(node, inCertList); michael@0: node = CERT_LIST_NEXT(node)) { michael@0: numNodes++; michael@0: } michael@0: michael@0: arrayLocalCopy = *certArray = (poolp == NULL) ? michael@0: PORT_NewArray(CERTCertificate*, (numNodes+1)) : michael@0: PORT_ArenaNewArray(poolp, CERTCertificate*, (numNodes+1)); michael@0: if (arrayLocalCopy == NULL) { michael@0: return SECFailure; michael@0: } michael@0: for (node = CERT_LIST_HEAD(inCertList), i=0; michael@0: !CERT_LIST_END(node, inCertList); michael@0: node = CERT_LIST_NEXT(node), i++) { michael@0: arrayLocalCopy[i] = CERT_DupCertificate(node->cert); michael@0: if (arrayLocalCopy[i] == NULL) { michael@0: int j; michael@0: michael@0: for (j=0; j 0); michael@0: if (inCertRepContent == NULL || michael@0: inCertResponses == NULL || michael@0: inCertRepContent->response != NULL) { michael@0: return SECFailure; michael@0: } michael@0: poolp = inCertRepContent->poolp; michael@0: mark = PORT_ArenaMark(poolp); michael@0: respArr = inCertRepContent->response = michael@0: PORT_ArenaZNewArray(poolp, CMMFCertResponse*, (inNumResponses+1)); michael@0: if (respArr == NULL) { michael@0: goto loser; michael@0: } michael@0: for (i=0; icertReqId, inCertReqId); michael@0: if (dummy != &newResp->certReqId) { michael@0: goto loser; michael@0: } michael@0: return newResp; michael@0: michael@0: loser: michael@0: if (newResp != NULL) { michael@0: CMMF_DestroyCertResponse(newResp); michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: SECStatus michael@0: CMMF_CertResponseSetPKIStatusInfoStatus(CMMFCertResponse *inCertResp, michael@0: CMMFPKIStatus inPKIStatus) michael@0: { michael@0: PORT_Assert (inCertResp != NULL && inPKIStatus >= cmmfGranted michael@0: && inPKIStatus < cmmfNumPKIStatus); michael@0: michael@0: if (inCertResp == NULL) { michael@0: return SECFailure; michael@0: } michael@0: return cmmf_PKIStatusInfoSetStatus(&inCertResp->status, NULL, michael@0: inPKIStatus); michael@0: } michael@0: michael@0: SECStatus michael@0: CMMF_CertResponseSetCertificate (CMMFCertResponse *inCertResp, michael@0: CERTCertificate *inCertificate) michael@0: { michael@0: CMMFCertifiedKeyPair *keyPair = NULL; michael@0: SECStatus rv = SECFailure; michael@0: michael@0: PORT_Assert(inCertResp != NULL && inCertificate != NULL); michael@0: if (inCertResp == NULL || inCertificate == NULL) { michael@0: return SECFailure; michael@0: } michael@0: if (inCertResp->certifiedKeyPair == NULL) { michael@0: keyPair = inCertResp->certifiedKeyPair = michael@0: PORT_ZNew(CMMFCertifiedKeyPair); michael@0: } else { michael@0: keyPair = inCertResp->certifiedKeyPair; michael@0: } michael@0: if (keyPair == NULL) { michael@0: goto loser; michael@0: } michael@0: rv = cmmf_CertOrEncCertSetCertificate(&keyPair->certOrEncCert, NULL, michael@0: inCertificate); michael@0: if (rv != SECSuccess) { michael@0: goto loser; michael@0: } michael@0: return SECSuccess; michael@0: loser: michael@0: if (keyPair) { michael@0: if (keyPair->certOrEncCert.derValue.data) { michael@0: PORT_Free(keyPair->certOrEncCert.derValue.data); michael@0: } michael@0: PORT_Free(keyPair); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: SECStatus michael@0: CMMF_CertRepContentSetCAPubs(CMMFCertRepContent *inCertRepContent, michael@0: CERTCertList *inCAPubs) michael@0: { michael@0: PLArenaPool *poolp; michael@0: void *mark; michael@0: SECStatus rv; michael@0: michael@0: PORT_Assert(inCertRepContent != NULL && michael@0: inCAPubs != NULL && michael@0: inCertRepContent->caPubs == NULL); michael@0: michael@0: if (inCertRepContent == NULL || michael@0: inCAPubs == NULL || inCertRepContent == NULL) { michael@0: return SECFailure; michael@0: } michael@0: michael@0: poolp = inCertRepContent->poolp; michael@0: mark = PORT_ArenaMark(poolp); michael@0: michael@0: rv = cmmf_ExtractCertsFromList(inCAPubs, poolp, michael@0: &inCertRepContent->caPubs); michael@0: michael@0: if (rv != SECSuccess) { michael@0: PORT_ArenaRelease(poolp, mark); michael@0: } else { michael@0: PORT_ArenaUnmark(poolp, mark); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: CERTCertificate* michael@0: CMMF_CertifiedKeyPairGetCertificate(CMMFCertifiedKeyPair *inCertKeyPair, michael@0: CERTCertDBHandle *inCertdb) michael@0: { michael@0: PORT_Assert(inCertKeyPair != NULL); michael@0: if (inCertKeyPair == NULL) { michael@0: return NULL; michael@0: } michael@0: return cmmf_CertOrEncCertGetCertificate(&inCertKeyPair->certOrEncCert, michael@0: inCertdb); michael@0: }