OpenSSL - 利用OpenSSL自簽證書和CA頒發證書


秘鑰操作

這個命令會生成一個1024/2048位的密鑰,包含私鑰和公鑰。

openssl genrsa -out private.key 1024/2038                     (with out password protected)   

openssl genrsa -des3 -out private.key 1024/2048    (password protected)

這個命令可以利用private.key文件生成公鑰。

openssl rsa -in private.key -pubout -out public.key 

查看私鑰命令

openssl rsa -noout -text -in public.key

 

證書請求

openssl req -new -key private.key -out cert.csr (-config openssl.cnf)

openssl req -new -nodes -key private.key -out cert.csr (-config openssl.cnf)

這個命令將會生成一個證書請求,當然,用到了前面生成的密鑰private.key文件
這里將生成一個新的文件cert.csr,即一個證書請求文件,你可以拿着這個文件去數字證書頒發機構(即CA)申請一個數字證書。CA會給你一個新的文件cacert.pem,那才是包含公鑰給對方用的數字證書。

查看證書請求

openssl req -noout -text -in cert.csr


生成證書

自簽名證書,用於自己測試,不需要CA簽發
openssl req -new -x509 -key private.key -out cacert.pem -days 1095 (-config openssl.cnf)

CA簽發證書:

CA是專門簽發證書的權威機構,處於證書的最頂端。自簽是用自己的私鑰給證書簽名,CA簽發則是用CA的私鑰給自己的證書簽名來保證證書的可靠性,

利用OpenSSL可以自己作為CA進行證書簽發,當然這並不權威。

CA簽發證書生成的cacert.pem 見“建立CA頒發證書

 

有了private.key和cacert.pem文件后就可以在自己的程序中使用了,比如做一個加密通訊的服務器

從證書中提取公鑰

openssl x509 -in cacert.pem -pubkey >> public.key

查看證書信息

openssl x509 -noout -text -in cacert.pem

 

建立CA頒發證書

(1) 環境准備

首先,需要准備一個目錄放置CA文件,包括頒發的證書和CRL(Certificate Revoke List)
mkdir ./CA

 

(2) 創建配置文件

之前生成秘鑰和證書可以進行命令行配置,但是在創建CA的時候必須使用配置文件,因為做證書頒發的時候只能使用配置文件。

創建配置文件如下:vi openssl.cnf

 1     ################################################################ 
 2     # openssl example configuration file. 
 3     # This is mostly used for generation of certificate requests. 
 4     ################################################################# 
 5     [ ca ] 
 6     default_ca= CA_default          # The default ca section 
 7     ################################################################# 
 8      
 9     [ CA_default ] 
10      
11     dir=/opt/iona/OrbixSSL1.0c/certs # Where everything is kept 
12     certs=$dir                       # Where the issued certs are kept 
13     crl_dir= $dir/crl                # Where the issued crl are kept 
14     database= $dir/index.txt         # database index file 
15     new_certs_dir= $dir/new_certs    # default place for new certs 
16     certificate=$dir/CA/OrbixCA      # The CA certificate 
17     serial= $dir/serial              # The current serial number 
18     crl= $dir/crl.pem                # The current CRL 
19     private_key= $dir/CA/OrbixCA.pk  # The private key 
20     RANDFILE= $dir/.rand             # private random number file 
21     default_days= 365                # how long to certify for 
22     default_crl_days= 30             # how long before next CRL 
23     default_md= md5                  # which message digest to use 
24     preserve= no                     # keep passed DN ordering 
25      
26     # A few different ways of specifying how closely the request should 
27     # conform to the details of the CA 
28      
29     policy= policy_match            # For the CA policy 
30     
31     [ policy_match ]  
32     countryName= match 
33     stateOrProvinceName= match 
34     organizationName= match 
35     organizationalUnitName= optional 
36     commonName= supplied 
37     emailAddress= optional 
38      
39     # For the `anything' policy 
40     # At this point in time, you must list all acceptable `object' 
41     # types 
42      
43     [ policy_anything ] 
44     countryName = optional 
45     stateOrProvinceName= optional 
46     localityName= optional 
47     organizationName = optional 
48     organizationalUnitName = optional 
49     commonName= supplied 
50     emailAddress= optional 
51      
52     [ req ] 
53     default_bits = 1024 
54     default_keyfile= privkey.pem 
55     distinguished_name = req_distinguished_name 
56     attributes = req_attributes 
57      
58     [ req_distinguished_name ] 
59     countryName= Country Name (2 letter code) 
60     countryName_min= 2 
61     countryName_max = 2 
62     stateOrProvinceName= State or Province Name (full name) 
63     localityName = Locality Name (eg, city) 
64     organizationName = Organization Name (eg, company) 
65     organizationalUnitName  = Organizational Unit Name (eg, section) 
66     commonName = Common Name (eg. YOUR name) 
67     commonName_max = 64 
68     emailAddress = Email Address 
69     emailAddress_max = 40 
70      
71     [ req_attributes ] 
72     challengePassword = A challenge password 
73     challengePassword_min = 4 
74     challengePassword_max = 20 
75     unstructuredName= An optional company name 
View Code

 

根據配置文件。創建以下三個文件:

touch index.txt

touch index.txt.attr

touch serial 內容為01

 

(3) 生成CA私鑰和證書

openssl genrsa -out ca.key 1024

openssl req -new -x509 -key ca.key -out ca.pem -days 365 -config openssl.cnf   (CA只能自簽名證書,注意信息與要頒發的證書信息一致)

 

(4) 頒發證書

頒發證書就是用CA的秘鑰給其他人簽名證書,輸入需要證書請求,CA的私鑰及CA的證書,輸出的是簽名好的還給用戶的證書

這里用戶的證書請求信息填寫的國家省份等需要與CA配置一致,否則頒發的證書將會無效。

openssl ca -in ../cert.csr -out cacert.pem -cert ca.pem -keyfile ca.key -config openssl.cnf

 

對比CA頒發的證書提取公鑰和私鑰導出的公鑰是否一致:

 

同時產生01.pem,這個是CA的備份保留,與生成發送給請求證書的內容一致,serial內序號自動+1。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM