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

浙公網安備 33010602011771號