如何使用 OpenSSL 生成 SSL 證書、私鑰和 CSR
OpenSSL 是一個 CLI(命令列工具),可用於保護伺服器,生成公鑰基礎設施 (PKI) 和 HTTPS。本文幫助您快速參考瞭解 OpenSSL 命令,這些命令在日常場景中非常有用,尤其適用於系統管理員。
證書籤名請求 (CSR)
如果我們想從證書頒發機構 (CA) 獲取 SSL 證書,則必須生成證書籤名請求 (CSR)。CSR 主要包含金鑰對的公鑰,以及一些其他資訊。這兩個元件在為 CSR 簽名時合併到證書中。
在生成 CSR 時,系統會提示輸入有關證書的資訊,此資訊稱為可分辨名稱 (DN)。DN 中的重要欄位是通用名稱 (CN),它應該是伺服器或我們打算在其中使用證書的主機的 FQDN(完全限定域名)。
DN 中的下一個專案是提供有關我們的業務或組織的其他資訊。如果我們從證書頒發機構 (CA) 購買 SSL 證書,則這些其他欄位(如“組織”)必須反映您的組織詳細資訊,這一點非常重要且必不可少。
以下是我們執行 OpenSSL 命令生成 CSR 時 CSR 資訊提示的通用示例。
Country Name (2 letter code) [US]:IN State or Province Name (full name) [Some-State]:Telengana Locality Name (eg, city) []:Hyderabad Organization Name (eg, company) [Internet Widgits Pty Ltd]:Ansole Pvt Ltd. Organizational Unit Name (eg, section) []:Application Common Name (e.g. server FQDN or YOUR name) []:domainname.com Email Address []:support@domainname.com
我們還可以透過非互動式答案提供 CSR 資訊生成的資訊,可以透過將 –subj 選項新增到我們嘗試生成或執行的任何 OpenSSL 命令來實現。
以下是 –subj 選項的示例,其中我們可以提供我們想要使用此 CSR 的組織的資訊。
-subj "/C=IN/ST=Telengana/L=Hyderabad/O=Ansole Pvt Ltd/CN=domainname.com"
生成 CSR
在本節中,我們將介紹與生成 CSR 相關的 OpenSSL 命令。此 CSR 可用於向證書頒發機構請求 SSL 證書。
生成私鑰和 CSR
如果我們想使用 HTTPS(HTTP over TLS)來保護 Apache 或 Nginx Web 伺服器(使用證書頒發機構 (CA) 來頒發 SSL 證書)。此外,我們將生成的“.CSR”必須傳送到 CA 以請求證書以獲取 CA 簽名的 SSL。
以下是從頭開始為“domain.key”建立 2048 位私鑰和“domain.csr”的 CSR 的命令。
$ openssl req -newkey rsa:2048 -nodes -keyout domain.key -out domain.csr Generating a 2048 bit RSA private key ..............................+++ .......................................+++ writing new private key to 'domain.key' ----- 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]:IN State or Province Name (full name) [Some-State]:Telengana Locality Name (eg, city) []:Hyderabad Organization Name (eg, company) [Internet Widgits Pty Ltd]:Ansol Pvt Ltd Organizational Unit Name (eg, section) []:Application Common Name (e.g. server FQDN or YOUR name) []:domainname.com Email Address []:support@domainname.com
“–newkey rsa:2048”是我們指定的選項,即金鑰應使用 RSA 演算法為 2048 位。' –nodes' 選項表示私鑰不應使用密碼短語加密。' -new' 選項表示正在生成 CSR。
從現有私鑰生成 CSR
在這裡,我們將瞭解如何為已擁有私鑰的 CSR 生成 CSR。
以下是基於我們已有的私鑰建立新的 .csr 檔案的命令。
$ openssl req -key domain.key -new -out domain.csr 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]:IN State or Province Name (full name) [Some-State]:Telengana Locality Name (eg, city) []:Hyderabad Organization Name (eg, company) [Internet Widgits Pty Ltd]:Ansol Pvt Ltd Organizational Unit Name (eg, section) []:Applicatoin Common Name (e.g. server FQDN or YOUR name) []:domainname.com Email Address []:support@domainname.com
從現有證書和私鑰生成 CSR
在這裡,我們可以生成或續訂現有證書,由於某些原因我們錯過了 CSR 檔案。在這裡,CSR 將使用我們擁有的 .CRT 檔案提取資訊。
以下是生成示例 -
$ openssl x509 in domain.crt-signkey domain.key -x509toreq -out domain.csr
其中 -x509toreq 指定我們正在使用 x509 證書檔案來製作 CSR。
生成自簽名證書
在這裡,我們將生成證書以保護 Web 伺服器,我們使用自簽名證書用於開發和測試目的。
$ openssl req -newkey rsa:2048 -nodes -keyout domain.key-x509 -days 365 -out domain.crt Generating a 2048 bit RSA private key ................+++ .......................................................+++ writing new private key to 'domain.key' ----- 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]:IN State or Province Name (full name) [Some-State]:Telengana Locality Name (eg, city) []:Hyderabad Organization Name (eg, company) [Internet Widgits Pty Ltd]:Ansol Pvt Ltd Organizational Unit Name (eg, section) []:Application Common Name (e.g. server FQDN or YOUR name) []:domainname.com Email Address []:support@domainname.com
在這裡,我們使用 –x509 選項生成自簽名證書,我們可以使用 –days 365 生成有效期為 365 天的證書,並且使用上述資訊生成臨時 .CSR 檔案。
檢視證書檔案
請注意,CSR 檔案使用 .PEM 格式編碼(人類無法讀取)。需要檢視證書。在本節中,我們可以介紹使用 .PEM 檔案編碼的 OpenSSL 命令。
檢視 CSR 檔案條目
以下命令用於以純文字格式檢視 .CRT 檔案 Ex(domain.crt)的內容。
$ sudo openssl x509 -text -noout -in domain.crt Certificate: Data: Version: 3 (0x2) Serial Number: 9717655772991591277 (0x86dc0c706eb0136d) Signature Algorithm: sha256WithRSAEncryption Issuer: C=IN, ST=Telengana, L=Hyderabad, O=Ansol Pvt Ltd, OU=Application , CN=domainname.com/emailAddress=support@domainname.com Validity Not Before: Jun 13 14:23:52 2016 GMT Not After : Jun 13 14:23:52 2017 GMT Subject: C=IN, ST=Telengana, L=Hyderabad, O=Ansol Pvt Ltd, OU=Applicatio n, CN=domainname.com/emailAddress=support@domainname.com ….
使用私鑰
在本節中,我們將瞭解如何使用特定於建立和驗證私鑰的 OpenSSL 命令。
建立私鑰
以下是建立受密碼保護且 2048 位加密的私鑰檔案(例如 domain.key)的命令 -
$ openssl genrsa -des3 -out domain.key 2048
出現提示時輸入密碼以完成該過程。
驗證私鑰
以下是檢查我們生成的私鑰(例如:domain.key)是否為有效金鑰的命令
$ openssl rsa -check -in domain.key
如果私鑰已加密,系統將提示您輸入密碼短語。成功輸入後,未加密的金鑰將作為終端輸出。
在本文中,我們學習了一些 OpenSSL 命令及其用法,這些命令處理 SSL 證書,其中 OpenSSL 具有許多功能。我們將在未來學習更多功能和用法。我希望本文能幫助我們瞭解 OpenSSL 的一些基本功能。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP