Ssl

OpenSSL s_client 在一台機器上返回不受支持的證書用途,但在具有相同證書的另一台機器上正常工作

  • October 24, 2019

我正在調試主機和 docker 容器之間的 TLS 連接問題。我的 docker 容器有一個伺服器證書:

-----BEGIN CERTIFICATE-----
MIICWjCCAgGgAwIBAgIRANvW2Yf383GwxnIkjgkYSkYwCgYIKoZIzj0EAwIwcDEL
MAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBG
cmFuY2lzY28xFjAUBgNVBAoTDWluc3VyYW5jZS1vcmcxHDAaBgNVBAMTE3Rsc2Nh
Lmluc3VyYW5jZS1vcmcwHhcNMTkxMDIzMDYyNjQyWhcNMjkxMDIwMDYyNjQyWjBT
MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2Fu
IEZyYW5jaXNjbzEXMBUGA1UEAxMOaW5zdXJhbmNlLXBlZXIwWTATBgcqhkjOPQIB
BggqhkjOPQMBBwNCAAR9Weg8lFSG6SdLYm78Vght0zHeGD3HkxkQ0lFkpL7cqNMF
Zjj9W+9iHI40Ww45Cj12EE5lts95s2fXtIRw5k/bo4GYMIGVMA4GA1UdDwEB/wQE
AwIFoDAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwDAYDVR0TAQH/BAIw
ADArBgNVHSMEJDAigCD+YRoM4vL2vWIlIp0YNf3HmJL5NxHyB2Cmv5GrEJ9VWTAp
BgNVHREEIjAggg5pbnN1cmFuY2UtcGVlcoIOaW5zdXJhbmNlLXBlZXIwCgYIKoZI
zj0EAwIDRwAwRAIgQBSXuMpnKYfxiSoH6KWtLEZYF6bGjju1vSE3O2BLWswCICUq
9A08+Aj+u1Kv+BpNpTsYMZ8FMQvv4ACoKKBD6PJ0
-----END CERTIFICATE-----

此證書由 CA 頒發,具有以下證書:

-----BEGIN CERTIFICATE-----
MIICPDCCAeOgAwIBAgIQYAbsZD8nQXqvSyVkNMYg/jAKBggqhkjOPQQDAjBwMQsw
CQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZy
YW5jaXNjbzEWMBQGA1UEChMNaW5zdXJhbmNlLW9yZzEcMBoGA1UEAxMTdGxzY2Eu
aW5zdXJhbmNlLW9yZzAeFw0xOTEwMjMwNjI2NDJaFw0yOTEwMjAwNjI2NDJaMHAx
CzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1TYW4g
RnJhbmNpc2NvMRYwFAYDVQQKEw1pbnN1cmFuY2Utb3JnMRwwGgYDVQQDExN0bHNj
YS5pbnN1cmFuY2Utb3JnMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEKkTdw/ia
IeHM/f4dzB0uGmhUCjo5uZxd7fpPHILRaWMwXh5BcxeNxNrwdBuvAJcrepiV+Pc+
eqv3RGF8u94j4aNfMF0wDgYDVR0PAQH/BAQDAgGmMA8GA1UdJQQIMAYGBFUdJQAw
DwYDVR0TAQH/BAUwAwEB/zApBgNVHQ4EIgQg/mEaDOLy9r1iJSKdGDX9x5iS+TcR
8gdgpr+RqxCfVVkwCgYIKoZIzj0EAwIDRwAwRAIgHR4M8sO20GazpTz9/MXDap4h
OVBMLCrP3M5fxLaykb0CIG8Vp1QszEvT1O5euZhsZoS5CcNLoFwbXT+ZuOlhUoV6
-----END CERTIFICATE-----

當我s_client主機上執行時:

$ openssl s_client -CAfile ./ca.crt -connect localhost:7051 -servername insurance-peer

它返回

SSL-Session:
   Protocol  : TLSv1.2
   Cipher    : ECDHE-ECDSA-AES256-GCM-SHA384
   Session-ID:
   Session-ID-ctx:
   Master-Key: 94B93D75B297584B53E87ADA0968F2B3B9121F1CAA048212642195E299F6BEA24E482E19E35F5FF18D596E138955EEBF
   PSK identity: None
   PSK identity hint: None
   SRP username: None
   Start Time: 1571897582
   Timeout   : 7200 (sec)
   Verify return code: 26 (unsupported certificate purpose)
   Extended master secret: no

注意返回碼是26 (unsupported certificate purpose)

當我s_clientdocker容器上執行時

$ openssl s_client -connect insurance-peer:7051 -CAfile ./ca.crt

它返回:

SSL-Session:
   Protocol  : TLSv1.2
   Cipher    : ECDHE-ECDSA-AES256-GCM-SHA384
   Session-ID:
   Session-ID-ctx:
   Master-Key:85B6CAD935415FFDCC76623E9A1B5FC1749D3B7B450B41316F581A181D29039CA092B55F25170ED8D125B7ACFBE33289
   Key-Arg   : None
   PSK identity: None
   PSK identity hint: None
   SRP username: None
   Start Time: 1571897694
   Timeout   : 300 (sec)
   Verify return code: 0 (ok)

注意這裡的返回碼是0 (ok)

因此,如果s_client主機和容器之間存在連接,則錯誤是unsupported certificate purpose在容器內連接的情況下,它可以正常工作。

$ openssl verify在兩種環境中都可以正常工作。

主持人:

$ openssl verify -CAfile ./ca.crt server.crt
server.crt: OK

碼頭工人:

# openssl verify -CAfile ./ca.crt server.crt
server.crt: OK

主機上的 OpenSSL 版本:OpenSSL 1.1.1d 10 Sep 2019

docker上的 OpenSSL 版本:OpenSSL 1.0.2g 1 Mar 2016

你能幫我這裡有什麼問題嗎?

看起來類似於此錯誤報告:Nginx #1760

您的 CA 密鑰報告“任何擴展密鑰使用”。從 OpenSSL 1.1.0 開始支持它。這就是不同版本行為不同的原因。我覺得要修復,您需要更新 OpenSSL。

引用自:https://serverfault.com/questions/989230