Nginx服務(wù)器SSL證書
生成pass key
下面的命令用于生成一個2048bit的pass key, -passout pass:111111 用于避免交互式輸入密碼
[tomcat@a02 tmp]$ openssl genrsa -aes256 -passout pass:111111 -out server.pass.key 2048
Generating RSA private key, 2048 bit long modulus
………..+++
…………………+++
e is 65537 (0x10001)
生成key
下面的命令用于生成私鑰, -passin pass:111111是和pass key的密碼對應(yīng)的, 用于避免交互式輸入密碼
[tomcat@a02 tmp]$ openssl rsa -passin pass:111111 -in server.pass.key -out server.key
writing RSA key
生成證書簽發(fā)請求文件(CSR)
下面的命令用于生成csr文件, 這里需要填寫機構(gòu)相關(guān)信息. 其中CN務(wù)必填寫為對應(yīng)的服務(wù)器域名. 最后那個challenge password, 是這個csr的password
[tomcat@a02 tmp]$ openssl req -new -sha256 -key server.key -out server.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) [XX]:CN
State or Province Name (full name) []:Beijing
Locality Name (eg, city) [Default City]:Chaoyang
Organization Name (eg, company) [Default Company Ltd]:HenSomeone
Organizational Unit Name (eg, section) []:iSomeone
Common Name (eg, your name or your server’s hostname) []:internal.someone.com
Email Address []:
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:222222
An optional company name []:
發(fā)送CSR文件給CA服務(wù)商簽發(fā)證書
如果是購買的CA服務(wù)商的SSL證書服務(wù), 這一步把CSR發(fā)給服務(wù)商就可以了. 收到證書后將內(nèi)容寫入到 server.pem 文件
在Nginx上這樣配置
server {
listen 443;
server_name www.example.com;
ssl on;
ssl_certificate /path/to/ssl/server.pem;
ssl_certificate_key /path/to/ssl/server.key;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_session_cache shared:ssl_www_example_com:5m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:DES-CBC3-SHA;
#…
location / {
#…
}
#…
}
制作自簽名證書
如果是打算制作自簽名證書, 則進行如下的操作生成pem證書
[tomcat@a02 tmp]$ openssl x509 -req -sha256 -days 3655 -in server.csr -signkey server.key -out server.pem
Signature ok
subject=/C=CN/ST=Beijing/L=Chaoyang/O=HenSomeone/OU=iSomeone/CN=internal.someone.com
Getting Private key
Nginx客戶端驗證證書
Nginx客戶端驗證證書和服務(wù)端SSL證書其實是沒關(guān)系的, 你可以一邊使用CA簽發(fā)的證書, 一邊使用自己制作的客戶端驗證證書.
生成服務(wù)器端私鑰
[tomcat@a02 tmp]$ openssl genrsa -aes256 -passout pass:201906 -out ca.pass.key 2048
Generating RSA private key, 2048 bit long modulus
…………………………………………………………………………………………………+++
……………………………..+++
e is 65537 (0x10001)
[tomcat@a02 tmp]$ openssl rsa -passin pass:201906 -in ca.pass.key -out ca.key
writing RSA key
生成服務(wù)器端證書
下面的命令會生成服務(wù)器證書ca.pem, 用于配制到nginx.
[tomcat@a02 tmp]$ openssl req -new -x509 -days 3655 -key ca.key -out ca.pem
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) [XX]:CN
State or Province Name (full name) []:Beijing
Locality Name (eg, city) [Default City]:Chaoyang
Organization Name (eg, company) [Default Company Ltd]:HenSomeone
Organizational Unit Name (eg, section) []:iSomeone
Common Name (eg, your name or your server’s hostname) []:internal.someone.com
Email Address []:
生成客戶端私鑰
[tomcat@a02 tmp]$ openssl genrsa -aes256 -passout pass:201906 -out client_01.pass.key 2048
Generating RSA private key, 2048 bit long modulus
……………………..+++
…..+++
e is 65537 (0x10001)
[tomcat@a02 tmp]$ openssl rsa -passin pass:201906 -in client_01.pass.key -out client_01.key
writing RSA key
生成客戶端證書簽發(fā)請求CSR
[tomcat@a02 tmp]$ openssl req -new -key client_01.key -out client_01.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) [XX]:CN
State or Province Name (full name) []:Beijing
Locality Name (eg, city) [Default City]:Chaoyang
Organization Name (eg, company) [Default Company Ltd]:HenSomeone
Organizational Unit Name (eg, section) []:Staff
Common Name (eg, your name or your server’s hostname) []:Staff
Email Address []:
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:201907
An optional company name []:
簽發(fā)客戶端證書
下面的命令, 用服務(wù)端的私鑰和服務(wù)端的證書, 對客戶端的CSR進行簽發(fā), 生成服務(wù)端證書. 這里有一個 -set_serial 01 的參數(shù), 如果簽發(fā)多個客戶端證書, 這個數(shù)字不能重復(fù)
[tomcat@a02 tmp]$ openssl x509 -req -days 3655 -in client_01.csr -CA ca.pem -CAkey ca.key -set_serial 01 -out client_01.pem
Signature ok
subject=/C=CN/ST=Beijing/L=Chaoyang/O=HenSomeone/OU=Staff/CN=Staff
Getting CA Private Key
客戶端證書格式轉(zhuǎn)換
前面生成的證書, 不能直接用于常見的應(yīng)用, 需要轉(zhuǎn)換成應(yīng)用需要的格式
Full PEM:
[tomcat@a02 tmp]$ cat client_01.key client_01.pem ca.pem > client_01.full.pem
PFX – 這里輸入的export password, 就是應(yīng)用導(dǎo)入PFX證書時需要輸入的密碼.
[tomcat@a02 tmp]$ openssl pkcs12 -export -out client_01.full.pfx -inkey client_01.key -in client_01.pem -certfile ca.pem
Enter Export Password:
Verifying – Enter Export Password:
配置Nginx的客戶端驗證證書
ssl_client_certificate /path/to/ca.pem;
ssl_verify_client optional; # or `on` if you require client key