This script is designed to automate the setup process of a YubiKey device for use within a mutual TLS (mTLS) environment managed by NGINX. mTLS is a secure communication protocol where both the client and server authenticate each other before establishing a connection. In this setup, the YubiKey device serves as a hardware security module (HSM) to securely store and handle cryptographic keys and certificates.
The script performs the following operations:
- Initialize necessary variables and prompt the user for custom or default Personal Identification Number (PIN), Personal Unblocking Key (PUK), and Management Key values.
- Change the PIN, PUK, and Management Key on the YubiKey to user-specified or default values.
- Adjust the retry settings for the PIN and PUK.
- Generate an elliptic curve key pair on the YubiKey.
- Create a Certificate Signing Request (CSR) for the generated key pair.
- Sign the CSR using a specified Certificate Authority (CA).
- Import the signed certificate back into the YubiKey.
- Optionally, clean up temporary files generated during the process.
The resulting setup facilitates the YubiKey's use as a client certificate in an mTLS environment, enhancing security by ensuring only authorized clients can establish connections to the server.
- YubiKey Manager (
ykman
) installed. - OpenSSL installed.
- A Certificate Authority (CA) certificate and key.
- The provided
generate_ca.sh
script can be used to generate a CA certificate and key.
- The provided
- NGINX configured to support mTLS.
- Ensure you CA file locations and subject name in the script.
- Save the script to a file, e.g.,
setup_yubikey.sh
. - Make the script executable:
chmod +x setup_yubikey.sh
. - Run the script:
./setup_yubikey.sh
. - Follow the on-screen prompts to provide or accept default values for the PIN, PUK, and Management Key.
- Once the script completes, your YubiKey is ready for use in the NGINX mTLS environment.
For mTLS in NGINX, client certificates are verified against a trusted certificate authority. The configured YubiKey now holds a client certificate signed by a trusted CA. To set up mTLS in NGINX, follow these steps:
- Update your NGINX configuration to require client certificates for a particular location or server:
server {
...
ssl_client_certificate /path/to/ca.crt;
ssl_verify_client on;
...
}
- Reload or restart NGINX to apply the changes.
- Now, when accessing the secured endpoint, the client will need to present the certificate stored on the YubiKey.
This README provides a general understanding of the script's operation and its usage within an NGINX mTLS setup. Ensure to adjust file paths and other configurations to match your environment.