Skip to content

Commit f4199d3

Browse files
committed
Move crate documentation to README and add code example
Signed-off-by: Wiktor Kwapisiewicz <[email protected]>
1 parent b63539d commit f4199d3

File tree

2 files changed

+74
-18
lines changed

2 files changed

+74
-18
lines changed

cryptoki/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,77 @@
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+
let pkcs11 = Pkcs11::new(
27+
std::env::var("PKCS11_SOFTHSM2_MODULE")
28+
.unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
29+
)?;
30+
31+
pkcs11.initialize(CInitializeArgs::OsThreads)?;
32+
33+
let slot = pkcs11.get_slots_with_token()?[0];
34+
35+
let so_pin = AuthPin::new("abcdef".into());
36+
pkcs11.init_token(slot, &so_pin, "Test Token")?;
37+
38+
let user_pin = AuthPin::new("fedcba".into());
39+
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+
let session = pkcs11.open_rw_session(slot)?;
47+
session.login(UserType::User, Some(&user_pin))?;
48+
49+
let pub_key_template = vec![
50+
Attribute::Token(true),
51+
Attribute::Private(false),
52+
Attribute::PublicExponent(vec![0x01, 0x00, 0x01]),
53+
Attribute::ModulusBits(1024.into()),
54+
];
55+
56+
let priv_key_template = vec![Attribute::Token(true)];
57+
58+
let (public, private) = session.generate_key_pair(&Mechanism::RsaPkcsKeyPairGen, &pub_key_template, &priv_key_template)?;
59+
# Ok(()) }
60+
```
61+
62+
## Conformance Notes
63+
64+
Throughout this crate, many functions and other items include additional
65+
"**Conformance**" notes. These notes may provide guarantees about behavior or
66+
additional, contextual information. In all cases, such items pertain
67+
to information from the PKCS#11 standard and are contingent on the provider
68+
being accessed through this crate conforming to that standard. That is, this
69+
crate is permitted to *assume* these guarantees, and is does not necessarily
70+
check for or enforce them itself.
71+
72+
## License
73+
74+
This project is licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0).
75+
76+
### Contribution
77+
78+
Unless you explicitly state otherwise, any contribution intentionally
79+
submitted for inclusion in this crate by you, as defined in the
80+
Apache-2.0 license, shall be licensed as above, without any
81+
additional terms or conditions.
82+
1083
*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)