1、CA概述
首先我們需要明確CA和CA服務器的區別,CA是指集技術和管理與一體的龐大機構,不僅要求技術能力,還需要相應的管理能力。CA服務器相對來說比較簡單,完成指定功能的一個應用程序。具體功能包括接受申請證書的請求、審核證書請求、簽發證書、發布證書、吊銷證書、生成和發布證書吊銷列表及證書庫的管理。
openssl提供了ca指令來模擬ca服務器,完成上述功能。上述功能繁雜,本文則主要講述證書的簽發過程,對證書發布過程、證書吊銷列表相關內容不做講述。
證書可分為兩類,終端證書和CA證書,終端證書是指具體應用在程序當中,不可以簽發其他證書,位於證書鏈的末端,而CA證書是指可以簽發下級CA證書或終端證書的證書,好暈……,見下圖
2、CA服務器的建立過程
建立openssl模擬的ca服務器需要三個步驟:
1、生成CA自簽名證書
/*生成自簽名證書,作為跟證書*/ xlzh@cmos:~$ openssl req -x509 -newkey rsa:2048 -keyout cakey.pem -out cacert.pem -passout pass:123456 -batch Generating a 2048 bit RSA private key .............................................................................................+++ .....+++ writing new private key to 'cakey.pem' ----- xlzh@cmos:~$
2、建立相應的目錄結構:
與openssl的其他指令不同,使用ca指令的時候需要根據配置文件建立相應的目錄結構,如果命令行不指定配置文件,則使用openssl自帶的配置文件/etc/ssl/openssl.cnf,讀者可自行查看該文件的CA_default字段。建立目錄結構如下
demoCA: CA根目錄
newcerts目錄:存放新生成的證書,以序列號命名
private目錄:存放CA證書的密鑰cakey.pem
cacert.pem文件:CA證書
serial:序列號文件,需給定初始值,可設置01
index.txt:文本數據庫,簽發證書后會更新該數據庫。
上述目錄結構簡化了本文不需要的目錄結構,請知悉。
3、把第一步的生成的證書和密鑰文件放到第二步生成的對應目錄中。
把第一步生成的ca證書cacert.pem放到demoCA目錄中,覆蓋空文件cacert.pem
把第一步生成的ca密鑰cakey.pem防盜demoCA/private/目錄,覆蓋空文件cakey.pem.
3、CA指令參數說明
查看ca指令的man手冊,可知ca選項如下
openssl ca [-verbose] [-config filename] [-name section] [-gencrl] [-revoke file] [-crl_reason reason] [-crl_hold instruction] [-crl_compromise time] [-crl_CA_compromise time] [-crldays days] [-crlhours hours] [-crlexts section] [-startdate date] [-enddate date] [-days arg] [-md arg] [-policy arg] [-keyfile arg] [-key arg] [-passin arg] [-cert file] [-selfsign] [-in file] [-out file] [-notext] [-outdir dir] [-infiles] [-spkac file] [-ss_cert file] [-preserveDN] [-noemailDN] [-batch] [-msie_hack] [-extensions section] [-extfile section] [-engine id] [-subj arg] [-utf8] [-multivalue-rdn]
現根據參數用戶分別說明參數作用
[in/infiles/out/outdir/spkac/ss_cert]
in:輸入為一個證書請求文件;
infiles:輸入為多個證書請求文件
out:輸出為一個證書文件
outdir:指定新證書的輸出目錄,默認生成demoCA/newcerts目錄下
ss_cert: 輸入的不是一個證書請求文件,而是一個自簽名證書,需要ca提取其中的用戶信息及公鑰生成用戶證書
spkac:輸入是一個SPKAC格式的文件,它是Netscape規定一種格式.
[config/name/extensions/extifle/policy]
config:指定配置文件,默認是/etc/ssl/openssl.cnf
name: 指定字段,默認是openssl.cnf中的[CA_default],該字段規定了目錄結構,證書有效期,匹配策略等信息,用戶可自己定義
extensions:指定擴展字段,CA_default字段中擴展字段默認為usr_cert,讀者可看成[usr_cert]定義的內容
extfile:指定擴展文件,與extensions類似,不過它是把字段定義在文件中
policy:指定策略,CA_default默認指定策略是policy_match,讀者可自行定義
[ policy_match ] countryName = match #證書請求與證書本身一樣 stateOrProvinceName = match #證書請求與證書本身一樣 organizationName = match #證書請求與證書本身一樣 organizationalUnitName = optional #可選項 commonName = supplied #證書請求中必須能存在該項 emailAddress = optional #可選項
[startdate/endate/days]
startdate:指定證書的生效日期,格式是YYMMDDHHMMSSZ,默認是當前時間
endate:指定證書到期日期,格式YYMMDDHHMMSSZ,默認時間365天
days:指定證書有效期,如果配置了endate,則days自動失效
[cert/keyfile/keyform]
cert:指定證書文件,默認使用demoCA/cacert.pem
keyfile:指定密鑰文件,默認使用demoCA/private/cakey.pem
keyform:指定密鑰文件格式
4、CA指令使用示例
本示例假設CA服務器已建立完成。
1、簽發終端證書
1、修改openssl.cnf中[v3_req]中basicConstraints = CA:FALSE,表明要生成的是終端證書請求 2、生成證書請求文件 xlzh@cmos:~/ca$ ls demoCA xlzh@cmos:~/ca$ openssl req -new -newkey rsa:1024 -keyout user_key.pem -out user_req.pem -passout pass:123456 Generating a 1024 bit RSA private key .............................++++++ .......++++++ writing new private key to 'user_key.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) [AU]:CH ... xlzh@cmos:~/ca$ ls demoCA user_key.pem user_req.pem xlzh@cmos:~/ca$ 3、使用CA簽發該證書 xlzh@cmos:~/ca$ openssl ca -in user_req.pem -out user_cert.pem Using configuration from /usr/lib/ssl/openssl.cnf Enter pass phrase for ./demoCA/private/cakey.pem: Check that the request matches the signature Signature ok ... Certificate is to be certified until Apr 26 09:36:14 2017 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
2、簽發二級CA證書
1、修改openssl.cnf中[v3_req]中basicConstraints = CA:TRUE,表明要生成的是CA證書請求 2、生成證書請求文件 xlzh@cmos:~/ca$ openssl req -new -newkey rsa:1024 -keyout ca_key.pem -out ca_req.pem -passout pass:123456 Generating a 1024 bit RSA private key ....................................................++++++ .....++++++ writing new private key to 'ca_key.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. ----- ...... 3、使用CA簽發該證書 xlzh@cmos:~/ca$ openssl ca -in ca_req.pem -out ca_cert.pem -extensions v3_ca Using configuration from /usr/lib/ssl/openssl.cnf Enter pass phrase for ./demoCA/private/cakey.pem: Check that the request matches the signature Signature ok ... 1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated xlzh@cmos:~/ca$
從上述兩個示例可以看出,簽發CA證書和終端證書有兩處不同:
1、生成證書請求文件的時候。讀者可查看openssl.cnf中[req]字段中擴展字段是v3_req,在v3_req中有個basicConstraints變量,
當basicConstraints=CA:TRUE時,表明要生成的證書請求是CA證書請求文件;
當basicConstraints=CA:FALSE時,表明要生成的證書請求文件是終端證書請求文件;
2、在簽發證書的時候。簽發終端證書的時候使用默認擴展字段usr_cert,當簽發CA證書的時候再命令行使用了extensions選項指定v3_ca字段。
在默認的usr_cert字段中 basicConstraints=CA:FALSE;表明要簽發終端證書
而在v3_ca字段中 basicConstraints=CA:TRUE;表明要簽發CA證書
5、總結
openssl的ca指令功能強大,上述只是介紹了其中主要的功能,讀者可在實際應用中學習其他選項的使用。
掌握一個指令的最好的方法就是嘗試各種組合,經常使用。