linux下Tomcat+OpenSSL配置單向&雙向認(rèn)證(自制證書)

背景

由于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ā)布


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末芹枷,一起剝皮案震驚了整個(gè)濱河市衅疙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌鸳慈,老刑警劉巖饱溢,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異走芋,居然都是意外死亡绩郎,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進(jìn)店門翁逞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來肋杖,“玉大人,你說我怎么就攤上這事挖函∽粗玻” “怎么了?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵怨喘,是天一觀的道長津畸。 經(jīng)常有香客問我,道長必怜,這世上最難降的妖魔是什么肉拓? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮梳庆,結(jié)果婚禮上暖途,老公的妹妹穿的比我還像新娘卑惜。我一直安慰自己,他們只是感情好丧肴,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布残揉。 她就那樣靜靜地躺著胧后,像睡著了一般芋浮。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上壳快,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天纸巷,我揣著相機(jī)與錄音,去河邊找鬼眶痰。 笑死瘤旨,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的竖伯。 我是一名探鬼主播存哲,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼七婴!你這毒婦竟也來了祟偷?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤打厘,失蹤者是張志新(化名)和其女友劉穎修肠,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體户盯,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡嵌施,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了莽鸭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吗伤。...
    茶點(diǎn)故事閱讀 40,096評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖硫眨,靈堂內(nèi)的尸體忽然破棺而出牲芋,到底是詐尸還是另有隱情,我是刑警寧澤捺球,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布缸浦,位于F島的核電站,受9級(jí)特大地震影響氮兵,放射性物質(zhì)發(fā)生泄漏裂逐。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一泣栈、第九天 我趴在偏房一處隱蔽的房頂上張望卜高。 院中可真熱鬧弥姻,春花似錦、人聲如沸掺涛。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽薪缆。三九已至秧廉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間拣帽,已是汗流浹背疼电。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留减拭,地道東北人蔽豺。 一個(gè)月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像拧粪,于是被迫代替她去往敵國和親修陡。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容