parseDer static method
Parses DER-encoded EC public key.
Currently this is implemented only for very specific inputs: those generated by Apple's CryptoKit. Apple could decide to change their implementation in future (though it has no reason to). Therefore we would like to transition to a proper ASN.1 decoder.
Implementation
static EcPublicKey parseDer(List<int> der, {required KeyPairType type}) {
// Parsing of CryptoKit generated keys:
// Unfortunately our current solutions is not future proof. Apple may change
// the format in the future. We want to transition to a proper ASN.1 decoder.
final config = CupertinoEcDer.get(type);
final prefix = config.publicKeyPrefix;
final numberLength = config.numberLength;
if (!bytesStartsWith(der, prefix, 0)) {
throw UnsupportedError(
'Your version of package:cryptography supports only specific DER encodings from Apple CryptoKit',
);
}
if (der.length != prefix.length + 2 * numberLength) {
throw UnsupportedError(
'Your version of package:cryptography supports only specific DER encodings from Apple CryptoKit',
);
}
final xIndex = prefix.length;
final yIndex = xIndex + numberLength;
final x = Uint8List.fromList(der.sublist(
xIndex,
yIndex,
));
final y = Uint8List.fromList(der.sublist(
yIndex,
yIndex + numberLength,
));
return EcPublicKey(x: x, y: y, type: type);
}