使用OpenSSL快速創(chuàng)建TLS/SSL證書
前言
在配置Apache杂瘸,F(xiàn)TP和Email郵件服務器的時候。為了實現(xiàn)安全傳輸伙菊,往往需要用到TLS或SSL服務來為服務器端和客戶端建立的連接加密败玉。不熟悉的話敌土,每次配置安裝TLS/SSL都得找一番資料。趁有時間索性寫一個關于TLS/SSL的快速文檔运翼。方便自己的同時返干,也方便別人。
理論描述
SSL(Secure Sockets Layer 安全套接層),及其繼任者傳輸層安全(Transport Layer Security血淌,TLS)用于在兩個通信應用程序之間提供保密性和數(shù)據(jù)完整性的一種安全協(xié)議矩欠。TLS與SSL在傳輸層對網絡連接進行加密。
TLS/SSL協(xié)議提供的服務主要有:
認證用戶和服務器悠夯,確保數(shù)據(jù)發(fā)送到正確的客戶機和服務器癌淮;
加密數(shù)據(jù)以防止數(shù)據(jù)中途被竊取沦补;
維護數(shù)據(jù)的完整性该默,確保數(shù)據(jù)在傳輸過程中不被改變。
操作步驟
準備工作
檢查是否正確安裝openssl軟件包
[root@nms ~]# rpm -q openssl
openssl-1.0.0-25.el6_3.1.x86_64
創(chuàng)建一個新的CA
[root@nms ~]# /etc/pki/tls/misc/CA -newca
CA certificate filename (or enter to create) ##按Enter鍵
Making CA certificate ...
Generating a 2048 bit RSA private key
...................................+++
..+++
writing new private key to '/etc/pki/CA/private/./cakey.pem' ##生成CA的密鑰
Enter PEM pass phrase: ##輸入一個cakey.pem私鑰保護密碼
###以后策彤,每一次訪問cakey.pem都需要輸入此密碼
###輸入密碼不回顯栓袖。
###一定要記住了,不然沒法給其他主機簽證書店诗。
Verifying - Enter PEM pass phrase: ##重新輸入裹刮,以驗證密碼
-----
##接下來給CA自己創(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) [XX]:CN ##CA所在國家
State or Province Name (full name) []:Beijing ##省份
Locality Name (eg, city) [Default City]:Beijing ##城市
Organization Name (eg, company) [Default Company Ltd]:Xiyang Liu Group Co., Ltd
###CA公司信息庞瘸,誰在運作這個CA捧弃,誰為信用承保。
Organizational Unit Name (eg, section) []:NIC ##部門
Common Name (eg, your name or your server s hostname) []:ca.xiyang-liu.com
###管理者或者服務器名
Email Address []:manager@ca.xiyang-liu.com
###管理郵箱擦囊,或者投訴郵箱违霞,所有簽過的證書都有這項信息。
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: ##加密CA證書的密碼瞬场,也要記住买鸽,可不輸入
An optional company name []: ##可以不輸入
Using configuration from /etc/pki/tls/openssl.cnf
##CA給自己簽名
Enter pass phrase for /etc/pki/CA/private/./cakey.pem: ##輸入cakey保護密碼
Check that the request matches the signature ##檢查請求文件的完整性
Signature ok
Certificate Details:
Serial Number:
b9:48:b2:aa:5b:76:6c:23
Validity
Not Before: Dec 6 12:06:42 2012 GMT
Not After : Dec 6 12:06:42 2015 GMT
Subject:
countryName = CN
stateOrProvinceName = Beijing
organizationName = Xiyang Liu Group Co., Ltd
organizationalUnitName = NIC
commonName = ca.xiyang-liu.com
emailAddress = manager@ca.xiyang-liu.com
X509v3 extensions:
X509v3 Subject Key Identifier:
EC:1C:4B:FB:82:DB:9C:86:.....47:0F:91:A5:6F:6F:32
X509v3 Authority Key Identifier:
keyid:EC:1C:4B:FB:82:D.......2:C8:47:0F:91:A5:6F:6F:32
X509v3 Basic Constraints:
CA:TRUE
Certificate is to be certified until Dec 6 12:06:42 2015 GMT (1095 days)
Write out database with 1 new entries
Data Base Updated
創(chuàng)建CA就結束了」岜唬總體就三個過程眼五。1.生成一個“絕密”的密鑰。2.創(chuàng)建一個簽名申請文件彤灶。3.自己給自己簽名看幼,生成一個標記CA的證書。
上述操作在CA服務器上完成幌陕。
CA服務器就是專管授權簽名的服務器诵姜。簡單說,它就是一個說一不二的“老大”搏熄。它要為簽過的證書承保棚唆。如果簽過的顧客干壞事了暇赤,它有連帶責任。所以讓CA簽名都是收錢的瑟俭。收的錢越多,從某種程度上說契邀,越有說服力摆寄。不收錢的CA,生存不下去坯门,也沒人看得起微饥。非常有名的CA有verisign。
創(chuàng)建簽名申請
創(chuàng)建一個簽名申請古戴,分兩個過程欠橘。創(chuàng)建一個自己的密鑰,然后提交給CA现恼,請求簽名肃续。
[root@nms ~]# openssl genrsa -des3 -out my-server.key 1024
### 1024:表示生成DES3加密的1024位的密碼
Generating RSA private key, 1024 bit long modulus
......++++++
......................++++++
e is 65537 (0x10001)
Enter pass phrase for my-server.key: ##輸入一個私鑰保護密碼
Verifying - Enter pass phrase for my-server.key: ##驗證輸入
從私鑰中生成公鑰:(可選,證書里就有叉袍,一般不用單獨導出來)
[root@nms ~]# openssl rsa -in my-server.key -pubout > my-server-public.key
Enter pass phrase for my-server.key: ##輸入剛才設定的私鑰保護密碼
writing RSA key
創(chuàng)建證書請求
[root@nms ~]# openssl req -new -key my-server.key -out my-server.csr
Enter pass phrase for my-server.key:
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.
-----
##下面的信息是證書請求方的信息始锚,不一定和CA相同。
###兩者的Common Name一定不能相同喳逛,否則CA不能給這個請求簽名
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Beijing
## State or Province Name必須和CA一致瞧捌,否則無法簽名。
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:Xiyang Liu Group Co., Ltd
## Organization Name必須和CA一致润文,否則無法簽名姐呐。
Organizational Unit Name (eg, section) []:NIC
Common Name (eg, your name or your server s hostname) []:nms.xiyang-liu.com
Email Address []:manager@nms.xiyang-liu.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:PASSW0D1 #可不輸入
An optional company name []:
兩步并作一步走,使用如下命令直接創(chuàng)建一個全新的申請:
openssl req -new -nodes -newkey rsa:1024 -keyout my-server-key.key -out my-server-req.csr -days 365
-nodes參數(shù)表示不加密私鑰典蝌。如果不添加nodes參數(shù)曙砂,以后每次使用私鑰時都必須輸入密碼(如Apache每次重啟)
-newkey rsa:1024 指定密鑰長度
-days 365 設定證書有效期為一年,而不是默認的30天骏掀。
填寫信息麦轰,命令執(zhí)行完畢,會生成兩個文件砖织。自己放好my-server-key.key款侵。把my-server-req.csr發(fā)給CA請求簽名。不差錢可以發(fā)給verisign哦侧纯。
在使用證書的主機上操作新锈。可以和CA是一個主機眶熬。
CA簽名請求
[root@nms ~]# openssl ca -in my-server-req.csr -out my-server-req.crt
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for /etc/pki/CA/private/cakey.pem: ##輸入cakey保護密碼
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number:
b9:48:b2:aa:5b:76:6c:24
Validity
Not Before: Dec 6 13:51:13 2012 GMT
Not After : Dec 6 13:51:13 2013 GMT
Subject:
countryName = CN
stateOrProvinceName = Beijing
organizationName = Xiyang Liu Group Co., Ltd
organizationalUnitName = NIC
commonName = nms.xiyang-liu.com
emailAddress = manager@nms.xiyang-liu.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
36:C4:AB:56:5............5:C2:84:A6:86:15
X509v3 Authority Key Identifier:
keyid:EC:1C:4B...........F:91:A5:6F:6F:32
Certificate is to be certified until Dec 6 13:51:13 2013 GMT (365 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
創(chuàng)建自簽證書
如果懶到連CA都不想創(chuàng)建妹笆,或者不需要建块请。就執(zhí)行下面命令直接創(chuàng)建自簽證書:
openssl req -x509 -nodes -newkey rsa:1024
-keyout my-server-key.key -out my-server.crt
命令執(zhí)行結束將只生成crt證書和私鑰文件,沒有CA的證書拳缠《招拢可用于傳遞電子郵件。
使用生成的證書
使用時窟坐,對于服務器往往需要CA的證書海渊、密鑰文件和簽名證書。我們的CA不受系統(tǒng)信任哲鸳,系統(tǒng)不會集成CA證書臣疑。所以需要指定。如果純粹是私人傳輸郵件加密用的徙菠,則完全何以使用自簽名證書讯沈。