國密證書生成實踐


實驗環境:

Linux ubuntu/Centos 64
x86_64 x86_64 x86_64 GNU/Linux

 

1、根據官網說明安裝gmssl

 

$ unzip GmSSL-master.zip
$ cd GmSSL-master
$ ./config no-saf no-sdf no-skf no-sof no-zuc  no-shared         #不去編譯動態庫,編譯出來的gmssl不再依賴libssl.so
$ make
$ sudo make install

  

2、修改/usr/local/ssl/openssl.cnf配置

$ vi   /usr/local/sslopenssl.cnf 

  

[ ca ]
default_ca      = CA_default            # The default ca section
[ CA_default ] #dir		= ./demoCA		# Where everything is kept 
dir		= /home/myapp/demoCA  #此處修改 

  

3、初始化CA目錄

1)創建根目錄

$ mkdir -p  /home/myapp/demoCA
$ cd /home/myapp/demoCA 

2)創建其他目錄

在此路徑下要創建好/usr/local/ssl/openssl.cnf中需要的certs, crl ,new_certs_dir和private_key的子目錄,默認是newcerts和private

$ mkdir certs crl newcerts private 

3)創建好database文件index.txt

touch index.txt 

4)創建好serial文件,並寫入初始序號,如01

echo "01" > serial

4、生成國密證書步驟 

(1)生成根證書

1)生成私鑰key

$ gmssl ecparam -genkey -name sm2p256v1 -text -out Root.key -config  /usr/local/ssl/openssl.cnf 

2)生成證書簽名請求

$ gmssl req -new -key Root.key -out Root.req -subj /C=CN/ST=Guang\ Zhou/L=GZ/O=Root/OU=Root\ Sign/CN=RootCA/emailAddress=Root@gmail.com  
-config /usr/local/ssl/openssl.cnf

3)生成根證書

$ gmssl x509 -req -days 3650 -sm3 -in Root.req -signkey Root.key -out RootCA.crt $ cp RootCA.crt demoCA/
$ cp Root.key demoCA/private/

 類似於 apache/ssl/ca.crt和apache/ssl/ca.key

(2)生成中間證書(即客戶端證書)

1)生成私鑰

$ gmssl ecparam -genkey -name sm2p256v1 -text -out Medium.key -config  /usr/local/ssl/openssl.cnf 

2)生成客戶證書請求

$ gmssl req -new -key Medium.key -out Medium.req -subj /C=CN/ST=Guang\ Zhou/L=GZ/O=Medium/OU=Medium\ Sign/CN=MediumCA/emailAddress=Medium@gmail.com  -config  /usr/local/ssl/openssl.cnf

3)簽發證書

$ gmssl x509 -req -sm3 -days 3650 -CA  RootCA.crt -CAkey demoCA/private/Root.key -CAcreateserial -in Medium.req -out MediumCA.crt 

4)證書驗證

$ gmssl verify -CAfile RootCA.crt MediumCA.crt 
$ cp MediumCA.crt demoCA/
$ cp Medium.key demoCA/private/

5)證書轉換成瀏覽器認識的格式 pfx

$ gmssl pkcs12 -export  -inkey Medium.key -in MediumCA.crt -out test.pfx -passin  pass:xxx -passout pass:xxx

6) 查看證書信息

PKCS轉換為PEM
gmssl pkcs12 -in test.pfx -out cert.pem -nodes
轉換后可查看證書信息
打印出證書的內容:
gmssl x509 -in cert.pem -noout -text
打印出證書的系列號
gmssl x509 -in cert.pem -noout -s erial
打印出證書的擁有者名字
gmssl x509 -in cert.pem -noout -subject
打印出證書的MD5特征參數
gmssl x509 -in cert.pem -noout -fingerprint

(3)生成服務器證書

  1) 生成私鑰

$ gmssl ecparam -genkey -name sm2p256v1 -text -out Server.key -config /usr/local/ssl/openssl.cnf

 2) 證書請求

$ gmssl req -new -key Server.key -out Server.csr -subj /C=CN/ST=Guang\ Zhou/L=GZ/O=Server/OU=Server\ Sign/CN=ServerCA/emailAddress=Server@gmail.com -config /usr/local/ssl/openssl.cnf

3) 簽發證書

$ gmssl x509 -req -sm3 -days 3650 -CA RootCA.crt -CAkey demoCA/private/Root.key -CAcreateserial -in Server.csr -out ServerCA.crt

4)證書驗證

$ gmssl verify -CAfile RootCA.crt ServerCA.crt

  

 


免責聲明!

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



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