局域網(wǎng)自簽名https證書(shū)
1、證書(shū)生成
生成密鑰,采用的是des3算法
openssl genrsa -des3 -out server.key 2048
根據(jù)密鑰簽發(fā)申請(qǐng)文件CSR
openssl req -new -key server.key -out server.csr
#這一步主要是去除密鑰的密碼,便于后續(xù)部署
openssl rsa -in server.key -out server.key
#生成自簽名根證書(shū)并設(shè)置證書(shū)的有效期,crt是發(fā)給客戶端的證書(shū)
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
#有的需要pem的證書(shū),對(duì)證書(shū)做的轉(zhuǎn)換
openssl x509 -in server.crt -out server.pem -outform PEM
2、配置Nginx
server {
listen 8080 ssl; #端口增加SSL支持
server_name _;
charset utf-8;
# ssl on;
ssl_certificate /data/app/nginx/nginx/cert/server.crt;# 證書(shū)文件
ssl_certificate_key /data/app/nginx/nginx/cert/server.key; #私鑰文件
# access_log ./logs/access_stream.log main;
# error_log ./logs/error_stream.log;
location / {
root /home/nginx/knowledge2.0/;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
3、瀏覽器訪問(wèn)
使用https協(xié)議訪問(wèn)8080端口,但是會(huì)提示證書(shū)不可信,后面我們會(huì)詳細(xì)講解怎樣解決這個(gè)問(wèn)題。
對(duì)于瀏覽器提示證書(shū)不可信,由于采用的自簽名證書(shū),瀏覽器是沒(méi)有我們的自簽名證書(shū)導(dǎo)致
4、證書(shū)不可信解決方案
生成證書(shū)時(shí)候增加ext.ini
extendedKeyUsage = serverAuth, clientAuth basicConstraints = CA:FALSE keyUsage = nonRepudiation,digitalSignature,keyEncipherment subjectAltName = @alt_names [alt_names] IP.1=172.1.0.1 DNS.1=www.test.com
然后重新使用Openssl重新生成證書(shū),上述配置說(shuō)明該證書(shū)是IP或者域名所有的:
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt -extfile ext.ini
生成證書(shū),然后導(dǎo)出crt證書(shū),在windows下加入到信任的根證書(shū)下面,再次訪問(wèn)瀏覽器,瀏覽器訪問(wèn)不會(huì)報(bào)錯(cuò)
二、基于域名的https
證書(shū)相關(guān)文件格式說(shuō)明
.key: 私鑰文件
.pem: 根證書(shū)公鑰
.crt: 域名證書(shū)公鑰
自簽名文件的生成過(guò)程
這里用的工具是openssl,怎么安裝可以自行查一下
普通域名的自簽名
我們以域名 td.todocoder.com為例
創(chuàng)建證書(shū)目錄:/root/cert,進(jìn)入/root/cert 創(chuàng)建 rootCA.key $ openssl genrsa -des3 -out rootCA.key 2048 使用生成的密鑰(rootCA.key)來(lái)創(chuàng)建新的根SSL證書(shū)。并將其保存為rootCA.pem,證書(shū)有效期為10年 $ openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 3650 -out rootCA.pem
輸入第一步設(shè)置的密碼
Enter pass phrase for rootCA.key: Country Name (2 letter code) [AU]:CN State or Province Name (full name) [Some-State]:Zhengzhou Locality Name (eg, city) []:Zhengzhou Organization Name (eg, company) [Internet Widgits Pty Ltd]:todocoderCA Organizational Unit Name (eg, section) []:todocoderCA Common Name (e.g. server FQDN or YOUR name) []:RootTodoCoder Cert Email Address []:todocoder@github.com
這一行是把pem 轉(zhuǎn)換成 crt格式
$ openssl x509 -outform der -in rootCA.pem -out rootCA.crt 提示填寫的字段大多都可以直接回車過(guò)就行了,只要Common Name字段需要填寫內(nèi)容,這是生成跟證書(shū)后導(dǎo)入到系統(tǒng)的證書(shū)名稱,我填的是RootTodoCoder Cert 創(chuàng)建生成域名ssl證書(shū)的前置文件 在這里我生成一個(gè) td.todocoder.com 的域名證書(shū)。創(chuàng)建一個(gè)td.ext文件,以創(chuàng)建一個(gè)X509 v3證書(shū)。注意我們指定了subjectAltName選項(xiàng)。 cat >> td.ext <<EOF authorityKeyIdentifier=keyid,issuer basicConstraints=CA:FALSE keyUsage=digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment subjectAltName=@alt_names [alt_names] DNS.1 = td.todocoder.com EOF 這里的 [alt_names] 域中即為我們需要指定的 subjectAltName,可以配置多個(gè) IP,DNS 或其他值。 生成域名ssl證書(shū)秘鑰(tdtodocoder.csr、tdtodocoder.key) $ openssl req -new -sha256 -nodes -out tdtodocoder.csr -newkey rsa:2048 -keyout tdtodocoder.key Country Name (2 letter code) [AU]:CN State or Province Name (full name) [Some-State]:Zhengzhou Locality Name (eg, city) []:Zhengzhou Organization Name (eg, company) [Internet Widgits Pty Ltd]:todocoder Organizational Unit Name (eg, section) []:todocoder Common Name (e.g. server FQDN or YOUR name) []:td.todocoder.com Email Address []:todocoder@github.com A challenge password []:rootCA An optional company name []:todocoder
這一步填寫域名證書(shū)密鑰需要的信息,會(huì)展示在瀏覽器的域名證書(shū)信息,其中最重要的是Common Name的內(nèi)容,必須是對(duì)應(yīng)需要https訪問(wèn)的域名的。
通過(guò)我們之前創(chuàng)建的根SSL證書(shū)頒發(fā),創(chuàng)建出一個(gè) test.testcloudide.com 的域名證書(shū)。輸出是一個(gè)名為的證書(shū)文件ttestcloudide.crt(對(duì)應(yīng)位置替換為自己的文件路徑)
$ openssl x509 -req -in tdtodocoder.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out tdtodocoder.crt -days 500 -sha256 -extfile td.ext 以上,我們把域名 td.todocoder.com的證書(shū)已經(jīng)創(chuàng)建好了,文件目錄如下: . ├── rootCA.crt ├── rootCA.key ├── rootCA.pem ├── td.ext ├── tdtodocoder.crt ├── tdtodocoder.csr └── tdtodocoder.key
接下來(lái)我們看一下,在nginx 和k8s中是如何用的。
nginx 配置域名td.todocoder.com的證書(shū)
我們拿到上面幾步生成的 td.todocoder.com域名的文件,tdtodocoder.key、tdtodocoder.crt
配置到nginx的配置中
server {
listen 443 ssl;
# 以td.todocoder.com域名訪問(wèn)
server_name td.todocoder.com;
# 配置ssl域名證書(shū)
ssl_certificate [ssl證書(shū)路徑]/tdtodocoder.crt;
ssl_certificate_key [ssl證書(shū)路徑]/tdtodocoder.key;
index index.html index.php;
# 反向代理需要被訪問(wèn)的本地服務(wù)
location / {
proxy_pass http://localhost:8080;
}
...
}
自簽名的域名訪問(wèn)需要配置host
vi /etc/hosts
192.168.111.37 td.todocoder.com
然后直接訪問(wèn) td.todocoder.com 在瀏覽器上應(yīng)該可以看到證書(shū)的信息,注意此時(shí)瀏覽器還提示的是證書(shū)無(wú)效,不安全。后面需要在操作系統(tǒng)信任一下證書(shū)就可以了。
如果你用的是k8s ,可以在ingress中配置
ingress配置tls證書(shū)
創(chuàng)建secret
$ kubectl -n todocoder create secret tls tdtodocoder-certs --key tdtodocoder.key --cert tdtodocoder.crt
# 重啟ingress controller
$ kubectl rollout restart daemonset.apps/nginx-ingress-controller -n ingress-nginx
配置ingress yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-todocoder
namespace: todocoder
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "true"
spec:
tls:
- hosts:
- "td.todocoder.com"
secretName: tdtodocoder-certs
ingressClassName: nginx
rules:
- host: "td.todocoder.com"
http:
paths:
- backend:
service:
name: todocoder-front
port:
number: 8080
path: /?(.*)
pathType: ImplementationSpecific
以上,我們把域名 td.todocoder.com的證書(shū)已經(jīng)創(chuàng)建好了,如果不創(chuàng)建泛域名的證書(shū)可以直接跳過(guò)泛域名的創(chuàng)建過(guò)程。接下來(lái)我們看一下泛域名證書(shū)的簽名流程
泛域名的自簽名
泛域名是域名前面的字符不是固定的,可能會(huì)根據(jù)id生成,比如,我的 id是:fsd423dsf234dsfs, 我自己的泛域名就是 fsd423dsf234dsfs.todocoder.com ,每個(gè)用戶都可以有不同的域名。那么我們只需要配置 *.todocoder.com 這個(gè)域名就可以,具體操作流程如下:
從上面第3步開(kāi)始,我們需要用同一個(gè)根證書(shū)簽名,所以rootCA 的證書(shū)就不用再新建了
創(chuàng)建生成域名ssl證書(shū)的前置文件
創(chuàng)建一個(gè)fanym.ext文件, 注意我們指定了subjectAltName選項(xiàng)。
cat >> fanym.ext <<EOF authorityKeyIdentifier=keyid,issuer basicConstraints=CA:FALSE keyUsage=digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment subjectAltName=@alt_names [alt_names] DNS.1 = *.todocoder.com EOF
注意:這里的 域名配置的 *.todocoder.com
生成域名ssl證書(shū)秘鑰(fantodocoder.csr、fantodocoder.key)
$ openssl req -new -sha256 -nodes -out fantodocoder.csr -newkey rsa:2048 -keyout fantodocoder.key Country Name (2 letter code) [AU]:CN State or Province Name (full name) [Some-State]:Zhengzhou Locality Name (eg, city) []:Zhengzhou Organization Name (eg, company) [Internet Widgits Pty Ltd]:fantodocoder Organizational Unit Name (eg, section) []:fantodocoder Common Name (e.g. server FQDN or YOUR name) []:*.todocoder.com Email Address []:todocoder@github.com ... A challenge password []:rootCA An optional company name []:fantodocoder
這一步填寫域名證書(shū)密鑰需要的信息,會(huì)展示在瀏覽器的域名證書(shū)信息,其中最重要的是Common Name的內(nèi)容,必須是對(duì)應(yīng)需要https訪問(wèn)的域名的。
通過(guò)我們之前創(chuàng)建的根SSL證書(shū)rootCA.pem, rootCA.key頒發(fā),創(chuàng)建出一個(gè) *.todocoder.com 的域名證書(shū)。輸出是一個(gè)名為的證書(shū)文件fantodocoder.crt(對(duì)應(yīng)位置替換為自己的文件路徑)
$ openssl x509 -req -in fantodocoder.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out fantodocoder.crt -days 500 -sha256 -extfile fanym.ext
Certificate request self-signature ok
subject=C = CN, ST = Zhengzhou, L = Zhengzhou, O = fantodocoder, OU = fantodocoder, CN = *.todocoder.com, emailAddress = todocoder@github.com
Enter pass phrase for rootCA.key:
當(dāng)前的目錄結(jié)構(gòu):
.
├── fantodocoder.crt
├── fantodocoder.csr
├── fantodocoder.key
├── fanym.ext
├── rootCA.crt
├── rootCA.key
├── rootCA.pem
├── td.ext
├── tdtodocoder.crt
├── tdtodocoder.csr
└── tdtodocoder.key
Nginx 配置
server {
listen 443 ssl;
server_name *.todocoder.com;
# 配置ssl域名證書(shū)
ssl_certificate [ssl證書(shū)路徑]/fantodocoder.crt;
ssl_certificate_key [ssl證書(shū)路徑]/fantodocoder.key;
index index.html index.php;
# 反向代理需要被訪問(wèn)的本地服務(wù)
location / {
proxy_pass http://localhost:8080;
}
...
}
我們用泛域名 fsd423dsf234dsfs.todocoder.com訪問(wèn),訪問(wèn)之前需要配置hosts
vi /etc/hosts
192.168.111.37 fsd423dsf234dsfs.todocoder.com
訪問(wèn) fsd423dsf234dsfs.todocoder.com 可以看到我們剛剛的泛域名證書(shū)信息。
截止到這一步,我們的域名在nginx或者k8s端已經(jīng)配置完成,我們想要的效果是在https 訪問(wèn)的時(shí)候,不報(bào)錯(cuò),并且提示證書(shū)安全有效,那么需要我們?cè)谖覀兊牟僮飨到y(tǒng)上配置信任證書(shū)
各個(gè)系統(tǒng)下證書(shū)的信任流程
mac 下證書(shū)的添加方式
mac系統(tǒng)只需要添加 域名證書(shū)就可以,不用添加rootCA 證書(shū)
在mac下我們拿到證書(shū)后,雙擊tdtodocoder.crt文件->打開(kāi)鑰匙串->選擇信任
然后重啟瀏覽器再訪問(wèn) td.todocoder.com,可以看到顯示https 安全了
Windows下證書(shū)的添加方式
Windows 需要添加 rootCA 和域名的證書(shū)到系統(tǒng)里面
如果要訪問(wèn) td.todocodoer.com 需要安裝 rootCA.crt、tdtodocoder.crt 這兩個(gè)證書(shū)
雙擊CA根證書(shū)->存儲(chǔ)位置選擇本地->指定證書(shū)位置(受信任的根證書(shū)頒發(fā)機(jī)構(gòu))
ubuntu下證書(shū)的添加方式
我的系統(tǒng)是Ubuntu 桌面版 22.04 的版本, ubuntu需要添加 rootCA 和域名的證書(shū)到系統(tǒng)里面
拷貝證書(shū)到ca證書(shū)的目錄 $ sudo cp tdtodocoder.crt /usr/share/ca-certificates/mozilla/tdtodocoder.crt $ sudo cp fantodocoder.crt /usr/share/ca-certificates/mozilla/fantodocoder.crt $ sudo cp rootCA.crt /usr/share/ca-certifi cates/mozilla/rootCA.crt 執(zhí)行下面的命令按提示選中新添加的證書(shū),確定(點(diǎn)回車) ,選中(點(diǎn)空格鍵)然后選 ”O(jiān)K” 就行了 $ sudo dpkg-reconfigure ca-certificates Updating certificates in /etc/ssl/certs... rehash: warning: skipping ca-certificates.crt,it does not contain exactly one certificate or CRL rehash: warning: skipping rootCA.pem,it does not contain exactly one certificate or CRL 1 added, 0 removed; done. 正在處理用于 ca-certificates (20230311ubuntu0.22.04.1) 的觸發(fā)器 ... Updating certificates in /etc/ssl/certs... 0 added, 0 removed; done. Running hooks in /etc/ca-certificates/update.d... done.
點(diǎn)回車,點(diǎn)空格選中 -> 回車確定
就可以了.
火狐瀏覽器證書(shū)的添加方式
Chrome,safari,Edge等瀏覽器直接就可以訪問(wèn)了,但是火狐瀏覽器(ubuntu桌面版火狐,其他系統(tǒng)的沒(méi)試)的話需要在設(shè)置里面添加跟證書(shū)。具體操作:
點(diǎn)擊設(shè)置-> 搜索證書(shū),點(diǎn)擊查看證書(shū) -> 證書(shū)頒發(fā)機(jī)構(gòu) -> 導(dǎo)入 -> rootCA.crt -> 編輯信任
可以看到,火狐瀏覽器已經(jīng)顯示安全
參考
https://mp.weixin.qq.com/s/Qi2pDCBJYGymLJxBIvqvqg
浙公網(wǎng)安備 33010602011771號(hào)