decodeCertPublicKey static method

PublicKey decodeCertPublicKey(
  1. Uint8List bytes
)

Format:

SubjectPublicKeyInfo  ::=  SEQUENCE  {
   algorithm            AlgorithmIdentifier,
   subjectPublicKey     BIT STRING
}

AlgorithmIdentifier  ::=  SEQUENCE  {
    algorithm           OBJECT IDENTIFIER,
    parameters          ANY DEFINED BY algorithm OPTIONAL
}

Implementation

static PublicKey decodeCertPublicKey(Uint8List bytes) {
  final parser = ASN1Parser(bytes);

  if (!parser.hasNext()) {
    throw ArgumentError('Not a ASN.1 sequence');
  }

  final pubKeyInfoSeq = parser.nextObject() as ASN1Sequence;
  final algorithmIdentifier = pubKeyInfoSeq.elements[0] as ASN1Sequence;
  final algorithm = algorithmIdentifier.elements[0] as ASN1ObjectIdentifier;

  if (algorithm.identifier!.contains('1.2.840.113549.1.1.1')) {
    final subjectPubKey = pubKeyInfoSeq.elements[1] as ASN1BitString;
    return decodePKCS1PublicKey(subjectPubKey.encodedBytes);
  }

  throw ArgumentError('Could only decode PKCS1 RSA Public Key');
}