dvCertificateP256PublicKey function

Uint8List? dvCertificateP256PublicKey(
  1. List<int> der
)

The uncompressed P-256 point a certificate's subject public key info carries, or null when der is not a certificate with a P-256 key.

Read by structure -- certificate, to-be-signed certificate, the seventh field of it -- never by searching the bytes: a certificate for another key can carry the pairing key anywhere else it likes.

Implementation

Uint8List? dvCertificateP256PublicKey(List<int> der) {
  try {
    final _Element certificate = _read(der, 0, der.length);
    if (certificate.tag != 0x30) return null;
    final _Element tbs = _read(der, certificate.start, certificate.end);
    if (tbs.tag != 0x30) return null;
    int at = tbs.start;
    _Element field = _read(der, at, tbs.end);
    // version is optional, as [0].
    if (field.tag == 0xa0) {
      at = field.end;
      field = _read(der, at, tbs.end);
    }
    // serialNumber, signature, issuer, validity, subject.
    for (int i = 0; i < 5; i++) {
      at = field.end;
      field = _read(der, at, tbs.end);
    }
    if (field.tag != 0x30) return null;
    final _Element algorithm = _read(der, field.start, field.end);
    if (algorithm.tag != 0x30) return null;
    final _Element keyType = _read(der, algorithm.start, algorithm.end);
    if (keyType.tag != 0x06 ||
        !_equals(der, keyType.start, keyType.end, _ecPublicKey)) {
      return null;
    }
    final _Element curve = _read(der, keyType.end, algorithm.end);
    if (curve.tag != 0x06 ||
        !_equals(der, curve.start, curve.end, _prime256v1)) {
      return null;
    }
    final _Element bits = _read(der, algorithm.end, field.end);
    if (bits.tag != 0x03 || bits.end - bits.start != 66) return null;
    if (der[bits.start] != 0 || der[bits.start + 1] != 0x04) return null;
    return Uint8List.fromList(der.sublist(bits.start + 1, bits.end));
  } on FormatException {
    return null;
  } on RangeError {
    return null;
  }
}