generate-key Subcommand
use cryptoki::context::Pkcs11;
use cryptoki::mechanism::Mechanism;
use cryptoki::object::{Attribute, KeyType, ObjectClass};
use cryptoki::session::UserType;
use cryptoki::types::AuthPin;
pub fn generate_ecdsa_key(
pkcs11: &Pkcs11,
slot: cryptoki::slot::Slot,
pin: &str,
label: &str,
) -> anyhow::Result<()> {
let session = pkcs11.open_rw_session(slot)?;
session.login(UserType::User, Some(&AuthPin::new(pin.into())))?;
let ec_params = hex::decode("06082a8648ce3d030107")?;
let pub_template = vec![
Attribute::Token(true),
Attribute::Private(false),
Attribute::Verify(true),
Attribute::EcParams(ec_params.clone()),
Attribute::Label(label.into()),
];
let priv_template = vec![
Attribute::Token(true),
Attribute::Private(true),
Attribute::Sensitive(true),
Attribute::Extractable(false),
Attribute::Sign(true),
Attribute::Label(label.into()),
];
let (pub_handle, _priv_handle) = session.generate_key_pair(
&Mechanism::EccKeyPairGen,
&pub_template,
&priv_template,
)?;
println!("Generated ECDSA P-256 key pair, label: {label}");
println!("Public key handle: {:?}", pub_handle);
Ok(())
}