Last active
April 25, 2022 20:23
-
-
Save BigNerd95/b122dcdb491bca9c8fb0fd0eaafbfb93 to your computer and use it in GitHub Desktop.
Create openvpn config files for openwrt on a PC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Change SERVER.DOMAIN.OR.IP in client.ovpn | |
write_conf(){ | |
export EASYRSA_CERT_EXPIRE=3650 | |
export EASYRSA_CRL_DAYS=3650 | |
export EASYRSA_PKI="pki" | |
export EASYRSA_REQ_CN="vpnca" | |
cat << 'EOF' > "pki/openssl-easyrsa.cnf" | |
RANDFILE = $ENV::EASYRSA_PKI/.rnd | |
#################################################################### | |
[ ca ] | |
default_ca = CA_default # The default ca section | |
#################################################################### | |
[ CA_default ] | |
dir = $ENV::EASYRSA_PKI # Where everything is kept | |
certs = $dir # Where the issued certs are kept | |
crl_dir = $dir # Where the issued crl are kept | |
database = $dir/index.txt # database index file. | |
new_certs_dir = $dir/certs_by_serial # default place for new certs. | |
certificate = $dir/ca.crt # The CA certificate | |
serial = $dir/serial # The current serial number | |
crl = $dir/crl.pem # The current CRL | |
private_key = $dir/private/ca.key # The private key | |
RANDFILE = $dir/.rand # private random number file | |
x509_extensions = basic_exts # The extentions to add to the cert | |
# This allows a V2 CRL. Ancient browsers don't like it, but anything Easy-RSA | |
# is designed for will. In return, we get the Issuer attached to CRLs. | |
crl_extensions = crl_ext | |
default_days = $ENV::EASYRSA_CERT_EXPIRE # how long to certify for | |
default_crl_days= $ENV::EASYRSA_CRL_DAYS # how long before next CRL | |
default_md = $ENV::EASYRSA_DIGEST # use public key default MD | |
preserve = no # keep passed DN ordering | |
# A few difference way of specifying how similar the request should look | |
# For type CA, the listed attributes must be the same, and the optional | |
# and supplied fields are just that :-) | |
policy = policy_anything | |
# For the 'anything' policy, which defines allowed DN fields | |
[ policy_anything ] | |
countryName = optional | |
stateOrProvinceName = optional | |
localityName = optional | |
organizationName = optional | |
organizationalUnitName = optional | |
commonName = supplied | |
name = optional | |
emailAddress = optional | |
#################################################################### | |
# Easy-RSA request handling | |
# We key off $DN_MODE to determine how to format the DN | |
[ req ] | |
default_bits = $ENV::EASYRSA_KEY_SIZE | |
default_keyfile = privkey.pem | |
default_md = $ENV::EASYRSA_DIGEST | |
distinguished_name = $ENV::EASYRSA_DN | |
x509_extensions = easyrsa_ca # The extentions to add to the self signed cert | |
# A placeholder to handle the $EXTRA_EXTS feature: | |
#%EXTRA_EXTS% # Do NOT remove or change this line as $EXTRA_EXTS support requires it | |
#################################################################### | |
# Easy-RSA DN (Subject) handling | |
# Easy-RSA DN for cn_only support: | |
[ cn_only ] | |
commonName = Common Name (eg: your user, host, or server name) | |
commonName_max = 64 | |
commonName_default = $ENV::EASYRSA_REQ_CN | |
# Easy-RSA DN for org support: | |
[ org ] | |
countryName = Country Name (2 letter code) | |
countryName_default = $ENV::EASYRSA_REQ_COUNTRY | |
countryName_min = 2 | |
countryName_max = 2 | |
stateOrProvinceName = State or Province Name (full name) | |
stateOrProvinceName_default = $ENV::EASYRSA_REQ_PROVINCE | |
localityName = Locality Name (eg, city) | |
localityName_default = $ENV::EASYRSA_REQ_CITY | |
0.organizationName = Organization Name (eg, company) | |
0.organizationName_default = $ENV::EASYRSA_REQ_ORG | |
organizationalUnitName = Organizational Unit Name (eg, section) | |
organizationalUnitName_default = $ENV::EASYRSA_REQ_OU | |
commonName = Common Name (eg: your user, host, or server name) | |
commonName_max = 64 | |
commonName_default = $ENV::EASYRSA_REQ_CN | |
emailAddress = Email Address | |
emailAddress_default = $ENV::EASYRSA_REQ_EMAIL | |
emailAddress_max = 64 | |
#################################################################### | |
# Easy-RSA cert extension handling | |
# This section is effectively unused as the main script sets extensions | |
# dynamically. This core section is left to support the odd usecase where | |
# a user calls openssl directly. | |
[ basic_exts ] | |
basicConstraints = CA:FALSE | |
subjectKeyIdentifier = hash | |
authorityKeyIdentifier = keyid,issuer:always | |
# The Easy-RSA CA extensions | |
[ easyrsa_ca ] | |
# PKIX recommendations: | |
subjectKeyIdentifier=hash | |
authorityKeyIdentifier=keyid:always,issuer:always | |
# This could be marked critical, but it's nice to support reading by any | |
# broken clients who attempt to do so. | |
basicConstraints = CA:true | |
# Limit key usage to CA tasks. If you really want to use the generated pair as | |
# a self-signed cert, comment this out. | |
keyUsage = cRLSign, keyCertSign | |
# nsCertType omitted by default. Let's try to let the deprecated stuff die. | |
# nsCertType = sslCA | |
# CRL extensions. | |
[ crl_ext ] | |
# Only issuerAltName and authorityKeyIdentifier make any sense in a CRL. | |
# issuerAltName=issuer:copy | |
authorityKeyIdentifier=keyid:always,issuer:always | |
EOF | |
} | |
writeserver(){ | |
cat << EOF > "server.conf" | |
verb 3 | |
user nobody | |
group nogroup | |
dev tun0 | |
port 1194 | |
proto udp | |
server 192.168.8.0 255.255.255.0 | |
topology subnet | |
client-to-client | |
keepalive 10 120 | |
persist-tun | |
persist-key | |
duplicate-cn | |
push "dhcp-option DNS 8.8.8.8" | |
push "redirect-gateway def1" | |
push "persist-tun" | |
push "persist-key" | |
<dh> | |
${DH_KEY} | |
</dh> | |
<tls-crypt> | |
${TC_KEY} | |
</tls-crypt> | |
<ca> | |
${CA_CERT} | |
</ca> | |
<cert> | |
${SER_CERT} | |
</cert> | |
<key> | |
${SER_KEY} | |
</key> | |
EOF | |
} | |
writeclient(){ | |
cat << EOF > "client.ovpn" | |
verb 3 | |
dev tun | |
nobind | |
client | |
remote SERVER.DOMAIN.OR.IP 1194 udp | |
auth-nocache | |
remote-cert-tls server | |
<tls-crypt> | |
${TC_KEY} | |
</tls-crypt> | |
<ca> | |
${CA_CERT} | |
</ca> | |
<cert> | |
${CLI_CERT} | |
</cert> | |
<key> | |
${CLI_KEY} | |
</key> | |
EOF | |
} | |
wget https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.6/EasyRSA-unix-v3.0.6.tgz | |
tar xzvf EasyRSA-unix-v3.0.6.tgz | |
cd EasyRSA-v3.0.6 | |
./easyrsa --batch init-pki | |
write_conf | |
./easyrsa --batch gen-dh | |
./easyrsa --batch build-ca nopass | |
./easyrsa --batch build-server-full vpnserver nopass | |
./easyrsa --batch build-client-full vpnclient | |
openvpn --genkey --secret pki/tc.pem | |
DH_KEY="$(cat "pki/dh.pem")" | |
TC_KEY="$(sed -e "/^#/d;/^\w/N;s/\n//" "pki/tc.pem")" | |
CA_CERT="$(openssl x509 -in "pki/ca.crt")" | |
SER_CERT="$(openssl x509 -in "pki/issued/vpnserver.crt")" | |
SER_KEY="$(cat "pki/private/vpnserver.key")" | |
CLI_CERT="$(openssl x509 -in "pki/issued/vpnclient.crt")" | |
CLI_KEY="$(cat "pki/private/vpnclient.key")" | |
writeserver | |
writeclient | |
echo Done! You can find config files here: | |
echo $(pwd)/server.conf | |
echo $(pwd)/client.ovpn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment