背景
由于ios將在2017年1月1日起強(qiáng)制實(shí)施ATS安全策略碎紊,所有通訊必須使用https傳輸,本文只針對自制證書鸣个,但目前尚不確定自制證書是否能通過appstore審核帅矗。
1、必須支持傳輸層安全(TLS)協(xié)議1.2以上版本
2科吭、證書必須使用SHA256或更高的哈希算法簽名
3昏滴、必須使用2048位以上RSA密鑰或256位以上ECC算法等等
4、證書必須是V3版本
以上是幾個(gè)注意點(diǎn)对人。主要針對ios的ATS策略
環(huán)境
linux: CentOS6.8
tomcat: Apache Tomcat/7.0.63
OpenSSL: OpenSSL 1.1.0c
OpenSSL升級(jí)(如果需要)
我使用的是阿里云服務(wù)器谣殊,linux自帶OpenSSL,只需要做一次升級(jí)牺弄,關(guān)于全新安裝請自行搜索姻几。
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命令軟連到對應(yīng)位置
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
創(chuàng)建證書目錄
//進(jìn)入tmp目錄
cd /tmp
//創(chuàng)建ca目錄,存放證書相關(guān)文件
mkdir ca
//進(jìn)入ca
cd ca
制作根證書
1. 創(chuàng)建根證書密鑰文件(自己做CA) root.key
openssl genrsa -des3 -out root.key 2048
輸出內(nèi)容為:
Generating RSA private key, 2048 bit long modulus
.....................................................................................................................+++
..........................+++
e is 65537 (0x010001)
Enter pass phrase for root.key: ← 輸入一個(gè)新密碼
Verifying – Enter pass phrase for root.key: ← 重新輸入一遍密碼
2. 創(chuàng)建根證書的申請文件 root.csr
openssl req -new -key root.key -out root.csr
輸出內(nèi)容為:
Enter pass phrase for root.key: ← 輸入前面創(chuàng)建的密碼
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 ← 國家代號(hào)势告,中國輸入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) []: ← 此時(shí)不輸入
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. 創(chuàng)建一個(gè)自當(dāng)前日期起為期十年的根證書 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
輸出內(nèi)容為:
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: ← 輸入前面創(chuàng)建的密碼
4.根據(jù)CA證書生成truststore JKS文件 root.truststore
//這一步只針對雙向認(rèn)證络拌,單向不需要
keytool -keystore root.truststore -keypass 123456 -storepass 123456 -alias ca -import -trustcacerts -file /tmp/ca/root.crt
鍵入回事后,提示是否信息此證書回溺,輸入yes, 則生成truststore成功
制作service服務(wù)器端證書
1.創(chuàng)建服務(wù)器證書密鑰 server.key
openssl genrsa -des3 -out server.key 2048
輸出內(nèi)容為:
Generating RSA private key, 2048 bit long modulus
...........................+++
...............+++
e is 65537 (0x010001)
Enter pass phrase for server.key: ← 輸入前面創(chuàng)建的密碼
Verifying - Enter pass phrase for server.key: ← 重新輸入一遍密碼
運(yùn)行時(shí)會(huì)提示輸入密碼,此密碼用于加密key文件(參數(shù)des3便是指加密算法,當(dāng)然也可以選用其他你認(rèn)為安全的算法.),以后每當(dāng)需讀取此文件(通過openssl提供的命令或API)都需輸入口令.如果覺得不方便,也可以去除這個(gè)口令,但一定要采取其他的保護(hù)措施!
去除key文件口令的命令:
openssl rsa -in server.key -out server.key
2.創(chuàng)建服務(wù)器證書的申請文件 server.csr
openssl req -new -key server.key -out server.csr
輸出內(nèi)容為:
Enter pass phrase for server.key: ← 輸入前面創(chuàng)建的密碼
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 ← 服務(wù)器主機(jī)名(或者IP)祥诽,若填寫不正確,瀏覽器會(huì)報(bào)告證書無效瓮恭,但并不影響使用
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.創(chuàng)建自當(dāng)前日期起有效期為期十年的服務(wù)器證書 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
輸出內(nèi)容為:
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: ← 輸入前面創(chuàng)建的密碼
4.導(dǎo)出.p12文件 server.p12
openssl pkcs12 -export -in /tmp/ca/server.crt -inkey /tmp/ca/server.key -out /tmp/ca/server.p12 -name "server"
根據(jù)命令提示,輸入server.key密碼屯蹦,創(chuàng)建p12密碼维哈。
5.將.p12 文件導(dǎo)入到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.創(chuàng)建客戶端證書密鑰文件 client.key
openssl genrsa -des3 -out client.key 2048
輸出內(nèi)容為:
Generating RSA private key, 2048 bit long modulus
...............................+++
.........................+++
e is 65537 (0x010001)
Enter pass phrase for client.key: ← 輸入一個(gè)新密碼
Verifying – Enter pass phrase for client.key: ← 重新輸入一遍密碼
2.創(chuàng)建客戶端證書的申請文件 client.csr
openssl req -new -key client.key -out client.csr
輸出內(nèi)容為:
Enter pass phrase for client.key: ← 輸入上一步中創(chuàng)建的密碼
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.創(chuàng)建一個(gè)自當(dāng)前日期起有效期為十年的客戶端證書 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
輸出內(nèi)容為:
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: ← 輸入上面創(chuàng)建的密碼
4.導(dǎo)出.p12文件 client.p12
openssl pkcs12 -export -in /tmp/ca/client.crt -inkey /tmp/ca/client.key -out /tmp/ca/client.p12 -name "client"
根據(jù)命令提示购撼,輸入client.key密碼跪削,創(chuàng)建p12密碼。
- 解釋
名稱 | |
---|---|
crt證書 | 只含有公鑰 |
p12證書 | 是包含證書(含公鑰)和私鑰 |
JKS(Java key store) | 存放密鑰的容器迂求。.jks .keystore .truststore等 |
KeyStore | 服務(wù)器的密鑰存儲(chǔ)庫碾盐,存服務(wù)器的公鑰私鑰證書 |
TrustStore | 服務(wù)器的信任密鑰存儲(chǔ)庫,存CA公鑰 |
- 單向認(rèn)證需要文件
名稱 | |
---|---|
root.crt | 客戶端使用的CA根證書 |
server.keystore | 服務(wù)端證書存放的容器 |
- 雙向認(rèn)證需要文件
名稱 | |
---|---|
root.crt | 客戶端使用的CA根證書 |
client.p12 | 客戶端證書包含私鑰 |
root.truststore | CA公鑰存放到受信賴的容器 |
server.keystore | 服務(wù)端證書存放的容器 |
單向認(rèn)證
客戶端只需要安裝root.crt這個(gè)CA根證書
服務(wù)器端配置server.keystore
配置Tomcat
1.關(guān)閉tomcat
tomcat的bin目錄下執(zhí)行
shutdown.sh
2.將keystore文件(server.keystore) 放在web服務(wù)器上
cp /tmp/ca/server.keystore /你的tomcat根目錄/conf
3.修改server.xml配置文件
cd /你的tomcat根目錄/conf
vi server.xml
找到下面被注釋的代碼揩局,刪除注釋符并修改內(nèi)容(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.啟動(dòng)tomcat
tomcat的bin目錄下執(zhí)行
startup.sh
5.訪問https服務(wù)
https://localhost:8443/
https://192.168.1.1:8443/ 你的IP
雙向認(rèn)證
客戶端需要安裝root.crt這個(gè)CA根證書毫玖,client.p12這個(gè)客戶端證書
服務(wù)器端配置server.keystore,root.truststore
配置Tomcat
1.關(guān)閉tomcat
tomcat的bin目錄下執(zhí)行
shutdown.sh
2.將keystore文件(server.keystore) 放在web服務(wù)器上
cp /tmp/ca/server.keystore /你的tomcat根目錄/conf
將truststore文件(root.truststore) 放在web服務(wù)器上
cp /tmp/ca/root.truststore /你的tomcat根目錄/conf
3.修改server.xml配置文件
cd /你的tomcat根目錄/conf
vi server.xml
找到下面被注釋的代碼凌盯,刪除注釋符并修改內(nèi)容(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.啟動(dòng)tomcat
tomcat的bin目錄下執(zhí)行
startup.sh
5.訪問https服務(wù)
https://localhost:8443/
https://192.168.1.1:8443/ 你的IP
完成励背。可以讓你的前端通過https協(xié)議訪問你的接口了砸西,注意此時(shí)的接口是8443.
ios開發(fā)
請參考下面文章
https請求之iOS客戶端---AFNetworking
參考文章
1.SSL證書生成方法
2.Tomcat6配置使用SSL雙向認(rèn)證(使用openssl生成證書)
3.Linux下生成https自簽名證書叶眉,解決蘋果發(fā)布問題重新整理
4.用tomcat配置https自簽名證書,解決 ios7.1以上系統(tǒng), 蘋果inHouse發(fā)布