1栏尚,通過openssl創(chuàng)建CA證書
- 第一步:創(chuàng)建一個(gè)秘鑰摆舟,這個(gè)便是CA證書的根本钝鸽,之后所有的東西都來(lái)自這個(gè)秘鑰
# 通過rsa算法生成2048位長(zhǎng)度的秘鑰
openssl genrsa -out myCA.key 2048
- 第二步:是通過秘鑰加密機(jī)構(gòu)信息形成公鑰
# 公鑰包含了機(jī)構(gòu)信息薯定,在輸入下面的指令之后會(huì)有一系列的信息輸入隙轻,這些信息便是機(jī)構(gòu)信息埠帕,公司名稱地址什么的
# 這里還有一個(gè)過期信息,CA證書也會(huì)過期玖绿,openssl默認(rèn)是一個(gè)月敛瓷,我們直接搞到10年
openssl req -new -x509 -key myCA.key -out myCA.cer -days 3650
參數(shù)名稱 | 參數(shù)值 |
---|---|
Country Name | 國(guó)家代碼,比如中國(guó)就是CN |
State or Province Name | 省名稱 |
Locality Name | 城市名稱 |
Organization Name | 機(jī)構(gòu)名稱 |
Organizational Unit Name | 機(jī)構(gòu)單位名稱 |
Common Name | 重點(diǎn)參數(shù):授權(quán)給什么斑匪,因?yàn)闄C(jī)構(gòu)是根節(jié)點(diǎn)所以是授權(quán)給自己 |
Email Address | 郵件地址 |
2呐籽,創(chuàng)建服務(wù)器證書
在得到CA證書之后,需要通過
openssl
工具對(duì)證書進(jìn)行轉(zhuǎn)換得到公鑰(.crt文件
)和密鑰(.key文件
)蚀瘸,無(wú)論CA證書是怎么來(lái)的到這里之后就沒有任何區(qū)別了狡蝶,服務(wù)器證書的制作流程相較CA證書要復(fù)雜一點(diǎn)點(diǎn)。
- 第一步:通過
openssl
工具創(chuàng)建服務(wù)器的秘鑰
# 通過RSA算法生成長(zhǎng)度2048位的秘鑰
openssl genrsa -out server.key 2048
- 第二步:這里是創(chuàng)建一個(gè)簽名請(qǐng)求
首先
https
證書的公鑰不同于自定義情況下的加密證書贮勃,這里需要安裝瀏覽器標(biāo)準(zhǔn)進(jìn)行配置牢酵,首先openssl
默認(rèn)的證書版本是V1,V1在支持https
時(shí)部分瀏覽器依舊會(huì)認(rèn)為不安全衙猪,所以需要使用V3版本;同時(shí)openssl
即便是使用V3版本依舊沒有附帶V3的subjectAltName
字段數(shù)據(jù)(這里是證書對(duì)應(yīng)的IP地址或者域名,可以用通配符)垫释。但是這些東西命令行沒法指定所以需要配置文件丝格,我這里準(zhǔn)備了一個(gè):
tsa_policy2 = 1.2.3.4.5.6
tsa_policy3 = 1.2.3.4.5.7
[ ca ]
default_ca = CA_default # The default ca section
[ CA_default ]
dir = ./demoCA # Where everything is kept
certs = $dir/certs # Where the issued certs are kept
crl_dir = $dir/crl # Where the issued crl are kept
database = $dir/index.txt # database index file.
new_certs_dir = $dir/newcerts # default place for new certs.
certificate = $dir/cacert.pem # The CA certificate
serial = $dir/serial # The current serial number
crlnumber = $dir/crlnumber # the current crl number
crl = $dir/crl.pem # The current CRL
private_key = $dir/private/cakey.pem# The private key
RANDFILE = $dir/private/.rand # private random number file
x509_extensions = usr_cert # The extentions to add to the cert
name_opt = ca_default # Subject Name options
cert_opt = ca_default # Certificate field options
default_days = 365 # how long to certify for
default_crl_days= 30 # how long before next CRL
default_md = default # use public key default MD
preserve = no # keep passed DN ordering
policy = policy_match
[ policy_match ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[ policy_anything ]
countryName = optional
stateOrProvinceName = optional
localityName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[ req ]
default_bits = 1024
default_keyfile = privkey.pem
distinguished_name = req_distinguished_name
attributes = req_attributes
x509_extensions = v3_ca # The extentions to add to the self signed cert
string_mask = utf8only
req_extensions = v3_req # The extensions to add to a certificate request
[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = CN
countryName_min = 2
countryName_max = 2
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = BeiJing
localityName = Locality Name (eg, city)
0.organizationName = Organization Name (eg, company)
0.organizationName_default = myca
organizationalUnitName = Organizational Unit Name (eg, section)
commonName = Common Name (e.g. server FQDN or YOUR name)
commonName_max = 64
emailAddress = Email Address
emailAddress_max = 64
[ req_attributes ]
challengePassword = A challenge password
challengePassword_min = 4
challengePassword_max = 20
unstructuredName = An optional company name
[ usr_cert ]
basicConstraints=CA:FALSE
nsCertType = client, email, objsign
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
nsComment = "OpenSSL Generated Certificate"
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid,issuer
[ svr_cert ]
basicConstraints=CA:FALSE
nsCertType = server
keyUsage = nonRepudiation, digitalSignature, keyEncipherment, dataEncipherment, keyAgreement
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid,issuer
extendedKeyUsage = serverAuth,clientAuth
[ v3_req ]
subjectAltName = @alt_names
# 這里是重點(diǎn),需要將里面配置為最終服務(wù)端需要的域名或者IP
# 這里可以寫多個(gè)棵譬,能夠自行添加DNS.X = XXXXXX
[ alt_names ]
DNS.1 = wangyq.top
[ v3_ca ]
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid:always,issuer
basicConstraints = CA:true
[ crl_ext ]
authorityKeyIdentifier=keyid:always
[ proxy_cert_ext ]
basicConstraints=CA:FALSE
nsComment = "OpenSSL Generated Certificate"
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid,issuer
proxyCertInfo=critical,language:id-ppl-anyLanguage,pathlen:3,policy:foo
[ tsa ]
default_tsa = tsa_config1 # the default TSA section
[ tsa_config1 ]
dir = ./demoCA # TSA root directory
serial = $dir/tsaserial # The current serial number (mandatory)
crypto_device = builtin # OpenSSL engine to use for signing
signer_cert = $dir/tsacert.pem # The TSA signing certificate
# (optional)
certs = $dir/cacert.pem # Certificate chain to include in reply
# (optional)
signer_key = $dir/private/tsakey.pem # The TSA private key (optional)
default_policy = tsa_policy1 # Policy if request did not specify it
# (optional)
other_policies = tsa_policy2, tsa_policy3 # acceptable policies (optional)
digests = md5, sha1 # Acceptable message digests (mandatory)
accuracy = secs:1, millisecs:500, microsecs:100 # (optional)
clock_precision_digits = 0 # number of digits after dot. (optional)
ordering = yes # Is ordering defined for timestamps?
# (optional, default: no)
tsa_name = yes # Must the TSA name be included in the reply?
# (optional, default: no)
ess_cert_id_chain = no # Must the ESS cert id chain be included?
# (optional, default: no)
將上面的配置內(nèi)容保存為openssl.cnf
放到生成的服務(wù)器證書文件的目錄下(注意:修改alt_names里面的域名或者IP為最終部署需要的地址显蝌,支持通配符),然后執(zhí)行創(chuàng)建簽名申請(qǐng)文件即可订咸,執(zhí)行運(yùn)行:
注意:在
openssl.conf
中一定要加有關(guān)的域名曼尊,然后生成對(duì)應(yīng)服務(wù)器的證書的時(shí)候也使用域名就可以實(shí)現(xiàn)正常的訪問了。
# 和創(chuàng)建CA時(shí)一樣這里需要輸入一堆服務(wù)器信息脏嚷,輸入項(xiàng)也是相同的骆撇。
# 不過在輸入Common Name(CN)最好直接輸入服務(wù)器的IP地址或者域名。
openssl req -config openssl.cnf -new -out server.req -key server.key
- 第三步:通過CA機(jī)構(gòu)證書對(duì)服務(wù)器證書進(jìn)行簽名認(rèn)證
這里服務(wù)器的公鑰是由CA證書的密鑰配對(duì)加密來(lái)的父叙。
# 這里沒有什么需要說的神郊,本質(zhì)上就是將簽名請(qǐng)求文件進(jìn)行簽名最終得到服務(wù)器的公鑰
openssl x509 -req -extfile openssl.cnf -extensions v3_req -in server.req -out server.cer -CAkey myCA.key -CA myCA.cer -days 3650 -CAcreateserial -CAserial serial
補(bǔ)充:如果需要將cer證書轉(zhuǎn)成crt證書,需要如下命令
openssl x509 -inform PEM -in server.cer -out server.crt
- 第四步部署證書
我們通過Nginx部署趾唱,最終得到server.key
就是秘鑰涌乳,server.cer
文件就是公鑰只需要配置給Nginx就行了。
我們可以單獨(dú)添加一個(gè)配置文件甜癞,并在默認(rèn)文件中通過【include】將其引入:
server {
listen 8080 ssl;
server_name localhost;
ssl_certificate /data/sslTest/server.cer;
ssl_certificate_key /data/sslTest/server.key;
ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /data/empowerment/web/dist;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location ^~ /#/^~ {
proxy_pass http://127.0.0.1:8081/;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
}
location /system {
proxy_pass http://127.0.0.1:8081/system;
}
location /list {
proxy_pass http://127.0.0.1:8081/list;
}
error_page 405 =200 @405;
location @405 {
proxy_method GET;
proxy_pass http://127.0.0.1:8081;
}
}
3夕晓,信任CA機(jī)構(gòu)證書
通過openssl
創(chuàng)建的CA證書,只需要將CA證書的公鑰(myCA.cer文件
)導(dǎo)入到系統(tǒng)信任的根證書頒發(fā)機(jī)構(gòu)里面就行了悠咱,即直接雙擊cer
文件進(jìn)行證書安裝蒸辆,最終不光是windows系統(tǒng),任何操作系統(tǒng)都可以安裝證書來(lái)進(jìn)行對(duì)CA機(jī)構(gòu)的進(jìn)行信任操作乔煞。
- 第一步:雙擊證書吁朦,點(diǎn)擊【安裝證書】;
- 第二步:選擇【本地計(jì)算機(jī)】渡贾,根據(jù)提示選擇【是】逗宜;
- 第三步:選擇【將所有的證書都放入下列存儲(chǔ)】,并點(diǎn)擊【瀏覽(R)】空骚;
- 第四步:選擇【受信任的根證書頒發(fā)機(jī)構(gòu)】纺讲,并點(diǎn)擊【確定】;
- 第五步:選擇【下一頁(yè)(N)】囤屹,緊接著點(diǎn)【完成】熬甚。
4,參考連接
1肋坚,局域網(wǎng)內(nèi)搭建瀏覽器可信任的SSL證書
2乡括,將cer格式的證書轉(zhuǎn)為crt格式
5肃廓,原理簡(jiǎn)介
(1),有一個(gè)CA機(jī)構(gòu)
只有CA機(jī)構(gòu)的公鑰證書被安裝在受信任的根證書頒發(fā)機(jī)構(gòu),才能達(dá)到將做到讓系統(tǒng)信任诲泌。
- 第一步:需要?jiǎng)?chuàng)建一個(gè)密鑰文件(myCA.key)盲赊;
- 第二步:根據(jù)這個(gè)創(chuàng)建出一個(gè)公鑰文件(myCA.cer)。
經(jīng)過如上兩步操作之后敷扫,我們就自定義了一個(gè)CA證書哀蘑,意味著我們有了一個(gè)CA機(jī)構(gòu)。
(2),再自定義生成一個(gè)證書文件葵第,并通過CA機(jī)構(gòu)進(jìn)行簽名
- 第一步:創(chuàng)建一個(gè)密鑰文件(server.key)绘迁;
- 第二步:創(chuàng)建一個(gè)簽名請(qǐng)求的配置文件(openssl.cnf);
- 第三步:根據(jù)配置文件卒密,創(chuàng)建出一個(gè)簽名的請(qǐng)求缀台,也會(huì)生成一個(gè)文件(server.req);
- 第四步:通過CA機(jī)構(gòu)對(duì)我們的密鑰文件(server.crt) 和 簽名請(qǐng)求(server.req) 一起來(lái)生成自定義的公鑰證書(server.cer)栅受;
- 第五步:如果需要将硝,也可以將.cer文件轉(zhuǎn)成.crt文件;
- 第六步:就是將證書通過Nginx代理屏镊;
(3),最后依疼,將CA結(jié)構(gòu)添加到訪問者的受信任區(qū)
因?yàn)槲覀冏远x的CA機(jī)構(gòu)一定不再請(qǐng)求的信任區(qū)內(nèi)的,所以我們需要手動(dòng)添加一下才可以而芥。