bytesUnwrapDer function

Uint8List bytesUnwrapDer(
  1. Uint8List 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))

derEncoded is the DER encoded and tagged data. oid is the DER encoded (and SEQUENCE wrapped!) expected OID

Implementation

Uint8List bytesUnwrapDer(Uint8List derEncoded, Uint8List oid) {
  int offset = 0;
  final buf = Uint8List.fromList(derEncoded);

  void check(int expected, String name) {
    if (buf[offset] != expected) {
      throw ArgumentError.value(
        buf[offset],
        name,
        'Expected $expected for $name but got',
      );
    }
    offset++;
  }

  check(0x30, 'sequence');
  offset += decodeLenBytes(buf, offset);
  if (!bufEquals(
    buf.sublist(offset, offset + oid.lengthInBytes).buffer,
    oid.buffer,
  )) {
    throw StateError('Not the expecting OID.');
  }
  offset += oid.lengthInBytes;
  check(0x03, 'bit string');
  offset += decodeLenBytes(buf, offset);
  check(0x00, '0 padding');
  return buf.sublist(offset);
}