摘要:實際情況中,內(nèi)部系統(tǒng)的互相通信使用,往往不可能向公有申請證書申請證書需要很高的費用,故我們需要創(chuàng)建一個私有來申請證書實現(xiàn)通信。
前言
https相較于http而言有很大的安全性,當(dāng)我們一個服務(wù)開啟https并與之通信時,往往需要證書的認證,如果是瀏覽器訪問服務(wù),只要在瀏覽器內(nèi)設(shè)置信任證書即可,而如果是程序內(nèi)訪問服務(wù)(如java程序),則需要導(dǎo)入該服務(wù)的證書所信任的證書。
實際情況中,內(nèi)部系統(tǒng)的互相通信使用https,往往不可能向公有CA申請證書(申請證書需要很高的費用),故我們需要創(chuàng)建一個私有CA來申請證書實現(xiàn)https通信。
名詞介紹 CA和證書認證詳見我的一篇博文:https簡單解讀
實現(xiàn)步驟 環(huán)境介紹64位 centos 7.X操作系統(tǒng)
裝有openssl命令
構(gòu)建私有CACA要給別人簽發(fā)證書,首先自己得有一個作為根證書,我們得在一切工作之前修改好CA的配置文件、序列號、索引等等。
輸入以下命令更改配置文件:
vi /etc/pki/tls/openssl.cnf
配置文件更改以下部分:
[ CA_default ] dir = /etc/pki/CA # Where everything is kept certs = $dir/certs # Where the issued certs are kept crl_dir = $dir/crl # Where the issued crl are kept database = $dir/index.txt # database index file. #unique_subject = no # Set to "no" to allow creation of # several ctificates with same subject. new_certs_dir = $dir/newcerts # default place for new certs. certificate = $dir/cacert.pem # The CA certificate serial = $dir/serial # The current serial number crlnumber = $dir/crlnumber # the current crl number # must be commented out to leave a V1 CRL crl = $dir/crl.pem # The current CRL private_key = $dir/private/cakey.pem # The private key RANDFILE = $dir/private/.rand # private random number file ... default_days = 3650 # how long to certify for ... # For the CA policy [ policy_match ] countryName = match stateOrProvinceName = optional localityName = optional organizationName = optional organizationalUnitName = optional commonName = supplied emailAddress = optional
在/etc/pki/CA目錄創(chuàng)建兩個文件index.txt和serial:
cd /etc/pki/CA && touch index.txt serial && echo 01 > serial
仍在當(dāng)前目錄下生成一個CA私鑰cakey.pem和自簽證書cacert.pem:
openssl genrsa -out private/cakey.pem 2048 openssl req -new -x509 -key private/cakey.pem -out cacert.pem
生成公鑰的時候會提示輸入一些信息,例子如下:
Country Name (2 letter code) []:CN #國家名 State or Province Name (full name) []:hangzhou #省份名 Locality Name (eg, city) []:hangzhou #地名 Organization Name (eg, company) []:company #公司名 Organizational Unit Name (eg, section) []:unit #部門名 Common Name (eg, your websites domain name) []:localhost #服務(wù)域名 Email Address []: #電子郵件
后面一些信息可按回車略過
這里比較重要的是Comman Name填寫的是服務(wù)的域名地址,即如果該證書用于某個服務(wù)則填該服務(wù)的域名地址(如用于百度服務(wù)器,則填寫www.baidu.com)
本方案的CA證書不用于某個服務(wù),故可填localhost
私有CA簽署證書為一個服務(wù)生成私鑰server.key和一個證書請求文件server.csr:
openssl genrsa -out server.key 2048 openssl req -new -key server.key -out server.csr
生成證書請求文件時,仍會提示輸入一些信息,例子如下:
Country Name (2 letter code) []:CN #國家名 State or Province Name (full name) []:hangzhou #省份名 Locality Name (eg, city) []:hangzhou #地名 Organization Name (eg, company) []:company #公司名 Organizational Unit Name (eg, section) []:unit #部門名 Common Name (eg, your websites domain name) []:XXX.XXX.XXX #服務(wù)域名 Email Address []: #電子郵件
這里的Common Name就應(yīng)該填你實際服務(wù)所用的域名了
下面將該證書請求文件server.csr由你構(gòu)建的私有CA簽署,生成一個server.crt證書:
openssl x509 -req -in server.csr -CA /etc/pki/CA/cacert.pem -CAkey /etc/pki/CA/private/cakey.pem -CAcreateserial -out server.crt
到此為止,server.crt證書可用于服務(wù)提供https訪問,客戶端若想訪問該服務(wù),導(dǎo)入CA根證書cacert.pem即可。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/72734.html
摘要:常見的對稱加密算法密鑰長度可選等非對稱加密非對稱加密,密鑰成對出現(xiàn),一公一私。申請者將自己的公鑰和個人站點信息發(fā)送給,請求其做認證。定義了證書的結(jié)構(gòu)以及認證協(xié)議標(biāo)準,目前使用的是第三版。 對稱加密 對稱加密中加密和解密使用相同的密鑰,加解密速度快,算法公開,計算量小。 showImg(https://segmentfault.com/img/bVbp5uW); 使用對稱加密,交易雙方都...
閱讀 2152·2023-04-25 22:58
閱讀 1488·2021-09-22 15:20
閱讀 2771·2019-08-30 15:56
閱讀 2116·2019-08-30 15:54
閱讀 2239·2019-08-29 12:31
閱讀 2843·2019-08-26 13:37
閱讀 672·2019-08-26 13:25
閱讀 2175·2019-08-26 11:58