nginx ssl双向认证实战

本次实验所使用操作系统为Ubuntu20.04,通过openssl工具生成所有的ssl证书。

1、生成自签CA证书

CA证书的作用是签发服务器和客户端证书,并且用于校验客户端的证书。

# 生成CA的私钥文件
root@k8s-master1:~/ssl# openssl genrsa -out ca.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
..................................................................................................................................+++++
.........................................+++++
e is 65537 (0x010001)
root@k8s-master1:~/ssl# 
# 生成CA证书,-subj参数可以免去交互式
root@k8s-master1:~/ssl# openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -subj "/C=CN/O=People's Republic of China/CN=China CA"
root@k8s-master1:~/ssl# 
root@k8s-master1:~/ssl# ls
ca.crt  ca.key
root@k8s-master1:~/ssl# 
# 通过openssl工具查看CA证书的内容
root@k8s-master1:~/ssl# openssl x509 -text -noout -in ca.crt 
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            0e:91:a9:4f:4b:b5:22:65:8f:f9:f4:13:10:a6:03:c6:f3:80:fd:b2
        Signature Algorithm: sha256WithRSAEncryption
        Issuer: C = CN, O = People's Republic of China, CN = China CA
        Validity
            Not Before: Oct 27 12:08:35 2022 GMT
            Not After : Oct 24 12:08:35 2032 GMT
        Subject: C = CN, O = People's Republic of China, CN = China CA
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                RSA Public-Key: (2048 bit)
                Modulus:
                    00:dd:00:26:20:9a:f2:ed:94:e5:30:99:fe:38:ae:
                    ...
                    ...
                    7d:56:26:48:35:a6:97:ac:1d:4c:23:dd:39:76:89:
                    31:5b
                Exponent: 65537 (0x10001)
        X509v3 extensions:
            X509v3 Subject Key Identifier: 
                42:19:EB:18:05:C7:A0:AB:E5:6C:93:9C:FC:EB:F6:E1:5F:93:E2:E1
            X509v3 Authority Key Identifier: 
                keyid:42:19:EB:18:05:C7:A0:AB:E5:6C:93:9C:FC:EB:F6:E1:5F:93:E2:E1

            X509v3 Basic Constraints: critical
                CA:TRUE
    Signature Algorithm: sha256WithRSAEncryption
         1c:e3:a5:45:c3:b6:1e:16:d2:d7:58:93:3d:0c:79:8f:6a:b0:
         ...
         ...
         6f:e6:d5:0e:71:92:ca:65:cd:1a:4b:7c:2b:f3:08:dd:bc:f7:
         dc:dd:c1:a3
root@k8s-master1:~/ssl# 
2、生成自签服务器证书

-subj 参数很重要,其中/CN要填写具体的域名,要与nginx的server_name匹配上。

chrome浏览器对证书很严格,证书必须要有SAN(Subject Alternative Name)。
v3_req表示生成是X.509 v3和SAN的证书,-extfile和-extensions这两个参数是可选的,如果没有这两个参数,那证书就是没有SAN的,推荐加上这两个参数。

# 生成服务器的私钥文件
root@k8s-master1:~/ssl# openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
..............................................+++++
..............................................+++++
e is 65537 (0x010001)
root@k8s-master1:~/ssl# 
# 生成服务器的证书签发文件,即csr后缀的文件
root@k8s-master1:~/ssl# openssl req -new -key server.key -out server.csr -subj "/C=CN/O=People's Republic of China/CN=example.com"
root@k8s-master1:~/ssl# 
# 使用CA签发服务器证书
root@k8s-master1:~/ssl# openssl x509 -req -in server.csr -out server.crt -CA ca.crt -CAkey ca.key -CAcreateserial -days 3650 -extfile <(sed "/\[ v3_req \]/ a\subjectAltName = @alt_names" /etc/ssl/openssl.cnf <(printf "\n[alt_names]\nDNS.1=example.com\nDNS.2=www.example.com")) -extensions v3_req
Signature ok
subject=C = CN, O = People's Republic of China, CN = example.com
Getting CA Private Key
root@k8s-master1:~/ssl# 
root@k8s-master1:~/ssl# ls
ca.crt  ca.key  ca.srl  server.crt  server.csr  server.key
root@k8s-master1:~/ssl# 
root@k8s-master1:~/ssl# openssl x509 -text -noout -in server.crt 
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            30:be:cf:e7:ce:18:b5:3c:04:a1:da:74:d0:8e:7c:a5:e9:08:7d:3d
        Signature Algorithm: sha256WithRSAEncryption
        Issuer: C = CN, O = People's Republic of China, CN = China CA
        Validity
            Not Before: Oct 27 12:12:32 2022 GMT
            Not After : Oct 24 12:12:32 2032 GMT
        Subject: C = CN, O = People's Republic of China, CN = example.com
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                RSA Public-Key: (2048 bit)
                Modulus:
                    00:b2:63:3f:85:f0:99:8a:13:f8:62:3d:d3:5f:60:
                    ...
                    ...
                    3f:a2:b3:a1:8a:d4:b3:48:b9:b4:18:b5:10:a0:b1:
                    61:ab
                Exponent: 65537 (0x10001)
        X509v3 extensions:
            X509v3 Subject Alternative Name: 
                DNS:example.com, DNS:www.example.com
            X509v3 Basic Constraints: 
                CA:FALSE
            X509v3 Key Usage: 
                Digital Signature, Non Repudiation, Key Encipherment
    Signature Algorithm: sha256WithRSAEncryption
         42:f0:fa:93:b9:75:5b:6a:63:4f:a9:c5:c2:c8:5a:e9:ca:9e:
         ...
         ...
         10:10:94:72:92:56:bf:e6:91:97:1f:b0:54:2c:6e:37:22:59:
         9f:bd:c2:67
root@k8s-master1:~/ssl# 
3、配置nginx,开启ssl双向认证

nginx ssl双向认证配置文件的关键内容如下。

server {
        listen       443 ssl;
        listen       [::]:443 ssl;
        server_name  example.com www.example.com;

        # 服务器证书所在路径
        ssl_certificate "/etc/nginx/ssl/server.crt";
        ssl_certificate_key "/etc/nginx/ssl/server.key";
        ssl_protocols TLSv1.2 TLSv1.3;

        # 开启ssl双向认证
        ssl_verify_client on; 
        # 使用ca.crt校验客户端证书,由该ca.crt签发的证书才可以通过校验
        ssl_client_certificate "/etc/nginx/ssl/ca.crt";

        location / {
                return 200 "success";
        }
}
4、生成客户端证书

客户端证书步骤跟服务器证书一样,只是参数不一样。

# 生成客户端的私钥文件
root@k8s-master1:~/ssl# openssl genrsa -out client.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
..............+++++
..........................................................................+++++
e is 65537 (0x010001)
root@k8s-master1:~/ssl# 
# 生成客户端的证书签发文件
root@k8s-master1:~/ssl# openssl req -new -key client.key -out client.csr -subj "/C=CN/O=People's Republic of China/CN=Private certificate assigned to Tom"
root@k8s-master1:~/ssl# 
# 使用CA签发客户端证书
root@k8s-master1:~/ssl# openssl x509 -req -in client.csr -out client.crt -CA ca.crt -CAkey ca.key -CAcreateserial -days 3650
Signature ok
subject=C = CN, O = People's Republic of China, CN = Private certificate assigned to Tom
Getting CA Private Key
root@k8s-master1:~/ssl# 
root@k8s-master1:~/ssl# ls
ca.crt  ca.key  ca.srl  client.crt  client.csr  client.key  server.crt  server.csr  server.key
root@k8s-master1:~/ssl# 
root@k8s-master1:~/ssl# openssl x509 -text -noout -in client.crt 
Certificate:
    Data:
        Version: 1 (0x0)
        Serial Number:
            30:be:cf:e7:ce:18:b5:3c:04:a1:da:74:d0:8e:7c:a5:e9:08:7d:3e
        Signature Algorithm: sha256WithRSAEncryption
        Issuer: C = CN, O = People's Republic of China, CN = China CA
        Validity
            Not Before: Oct 27 12:24:28 2022 GMT
            Not After : Oct 24 12:24:28 2032 GMT
        Subject: C = CN, O = People's Republic of China, CN = Private certificate assigned to Tom
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                RSA Public-Key: (2048 bit)
                Modulus:
                    00:c1:ed:df:46:b9:8b:3a:e9:3e:ed:09:29:a6:56:
                    ...
                    ...
                    3b:87:22:39:87:8b:ce:bd:e4:f4:28:94:a4:83:95:
                    a2:df
                Exponent: 65537 (0x10001)
    Signature Algorithm: sha256WithRSAEncryption
         46:04:fe:6a:b1:c9:9b:df:f6:8e:ac:c4:e2:dd:a1:75:1a:f4:
         ...
         ...
         ef:94:84:c5
root@k8s-master1:~/ssl# 
5、使用curl测试ssl双向认证

curl没有使用客户端证书去请求,会报400错误。如果使用的客户端证书不是由特定CA签发的,也同样会报400错误。

root@k8s-master1:~/ssl# curl -k -vo /dev/null https://www.example.com/ --resolve www.example.com:443:127.0.0.1
* Added www.example.com:443:127.0.0.1 to DNS cache
* Hostname www.example.com was found in DNS cache
*   Trying 127.0.0.1:443...
* TCP_NODELAY set
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to www.example.com (127.0.0.1) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
} [5 bytes data]
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
...
...
* TLSv1.3 (OUT), TLS handshake, Finished (20):
} [52 bytes data]
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: C=CN; O=People's Republic of China; CN=example.com
*  start date: Oct 27 12:12:32 2022 GMT
*  expire date: Oct 24 12:12:32 2032 GMT
*  issuer: C=CN; O=People's Republic of China; CN=China CA
*  SSL certificate verify result: certificate signature failure (7), continuing anyway.
} [5 bytes data]
> GET / HTTP/1.1
> Host: www.example.com
> User-Agent: curl/7.68.0
> Accept: */*
> 
{ [5 bytes data]
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
{ [265 bytes data]
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
{ [265 bytes data]
* old SSL session ID is stale, removing
{ [5 bytes data]
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 Bad Request
< Server: nginx
< Date: Thu, 27 Oct 2022 12:39:16 GMT
< Content-Type: text/html
< Content-Length: 230
< Connection: close
< 
{ [230 bytes data]
100   230  100   230    0     0  20909      0 --:--:-- --:--:-- --:--:-- 20909
* Closing connection 0
} [5 bytes data]
* TLSv1.3 (OUT), TLS alert, close notify (256):
} [2 bytes data]
root@k8s-master1:~/ssl# 
root@k8s-master1:~/ssl# curl -k https://www.example.com/ --resolve www.example.com:443:127.0.0.1
<html>
<head><title>400 No required SSL certificate was sent</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<center>No required SSL certificate was sent</center>
<hr><center>nginx</center>
</body>
</html>
root@k8s-master1:~/ssl# 
root@k8s-master1:~/ssl# 

--cert和--key参数指定客户端证书,服务器校验证书没问题就能正常访问。
也可以将客户端证书转换成PC浏览器用的证书格式,命令如下。

openssl pkcs12 -export -clcerts -out client.p12 -in client.crt -inkey client.key

温馨提示:不需要密码就按回车键。

root@k8s-master1:~/ssl# curl -k -vo /dev/null https://www.example.com/ --resolve www.example.com:443:127.0.0.1 --cert ./client.crt --key ./client.key
* Added www.example.com:443:127.0.0.1 to DNS cache
* Hostname www.example.com was found in DNS cache
*   Trying 127.0.0.1:443...
* TCP_NODELAY set
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to www.example.com (127.0.0.1) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
} [5 bytes data]
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
...
...
* TLSv1.3 (OUT), TLS handshake, Finished (20):
} [52 bytes data]
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: C=CN; O=People's Republic of China; CN=example.com
*  start date: Oct 27 12:12:32 2022 GMT
*  expire date: Oct 24 12:12:32 2032 GMT
*  issuer: C=CN; O=People's Republic of China; CN=China CA
*  SSL certificate verify result: certificate signature failure (7), continuing anyway.
} [5 bytes data]
> GET / HTTP/1.1
> Host: www.example.com
> User-Agent: curl/7.68.0
> Accept: */*
> 
{ [5 bytes data]
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
{ [1097 bytes data]
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
{ [1097 bytes data]
* old SSL session ID is stale, removing
{ [5 bytes data]
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx
< Date: Thu, 27 Oct 2022 12:46:20 GMT
< Content-Type: application/octet-stream
< Content-Length: 7
< Connection: keep-alive
< 
{ [7 bytes data]
100     7  100     7    0     0    583      0 --:--:-- --:--:-- --:--:--   583
* Connection #0 to host www.example.com left intact
root@k8s-master1:~/ssl# 
6、命令汇总
生成自签CA证书
openssl genrsa -out ca.key 2048
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -subj "/C=CN/O=People's Republic of China/CN=China CA"
生成服务器证书
openssl genrsa -out server.key 2048
openssl req -new -key server.key -out server.csr -subj "/C=CN/O=People's Republic of China/CN=example.com"
openssl x509 -req -in server.csr -out server.crt -CA ca.crt -CAkey ca.key -CAcreateserial -days 3650 -extfile <(sed "/\[ v3_req \]/ a\subjectAltName = @alt_names" /etc/ssl/openssl.cnf <(printf "\n[alt_names]\nDNS.1=example.com\nDNS.2=www.example.com")) -extensions v3_req
生成客户端证书
openssl genrsa -out client.key 2048
openssl req -new -key client.key -out client.csr -subj "/C=CN/O=People's Republic of China/CN=Private certificate assigned to Tom"
openssl x509 -req -in client.csr -out client.crt -CA ca.crt -CAkey ca.key -CAcreateserial -days 3650
curl命令
curl -k -vo /dev/null https://www.example.com/ --resolve www.example.com:443:127.0.0.1
curl -k https://www.example.com/ --resolve www.example.com:443:127.0.0.1
curl -k -vo /dev/null https://www.example.com/ --resolve www.example.com:443:127.0.0.1 --cert ./client.crt --key ./client.key
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,080评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,422评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,630评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,554评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,662评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,856评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,014评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,752评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,212评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,541评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,687评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,347评论 4 331
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,973评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,777评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,006评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,406评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,576评论 2 349

推荐阅读更多精彩内容