背景

部门负责人联系说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年过去了,这个拨入途径已经成为了部门内正常开展工作的基础设施……

处理方法

方法一、续期证书

#服务端
./easyrsa renew server nopass

#客户端
./easyrsa renew 客户端名 nopass

方法二、生成新证书并替换

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

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加载新的证书文件。

注意事项:

1、确保文件目录与旧的路径保持一致,并确保OpenVPN服务进程重启过。不行就重启机器试一下。

2、不需要更新客户端配置。直接重拨即可。