decodeSecp256r1PrivateKeySec1 static method
Implementation
static Secp256r1Sec1Key decodeSecp256r1PrivateKeySec1(List<int> der) {
final root = DerReader(der).readNode();
_expectTag(root, DerTag.sequence, 'ECPrivateKey SEQUENCE');
final fields = root.children();
if (fields.length < 2) {
throw CoreErrors.cryptoFailed(
"decodeSecp256r1PrivateKeySec1",
reason: 'Malformed ECPrivateKey',
);
}
_expectTag(fields[0], DerTag.integer, 'version');
if (fields[0].value.isNotEmpty && fields[0].value.last != 1) {
throw CoreErrors.cryptoFailed(
"decodeSecp256r1PrivateKeySec1",
reason: 'Unsupported ECPrivateKey version',
);
}
_expectTag(fields[1], DerTag.octetString, 'privateKey');
final privateKey = fields[1].value;
String? curveOid;
BuBytes? publicKeyPoint;
for (var i = 2; i < fields.length; i++) {
final f = fields[i];
if (f.tag == DerTag.ctx0) {
final inner = DerReader(f.value).readNode();
_expectTag(inner, DerTag.oid, 'namedCurve');
curveOid = DerUtils.decodeOid(inner.value);
} else if (f.tag == DerTag.ctx1) {
final inner = DerReader(f.value).readNode();
_expectTag(inner, DerTag.bitString, 'publicKey');
if (inner.value.isEmpty) {
throw CoreErrors.cryptoFailed(
"decodeSecp256r1PrivateKeySec1",
reason: 'Malformed publicKey',
);
}
publicKeyPoint = inner.value.sublist(1); // drop unused-bits count byte
}
}
return Secp256r1Sec1Key.from(
privateKey: SecretBytes.take(privateKey),
publicKeyPoint: publicKeyPoint?.asImmutableBytes,
curveOid: curveOid,
);
}