Skip to content

Commit af58a93

Browse files
committed
tests: Test different behavior in different tokens
Signed-off-by: Jakub Jelen <[email protected]>
1 parent 88676a5 commit af58a93

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

cryptoki/tests/basic.rs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
mod common;
44

5-
use crate::common::{get_pkcs11, SO_PIN, USER_PIN};
5+
use crate::common::{get_pkcs11, is_softhsm, SO_PIN, USER_PIN};
66
use common::init_pins;
77
use cryptoki::context::Function;
88
use cryptoki::error::{Error, RvError};
@@ -411,7 +411,11 @@ fn import_export() -> TestResult {
411411
fn get_token_info() -> TestResult {
412412
let (pkcs11, slot) = init_pins();
413413
let info = pkcs11.get_token_info(slot)?;
414-
assert_eq!("SoftHSM project", info.manufacturer_id());
414+
if is_softhsm() {
415+
assert_eq!("SoftHSM project", info.manufacturer_id());
416+
} else {
417+
assert_eq!("Kryoptic Project", info.manufacturer_id());
418+
}
415419

416420
Ok(())
417421
}
@@ -698,9 +702,15 @@ fn get_info_test() -> TestResult {
698702
let (pkcs11, _) = init_pins();
699703
let info = pkcs11.get_library_info()?;
700704

701-
assert_eq!(info.cryptoki_version().major(), 2);
702-
assert_eq!(info.cryptoki_version().minor(), 40);
703-
assert_eq!(info.manufacturer_id(), String::from("SoftHSM"));
705+
if is_softhsm() {
706+
assert_eq!(info.cryptoki_version().major(), 2);
707+
assert_eq!(info.cryptoki_version().minor(), 40);
708+
assert_eq!(info.manufacturer_id(), String::from("SoftHSM"));
709+
} else {
710+
assert_eq!(info.cryptoki_version().major(), 3);
711+
assert_eq!(info.cryptoki_version().minor(), 0);
712+
assert_eq!(info.manufacturer_id(), String::from("Kryoptic"));
713+
}
704714
Ok(())
705715
}
706716

@@ -712,7 +722,12 @@ fn get_slot_info_test() -> TestResult {
712722
assert!(slot_info.token_present());
713723
assert!(!slot_info.hardware_slot());
714724
assert!(!slot_info.removable_device());
715-
assert_eq!(slot_info.manufacturer_id(), String::from("SoftHSM project"));
725+
let manufacturer = if is_softhsm() {
726+
String::from("SoftHSM project")
727+
} else {
728+
String::from("Kryoptic")
729+
};
730+
assert_eq!(slot_info.manufacturer_id(), manufacturer);
716731
Ok(())
717732
}
718733

@@ -1273,9 +1288,13 @@ fn sha256_digest() -> TestResult {
12731288

12741289
#[test]
12751290
#[serial]
1276-
// Currently empty AAD crashes SoftHSM, see: https://github.com/opendnssec/SoftHSMv2/issues/605
1277-
#[ignore]
12781291
fn aes_gcm_no_aad() -> TestResult {
1292+
// Currently empty AAD crashes SoftHSM, see: https://github.com/opendnssec/SoftHSMv2/issues/605
1293+
if is_softhsm() {
1294+
/* return Ignore(); */
1295+
return Ok(());
1296+
}
1297+
12791298
// Encrypt two blocks of zeros with AES-128-GCM
12801299
let key = vec![0; 16];
12811300
let mut iv = [0; 12];
@@ -1370,8 +1389,13 @@ fn rsa_pkcs_oaep_empty() -> TestResult {
13701389

13711390
#[test]
13721391
#[serial]
1373-
#[ignore] // it's not clear why the test with data specified fails
13741392
fn rsa_pkcs_oaep_with_data() -> TestResult {
1393+
/* SoftHSM does not support additional OAEP Source */
1394+
if is_softhsm() {
1395+
/* return Ignore(); */
1396+
return Ok(());
1397+
}
1398+
13751399
let (pkcs11, slot) = init_pins();
13761400
let session = pkcs11.open_rw_session(slot)?;
13771401
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
@@ -1404,11 +1428,16 @@ fn rsa_pkcs_oaep_with_data() -> TestResult {
14041428
#[test]
14051429
#[serial]
14061430
fn get_slot_event() -> TestResult {
1407-
// Not implemented in SoftHSMv2
1408-
// https://github.com/opendnssec/SoftHSMv2/issues/370
14091431
let (pkcs11, _slot) = init_pins();
1410-
let event = pkcs11.get_slot_event()?;
1411-
assert_eq!(None, event);
1432+
if is_softhsm() {
1433+
// Not implemented in SoftHSMv2
1434+
// https://github.com/opendnssec/SoftHSMv2/issues/370
1435+
let event = pkcs11.get_slot_event()?;
1436+
assert_eq!(None, event);
1437+
} else {
1438+
// Not implemented in Kryoptic
1439+
pkcs11.get_slot_event().unwrap_err();
1440+
}
14121441
Ok(())
14131442
}
14141443

cryptoki/tests/common.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ pub static USER_PIN: &str = "fedcba";
1111
// The default SO pin
1212
pub static SO_PIN: &str = "abcdef";
1313

14+
fn get_pkcs11_path() -> String {
15+
env::var("PKCS11_SOFTHSM2_MODULE")
16+
.unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string())
17+
}
18+
19+
pub fn is_softhsm() -> bool {
20+
get_pkcs11_path().contains("softhsm")
21+
}
22+
1423
pub fn get_pkcs11() -> Pkcs11 {
15-
Pkcs11::new(
16-
env::var("PKCS11_SOFTHSM2_MODULE")
17-
.unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
18-
)
19-
.unwrap()
24+
Pkcs11::new(get_pkcs11_path()).unwrap()
2025
}
2126

2227
pub fn init_pins() -> (Pkcs11, Slot) {

0 commit comments

Comments
 (0)