RSAPrivateKey.fromASN1 constructor

RSAPrivateKey.fromASN1(
  1. dynamic input, {
  2. bool fromPkcs1 = true,
})

Implementation

factory RSAPrivateKey.fromASN1(dynamic /* String | Iterable<int> */ input,
    {bool fromPkcs1 = true}) {
  if (!fromPkcs1) {
    final seq = ASN1Sequence.decode(input);
    if (seq.children.length != 3) {
      throw Exception('Invalid structure');
    }

    if (seq.children[2] is! ASN1OctetString) {
      throw Exception('Invalid structure');
    }

    final bitString = seq.children[2] as ASN1OctetString;

    if (seq.children[1] is! ASN1Sequence) {
      throw Exception('Invalid structure');
    }

    final algIdentifier = seq.children[1] as ASN1Sequence;

    if (algIdentifier.children.isEmpty) {
      throw Exception('Invalid structure');
    }

    if (algIdentifier.children.first is! ASN1ObjectIdentifier) {
      throw Exception('Invalid structure');
    }

    final ASN1ObjectIdentifier identifer =
        algIdentifier.children.first as ASN1ObjectIdentifier;

    if (identifer.objectIdentifierAsString != '1.2.840.113549.1.1.1') {
      throw Exception('Invalid structure');
    }

    input = bitString.value;
  }

  final seq = ASN1Sequence.decode(input);

  if (seq.children.length < 9) {
    throw Exception('Invalid structure');
  }

  final relevant = seq.children.skip(1).take(5);
  if (relevant.any((el) => el is! ASN1Integer)) {
    throw Exception('Invalid structure');
  }

  final bigInts = relevant.map((e) => (e as ASN1Integer).value).toList();
  return RSAPrivateKey(
      bigInts[0], bigInts[1], bigInts[2], bigInts[3], bigInts[4]);
}