Https:建立部署SSL證書進行雙向認證

 

1、前言git

  創建客戶端與服務器的Https的鏈接須要證書進行雙向驗證後,纔可訪問。web

 

2、證書類型算法

 

不一樣數字證書部署在服務器上後,用戶瀏覽器訪問網站時,展現以下:apache

1、無證書時瀏覽器

顯示不安全標識。tomcat

二、域名型(DV)SSL安全

能夠顯示安全鏈接標識,證書沒法顯示組織信息。服務器

 

三、企業型(OV)SSL網站

能夠顯示安全鏈接標識,證書詳細內顯示組織信息。ui

四、加強型(EV)SSL

地址欄綠色,能夠顯示安全鏈接標識,地址欄直接顯示組織信息。

 

 

3、生成SSL證書

  證書的建立與做用

  生成環境:

      Linux: CentOS_7(及以上)

      Tomcat: Apache Tomcat_8.5.42(及以上)

      OpenSSL: OpenSSL 1.1.0c 下載路徑:https://www.openssl.org/source/openssl-1.1.0c.tar.gz

 

  • 製做服務器根證書

  1.解壓安裝

    

#解壓tar包 tar -xzvf openssl-1.1.0c.tar.gz #進入加壓後的文件夾內 cd openssl-1.1.0c #安裝openssl到 /usr/local/openssl 目錄,安裝以後,編譯;這個時候執行須要有耐心,能夠查看命令行一直在滾屏,等到滾屏結束,安裝編譯完成 ./config --prefix=/usr/local/openssl && make && make install #重命名 mv /usr/bin/openssl /usr/bin/openssl.ori mv /usr/include/openssl /usr/include/openssl.ori #將安裝好的openssl命令軟連到對應位置 ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl ln -s /usr/local/openssl/include/openssl /usr/include/openssl #在/etc/ld.so.conf文件中寫入openssl庫文件的搜索路徑 echo /usr/local/openssl/lib >> /etc/ld.so.conf ldconfig -v openssl version -a

 

 

  2.生成證書

    2.1 建立根證書密鑰文件(本身作CA) root.key

#生成命令 openssl genrsa -des3 -out root.key 2048 #輸出日誌 Generating RSA private key, 2048 bit long modulus .....................................................................................................................+++ ..........................+++ e is 65537 (0x010001) Enter pass phrase for root.key: ← #在這輸入一個新密碼 Verifying – Enter pass phrase for root.key: ← 在這從新輸入一遍密碼

    

    2.2 建立根證書的申請文件 root.csr

#生成命令 openssl req -new -key root.key -out root.csr #輸出日誌 Enter pass phrase for root.key: ← 在這輸入前面建立的密碼 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 ← 國家代號,中國輸入CN State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音 Locality Name (eg, city) []:BeiJing ← 市的全名,拼音 Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 Organizational Unit Name (eg, section) []: ← 能夠不輸入 Common Name (eg, YOUR name) []: ← 此時不輸入 Email Address []:admin@mycompany.com ← 電子郵箱,可隨意填 Please enter the following ‘extra’ attributes to be sent with your certificate request A challenge password []: ← 能夠不輸入 An optional company name []: ← 能夠不輸入

 

    2.3 建立一個自當前日期起爲期十年的根證書 root.crt

#生成命令 openssl x509 -req -days 3650 -sha256 -extfile /usr/local/openssl/ssl/openssl.cnf -extensions v3_ca -signkey root.key -in root.csr -out root.crt #輸出日誌 Signature ok subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./emailAddress=admin@mycompany.com Getting Private key Enter pass phrase for root.key: ← 在這輸入前面建立的密碼

      

    2.4 根據CA證書生成truststore JKS文件 root.truststore

      這一步只針對雙向認證,單向不須要

 

#生成命令 keytool -keystore root.truststore -keypass 123456 -storepass 123456 -alias ca -import -trustcacerts -file /tmp/ca/root.crt

 #鍵入回車後,提示是否信任此證書,輸入yes, 則生成truststore成功

 

 

 

 

  • 製做service服務器端證書

 

1.建立服務器證書密鑰 server.key

#生成命令 openssl genrsa -des3 -out server.key 2048 #輸出內容爲: Generating RSA private key, 2048 bit long modulus ...........................+++ ...............+++ e is 65537 (0x010001) Enter pass phrase for server.key: ←在這輸入前面建立的密碼 Verifying - Enter pass phrase for server.key: ← 在這從新輸入一遍密碼 #運行時會提示輸入密碼,此密碼用於加密key文件(參數des3即是指加密算法,固然也能夠選用其餘你認爲安全的算法.),之後每當需讀取此文件(經過openssl提供的命令或API)都需輸入口令(密碼).若是以爲不方便,也能夠去除這個口令,但必定要採起其餘的保護措施! #去除口令的命令 openssl rsa -in server.key -out server.key

 

2.建立服務器證書的申請文件 server.csr

#生成命令 openssl req -new -key server.key -out server.csr #輸出內容爲: Enter pass phrase for server.key: ← 輸入前面建立的密碼 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 ← 國家名稱,中國輸入CN State or Province Name (full name) [Some-State]:BeiJing ← 省名,拼音 Locality Name (eg, city) []:BeiJing ← 市名,拼音 Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 Organizational Unit Name (eg, section) []: ← 能夠不輸入 Common Name (eg, YOUR name) []:www.xxx.com ← 服務器主機名(或者IP),若填寫不正確,瀏覽器會報告證書無效,但並不影響使用 Email Address []:admin@mycompany.com ← 電子郵箱,可隨便填 Please enter the following ‘extra’ attributes to be sent with your certificate request A challenge password []: ← 能夠不輸入 An optional company name []: ← 能夠不輸入

 

3.建立自當前日期起有效期爲期十年的服務器證書 server.crt

#生成命令 openssl x509 -req -days 3650 -sha256 -extfile /usr/local/openssl/ssl/openssl.cnf -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in server.csr -out server.crt #輸出內容爲: Signature ok subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com Getting CA Private Key Enter pass phrase for root.key: ← 輸入前面建立的密碼

 

4.導出.p12文件 server.p12

#生成命令 openssl pkcs12 -export -in /tmp/ca/server.crt -inkey /tmp/ca/server.key -out /tmp/ca/server.p12 -name "server" 根據命令提示,輸入server.key密碼,建立p12密碼。

 

5.將.p12 文件導入到keystore JKS文件 server.keystore

#生成命令 keytool -importkeystore -v -srckeystore /tmp/ca/server.p12 -srcstoretype pkcs12 -srcstorepass 123456 -destkeystore /tmp/ca/server.keystore -deststoretype jks -deststorepass 123456 這裏srcstorepass後面的123456爲server.p12的密碼deststorepass後的123456爲keyStore的密碼

 

 

  • 製做Client客戶端證書

 

1.建立客戶端證書密鑰文件 client.key

#生成命令 openssl genrsa -des3 -out client.key 2048 #輸出內容爲: Generating RSA private key, 2048 bit long modulus ...............................+++ .........................+++ e is 65537 (0x010001) Enter pass phrase for client.key: ← 輸入新密碼 Verifying – Enter pass phrase for client.key: ← 從新輸入一遍密碼

 

2.建立客戶端證書的申請文件 client.csr

#生成命令 openssl req -new -key client.key -out client.csr #輸出內容爲: Enter pass phrase for client.key: ← 輸入上一步中建立的密碼 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 ← 國家名稱,中國輸入CN State or Province Name (full name) [Some-State]:BeiJing ← 省名稱,拼音 Locality Name (eg, city) []:BeiJing ← 市名稱,拼音 Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 Organizational Unit Name (eg, section) []: ← 能夠不填 Common Name (eg, YOUR name) []:Lenin ← 本身的英文名,能夠隨便填 Email Address []:admin@mycompany.com ← 電子郵箱,能夠隨便填 Please enter the following ‘extra’ attributes to be sent with your certificate request A challenge password []: ← 能夠不填 An optional company name []: ← 能夠不填

 

3.建立一個自當前日期起有效期爲十年的客戶端證書 client.crt

#生成命令 openssl x509 -req -days 3650 -sha256 -extfile /usr/local/openssl/ssl/openssl.cnf -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in client.csr -out client.crt #輸出內容爲: Signature ok subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com Getting CA Private Key Enter pass phrase for root.key: ← 輸入上面建立的密碼

 

4.導出.p12文件 client.p12

#生成命令 openssl pkcs12 -export -in /tmp/ca/client.crt -inkey /tmp/ca/client.key -out /tmp/ca/client.p12 -name "client" 根據命令提示,輸入client.key密碼,建立p12密碼。

 

 

4、配置Tomcat

 

1.關閉tomcat

tomcat的bin目錄下執行

shutdown.sh

 

2.將證書放入web服務器

  將keystore文件(server.keystore) 放在web服務器上

cp /tmp/ca/server.keystore /你的tomcat根目錄/conf

  將truststore文件(root.truststore) 放在web服務器上

cp /tmp/ca/root.truststore /你的tomcat根目錄/conf

 

3.修改server.xml配置文件

cd /你的tomcat根目錄/conf

vi server.xml

找到下面被註釋的代碼,刪除註釋符並修改內容(vi命令操做)

<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" maxThreads="150" SSLEnabled="true" scheme="https" secure="true" keystoreFile="/你的路徑/apache-tomcat-8.5.42/conf/server.keystore" keystorePass="******" truststoreFile="/你的路徑/apache-tomcat-8.5.42/conf/root.truststore" truststorePass="******" clientAuth="false" sslEnabledProtocols="TLSv1.2"
                />

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />

 

4.啓動tomcat

tomcat的bin目錄下執行

startup.sh

 

5.訪問https服務

https://localhost:8443/

https://192.168.1.1:8443/  你的IP

相關文章
相關標籤/搜索