benx 发表于 2013-1-26 13:56:24

keytool简单使用和tomcat双向认证

 
本文将使用keytool和openssl工具来自己生成证书,包含CA,私钥,jks证书管理,配置tomcat的双向认证
 
1、安装openssl
# tar –zxvf openssl
# cd openssl
# ./config--prefix=/usr/local/openssl
# make
# make install
 
2、生成CA证书和私钥
opensslreq -new -x509 -days 18900 -sha1 -newkey rsa:1024 -keyout  ca_private.key -out  ca.crt 
正常情况下CA代表大家都可信任的机构,比如VeriSign机构,但是这里为了测试,自己生成一个CA
 
3、配置openssl
打开openssl_home/apps/openssl.cnf,找到如下部分
dir             = ./demoCA              # Where everything is kept
certs           = $dir/certs            # Where the issued certs are kept
crl_dir         = $dir/crl              # Where the issued crl are kept
database        = $dir/index.txt        # database index file.
#unique_subject= no                    # Set to 'no' toallow creation of
                                        #several ctificates with same subject.
new_certs_dir   = $dir/newcerts         # default place for new certs.
 
certificate     = $dir/ca.crt       # The CA certificate
serial          = $dir/serial           # The current serial number
crlnumber       = $dir/crlnumber        # the current crl number
                                        # mustbe commented out to leave a V1 CRL
crl             = $dir/crl.pem          # The current CRL
private_key     = $dir/private/ca_private.key# The privatekey
RANDFILE        = $dir/private/.rand    # private random number file
 
 
修改certificate     和private_key     这两个分别代表CA和私钥,修改为你自己的CA和私钥文件
database        ,每生成一个证书,这里会有记录,如果重复生成,可以把之前生成记录删除
new_certs_dir  ,这个是证书生成后保存地址
 
 
4、创建服务器证书cn要和域名一样在keystore是keyentry 
 keytool-genkey -validity 3650 -keyalg RSA  -alias server -keysize 1024 -keystoretomcat.store       
<!---->
<!---->
 
5、创建服务证书请求
keytool-certreq -alias server -sigalg MD5withRSA -file server.csr -keystore tomcat.store    
 
6、用ca证书和私钥签名服务器证书请求(一般由CA中心认证,这里用我自己生成的CA和私钥来签名) 
openssl ca -in server.csr -out tomcat_server.crt -notext -configopenssl.cnf     
在使用这个命令之前特别主要在openssl中已经配置了ca和private,在生成过程中如果发现某个文件不存在,创建它即可
 
7、把ca导入到keystore,当作信任证书 在keystore是trustedCertEntry 
keytool-import -v -trustcacerts -alias mas_ca -file ca.crt -keystoretomcat.store               
trustcacerts就是说,在浏览器使用的证书如果要访问该网站,证书必须是由该CA生成或CA的子证书签名生成
 
8、导入服务器证书到keystore中
keytool-import -v -file tomcat_server.crt -alias server -keystore tomcat.store 
把服务器导入到keystore,此时的keyEntry是有证书链的.如果直接使用keytool的import类型为trustedCertEntry,使用keytool的genkey这时是keyEntry,但是没有证书链,如果要要有证书链可以如上,先签名后用ca认证,再导入ca证书,再import 
 
查看keystore
C:\key>keytool-v -list -keystore tomcat.store
输入keystore密码: mas123
 
Keystore类型: jks
Keystore提供者: SUN
 
您的 keystore 包含 2 输入
 
别名名称: mas_ca
创建日期: 2011-5-19
输入类型: trustedCertEntry
 
Owner:CN=xiongjin, OU=99bill, O=99bill, ST=shanghai, C=cn
发照者: EMAILADDRESS=xiongjin1983@gmail.com, CN=localhost, OU=99bill,O=99bill, L=shanghai, ST=shanghai, C=cn
序号: 11f
有效期间: Thu May 19 14:01:09 CST 2011 至: Fri May 1814:01:09 CST 2012
认证指纹:
         MD5: BD:5F:F2:27:BD:A7:EA:B1:92:D9:D9:06:21:F2:C6:C2
         SHA1:15:86:43:C2:49:B9:CA:60:5F:91:BB:E1:5B:39:91:37:78:F0:9E:66
 
 
*******************************************
*******************************************
 
 
别名名称: server
创建日期: 2011-5-19
输入类型:KeyEntry
认证链长度: 2
认证 :
Owner:CN=xiongjin, OU=99bill, O=99bill, ST=shanghai, C=cn
发照者: EMAILADDRESS=xiongjin1983@gmail.com, CN=localhost, OU=99bill,O=99bill, L=shanghai, ST=shanghai, C=cn
序号: 11f
有效期间: Thu May 19 14:01:09 CST 2011 至: Fri May 1814:01:09 CST 2012
认证指纹:
         MD5: BD:5F:F2:27:BD:A7:EA:B1:92:D9:D9:06:21:F2:C6:C2
         SHA1:15:86:43:C2:49:B9:CA:60:5F:91:BB:E1:5B:39:91:37:78:F0:9E:66
认证 :
Owner:EMAILADDRESS=xiongjin1983@gmail.com, CN=localhost, OU=99bill, O=99bill,L=shanghai, ST=shanghai, C=cn
发照者: EMAILADDRESS=xiongjin1983@gmail.com, CN=localhost, OU=99bill,O=99bill, L=shanghai, ST=shanghai, C=cn
序号: f71d6ea47619bb8f
有效期间: Thu May 19 09:52:00 CST 2011 至: Mon Jan 1003:29:36 CST 1927
认证指纹:
         MD5: C2:E0:B1:FD:66:0D:BD:16:D2:2C:C1:87:8A:01:68:16
         SHA1:21:38:9A:28:A0:95:E6:1E:13:42:85:8E:BB:B9:99:4B:8A:0B:0F:06
 
 
*******************************************
*******************************************
 
9、配置tomcat双向认证打开tomcat的 server.xml文件,修改配置 
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" 
              maxThreads="150" scheme="https"secure="true" 
              clientAuth="false" sslProtocol="TLS" 
              keystoreFile="conf/tomcat.store"keystorePass="mas123" 
              truststoreFile="conf/tomcat.store" truststorePass="mas123"/> 
 
 
 
使用httpClient访问ssl
 
//第一个URL对应的keystore为keystore,里面包含客户端证书
//第二个URL对应的keyStore为truststore,基本和服务器的truststore一致
Protocolauthhttps = new Protocol("https", new AuthSSLProtocolSocketFactory(newURL("file:c:/tomcat.store"), "mas123", new URL("file:c:/tomcat.store"),"mas123"), 8443);
 
HttpClientclient = new HttpClient();
client.getHostConfiguration().setHost("localhost",8443, authhttps);
PostMethodhttpget = new PostMethod("/");
inti = client.executeMethod(httpget);
StringrespXml = new String(httpget.getResponseBody(), "UTF8");
System.out.println(respXml);
页: [1]
查看完整版本: keytool简单使用和tomcat双向认证