# 生成root私钥
openssl genrsa -out root.key 1024
# 根据私钥创建根证书请求文件,需要输入一些证书的元信息:邮箱、域名等
openssl req -new -out root.csr -key root.key
# 结合私钥和请求文件,创建根证书,有效期10年
openssl x509 -req -in root.csr -out root.crt -signkey root.key -CAcreateserial -days 3650
# 创建服务端私钥
openssl genrsa -out server.key 1024
# 根据私钥生成请求文件
openssl req -new -out server.csr -key server.key
# 结合私钥和请求文件创建服务端证书,有效期10年
openssl x509 -req -in server.csr -out server.crt -signkey server.key -CA root.crt -CAkey root.key -CAcreateserial -days 3650
如果需要只需要部署服务端证书端话,就可以结束了。拿着server.crt公钥和server.key私钥部署在服务器上,然后解析域名到改服务器指向到IP,证书就部署成功了。
如果需要做双向验证的,也就是服务端要验证客户端证书的情况。那么需要在同一个根证书下再生成一个客户端证书
# 生成私钥
openssl genrsa -out client.key 1024
# 申请请求文件
openssl req -new -out client.csr -key client.key
# 生成证书
openssl x509 -req -in client.csr -out client.crt -signkey client.key -CA root.crt -CAkey root.key -CAcreateserial -days 3650
# 生成客户端集成证书pkcs12格式的文件,方便浏览器或者http客户端访问(密码:123456)
openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.p12
server {
listen 80;
listen 443 ssl;
server_name asgcode.test.com;
ssl on;
ssl_certificate /home/zhaoyingjie/asgcode/run/ssl/server.crt; # server公钥
ssl_certificate_key /home/zhaoyingjie/asgcode/run/ssl/server.key; #server私钥
ssl_client_certificate /home/zhaoyingjie/asgcode/run/ssl/root.crt; #根级证书公钥,用于验证各个二级client
ssl_verify_client on;
ssl_session_timeout 5m;
client_max_body_size 20m;
ssl_prefer_server_ciphers on;
access_log /data/asgcode/logs/nginx/access.log main;
error_log /data/asgcode/logs/nginx/error.log;
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods POST,GET,OPTIONS;
add_header Access-Control-Allow-Headers x-requseted-with,content-type;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
set_real_ip_from 0.0.0.0/0;
real_ip_header X-Forwarded-For;
include uwsgi_params;
uwsgi_pass 127.0.0.1:7060;
}
}
import requests
import json
if __name__ == '__main__':
CA_FILE = "/Users/mac/repose/asgcode/run/ssl/root.crt" # 根级证书公钥
KEY_FILE = "/Users/mac/repose/asgcode/run/ssl/client.key" # 客户端私钥
CERT_FILE = "/Users/mac/repose/asgcode/run/ssl/client.crt" # 客户端公钥
try: # cert
# 通过request()方法创建一个请求:
req = requests.get('https://asgcode.test.com/health', cert=(CERT_FILE, KEY_FILE), verify=False)
print(req.content)
print(req)
except Exception as ex:
print("Found Error in auth phase:%s" % str(ex))
curl -k --cert /Users/mac/repose/asgcode/run/ssl/client.crt --key /Users/mac/repose/asgcode/run/ssl/client.key https://asgcode.guoyafeng.com/health