cryptoki

Crates.iocryptoki
lib.rscryptoki
version
sourcesrc
created_at2021-03-18 15:45:17.211515+00
updated_at2025-02-17 09:47:59.604948+00
descriptionRust-native wrapper around the PKCS #11 API
homepage
repositoryhttps://github.com/parallaxsecond/rust-cryptoki
max_upload_size
id370556
Cargo.toml error:TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Wiktor Kwapisiewicz (wiktor-k)

documentation

https://docs.rs/crate/cryptoki

README

Cryptoki Rust Wrapper

Crates.io Code documentation

This is the high-level, Rust idiomatic wrapper crate for PKCS #11.

The items in this crate only expose idiomatic and safe Rust types and functions to interface with the PKCS11 API. All the PKCS11 items might not be implemented but everything that is implemented is safe.

Prerequisites

In order to use this crate you will need to have access to a PKCS11 dynamic library to load, to use your HSM. To develop locally on this crate and in the CI we use SoftHSM version 2. You can also use that if you want to run the example below.

You can follow the installation steps directly in the repository's README but here are instructions proven to work on Ubuntu 24.01:

sudo apt install libsofthsm2
mkdir /tmp/tokens
echo "directories.tokendir = /tmp/tokens" > /tmp/softhsm2.conf
export PKCS11_SOFTHSM2_MODULE="/usr/lib/softhsm/libsofthsm2.so"
export SOFTHSM2_CONF="/tmp/softhsm2.conf"
cargo run --example generate_key_pair

Example

The following example initializes an empty token and generates a new RSA key. You can find it in the examples folder and run it with cargo run --example generate_key_pair.

# fn main() -> testresult::TestResult {
use cryptoki::object::Attribute;
use cryptoki::context::{CInitializeArgs, Pkcs11};
use cryptoki::session::UserType;
use cryptoki::types::AuthPin;
use cryptoki::mechanism::Mechanism;
use std::env;

// initialize a new Pkcs11 object using the module from the env variable
let pkcs11 = Pkcs11::new(
    env::var("PKCS11_SOFTHSM2_MODULE")
        .unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
)?;

pkcs11.initialize(CInitializeArgs::OsThreads)?;

let slot = pkcs11.get_slots_with_token()?[0];

// initialize a test token
let so_pin = AuthPin::new("abcdef".into());
pkcs11.init_token(slot, &so_pin, "Test Token")?;

let user_pin = AuthPin::new("fedcba".into());

// initialize user PIN
{
  let session = pkcs11.open_rw_session(slot)?;
  session.login(UserType::So, Some(&so_pin))?;
  session.init_pin(&user_pin)?;
}

// login as a user, the token has to be already initialized
let session = pkcs11.open_rw_session(slot)?;
session.login(UserType::User, Some(&user_pin))?;

// template of the public key
let pub_key_template = vec![
    Attribute::Token(true),
    Attribute::Private(false),
    Attribute::PublicExponent(vec![0x01, 0x00, 0x01]),
    Attribute::ModulusBits(1024.into()),
];

let priv_key_template = vec![Attribute::Token(true)];

// generate an RSA key according to passed templates
let (public, private) = session.generate_key_pair(&Mechanism::RsaPkcsKeyPairGen, &pub_key_template, &priv_key_template)?;
# Ok(()) }

See also

Conformance Notes

Throughout this crate, many functions and other items include additional "Conformance" notes. These notes may provide guarantees about behavior or additional, contextual information. In all cases, such items pertain to information from the PKCS#11 standard and are contingent on the provider being accessed through this crate conforming to that standard. That is, this crate is permitted to assume these guarantees, and is does not necessarily check for or enforce them itself.

License

This project is licensed under the Apache License, Version 2.0.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.

Copyright 2021 Contributors to the Parsec project.

Commit count: 392

cargo fmt