node-http2-openssl 驗證服務器證書過程

nodehttp2的example內有http/2服務器(server.js)和客戶端(client.js)的的代碼案例。其中的server.js 提供了一個簡單的http/2服務器代碼模板。node

http/2的一個特色,就是流量走向了TLS,也就是走了加密信道。http 1.x都是明文的,文本化的,機器可讀人也可讀,調試和差錯都比較容易,編碼也不難。雖然你們一再說爲了性能的提高,http2的性能也確實有很大的提高,而後走向TLS終究讓不少人感到不快。git

正如 微博網友 @Livid 說:golang

愈來愈多的網絡流量 TLS 化實際上是一件挺悲哀的事情。就像現實生活中,若是愈來愈多的地方的入口
(機場,地鐵,甚至辦公樓)須要安檢,那其實並非一件讓人愉快的事情。
1. 最先的一切互聯網協議,都是由一羣很是善良的人設計的。這些協議在一個完美的環境裏能夠完美工做。
2. 可是這個世界是不完美的。很是不完美。因而,從某個時間點開始,進入到某種攻防遊戲中。
全網流量的 TLS 化是第一步。

理論上說,儘管http/2 能夠在plain TCP上跑,可是不管chrome,仍是firefox都僅僅選擇實現了TLS的。拋開這些朦朧的傷感,TLS也是必然的現實,不如咱們去面對它。chrome

到底TLS過程是怎樣的?我想用openssl,從外部來看node-http2這個黑盒。瀏覽器

證書

TLS須要證書,這是一個顯著的和前些版本的差異。正規的證書,須要第三方發佈,而且是比較權威的第三方,期間的過程也須要有些紙質的證實文件的往來。固然也得繳費,且極可能價值不菲。這個東西是用來證實服務器身份的,在那個地方,是誰的等等。固然不能你本身所說就算的。服務器

還好,CA們仍是網開一面。若是隻是測試,咱們能夠作自簽名證書。特色就是證書內的Issuer(簽署人),subject(被證實人)都是一我的。典型的作法,就是使用openssl來作(剛剛發現Windows7自帶openssl:)。網絡

$ openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 101 -nodes
Generating a 2048 bit RSA private key
...........+++
......+++
writing new private key to 'key.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN // 國家碼
State or Province Name (full name) [Some-State]:SC//地區碼
Locality Name (eg, city) []:localcity// 本地碼
Organization Name (eg, company) [Internet Widgits Pty Ltd]:sanrex // 公司名稱
Organizational Unit Name (eg, section) []:trd // 部門
Common Name (eg, YOUR name) []:samwise// 名字
Email Address []:frodo@gmail.com //郵箱

服務器端

$node example\server.js

客戶端

$openssl s_client -connect localhost:8080

客戶端輸出


λ openssl s_client -connect localhost:8080 CONNECTED(00000003) depth=0 /C=CN/ST=SC/L=localcity/O=sanrex/OU=trd/CN=samwise/emailAddress=frodo@gmail.com verify error:num=18:self signed certificate// 這裏也指明瞭是自簽名證書,是不被廣大瀏覽器信任的 verify return:1 depth=0 /C=CN/ST=SC/L=localcity/O=sanrex/OU=trd/CN=samwise/emailAddress=frodo@gmail.com verify return:1 --- Certificate chain // 自簽名,固然s(subject),i(issuer)是同樣的 0 s:/C=CN/ST=SC/L=localcity/O=sanrex/OU=trd/CN=samwise/emailAddress=frodo@gmail.com i:/C=CN/ST=SC/L=localcity/O=sanrex/OU=trd/CN=samwise/emailAddress=frodo@gmail.com --- Server certificate// 這個證書,和我服務器生成的證書是同樣的 -----BEGIN CERTIFICATE----- MIIEejCCA2KgAwIBAgIJAMOercExD57pMA0GCSqGSIb3DQEBBQUAMIGEMQswCQYD VQQGEwJDTjELMAkGA1UECBMCU0MxEDAOBgNVBAcTB2NoZW5nZHUxDzANBgNVBAoT BnNhbnJleDEMMAoGA1UECxMDdHJkMRQwEgYDVQQDEwtsaXVjaHVhbmp1bjEhMB8G CSqGSIb3DQEJARYSMTAwMGNvcHlAZ21haWwuY29tMB4XDTE1MDQyMTAyMDQwN1oX DTE1MDczMTAyMDQwN1owgYQxCzAJBgNVBAYTAkNOMQswCQYDVQQIEwJTQzEQMA4G A1UEBxMHY2hlbmdkdTEPMA0GA1UEChMGc2FucmV4MQwwCgYDVQQLEwN0cmQxFDAS BgNVBAMTC2xpdWNodWFuanVuMSEwHwYJK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KcEIqMaUSQNwX686rLp2NVnvMeg3t8F7JlHamfd2YQ== -----END CERTIFICATE----- subject=/C=CN/ST=SC/L=localcity/O=sanrex/OU=trd/CN=samwise/emailAddress=frodo@gmail.com issuer=/C=CN/ST=SC/L=localcity/O=sanrex/OU=trd/CN=samwise/emailAddress=frodo@gmail.com --- No client certificate CA names sent --- SSL handshake has read 1312 bytes and written 444 bytes --- New, TLSv1/SSLv3, Cipher is AES128-SHA Server public key is 2048 bit Compression: NONE Expansion: NONE SSL-Session: Protocol : TLSv1 Cipher : AES128-SHA Session-ID: 53D09139D2AF68CF254772FBA3C3133944C2F11CD451360E1C03ED54EDF5BAEA Session-ID-ctx: Master-Key: C19B36148B180EB26487873D575B73A0C4C763C1E07BD5571E9F35A7C5520C44AF423A5AEE4DE6F27D74C44CAC9D084C Key-Arg : None Start Time: 1429581989 Timeout : 300 (sec) Verify return code: 18 (self signed certificate) ---

以上過程,說明server.js是支持TLS的,也能夠作了證書的傳遞和TLS handshake的。而不是隻能和本身的client.js 玩的,這是一個開放的玩伴。性能

相關文章
相關標籤/搜索