decodeSecp256r1PublicKeySpki static method
Implementation
static BuBytes decodeSecp256r1PublicKeySpki(List<int> der) {
final root = DerReader(der).readNode();
_expectTag(root, DerTag.sequence, 'SubjectPublicKeyInfo SEQUENCE');
final fields = root.children();
if (fields.length < 2) {
throw CoreErrors.cryptoFailed(
"decodeSecp256r1PublicKeySpki",
reason: 'Malformed SubjectPublicKeyInfo',
);
}
final algId = fields[0];
_expectTag(algId, DerTag.sequence, 'AlgorithmIdentifier');
final algFields = algId.children();
if (algFields.isEmpty) {
throw CoreErrors.cryptoFailed(
"decodeSecp256r1PublicKeySpki",
reason: 'Malformed AlgorithmIdentifier',
);
}
final algOid = DerUtils.decodeOid(algFields[0].value);
if (algOid != Oids.ecPublicKey) {
throw CoreErrors.cryptoFailed(
"decodeSecp256r1PublicKeySpki",
reason: 'Not an EC key (algorithm OID $algOid)',
);
}
if (algFields.length > 1) {
final curveOid = DerUtils.decodeOid(algFields[1].value);
if (curveOid != Oids.secp256r1) {
throw CoreErrors.cryptoFailed(
"decodeSecp256r1PublicKeySpki",
reason: 'Not a secp256r1 key (curve OID $curveOid)',
);
}
}
_expectTag(fields[1], DerTag.bitString, 'subjectPublicKey');
if (fields[1].value.isEmpty) {
throw CoreErrors.cryptoFailed(
"decodeSecp256r1PublicKeySpki",
reason: 'Malformed subjectPublicKey',
);
}
return BuBytes(fields[1].value.sublist(1)); // drop unused-bits count byte
}