一、OpenSSL:CA默認配置信息
1.證書簽發機構CA:公共信任CA、私有CA
建立私有CA方式如下:
小范圍測試使用openssl、
大范圍維護大量證書企業使用OpenCA(對openssl進行了二次封裝,更加方便使用)
2.openssl 配置文件:/etc/pki/tls/openssl.cnf
[root@localhost tmp]# cat /etc/pki/tls/openssl.cnf
該配置文件中以 "[配置段]",的形式配置相關信息
===============================================================================================
==================================== openssl.cnf部分內容摘要=====================================
# OpenSSL example configuration file.
# This is mostly being used for generation of certificate requests.
######################################################################################
[ ca ] #CA相關配置段
default_ca = CA_default # The default ca section # 默認CA在[ CA_default ]配置
######################################################################################
[ CA_default ] # 默認當做CA的工作環境
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.頒發過的證書索引文件
new_certs_dir = $dir/newcerts # default place for new certs.
certificate = $dir/cacert.pem # The CA certificate指明CA的自簽證書
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,CA自己的私鑰
RANDFILE = $dir/private/.rand # private random number file
x509_extensions = usr_cert # The extentions to add to the cert
# Comment out the following two lines for the "traditional"
# (and highly broken) format.
name_opt = ca_default # Subject Name options
cert_opt = ca_default # Certificate field options
default_days = 365 # how long to certify for證書的默認有效期
default_crl_days= 30 # how long before next CRL默認聲明有效期
default_md = sha256 # use SHA-256 by default默認的生成算法
preserve = no # keep passed DN ordering
####################################################################
[ req ] # 向CA證書簽署發起注冊請求相關屬性
default_bits = 2048
default_md = sha256
default_keyfile = privkey.pem
distinguished_name = req_distinguished_name
attributes = req_attributes
x509_extensions = v3_ca # The extentions to add to the self signed cert
===============================================================================================
二、OpenSSL:創建私有證書簽發機構CA步驟
在確定配置為CA的服務器主機上生成一個自簽證書,并為CA提供所需要的目錄及文件;
在真正的通信過程中CA服務器主機不需要網絡參與,只需要參與到簽名中,不需要提供服務
1.生成私鑰;
~]# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 4096)
因為在默認配置文件中默認配置/etc/pki/CA/private/cakey.pem,所以指定目錄和文件名要和配置文件一致
2.生成CA自簽證書;
req – PKCS#10 certificate request and certificate generating utility,證書請求及生成工具;
[root@localhost tmp]# man req
~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3655
/etc/pki/CA/cacert.pem:配置文件中第一的目錄及文件名稱
-new:生成新證書簽署請求;
-x509:生成自簽格式證書,專用于創建私有CA時;
-key:生成請求時用到的私有文件路徑;
-out:生成的請求文件路徑;如果自簽操作將直接生成簽署過的證書;
-days:證書的有效時長,單位是day;
注意:
1)-key /etc/pki/CA/private/cakey.pem指明的是私鑰的位置,知識因為此處會自動抽取出私鑰中的公鑰
2)req只能發起簽署請求,需要加-x509參數實現自己發出請求,自己簽署。非自簽無需增加此參數
[root@localhost tmp]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3655
====================================填寫證書請求相關信息=======================================
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) [XX]: # 兩個字符表示的國家代碼,CN為中國
State or Province Name (full name) []: # 省或洲的完整名稱
Locality Name (eg, city) [Default City]: # 所在位置的名稱(默認為城市)
Organization Name (eg, company) [Default Company Ltd]: # 組織機構名稱(默認為公司)
Organizational Unit Name (eg, section) []: # 組織機構單元名稱(eg.部門)
Common Name (eg, your name or your server's hostname) []: # 持有者名或者所在服務器主機名(即域名)
Email Address []: # 管理員郵件地址,可以省略
================================================================================================
3.為CA提供所需的目錄及文件;
~]# mkdir -pv /etc/pki/CA/{certs,crl,newcerts} #當不存在時需要創建簽發證書、吊銷證書、新證書目錄
~]# touch /etc/pki/CA/{serial,index.txt} #創建證書序列號文件、證書索引文件
~]# echo 01 > /etc/pki/CA/serial # 第一次創建的時候需要給予證書序列號
三、OpenSSL;服務申請證書簽署實現SSL安全通信
要用到證書進行安全通信的服務器,需要向CA請求簽署證書;
需要簽署的服務無需和CA證書簽署機構主機在同一臺服務器上。
此處以httpd服務為例進行演示,步驟如下:
演示環境:
httpd服務放置172.16.249.210主機(此處為rpm包安裝)
CA私有簽機構放置172.16. 249.18主機:
1.用到證書的服務器生成私鑰;
~]# mkdir /etc/httpd/ssl
~]# cd /etc/httpd/ssl
~]# (umask 077; openssl genrsa -out /etc/httpd/ssl/httpd.key 2048) # 生成私鑰
生成httpd服務的私鑰創建時候無需在/etc/pki/CA創建,/etc/pki/CA目錄僅在創建CA主機時候
2.生成證書簽署請求
~]# openssl req -new -key /etc/httpd/ssl/httpd.key -out /etc/httpd/ssl/httpd.csr -days 365
1) *.csr表示證書簽署請求文件
2)要保證和簽署機構CA簽署機構信息一致
3.將請求通過可靠方式發送給CA主機
~]# scp /etc/httpd/ssl/httpd.csr root@172.16.249.18:/tmp/
4.在CA主機上簽署證書
~]# openssl ca -in /tmp/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365
*.crt:表示證書文件
-days :簽署證書的有效期
注意:此處需要自己去查看信息是否正確,并確定是否給予簽署證書
5.查看所簽署的證書信息
方法一:~]# cat /etc/pki/CA/index.txt
V:表示已經簽署的
01:表示證書序列號
/C=CN/ST=Beijing/O=… …: 表示主題信息(主題標示)
方法二:查看證書中的信息(CA或者服務端均可):
~]# openssl x509 -in /etc/pki/CA/certs/httpd.crt -noout -serial -subject
-serial :序列號 -subject:主題信息
6.將CA簽署機構的.crt證書發送給服務器
~]# scp /etc/pki/CA/certs/httpd.crt root@172.16.249.210:/etc/httpd/ssl
注意:第一次進行主機間基于ssh的scp操作會接收一個證書,Queue要你那認證
7.刪除服務器和CA主機上簽署前的*.csr文件,確保安全
httpd主機:~]# rm -rf /etc/httpd/ssl/httpd.csr
CA主機:~]# rm -rf /tmp/httpd.csr
四、OpenSSL:私有CA證書簽署機構吊銷證書
1.客戶端獲取要吊銷的證書的serial(在使用證書的主機上執行)
~]# openssl x509 -in /etc/pki/CA/certs/httpd.crt -noout -serial -subject
2.CA主機吊銷證書
先根據客戶提交的serial和subject信息,對比其與本機數據庫index.txt中存儲的是否一致;
在/etc/pki/CA/crets/*下生成證書后,會在/etc/pki/CA/newcrets/*以對應證書命名為SERIAL.pem文件存放
吊銷:
# openssl ca -revoke /etc/pki/CA/newcerts/SERIAL.pem 其中SERIAL要換成證書真正的序列號:eg. 01.pem
3.生成吊銷證書的吊銷編號(第一次吊銷證書時執行)
# echo 01 > /etc/pki/CA/crlnumber
4.更新證書吊銷列表
# openssl ca -gencrl -out thisca.crl
5.查看crl文件:
# openssl crl -in /PATH/FROM/CRL_FILE.crl -noout -text
Nicolo:http://xuding.blog.51cto.com/4890434/1732751
原創文章,作者:Nicolo,如若轉載,請注明出處:http://www.www58058.com/15729