﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-ゞ沉默是金ゞ-随笔分类-Broker</title><link>http://www.blogjava.net/dashi99/category/51985.html</link><description>&lt;div align="center"&gt;
&lt;img height="50" width="200" name="welcome" src="http://www.blogjava.net/images/blogjava_net/majianan/14891/r_5858488902000cu2.gif"/&gt;
&lt;/div&gt;
&lt;br/&gt;
&lt;center&gt;&lt;font size=4 &gt;鱼离不开水,但是没有说不离开哪滴水.&lt;/font&gt;&lt;/center&gt;</description><language>zh-cn</language><lastBuildDate>Tue, 26 Jun 2012 12:20:50 GMT</lastBuildDate><pubDate>Tue, 26 Jun 2012 12:20:50 GMT</pubDate><ttl>60</ttl><item><title>How to Create Client/Server Keystores using Java Keytool</title><link>http://www.blogjava.net/dashi99/archive/2012/06/26/381538.html</link><dc:creator>ゞ沉默是金ゞ</dc:creator><author>ゞ沉默是金ゞ</author><pubDate>Tue, 26 Jun 2012 11:15:00 GMT</pubDate><guid>http://www.blogjava.net/dashi99/archive/2012/06/26/381538.html</guid><wfw:comment>http://www.blogjava.net/dashi99/comments/381538.html</wfw:comment><comments>http://www.blogjava.net/dashi99/archive/2012/06/26/381538.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dashi99/comments/commentRss/381538.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dashi99/services/trackbacks/381538.html</trackback:ping><description><![CDATA[<div>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Here I have described how to create client/server keystores which can be used to secure Axsi2 webservices and invoke Axis2 secured webservices. i.e. It can be easily used with any <a href="http://wso2.com/products">WSO2 Product</a> to experience security scenarios.More detailed explanation on creating client/server keystores using openssl including Certificate Authority(CA) Requests, can be found at <a href="http://wso2.org/library/174">http://wso2.org/library/174</a>.</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">&nbsp;</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Java keytool stores the keys and certificates in a keystore, protected by a keystore password. Further, it protects private key again with another password. A Java keystore contains private-public key pair and multiple trusted certificate entries. All entries in a keystore are referred by aliases. Both private key and self signed public key is referred by one alias while any other trusted certificates are referred by different individual aliases.</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">&nbsp;</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">As the first step, let's create a keystore for server. In order to do it, execute following command in a terminal. "server" in the following command corresponds to the private key/self signed public key certificate alias in the keystore while "server.jks" is the name of the creating keystore file.</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt"><span style="font-weight:bold">keytool -genkey -alias server -keyalg RSA -keystore server.jks</span></p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">when you execute the above command it will first prompt you to specify a password which is corresponded to the keystore password. Then it will prompt several questions. You can give answers that you wish. At the end it will ask for a password again, which will be used to secure the generated private key.</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Enter keystore password:&nbsp;<br /> Re-enter new password: <br /> What is your first and last name?<br /> [Unknown]:&nbsp;Ruchira Wageesha<br /> What is the name of your organizational unit?<br /> [Unknown]:&nbsp;Mashup Server<br /> What is the name of your organization?<br /> [Unknown]:&nbsp;WSO2<br /> What is the name of your City or Locality?<br /> [Unknown]:&nbsp;Ahangama<br /> What is the name of your State or Province?<br /> [Unknown]:&nbsp;Southern<br /> What is the two-letter country code for this unit?<br /> [Unknown]:&nbsp;LK<br /> Is CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK correct?<br /> [no]: yes<br /> Enter key password for <br /> (RETURN if same as keystore password):</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Once you successfully completed this, java keytool will create a file named "server.jks". In the same way, you can create a client keystore named "client.jks" with the alias "client" using following command.</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt"><span style="font-weight:bold">keytool -genkey -alias client -keyalg RSA -keystore client.jks</span></p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Now, you have two files named client.jks and server.jks. You can view the content of these keystore files using the following command. Replacess "ruchira" with the keystore password you entered while creating the keystore.</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt"><span style="font-weight:bold">keytool -list -v -keystore server.jks -storepass ruchira</span></p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">This will list something like this.</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Keystore type: JKS<br /> Keystore provider: SUN</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Your keystore contains 1 entry</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Alias name: server<br /> Creation date: Jul 8, 2010<br /> Entry type: PrivateKeyEntry<br /> Certificate chain length: 1<br /> Certificate[1]:<br /> Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK<br /> Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK<br /> Serial number: 4c356225<br /> Valid from: Thu Jul 08 10:59:09 IST 2010 until: Wed Oct 06 10:59:09 IST 2010<br /> Certificate fingerprints:<br /> MD5:&nbsp;60:0B:48:0D:DB:56:8C:68:8C:2D:94:4A:D6:DA:04:B8<br /> SHA1: A7:CE:57:10:70:87:C1:2C:C0:9D:1D:90:8C:BB:69:B6:66:26:97:13<br /> Signature algorithm name: SHA1withRSA<br /> Version: 3</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">*******************************************<br /> *******************************************</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">The next step is, getting server's self signed public key certificate and storing it in client's keystore. And getting and storing client's self signed public key certificate in server's keystore. In order to do that, first we need to export both server and client public key certificates into files. Using the following command, you can export server's public key certificate into server.cert file and client's public key certificate into client.cert file.</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt"><span style="font-weight:bold">keytool -export -file server.cert -keystore server.jks -storepass ruchira -alias server</span><br /> <span style="font-weight:bold">keytool -export -file client.cert -keystore client.jks -storepass ruchira -alias client</span></p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Now you have server.cert and client.cert. You can use following commands to view certificate contents.</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt"><span style="font-weight:bold">keytool -printcert -v -file server.cert<br /> keytool -printcert -v -file client.cert</span></p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">As the last step, we need to import server.cert into client keystore and client.cert into server keystore. As I mentioned earlier, each entry of a Java Keystore is stored against an alias. So, we need to specify aliases here, which will be used to refer the certificates that we are going to store.</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt"><span style="font-weight:bold">keytool -import -file client.cert -keystore server.jks -storepass ruchira -alias client</span></p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Above command will store client's self signed public key certificate(client.cert) in server.jks against the alias "client". So, using "client" alias on server.jks, we can refer client's certificate anytime. Likewise, following command will store server.cert within client.jks against the alias "server".</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt"><span style="font-weight:bold">keytool -import -file server.cert -keystore client.jks -storepass ruchira -alias server</span></p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">After all, please view the content of both keystore again using following commands.</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt"><span style="font-weight:bold">keytool -list -v -keystore server.jks -storepass ruchira<br /> keytool -list -v -keystore client.jks -storepass ruchira</span></p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">It will give you something like bellow for server.jks</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Keystore type: JKS<br /> Keystore provider: SUN</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Your keystore contains 2 entries</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Alias name: server<br /> Creation date: Jul 8, 2010<br /> Entry type: PrivateKeyEntry<br /> Certificate chain length: 1<br /> Certificate[1]:<br /> Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK<br /> Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK<br /> Serial number: 4c3562a0<br /> Valid from: Thu Jul 08 11:01:12 IST 2010 until: Wed Oct 06 11:01:12 IST 2010<br /> Certificate fingerprints:<br /> MD5:&nbsp;AB:77:72:F1:0D:09:55:E3:B6:D3:DC:A6:4D:D4:39:36<br /> SHA1: D7:C1:60:5C:7E:34:40:A9:0B:E4:2C:65:6C:E0:79:7C:EE:37:A7:19<br /> Signature algorithm name: SHA1withRSA<br /> Version: 3</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">*******************************************<br /> *******************************************</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Alias name: client<br /> Creation date: Jul 8, 2010<br /> Entry type: trustedCertEntry</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK<br /> Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK<br /> Serial number: 4c356225<br /> Valid from: Thu Jul 08 10:59:09 IST 2010 until: Wed Oct 06 10:59:09 IST 2010<br /> Certificate fingerprints:<br /> MD5:&nbsp;60:0B:48:0D:DB:56:8C:68:8C:2D:94:4A:D6:DA:04:B8<br /> SHA1: A7:CE:57:10:70:87:C1:2C:C0:9D:1D:90:8C:BB:69:B6:66:26:97:13<br /> Signature algorithm name: SHA1withRSA<br /> Version: 3</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">*******************************************<br /> *******************************************</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">something like below for client.jks</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Keystore type: JKS<br /> Keystore provider: SUN</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Your keystore contains 2 entries</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Alias name: server<br /> Creation date: Jul 8, 2010<br /> Entry type: trustedCertEntry</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK<br /> Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK<br /> Serial number: 4c3562a0<br /> Valid from: Thu Jul 08 11:01:12 IST 2010 until: Wed Oct 06 11:01:12 IST 2010<br /> Certificate fingerprints:<br /> MD5:&nbsp;AB:77:72:F1:0D:09:55:E3:B6:D3:DC:A6:4D:D4:39:36<br /> SHA1: D7:C1:60:5C:7E:34:40:A9:0B:E4:2C:65:6C:E0:79:7C:EE:37:A7:19<br /> Signature algorithm name: SHA1withRSA<br /> Version: 3</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">*******************************************<br /> *******************************************</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">Alias name: client<br /> Creation date: Jul 8, 2010<br /> Entry type: PrivateKeyEntry<br /> Certificate chain length: 1<br /> Certificate[1]:<br /> Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK<br /> Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK<br /> Serial number: 4c356225<br /> Valid from: Thu Jul 08 10:59:09 IST 2010 until: Wed Oct 06 10:59:09 IST 2010<br /> Certificate fingerprints:<br /> MD5:&nbsp;60:0B:48:0D:DB:56:8C:68:8C:2D:94:4A:D6:DA:04:B8<br /> SHA1: A7:CE:57:10:70:87:C1:2C:C0:9D:1D:90:8C:BB:69:B6:66:26:97:13<br /> Signature algorithm name: SHA1withRSA<br /> Version: 3</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">*******************************************<br /> *******************************************</p>  <p style="margin:0in;font-family:Calibri;font-size:12.0pt">If everything went well, you might have successfully created server.jks and client.jks which can be used to secure Axis2 Services and access those secured services. </p>  </div><img src ="http://www.blogjava.net/dashi99/aggbug/381538.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dashi99/" target="_blank">ゞ沉默是金ゞ</a> 2012-06-26 19:15 <a href="http://www.blogjava.net/dashi99/archive/2012/06/26/381538.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>