security/nss/lib/crmf/cmmfasn1.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/crmf/cmmfasn1.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,132 @@
     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 +#include "cmmf.h"
    1.10 +#include "cmmfi.h"
    1.11 +#include "secasn1.h"
    1.12 +#include "secitem.h"
    1.13 +
    1.14 +SEC_ASN1_MKSUB(SEC_SignedCertificateTemplate)
    1.15 +
    1.16 +static const SEC_ASN1Template CMMFSequenceOfCertifiedKeyPairsTemplate[] = {
    1.17 +    { SEC_ASN1_SEQUENCE_OF, 0, CMMFCertifiedKeyPairTemplate}
    1.18 +};
    1.19 +
    1.20 +static const SEC_ASN1Template CMMFKeyRecRepContentTemplate[] = {
    1.21 +    { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CMMFKeyRecRepContent)},
    1.22 +    { SEC_ASN1_INLINE, offsetof(CMMFKeyRecRepContent, status), 
    1.23 +      CMMFPKIStatusInfoTemplate},
    1.24 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_POINTER | 
    1.25 +		SEC_ASN1_XTRN | 0,
    1.26 +      offsetof(CMMFKeyRecRepContent, newSigCert),
    1.27 +      SEC_ASN1_SUB(SEC_SignedCertificateTemplate)},
    1.28 +    { SEC_ASN1_CONSTRUCTED | SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | 1,
    1.29 +      offsetof(CMMFKeyRecRepContent, caCerts),
    1.30 +      CMMFSequenceOfCertsTemplate},
    1.31 +    { SEC_ASN1_CONSTRUCTED | SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | 2,
    1.32 +      offsetof(CMMFKeyRecRepContent, keyPairHist),
    1.33 +      CMMFSequenceOfCertifiedKeyPairsTemplate},
    1.34 +    { 0 }
    1.35 +};
    1.36 +
    1.37 +SECStatus
    1.38 +CMMF_EncodeCertRepContent (CMMFCertRepContent        *inCertRepContent,
    1.39 +			   CRMFEncoderOutputCallback  inCallback,
    1.40 +			   void                      *inArg)
    1.41 +{
    1.42 +    return cmmf_user_encode(inCertRepContent, inCallback, inArg,
    1.43 +			    CMMFCertRepContentTemplate);
    1.44 +}
    1.45 +
    1.46 +SECStatus
    1.47 +CMMF_EncodePOPODecKeyChallContent(CMMFPOPODecKeyChallContent *inDecKeyChall,
    1.48 +				  CRMFEncoderOutputCallback inCallback,
    1.49 +				  void                     *inArg)
    1.50 +{
    1.51 +    return cmmf_user_encode(inDecKeyChall, inCallback, inArg,
    1.52 +			    CMMFPOPODecKeyChallContentTemplate);
    1.53 +}
    1.54 +
    1.55 +CMMFPOPODecKeyRespContent*
    1.56 +CMMF_CreatePOPODecKeyRespContentFromDER(const char *buf, long len)
    1.57 +{
    1.58 +    PLArenaPool               *poolp;
    1.59 +    CMMFPOPODecKeyRespContent *decKeyResp;
    1.60 +    SECStatus                  rv;
    1.61 +
    1.62 +    poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
    1.63 +    if (poolp == NULL) {
    1.64 +        return NULL;
    1.65 +    }
    1.66 +    decKeyResp = PORT_ArenaZNew(poolp, CMMFPOPODecKeyRespContent);
    1.67 +    if (decKeyResp == NULL) {
    1.68 +        goto loser;
    1.69 +    }
    1.70 +    decKeyResp->poolp = poolp;
    1.71 +    rv = SEC_ASN1Decode(poolp, decKeyResp, CMMFPOPODecKeyRespContentTemplate,
    1.72 +			buf, len);
    1.73 +    if (rv != SECSuccess) {
    1.74 +        goto loser;
    1.75 +    }
    1.76 +    return decKeyResp;
    1.77 +    
    1.78 + loser:
    1.79 +    if (poolp != NULL) {
    1.80 +        PORT_FreeArena(poolp, PR_FALSE);
    1.81 +    }
    1.82 +    return NULL;
    1.83 +}
    1.84 +
    1.85 +SECStatus
    1.86 +CMMF_EncodeKeyRecRepContent(CMMFKeyRecRepContent      *inKeyRecRep,
    1.87 +			    CRMFEncoderOutputCallback  inCallback,
    1.88 +			    void                      *inArg)
    1.89 +{
    1.90 +    return cmmf_user_encode(inKeyRecRep, inCallback, inArg,
    1.91 +			    CMMFKeyRecRepContentTemplate);
    1.92 +}
    1.93 +
    1.94 +CMMFKeyRecRepContent* 
    1.95 +CMMF_CreateKeyRecRepContentFromDER(CERTCertDBHandle *db, const char *buf, 
    1.96 +				   long len)
    1.97 +{
    1.98 +    PLArenaPool          *poolp;
    1.99 +    CMMFKeyRecRepContent *keyRecContent;
   1.100 +    SECStatus             rv;
   1.101 +
   1.102 +    poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
   1.103 +    if (poolp == NULL) {
   1.104 +        return NULL;
   1.105 +    }
   1.106 +    keyRecContent = PORT_ArenaZNew(poolp, CMMFKeyRecRepContent);
   1.107 +    if (keyRecContent == NULL) {
   1.108 +        goto loser;
   1.109 +    }
   1.110 +    keyRecContent->poolp = poolp;
   1.111 +    rv = SEC_ASN1Decode(poolp, keyRecContent, CMMFKeyRecRepContentTemplate,
   1.112 +			buf, len);
   1.113 +    if (rv != SECSuccess) {
   1.114 +        goto loser;
   1.115 +    }
   1.116 +    if (keyRecContent->keyPairHist != NULL) {
   1.117 +        while(keyRecContent->keyPairHist[keyRecContent->numKeyPairs] != NULL) {
   1.118 +	    rv = cmmf_decode_process_certified_key_pair(poolp, db,
   1.119 +		       keyRecContent->keyPairHist[keyRecContent->numKeyPairs]);
   1.120 +	    if (rv != SECSuccess) {
   1.121 +	        goto loser;
   1.122 +	    }
   1.123 +	    keyRecContent->numKeyPairs++;
   1.124 +	}
   1.125 +	keyRecContent->allocKeyPairs = keyRecContent->numKeyPairs;
   1.126 +    }
   1.127 +    keyRecContent->isDecoded = PR_TRUE;
   1.128 +    return keyRecContent;
   1.129 + loser:
   1.130 +    if (poolp != NULL) {
   1.131 +        PORT_FreeArena(poolp, PR_FALSE);
   1.132 +    }
   1.133 +    return NULL;
   1.134 +}
   1.135 +

mercurial