All files / src/codec/ber/decoder BERDecoder.ts

83.14% Statements 74/89
73.33% Branches 22/30
100% Functions 5/5
82.95% Lines 73/88

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 1641x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 4x 4x 4x 4x           64x 64x   64x   64x   64x 64x 64x                                           64x 64x   64x       64x 47x 47x       17x 17x 31x 31x   17x 17x             37x 37x                 37x     37x 37x 37x 60x 60x 33x 33x 33x   27x   37x       27x   27x                   27x 5x 5x   22x 12x 12x   10x     1x 1x   9x     3x 3x   6x 2x         2x 2x   4x 1x 1x   3x 3x 3x            
import {CursorDataView} from '../../../CursorDataView';
import {ASN1Message} from '../../../interfaces/ASN1Message';
import {BERLength} from '../../../interfaces/BERLength';
import {LengthType} from '../../../interfaces/LengthType';
import {TagUniversal} from '../../../interfaces/TagUniversal';
import {TagDecoder} from '../../../TagDecoder';
import {Identifier} from './Identifier';
import {getTagClass, isConstructed} from './misc';
import {readBitString} from './primitive/ReadBitString';
import {readBoolean} from './primitive/ReadBoolean';
import {readIA5String} from './primitive/ReadIA5String';
import {readInteger} from './primitive/ReadInteger';
import {readObjectIdentifier} from './primitive/ReadObjectIdentifier';
import {readOctetString} from './primitive/ReadOctetString';
 
export function BERDecode(input: ArrayBuffer): ASN1Message {
  const cdv = new CursorDataView(input);
  const identifier = readIdentifierOctets(cdv);
  Eif (identifier.isComposed()) {
    return readComposed(cdv, identifier);
  }
  return readSimple(cdv, identifier);
}
 
function readIdentifierOctets(cdv: CursorDataView): Identifier {
  const result = new Identifier();
  const octet = cdv.read();
  // (8.1.2.2 a)
  result.tagClass = getTagClass(octet);
  // (8.1.2.2 b)
  result.constructed = isConstructed(octet);
  // (8.1.2.4.1 c)
  result.tag = octet & 0b0001_1111;
  Eif (result.tag < 0b0001_1111) {
    return result;
  }
  // multi octets.
  let tagNumberStr = '';
  let octetN;
  while (true) {
    octetN = cdv.read();
    // bit 7 to 1 (8.1.2.4.2 b)
    const str = (octetN & 0b0111_1111).toString(2);
    console.log('str: ', str);
    tagNumberStr += str;
    // last octet (8.1.2.4.2 a)
    if ((octetN & 0b1000_0000) === 0) {
      break;
    }
  }
  // (8.1.2.4.2 b) concactenation
  result.tag = parseInt(tagNumberStr, 2);
  return result;
}
 
function readLengthOctets(cdv: CursorDataView): BERLength {
  const berLength: BERLength = {type: LengthType.DEFINITE, length: 0};
  const length = cdv.read();
  // (8.1.3.6.1): indefinite form
  Iif (length === 0b1000_0000) {
    return {type: LengthType.INDEFINITE, length: -1};
  }
  // (8.1.3.4): one octet case (<=127, definite form)
  if ((length & 0b1000_0000) === 0) {
    berLength.length = length;
    return berLength;
  }
 
  // (8.1.3.5 b) Big Length case (>127)
  let bigLength = 0;
  for (let i = 0; i < length - 0b1000_0000; i++) {
    const nbr = cdv.read();
    bigLength = bigLength * 256 + nbr;
  }
  berLength.length = bigLength;
  return berLength;
}
 
function readComposed(
  cdv: CursorDataView,
  identifier: Identifier
): ASN1Message {
  const {length, type} = readLengthOctets(cdv);
  const result: ASN1Message = {
    tagClass: identifier.tagClass,
    isConstructed: identifier.constructed,
    tagCode: identifier.tag,
    tagLabel: TagDecoder.getLabel(identifier.tagClass, identifier.tag),
    length: length,
    lengthType: type,
    value: null,
  };
  Iif (length === 0) {
    return result;
  }
  const max = cdv.index + length;
  result.value = [];
  while (cdv.index < max) {
    const ident = readIdentifierOctets(cdv);
    if (ident.isComposed()) {
      const a = readComposed(cdv, ident);
      result.value.push(a);
      continue;
    }
    result.value.push(readSimple(cdv, ident));
  }
  return result;
}
 
function readSimple(cdv: CursorDataView, identifier: Identifier): ASN1Message {
  const {length, type} = readLengthOctets(cdv);
 
  const result: ASN1Message = {
    tagClass: identifier.tagClass,
    isConstructed: identifier.constructed,
    tagCode: identifier.tag,
    tagLabel: TagDecoder.getLabel(identifier.tagClass, identifier.tag),
    length: length,
    lengthType: type,
    value: null,
  };
 
  if (identifier.tag === TagUniversal.BOOLEAN.code) {
    result.value = readBoolean(cdv, length);
    return result;
  }
  if (identifier.tag === TagUniversal.INTEGER.code) {
    result.value = readInteger(cdv, length);
    return result;
  }
  if (identifier.tag === TagUniversal.BIT_STRING.code) {
    // 8.6.1 The encoding of a bitstring value shall be either primitive
    // or constructed at the option of the sender.
    result.value = readBitString(cdv, length);
    return result;
  }
  if (identifier.tag === TagUniversal.OCTET_STRING.code) {
    // 8.7.1 The encoding of an octetstring value shall be either primitive
    // or constructed at the option of the sender.
    result.value = readOctetString(cdv, length);
    return result;
  }
  if (identifier.tag === TagUniversal.OBJECT_IDENTIFIER.code) {
    Iif (identifier.constructed) {
      throw new Error(
        'The encoding of an Object Identifier should be primitive (clause 8.19.1).'
      );
    }
    result.value = readObjectIdentifier(cdv, length);
    return result;
  }
  if (identifier.tag === TagUniversal.IA5STRING.code) {
    result.value = readIA5String(cdv, length);
    return result;
  }
  Eif (identifier.tag === TagUniversal.GENERAL_STRING.code) {
    result.value = readIA5String(cdv, length);
    return result;
  }
  throw new Error(
    `cannot understand the identifier tag number: ${identifier.tag}`
  );
}