自定義根證書(shū)頒發(fā)機(jī)構(gòu) CA 生成自簽名證書(shū)
本文為使用過(guò)程中的一個(gè)工具記錄,可實(shí)現(xiàn)在本地開(kāi)啟一個(gè) HTTPS 服務(wù)器用于開(kāi)發(fā)或測(cè)試。我們會(huì)先創(chuàng)建一個(gè) CA 根證書(shū),再創(chuàng)建一個(gè)由 CA 根證書(shū)簽名的自定義證書(shū)。
本文從以下幾個(gè)方面講解:
- 創(chuàng)建自己的自定義證書(shū)頒發(fā)機(jī)構(gòu) CA
- 使用 CA 根證書(shū)簽名服務(wù)器證書(shū)
- 在 Node.js 服務(wù)器中配置證書(shū)
- 添加根證書(shū)到本地計(jì)算機(jī)的受信任根存儲(chǔ)中
創(chuàng)建自己的自定義證書(shū)頒發(fā)機(jī)構(gòu) CA
- 生成私鑰
- $ openssl ecparam -out ca.key -name prime256v1 -genkey
- 生成證書(shū)請(qǐng)求文件
- $ openssl req -new -sha256 -key ca.key -out ca.csr
- # 以下為需要輸入的交互信息
- Country Name (2 letter code) []:CN
- State or Province Name (full name) []:BeiJing
- Locality Name (eg, city) []:BeiJing
- Organization Name (eg, company) []:Node.js
- Organizational Unit Name (eg, section) []:Node.js
- Common Name (eg, fully qualified host name) []:test.ca.com
- Email Address []:
- Please enter the following 'extra' attributes
- to be sent with your certificate request
- A challenge password []:abc123***
- 生成根證書(shū)
- $ openssl x509 -req -sha256 -days 365 -in ca.csr -signkey ca.key -out ca.crt
使用 CA 根證書(shū)簽名服務(wù)器證書(shū)
- 生成私鑰
- $ openssl ecparam -out server.key -name prime256v1 -genkey
- 生成證書(shū)請(qǐng)求文件
- $ openssl req -new -sha256 -key server.key -out server.csr
- # 注意下面服務(wù)器證書(shū)的 Common Name 不能與上面頒發(fā)者 CA 的 Common Name 一樣
- Country Name (2 letter code) []:CN
- State or Province Name (full name) []:ShangHai
- Locality Name (eg, city) []:ShangHai
- Organization Name (eg, company) []:Node.js
- Organizational Unit Name (eg, section) []:Node.js
- Common Name (eg, fully qualified host name) []:test.https.com
- Email Address []:
- Please enter the following 'extra' attributes
- to be sent with your certificate request
- A challenge password []:abc123***
- 使用 CA 的根證書(shū)為服務(wù)器證書(shū)簽名
- $ openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365 -sha256
- # 成功之后有以下提示
- Signature ok
- subject=/C=CN/ST=ShangHai/L=ShangHai/O=Node.js/OU=Node.js/CN=test.https.com
- Getting CA Private Key
服務(wù)端證書(shū)中使用到的域名是我們自己定義的,需要在本地 hosts 文件做映射,如果不知道為什么要修改和該如何修改的參考文章 DNS 域名解析過(guò)程?github.com/qufei1993/http-protocol/blob/master/docs/dns-process.md
- 證書(shū)文件列表
完成之后可以看到如下文件,server.crt 是服務(wù)器的證書(shū)文件,ca.crt 就是我們創(chuàng)建的根正書(shū)。
在 Node.js 服務(wù)器中配置證書(shū)
- const express = require('express');
- const https = require('https');
- const fs = require('fs');
- const app = express();
- const PORT = 8443;
- const options = {
- key: fs.readFileSync('./cert/server.key'),
- cert: fs.readFileSync('./cert/server.crt')
- };
- https.createServer(options, app)
- .listen(PORT, () => console.log(`App listening on port ${PORT}!`));
- app.get('/', (req, res) => res.send('Hello World!'));
此時(shí)在 Chrome 瀏覽器中仍無(wú)法訪問(wèn),至少在 Chrome 85.0.4183.121 是這樣的,瀏覽器中打開(kāi)證書(shū)文件也顯示的證書(shū)是不受信任的。
為了解決這個(gè)問(wèn)題,繼續(xù)往下看。
添加根證書(shū)到本地計(jì)算機(jī)的受信任根存儲(chǔ)中
找到我們剛生成的根證書(shū)文件,雙擊打開(kāi)。
得到如下提示,是因?yàn)橄到y(tǒng)提示新根證書(shū)應(yīng)添加到當(dāng)前用戶(hù)下,這樣就不會(huì)因?yàn)闇y(cè)試去影響其它用戶(hù),系統(tǒng)根證書(shū)是不建議修改的,這會(huì)對(duì)當(dāng)前計(jì)算的所有用戶(hù)生效,另外 Mac 中也是不能修改的。
image.png
按照以下步驟添加根證書(shū),修改證書(shū)為信任,最后會(huì)需要用到密碼進(jìn)行確認(rèn)
重新打開(kāi)鏈接,是有提示的,我們可以繼續(xù)前往訪問(wèn),另外證書(shū)的狀態(tài)也顯示為了有效。
Reference
- support.apple.com/zh-cn/guide/keychain-access/kyca2431/mac
- configure-the-certificate-in-your-web-servers-tls-settings
本文轉(zhuǎn)載自微信公眾號(hào)「Nodejs技術(shù)棧」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系Nodejs技術(shù)棧公眾號(hào)。