I want to generate clients key with PHP. When a client key generated it should give me the expiry date of the key.
root@zohaib-VirtualBox:/etc/openvpn/easy-rsa# ./build-key client1
Generating a 2048 bit RSA private key .............................................................+++ ............................+++
writing new private key to 'client1.key' You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value,
If you enter '.', the field will be left blank. Country Name (2 letter code) [GB]:
State or Province Name (full name) [London]:
Locality Name (eg, city) [London]:
Organization Name (eg, company) [Org]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) [client1]:
Name [OrgServer]:
Email Address [admin@org.com]:
Please enter the following 'extra' attributes to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/easy-rsa/openssl-1.0.0.cnf
Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'GB'
stateOrProvinceName :PRINTABLE:'London'
localityName :PRINTABLE:'London'
organizationName :PRINTABLE:'Org'
commonName :PRINTABLE:'client1'
name :PRINTABLE:'OrgServer'
emailAddress :IA5STRING:'admin@gamban.com'
Certificate is to be certified until Apr 21 15:43:47 2026 GMT (3650 days) Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
root@zohaib-VirtualBox:/etc/openvpn/easy-rsa#
You can use shell_exec and get the result to use, for example, with a regex to match expiry the date of key etc, i.e.:
The result of the command will held on var
$ovpnKey
.Update:
To automatize the creation of new OpenVPN client certificates, use the following script. Make sure you edit, at least, the following variables
OPENVPN_RSA_DIR
OPENVPN_KEYS
KEY_DOWNLOAD_PATH
Save the above bash script as
new-openvpn-client.sh
and give it execute permissions.Then use php
shell_exec
to generate the keys:Sources:
https://gist.github.com/hcooper/814247