使用openssl爲ssl證書增加“使用者備用名稱(DNS)”

上一片講到了多CN的簽署方式利用openssl簽署多域名證書,在實際使用中遇到了一個問題,在android系統中,瀏覽器不識別多CN的域名,會報錯“證書名稱和服務器名稱不符”,開始以爲是自簽署CA的問題,換成單個CN之後就正常了,沒辦法,只能換其他方法了,google N久之後,找到到了方法:


主要修改在openssl.cnf


# 確保req下存在以下2行(默認第一行是有的,第2行被註釋了)
[ req ]
distinguished_name = req_distinguished_name
req_extensions = v3_req


# 確保req_distinguished_name下沒有 0.xxx 的標籤,有的話把0.xxx的0. 去掉
[ req_distinguished_name ]
countryName              = Country Name (2 letter code)
countryName_default = CN
stateOrProvinceName             = State or Province Name (full name)
stateOrProvinceName_default = ShangHai
localityName              = Locality Name (eg, city)
localityName_default = ShangHai
organizationalUnitName             = Organizational Unit Name (eg, section)
organizationalUnitName_default = Domain Control Validated
commonName         = Internet Widgits Ltd
commonName_max = 64


# 新增最後一行內容 subjectAltName = @alt_names(前2行默認存在)
[ v3_req ]
# Extensions to add to a certificate request
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names


# 新增 alt_names,注意括號前後的空格,DNS.x 的數量可以自己加
[ alt_names ]
DNS.1 = abc.example.com
DNS.2 = dfe.example.org
DNS.3 = ex.abcexpale.net


其他的步驟:

openssl.cnf中會要求部分文件及目錄存在:

[root@localhost]#mkdir -p CA/{certs,crl,newcerts,private}

[root@localhost]# touch CA/index.txt

[root@localhost]#echo 00 > CA/serial


1.生成ca.key並自簽署

openssl req -new -x509 -days 3650 -keyout ca.key -out ca.crt -config openssl.cnf


2.生成server.key(名字不重要)
openssl genrsa -out server.key 2048

3.生成證書籤名請求
openssl req -new -key server.key -out server.csr -config openssl.cnf
Common Name 這個寫主要域名就好了(注意:這個域名也要在openssl.cnf的DNS.x裏)

4.查看請求文件
openssl req -text -noout -in server.csr 
應該可以看到這些內容:
    Certificate Request:
    Data:
    Version: 0 (0x0)
    Subject: C=US, ST=Texas, L=Fort Worth, O=My Company, OU=My Department,             CN=server.example
    Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public Key: (2048 bit)
    Modulus (2048 bit): blahblahblah
    Exponent: 65537 (0x10001)
    Attributes:
    Requested Extensions: X509v3
    Basic Constraints: CA:FALSE
    X509v3 Key Usage: Digital Signature, Non Repudiation, Key Encipherment
    X509v3 Subject Alternative Name: DNS:domain.example.com, DNS:xxx.example.com
    Signature Algorithm: sha1WithRSAEncryption
 

5.使用自簽署的CA,簽署server.scr
openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key -extensions v3_req -config openssl.cnf
#輸入第一步設置的密碼,一直按y就可以了


server.crt server.key就是web服務器中使用的文件。

NGINX 雙向認證

如果要做NGINX客戶端證書驗證的話,重複2、3、4,並執行下面命令生成個人證書
5.生成個人證書
openssl  pkcs12 -export -inkey xxx.key -in xxx.crt -out  xxx.p12

將個人證書導入pc,同時在nginx ssl基礎上增加設置:
ssl_verify_client on;
ssl_client_certificate ca.crt;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章