-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Arthur Gautier <[email protected]>
- Loading branch information
Showing
4 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
[workspace] | ||
members = ["cryptoki", "cryptoki-sys", "cryptoki-rustcrypto"] | ||
|
||
[patch.crates-io] | ||
# https://github.com/RustCrypto/formats/pull/1269 | ||
spki = { git = "https://github.com/baloo/formats.git", branch = "baloo/spki/from_key" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2021 Contributors to the Parsec project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
mod common; | ||
|
||
use crate::common::USER_PIN; | ||
use common::init_pins; | ||
use cryptoki::{mechanism::Mechanism, object::Attribute, session::UserType, types::AuthPin}; | ||
use cryptoki_rustcrypto::rsa::pss; | ||
use der::{pem::LineEnding, EncodePem}; | ||
use serial_test::serial; | ||
use signature::Keypair; | ||
use spki::SubjectPublicKeyInfoOwned; | ||
use std::{str::FromStr, time::Duration}; | ||
use testresult::TestResult; | ||
use x509_cert::{ | ||
builder::{Builder, CertificateBuilder, Profile}, | ||
name::Name, | ||
serial_number::SerialNumber, | ||
time::Validity, | ||
}; | ||
|
||
#[test] | ||
#[serial] | ||
fn pss_create_ca() -> TestResult { | ||
let (pkcs11, slot) = init_pins(); | ||
|
||
// open a session | ||
let session = pkcs11.open_rw_session(slot)?; | ||
|
||
// log in the session | ||
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?; | ||
|
||
// get mechanism | ||
let mechanism = Mechanism::RsaPkcsKeyPairGen; | ||
|
||
let public_exponent: Vec<u8> = vec![0x01, 0x00, 0x01]; | ||
let modulus_bits = 1024; | ||
|
||
let label = b"demo-signer"; | ||
|
||
// pub key template | ||
let pub_key_template = vec![ | ||
Attribute::Token(true), | ||
Attribute::Private(false), | ||
Attribute::Label(label.to_vec()), | ||
Attribute::PublicExponent(public_exponent), | ||
Attribute::ModulusBits(modulus_bits.into()), | ||
]; | ||
|
||
// priv key template | ||
let priv_key_template = vec![Attribute::Token(true), Attribute::Label(label.to_vec())]; | ||
|
||
// generate a key pair | ||
let (public, private) = | ||
session.generate_key_pair(&mechanism, &pub_key_template, &priv_key_template)?; | ||
|
||
let signer = pss::Signer::<sha2::Sha256>::new(session, label).expect("Lookup keys from HSM"); | ||
|
||
let serial_number = SerialNumber::from(42u32); | ||
let validity = Validity::from_now(Duration::new(5, 0)).unwrap(); | ||
let profile = Profile::Root; | ||
let subject = | ||
Name::from_str("CN=World domination corporation,O=World domination Inc,C=US").unwrap(); | ||
let pub_key = SubjectPublicKeyInfoOwned::from_key(signer.verifying_key()).unwrap(); | ||
|
||
let builder = | ||
CertificateBuilder::new(profile, serial_number, validity, subject, pub_key, &signer) | ||
.expect("Create certificate"); | ||
|
||
let certificate = builder.build().unwrap(); | ||
|
||
let pem = certificate.to_pem(LineEnding::LF).expect("generate pem"); | ||
println!("{}", pem); | ||
|
||
let session = signer.into_session(); | ||
|
||
// delete keys | ||
session.destroy_object(public)?; | ||
session.destroy_object(private)?; | ||
|
||
Ok(()) | ||
} |