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: #include "cmmf.h" michael@0: #include "cmmfi.h" michael@0: #include "secasn1.h" michael@0: #include "secitem.h" michael@0: michael@0: SEC_ASN1_MKSUB(SEC_SignedCertificateTemplate) michael@0: michael@0: static const SEC_ASN1Template CMMFSequenceOfCertifiedKeyPairsTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE_OF, 0, CMMFCertifiedKeyPairTemplate} michael@0: }; michael@0: michael@0: static const SEC_ASN1Template CMMFKeyRecRepContentTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CMMFKeyRecRepContent)}, michael@0: { SEC_ASN1_INLINE, offsetof(CMMFKeyRecRepContent, status), michael@0: CMMFPKIStatusInfoTemplate}, michael@0: { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_POINTER | michael@0: SEC_ASN1_XTRN | 0, michael@0: offsetof(CMMFKeyRecRepContent, newSigCert), michael@0: SEC_ASN1_SUB(SEC_SignedCertificateTemplate)}, michael@0: { SEC_ASN1_CONSTRUCTED | SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | 1, michael@0: offsetof(CMMFKeyRecRepContent, caCerts), michael@0: CMMFSequenceOfCertsTemplate}, michael@0: { SEC_ASN1_CONSTRUCTED | SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | 2, michael@0: offsetof(CMMFKeyRecRepContent, keyPairHist), michael@0: CMMFSequenceOfCertifiedKeyPairsTemplate}, michael@0: { 0 } michael@0: }; michael@0: michael@0: SECStatus michael@0: CMMF_EncodeCertRepContent (CMMFCertRepContent *inCertRepContent, michael@0: CRMFEncoderOutputCallback inCallback, michael@0: void *inArg) michael@0: { michael@0: return cmmf_user_encode(inCertRepContent, inCallback, inArg, michael@0: CMMFCertRepContentTemplate); michael@0: } michael@0: michael@0: SECStatus michael@0: CMMF_EncodePOPODecKeyChallContent(CMMFPOPODecKeyChallContent *inDecKeyChall, michael@0: CRMFEncoderOutputCallback inCallback, michael@0: void *inArg) michael@0: { michael@0: return cmmf_user_encode(inDecKeyChall, inCallback, inArg, michael@0: CMMFPOPODecKeyChallContentTemplate); michael@0: } michael@0: michael@0: CMMFPOPODecKeyRespContent* michael@0: CMMF_CreatePOPODecKeyRespContentFromDER(const char *buf, long len) michael@0: { michael@0: PLArenaPool *poolp; michael@0: CMMFPOPODecKeyRespContent *decKeyResp; michael@0: SECStatus rv; michael@0: michael@0: poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE); michael@0: if (poolp == NULL) { michael@0: return NULL; michael@0: } michael@0: decKeyResp = PORT_ArenaZNew(poolp, CMMFPOPODecKeyRespContent); michael@0: if (decKeyResp == NULL) { michael@0: goto loser; michael@0: } michael@0: decKeyResp->poolp = poolp; michael@0: rv = SEC_ASN1Decode(poolp, decKeyResp, CMMFPOPODecKeyRespContentTemplate, michael@0: buf, len); michael@0: if (rv != SECSuccess) { michael@0: goto loser; michael@0: } michael@0: return decKeyResp; michael@0: 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_EncodeKeyRecRepContent(CMMFKeyRecRepContent *inKeyRecRep, michael@0: CRMFEncoderOutputCallback inCallback, michael@0: void *inArg) michael@0: { michael@0: return cmmf_user_encode(inKeyRecRep, inCallback, inArg, michael@0: CMMFKeyRecRepContentTemplate); michael@0: } michael@0: michael@0: CMMFKeyRecRepContent* michael@0: CMMF_CreateKeyRecRepContentFromDER(CERTCertDBHandle *db, const char *buf, michael@0: long len) michael@0: { michael@0: PLArenaPool *poolp; michael@0: CMMFKeyRecRepContent *keyRecContent; michael@0: SECStatus rv; michael@0: michael@0: poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE); michael@0: if (poolp == NULL) { michael@0: return NULL; michael@0: } michael@0: keyRecContent = PORT_ArenaZNew(poolp, CMMFKeyRecRepContent); michael@0: if (keyRecContent == NULL) { michael@0: goto loser; michael@0: } michael@0: keyRecContent->poolp = poolp; michael@0: rv = SEC_ASN1Decode(poolp, keyRecContent, CMMFKeyRecRepContentTemplate, michael@0: buf, len); michael@0: if (rv != SECSuccess) { michael@0: goto loser; michael@0: } michael@0: if (keyRecContent->keyPairHist != NULL) { michael@0: while(keyRecContent->keyPairHist[keyRecContent->numKeyPairs] != NULL) { michael@0: rv = cmmf_decode_process_certified_key_pair(poolp, db, michael@0: keyRecContent->keyPairHist[keyRecContent->numKeyPairs]); michael@0: if (rv != SECSuccess) { michael@0: goto loser; michael@0: } michael@0: keyRecContent->numKeyPairs++; michael@0: } michael@0: keyRecContent->allocKeyPairs = keyRecContent->numKeyPairs; michael@0: } michael@0: keyRecContent->isDecoded = PR_TRUE; michael@0: return keyRecContent; 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: