文章目錄
一、CA機構分級管理
數字簽名的重要性:
數字證書就是互聯網通訊中標誌通訊各方身份信息,數字證書不是通信雙方的身份證書,而是認證機構在數字證書上扣得一個章,確認你的身份證書有效,它是由權威的CA機構頒發用於表示互聯網上通信雙方的身份。個人搭建的自由CA服務器在互聯網中是不被認可的,但是可以在企業自己的內部網絡中使用。
驗證過程:
首先在根CA進行簽署自證證書,然後子CA向根CA申請證書,根CA簽署證書後子CA就可以向其他申請者發放證書。此時的子CA服務器相對於根服務器來說是申請者,相對於web服務器申請者是簽署者,所以子CA是兩個身份,既是申請者又是簽署者
CA通訊過程:
二、OpenSSL
確認OpenSSL軟件包是否安裝
(一)生成隨機數:
格式:openssl rand -base64|-hex NUM
NUM: 表示字節數,使用-hex,每個字符爲十六進制,相當於4位二進制,出現的字符數爲NUM*2
示例1:[root@CentOS7 data]#openssl rand -hex 12
生成的是16進制數,2位16進制數是1個字節,12個字節就是12*2=24位16進制數
示例2: base64:[root@CentOS7 data]#openssl rand -base64 12
base64轉換爲64個常見字符:
(二)ASCII編碼與Base64編碼轉換:
轉換方法:ASCII表對應字母10進制轉二進制,高位起每6位做分隔,不夠6位用0補齊(補齊的用=表示);轉爲對應base64編碼表
Base64 編碼表:
示例1:
man ASCII
Oct Dec Hex Char
141 97 61 a
142 98 62 b
a十進制:97 對應二進制:01100001
b十進制:98 對應二進制:01100010
ab組合的二進制:0110000101100010
base64:(0-63)用6位二進制可表示64位數,因此對ab的二進制高位起每6位做分隔,不夠6位用0補齊(補齊的用=表示):二進制位是6和8的公倍數纔不會出現等號,6和8最小公倍數是24
011000 010110 001000 #二進制
24 22 8 # 32 16 8 4 2 1
Y W I= #後面補了0 ,用=表示
示例2:[root@CentOS7 data]#openssl rand -base64 3
3個字節是24位,所以3的倍數個字節不帶等號,即6和8最小公倍數的倍數也可整除6和8
(三)隨機數生成器:
鍵盤和鼠標,塊設備中斷
/dev/random:
僅從熵池返回隨機數;隨機數用盡,阻塞
/dev/urandom:
從熵池返回隨機數;隨機數用盡,會利用軟件生成僞隨機數,非阻塞
加密:
生成私鑰:
[root@CentOS7 data]#(umask 077;openssl genrsa -out /data/192.168.37.100.key)
對生成私鑰加密:
[root@CentOS7 data]#(umask 077;openssl genrsa -out /data/192.168.37.100.key -des 1024)
對加密密鑰解密
[root@CentOS7 data]#openssl rsa -in 192.168.37.100.key -out 192.168.37.100-2.key
不加密密鑰:
生成加密密鑰:
解密加密的密鑰:
生成公鑰:(由私鑰推出)
openssl rsa -in 192.168.37.100-2.key -pubout -out 192.168.37.100.key.pub
三、搭建私有CA
(一)證書申請及簽署步驟:
1、生成申請請求
2、RA覈驗
3、CA簽署
4、獲取證書
openssl的配置文件:/etc/pki/tls/openssl.cnf
三種策略:match匹配、optional可選、supplied提供
match:要求申請填寫的信息跟CA設置信息必須一致
optional:可有可無,跟CA設置信息可不一致
supplied:必須填寫這項申請信息
####################################################################
[ ca ]
default_ca = CA_default # The default ca section 機器可搭建多個CA;默認CA
####################################################################
[ CA_default ]
dir = /etc/pki/CA # Where everything is kept CA信息目錄
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 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
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
(二)搭建CA
1.生成私鑰
[root@CentOS7 ~]#(umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 4096)
2.生成自簽名證書
(命令中帶x509j是自簽名證書,不帶的是申請證書;-key 指定私鑰文件;-days指定CA有效期;一般公司是一年)
openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3650
openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3650 <<EOF
CN
beijing
beijing
Dell
devops
ca.Dell.com
[email protected]
EOF
填寫的內容如下:可通過多行重定向方式填寫
Country Name (2 letter code) [XX]:CN #國家
State or Province Name (full name) []:Beijing 省
Locality Name (eg, city) [Default City]:Beijing 市
Organization Name (eg, company) [Default Company Ltd]:Dell #公司名稱
Organizational Unit Name (eg, section) []:Devops #部門
Common Name (eg, your name or your server's hostname) []:ca.dell.com #域名
Email Address []:mail.dell.com #企業郵箱地址
查看證書內容:
[root@CentOS7 ~]#openssl x509 -in /etc/pki/CA/cacert.pem -noout -text
Issuer: 頒發者
Validity:有效期
Subject:頒發給
3.創建數據庫以及新頒發證書數字
[root@CentOS7 ~]#touch /etc/pki/CA/index.txt
[root@CentOS7 ~]#touch /etc/pki/CA/serial
4.設置證書起始編號
編號必須是整數位:
[root@CentOS7 ~]#echo 0F > /etc/pki/CA/serial
注: 無此兩個文件,直接頒發證書時可能會出現如下報錯:
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
9b:4c:11:78:5f:08:37:a6
Signature Algorithm: sha256WithRSAEncryption
頒發者: Issuer: C=CN, ST=Beijing, L=Beijing, O=Dell, OU=Devops, CN=ca.dell.com/emailAddress=mail.dell.com
有效期 Validity
Not Before: Jun 22 05:56:38 2019 GMT
Not After : Jun 19 05:56:38 2029 GMT
頒發給: Subject: C=CN, ST=Beijing, L=Beijing, O=Dell, OU=Devops, CN=ca.dell.com/emailAddress=mail.dell.com
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (4096 bit)
Modulus:
00:a9:a5:45:d9:29:d1:56:af:7c:13:e7:ba:ce:58:
c2:31:85:f2:d0:34:c9:39:26:6f:36:c1:da:3d:da:
60:31:34:50:99:fc:fc:69:4d:7e:cc:5e:0d:8d:0b:
5d:f2:ff:8f:67:34:70:fe:e8:d2:f1:f6:02:24:41:
(三) 申請證書:企業服務
申請的證書放在具體服務的配置文件路徑中;申請時間由頒發者規定的
1.生成私鑰文件:
[root@CentOS6 httpd]#(umask 066;openssl genrsa -out /data/httpd/httpd.key 2048)
2.生成證書申請文件
[root@CentOS6 httpd]#openssl req -new -key httpd.key -out httpd.csr
3.將申請文件發送給CA機構
[root@CentOS6 data]#openssl req -new -key httpd.key -out httpd.csr
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) []:Beijing
Locality Name (eg, city) [Default City]:bj
Organization Name (eg, company) [Default Company Ltd]:Dell
Organizational Unit Name (eg, section) []:test
Common Name (eg, your name or your server's hostname) []:ca.tmall.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
(四)頒發證書:CA
頒發證書,默認生效日期爲365天,證書後綴爲crt、pem都可以
1.生成證書文件
openssl ca -in /data/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 100
2.將證書文件發送給申請公司
(五)使用證書:企業服務
申請者上傳至頒發的證書至具體服務目錄下
windows主機:使用sz工具將他導出到windows機器中。然後雙擊安裝此證書到受信任的根證書頒發機構
(六)吊銷證書:CA
[root@CentOS7 ~]#openssl ca -revoke /etc/pki/CA/newcerts/0F.pem #吊銷證書
[root@CentOS7 ~]#openssl ca -status 0F #查看證書狀態
[root@CentOS7 ~]#echo FF > /etc/pki/CA/crlnumber #定義吊銷列表起始編號
[root@CentOS7 ~]#openssl ca -gencrl -out /etc/pki/CA/crl.pem #更新吊銷列表
[root@CentOS7 ~]#openssl crl -in /etc/pki/CA/crl.pem -noout -text #查看crl.pem 文件