自建HTTPS证书
文章目錄
- 1. 背景
- 2. 創(chuàng)建自建證書
- 2.1 步驟
- 2.1.1 生成私鑰
- 2.1.2 創(chuàng)建 openSSL.conf 配置文件
- 2.1.3 生成 server 證書簽名請求
- 2.1.3.1 server
- 2.1.3.2 client
- 2.1.4 生成 ca 證書
- 2.1.5 使用 ca 簽發(fā)證書
- 2.1.5.1 創(chuàng)建 extension 文件
- 2.1.5.2 簽發(fā) server 證書
- 2.1.5.3 簽發(fā) client 證書
- 2.1.6 將 ca 證書打包為 p12 格式
- 2.1.7 對 p12 格式的證書進(jìn)行 BASE64 編碼
- 2.1.7.1 macOS
- 2.1.7.2 nodejs
- 3. 信任證書
- 3.1 chrome
- 3.2 macOS
- 3.3 windows
- 3.3.1 GUI
- 3.3.2 命令行
1. 背景
在做 Web 相關(guān)開發(fā)的時候,有可能需要在本地搭建 https 的環(huán)境,而在 https 環(huán)境的過程中,需要私鑰和證書文件,本文提供自建證書的方案供讀者參考。
2. 創(chuàng)建自建證書
2.1 步驟
一般情況下,只需執(zhí)行步驟1 ~ 5即可滿足需求。
2.1.1 生成私鑰
openssl genrsa -out key.pem 2048
2.1.2 創(chuàng)建 openSSL.conf 配置文件
openSSL.conf
[ req ] distinguished_name = req_distinguished_name attributes = req_attributes x509_extensions = v3_ca string_mask = utf8only[ req_attributes ] challengePassword = A challenge password challengePassword_min = 4 challengePassword_max = 20 unstructuredName = An optional company name[ v3_ca ] subjectKeyIdentifier=hash authorityKeyIdentifier=keyid:always,issuer basicConstraints = critical,CA:true[ req_distinguished_name ] countryName = Country Name (2 letter code) countryName_default = CN countryName_min = 2 countryName_max = 2stateOrProvinceName = State or Province Name (full name) stateOrProvinceName_default = Demo ProvincelocalityName = Locality Name (eg, city) localityName_default = Demo City0.organizationName = Organization Name (eg, company) 0.organizationName_default = Demo CompanyorganizationalUnitName = Organizational Unit Name (eg, section) organizationalUnitName_default = ITcommonName = Common Name (e.g. server FQDN or YOUR name) commonName_default = Demo commonName_max = 64emailAddress = Email Address emailAddress_default = demo@mail.com emailAddress_max = 642.1.3 生成 server 證書簽名請求
2.1.3.1 server
openssl req -new -key key.pem -out server.csr -config openSSL.conf
2.1.3.2 client
openssl req -new -key key.pem -out client.csr -config openSSL.conf
2.1.4 生成 ca 證書
openssl req -x509 -config openSSL.conf -new -nodes -key key.pem -days 36500 -out cacert.crt
2.1.5 使用 ca 簽發(fā)證書
2.1.5.1 創(chuàng)建 extension 文件
https.ext
keyUsage = nonRepudiation, digitalSignature, keyEncipherment extendedKeyUsage = serverAuth, clientAuth subjectAltName = @SubjectAlternativeName[ SubjectAlternativeName ] DNS.1 = *.test.local IP.1 = 127.0.0.12.1.5.2 簽發(fā) server 證書
openssl x509 -req -in server.csr -CA cacert.crt -CAkey key.pem -CAcreateserial -out server.crt -days 36500 -sha256 -extfile https.ext
2.1.5.3 簽發(fā) client 證書
openssl x509 -req -in client.csr -CA cacert.crt -CAkey key.pem -CAcreateserial -out client.crt -days 36500 -sha256
2.1.6 將 ca 證書打包為 p12 格式
openssl pkcs12 -export -clcerts -in cacert.crt -inkey key.pem -out cacert.p12 -password pass:8BD0E8EA
2.1.7 對 p12 格式的證書進(jìn)行 BASE64 編碼
2.1.7.1 macOS
base64 cacert.p12 > cacert-p12.base64
2.1.7.2 nodejs
用 nodejs 簡單寫了個轉(zhuǎn) base64 模塊。
const fs = require('fs') const path = require('path') const p12Path = path.join(__dirname, 'cacert.p12') const p12Base64Path = path.join(__dirname, 'cacert-p12.base64')!(async () => {const fileBuffer = fs.readFileSync(p12Path)const p12Base64 = fileBuffer.toString('base64')fs.writeFileSync(p12Base64Path, p12Base64)console.log(`p12Base64Path: ${p12Base64Path}`) })()3. 信任證書
3.1 chrome
3.2 macOS
3.3 windows
3.3.1 GUI
3.3.2 命令行
- certutil -addstore "Root" "D:\workspace\frontend\my-desktop-tools\electron\proxy-server\ssl\cacert.crt"
- certutil -addstore "Root" "D:\workspace\frontend\my-desktop-tools\electron\proxy-server\ssl\server.crt"
總結(jié)
- 上一篇: oracle计算6的阶乘,oracle
- 下一篇: 基于单片机火灾监测报警系统设计-毕设资料