tomcat部署https,用openssl簽發證書

常見後綴

cer,crt證書(不支持私鑰)
一般是簽署後CA頒發的證書,實質是CA用私鑰在申請者的公鑰上簽名
—–BEGIN CERTIFICATE—–

csr:(Certificate Signing Request) 申請簽名的證書請求
–-BEGIN NEW CERTIFICATE REQUEST–

pfx,p12:(Personal Information Exchange)
存儲證書和私鑰

crl:(Certificate Revocation List)
證書銷燬清單

key,pem:(Privacy-enhanced Electronic Mail)
linux/unix下存儲密鑰的,window下不能用keytool導出密鑰
—–BEGIN RSA PRIVATE KEY—–
pem同時也能存儲公鑰
—–BEGIN PUBLIC KEY—–

Various SSL/TLS Certificate File Types/Extensions

keystore是用來管理各種條目的,包括:
PrivateKeyEntry : 密鑰對
SecretKeyEntry : 私鑰【keystore格式僅爲jceks】
TrustedCertificateEntry : 證書
keystore介紹:
Manage keys, certificates and keystores

生成ssl證書

1.用easyRSA,OpenSSL,keytool都可以生成密鑰對
keytool直接是生成密鑰對,無法提取密鑰的,需要用到 java-exportpriv
Tomcat服務器配置https雙向認證(使用keytool生成證書)
2.直接在免費CA上申請服務器證書,如CA365

openssl生成方式

  1. 生成CA密鑰
    openssl genrsa -out private/cakey.pem 2048

  2. 生成CA自簽名證書
    openssl req -new -x509 -key private/cakey.pem -out cacert.pem

  3. 生成服務器的私鑰
    openssl genrsa -out private/pencilboxserver.key 2048

  4. 生成服務器的簽名請求
    openssl req -new -key private/pencilboxserver.key -out pencilboxcert.csr
    【CommonName設置爲自己的域名】

  5. 用CA爲服務器生成簽名的證書
    openssl x509 -req -in pencilboxcert.csr -CA cacert.pem -CAkey private/cakey.pem -CAcreateserial -out pencilboxsigncert.crt -days 730 -sha512

  6. 打包服務端密鑰庫
    openssl pkcs12 -export -in pencilboxsigncert.crt -inkey private/pencilboxserver.key -out pencilboxpsdlib.pfx

  7. 生成客戶端私鑰
    openssl genrsa -out private/client.pem 2048
    ps:如無需實現雙向認證,則無需準備客戶端的東東

  8. 生成客戶端的簽名請求
    openssl req -new -key private/client.pem -out client.csr

  9. 用CA爲客戶端生成簽名的證書
    openssl x509 -req -in client.csr -CA cacert.pem -CAkey private/cakey.pem -CAcreateserial -out client.crt

  10. 打包客戶端密鑰庫
    openssl pkcs12 -export -in client.crt -inkey private/client.pem -out client.pfx

openssl官方指令
SSL證書生成方式
基於OpenSSL自建CA和頒發SSL證書

keytool導入

導入CA證書

keytool -importcert -v -file /Users/pencil-box/Longwei/CA/cacert.pem -keystore /Users/pencil-box/pencilboxserver.keystore 

導入密鑰對

keytool -importkeystore -v -srckeystore /Users/pencil-box/Longwei/CA/pencilboxpsdlib.pfx -destkeystore /Users/pencil-box/pencilboxserver.keystore

PS:注意keystore是自己創建,或者是用默認的

keytool -genkey -keystore /working/xxx.keystore

keytool指令介紹:
keytool - Key and Certificate Management Tool

Tomcat配置

Tomcat的目錄下,conf目錄下需要更改server.xml和web.xml文件
server.xml

<Connector SSLEnabled="true" acceptCount="200"              clientAuth="true"
disableUploadTimeout="true" enableLookups="false" maxThreads="125"
port="8443" 
keystoreFile="/Users/pencil-box/Longwei/CA/pencilboxserver.keystore" keystorePass="123456"
truststoreFile="/Users/pencil-box/pencilboxserver.keystore" truststorePass="123456"
protocol="org.apache.coyote.http11.Http11NioProtocol"       scheme="https"
sslProtocol="TLS"
secure="true" />

clientAuth是開啓客戶端驗證的參數

true的時候開啓雙向認證
客戶端需要導入client.pfx
服務端設置truststoreFile,信任簽發客戶端證書的cacert.pem

keystroeFile是服務器密鑰對存儲的keystore
truststoreFile是簽署客戶端證書的根證書位置

注意這裏是NIO模式,如果啓用Tomcat APR模式配置中略有不同,詳見mac下tomcat啓用APR模式

web.xml
將下面這段嵌在web-app節點下:

<security-constraint>
<web-resource-collection>
    <web-resource-name>securedapp</web-resource-name>
    <url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
    <transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>

這裏配置了transport-guarantee的參數爲confidential,即全網站都是通過ssl加密
web.xml Reference Guide for Tomcat

客戶端配置

導入簽發服務器證書的CA自簽證書即可,在這裏是導入cacert.pem
雙向驗證的時候,還需要導入client.pfx

如果服務端沒有相應的域名,那麼可以通過改hosts的方式,強行進行域名映射。瀏覽器會匹配域名是否爲服務器證書的Common Name,匹配不成功會報告不安全。

注意事項

  1. 錯誤:
    Caused by: java.security.UnrecoverableKeyException: Cannot recover key
    解決方案:
    修改的key密鑰的密碼與keystore的一致。

  2. 導入keystore中需要導入服務端的證書與密鑰打包成的pkcs12文件,因爲keystore不能單獨導入密鑰。

  3. 簽發證書的時候,要保證CommonName是自己的域名or Ip,
    匹配原則是*.domain.com 只能匹配 ssl.domain.com 不能匹配 ssl1.ssl2.domain.com。

  4. 使用sha-1的簽名算法,chrome瀏覽器會不提示安全鏈接,因爲sha-1 存在脆弱性。詳見:http://www.freebuf.com/news/special/44288.html

  5. SSL的握手協議可以通過wireshark進行查看,默認的443端口是直接解析的,非443端口需要配置端口監聽SSL,具體在preferences的RSA keylist裏面。
    對於SSL握手中協商的公鑰算法,如果使用了Diffie-Hellman算法,那麼TLS Session Key會用一個動態產生的密鑰對進行加密,並不會使用證書的公鑰,所以即使配置了服務端的密鑰,也是不能解密的。
    詳情可看下面這篇文章。
    如何通過Wireshark查看HTTPS、HTTP/2網絡包(解碼TLS、SSL)
    Is there any particular reason to use Diffie-Hellman over RSA for key exchange?

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