背景

几年前,之前工作的部门为了方便连接到云环境私网网络,搭建了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加载新的证书文件。

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

标签: none

已有 16 条评论

  1. 友链已加,https://blog.yuanpei.me/links

  2. 蓝鸽@我是你 蓝鸽@我是你

    学习了几天没弄好,咨询朋友,给了个快速的方法:
    ./easyrsa renew server nopass
    就搞定啦 ,如果 是客户端
    ./easyrsa renew 客户端名 nopass 就OK啦 。

    1. 感谢您的回复,您的解决方案应该是最简单的,直接续期之前的证书。
      我文章中给出的步骤,是参考了当时安装OpenVPN时使用的一键安装脚本中生成证书的步骤,因此可以认为是重新执行一遍生成证书的过程。

    2. wanglei wanglei

      请教一下,客户端延期怎么设置?客户端的ovpn文件不用替换吗?

      1. 不需要替换,我在服务端更新完证书的有效时间后,客户端重拨即可正常上线。

    3. Johnnian Johnnian

      这个亲测有用,感谢!

      1. 感谢您的回复,很高兴帮到您!

    4. 醉落西风 醉落西风

      你好,我操作完以后,还是显示证书过期,服务器里面显示证书已经续期到2030年了 ,方便指导一下吗

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

    5. lost lost

      我执行renew但提示Unable to renew as the input file is not a valid certificate

      1. 看错误信息像是证书不合法,文件可能指定错误或者文件损坏,可能需要从头开始新建一个证书了。

    6. 222 222

      Certificate expires in more than 30 days.
      Renewal not allowed.

      1. 看错误信息是证书过期太久无法续期。只能考虑重新生成证书了。

  3. wanglei wanglei

    您的文章所述操作完成后,客户端是否用更新证书吗?还是说只是服务器端更新即可?

    1. 我的实践中,不需要更新客户端配置。直接重拨即可。

添加新评论