Skip to content

Commit c01405d

Browse files
committed
chore: add dev information to the README
Adds the same example present in the README in a new examples folder. Adds instruction on how to get started to locally develop on the crate. Signed-off-by: Hugues de Valon <[email protected]>
1 parent c45a81b commit c01405d

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
The `cryptoki` crate provides an idiomatic interface to the PKCS #11 API.
44
The `cryptoki-sys` crate provides the direct FFI bindings.
55

6+
Check the `cryptoki` [README file](cryptoki/README.md) to get started!
7+
68
# Community
79

810
Come and ask questions or talk with the Parsec Community in our Slack channel or biweekly meetings.

cryptoki/README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,26 @@ The items in this crate only expose idiomatic and safe Rust types and
1111
functions to interface with the PKCS11 API. All the PKCS11 items might
1212
not be implemented but everything that is implemented is safe.
1313

14+
## Prerequisites
15+
16+
In order to use this crate you will need to have access to a PKCS11 dynamic library to load, to use your HSM.
17+
To develop locally on this crate and in the CI we use [SoftHSM version 2](https://github.com/softhsm/SoftHSMv2). You can also use that if you want to run the example below.
18+
19+
You can follow the installation steps directly in the repository's README but here are instructions proven to work on Ubuntu 24.01:
20+
21+
```bash
22+
sudo apt install libsofthsm2
23+
mkdir /tmp/tokens
24+
echo "directories.tokendir = /tmp/tokens" > /tmp/softhsm2.conf
25+
export PKCS11_SOFTHSM2_MODULE="/usr/lib/softhsm/libsofthsm2.so"
26+
export SOFTHSM2_CONF="/tmp/softhsm2.conf"
27+
cargo run --example generate_key_pair
28+
```
29+
1430
## Example
1531

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

1835
```rust
1936
# fn main() -> testresult::TestResult {
@@ -22,9 +39,13 @@ use cryptoki::context::{CInitializeArgs, Pkcs11};
2239
use cryptoki::session::UserType;
2340
use cryptoki::types::AuthPin;
2441
use cryptoki::mechanism::Mechanism;
42+
use std::env;
2543

2644
// initialize a new Pkcs11 object using the module from the env variable
27-
let pkcs11 = Pkcs11::new(std::env::var("PKCS11_SOFTHSM2_MODULE")?)?;
45+
let pkcs11 = Pkcs11::new(
46+
env::var("PKCS11_SOFTHSM2_MODULE")
47+
.unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
48+
)?;
2849

2950
pkcs11.initialize(CInitializeArgs::OsThreads)?;
3051

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2024 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use cryptoki::context::{CInitializeArgs, Pkcs11};
4+
use cryptoki::mechanism::Mechanism;
5+
use cryptoki::object::Attribute;
6+
use cryptoki::session::UserType;
7+
use cryptoki::types::AuthPin;
8+
use std::env;
9+
10+
// The default user pin
11+
pub static USER_PIN: &str = "fedcba";
12+
// The default SO pin
13+
pub static SO_PIN: &str = "abcdef";
14+
15+
fn main() -> testresult::TestResult {
16+
// initialize a new Pkcs11 object using the module from the env variable
17+
let pkcs11 = Pkcs11::new(
18+
env::var("PKCS11_SOFTHSM2_MODULE")
19+
.unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
20+
)?;
21+
22+
pkcs11.initialize(CInitializeArgs::OsThreads)?;
23+
24+
let slot = pkcs11.get_slots_with_token()?[0];
25+
26+
// initialize a test token
27+
let so_pin = AuthPin::new("abcdef".into());
28+
pkcs11.init_token(slot, &so_pin, "Test Token")?;
29+
30+
let user_pin = AuthPin::new("fedcba".into());
31+
32+
// initialize user PIN
33+
{
34+
let session = pkcs11.open_rw_session(slot)?;
35+
session.login(UserType::So, Some(&so_pin))?;
36+
session.init_pin(&user_pin)?;
37+
}
38+
39+
// login as a user, the token has to be already initialized
40+
let session = pkcs11.open_rw_session(slot)?;
41+
session.login(UserType::User, Some(&user_pin))?;
42+
43+
// template of the public key
44+
let pub_key_template = vec![
45+
Attribute::Token(true),
46+
Attribute::Private(false),
47+
Attribute::PublicExponent(vec![0x01, 0x00, 0x01]),
48+
Attribute::ModulusBits(1024.into()),
49+
];
50+
51+
let priv_key_template = vec![Attribute::Token(true)];
52+
53+
// generate an RSA key according to passed templates
54+
let (_public, _private) = session.generate_key_pair(
55+
&Mechanism::RsaPkcsKeyPairGen,
56+
&pub_key_template,
57+
&priv_key_template,
58+
)?;
59+
Ok(())
60+
}

0 commit comments

Comments
 (0)