OpenSSL 及創建私有CA
OpenSSL
OpenSSL,是一個開放源代碼的軟件庫包,應用程序可以使用這個包來進行安全通信,避免竊聽,同時確認另一端連接者的身份。這個包廣泛被應用在互聯網的網頁服務器上。
接下來對openssl基本命令進行介紹
openssl 非常強大其子命令很多,這里只是對完成某些操作進行部分解釋
openssl 的基本格式為
openssl enc -ciphername [-in filename] [-out filename] [-e] [-d] …
-ciphername :指定加密算法,例如:-base64
-in filename :指定要加密的文件,filename為該文件的文件名
-out filename :指定將加密后的數據保存到filename指定的文件中
-e :指定為加密過程,此為默認過程
-d :指定為解密過程
加密
openssl對稱加密
enc命令
加密
openssl enc -e des3 -a salt -in testfile -out testfile.cipher
解密
openssl enc -d des3 -a salt –in testfile.cipher -out testfile
單項加密
dgst命令
openssl dgst -md5 testfile
生成用戶密碼
生成用戶密碼: passwd命令:
openssl passwd -1 -salt SALT(最多8位)
openssl passwd -1 –salt centos
?生成隨機數:
openssl rand -base64|-hex NUM
NUM: 表示字節數;-hex時,每個字符為十六進制,相當于4位二進制,出現的字符數為NUM*2
創建私有CA
什么是CA
一種數字證書,如果用戶想得到一份屬于自己的證書,他應先向 CA 提出申請。在 CA 判明申請者的身份后,便為他分配一個公鑰,并且 CA 將該公鑰與申請者的身份信息綁在一起,并為之簽字后,便形成證書發給申請者。 如果一個用戶想鑒別另一個證書的真偽,他就用 CA 的公鑰對那個證書上的簽字進行驗證,一旦驗證通過,該證書就被認為是有效的。證書實際是由證書簽證機關(CA)簽發的對用戶的公鑰的認證。 證書的內容包括:電子簽證機關的信息、公鑰用戶信息、公鑰、權威機構的簽字和有效期等等。目前,證書的格式和驗證方法普遍遵循X.509 國際標準。
在制作私有CA之前我們先來了解一下,與CA相關的配置文件,在/etc/pki/tls/下的openssl.cnf文件
我們來看一下,部分內容如下圖:
#CA的默認設置
[ CA_default ]
dir = /etc/pki/CA # 默認工作目錄
certs = $dir/certs #服務器證書存放位置
crl_dir = $dir/crl #證書吊銷列表
database = $dir/index.txt #證書數據列表文件
#unique_subject = no # Set to ‘no’ to allow creation of
# several ctificates with same subject.
new_certs_dir = $dir/newcerts # 新證書存放位置
certificate = $dir/cacert.pem #CA自己的證書存放處
serial = $dir/serial #序列號(十六進制數)為下一個證書的生成編號
crlnumber = $dir/crlnumber #吊銷列表的工作號
# must be commented out to leave a V1 CRL
crl = $dir/crl.pem #證書吊銷列表文件
private_key = $dir/private/cakey.pem #CA私鑰
RANDFILE = $dir/private/.rand # 隨機數文件
x509_extensions = usr_cert #
default_days = 365 # how long to certify for 證書時間
default_crl_days= 30 # how long before next CRL 吊銷列表的時間
default_md = default # use public key default MD 單相加密算法
preserve = no # keep passed DN ordering
我們來看一下/etc/pki/CA下的文件
[root@Centos6 /etc/pki/CA]#ls
certs crl newcerts private
對比配置文件,可以看到缺少index.txt serial文件,缺少的文件是需要我們自己創建出來的
CA policy (匹配的策略)是否需要相同 匹配策略
在配置的設定中,國家、省份及公司名稱是必須要一致的,否則在創建CA時會提示不匹配錯誤,當然也可以指定為都不匹配
創建CA
1、創建所需要的文件
在上面提到過的缺少的文件需要創建
touch /etc/pki/CA/index.txt 生成證書索引數據庫文件
echo 01 > /etc/pki/CA/serial 指定第一個頒發證書的序列號,以后不需重新指定
2、CA自簽證書
那么誰會給CA本身頒發證書呢,CA本身就是最高的認證,這就需要自己給自己認證了
(1)、生成私鑰
在配置文件中,已經定義了私鑰的存放位置,以及名稱,這里要注意與配置文件openssl.cnf當中的指定路徑一致
cd /etc/pki/CA
執行命令,并設定生成的文件權限為600
[root@Centos6 /etc/pki/CA]#(umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem -des 1024)
Generating RSA private key, 1024 bit long modulus
.++++++
…………………………++++++
e is 65537 (0x10001)
Enter pass phrase for /etc/pki/CA/private/cakey.pem:
Verifying – Enter pass phrase for /etc/pki/CA/private/cakey.pem:
(2)、生成自簽名證書
生成自簽名證書,需要自己給自己頒發,openssl有專門的命令選項x509可以使用
#執行命令后,填寫信息表即可
[root@Centos6 /etc/pki/CA]#openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem
Enter pass phrase for /etc/pki/CA/private/cakey.pem:
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) []: henan
Locality Name (eg, city) [Default City]: zhengzhou
Organization Name (eg, company) [Default Company Ltd]: magedu
Organizational Unit Name (eg, section) []: tech
Common Name (eg, your name or your server’s hostname) []: magedu.com
Email Address []:
注意:這里所填的國家、省份及公司名稱要記得,接下來在提交申請證書時會用到
相關的命令參數
-req : requst 申請
-new : 生成新證書簽署請求
-x509 : 專用于CA生成自簽證書
-key : 生成請求時用到的私鑰文件
-days n :證書的有效期限
-out /PATH/TO/SOMECERTFILE: 證書的保存路徑
(3)、頒發證書
A、在需要使用證書的主機生成證書請求
#給web服務器生成私鑰
[root@centos7 ~]#(umask 077; openssl genrsa -out /etc/pki/tls/private/test.key 1024)
Generating RSA private key, 1024 bit long modulus
……….++++++
……++++++
e is 65537 (0x10001)
#生成證書申請文件
[root@centos7 /etc/pki/tls]#openssl req -new -key /etc/pki/tls/private/test.key -days 365 -out /etc/pki/tls/test.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) []:henan
Locality Name (eg, city) [Default City]:zhengzhou
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:tech
Common Name (eg, your name or your server’s hostname) []:magedu.com
Email Address []:
(命令里的時間制定其實沒什么意義,最終是要頒發者指定的)
以上申請證書的文件已經完成,可以提交了
B、向CA提交證書申請
[root@centos7 /etc/pki/tls]#scp test.csr 172.18.18.18:/etc/pki/CA
root@172.18.18.18’s password:
test.csr 100% 647 0.6KB/s 00:00
C、簽署證書
[root@Centos6 /etc/pki/CA]#openssl ca -in /etc/pki/CA/test.csr -out /etc/pki/CA/certs/test.crt -days 3650
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for /etc/pki/CA/private/cakey.pem:
Check that the request matches the signature
Signature ok
#申請證書的詳細信息
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Sep 10 09:41:12 2017 GMT
Not After : Sep 8 09:41:12 2027 GMT
Subject:
countryName = CN
stateOrProvinceName = henan
organizationName = magedu
organizationalUnitName = tech
commonName = magedu.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
AC:FB:56:45:E0:A8:B5:EA:82:15:14:6E:9F:FB:18:F4:FB:78:67:FA
X509v3 Authority Key Identifier:
keyid:C1:37:D1:E8:53:EB:C9:02:0B:E9:71:1A:60:42:0F:33:C9:11:ED:7A
Certificate is to be certified until Sep 8 09:41:12 2027 GMT (3650 days)
#是否簽署文件
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
查看證書的信息及狀態,可以執行以下命令
openssl x509 -in app.crt -noout -text|issuer|subject|dates
text代表全部信息
issuer代表頒發者
subject代表主題
dates代表有效期
openssl ca -status SERIAL
到此證書的簽署就已經完成了,之后就可以頒發給申請者了
我們可以看一下完成一系列流程的CA目錄
#CA的目錄樹
[root@Centos6 /etc/pki/CA]#tree
.
├── cacert.pem
├── certs
│ └── test.crt
├── crl
├── index.txt
├── index.txt.attr
├── index.txt.old
├── newcerts
│ └── 01.pem
├── private
│ └── cakey.pem
├── serial
└── serial.old
吊銷證書
當證書過期或者不受信任時需要對簽署的證書進行吊銷,可以執行以下命令
(1)、吊銷證書
[root@Centos6 /etc/pki/tls]#openssl ca -revoke /etc/pki/CA/newcerts/01.pem
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for /etc/pki/CA/private/cakey.pem:
Revoking Certificate 01.
Data Base Updated
(2)、指定吊銷證書的編號
echo 01 > /etc/pki/CA/crlnumber
注意:第一次更新吊銷列表前,才需要執行,以后不需要了
(3)、更新證書吊銷列表
[root@Centos6 /etc/pki/CA]#openssl ca -gencrl -out /etc/pki/CA/crl/crl.pem
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for /etc/pki/CA/private/cakey.pem:
#查看更新后的crl文件
[root@Centos6 /etc/pki/CA]#openssl crl -in /etc/pki/CA/crl/crl.pem -noout -text
Certificate Revocation List (CRL):
Version 2 (0x1)
Signature Algorithm: sha1WithRSAEncryption
Issuer: /C=CN/ST=henan/L=zhengzhou/O=magedu/OU=tech/CN=magedu.com
Last Update: Sep 10 12:42:49 2017 GMT
Next Update: Oct 10 12:42:49 2017 GMT
CRL extensions:
X509v3 CRL Number:
1
Revoked Certificates:
Serial Number: 01
Revocation Date: Sep 10 12:38:03 2017 GMT
Signature Algorithm: sha1WithRSAEncryption
6b:06:d1:c2:9c:c8:9a:81:21:09:10:f4:aa:28:6c:bf:a3:64:
以下省略
…
通過crl文件可以看出,確實將申請的證書吊銷了
到此創建私有的證書及頒發完畢,不足及錯誤之處還望指正
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/86931
內容不錯,注意排版,繼續加油!