tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

cmmfasn1.c (4308B)


      1 /* -*- Mode: C; tab-width: 8 -*-*/
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include "cmmf.h"
      7 #include "cmmfi.h"
      8 #include "secasn1.h"
      9 #include "secitem.h"
     10 
     11 SEC_ASN1_MKSUB(SEC_SignedCertificateTemplate)
     12 
     13 static const SEC_ASN1Template CMMFSequenceOfCertifiedKeyPairsTemplate[] = {
     14    { SEC_ASN1_SEQUENCE_OF, 0, CMMFCertifiedKeyPairTemplate }
     15 };
     16 
     17 static const SEC_ASN1Template CMMFKeyRecRepContentTemplate[] = {
     18    { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CMMFKeyRecRepContent) },
     19    { SEC_ASN1_INLINE, offsetof(CMMFKeyRecRepContent, status),
     20      CMMFPKIStatusInfoTemplate },
     21    { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_POINTER |
     22          SEC_ASN1_XTRN | 0,
     23      offsetof(CMMFKeyRecRepContent, newSigCert),
     24      SEC_ASN1_SUB(SEC_SignedCertificateTemplate) },
     25    { SEC_ASN1_CONSTRUCTED | SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | 1,
     26      offsetof(CMMFKeyRecRepContent, caCerts),
     27      CMMFSequenceOfCertsTemplate },
     28    { SEC_ASN1_CONSTRUCTED | SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | 2,
     29      offsetof(CMMFKeyRecRepContent, keyPairHist),
     30      CMMFSequenceOfCertifiedKeyPairsTemplate },
     31    { 0 }
     32 };
     33 
     34 SECStatus
     35 CMMF_EncodeCertRepContent(CMMFCertRepContent *inCertRepContent,
     36                          CRMFEncoderOutputCallback inCallback,
     37                          void *inArg)
     38 {
     39    return cmmf_user_encode(inCertRepContent, inCallback, inArg,
     40                            CMMFCertRepContentTemplate);
     41 }
     42 
     43 SECStatus
     44 CMMF_EncodePOPODecKeyChallContent(CMMFPOPODecKeyChallContent *inDecKeyChall,
     45                                  CRMFEncoderOutputCallback inCallback,
     46                                  void *inArg)
     47 {
     48    return cmmf_user_encode(inDecKeyChall, inCallback, inArg,
     49                            CMMFPOPODecKeyChallContentTemplate);
     50 }
     51 
     52 CMMFPOPODecKeyRespContent *
     53 CMMF_CreatePOPODecKeyRespContentFromDER(const char *buf, long len)
     54 {
     55    PLArenaPool *poolp;
     56    CMMFPOPODecKeyRespContent *decKeyResp;
     57    SECStatus rv;
     58 
     59    poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
     60    if (poolp == NULL) {
     61        return NULL;
     62    }
     63    decKeyResp = PORT_ArenaZNew(poolp, CMMFPOPODecKeyRespContent);
     64    if (decKeyResp == NULL) {
     65        goto loser;
     66    }
     67    decKeyResp->poolp = poolp;
     68    rv = SEC_ASN1Decode(poolp, decKeyResp, CMMFPOPODecKeyRespContentTemplate,
     69                        buf, len);
     70    if (rv != SECSuccess) {
     71        goto loser;
     72    }
     73    return decKeyResp;
     74 
     75 loser:
     76    if (poolp != NULL) {
     77        PORT_FreeArena(poolp, PR_FALSE);
     78    }
     79    return NULL;
     80 }
     81 
     82 SECStatus
     83 CMMF_EncodeKeyRecRepContent(CMMFKeyRecRepContent *inKeyRecRep,
     84                            CRMFEncoderOutputCallback inCallback,
     85                            void *inArg)
     86 {
     87    return cmmf_user_encode(inKeyRecRep, inCallback, inArg,
     88                            CMMFKeyRecRepContentTemplate);
     89 }
     90 
     91 CMMFKeyRecRepContent *
     92 CMMF_CreateKeyRecRepContentFromDER(CERTCertDBHandle *db, const char *buf,
     93                                   long len)
     94 {
     95    PLArenaPool *poolp;
     96    CMMFKeyRecRepContent *keyRecContent;
     97    SECStatus rv;
     98 
     99    poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
    100    if (poolp == NULL) {
    101        return NULL;
    102    }
    103    keyRecContent = PORT_ArenaZNew(poolp, CMMFKeyRecRepContent);
    104    if (keyRecContent == NULL) {
    105        goto loser;
    106    }
    107    keyRecContent->poolp = poolp;
    108    rv = SEC_ASN1Decode(poolp, keyRecContent, CMMFKeyRecRepContentTemplate,
    109                        buf, len);
    110    if (rv != SECSuccess) {
    111        goto loser;
    112    }
    113    if (keyRecContent->keyPairHist != NULL) {
    114        while (keyRecContent->keyPairHist[keyRecContent->numKeyPairs] != NULL) {
    115            rv = cmmf_decode_process_certified_key_pair(poolp, db,
    116                                                        keyRecContent->keyPairHist[keyRecContent->numKeyPairs]);
    117            if (rv != SECSuccess) {
    118                goto loser;
    119            }
    120            keyRecContent->numKeyPairs++;
    121        }
    122        keyRecContent->allocKeyPairs = keyRecContent->numKeyPairs;
    123    }
    124    keyRecContent->isDecoded = PR_TRUE;
    125    return keyRecContent;
    126 loser:
    127    if (poolp != NULL) {
    128        PORT_FreeArena(poolp, PR_FALSE);
    129    }
    130    return NULL;
    131 }