近日在开发项目时使用到wifi,3/4G通讯。 由于行业问题,当连接服务器时需要对服务器,客户端做双向认证。
在行业内,设备需要进行PCI认证,所有的通讯必须进行加密,连接服务器必须支持双向认证,以保证连接的服务器是安全的,保证客户端设备是授权的。
操作工具:OPENSSL 可自行下载安装。
客户端证书,以及客户端私钥。
以下过程我们以: www.baidu.com 为服务器,测试客户端。
C:\Users\Risten>openssl version
OpenSSL 1.1.1d 10 Sep 2019
openssl s_client -host www.baidu.com -port 443 -msg -state -showcerts -tls1_2
关于openssl指令的具体参数,可参考openssl s_client 博文。 或者使用指令: openssl s_client help 来查询。
---
Certificate chain
0 s:C = CN, ST = beijing, L = beijing, OU = service operation department, O = "Beijing Baidu Netcom Science Technology Co., Ltd", CN = baidu.com
i:C = BE, O = GlobalSign nv-sa, CN = GlobalSign Organization Validation CA - SHA256 - G2
-----BEGIN CERTIFICATE-----
MIIKLjCCCRagAwIBAgIMclh4Nm6fVugdQYhIMA0GCSqGSIb3DQEBCwUAMGYxCzAJ
BgNVBAYTAkJFMRkwFwYDVQQKExBHbG9iYWxTaWduIG52LXNhMTwwOgYDVQQDEzNH
bG9iYWxTaWduIE9yZ2FuaXphdGlvbiBWYWxpZGF0aW9uIENBIC0gU0hBMjU2IC0g
RzIwHhcNMjAwNDAyMDcwNDU4WhcNMjEwNzI2MDUzMTAyWjCBpzELMAkGA1UEBhMC
Q04xEDAOBgNVBAgTB2JlaWppbmcxEDAOBgNVBAcTB2JlaWppbmcxJTAjBgNVBAsT
HHNlcnZpY2Ugb3BlcmF0aW9uIGRlcGFydG1lbnQxOTA3BgNVBAoTMEJlaWppbmcg
QmFpZHUgTmV0Y29tIFNjaWVuY2UgVGVjaG5vbG9neSBDby4sIEx0ZDESMBAGA1UE
AxMJYmFpZHUuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwamw
rkca0lfrHRUfblyy5PgLINvqAN8p/6RriSZLnyMv7FewirhGQCp+vNxaRZdPrUEO
vCCG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XaPWiUqXRWYLaRST38KecoekKIof5fXmsgIhAMkZ
tF8XyKCu/nZll1e9vIlKbW8RrUr/74HpmScVRRsBAHYAb1N2rDHwMRnYmQCkURX/
dxUcEdkCwQApBo2yCJo32RMAAAFxObU85AAABAMARzBFAiBURWwwTgXZ+9IV3mhm
E0EOzbg901DLRszbLIpafDY/XgIhALsvEGqbBVrpGxhKoTVlz7+GWom8SrfUeHcn
4+9Dn7xGAHYA9lyUL9F3MCIUVBgIMJRWjuNNExkzv98MLyALzE7xZOMAAAFxObU8
qwAABAMARzBFAiBFBYPxKEdhlf6bqbwxQY7tskgdoFulPxPmdrzS5tNpPwIhAKnK
qwzch98lINQYzLAV52+C8GXZPXFZNfhfpM4tQ6xbMA0GCSqGSIb3DQEBCwUAA4IB
AQC83ALQ2d6MxeLZ/k3vutEiizRCWYSSMYLVCrxANdsGshNuyM8B8V/A57c0Nzqo
CPKfMtX5IICfv9P/bUecdtHL8cfx24MzN+U/GKcA4r3a/k8pRVeHeF9ThQ2zo1xj
k/7gJl75koztdqNfOeYiBTbFMnPQzVGqyMMfqKxbJrfZlGAIgYHT9bd6T985IVgz
tRVjAoy4IurZenTsWkG7PafJ4kAh6jQaSu1zYEbHljuZ5PXlkhPO9DwW1WIPug6Z
rlylLTTYmlW3WETOATi70HYsZN6NACuZ4t1hEO3AsF7lqjdA2HwTN10FX2HuaUvf
5OzP+PKupV9VKw8x8mQKU6vr
-----END CERTIFICATE-----
1 s:C = BE, O = GlobalSign nv-sa, CN = GlobalSign Organization Validation CA - SHA256 - G2
i:C = BE, O = GlobalSign nv-sa, OU = Root CA, CN = GlobalSign Root CA
-----BEGIN CERTIFICATE-----
MIIEaTCCA1GgAwIBAgILBAAAAAABRE7wQkcwDQYJKoZIhvcNAQELBQAwVzELMAkG
A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv
b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm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K3w4s
32R1juUYcqeMOovJrKV3UPfvnqTgoI8UV6MqX+x+bRDmuo2wCId2Dkyy2VG7EQLy
XN0cvfNVlg/UBsD84iOKJHDTu/B5GqdhcIOKrwbFINihY9Bsrk8y1658GEV1BSl3
30JAZGSGvip2CTFvHST0mdCF/vIhCPnG9vHQWe3WVjwIKANnuvD58ZAWR65n5ryA
SOlCdjSXVWkkDoPWoC209fN5ikkodBpBocLTJIg1MGCUF7ThBCIxPTsvFwayuJ2G
K1pp74P1S8SqtCr4fKGxhZSM9AyHDPSsQPhZSZg=
-----END CERTIFICATE-----
---
Server certificate
subject=C = CN, ST = beijing, L = beijing, OU = service operation department, O = "Beijing Baidu Netcom Science Technology Co., Ltd", CN = baidu.com
issuer=C = BE, O = GlobalSign nv-sa, CN = GlobalSign Organization Validation CA - SHA256 - G2
---
从证书验证结果看,百度的证书是由GOV签发的,证书使用的域名是:baidu.com。
验证签名时,发现百度证书由 GlobalSign Organization Validation 签发,验证时必须使用跟证书进行验证,以上过程中可以看到验证时使用了 root ca证书。
从百度网页上,也可以看到证书的具体内容,如下图片。
由于我们在连接百度时,没有添加客户端证书,所以会在操作中有以下显示
---
No client certificate CA names sent
Peer signing digest: SHA256
Peer signature type: RSA
Server Temp Key: ECDH, P-256, 256 bits
---
SSL handshake has read 4392 bytes and written 342 bytes
Verification error: unable to get local issuer certificate
---
服务器没有收到客户端证书信息,验证错误,无法确认客户端认证情况。
虽然没有发送客户端证书,但是百度是允许在未验证客户端的情况下访问,以下是SSL连接信息
---
New, TLSv1.2, Cipher is ECDHE-RSA-AES128-GCM-SHA256
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
Protocol : TLSv1.2
Cipher : ECDHE-RSA-AES128-GCM-SHA256
Session-ID: C5D0FBFA580B18A9FC0641DAB9E23DA989D453E41551E5C9F4059287656E4FBD
Session-ID-ctx:
Master-Key: 8904C0BD0B950BF5EA8DF39498DD6A64E93991629FD1899B679441988ECBFD8F5BAEBE183C60A61CFBB577F0CC3111A5
PSK identity: None
PSK identity hint: None
SRP username: None
TLS session ticket:
0000 - 02 88 8d 30 01 b3 19 af-6a ce 20 77 f3 15 9d 55 ...0....j. w...U
0010 - 1c b9 6d 41 71 d3 8a b9-08 b0 86 46 f2 7d 7a a7 ..mAq......F.}z.
0020 - 7b 11 79 4d e5 fd cd c5-fe 3c 4e da 09 e2 c4 17 {.yM.....
从信息中可以看到以下信息
服务器公钥: RSA 2048bit
协议: TLS1.2
加密方式:ECDHE-RSA-AES128-GCM-SHA256
从此之后,所有的通讯都是使用 ECDHE-RSA-AES128-GCM-SHA256 加密方式来解密通讯数据。通讯密钥,是在连接验证时随机生成并同步给对方的。
关于SSL的认证到此就完成了,但是是否可以通讯我们需要进一步验证,从各网站都没有找到如何发送指令来进一步操作,以下是实操。
在OPSSL指令操作完之后,发现连接既不断开又不继续运行,此时等待发送指令。
GET / HTTP/1.0
发送指令后,会返回网站信息,通讯成功
GET / HTTP/1.0
HTTP/1.0 200 OK
Accept-Ranges: bytes
Cache-Control: no-cache
Content-Length: 14722
Content-Type: text/html
Date: Fri, 06 Nov 2020 07:07:33 GMT
P3p: CP=" OTI DSP COR IVA OUR IND COM "
P3p: CP=" OTI DSP COR IVA OUR IND COM "
Pragma: no-cache
Server: BWS/1.1
Set-Cookie: BAIDUID=C70A43ABE85FED0370841E5D9BB26F8F:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=C70A43ABE85FED0370841E5D9BB26F8F; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1604646453; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BAIDUID=C70A43ABE85FED033330FEA77590E11B:FG=1; max-age=31536000; expires=Sat, 06-Nov-21 07:07:33 GMT; domain=.baidu.com; path=/; version=1; comment=bd
Traceid: 160464645324231866987921237759746760345
Vary: Accept-Encoding
X-Ua-Compatible: IE=Edge,chrome=1
鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾
..............以下省略
closed
返回网站信息后,下面会出现closed,代表通讯完成,连接关闭。下次通讯的时候,重新连接(涉及到长连接和短链接,此处不讨论)。
至此,操作完成