> ## Documentation Index
> Fetch the complete documentation index at: https://docs.futurex.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Renew or replace the certificate

> Renew or replace the certificate on an existing CryptoHub key pair with a certificate signing request (CSR) from that key, without changing Apache Tomcat.

Use this procedure to put a CA-signed certificate on the key pair you generated in [Generate key pair on the CryptoHub by using Java Keytool](https://docs.futurex.com/Integrations/CryptoHub/TLS_offloading/Apache_Tomcat/Generate_key_pair_on_the_CryptoHub_by_using_Java_Keytool.md), or to renew that certificate before it expires. You generate a certificate signing request (CSR) from the existing private key, have your certificate authority (CA) sign it, and import the signed certificate back to the same alias. The private key never leaves the CryptoHub, and the alias does not change, so Apache Tomcat needs no configuration change.

> [!NOTE]
>
> Renewing the certificate on the same key pair is supported. Generate a new key pair instead when your key-rotation policy requires it or when your CA no longer accepts the key size.

## Before you begin

* The key pair exists on the CryptoHub, and `keytool -list` shows it as a `PrivateKeyEntry`.
* `FXPKCS11_CFG` points to your `fxpkcs11.cfg` in the shell where you run **keytool**, as described in [Install and configure Futurex PKCS #11](https://docs.futurex.com/Integrations/CryptoHub/TLS_offloading/Apache_Tomcat/Install_and_configure_Futurex_PKCS_11.md).
* You have the signed certificate chain from your CA, or you can obtain it: the signed certificate plus every intermediate and root CA certificate.

## Back up the current certificate

**Step 1**

List the entries on the token and copy the exact alias of the key pair:

```shell expandable lines wrap title="Shell" theme={null}
keytool -list -keystore NONE -storetype PKCS11 -providerClass sun.security.pkcs11.SunPKCS11 -providerArg /usr/local/etc/pkcs11.cfg -providerName SunPKCS11-Futurex
```

> [!TIP]
>
> The entry shows as `PrivateKeyEntry`, with an alias in the form `<alias>-<id>:<alias>` (for example, `futurex-tomcat-1784141140:futurex-tomcat`).

**Step 2**

Export the current certificate. Keep it, together with the CA chain it was issued with, so you can restore it if you need to:

```shell expandable lines wrap title="Shell" theme={null}
keytool -exportcert -rfc -alias "futurex-tomcat-1784141140:futurex-tomcat" -file current-cert.pem -keystore NONE -storetype PKCS11 -providerClass sun.security.pkcs11.SunPKCS11 -providerArg /usr/local/etc/pkcs11.cfg -providerName SunPKCS11-Futurex
```

> [!TIP]
>
> `current-cert.pem` contains one PEM certificate.

> [!NOTE]
>
> When **keytool** prompts for the key store password, enter the CryptoHub identity password (PKCS #11 PIN) from the `<CRYPTO-OPR-PASS>` tag in `fxpkcs11.cfg`.

## Generate a CSR from the existing key

Run the following command with the exact alias you copied. The CryptoHub signs the CSR with the existing private key; no new key is created:

```shell expandable lines wrap title="Shell" theme={null}
keytool -certreq -alias "futurex-tomcat-1784141140:futurex-tomcat" -sigalg SHA256withRSA -file renew.csr -keystore NONE -storetype PKCS11 -providerClass sun.security.pkcs11.SunPKCS11 -providerArg /usr/local/etc/pkcs11.cfg -providerName SunPKCS11-Futurex
```

> [!TIP]
>
> `openssl req -in renew.csr -noout -verify -subject` reports `Certificate request self-signature verify OK` and shows the subject of the current certificate.

Submit `renew.csr` to your CA. Request the signed certificate together with its full chain, as a PKCS #7 file (`.p7b`) or as one PEM file that holds the signed certificate followed by each intermediate and root CA certificate.

## Import the signed certificate

> [!WARNING]
>
> Import the full chain. If the file contains only the signed certificate, **keytool** cannot build the chain and stops with `Failed to establish chain from reply`. The certificate on the CryptoHub does not change when this happens.

**Step 1**

Import the CA response to the same alias:

```shell expandable lines wrap title="Shell" theme={null}
keytool -importcert -alias "futurex-tomcat-1784141140:futurex-tomcat" -file signed-chain.pem -keystore NONE -storetype PKCS11 -providerClass sun.security.pkcs11.SunPKCS11 -providerArg /usr/local/etc/pkcs11.cfg -providerName SunPKCS11-Futurex
```

> [!TIP]
>
> **keytool** prints `Certificate reply was installed in keystore`. If **keytool** asks whether to install a reply from a CA it does not trust, confirm that the root certificate in the file belongs to your CA, and then enter `yes`.

**Step 2**

Confirm the result:

```shell expandable lines wrap title="Shell" theme={null}
keytool -list -v -alias "futurex-tomcat-1784141140:futurex-tomcat" -keystore NONE -storetype PKCS11 -providerClass sun.security.pkcs11.SunPKCS11 -providerArg /usr/local/etc/pkcs11.cfg -providerName SunPKCS11-Futurex
```

> [!TIP]
>
> The entry keeps the same alias and is still a `PrivateKeyEntry`. The first certificate in the chain shows the new serial number, validity dates, and your CA as the issuer. The token holds one certificate for the alias: the import replaces the previous certificate rather than adding a second one.

## Restart Apache Tomcat

Restart Tomcat with `Tomcat_HOME/bin/shutdown.sh` and then `Tomcat_HOME/bin/startup.sh`. A running Tomcat keeps serving the old certificate until it restarts, even though the CryptoHub already holds the new one. The `keyAlias` and `certificateKeyAlias` values in `server.xml` stay the same.

## Verify it works

Connect to the HTTPS connector, for example with `openssl s_client -connect <tomcat-host>:8443 -showcerts`, and confirm that the server presents the new serial number and expiration date.

## Troubleshooting

| Symptom | Cause and solution |
| - | - |
| `keytool error: java.lang.Exception: Failed to establish chain from reply` | The file holds only the signed certificate. Build a file that contains the signed certificate followed by each intermediate and root CA certificate, or use the CA's PKCS #7 file, and run the import again. |
| `keytool -list -v` shows the new certificate, but Apache Tomcat still presents the old one | The process loaded the key store before the import. Restart it. |
| `Alias <name> does not exist` | The command uses the short `-alias` value from key generation. Copy the full `<alias>-<id>:<alias>` value from `keytool -list`. |

To roll back, import the previous certificate and its full CA chain to the same alias with the same `keytool -importcert` command, and then restart Apache Tomcat.

> [!NOTE]
>
> The CSR command uses `SHA256withRSA` for the RSA key pairs this guide generates. Futurex validated this procedure on CryptoHub 7.0.3.x with Futurex PKCS #11 (FXPKCS11) 6.0 rev dc27 and 6.5, OpenJDK 21, and an RSA 3072 key pair.
