天天看點

自建CA并簽名server證書實作https

因為目前環境在本地進行測試,是以我們采用自建CA并簽名server證書的方式實作https。(如果使用AWS服務則是AWS作為CA,并根據我們提供的資料生成server證書)

數字簽名相關知識課參閱:https://blog.csdn.net/m0_37263637/article/details/80285143

server環境:nginx(ubuntu)

工具:openssl

名詞說明:CA 證書授權中心(Certificate Authority )

參考連結:http://www.cnblogs.com/kyrios/p/tls-and-certificates.html

1 自建CA

1.1 生成CA私匙

openssl genrsa -out icatchtek.key 2048           

1.2 生成CA憑證請求

openssl req -new -key icatchtek.key -out icatchtek.csr           

過程中需要你輸入該根證書相關資訊

PS:證書請求是對簽名的請求,需要使用私鑰進行簽名

1.3 生成CA根證書

openssl x509 -req -in icatchtek.csr -extensions v3_ca -signkey icatchtek.key -out icatchtek.crt           

這裡就得到的CA的根證書

PS:證書是自簽名或CA簽名過的憑據,用來進行身份認證

2 自建server端證書

2.1 生成server私匙

openssl genrsa -out smarthome_server.key 2048           

2.2 生成server證書請求

openssl req -new -key smarthome_server.key -out smarthome_server.csr           

2.3 生成server證書

生成server證書,需要一份配置檔案,可參閱:vi openssl.cnf

[req]  
    distinguished_name = req_distinguished_name  
    req_extensions = v3_req  

    [req_distinguished_name]  
    countryName = Country Name (2 letter code)  
    countryName_default = CN  
    stateOrProvinceName = State or Province Name (full name)  
    stateOrProvinceName_default = SiChuan  
    localityName = ChengDu (eg, city)  
    localityName_default = xx.xx
    organizationalUnitName  = xxxxxx
    organizationalUnitName_default  = Domain Control Validated  
    commonName = Internet Widgits Ltd  
    commonName_max  = 64  

    [ v3_req ]  
    # Extensions to add to a certificate request  
    basicConstraints = CA:FALSE  
    keyUsage = nonRepudiation, digitalSignature, keyEncipherment  
    subjectAltName = @alt_names  

    [alt_names]  
   #注意這個IP.1的設定,IP位址需要和你的伺服器的監聽位址一樣 DNS為server網址
    IP.1 = 172.28.28.4
    DNS.1 = www.helloworld.com           

需要将server監聽的位址寫入證書中,如果方式時位址與證書中位址不一緻将不能通過證書認證。

執行指令生成server證書

openssl x509 -days 365 -req -in smarthome_server.csr -extensions  v3_req -CAkey icatchtek.key -CA icatchtek.crt -CAcreateserial -out smarthome_server.crt  -extfile openssl.cnf           

-days 為證書有效期

3 在server部署server證書

Nginx 部署可參考:https://blog.csdn.net/m0_37263637/article/details/80077925

在2中我們得到了經過CA認證後的并生成的server證書,現在我們将證書部署到nginx目錄下進行測試.

在nginx.conf中 server 塊内 listen 端口後添加下面的語句

ssl on;
      ssl_certificate      /home/ubuntu/webvideo/nginx/conf/smarthome_server.crt;
      ssl_certificate_key  /home/ubuntu/webvideo/nginx/conf/smarthome_server.key;           

我們直接通路nginx 開放的端口,會得到浏覽器的警告:

自建CA并簽名server證書實作https

PS:2中openssl.cnf中的位址一定要與nginx架設位址一緻。

4 将自建CA根證書加入到浏覽器

因為我們不是官方的CA機構并沒有内置在浏覽器或系統中,是以浏覽器會認為該CA非法,此時我們需要将我們自建CA的根證書加入到浏覽器中。(1操作流程中得到的證書)

  • Chrome :設定-設定-設定-進階-隐私設定和安全性-管理證書-導入
  • 火狐:設定-選項-進階-證書-檢視證書-證書機構-導入

以上設定均可百度相關浏覽器導入證書。

IE 和 safari 使用的windows 預設證書導入方式但不能被識别

現在通路就沒有這個警告了。

自建CA并簽名server證書實作https

繼續閱讀