> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/cgw/1.9/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/cgw/1.9/tutorials/03-setup-certificate-vpn.md).

# Set up a certificate-based VPN

CGW has the option to use certificates for VPN connections.
To use them, the certificates and keys must be managed as Kubernetes Secrets. These Secrets are later mounted into the CGW pod.

To configure CGW to use Secrets for VPN connections, there are two steps to fulfil:

1. Convert the certificates and keys to Secrets.
2. Configure strongSwan to use Secrets.

## Prerequisites

- Certificate of the CA, which signs the trusted keys, is available
- Private/public key-pair is created for this instance of CGW
- Certificate signing request (CSR) was created
- Keys were signed and the certificate is available

Use [strongSwan documentation](https://wiki.strongswan.org/projects/strongswan/wiki/SimpleCA) as a starting point to learn how to create keys and certificates.

## Enable mounting of certificates

Set the following parameters to enable mounting of certificates:

```yaml
ipsec:
  certBasedIPSEC: true
```

## Manage Secrets

There are four different kinds of data that are managed in Secrets and are part of the certificate-based VPN configuration:

- Public/private keys
- Certificates
- CA certificates
- Certificate revocation lists

All these files follow the same schema.

### Define Secrets for certificates and keys

To configure CGW to use the certificates and keys you store on you hard drive, add them to the configuration:

```yaml
ipsec:
  certs:
    privateKeys:     # add private keys here
      keypair1.der: <base64 encoded file>
      keypair2.der: <base64 encoded file>
    certificates:    # add existing certificates here
      cert1.der: <base64 encoded file>
    caCertificates:  # add CA certificates here
      myCA.der: <base64 encoded file>
    certificateRevocationLists: # add CRLs here
      crl1.der: <base64 encoded file>
```

If one of the four categories are not defined, no Secret for the corresponding category is defined and no folder is mounted.

### Convert certificates and keys into Secrets

To convert your files to a base64-encoded string, use the following command in your shell:

```sh
cat keypair1.der | base64
```

### Set up Secrets manually

You can also create the Secrets to store your certificates manually, for example to keep them outside of your values files.
To do so, create a Secret:

```yaml
apiVersion: v1
kind: Secret
metadata:
  name: my-company-ca-certificate
  labels:
    company: "ACME Corp."
  namespace: interconnection
data:
  cert1.der: <base64 encoded file>
```

Then, store it to a file and apply it to your Kubernetes cluster:

```sh
kubectl apply -f ca-secret.yaml
```

After that, configure the certificate name in the CGW configuration:

```yaml
ipsec:
  certs:
    externalCaCertificates: my-company-ca-certificate  # name of the secret
```

The same applies to the following values:

- `externalCertificates`
- `externalCertificateRevocationLists`
- `externalPrivateKeys`

If the files are defined in the values files and the external certificates are set, the external certificates are preferred. It is though recommended to delete or comment the unused part out. See the example:

  ```yaml
  ipsec:
    certs:
      caCertificates:                                # Add CA certificates here
        myCA.der: {base64-encoded file}
      externalCaCertificates: {external-secret-name}   # This certificate is preferred
  ```

## Configure strongSwan

The following sections describe the strongSwan configurations and Secrets.
The [Set up a VPN connection with IPsec](https://docs.cennso.com/components/cgw/1.9/tutorials/02-setup-vpn-ipsec.md#manual-configuration-of-strongswan) tutorial describes how to set up the manual configuration.

### Setting up the CA

According to the [strongSwan documentation](https://wiki.strongswan.org/projects/strongswan/wiki/CaSection), the setup of a CA section in the configuration file is optional. All defined CA certificates are automatically loaded.

If you want to configure the CA section, add URIs of CRLs or OCSP for online verification of certificates.

### Configure connections

To configure a connection to a peer in a site-to-site scenario, set the corresponding IDs and certificates in the corresponding `ipsec.conf` file:

`ipsec.test.conf:`

```
conn sts-base
    ike=aes192-sha256-modp3072,default
    esp=aes192-sha256-ecp256-modp3072#
    leftid=foobar
    leftcert=foobar.pem

# this conn is set up for a remote host with a static IP
conn site-1-static-ip
    also=sts-base
    keyexchange=ikev2
    leftsubnet=10.1.2.0/24,10.1.1.0/24
    rightsubnet=10.1.3.0/24
    right=1.2.3.4
    rightcert=1.2.3.4.pem
    auto=route
```

When no `rightid` is given, it is taken from the certificate itself. The defined ID must match the ID from the certificate.
For more information about configuration of connections, refer to the [strongSwan documentation](https://wiki.strongswan.org/projects/strongswan/wiki/ConnSection).

#### Setting the private key

Define the private key that should be used by CGW in the `ipsec.secrets` file:

`ipsec.test.secrets:`

```
# ipsec.test.secrets
: RSA cgwKey.pem
```

For more information about configuration of Secrets, refer to the [strongSwan documentation](https://wiki.strongswan.org/projects/strongswan/wiki/Ipsecsecrets).
