Skip to content

Commit 7d5cccd

Browse files
authored
Merge pull request #231 from wiktor-k/wiktor/better-readme
Move crate documentation to README and add code example
2 parents c2e4f89 + 5194772 commit 7d5cccd

File tree

2 files changed

+77
-18
lines changed

2 files changed

+77
-18
lines changed

cryptoki/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,80 @@
77

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

10+
The items in this crate only expose idiomatic and safe Rust types and
11+
functions to interface with the PKCS11 API. All the PKCS11 items might
12+
not be implemented but everything that is implemented is safe.
13+
14+
## Example
15+
16+
The following example initializes an empty token and generates a new RSA key.
17+
18+
```rust
19+
# fn main() -> testresult::TestResult {
20+
use cryptoki::object::Attribute;
21+
use cryptoki::context::{CInitializeArgs, Pkcs11};
22+
use cryptoki::session::UserType;
23+
use cryptoki::types::AuthPin;
24+
use cryptoki::mechanism::Mechanism;
25+
26+
// initialize a new Pkcs11 object using the module from the env variable
27+
let pkcs11 = Pkcs11::new(std::env::var("PKCS11_SOFTHSM2_MODULE")?)?;
28+
29+
pkcs11.initialize(CInitializeArgs::OsThreads)?;
30+
31+
let slot = pkcs11.get_slots_with_token()?[0];
32+
33+
// initialize a test token
34+
let so_pin = AuthPin::new("abcdef".into());
35+
pkcs11.init_token(slot, &so_pin, "Test Token")?;
36+
37+
let user_pin = AuthPin::new("fedcba".into());
38+
39+
// initialize user PIN
40+
{
41+
let session = pkcs11.open_rw_session(slot)?;
42+
session.login(UserType::So, Some(&so_pin))?;
43+
session.init_pin(&user_pin)?;
44+
}
45+
46+
// login as a user, the token has to be already initialized
47+
let session = pkcs11.open_rw_session(slot)?;
48+
session.login(UserType::User, Some(&user_pin))?;
49+
50+
// template of the public key
51+
let pub_key_template = vec![
52+
Attribute::Token(true),
53+
Attribute::Private(false),
54+
Attribute::PublicExponent(vec![0x01, 0x00, 0x01]),
55+
Attribute::ModulusBits(1024.into()),
56+
];
57+
58+
let priv_key_template = vec![Attribute::Token(true)];
59+
60+
// generate an RSA key according to passed templates
61+
let (public, private) = session.generate_key_pair(&Mechanism::RsaPkcsKeyPairGen, &pub_key_template, &priv_key_template)?;
62+
# Ok(()) }
63+
```
64+
65+
## Conformance Notes
66+
67+
Throughout this crate, many functions and other items include additional
68+
"**Conformance**" notes. These notes may provide guarantees about behavior or
69+
additional, contextual information. In all cases, such items pertain
70+
to information from the PKCS#11 standard and are contingent on the provider
71+
being accessed through this crate conforming to that standard. That is, this
72+
crate is permitted to *assume* these guarantees, and is does not necessarily
73+
check for or enforce them itself.
74+
75+
## License
76+
77+
This project is licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0).
78+
79+
### Contribution
80+
81+
Unless you explicitly state otherwise, any contribution intentionally
82+
submitted for inclusion in this crate by you, as defined in the
83+
Apache-2.0 license, shall be licensed as above, without any
84+
additional terms or conditions.
85+
1086
*Copyright 2021 Contributors to the Parsec project.*

cryptoki/src/lib.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
// Copyright 2021 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
3-
//! Rust PKCS11 new abstraction
4-
//!
5-
//! The items in the new module only expose idiomatic and safe Rust types and functions to
6-
//! interface with the PKCS11 API. All the PKCS11 items might not be implemented but everything
7-
//! that is implemented is safe.
8-
//!
9-
//! The modules under `new` follow the structure of the PKCS11 document version 2.40 available [here](http://docs.oasis-open.org/pkcs11/pkcs11-base/v2.40/pkcs11-base-v2.40.html).
10-
//!
11-
//! # Conformance Notes
12-
//!
13-
//! Throughout this crate, many functions and other items include additional
14-
//! "**Conformance**" notes. These notes may provide guarantees about behavior or
15-
//! additional, contextual information. In all cases, such items pertain
16-
//! to information from the PKCS#11 standard and are contingent on the provider
17-
//! being accessed through this crate conforming to that standard. That is, this
18-
//! crate is permitted to *assume* these guarantees, and is does not necessarily
19-
//! check for or enforce them itself.
20-
3+
#![doc = include_str!("../README.md")]
214
// This list comes from
225
// https://github.com/rust-unofficial/patterns/blob/master/anti_patterns/deny-warnings.md
236
#![allow(renamed_and_removed_lints, unknown_lints)]

0 commit comments

Comments
 (0)