Issue with SSL Pining using Dio Flutter my certificate is wildcard certificate

Viewed 52

In flutter i am trying to implement SSL Pinning using dio: ^4.0.4 but every time I am getting this error

HandshakeException: Handshake error in client (OS Error: CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:393))

   String pem = "";

late ByteData _byteData;

Future loadAsset() async { String _part = BUILD_ENV == 0 ? "dev" : BUILD_ENV == 1 ? "stage" : BUILD_ENV == 3 ? "test" : "live"; return await rootBundle.load('assets/certificate/$_part/sslCertificate.pem');}

Future loadPem() async { String _part = BUILD_ENV == 0 ? "dev" : BUILD_ENV == 1 ? "stage" : BUILD_ENV == 3 ? "test" : "live"; return rootBundle.loadString('assets/certificate/$_part/sslCertificate.pem'); }

method 1:

(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) {
  SecurityContext securityContext = SecurityContext();
  securityContext.setTrustedCertificatesBytes(_byteData.buffer.asUint8List());
  HttpClient httpClient = HttpClient(context: securityContext);
  return httpClient;
};

method 2:

(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate  = (client) {
  debugPrint("badCertificateCallback :: xx");
  client.badCertificateCallback=(X509Certificate cert, String host, int port){
    debugPrint("badCertificateCallback :: yy ::${cert.pem}");
    if(cert.pem==pem){ // Verify the certificate
      return true;
    }
    return false;
  };
  return client;
};

method 1 keeps giving handshake error (described above)

method 2 (badCertificateCallback :: yy ::) line never get executed (this method never worked)

one more thing my certificate is wildcard certificate *.example.com and my API base URL is api.example.com

can anyone help me to fix this

thanks in advance

1 Answers

String FINGERPRINT_DEV = '45 ED 46 4E 79 3D F6 A7 FD 5F 34 14 5B DF FD B6 B3 EE 7A C1 5C 3D E6 0A 21 45 C7 A1 87 C6 80 23';

String FINGERPRINT_STAGE = '45 22 D3 FD 31 15 74 DA FA 27 1C 88 61 C1 D3 7D A9 00 8C 3F E4 D0 86 1A 8B 45 04 17 68 25 0E E8';

String FINGERPRINT_TEST = '45 AE FB E8 E4 20 8C CD B4 9D EB 51 CD 1D 57 81 61 97 90 46 6F 26 B1 F1 0E BC 45 11 52 5C 1A D7';

String FINGERPRINT_LIVE = '5B 45 78 A0 96 AC 97 12 05 35 92 4B 25 FD AB 7E A1 42 B2 FE 12 BF AD 4C 45 5A C4 D0 BA 5A AB 14';

dio.interceptors.add(CertificatePinningInterceptor(allowedSHAFingerprints: [BUILD_ENV == 0 ? FINGERPRINT_DEV : BUILD_ENV == 1 ? FINGERPRINT_STAGE : BUILD_ENV == 3 ? FINGERPRINT_TEST : FINGERPRINT_LIVE]));

Related