linux下Tomcat+OpenSSL配置单向&双向认证(自制证书)

背景

由于ios将在2017年1月1日起强制实施ATS安全策略,所有通讯必须使用https传输,本文只针对自制证书,但目前尚不确定自制证书是否能通过appstore审核。

1、必须支持传输层安全(TLS)协议1.2以上版本
2、证书必须使用SHA256或更高的哈希算法签名
3、必须使用2048位以上RSA密钥或256位以上ECC算法等等
4、证书必须是V3版本
以上是几个注意点。主要针对ios的ATS策略


环境

linux: CentOS6.8
tomcat: Apache Tomcat/7.0.63
OpenSSL: OpenSSL 1.1.0c

OpenSSL升级(如果需要)

我使用的是阿里云服务器,linux自带OpenSSL,只需要做一次升级,关于全新安装请自行搜索。

1.查看版本
openssl version -a
2.更新zlib
yum install -y zlib
3.下载(注意cd到自己需要的路径下)
wget https://www.openssl.org/source/openssl-1.1.0c.tar.gz
4.解压安装
tar zxf openssl-1.1.0c.tar.gz
cd openssl-1.1.0c
./config --prefix=/usr/local/openssl
make
make install
//重命名原来的openssl
mv /usr/bin/openssl /usr/bin/openssl.ori
mv /usr/include/openssl /usr/include/openssl.ori
//将安装好的openssl命令软连到对应位置
ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
//在/etc/ld.so.conf文件中写入openssl库文件的搜索路径
echo /usr/local/openssl/lib >> /etc/ld.so.conf
ldconfig -v
openssl version -a

创建证书目录

//进入tmp目录
cd /tmp
//创建ca目录,存放证书相关文件
mkdir ca
//进入ca
cd ca

制作根证书

1. 创建根证书密钥文件(自己做CA) root.key
openssl genrsa -des3 -out root.key 2048
输出内容为:
Generating RSA private key, 2048 bit long modulus
.....................................................................................................................+++
..........................+++
e is 65537 (0x010001)
Enter pass phrase for root.key: ← 输入一个新密码 
Verifying – Enter pass phrase for root.key: ← 重新输入一遍密码
2. 创建根证书的申请文件 root.csr
openssl req -new -key root.key -out root.csr
输出内容为:
Enter pass phrase for root.key: ← 输入前面创建的密码 
You are about to be asked to enter information that will be incorporated 
into your certificate request. 
What you are about to enter is what is called a Distinguished Name or a DN. 
There are quite a few fields but you can leave some blank 
For some fields there will be a default value, 
If you enter ‘.’, the field will be left blank. 
—– 
Country Name (2 letter code) [AU]:CN ← 国家代号,中国输入CN 
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音 
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 
Organizational Unit Name (eg, section) []: ← 可以不输入 
Common Name (eg, YOUR name) []: ← 此时不输入 
Email Address []:admin@mycompany.com ← 电子邮箱,可随意填

Please enter the following ‘extra’ attributes 
to be sent with your certificate request 
A challenge password []: ← 可以不输入 
An optional company name []: ← 可以不输入
3. 创建一个自当前日期起为期十年的根证书 root.crt
openssl x509 -req -days 3650 -sha256 -extfile /usr/local/openssl/ssl/openssl.cnf -extensions v3_ca -signkey root.key -in root.csr -out root.crt
输出内容为:
Signature ok 
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./emailAddress=admin@mycompany.com
Getting Private key 
Enter pass phrase for root.key: ← 输入前面创建的密码
4.根据CA证书生成truststore JKS文件 root.truststore
//这一步只针对双向认证,单向不需要
keytool -keystore root.truststore -keypass 123456 -storepass 123456 -alias ca -import -trustcacerts -file /tmp/ca/root.crt
键入回事后,提示是否信息此证书,输入yes, 则生成truststore成功

制作service服务器端证书

1.创建服务器证书密钥 server.key
openssl genrsa -des3 -out server.key 2048
输出内容为:
Generating RSA private key, 2048 bit long modulus
...........................+++
...............+++
e is 65537 (0x010001)
Enter pass phrase for server.key: ← 输入前面创建的密码
Verifying - Enter pass phrase for server.key: ← 重新输入一遍密码
运行时会提示输入密码,此密码用于加密key文件(参数des3便是指加密算法,当然也可以选用其他你认为安全的算法.),以后每当需读取此文件(通过openssl提供的命令或API)都需输入口令.如果觉得不方便,也可以去除这个口令,但一定要采取其他的保护措施! 
去除key文件口令的命令: 
openssl rsa -in server.key -out server.key
2.创建服务器证书的申请文件 server.csr
openssl req -new -key server.key -out server.csr
输出内容为:
Enter pass phrase for server.key: ← 输入前面创建的密码
You are about to be asked to enter information that will be incorporated 
into your certificate request. 
What you are about to enter is what is called a Distinguished Name or a DN. 
There are quite a few fields but you can leave some blank 
For some fields there will be a default value, 
If you enter ‘.’, the field will be left blank. 
—– 
Country Name (2 letter code) [AU]:CN ← 国家名称,中国输入CN 
State or Province Name (full name) [Some-State]:BeiJing ← 省名,拼音 
Locality Name (eg, city) []:BeiJing ← 市名,拼音 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 
Organizational Unit Name (eg, section) []: ← 可以不输入 
Common Name (eg, YOUR name) []:www.xxx.com ← 服务器主机名(或者IP),若填写不正确,浏览器会报告证书无效,但并不影响使用
Email Address []:admin@mycompany.com ← 电子邮箱,可随便填

Please enter the following ‘extra’ attributes 
to be sent with your certificate request 
A challenge password []: ← 可以不输入 
An optional company name []: ← 可以不输入
3.创建自当前日期起有效期为期十年的服务器证书 server.crt
openssl x509 -req -days 3650 -sha256 -extfile /usr/local/openssl/ssl/openssl.cnf -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in server.csr -out server.crt
输出内容为:
Signature ok 
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com 
Getting CA Private Key 
Enter pass phrase for root.key: ← 输入前面创建的密码
4.导出.p12文件 server.p12
openssl pkcs12 -export -in /tmp/ca/server.crt -inkey /tmp/ca/server.key -out  /tmp/ca/server.p12 -name "server"
根据命令提示,输入server.key密码,创建p12密码。
5.将.p12 文件导入到keystore JKS文件 server.keystore
keytool -importkeystore -v -srckeystore  /tmp/ca/server.p12 -srcstoretype pkcs12 -srcstorepass 123456 -destkeystore /tmp/ca/server.keystore -deststoretype jks -deststorepass 123456
这里srcstorepass后面的123456为server.p12的密码deststorepass后的123456为keyStore的密码

制作client客户端证书

1.创建客户端证书密钥文件 client.key
openssl genrsa -des3 -out client.key 2048
输出内容为:
Generating RSA private key, 2048 bit long modulus
...............................+++
.........................+++
e is 65537 (0x010001)
Enter pass phrase for client.key: ← 输入一个新密码 
Verifying – Enter pass phrase for client.key: ← 重新输入一遍密码
2.创建客户端证书的申请文件 client.csr
openssl req -new -key client.key -out client.csr
输出内容为:
Enter pass phrase for client.key: ← 输入上一步中创建的密码 
You are about to be asked to enter information that will be incorporated 
into your certificate request. 
What you are about to enter is what is called a Distinguished Name or a DN. 
There are quite a few fields but you can leave some blank 
For some fields there will be a default value, 
If you enter ‘.’, the field will be left blank. 
—– 
Country Name (2 letter code) [AU]:CN ← 国家名称,中国输入CN 
State or Province Name (full name) [Some-State]:BeiJing ← 省名称,拼音 
Locality Name (eg, city) []:BeiJing ← 市名称,拼音 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 
Organizational Unit Name (eg, section) []: ← 可以不填 
Common Name (eg, YOUR name) []:Lenin ← 自己的英文名,可以随便填 
Email Address []:admin@mycompany.com ← 电子邮箱,可以随便填

Please enter the following ‘extra’ attributes 
to be sent with your certificate request 
A challenge password []: ← 可以不填 
An optional company name []: ← 可以不填
3.创建一个自当前日期起有效期为十年的客户端证书 client.crt
openssl x509 -req -days 3650 -sha256 -extfile /usr/local/openssl/ssl/openssl.cnf -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in client.csr -out client.crt
输出内容为:
Signature ok 
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com 
Getting CA Private Key 
Enter pass phrase for root.key: ← 输入上面创建的密码
4.导出.p12文件 client.p12
openssl pkcs12 -export -in /tmp/ca/client.crt -inkey /tmp/ca/client.key -out  /tmp/ca/client.p12 -name "client"
根据命令提示,输入client.key密码,创建p12密码。

  • 解释
名称
crt证书 只含有公钥
p12证书 是包含证书(含公钥)和私钥
JKS(Java key store) 存放密钥的容器。.jks .keystore .truststore等
KeyStore 服务器的密钥存储库,存服务器的公钥私钥证书
TrustStore 服务器的信任密钥存储库,存CA公钥

  • 单向认证需要文件
名称
root.crt 客户端使用的CA根证书
server.keystore 服务端证书存放的容器
  • 双向认证需要文件
名称
root.crt 客户端使用的CA根证书
client.p12 客户端证书包含私钥
root.truststore CA公钥存放到受信赖的容器
server.keystore 服务端证书存放的容器

单向认证

客户端只需要安装root.crt这个CA根证书
服务器端配置server.keystore

配置Tomcat

1.关闭tomcat
tomcat的bin目录下执行
shutdown.sh
2.将keystore文件(server.keystore) 放在web服务器上
cp /tmp/ca/server.keystore /你的tomcat根目录/conf
3.修改server.xml配置文件
cd /你的tomcat根目录/conf
vi server.xml
找到下面被注释的代码,删除注释符并修改内容(vi命令操作)
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               keystoreFile="/你的tomcat根目录/conf/server.keystore"
               keystorePass="123456"
               clientAuth="false" sslEnabledProtocols="TLSv1.2"
               ciphers="TLS_RSA_WITH_AES_128_CBC_SHA,
                               TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
                               TLS_RSA_WITH_AES_128_CBC_SHA256,
                               TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
                               TLS_RSA_WITH_3DES_EDE_CBC_SHA,
                               TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA" />
4.启动tomcat
tomcat的bin目录下执行
startup.sh
5.访问https服务
https://localhost:8443/
https://192.168.1.1:8443/  你的IP

双向认证

客户端需要安装root.crt这个CA根证书,client.p12这个客户端证书
服务器端配置server.keystore,root.truststore

配置Tomcat

1.关闭tomcat
tomcat的bin目录下执行
shutdown.sh
2.将keystore文件(server.keystore) 放在web服务器上
cp /tmp/ca/server.keystore /你的tomcat根目录/conf
  将truststore文件(root.truststore) 放在web服务器上
cp /tmp/ca/root.truststore /你的tomcat根目录/conf
3.修改server.xml配置文件
cd /你的tomcat根目录/conf
vi server.xml
找到下面被注释的代码,删除注释符并修改内容(vi命令操作)
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               keystoreFile="/你的tomcat根目录/conf/server.keystore"
               keystorePass="123456"
               truststoreFile="/你的tomcat根目录/conf/root.truststore"
               truststorePass="123456"
               clientAuth="true" sslEnabledProtocols="TLSv1.2"
               ciphers="TLS_RSA_WITH_AES_128_CBC_SHA,
                               TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
                               TLS_RSA_WITH_AES_128_CBC_SHA256,
                               TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
                               TLS_RSA_WITH_3DES_EDE_CBC_SHA,
                               TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA" />
注意!clientAuth为true。这里和单向的不同。
4.启动tomcat
tomcat的bin目录下执行
startup.sh
5.访问https服务
https://localhost:8443/
https://192.168.1.1:8443/  你的IP

完成。可以让你的前端通过https协议访问你的接口了,注意此时的接口是8443.


ios开发

请参考下面文章
https请求之iOS客户端---AFNetworking


参考文章

1.SSL证书生成方法
2.Tomcat6配置使用SSL双向认证(使用openssl生成证书)
3.Linux下生成https自签名证书,解决苹果发布问题重新整理
4.用tomcat配置https自签名证书,解决 ios7.1以上系统, 苹果inHouse发布


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容