openssl 生成证书上 grpc 报 legacy Common Name field, use SANs or temporarily enable Common Name matching with GODEBUG=x509ignoreCN=0

最近用传统的方式 生成的证书上用golang 1.15. 版本 报 grpc 上面

 

➜  ~ go version
go version go1.15.3 darwin/amd64

上面调用的时候报错了

rpc error: code = Unavailable desc = connection error: desc = "transport: authentication handshake failed: x509: certificate relies on legacy Common Name field, use SANs or temporarily enable Common Name matching with GODEBUG=x509ignoreCN=0"

如果出现上述报错,是因为 go 1.15 版本开始废弃 CommonName,因此推荐使用 SAN 证书。 如果想兼容之前的方式,需要设置环境变量 GODEBUG 为 x509ignoreCN=0

 

什么是 SAN
SAN(Subject Alternative Name) 是 SSL 标准 x509 中定义的一个扩展。使用了 SAN 字段的 SSL 证书,可以扩展此证书支持的域名,使得一个证书可以支持多个不同域名的解析。

下面简单示例如何用 openssl 生成 ca 和双方 SAN 证书。

 准备默认 OpenSSL 配置文件于当前目录

linux系统在 :

/etc/pki/tls/openssl.cnf

Mac系统在:

/System/Library/OpenSSL/openssl.cnf

cp 目录到你随意目录进行修改设置

cp /System/Library/OpenSSL/openssl.cnf /Users/jackluo/works/golang/src/grpc-go-practice/example/hello/sslconf

此文件的格式是类似 ini 的配置文件格式,找到 [ req ] 段落,加上下面的配置:

req_extensions = v3_req # The extensions to add to a certificate request

将前面的#号去掉

加入一段名为 v3_req 的配置

这段配置中最重要的是在最后导入名为 alt_names 的配置段,因此我们还需要添加一个名为 [ alt_names ] 的配置段:

[ alt_names ]
DNS.1 = www.zchd.ltd
DNS.2 = www.test.zchd.ltd

这里填入需要加入到 Subject Alternative Names 段落中的域名名称,可以写入多个。

接着使用这个临时配置生成证书:

➜ openssl req -new -nodes -keyout ustack.key -out ustack.csr -config openssl.cnf
➜  openssl x509 -text -noout -in zchd.crt 
Certificate Request:
    Data:
        Version: 0 (0x0)
        Subject: C=CN, ST=Some-State, O=Internet Widgits Pty Ltd, CN=www.zchd.ltd
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (1024 bit)
                Modulus:
                    00:be:b9:25:23:e3:89:39:8e:9e:71:4e:e1:89:da:
                    fc:e8:ad:46:67:1a:ab:dd:1f:0e:24:52:32:fb:cd:
                    76:0b:bd:a5:1e:44:88:c1:5d:5d:61:ac:0a:54:6c:
                    b3:ef:37:a7:e5:d3:73:13:55:c8:17:2c:5b:20:35:
                    27:03:9e:da:73:97:3e:ce:35:98:0b:a6:22:c0:07:
                    b2:4e:75:07:29:ee:7b:20:04:79:fd:ff:39:a2:bf:
                    c6:51:fd:53:9b:20:3c:dc:f4:8c:c1:48:7a:82:df:
                    e7:bf:a6:95:52:3e:be:77:61:44:9a:b5:18:51:4b:
                    22:1f:0f:84:9a:62:fb:37:07
                Exponent: 65537 (0x10001)
        Attributes:
        Requested Extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            X509v3 Key Usage: 
                Digital Signature, Non Repudiation, Key Encipherment
            X509v3 Subject Alternative Name: 
                DNS:www.zchd.ltd, DNS:www.test.zchd.ltd
    Signature Algorithm: sha256WithRSAEncryption
         69:c1:c3:4a:26:b3:87:1e:88:2e:be:de:f3:13:00:53:9a:7e:
         60:6c:f5:1c:81:f1:04:84:9a:94:55:09:8d:66:05:da:79:7e:
         6c:aa:53:a6:1a:d8:d5:bf:bd:51:2e:ee:45:04:6b:c9:24:73:
         5b:5b:64:e6:3b:3b:b4:15:90:ba:b5:a4:a6:20:f8:4c:e8:f1:
         2e:07:3c:ac:68:a5:3b:8c:ce:86:39:f1:84:59:26:9e:de:4f:
         54:19:0c:8b:be:56:49:ef:86:11:86:4e:66:2f:d5:78:1d:fa:
         16:76:a4:9f:4c:34:96:72:ef:d0:1d:ef:18:bf:ae:2b:f7:39:
         81:38

使用单条命令实现

生成默认 ca:

openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -subj "/CN=example.ca.com" -days 5000 -out ca.crt

生成证书

openssl req -new -sha256 \
    -key ca.key \
    -subj "/C=CN/ST=Beijing/L=Beijing/O=UnitedStack/OU=Devops/CN=www.zchd.ltd" \
    -reqexts SAN \
    -config <(cat /System/Library/OpenSSL/openssl.cnf \
        <(printf "[SAN]\nsubjectAltName=DNS:www.zchd.ltd,DNS:www.test.zchd.ltd")) \
    -out zchd.csr    

签名证书

openssl x509 -req -days 365000 \
    -in zchd.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
    -extfile <(printf "subjectAltName=DNS:www.zchd.ltd,DNS:www.test.zchd.ltd") \
    -out zchd.crt

 

基本上就可以很愉快的玩耍了.

上面生成证书请求时的几个字段的意义:

C  => Country
ST => State
L  => City
O  => Organization
OU => Organization Unit
CN => Common Name (证书所请求的域名)
emailAddress => main administrative point of contact for the certificate

 

posted @ 2020-10-19 17:07  jackluo  阅读(2177)  评论(1编辑  收藏