Generate keys, sign data and manage objects on a YubiHSM 2 net
Drive a YubiHSM 2 from a cargo test: generate a key, sign data with it, and manage the
objects on the device. It requires box software 0.59.0 or later.
Every method except info() logs in with an authentication key. Give the key and its
password in an HsmAuth:
use lager::HsmAuth;let auth = HsmAuth::new(std::env::var("HSM_PASSWORD").expect("HSM_PASSWORD"));let other = HsmAuth::with_key(2, "another-password");
HsmAuth::new uses authentication key 1, the factory key. The Debug output of an
HsmAuth leaves out the password, so it cannot reach a test log through {:?}.
If the network between your computer and the box is not trusted, reach the box only
through Tailscale or an SSH tunnel. The crate sends the password to the box API over
plain HTTP, the same as all other box API traffic. The box removes the password from
its logs.
The factory login is authentication key 1 with the password password. Replace that
key before you put keys on the HSM that matter.
Returns:GeneratedKey, with the key’s id and its public_key_pem.The box refuses RSA generation, because it can take minutes. To make an RSA key, use
the Python API.
Sign data with a stored key, and return the signature bytes. The HSM hashes the data
for the ECDSA and RSA mechanisms, so pass the message itself, not a digest of it.
let signature = hsm.sign(&auth, key.id, b"message to sign", "ecdsa-sha256")?;
List the objects that the authentication key can see. Pass a type or a label to
filter the list. The types are asymmetric-key, authentication-key, opaque,
wrap-key, hmac-key, template and otp-aead-key.
reset() deletes every key and object on the device, and you cannot undo it. The
device reboots and keeps only the factory login. Call it only on an HSM that holds no
key you need.