Java SSL 自我簽署憑證產生

來源:互聯網
上載者:User
什麼是HTTPS?
當使用 基於SSL/TLS(通常使用 https:// URL)向網站進行HTTP請求時,從伺服器向客戶機發送一個認證。客戶機使用已安裝的公用認證通過這個認證驗證伺服器的身份,然後檢查 IP 名稱(機器名)與客戶機串連的機器是否匹配。客戶機產生一些可以用來產生對話的私密金鑰(稱為工作階段金鑰)的隨機資訊,然後用伺服器的公開金鑰對它加密並將它發送到 伺服器。伺服器用自己的私密金鑰解密訊息,然後用該隨機資訊派生出和客戶機一樣的私人工作階段金鑰。通常在這個階段使用 RSA 公開金鑰演算法。然後,客戶機和伺服器使用私人工作階段金鑰和私密金鑰演算法(通常是 RC4)進行通訊。使用另一個密鑰的訊息認證碼來確保訊息的完整性。

什麼是數位簽章?
所 謂數位簽章就是資訊寄件者用其私人金鑰組從所傳報文中提取出的特徵資料(或稱數位指紋)進行RSA演算法操作,以保證發信人無法抵賴曾發過該資訊(即不可抵 賴性),同時也確保資訊報文在經簽名後末被篡改(即完整性)。當資訊接收者收到報文後,就可以用寄件者的公開金鑰對數位簽章進行驗證。
Overview of SSL?
     SSL, or Secure Socket Layer, is a technology which allows web browsers and web servers to communicate over a secured connection. This means that the data being sent is encrypted by one side, transmitted, then decrypted by the other side before processing. This is a two-way process, meaning that both the server AND the browser encrypt all traffic before sending out data.
     Another important aspect of the SSL protocol is Authentication. This means that during your initial attempt to communicate with a web server over a secure connection, that server will present your web browser with a set of credentials, in the form of a "Certificate", as proof the site is who and what it claims to be. In certain cases, the server may also request a Certificate from your web browser, asking for proof that you are who you claim to be. This is known as "Client Authentication," although in practice this is used more for business-to-business (B2B) transactions than with individual users. Most SSL-enabled web servers do not request Client Authentication.
https與Tomcat
   The Apache Jakarta Tomcat 5 Servlet/JSP Container SSL Configuration HOW-TO?
利用快速配置:
  1,用jdk中的keytool產生一個tomcat keystore
    在命令列敲入:%JAVA_HOME%/bin/keytool -genkey -alias tomcat -keyalg RSA
    會提示你輸入keypassword 和 keystorepassword 以及其它相關資訊。按提示確認完成。
  2, 在conf/server.xml中加入:
    
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" disableUploadTimeout="true"
               acceptCount="100" debug="0" scheme="https" secure="true"
               clientAuth="false" keystorePass="password" sslProtocol="TLS" />
  3,啟動tomcat,開啟瀏覽器輸入https://localhost:8443 就OK了。
注意:用此方法產生的keystore檔案置於C:/Documents and Settings/user下
注釋:

Attribute Description
clientAuth Set this value to true if you want Tomcat to require all SSL clients to present a client Certificate in order to use this socket. Set this value to want if you want Tomcat to request a client Certificate, but not fail if one isn't presented.
keystoreFile Add this attribute if the keystore file you created is not in the default place that Tomcat expects (a file named .keystore in the user home directory under which Tomcat is running). You can specify an absolute pathname, or a relative pathname that is resolved against the $CATALINA_BASE environment variable.
keystorePass Add this element if you used a different keystore (and Certificate) password than the one Tomcat expects (changeit).
keystoreType Add this element if using a PKCS12 keystore. The valid values are JKS and PKCS12.
sslProtocol The encryption/decryption protocol to be used on this socket. It is not recommended to change this value if you are using Sun's JVM. It is reported that IBM's 1.4.1 implementation of the TLS protocol is not compatible with some popular browsers. In this case, use the value SSL.
ciphers The comma separated list of encryption ciphers that this socket is allowed to use. By default, any available cipher is allowed.
algorithm The X509 algorithm to use. This defaults to the Sun implementation (SunX509). For IBM JVMs you should use the value IbmX509. For other vendors, consult the JVM documentation for the correct value.
truststoreFile The TrustStore file to use to validate client certificates.
truststorePass The password to access the TrustStore. This defaults to the value of keystorePass.
truststoreType Add this element if your are using a different format for the TrustStore then you are using for the KeyStore. The valid values are JKS and PKCS12.

Java keytool工具的作用及使用方法?
Keytool 是安全鑰匙與認證的管理工具。它管理一個儲存了私人鑰匙和驗證相應公用鑰匙的與它們相關聯的X.509 憑證鏈的keystore(相當一個資料庫)。
Keytool 是一個有效安全鑰匙和認證的管理工具。它能夠使使用者使用數位簽章來管理他們自己的私人/公用鑰匙對,管理用來作自我評鑑的相關的認證,管理資料完整性和評鑑服務。它還能使使用者在通訊時緩衝它們的公用鑰匙.
一個認證是某一實體(個人,公司等)的數位簽章,指出其他實體的公用鑰匙(或其他資訊)的詳細的值.當資料被簽名後,這個簽名資訊被用來檢驗資料的完整性和真實性.完整性指資料沒有被修改和篡改,真實性指資料從任何產生和簽名的一方真正的傳輸到達。
Keytool 把鑰匙和認證儲存到一個keystore.默任的實現keystore的是一個檔案。它用一個密碼保護鑰匙。而另外的一個工具jarsigner用keystore中的資訊產生或檢驗Java aRchive(jar檔案)中的數位簽章。
Keystore有兩個不同的入口:
1.       鑰匙入口:儲存了非常敏感的加密的鑰匙資訊,並且是用一個保護的格式儲存以防止未被授權的訪問.以這種形式儲存的鑰匙是秘密鑰匙,或是一個對應憑證鏈結中公有鑰匙的私人鑰匙.
2.    信任認證入口:包含一個屬於其他部分的單一公用鑰匙認證.它之所以被稱為"信任認證",是因為keystore信任的認證中的公用鑰匙真正屬於認證所有者的身份識別.
 
Keystore的別名:
所有的keystore入口(鑰匙和信任認證入口)是通過唯一的別名訪問.別名是不區分大小寫。如別名Hugo和hugo指向同一個keystore入口.
可以在加一個入口到keystore的時候使用-genkey參數來產生一個鑰匙對(公用鑰匙和私人鑰匙)時指定別名.也可以用-import參數加一個認證或憑證鏈結到信任認證。
 
如:keytool -genkey -alias duke -keypass dukekeypasswd
 
其中duke為別名,dukekeypasswd為duke別名的密碼。這行命令的作用是產生一個新的公用/私人鑰匙對. 其中duke為別名,dukekeypasswd為duke別名的密碼.這行命令的作用是產生一個新的公用/私人鑰匙對.
假如你想修改密碼,可以用:
keytool -keypasswd -alias duke -keypass dukekeypasswd -new newpass
將舊密碼dukekeypasswd改為newpass.
 
Keystore的產生:
1.       當使用-genkey 或-import或-identitydb命令添加資料到一個keystore,而當這個keystore不存在時,產生一個keystore.預設名是.keystore,存放到user-home目錄.
2.    當用-keystore指定時,將產生指定的keystore.
 
Keystore的實現:
Keytool 類位於java.security包下,提供一個非常好的介面去取得和修改一個keystore中的資訊. 目前有兩個命令列:keytool和jarsinger,一個GUI工具Policy 可以實現keystore.由於keystore是公開的,使用者可以用它寫一些額外的安全應用程式。
Keystore還有一個sun公司提供的內在實現.它把keystore作為一個檔案來實現.利用了一個keystore類型(格式)"JKS".它用單獨的密碼保護每一個私人鑰匙.也用可能不同的密碼保護整個keystore的完整性.
 
支援的演算法和鑰匙大小:
keytool允許使用者指定鑰匙對和註冊密碼服務供應者所提供的簽名演算法.預設的鑰匙對產生演算法是"DSA"。假如私人鑰匙是"DSA"類型,預設簽名演算法是"SHA1withDSA",假如私人鑰匙是"RSA"類型,預設演算法是"MD5withRSA".
當產生一個DSA鑰匙對,鑰匙必須在512-1024位之間.對任何演算法的預設鑰匙大小是1024位.
 
1.       認證:一個認證是一個實體的數位簽章,指出其他實體的公用鑰匙有明確的值。
2.       公用鑰匙:是同一個詳細的實體的數字關聯,並有意讓所有想同這個實體發生信任關係的其他實體知道,公用鑰匙用來檢驗簽名;
3.    數位簽章:假如資料已被簽名,並用身份儲存在一個實體中,一個簽名能夠證明這個實體知道這個資料.這個資料用實體私人鑰匙簽名並遞交;
4.    身份:知道實體的方法.在一些系統中身份是公用鑰匙,其他系統中可以是從一個X.509名字的郵件地址的Unix UID來的任何東西;
5.    簽名:一個簽名用實體私人鑰匙來計算某些加密資料;
6.    私人鑰匙:是一些數字,每一個私人鑰匙只能被特定的擁有該私人鑰匙的實體知道.私人和公用鑰匙存在所有用公用鑰匙加密的系統的鑰匙對中.一個公用鑰匙加密(如DSA),一個私人鑰匙與一個正確的公用鑰匙通訊.私人鑰匙用來計算簽名。
7.    實體:一個實體可以是一個人,一個組織,一個程式,一台電腦,一個商業,一個銀行,或其他你想信任的東西.
 
Keytool應用執行個體:
 
1.產生一個keystore:
 
keytool -genkey -alias User(keystore的別名) -keyalg RSA -validity 7 -keystore keystore(指定keystore).
 
運行這個命令,系統提示:
Enter keystore password:yourpassword(輸入密碼)
 
What is your first and last name?
[Unknown]: your name(輸入你的名字)
 
What is the name of your organizational unit?
[Unknown]:your organizational(輸入你所在組織單位的名字)
 
What is the name of your organization?
[Unknown]:your organization name (輸入你所在組織的名字)
 
What is the name of your City or Locality?
[Unknown]:your city name(輸入所在城市的名字)
 
What is the name of your State or Province?
[Unknown]:your provice name(輸入所在省份名字)
 
What is the two-letter country code for this unit?
[Unknown]:cn(輸入國家名字)
Is CN=your name, OU=your organizaion, O="your organization name",L=your city name, ST=your province name, C=cn correct?
[no]: yes
 
3.    檢查一個keystore:
keytool -list -v -keystore keystore
Enter keystore password:your password(輸入密碼)
 
將顯示keystore內容如:
Keystore type: jks
Keystore provider: SUN
 
Your keystore contains 1 entry
Alias name: yourname
Creation date: Dec 20, 2001
Entry type: keyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=yourname, OU=your organization, O="your organization name",
L=your city name, ST=your province name, C=CN
Issuer: CN=Duke, OU=Java Software, O="Sun Microsystems, Inc.", L=Palo Alto, ST=CA, C=US
Serial number: 3c22adc1
Valid from: Thu Dec 20 19:34:25 PST 2001 until: Thu Dec 27 19:34:25 PST 2001
Certificate fingerprints:
MD5: F1:5B:9B:A1:F7:16:CF:25:CF:F4:FF:35:3F:4C:9C:F0
SHA1: B2:00:50:DD:B6:CC:35:66:21:45:0F:96:AA:AF:6A:3D:E4:03:7C:74
 
3.輸出keystore到一個檔案:testkey:
keytool -export -alias duke -keystore keystore -rfc -file testkey
系統輸出:Enter keystore password:your password(輸入密碼)
Certificate stored in file < td>
 
4.輸入認證到一個新的truststore:
keytool -import -alias dukecert -file testkey -keystore truststore
Enter keystore password:your new password.(輸入truststore新密碼)
 
 
5.檢查truststore:
keytool -list -v -keystore truststore
系統將顯示truststore的資訊.
 
現在可以用適當的keystore運行你的應用程式.如:
java -Djavax.net.ssl.keyStore = keystore
-Djavax.net.ssl.keyStorePassword=password Server
 
和:
 java -Djavax.net.ssl.trustStore=truststore
-Djavax.net.ssl.trustStorePassword=trustword Client

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.