Linux下OpenSSL自簽ssl證書


0x00 介紹

  • OpenSSL是SSL/TLS協議的實現工具
  • key是私鑰文件,用於對發送給客戶端的數據加密,以及對從客戶端接收的數據進行解密。
  • csr是證書簽名請求文件,用於提交給證書頒發機構(CA)對證書簽名。
  • crt是由證書頒發機構(CA)簽名后的證書,或者是開發者自簽名的證書,包含證書持有人的信息,持有人的公鑰,以及簽署者的簽名等信息

0x01 生成證書

成為CA頒發機構

生成私鑰

openssl genrsa -des3 -out myCA.key 2048
##openssl genrsa 用於生成RSA私鑰,不會生成公鑰,因為公鑰提取自私鑰
## -des3為加密方式
## 2048為生成秘鑰長度
## 可以加上-nodes參數,禁止進行加密,即可不運行下面的消除密碼

消除私鑰key的密碼

openssl rsa -in myCA.key -out myCA.key

生成pem文件

openssl req -utf8 -x509 -new -nodes -key myCA.key -sha256 -days 825 -out myCA.pem
創建CA簽名證書

生成私鑰

openssl genrsa -out server.key 2048

創建證書簽名請求

openssl req -new -key server.key -out server.csr
##Common Name應該與域名保持一致,否則會引起瀏覽器警告

為擴展創建一個配置文件

>server.ext cat <<-EOF

authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = www.baidu.com # Be sure to include the domain name here because Common Name is not so commonly honoured by itself
DNS.2 = www.sougou.com # Optionally, add additional domains (I've added a subdomain here)
IP.1 = 192.168.1.1 # Optionally, add an IP address (if the connection which you have planned requires it)
EOF

## chrome 會查看當前域名是否在證書中聲明,該聲明由 subjectAltName 字段設置。上述的生成步驟默認未設置該字段。

創建簽名證書

openssl x509 -req -in server.csr -CA myCA.pem -CAkey myCA.key -CAcreateserial -out server.crt -days 3650 -sha256 -extfile server.ext

最后得到的

  • server.crt
  • server.key

就是我們想要的文件

0x02 參考


免責聲明!

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



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