Skip to content

Commit 0273ed5

Browse files
committed
[tests/full] Make keygen-signing test generic over algorithm
At the moment, 'kmip2pkcs11' only supports RSA-SHA256 and ECDSAP256SHA256, so those are the only ones we test.
1 parent dce53f9 commit 0273ed5

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

tests/full.rs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ use std::{
2121
time::Duration,
2222
};
2323

24-
use domain::crypto::{
25-
ring,
26-
sign::{SignRaw, Signature},
24+
use domain::{
25+
base::iana::SecurityAlgorithm,
26+
crypto::{
27+
ring,
28+
sign::{GenerateParams, SignRaw, Signature},
29+
},
2730
};
2831
use kmip::client::pool::SyncConnPool;
2932

@@ -195,29 +198,52 @@ fn main() {
195198
)
196199
.unwrap();
197200

198-
// Generate a new RSA-SHA256 key.
199-
let key = domain_kmip::sign::generate(
200-
"A-pub".into(),
201-
"A-priv".into(),
202-
domain::crypto::sign::GenerateParams::RsaSha256 { bits: 1024 },
203-
0,
204-
conn_pool,
205-
)
206-
.unwrap();
201+
print!("test_keygen_signing(RsaSha256 {{ bits: 1024 }})...");
202+
test_keygen_signing(&conn_pool, GenerateParams::RsaSha256 { bits: 1024 });
203+
println!("ok");
204+
205+
print!("test_keygen_signing(EcdsaP256Sha256)...");
206+
test_keygen_signing(&conn_pool, GenerateParams::EcdsaP256Sha256);
207+
println!("ok");
208+
}
209+
210+
/// Test that key generation and signing works.
211+
///
212+
/// A new key will be generated (using the given parameters) and used for
213+
/// signing. The public key will be retrieved and used to locally verify the
214+
/// signature.
215+
fn test_keygen_signing(pool: &SyncConnPool, key_params: GenerateParams) {
216+
let algorithm = key_params.algorithm();
217+
218+
// Generate a new key.
219+
let key =
220+
domain_kmip::sign::generate("A-pub".into(), "A-priv".into(), key_params, 0, pool.clone())
221+
.unwrap();
207222

208223
// Retrive the public key, for local use.
209224
let dnskey = key.dnskey();
210225
let pubkey = ring::PublicKey::from_dnskey(&dnskey).unwrap();
211226

212227
// Sign data with this key.
213228
let data = b"Hello World!";
214-
let sig = match key.sign_raw(data).unwrap() {
215-
Signature::RsaSha256(sig) => sig,
216-
sig => {
217-
panic!("Unexpected signature algorithm {:?}", sig.algorithm());
229+
let sig = key.sign_raw(data).unwrap();
230+
let sig = match (algorithm, &sig) {
231+
(SecurityAlgorithm::RSASHA1, Signature::RsaSha1(sig)) => &**sig,
232+
(SecurityAlgorithm::RSASHA1_NSEC3_SHA1, Signature::RsaSha1Nsec3Sha1(sig)) => sig,
233+
(SecurityAlgorithm::RSASHA256, Signature::RsaSha256(sig)) => sig,
234+
(SecurityAlgorithm::RSASHA512, Signature::RsaSha512(sig)) => sig,
235+
(SecurityAlgorithm::ECDSAP256SHA256, Signature::EcdsaP256Sha256(sig)) => &**sig,
236+
(SecurityAlgorithm::ECDSAP384SHA384, Signature::EcdsaP384Sha384(sig)) => &**sig,
237+
(SecurityAlgorithm::ED25519, Signature::Ed25519(sig)) => &**sig,
238+
(SecurityAlgorithm::ED448, Signature::Ed448(sig)) => &**sig,
239+
(alg, sig) => {
240+
panic!(
241+
"Unexpected signature algorithm {:?}, expecting {alg:?}",
242+
sig.algorithm()
243+
);
218244
}
219245
};
220246

221247
// Verify the signature.
222-
pubkey.verify(data, &sig).unwrap();
248+
pubkey.verify(data, sig).unwrap();
223249
}

0 commit comments

Comments
 (0)