setupHttpsCertificateVerification method
HTTPS证书验证
use openssl to read the SHA256 value of a certificate:
openssl s_client -servername pinning-test.badssl.com -connect pinning-test.badssl.com:443
< /dev/null 2>/dev/null \ | openssl x509 -noout -fingerprint -sha256
Implementation
void setupHttpsCertificateVerification(String fingerprint) {
_client!.httpClientAdapter = IOHttpClientAdapter(
createHttpClient: () {
// Don't trust any certificate just because their root cert is trusted.
final HttpClient client =
HttpClient(context: SecurityContext(withTrustedRoots: false));
// You can test the intermediate / root cert here. We just ignore it.
client.badCertificateCallback = (cert, host, port) => true;
return client;
},
validateCertificate: (cert, host, port) {
// Check that the cert fingerprint matches the one we expect.
// We definitely require _some_ certificate.
if (cert == null) {
return false;
}
// Validate it any way you want. Here we only check that
// the fingerprint matches the OpenSSL SHA256.
return fingerprint == sha256.convert(cert.der).toString();
},
);
}