1 SSL 服務器 / 客戶端雙向驗證證書的生成
1.1 創建一個新的 CA 根證書(使用root用戶)
在 nginx 安裝目錄下新建 ca 文件夾,進入 ca,創建幾個子文件夾:
mkdir ca
cd ca
mkdir newcerts private conf server
newcerts 子目錄將用於存放 CA 簽署過的數字證書(證書備份目錄);private 用於存放 CA 的私鑰;conf 目錄用於存放一些簡化參數用的配置文件;server 存放服務器證書文件。
1.2conf 目錄新建 openssl.conf 文件
編輯其內容如下:
[ ca ]
default_ca = foo # The default ca section
[ foo ]
dir = ./ # top dir
database = ./index.txt # index file.
new_certs_dir = ./newcerts # new certs dir
certificate = ./private/ca.crt # The CA cert
serial = ./serial # serial no file
private_key = ./private/ca.key # CA private key
RANDFILE = ./private/.rand # random number file
default_days = 3650 # how long to certify for
default_crl_days= 30 # how long before next CRL
default_md = sha256 # message digest method to use
unique_subject = no # Set to 'no' to allow creation of
# several ctificates with same subject.
policy = policy_any # default policy
[ policy_any ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = match
localityName = optional
commonName = supplied
emailAddress = optional
1.3 生成私鑰 key 文件
openssl genrsa -out private/ca.key 2048
輸出
Generating RSA private key, 2048 bit long modulus
.......+++
.........................+++
e is 65537 (0x10001)
private 目錄下有 ca.key 文件生成。
openssl 默認生成 512 位的。一般是用 2048 位的。
1.4 生成證書請求 csr 文件
openssl req -new -key private/ca.key -out private/ca.csr
提示輸入 Country Name,輸入 CN 並回車后:
提示輸入 State or Province Name (full name),輸入 Shanghai 並回車后:
提示輸入 Locality Name,輸入 Shanghai 並回車后:
提示輸入 Organization Name,輸入 Defonds 並回車后:
提示輸入 Organizational Unit Name,輸入 Dev 並回車后:
提示輸入 Common Name,如果沒有域名的話,輸入 localhost 並回車后:
提示輸入 Email Address,輸入 defonds@163.com 並回車后:
提示輸入 A challenge password,這個是根證書口令。輸入 defonds 並回車后:
提示輸入 An optional company name,輸入 df 並回車。private 目錄下有 ca.csr 文件生成。
1.5 生成憑證 crt 文件
openssl x509 -req -days 365 -in private/ca.csr -signkey private/ca.key -out private/ca.crt
控制台輸出
Signature ok
subject=/C=CN/ST=Shanghai/L=Shanghai/O=Defonds/OU=Dev/CN=localhost/emailAddress=defonds@163.com
Getting Private key
private 目錄下有 ca.crt 文件生成。
1.6 為我們的 key 設置起始序列號
echo FACE > serial
可以是任意四個字符
1.7創建 CA 鍵庫
touch index.txt
1.7 為 "用戶證書" 的移除創建一個證書撤銷列表
openssl ca -gencrl -out ./private/ca.crl -crldays 7 -config "./conf/openssl.conf"
# 輸出
Using configuration from ./conf/openssl.conf
private 目錄下有 ca.crl 文件生成。
2 服務器證書的生成
2.1 創建一個 key
openssl genrsa -out server/server.key
輸出
Generating RSA private key, 512 bit long modulus
...........................++++++++++++
.................++++++++++++
e is 65537 (0x10001)
server 目錄下有 server.key 文件生成。
openssl 默認生成 512 位的。一般是用 2048 位的:sudo openssl genrsa -out server/server.key 2048
2.2 為我們的 key 創建一個證書簽名請求 csr 文件
openssl req -new -key server/server.key -out server/server.csr
這時會要求你輸入和 2.1.2.2 步一樣的那些問題,所有輸入需要和那一步一致。但 A challenge password 是服務器證書口令,可以與根證書口令一致。這里:
server 目錄下有 server.csr 文件生成。
2.3 使用我們私有的 CA key 為剛才的 key 簽名
openssl ca -in server/server.csr -cert private/ca.crt -keyfile private/ca.key -out server/server.crt -config "/usr/local/nginx/ca/conf/openssl.conf"
輸出
兩次都輸入 y,server 目錄下有 server.crt 文件生成。
3 客戶端證書的生成
3.1 創建存放 key 的目錄 users
mkdir users
位置 /usr/local/nginx/ca/users。
3.2 為用戶創建一個 key
openssl genrsa -des3 -out /usr/local/nginx/ca/users/client.key 1024
要求輸入 pass phrase,這個是當前 key 的口令,以防止本密鑰泄漏后被人盜用。兩次輸入同一個密碼(比如我這里輸入 defonds),users 目錄下有 client.key 文件生成。
3.3 為 key 創建一個證書簽名請求 csr 文件
openssl req -new -key /usr/local/nginx/ca/users/client.key -out /usr/local/nginx/ca/users/client.csr
提示輸入 pass phrase,即 client.key 的口令。將 2.3.2 步保存的 pass phrase 輸入后並回車:
要求你輸入和 1.3 步一樣的那些問題。輸入需要和那一步一致。但 A challenge password 是客戶端證書口令(請注意將它和 client.key 的口令區分開!),可以與服務器端證書或者根證書口令一致:
users 目錄下有 client.csr 文件生成。
3.4 使用我們私有的 CA key 為剛才的 key 簽名
openssl ca -in /usr/local/nginx/ca/users/client.csr -cert /usr/local/nginx/ca/private/ca.crt -keyfile /usr/local/nginx/ca/private/ca.key -out /usr/local/nginx/ca/users/client.crt -config "/usr/local/nginx/ca/conf/openssl.conf"
輸出
Using configuration from /usr/local/nginx/ca/conf/openssl.conf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'CN'
stateOrProvinceName :PRINTABLE:'Shanghai'
localityName :PRINTABLE:'Shanghai'
organizationName :PRINTABLE:'Defonds'
organizationalUnitName:PRINTABLE:'Dev'
commonName :PRINTABLE:'localhost'
emailAddress :IA5STRING:'defonds@163.com'
Certificate is to be certified until Mar 16 11:47:48 2016 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
兩次都輸入 y,users 目錄下有 client.crt 文件生成。
3.5 將證書轉換為大多數瀏覽器都能識別的 PKCS12 文件
openssl pkcs12 -export -clcerts -in /usr/local/nginx/ca/users/client.crt -inkey /usr/local/nginx/ca/users/client.key -out /usr/local/nginx/ca/users/client.p12
要求輸入 client.key 的 pass phrase,輸入 2.3.2 步輸入的 pass phrase 並回車后:
要求輸入 Export Password,這個是客戶端證書的保護密碼(其作用類似於 2.3.3 保存的口令),在客戶端安裝證書的時候需要輸入這個密碼。我還是輸入 defonds。users 目錄下有 client.p12 文件生成。
該文章來源為:https://blog.csdn.net/ronon77/article/details/84751088
記錄該文章主要是為了方便自己以后生成ssl方便,僅記錄學習膜拜。未做其他用途