apache https 雙向認證


Https分單向認證和雙向認證

單向認證表現形式:網站URL鏈接為https://xxx.com格式

雙向認證表現心事:網站URL鏈接為https://xxx.com格式,並且需要客戶端瀏覽器安裝一個client.pfx格式的證書文件才能打開網址

 

需求說明:假設需要實現本地環境newdefend.com域名雙向認證。單向認證,直接忽略本文中黃色底的操作步驟即可。也不需要生成和安裝客戶端證書:client.pfx

參考網址:http://blog.163.com/hr_php/blog/static/235853083201503011428985/

安裝環境:Wampserver集成安裝包;window7系統。

第一步:cmd進入apache的bin目錄。eg:D:\wamp5.3\bin\apache\Apache2.2.21\bin

指明配置文件

set OPENSSL_CONF=../conf/openssl.cnf

配置參考(不需要手動去改這個文件的內容,這里只是貼出代碼,提供參考。下面建立文件夾,主要就是從這里看的):

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.
# several ctificates with same subject.
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
# must be commented out to leave a V1 CRL
crl        = $dir/crl.pem         # The current CRL
private_key    = $dir/private/cakey.pem# The private key
RANDFILE    = $dir/private/.rand    # private random number file

 新建一些目錄和文件。創建目錄的原因:由於配置文件openssl.cnf里面會用到這些目錄或者文件,繼續下面的命令時,會操作這些目錄和文件

在apache/bin目錄創建如下文件夾
demoCA/newcerts/
demoCA/private/
創建如下文件
demoCA/index.txt
demoCA/serial   //內容寫 2個(0-9,A-F)的字符即可,如1A

 第二步:生成需要的私鑰key

openssl genrsa > root.key   // 生成根密鑰
openssl genrsa > server.key  // 生成服務端密鑰
openssl genrsa > client.key  // 生成客戶端密鑰

第三步:生成自簽名的根證書

openssl req -x509 -new -key root.key >root.crt

 說明:-new 生成一個新的文件 -key filename 參數filename指明我們的私有密鑰文件名 -x509 將產生自簽名的證書,一般用來測試用,或者自己玩下做個Root CA.證書的擴展在config文件里面指定  

第四步:生成服務端,客戶端簽名請求文件。

openssl req -new -key server.key -out server.csr
openssl req -new -key client.key -out client.csr  //此處可能會有錯誤信息,請看下面的錯誤解決方案

 說明:按提示輸入一系列的參數 CN - SH - SH - '' - '' - 域名/IP 。除了Common Name處填寫域名/ip需要特別注意,國家地區填寫“中國CN上海SH”。其余地方可以為空

Country Name (2 letter code) [AU]:CNISO國家代碼(只支持兩位字符)
State or Province Name (full name) [Some-State]:ZJ所在省份
Locality Name (eg, city) []:HZ所在城市
Organization Name (eg, company):THS公司名稱
Organizational Unit Name (eg, section) []:THS組織名稱
Common Name (eg, YOUR name) []:localhost(申請證書的域名或IP地址)
Email Address []:laoniangke@xxx.com管理員郵箱
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:交換密鑰
An optional company name []:

 提示:錯誤解決方案(出現在最開始創建的那堆文件里)

//錯誤提示
Sign the certificate? [y/n]:y failed to update database TXT_DB error number 2 //產生的原因是: This thing happens when certificates share common data. You cannot have two certificates that look otherwise the same. //解決辦法 //方法一: 修改demoCA下 index.txt.attr unique_subject = yes 為 unique_subject = no //方法二: 刪除demoCA下的index.txt里面的內容 //方法三: 將 common name設置成不同的

 第五步:使用根證書為服務端及客戶端簽名  

openssl ca -in server.csr -cert root.crt -keyfile root.key -out server.crt
openssl ca -in client.csr -cert root.crt -keyfile root.key -out client.crt

 說明:-in filename要簽名的csr文件 -cert CA本身的證書文件名 -keyfile CA自己的私有密鑰文件 -out filename簽名后的證書文件名。證書的細節也會給寫進去

第六步:配置openssl.cnf,創建文件路徑,生成所需cakey.pem(CA的key文件)和cacert.pem(CA的crt文件)文件 

openssl genrsa -out demoCA/private/cakey.pem 2048
openssl req -out demoCA/cacert.pem   -x509 -new -key demoCA/private/cakey.pem

 第七步:客戶端證書轉成pfx格式,生成后,直接雙擊文件安裝到瀏覽器 (重要)

openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.pfx

 注意:這個時候填寫的密碼,是客戶安裝證書的時候,需要填寫的密碼。密碼可以為空

到此openssl相關的證書文件就結束了

第八步:配置apache目錄httpd.conf文件

LoadModule ssl_module modules/mod_ssl.so
Include conf/extra/httpd-ssl.conf

 第九步:配置httpd-ssl.conf。說明:如果apache下有多個站點,還要配置http-vhosts.conf

SSLSessionCache        "shmcb:D:/wamp/bin/apache/apache2.4.9/logs/ssl_scache(512000)"
<VirtualHost _default_:443>

    DocumentRoot "D:/wamp/www"
    ServerName  localhost
 
    SSLEngine on
    SSLCertificateFile "D:/wamp/bin/apache/apache2.4.9/bin/server.crt"
    SSLCertificateKeyFile "D:/wamp/bin/apache/apache2.4.9/bin/server.key"    # 如果是單向認證,下面3行注釋掉 SSLCACertificateFile "D:/wamp/bin/apache/apache2.4.9/bin/root.crt" SSLVerifyClient require SSLVerifyDepth 1
CustomLog "D:/wamp/bin/apache/apache2.4.9/logs/ssl_request.log" \ "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" </VirtualHost>

 http-vhosts.conf

<VirtualHost *:80>  
    DocumentRoot "D:/wamp/www/www.newdefend.com"
    ServerName my.newdefend.com
</VirtualHost>
<VirtualHost *:443>  
    DocumentRoot "D:/wamp/www/www.newdefend.com"
    ServerName my.newdefend.com
    ErrorLog "D:/wamp/www/logs/error.log"
    TransferLog "D:/wamp/www/logs/access.log"
 
    SSLEngine on
    SSLCertificateFile "D:/wamp/bin/apache/apache2.4.9/bin/server.crt"
    SSLCertificateKeyFile "D:/wamp/bin/apache/apache2.4.9/bin/server.key"    

# 如果是單向認證,下面3行注釋掉 SSLCACertificateFile
"D:/wamp/bin/apache/apache2.4.9/bin/root.crt" SSLVerifyClient require SSLVerifyDepth 10 </VirtualHost>

 重啟wamp

 雙向認證效果展示:

下圖表示:client.pfx文件還沒有在電腦上安裝,請雙擊安裝

下圖表示:client.pfx已經安裝,客戶端打開時,需要選擇證書

下圖表示:成功,直接訪問即可。因為是本地生成的證書,所以不被瀏覽器信任。真是上線的時候,找個權威的CA機構就不會出現這種情況了

最后:查看電腦上已經安裝了那些證書,請參考:https://jingyan.baidu.com/article/c275f6baf8622ae33d756794.html

效果展示如下:192.168.184.53就是我自己生成的證書

 

--------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM