TPM: Difference between revisions
Added instructions for loading TPM2 keys into ssh-agent |
mNo edit summary |
||
| Line 30: | Line 30: | ||
<syntaxhighlight>PKCS11Provider /run/current-system/sw/lib/libtpm2_pkcs11.so</syntaxhighlight>To load your ssh key into the running <code>ssh-agent</code>, use <code>ssh-add -s</code>:<syntaxhighlight lang="bash"> | <syntaxhighlight>PKCS11Provider /run/current-system/sw/lib/libtpm2_pkcs11.so</syntaxhighlight>To load your ssh key into the running <code>ssh-agent</code>, use <code>ssh-add -s</code>:<syntaxhighlight lang="bash"> | ||
ssh-add -s /run/current-system/sw/lib/libtpm2_pkcs11.so | ssh-add -s /run/current-system/sw/lib/libtpm2_pkcs11.so | ||
</syntaxhighlight>Note that | </syntaxhighlight>Note that by default, <code>ssh-agent</code> refuses to load PKCS#11 modules outside a whitelist of trusted paths, and <code>/nix/store</code> paths are not included in this default list. You may need to start the agent with the <code>-P</code> flag to allow the library paths:<syntaxhighlight lang="bash"> | ||
ssh-agent -P "/run/current-system/sw/lib/*,/nix/store/*/lib/*" | ssh-agent -P "/run/current-system/sw/lib/*,/nix/store/*/lib/*" | ||
</syntaxhighlight>For a persistent setup, you can configure the system-wide agent in your <code>configuration.nix</code>: <syntaxhighlight lang="nixos"> | </syntaxhighlight>For a persistent setup, you can configure the system-wide agent in your <code>configuration.nix</code>: <syntaxhighlight lang="nixos">programs.ssh = { | ||
programs.ssh = { | |||
startAgent = true; | startAgent = true; | ||
agentPKCS11Whitelist = "${config.security.tpm2.pkcs11.package}/lib/*,/run/current-system/sw/lib/*"; | agentPKCS11Whitelist = "${config.security.tpm2.pkcs11.package}/lib/*,/run/current-system/sw/lib/*"; | ||
}; | };</syntaxhighlight> | ||
</syntaxhighlight> | |||
== Frequently Asked Questions == | == Frequently Asked Questions == | ||
Latest revision as of 00:28, 6 January 2026
TPM (Trusted Platform Module) is a secure microprocessor commonly embedded in modern computers. It can be used for boot chain audit, key storage and random number generation.
NixOS configuration
A minimal NixOS configuration to be able to use the TPM from userspace can be:
security.tpm2.enable = true;
security.tpm2.pkcs11.enable = true; # expose /run/current-system/sw/lib/libtpm2_pkcs11.so
security.tpm2.tctiEnvironment.enable = true; # TPM2TOOLS_TCTI and TPM2_PKCS11_TCTI env variables
users.users.YOUR_USER.extraGroups = [ "tss" ]; # tss group has access to TPM devices
After rebooting with this configuration, TPM2TOOLS_TCTI and TPM2_PKCS11_TCTI should point to device:/dev/tpmrm0 and your user should be able to read and write to /dev/tpmrm0.
Using a TPM2 with OpenSSH
For example, the following commands create a new token associated with PIN-code YOUR_PIN (Personal Identification Number) and a recovery SOPIN-code YOUR_SOPIN (Security Officer Personal Identification Number) and then a new secp256r1 key:
tpm2_ptool init
tpm2_ptool addtoken --pid=1 --label=ssh --userpin=YOUR_PIN --sopin=YOUR_SOPIN
tpm2_ptool addkey --label=ssh --userpin=YOUR_PIN --algorithm=ecc256
Now you may show your public key:
ssh-keygen -D /run/current-system/sw/lib/libtpm2_pkcs11.so
To tell OpenSSH to use the TPM2 during login, you may add the following line to your ~/.ssh/config:
PKCS11Provider /run/current-system/sw/lib/libtpm2_pkcs11.soTo load your ssh key into the running ssh-agent, use ssh-add -s:
ssh-add -s /run/current-system/sw/lib/libtpm2_pkcs11.so
Note that by default, ssh-agent refuses to load PKCS#11 modules outside a whitelist of trusted paths, and /nix/store paths are not included in this default list. You may need to start the agent with the -P flag to allow the library paths:
ssh-agent -P "/run/current-system/sw/lib/*,/nix/store/*/lib/*"
For a persistent setup, you can configure the system-wide agent in your configuration.nix:
programs.ssh = {
startAgent = true;
agentPKCS11Whitelist = "${config.security.tpm2.pkcs11.package}/lib/*,/run/current-system/sw/lib/*";
};
Frequently Asked Questions
How does the PIN-code bruteforce protection work?
You may run the following command to query the variable properties of your TPM:
nix-shell -p tpm2-tools --run "tpm2_getcap properties-variable"
TPM2_PT_LOCKOUT_COUNTERis the current number of failed attempts,TPM2_PT_MAX_AUTH_FAILis the maximum number of failed attempts before lockdown,TPM2_PT_LOCKOUT_INTERVALandTPM2_PT_LOCKOUT_RECOVERYare durations in seconds for recovery.
How to get TPM model information?
The following command will dump found strings from the raw TPM2 certificates:
nix-shell -p tpm2-tools --run "tpm2_getekcertificate" | strings
Usually, you may find a vendor such as STMicroelectronics or Infineon Technologies and a model such as ST33HTPxAHB61.
To get firmware version information, you might want to look at:
nix-shell -p tpm2-tools --run "tpm2_getcap properties-fixed"