openssl搭建雙向認證https

http://www.barretlee.com/blog/2015/10/05/how-to-build-a-https-server/

http://blog.163.com/fangjinbao@126/blog/static/50873786201111265749952/

1、生成密鑰、證書

        第一步,爲服務器端和客戶端準備公鑰、私鑰git

# 生成服務器端私鑰
openssl genrsa -out server.key 1024
# 生成服務器端公鑰
openssl rsa -in server.key -pubout -out server.pem


# 生成客戶端私鑰
openssl genrsa -out client.key 1024
# 生成客戶端公鑰
openssl rsa -in client.key -pubout -out client.pem

        第二步,生成 CA 證書web

# 生成 CA 私鑰
openssl genrsa -out ca.key 1024
# X.509 Certificate Signing Request (CSR) Management.
openssl req -new -key ca.key -out ca.csr
# X.509 Certificate Data Management.
openssl x509 -req -in ca.csr -signkey ca.key -out ca.crt

         在生成csr申請文件的時候須要填寫公司的具體信息,如:apache

➜  keys  openssl req -new -key ca.key -out ca.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Zhejiang
Locality Name (eg, city) []:Hangzhou
Organization Name (eg, company) [Internet Widgits Pty Ltd]:My CA
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:*.yunzhu.com
Email Address []:

           第三步,生成服務器端證書和客戶端證書(服務器端證書須要特別注意申請的域名或者ip,客戶端會驗證本身請求的地址是否和證書裏面的地址是否相同)windows

# 服務器端須要向 CA 機構申請簽名證書,在申請簽名證書以前依然是建立本身的 CSR 文件
openssl req -new -key server.key -out server.csr
# 向本身的 CA 機構申請證書,簽名過程須要 CA 的證書和私鑰參與,最終頒發一個帶有 CA 簽名的證書
openssl x509 -req -CA ca.crt -CAkey ca.key -CAcreateserial -in server.csr -out server.crt

# client 端
openssl req -new -key client.key -out client.csr
# client 端到 CA 簽名
openssl x509 -req -CA ca.crt -CAkey ca.key -CAcreateserial -in client.csr -out client.crt

 

四、生成pkcs12格式證書(該格式證書包含CA證書,私鑰和本身的證書)
在tomcat中實現雙向認證有時須要pkcs12格式的證書(該證書包含根證書、服務器端或客戶端的證書和密鑰文件)瀏覽器

4.一、生成pkcs12服務器證書tomcat.p12,設置密碼爲1234563
openssl pkcs12 -export -in server/server.crt -inkey server/server.key -out server/tomcat.p12 -name tomcat -CAfile ca/ca.crt -caname root -chain
1234563(後邊Tomcat的配置文件中須要)tomcat

4.二、生成pkcs12客戶端證書client1.p12,設置密碼爲1234562服務器

openssl pkcs12 -export -in client/client.crt -inkey client/client.key -out client/client.p12 -name client -chain -CAfile ca/ca.crt 
1234562  回車(設置密碼爲空,在用戶導入時不用輸入密碼) 回車tcp

五、有時可能須要別的格式的證書ui

生成pem格式證書spa

cat private/privatekey.crt  private/privatekey.key> private/privatekey.pem

cat server/server.crt  server/server.key > server/server.pem

2、tomcat實現雙向認證

按照以上方法證書生成後,咱們再配置TomCat便可實現雙向認證。此時服務器端咱們只用到服務器端pkcs12格式的證書tomcat.p12和CA的根證書ca.crt便可,客戶端用到pkcs12格式的證書client.p12。

一、將以上建立的證書server.crt和tomcat.p12拷貝到tomcat主目錄下的conf文件夾下。
cp server/server.crt server/tomcat.p12 /home/apache-tomcat-6.0.35/conf/

二、建立服務器信任的CA證書庫(原理上導入CA證書便可,可是本身作實驗發現導入CA證書致使服務端不信任客戶端,導入客戶端的證書以後就行了,不知道哪裏出錯了)
把ca.crt證書導入信任證書庫,命令行模式進入tomcat主目錄下的conf目錄,執行如下命令: 
cd /home/apache-tomcat-6.0.35/conf/
keytool -keystore truststore.jks -keypass 111111 -storepass 111111 -alias ca -import -trustcacerts -file /usr/local/openssl/ca/ca.crt

*若是出現ca已經存在的錯誤,keytool -delete -alias ca -keystore truststore.jks      111111(keytool的密碼,上一次使用時使用的密碼)

能夠用如下命令查看信任證書庫內容:
keytool -keystore truststore.jks -keypass 111111 -storepass 111111 -list -v

(
 參考修改keytool的密碼:
 keytool -storepasswd -new 123456  -storepass 111111 -keystore truststore.jks
  其中-storepass指定原密碼,-new指定新密碼。
  keytool -delete -alias ca -keystore truststore.jks  要求輸入的密碼就是上邊修改的密碼

)

三、配置Tomcat支持HTTPS雙向認證
修改tomcat的conf目錄裏的server.xml文件($TOMCAT_HOME/conf/server.xml),找到相似下面內容的配置處,添加配置以下:

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="true" sslProtocol="TLS"
        keystoreFile="conf/tomcat.p12" keystorePass="1234563" keystoreType="PKCS12"
        truststoreFile="conf/truststore.jks" truststorePass="111111" truststoreType="JKS"
 />

配置好後,重啓tomcat,

/home/apache-tomcat-6.0.35/bin/catalina.sh stop

/home/apache-tomcat-6.0.35/bin/catalina.sh start

能夠監控8443端口看https是否啓動起來:

netstat –an|grep 8443
tcp       0      0 :::8443                     :::*                    LISTEN

3、客戶端安裝證書:客戶端導入client1.p12 證書,windows系統雙擊安裝便可。

     經過瀏覽器https方式訪問服務器的web時會提示選擇證書。

     安裝CA的證書來讓客戶端來信任服務端的證書

   https://服務器Ip地址:服務端口號

相關文章
相關標籤/搜索