My server provides a Self Signed certificate when calling its HTTPS API. I have the certificate file in the asset
folder and referenced its path in pubspec.yaml
I have tried passing the certificate to SecurityContext
and then using that context to create an HttpClient
. But the way I'm passing the certificate to SecurityContext
is not working. Here is the code:
Future<ByteData> getFileData(String path) async {
return await rootBundle.load(path);
}
void initializeHttpClient() async {
try {
Future<ByteData> data = getFileData('assets/raw/certificate.crt');
await data.then((value) {
var context = SecurityContext.defaultContext;
context.useCertificateChainBytes(value.buffer.asInt8List());
client = HttpClient(context: context);
});
} on Exception catch (exception) {
print(exception.toString());
}
}
The SecurityContext
has two methods:
1) useCertificateChain()
this accepts a file path. But when I give the path of the file in my asset folder ('assets/raw/certificate.crt'). It says file not found.
2) useCertificateChainBytes()
the above code is using this method. But this also gives me error like (unexpected end of file).
Solution as of now
I am bypassing it using client.badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
.
but I'd like to make it work with certificate