國密證書生成實踐


實驗環境:

1
2
Linux ubuntu/Centos 64
x86_64 x86_64 x86_64 GNU/Linux

 

1、根據官網說明安裝gmssl

 

1
2
3
4
5
$ 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配置

1
vi    /usr/local/sslopenssl .cnf

  

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

  

3、初始化CA目錄

1)創建根目錄

1
2
mkdir  -p   /home/myapp/demoCA
cd  /home/myapp/demoCA

2)創建其他目錄

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

1
mkdir  certs crl newcerts private

3)創建好database文件index.txt

1
touch  index.txt 

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

1
echo  "01"  > serial

4、生成國密證書步驟 

(1)生成根證書

1)生成私鑰key

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

2)生成證書簽名請求

1
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)生成根證書

1
2
$ 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)生成私鑰

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

2)生成客戶證書請求

1
$ 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)簽發證書

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

4)證書驗證

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

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

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

6) 查看證書信息

1
2
3
4
5
6
7
8
9
10
11
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) 生成私鑰

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

 2) 證書請求

1
$ 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) 簽發證書

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

4)證書驗證

1
$ gmssl verify -CAfile RootCA.crt ServerCA.crt

  

 

如果你覺得不錯的話,右下角,隨手點個推薦,鼓勵支持hhh^_^


免責聲明!

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



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