如何修复x509:无法验证<ip>的证书,因为它不包含任何IP SAN

问题描述

我正在使用go http客户端连接到具有自签名证书的物联网设备。我已经有

Widget build(BuildContext context) {


    return WillPopScope(
      onWillPop: ()=> browserBack(context),child:Scaffold(

      body: Builder(builder: (BuildContext context){
          return WebView(
            initialUrl: 'https://Flutter.dev/docs/deployment/android',javascriptMode: JavascriptMode.unrestricted,onWebViewCreated: (WebViewController webViewController) {
              _controller.complete(webViewController);
              controllerGlobal = webViewController;
            },);
      },),);

尽管 TLSClientConfig: &tls.Config{ RootCAs: certPool,Certificates: []tls.Certificate{tlsClientCert},InsecureSkipVerify: true,},仍然尝试验证证书:

InsecureSkipVerify=true

由于我无法在设备上更改证书,我可以修改TLS客户端配置的哪一部分?

更新

可以在设备上运行https://github.com/jbardin/gotlsscan/blob/master/main.go来复制go错误

x509: cannot validate certificate for <ip> because it doesn't contain any IP SANs

openssl在运行Testing TLS1.2 ... TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA [NOT SUPPORTED] TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 [NOT SUPPORTED] x509: cannot validate certificate for 192.168.1.145 because it doesn't contain any IP SANs TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 [NOT SUPPORTED] ... 时说的是这样:

openssl s_client -connect <ip:port>

更新我正在运行最新的go 1.15.2

解决方法

这可能有用,

证书中有一个字段叫做“SANs”,我们需要在这个SAN列表中添加'hostname'。

Properties

添加后,应使用“ServerName”字段在 TLS 配置中添加相同的名称。在此配置后,这将得到解决。在 SANS 属性中,我添加了“test.com”,因此我按如下方式配置了 TLS,

        ServerName:   "test.com",RootCAs:      pool,Certificates: []tls.Certificate{clientCert},MinVersion:   tls.VersionTLS12,

由于您使用的证书可能不包含任何 SAN,因此发生此错误。我还在探索中,如果你们对此有任何意见,请留下回复。