HTTPS证书制作和配置示例

今天在校内尝试从git.bistu.edu.cn克隆代码,使用https协议时候发现clone失败,于是开始实验,使用curl连接cas.bistu.edu.cn、x.bistu.edu.cn、chat.bistu.edu.cn、community.bistu.edu.cn、site.bistu.edu.cn
发现只有cas和site正常,其他的都报错了,但是浏览器访问并未发现错误。
联想之前ZFZ的safari访问git报错而我电脑的浏览器却没事,瞬间意识到之前配置的HTTPS可能很多都有问题。
经过一翻折腾,发现之前的补链不全导致有些浏览器无法识别完整的信任链,最终通过补链解决。
当Apache、IIS、Nginx都不报错之后,只有Tomcat还有问题。
Tomcat之前的jks使用的是http://www.agentbob.info/agentbob/79-AB.html这篇文章的工具生成的,说明这个工具有问题。
继续搜索发现可以使用
openssl s_client -msg -connect cas.bistu.edu.cn:8443
这个命令来调试详细的ssl连接过程
和正常的过程对比发现Tomcat的信任链依然不一样。遂放弃这个工具。
最终在文末的文章中找到了解决方案。
废话说完了,下面是完整操作记录。
—————————————————————–
拿到手的有bistu.crt证书一张、bistu.key私钥一个。
实际需要开启HTTPS的服务器若干,服务器种类为Tomcat、Nginx、Apache、IIS。
到证书签发机构官网下载root根证书和中间证书,在windows上另存为Base64PEM格式
分别为root.crt和intermediate.crt
拷贝到Linux下开始制作

补链后证书为bistu.chained.crt

生成bistu.jks为tomcat所需格式

openssl pkcs12 -export -out bistu.pfx -inkey bistu.key -in bistu.crt

生成bistu.pfx为IIS所需格式

至此所有文件如下
1.bistu.crt #原始证书
2.bistu.key #私钥
3.root.crt #根证书
4.intermediate.crt #中间证书
5.bistu.chained.crt #补链后证书
6.combined.crt #中间链+根证书
7.bistu.p12 #p12格式的证书库
8.bistu.jks #jks格式的证书库
9.bistu.pfx #pfx格式的证书库

Apache需要的:1、2、5
Nginx需要的:2、5
Tomcat需要的:8
IIS需要的:2、9

配置示例

Apache

Nginx配置示例

Tomcat配置示例

参考:
http://blog.csdn.net/jun55xiu/article/details/8980812
http://www.fourproc.com/2010/06/23/create-a-ssl-keystore-for-a-tomcat-server-using-openssl-.html
https://blogs.oracle.com/blogbypuneeth/entry/steps_to_create_a_jks

Apache+Tomcat8的HTTPS配置+反向代理

http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html

1.生成tomcat自签名证书
keytool -genkey -alias tomcat -keyalg RSA -keystore self.jks

Enter keystore password: 设置keystore密码
Re-enter new password: 设置keystore密码
What is your first and last name?
[Unknown]: localhost 输入localhost
What is the name of your organizational unit?
[Unknown]:
What is the name of your organization?
[Unknown]:
What is the name of your City or Locality?
[Unknown]:
What is the name of your State or Province?
[Unknown]:
What is the two-letter country code for this unit?
[Unknown]:
Is CN=localhost, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct?
[no]: yes

Enter key password for
(RETURN if same as keystore password): 直接回车

直接生成了self.jks

2.让系统信任这个证书
keytool -exportcert -alias tomcat -keystore self.jks -file self.cer
sz self.cer
传到windows上打开self.cer,点击详细信息->复制到文件->Base64编码X.509->另存为self.crt
rz
回传self.crt
sudo mkdir -p /usr/share/ca-certificates/tomcat
sudo cp self.crt /usr/share/ca-certificates/tomcat
sudo dpkg-reconfigure ca-certificates
第一步选yes,然后按空格选中self.crt,回车。

3.tomcat开启SSL
打开/etc/tomcat8/server.xml,修改这段如下

4.apache开启SSL模块和proxy模块
cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/proxy.load .
sudo ln -s ../mods-available/proxy_http.load .
sudo ln -s ../mods-available/ssl.load .

5.Apache反向代理配置

重启apache和tomcat即可