在Ubuntu上使用OpenSSL實現雙向認證(也稱為客戶端證書認證)涉及幾個步驟。以下是一個基本的指南:
首先,你需要創建一個證書頒發機構(CA),并生成服務器和客戶端的證書和密鑰。
openssl genrsa -out ca.key 2048
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -subj /CN=MyCA
openssl genrsa -out server.key 2048
openssl req -new -key server.key -out server.csr -subj /CN=localhost
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365
openssl genrsa -out client.key 2048
openssl req -new -key client.key -out client.csr -subj /CN=client
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -days 365
編輯服務器配置文件(例如,對于Apache HTTP服務器,編輯/etc/apache2/sites-available/default-ssl.conf
),添加以下內容:
<VirtualHost *:443>
ServerName localhost
SSLEngine on
SSLCertificateFile /path/to/server.crt
SSLCertificateKeyFile /path/to/server.key
SSLCACertificateFile /path/to/ca.crt
SSLVerifyClient require
SSLVerifyDepth 2
# 其他配置...
</VirtualHost>
將客戶端的證書和密鑰配置到客戶端應用程序中。例如,如果你使用的是curl
,可以這樣配置:
curl --cacert /path/to/ca.crt --cert /path/to/client.crt --key /path/to/client.key https://localhost
啟動服務器并嘗試使用客戶端證書進行連接。如果一切配置正確,服務器應該要求客戶端提供有效的證書,并且只有有效的客戶端證書才能成功連接。
SSLVerifyDepth
和SSLOptions
。通過以上步驟,你可以在Ubuntu上使用OpenSSL實現雙向認證。