文章转自:http://blog.csdn.net/gtuu0123/article/details/5827818
openssl在windows上的安装
从此处下载openssl for windows
http://gnuwin32.sourceforge.net/packages/openssl.htm
解压,并设置PATH环境变量指向其bin文件夹
下载openssl的配置文件http://www.securityfocus.com/data/tools/openssl.conf
并将其拷到一个文件夹下,以便用命令行指定,这里是c:/ssl/下 否则运行时会报Unable to load config info from /usr/local/ssl/openssl.cnf错误
此为路径错误error;
另一种方法:用绝对路径,后面用命令生成证书的时候配成绝对路径,可以避免.
一、建立目录
cd /home
mkdir ssl
cd ssl
mkdir ca
mkdir client
mkdir server
创建一个证书的步骤:
(1)生成系统私钥
(2)生成待签名证书
(3)生成x509证书, 用CA私钥进行签名
(4)导成浏览器支持的p12格式证书
二:生成CA证书
目前不使用第三方权威机构的CA来认证,自己充当CA的角色。
1. 创建私钥 :
openssl genrsa -out ca/ca-key.pem 1024
2.创建证书请求 :
openssl req -new -out ca/ca-req.csr -key ca/ca-key.pem
-----
Country Name (2 letter code) [AU]:cn
State or Province Name (full name) [Some-State]:bj
Locality Name (eg, city) []:bj
Organization Name (eg, company) [Internet Widgits Pty Ltd]:tb
Organizational Unit Name (eg, section) []:tb
Common Name (eg, YOUR name) []:ca
Email Address []:ca@ca.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
3.自签署证书 :
openssl x509 -req -in ca/ca-req.csr -out ca/ca-cert.pem -signkey ca/ca-key.pem -days 3650
4.将证书导出成浏览器支持的.p12格式 :
openssl pkcs12 -export -clcerts -in ca/ca-cert.pem -inkey ca/ca-key.pem -out ca/ca.p12
密码:123456
上面的信息随意填,密码6位,随意输入
三.生成server证书
1.创建私钥 :
openssl genrsa -out server/server-key.pem 1024
2.创建证书请求 :
openssl req -new -out server/server-req.csr -key server/server-key.pem
-----
Country Name (2 letter code) [AU]:cn
State or Province Name (full name) [Some-State]:bj
Locality Name (eg, city) []:bj
Organization Name (eg, company) [Internet Widgits Pty Ltd]:tb
Organizational Unit Name (eg, section) []:tb
Common Name (eg, YOUR name) []:localhost #此处一定要写服务器所在ip
Email Address []:server@server.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
3.自签署证书 :
openssl x509 -req -in server/server-req.csr -out server/server-cert.pem -signkey server/server-key.pem -CA ca/ca-cert.pem -CAkey ca/ca-key.pem -CAcreateserial -days 3650
4.将证书导出成浏览器支持的.p12格式 :
openssl pkcs12 -export -clcerts -in server/server-cert.pem -inkey server/server-key.pem -out server/server.p12
密码:123456
四.生成client证书
1.创建私钥 :
openssl genrsa -out client/client-key.pem 1024
2.创建证书请求 :
openssl req -new -out client/client-req.csr -key client/client-key.pem
-----
Country Name (2 letter code) [AU]:cn
State or Province Name (full name) [Some-State]:bj
Locality Name (eg, city) []:bj
Organization Name (eg, company) [Internet Widgits Pty Ltd]:tb
Organizational Unit Name (eg, section) []:tb
Common Name (eg, YOUR name) []:dong
Email Address []:dong@dong.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
3.自签署证书 :
openssl x509 -req -in client/client-req.csr -out client/client-cert.pem -signkey client/client-key.pem -CA ca/ca-cert.pem -CAkey ca/ca-key.pem -CAcreateserial -days 3650
4.将证书导出成浏览器支持的.p12格式 :
openssl pkcs12 -export -clcerts -in client/client-cert.pem -inkey client/client-key.pem -out client/client.p12
密码:123456
五.根据ca证书生成jks文件 (java keystore)
keytool -keystore truststore.jks -keypass 222222 -storepass 222222 -alias ca -import -trustcacerts -file ca/ca-cert.pem
六.配置tomcat ssl
修改conf/server.xml。tomcat6中多了SSLEnabled="true"属性。keystorefile, truststorefile设置为你正确的相关路径
xml 代码
tomcat 5.5的配置:
tomcat6.0的配置:
七、测试(linux下)
openssl s_client -connect localhost:8443 -cert /home/ssl/client/client-cert.pem -key /home/ssl/client/client-key.pem -tls1 -CAfile /home/ssl/ca/ca-cert.pem -state -showcerts
GET /index.jsp HTTP/1.0
八、导入证书
服务端导入server.P12 和ca.p12证书
客户端导入将ca.p12,client.p12证书
IE中(打开IE->;Internet选项->内容->证书)
ca.p12导入至受信任的根证书颁发机构,client.p12导入至个人
Firefox中(工具-选项-高级-加密-查看证书-您的证书)
将ca.p12和client.p12均导入这里
注意:ca,server,client的证书的common name(ca=ca,server=localhost,client=dong)一定不能重复,否则ssl不成功
此处应该非常注意,我就是犯了此错误导致证书不能被信任,直接被Tomcat 的SSL拦截
九、tomcat应用程序使用浏览器证书认证
在server/webapps/manager/WEB-INF/web.xml中,将BASIC认证改为证书认证
在conf/tomcat-users.xml中填入下列内容
这个我没配置,因为我在用分布式部署多个Tomcat,多个应用,跨地域,最近悲剧中
访问http://localhost:8443即可验证ssl是否成功
访问http://localhost:8443/manager/html可验证应用程序利用client证书验证是否成功