unwrapDER function

Uint8List unwrapDER(
  1. ByteBuffer derEncoded,
  2. Uint8List oid
)

Extracts a payload from the given derEncoded data, and checks that it was tagged with the given oid.

derEncoded = SEQUENCE(oid, BITSTRING(payload))

@param derEncoded The DER encoded and tagged data @param oid The DER encoded (and SEQUENCE wrapped!) expected OID @returns The unwrapped payload

Implementation

Uint8List unwrapDER(ByteBuffer derEncoded, Uint8List oid) {
  var offset = 0;

  final buf = Uint8List.fromList(derEncoded.asUint8List());
  check(int n, String msg) {
    if (buf[offset++] != n) throw 'Expected: ' + msg;
  }

  check(0x30, 'sequence');
  offset += decodeLenBytes(buf, offset);

  if (!bufEquals(
      buf.sublist(offset, offset + oid.lengthInBytes).buffer, oid.buffer)) {
    throw 'Not the expected OID.';
  }
  offset += oid.lengthInBytes;

  check(0x03, 'bit string');
  offset += decodeLenBytes(buf, offset);
  check(0x00, '0 padding');
  return buf.sublist(offset);
}