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