«

记一次OpenVPN因证书有效期到期更换新证书的过程

时间:2022-10-11 23:28     作者:Anglei     分类: Linux


背景

几年前,之前工作的部门为了方便连接到云环境私网网络,搭建了OpenVPN服务端,所有同事连接之后即可拨入私网网络进行直连访问与程序调试。某天,部门负责人联系说所有客户端均无法拨入,查看服务端日志发现如下错误信息:

Thu Apr 14 16:53:41 2022 27.115.3.186:16890 TLS Error: TLS handshake failed
Thu Apr 14 16:53:41 2022 27.115.3.186:16890 SIGUSR1[soft,tls-error] received, client-instance restarting
Thu Apr 14 16:53:41 2022 27.115.3.186:16949 TLS: Initial packet from [AF_INET]27.115.3.186:16949, sid=b5820c7e 6a773959
Thu Apr 14 16:53:41 2022 27.115.3.186:16950 TLS: Initial packet from [AF_INET]27.115.3.186:16950, sid=cfca97f5 18cff2fe
Thu Apr 14 16:53:42 2022 27.115.3.186:16893 TLS Error: TLS key negotiation failed to occur within 60 seconds (check your network connectivity)

原因
查看上下文,发现了报错信息中的关键点:

Thu Apr 14 16:29:58 2022 WARNING: Your certificate has expired!
于是找到OpenVPN服务端配置目录“/etc/openvpn”,使用如下命令进行证书有效期验证:

openssl x509 -noout -text -in server.crt
验证结果如下(隐去部分关键信息):

Certificate:
Data:
Version: 3 (0x2)
Serial Number:
3e:e4:c1:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:93
Signature Algorithm: sha256WithRSAEncryption
Issuer: CN=Easy-RSA CA
Validity
Not Before: Apr 30 04:54:55 2019 GMT
Not After : Apr 14 04:54:55 2022 GMT
Subject: CN=server
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
00:b8:36:xx:xx:xx:xx:xx:xx:xx:xx:3a:06:5e:b2:
...

可以看到,“Not After”字段明确指示了该证书的过期时间,为报障当天中午12天54分(GMT时间根据东八区加8小时)。

当时安装OpenVPN服务端时,因为计划是临时使用一段时间,所以证书的有效期仅仅设置了3年,没想到3年过去了,这个拨入途径已经成为了部门内正常开展工作的基础设施……

(P.S. 遇到问题经常是先临时解决问题,后面再想更好的方案。但用着用着,这个临时的方案就成为了常态化方案……)

处理过程
既然证书已过期,那么现在的当务之急就是需要尽快替换新证书,保障其他同事尽早恢复正常工作。

首先新建一个空文件夹,用于存放新证书相关文件。

mkdir /etc/openvpn/cert_new

当时搭建OpenVPN时,使用的是easy-rsa进行证书生成,因此将之前的easy-rsa拷贝至cert_new文件夹,

cp -r /etc/openvpn/easy-rsa /etc/openvpn/cert_new

进入新的easy-rsa文件夹,删除旧的pki文件夹。

rm -rf pki

使用“easyrsa”命令新建pki目录:

[root@xxx easy-rsa]# ./easyrsa init-pki

init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/openvpn/cert_new/easy-rsa/pki

生成ca证书:

[root@xxx easy-rsa]# ./easyrsa --batch build-ca nopass

Generating RSA private key, 2048 bit long modulus
...............................................................................................................+++
...................+++
e is 65537 (0x10001)

生成服务端证书(前面的环境变量代表证书超时天数为3650天):

[root@xxx easy-rsa]# EASYRSA_CERT_EXPIRE=3650 ./easyrsa build-server-full server nopass

*Using SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017
Generating a 2048 bit RSA private key
................+++
......+++
writing new private key to '/etc/openvpn/cert_new/easy-rsa/pki/private/server.key.c8ybv0BPWo'

Using configuration from /etc/openvpn/cert_new/easy-rsa/pki/safessl-easyrsa.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName :ASN.1 12:'server'
Certificate is to be certified until Apr 26 07:27:46 2032 GMT (3650 days)

Write out database with 1 new entries
Data Base Updated

生成客户端证书:

[root@xxx easy-rsa]# EASYRSA_CERT_EXPIRE=3650 ./easyrsa build-client-full client nopass

Using SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017
Generating a 2048 bit RSA private key
.........+++
.............+++
writing new private key to '/etc/openvpn/cert_new/easy-rsa/pki/private/client.key.nsEQpRpArw'

Using configuration from /etc/openvpn/cert_new/easy-rsa/pki/safessl-easyrsa.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName :ASN.1 12:'client'
Certificate is to be certified until Apr 26 07:30:30 2032 GMT (3650 days)

Write out database with 1 new entries
Data Base Updated

生成crl.pem文件:

[root@xxx easy-rsa]# EASYRSA_CRL_DAYS=3650 ./easyrsa gen-crl

Using SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017
Using configuration from /etc/openvpn/cert_new/easy-rsa/pki/safessl-easyrsa.cnf

An updated CRL has been created.
CRL file: /etc/openvpn/cert_new/easy-rsa/pki/crl.pem

将这些文件统一复制到/etc/openvpn/cert_new目录:

cp pki/ca.crt pki/private/ca.key pki/issued/server.crt pki/private/server.key pki/crl.pem /etc/openvpn/cert_new

为避免权限问题,将crl.pem的所有者改为nobody:

chown nobody:nobody crl.pem

进入/etc/openvpn/cert_new目录,使用openssl命令验证证书有效性:

[root@xxx cert_new]# openssl verify -CAfile ca.crt -purpose sslserver server.crt

server.crt: OK

生成OpenVPN所需的secret文件ta.key:

openvpn --genkey --secret ta.key

将所有需要的文件复制到/etc/openvpn

cp ca.crt ca.key crl.pem easy-rsa server.crt server.key ta.key /etc/openvpn

重启OpenVPN服务,即可使OpenVPN加载新的证书文件。

通知所有客户端重新拨号,成功拨入,问题解决。

还有一种更简单的方法,直接续期之前的证书

本文完结,相关标签: openvpn

 版权所有:Anglei
 文章标题:记一次OpenVPN因证书有效期到期更换新证书的过程
 除非注明,本站文章如未特殊说明均为 MAXADA社区知识库 原创,且版权所有,请勿用于任何商业用途。

推荐阅读:

看完后感想如何?

路过(0)

雷人(0)

握手(0)

鲜花(0)

鸡蛋(0)
分享到: