getCertifications method

  1. @override
Future<List<Certificate>> getCertifications()
override

Implementation

@override
Future<List<Certificate>> getCertifications() async {
  try {
    final response =
        await methodChannel.invokeMethod<List<dynamic>>('getCertifications');
    if (response == null || response.isEmpty) {
      return List.empty();
    }
    return response.where((item) {
      try {
        if (item is! UnmodifiableUint8ListView) {
          return false;
        }
        final bytes = Uint8List.fromList(item);
        final asn1Parser = ASN1Parser(bytes);
        final asn1Sequence = asn1Parser.nextObject();
        if (asn1Sequence is! ASN1Sequence) {
          return false;
        }
        final x509 = x509Cert.X509Certificate.fromAsn1(asn1Sequence);
        final subjectNames = x509.tbsCertificate.subject?.names;
        final cn = subjectNames?[0].values.first.toString();
        if (cn == null) {
          return false;
        }
        return true;
      } catch (e) {
        if (kDebugMode) {
          print('Error processing item: $e');
        }
        return false;
      }
    }).map<Certificate>((item) {
      final bytes = Uint8List.fromList(item);
      final asn1Parser = ASN1Parser(bytes);
      final asn1Sequence = asn1Parser.nextObject() as ASN1Sequence;
      final x509 = x509Cert.X509Certificate.fromAsn1(asn1Sequence);
      final subjectNames = x509.tbsCertificate.subject?.names;
      final cn = subjectNames?[0].values.first.toString();
      return Certificate(
        publickey: item,
        x509certificate: x509,
        cn: cn!,
      );
    }).toList();
  } catch (e) {
    rethrow;
  }
}