OpenSSL自建CA和颁发SSL证书
系統環境:
Ubuntu 18.10
OpenSSL 1.1.1 ?11 Sep 2018
一:自建CA
1:依次創建如下目錄
mkdir -p /opt/ca/root
mkdir?/opt/ca/root/key
2:vim /opt/ca/root/openssl.cnf
[ ca ] default_ca = CA_default[ CA_default ] dir = /opt/ca/root certs = $dir/certs crl_dir = $dir/crl database = $dir/index.txt new_certs_dir = $dir/newcerts certificate = $dir/key/cacert.crt serial = $dir/serial crlnumber = $dir/crlnumber crl = $dir/crl.pem private_key = $dir/key/cakey.pem RANDFILE = $dir/key/.rand unique_subject = nox509_extensions = usr_cert copy_extensions = copyname_opt = ca_default cert_opt = ca_defaultdefault_days = 365 default_crl_days= 30 default_md = sha256 preserve = no policy = policy_ca[ policy_ca ] countryName = supplied stateOrProvinceName = supplied organizationName = supplied organizationalUnitName = supplied commonName = supplied emailAddress = optional[ req ] default_bits = 2048 default_keyfile = privkey.pem distinguished_name = req_distinguished_name attributes = req_attributes x509_extensions = v3_ca string_mask = utf8only utf8 = yes prompt = no[ req_distinguished_name ] countryName = CN stateOrProvinceName = beijing localityName = beijing organizationName = Global Google CA Inc organizationalUnitName = Root CA commonName = Global Google Root CA[ usr_cert ] basicConstraints = CA:TRUE[ v3_ca ] basicConstraints = CA:TRUE[ req_attributes ]3:創建如下目錄及文件
mkdir /opt/ca/root/newcerts
touch /opt/ca/root/index.txt
touch /opt/ca/root/index.txt.attr
echo 01 > /opt/ca/root/serial
4:創建CA私鑰
openssl genrsa -out /opt/ca/root/key/cakey.pem 2048
5:生成CA證書請求文件
openssl req -new -key /opt/ca/root/key/cakey.pem -out /opt/ca/root/key/ca.csr -config /opt/ca/root/openssl.cnf
6:自簽名
openssl ca -selfsign -in /opt/ca/root/key/ca.csr -out /opt/ca/root/key/cacert.crt -config /opt/ca/root/openssl.cnf
7:修改/opt/ca/root/openssl.cnf配置,把
[ usr_cert ]
basicConstraints = CA:TRUE
修改為
[ usr_cert ]
basicConstraints = CA:FALSE
CA:TRUE代表的是簽發的是CA機構(自己是CA機構),CA:FALSE代表的是簽發的是證書(改成false就不能去簽發其他CA)
經過以上7個步驟,就成功創建了CA私鑰及CA證書。有了這些就可以去簽發其他的證書請求了
?
二:使用自建CA簽名證書
1:mkdir /opt/ca/taobao
2:vim /opt/ca/taobao/openssl.cnf
[ req ] prompt = no distinguished_name = server_distinguished_name req_extensions = req_ext x509_extensions = v3_req attributes = req_attributes string_mask = utf8only utf8 = yes[ server_distinguished_name ] commonName = taobao2018.cn stateOrProvinceName = guangzhou countryName = CN organizationName = 廣州我要淘科技有限公司 organizationalUnitName = IT[ v3_req ] basicConstraints = CA:FALSE keyUsage = nonRepudiation, digitalSignature, keyEncipherment[ req_attributes ][ req_ext ] subjectAltName = @alternate_names[ alternate_names ] DNS.1 = taobao2018.cn DNS.2 = bbs.taobao2018.cn DNS.3 = taobao2019.cn3:生成網站私鑰
openssl genrsa -out /opt/ca/taobao/privkey.pem 2048
4:生成證書請求文件(csr文件)
openssl req -new -key /opt/ca/taobao/privkey.pem -out /opt/ca/taobao/taobao.csr -config /opt/ca/taobao/openssl.cnf
5:使用自建CA進行簽發證書
openssl ca -in /opt/ca/taobao/taobao.csr -out /opt/ca/taobao/taobao.crt -config /opt/ca/root/openssl.cnf
6:查看證書信息(可選)
openssl x509 -text -in?/opt/ca/taobao/taobao.crt
經過以上幾個步驟,就生成了由自建CA簽發的證書了
?
三:配置nginx的ssl
server {listen 443 ssl;server_name taobao2018.cn bbs.taobao2018.cn taobao2019.cn;ssl_certificate /opt/ca/taobao/taobao.crt;ssl_certificate_key /opt/ca/taobao/privkey.pem;ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers on;location / {root html;index index.html index.htm;} }保存配置文件之后,啟動nginx
?
四:導入自建CA的證書(根證書)
這里以Firefox為例,打開:選項 -> 隱私與安全 -> 查看證書,在證書頒發機構里面選擇導入,
選擇文件 /opt/ca/root/key/cacert.crt 導入并勾選2個信任的復選框
?
五:配置hosts
192.168.133.134 taobao2018.cn 192.168.133.134 bbs.taobao2018.cn 192.168.133.134 taobao2019.cn最后,使用https方式訪問上面的三個url中的任意一個均可
訪問之后,也可以在Firefox上查看證書
?
注意:
1:證書的x509信息如:stateOrProvinceName、organizationalUnitName已經在openssl.cnf配置文件中指定了,所以在生成證書請求文件的時候,不需要再輸入了
2:證書請求文件里面的commonName,只需要填寫主要的域名就可以了,其他的域名(包括主域名)必須要在openssl.cnf配置文件的subjectAltName屬性中指定,否則瀏覽器會報不安全警告。本例子中展示了證書支持3個域名,所以這3個域名都要配置在subjectAltName屬性中
總結
以上是生活随笔為你收集整理的OpenSSL自建CA和颁发SSL证书的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 收集的JS工具函数
- 下一篇: c语言 常量整数,C语言的整型常量