1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
|
// Copyright 2021 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
mod common;
use crate::common::{get_pkcs11, SO_PIN, USER_PIN};
use common::init_pins;
use cryptoki::error::{Error, RvError};
use cryptoki::mechanism::aead::GcmParams;
use cryptoki::mechanism::rsa::{PkcsMgfType, PkcsOaepParams, PkcsOaepSource};
use cryptoki::mechanism::{Mechanism, MechanismType};
use cryptoki::object::{Attribute, AttributeInfo, AttributeType, KeyType, ObjectClass};
use cryptoki::session::{SessionState, UserType};
use cryptoki::types::AuthPin;
use serial_test::serial;
use std::collections::HashMap;
use std::thread;
/// Error with stacktrace
///
/// Any other type of error can be converted to this one but the
/// conversion will always panic.
///
/// This type is useful only in the result of unit tests and cannot be instantiated.
#[derive(Debug)]
#[doc(hidden)]
pub enum TestError {}
impl<T: std::fmt::Display> From<T> for TestError {
#[track_caller] // Will show the location of the caller in test failure messages
fn from(error: T) -> Self {
panic!("error: {} - {}", std::any::type_name::<T>(), error);
}
}
/// Unit test result
///
/// This type allows panicking when encountering any type of
/// failure. Thus it allows using `?` operator in unit tests but still
/// get the complete stacktrace and exact place of failure during
/// tests.
///
/// # Examples
///
/// ```
/// use testresult::TestResult;
///
/// #[test]
/// fn it_works() -> TestResult {
/// // ...
/// std::fs::File::open("this-file-does-not-exist")?;
/// // ...
/// Ok(())
/// }
/// ```
pub type TestResult = std::result::Result<(), TestError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore] // ignored test must still compile
// this checks whether conversion from all errors is accomplished
fn compilation_works() -> TestResult {
std::fs::File::open("this-file-does-not-exist")?;
Ok(())
}
#[test]
fn check_if_panics() -> TestResult {
let result = std::panic::catch_unwind(|| {
fn test_fn() -> TestResult {
std::fs::File::open("this-file-does-not-exist")?;
Ok(())
}
let _ = test_fn();
});
assert!(result.is_err());
Ok(())
}
}
#[test]
#[serial]
fn sign_verify() -> 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;
// pub key template
let pub_key_template = vec![
Attribute::Token(true),
Attribute::Private(false),
Attribute::PublicExponent(public_exponent),
Attribute::ModulusBits(modulus_bits.into()),
];
// priv key template
let priv_key_template = vec![Attribute::Token(true)];
// generate a key pair
let (public, private) =
session.generate_key_pair(&mechanism, &pub_key_template, &priv_key_template)?;
// data to sign
let data = [0xFF, 0x55, 0xDD];
// sign something with it
let signature = session.sign(&Mechanism::RsaPkcs, private, &data)?;
// verify the signature
session.verify(&Mechanism::RsaPkcs, public, &data, &signature)?;
// delete keys
session.destroy_object(public)?;
session.destroy_object(private)?;
Ok(())
}
#[test]
#[serial]
fn sign_verify_ed25519() -> TestResult {
let (pkcs11, slot) = init_pins();
let session = pkcs11.open_rw_session(slot)?;
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
let mechanism = Mechanism::EccEdwardsKeyPairGen;
let pub_key_template = vec![
Attribute::Token(true),
Attribute::Private(false),
Attribute::Verify(true),
// Ed25519 OID
// See: https://github.com/opendnssec/SoftHSMv2/blob/ac70dc398b236e4522101930e790008936489e2d/src/lib/test/SignVerifyTests.cpp#L173
Attribute::EcParams(vec![
0x13, 0x0c, 0x65, 0x64, 0x77, 0x61, 0x72, 0x64, 0x73, 0x32, 0x35, 0x35, 0x31, 0x39,
]),
];
let priv_key_template = vec![Attribute::Token(true)];
let (public, private) =
session.generate_key_pair(&mechanism, &pub_key_template, &priv_key_template)?;
let data = [0xFF, 0x55, 0xDD];
let signature = session.sign(&Mechanism::Eddsa, private, &data)?;
session.verify(&Mechanism::Eddsa, public, &data, &signature)?;
session.destroy_object(public)?;
session.destroy_object(private)?;
Ok(())
}
#[test]
#[serial]
fn encrypt_decrypt() -> 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;
// pub key template
let pub_key_template = vec![
Attribute::Token(true),
Attribute::Private(false),
Attribute::PublicExponent(public_exponent),
Attribute::ModulusBits(modulus_bits.into()),
Attribute::Encrypt(true),
];
// priv key template
let priv_key_template = vec![Attribute::Token(true), Attribute::Decrypt(true)];
// generate a key pair
let (public, private) =
session.generate_key_pair(&mechanism, &pub_key_template, &priv_key_template)?;
// data to encrypt
let data = vec![0xFF, 0x55, 0xDD];
// encrypt something with it
let encrypted_data = session.encrypt(&Mechanism::RsaPkcs, public, &data)?;
// decrypt
let decrypted_data = session.decrypt(&Mechanism::RsaPkcs, private, &encrypted_data)?;
// The decrypted buffer is bigger than the original one.
assert_eq!(data, decrypted_data);
// delete keys
session.destroy_object(public)?;
session.destroy_object(private)?;
Ok(())
}
#[test]
#[serial]
fn derive_key() -> 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::EccKeyPairGen;
let secp256r1_oid: Vec<u8> = vec![0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07];
// pub key template
let pub_key_template = vec![
Attribute::Token(true),
Attribute::Private(false),
Attribute::Derive(true),
Attribute::KeyType(KeyType::EC),
Attribute::Verify(true),
Attribute::EcParams(secp256r1_oid),
];
// priv key template
let priv_key_template = vec![
Attribute::Token(true),
Attribute::Private(true),
Attribute::Sensitive(true),
Attribute::Extractable(false),
Attribute::Derive(true),
Attribute::Sign(true),
];
// generate a key pair
let (public, private) =
session.generate_key_pair(&mechanism, &pub_key_template, &priv_key_template)?;
let ec_point_attribute = session
.get_attributes(public, &[AttributeType::EcPoint])?
.remove(0);
let ec_point = if let Attribute::EcPoint(point) = ec_point_attribute {
point
} else {
panic!("Expected EC point attribute.");
};
use cryptoki::mechanism::elliptic_curve::*;
let params = Ecdh1DeriveParams::new(EcKdf::null(), &ec_point);
let shared_secret = session.derive_key(
&Mechanism::Ecdh1Derive(params),
private,
&[
Attribute::Class(ObjectClass::SECRET_KEY),
Attribute::KeyType(KeyType::GENERIC_SECRET),
Attribute::Sensitive(false),
Attribute::Extractable(true),
Attribute::Token(false),
],
)?;
let value_attribute = session
.get_attributes(shared_secret, &[AttributeType::Value])?
.remove(0);
let value = if let Attribute::Value(value) = value_attribute {
value
} else {
panic!("Expected value attribute.");
};
assert_eq!(value.len(), 32);
// delete keys
session.destroy_object(public)?;
session.destroy_object(private)?;
Ok(())
}
#[test]
#[serial]
fn import_export() -> 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())))?;
let public_exponent: Vec<u8> = vec![0x01, 0x00, 0x01];
let modulus = vec![0xFF; 1024];
let template = vec![
Attribute::Token(true),
Attribute::Private(false),
Attribute::PublicExponent(public_exponent),
Attribute::Modulus(modulus.clone()),
Attribute::Class(ObjectClass::PUBLIC_KEY),
Attribute::KeyType(KeyType::RSA),
Attribute::Verify(true),
];
{
// Intentionally forget the object handle to find it later
let _public_key = session.create_object(&template)?;
}
let is_it_the_public_key = session.find_objects(&template)?.remove(0);
let attribute_info = session
.get_attribute_info(is_it_the_public_key, &[AttributeType::Modulus])?
.remove(0);
if let AttributeInfo::Available(size) = attribute_info {
assert_eq!(size, 1024);
} else {
panic!("The Modulus attribute was expected to be present.")
};
let attr = session
.get_attributes(is_it_the_public_key, &[AttributeType::Modulus])?
.remove(0);
if let Attribute::Modulus(modulus_cmp) = attr {
assert_eq!(modulus[..], modulus_cmp[..]);
} else {
panic!("Expected the Modulus attribute.");
}
// delete key
session.destroy_object(is_it_the_public_key)?;
Ok(())
}
#[test]
#[serial]
fn get_token_info() -> TestResult {
let (pkcs11, slot) = init_pins();
let info = pkcs11.get_token_info(slot)?;
assert_eq!("SoftHSM project", info.manufacturer_id());
Ok(())
}
#[test]
#[serial]
fn wrap_and_unwrap_key() {
let (pkcs11, slot) = init_pins();
// open a session
let session = pkcs11.open_rw_session(slot).unwrap();
// log in the session
session
.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))
.unwrap();
let key_to_be_wrapped_template = vec![
Attribute::Token(true),
// the key needs to be extractable to be suitable for being wrapped
Attribute::Extractable(true),
Attribute::Encrypt(true),
];
// generate a secret key that will be wrapped
let key_to_be_wrapped = session
.generate_key(&Mechanism::Des3KeyGen, &key_to_be_wrapped_template)
.unwrap();
// Des3Ecb input length must be a multiple of 8
// see: PKCS#11 spec Table 10-10, DES-ECB Key And Data Length Constraints
let encrypted_with_original = session
.encrypt(
&Mechanism::Des3Ecb,
key_to_be_wrapped,
&[1, 2, 3, 4, 5, 6, 7, 8],
)
.unwrap();
// pub key template
let pub_key_template = vec![
Attribute::Token(true),
Attribute::Private(true),
Attribute::PublicExponent(vec![0x01, 0x00, 0x01]),
Attribute::ModulusBits(1024.into()),
// key needs to have "wrap" attribute to wrap other keys
Attribute::Wrap(true),
];
// priv key template
let priv_key_template = vec![Attribute::Token(true)];
let (wrapping_key, unwrapping_key) = session
.generate_key_pair(
&Mechanism::RsaPkcsKeyPairGen,
&pub_key_template,
&priv_key_template,
)
.unwrap();
let wrapped_key = session
.wrap_key(&Mechanism::RsaPkcs, wrapping_key, key_to_be_wrapped)
.unwrap();
assert_eq!(wrapped_key.len(), 128);
let unwrapped_key = session
.unwrap_key(
&Mechanism::RsaPkcs,
unwrapping_key,
&wrapped_key,
&[
Attribute::Token(true),
Attribute::Private(true),
Attribute::Encrypt(true),
Attribute::Class(ObjectClass::SECRET_KEY),
Attribute::KeyType(KeyType::DES3),
],
)
.unwrap();
let encrypted_with_unwrapped = session
.encrypt(
&Mechanism::Des3Ecb,
unwrapped_key,
&[1, 2, 3, 4, 5, 6, 7, 8],
)
.unwrap();
assert_eq!(encrypted_with_original, encrypted_with_unwrapped);
}
#[test]
#[serial]
fn login_feast() {
const SESSIONS: usize = 100;
let (pkcs11, slot) = init_pins();
let mut threads = Vec::new();
for _ in 0..SESSIONS {
let pkcs11 = pkcs11.clone();
threads.push(thread::spawn(move || {
let session = pkcs11.open_rw_session(slot).unwrap();
match session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into()))) {
Ok(_) | Err(Error::Pkcs11(RvError::UserAlreadyLoggedIn)) => {}
Err(e) => panic!("Bad error response: {}", e),
}
match session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into()))) {
Ok(_) | Err(Error::Pkcs11(RvError::UserAlreadyLoggedIn)) => {}
Err(e) => panic!("Bad error response: {}", e),
}
match session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into()))) {
Ok(_) | Err(Error::Pkcs11(RvError::UserAlreadyLoggedIn)) => {}
Err(e) => panic!("Bad error response: {}", e),
}
match session.logout() {
Ok(_) | Err(Error::Pkcs11(RvError::UserNotLoggedIn)) => {}
Err(e) => panic!("Bad error response: {}", e),
}
match session.logout() {
Ok(_) | Err(Error::Pkcs11(RvError::UserNotLoggedIn)) => {}
Err(e) => panic!("Bad error response: {}", e),
}
match session.logout() {
Ok(_) | Err(Error::Pkcs11(RvError::UserNotLoggedIn)) => {}
Err(e) => panic!("Bad error response: {}", e),
}
}));
}
for thread in threads {
thread.join().unwrap();
}
}
#[test]
#[serial]
fn get_info_test() -> TestResult {
let (pkcs11, _) = init_pins();
let info = pkcs11.get_library_info()?;
assert_eq!(info.cryptoki_version().major(), 2);
assert_eq!(info.cryptoki_version().minor(), 40);
assert_eq!(info.manufacturer_id(), String::from("SoftHSM"));
Ok(())
}
#[test]
#[serial]
fn get_slot_info_test() -> TestResult {
let (pkcs11, slot) = init_pins();
let slot_info = pkcs11.get_slot_info(slot)?;
assert!(slot_info.token_present());
assert!(!slot_info.hardware_slot());
assert!(!slot_info.removable_device());
assert_eq!(slot_info.manufacturer_id(), String::from("SoftHSM project"));
Ok(())
}
#[test]
#[serial]
fn get_session_info_test() -> TestResult {
let (pkcs11, slot) = init_pins();
{
let session = pkcs11.open_ro_session(slot)?;
let session_info = session.get_session_info()?;
assert!(!session_info.read_write());
assert_eq!(session_info.slot_id(), slot);
assert!(matches!(
session_info.session_state(),
SessionState::RoPublic
));
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
let session_info = session.get_session_info()?;
assert!(!session_info.read_write());
assert_eq!(session_info.slot_id(), slot);
assert!(matches!(session_info.session_state(), SessionState::RoUser));
session.logout()?;
if let Err(cryptoki::error::Error::Pkcs11(rv_error)) =
session.login(UserType::So, Some(&AuthPin::new(SO_PIN.into())))
{
assert_eq!(rv_error, RvError::SessionReadOnlyExists)
} else {
panic!("Should error when attempting to log in as CKU_SO on a read-only session");
}
}
let session = pkcs11.open_rw_session(slot)?;
let session_info = session.get_session_info()?;
assert!(session_info.read_write());
assert_eq!(session_info.slot_id(), slot);
assert!(matches!(
session_info.session_state(),
SessionState::RwPublic
));
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
let session_info = session.get_session_info()?;
assert!(session_info.read_write());
assert_eq!(session_info.slot_id(), slot);
assert!(matches!(session_info.session_state(), SessionState::RwUser,));
session.logout()?;
session.login(UserType::So, Some(&AuthPin::new(SO_PIN.into())))?;
let session_info = session.get_session_info()?;
assert!(session_info.read_write());
assert_eq!(session_info.slot_id(), slot);
assert!(matches!(
session_info.session_state(),
SessionState::RwSecurityOfficer
));
Ok(())
}
#[test]
#[serial]
fn generate_random_test() -> TestResult {
let (pkcs11, slot) = init_pins();
let session = pkcs11.open_ro_session(slot)?;
let poor_seed: [u8; 32] = [0; 32];
session.seed_random(&poor_seed)?;
let mut random_data: [u8; 32] = [0; 32];
session.generate_random_slice(&mut random_data)?;
// This of course assumes the RBG in the the SoftHSM is not terrible
assert!(!random_data.iter().all(|&x| x == 0));
let random_vec = session.generate_random_vec(32)?;
assert_eq!(random_vec.len(), 32);
assert!(!random_vec.iter().all(|&x| x == 0));
Ok(())
}
#[test]
#[serial]
fn set_pin_test() -> TestResult {
let new_user_pin = "123456";
let (pkcs11, slot) = init_pins();
let session = pkcs11.open_rw_session(slot)?;
let user_pin = AuthPin::new(USER_PIN.into());
let new_user_pin = AuthPin::new(new_user_pin.into());
session.login(UserType::User, Some(&user_pin))?;
session.set_pin(&user_pin, &new_user_pin)?;
session.logout()?;
session.login(UserType::User, Some(&new_user_pin))?;
Ok(())
}
#[test]
#[serial]
fn get_attribute_info_test() -> 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 = 2048;
// pub key template
let pub_key_template = vec![
Attribute::Token(false),
Attribute::Private(false),
Attribute::PublicExponent(public_exponent),
Attribute::ModulusBits(modulus_bits.into()),
];
// priv key template
let priv_key_template = vec![
Attribute::Token(false),
Attribute::Sensitive(true),
Attribute::Extractable(false),
];
// generate a key pair
let (public, private) =
session.generate_key_pair(&mechanism, &pub_key_template, &priv_key_template)?;
let pub_attribs = vec![AttributeType::PublicExponent, AttributeType::Modulus];
let mut priv_attribs = pub_attribs.clone();
priv_attribs.push(AttributeType::PrivateExponent);
let attrib_info = session.get_attribute_info(public, &pub_attribs)?;
let hash = pub_attribs
.iter()
.zip(attrib_info.iter())
.collect::<HashMap<_, _>>();
if let AttributeInfo::Available(size) = hash[&AttributeType::Modulus] {
assert_eq!(*size, 2048 / 8);
} else {
panic!("Modulus should not return Unavailable for an RSA public key");
}
match hash[&AttributeType::PublicExponent] {
AttributeInfo::Available(_) => {}
_ => panic!("Public Exponent should not return Unavailable for an RSA public key"),
}
let attrib_info = session.get_attribute_info(private, &priv_attribs)?;
let hash = priv_attribs
.iter()
.zip(attrib_info.iter())
.collect::<HashMap<_, _>>();
if let AttributeInfo::Available(size) = hash[&AttributeType::Modulus] {
assert_eq!(*size, 2048 / 8);
} else {
panic!("Modulus should not return Unavailable on an RSA private key");
}
match hash[&AttributeType::PublicExponent] {
AttributeInfo::Available(_) => {}
_ => panic!("PublicExponent should not return Unavailable on an RSA private key"),
}
match hash[&AttributeType::PrivateExponent] {
AttributeInfo::Sensitive => {}
_ => panic!("Private Exponent of RSA private key should be sensitive"),
}
let hash = session.get_attribute_info_map(private, priv_attribs)?;
if let AttributeInfo::Available(size) = hash[&AttributeType::Modulus] {
assert_eq!(size, 2048 / 8);
} else {
panic!("Modulus should not return Unavailable on an RSA private key");
}
match hash[&AttributeType::PublicExponent] {
AttributeInfo::Available(_) => {}
_ => panic!("Public Exponent should not return Unavailable for an RSA private key"),
}
match hash[&AttributeType::PrivateExponent] {
AttributeInfo::Sensitive => {}
_ => panic!("Private Exponent of RSA private key should be sensitive"),
}
Ok(())
}
#[test]
#[serial]
fn is_fn_supported_test() {
use cryptoki::context::Function;
let (pkcs11, _) = init_pins();
assert!(
pkcs11.is_fn_supported(Function::Initialize),
"C_Initialize function reports as not supported"
);
assert!(
pkcs11.is_fn_supported(Function::Sign),
"C_Sign function reports as not supported"
);
assert!(
pkcs11.is_fn_supported(Function::DigestFinal),
"C_DigestFinal function reports as not supported"
);
}
#[test]
#[serial]
fn is_initialized_test() {
use cryptoki::context::CInitializeArgs;
let pkcs11 = get_pkcs11();
assert!(
!pkcs11.is_initialized(),
"Context created with initialized flag on"
);
// initialize the library
pkcs11.initialize(CInitializeArgs::OsThreads).unwrap();
assert!(
pkcs11.is_initialized(),
"Context was not marked as initialized"
);
match pkcs11.initialize(CInitializeArgs::OsThreads) {
Err(Error::AlreadyInitialized) => (),
Err(e) => panic!("Got unexpected error when initializing: {}", e),
Ok(()) => panic!("Initializing twice should not have been allowed"),
}
}
#[test]
#[serial]
#[allow(clippy::redundant_clone)]
fn test_clone_initialize() {
use cryptoki::context::CInitializeArgs;
let pkcs11 = get_pkcs11();
let clone = pkcs11.clone();
assert!(
!pkcs11.is_initialized(),
"Before initialize() it should not be initialized"
);
assert!(
!clone.is_initialized(),
"Before initialize() the clone should not be initialized"
);
pkcs11.initialize(CInitializeArgs::OsThreads).unwrap();
assert!(
pkcs11.is_initialized(),
"After initialize() it should be initialized"
);
assert!(
clone.is_initialized(),
"After initialize() the clone should be initialized"
);
}
#[test]
#[serial]
fn aes_key_attributes_test() -> 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::AesKeyGen;
// pub key template
let key_template = vec![
Attribute::Class(ObjectClass::SECRET_KEY),
Attribute::Token(true),
Attribute::Sensitive(true),
Attribute::ValueLen(16.into()),
Attribute::KeyType(KeyType::AES),
Attribute::Label(b"testAES".to_vec()),
Attribute::Private(true),
];
// generate a key pair
let key = session.generate_key(&mechanism, &key_template)?;
let mut attributes_result =
session.get_attributes(key, &[AttributeType::EndDate, AttributeType::StartDate])?;
if let Some(Attribute::StartDate(date)) = attributes_result.pop() {
assert!(date.is_empty());
} else {
panic!("Last attribute was not a start date");
}
if let Some(Attribute::EndDate(date)) = attributes_result.pop() {
assert!(date.is_empty());
} else {
panic!("First attribute was not an end date");
}
Ok(())
}
#[test]
#[serial]
fn ro_rw_session_test() -> TestResult {
let public_exponent: Vec<u8> = vec![0x01, 0x00, 0x01];
let modulus = vec![0xFF; 1024];
let template = vec![
Attribute::Token(true),
Attribute::Private(false),
Attribute::PublicExponent(public_exponent),
Attribute::Modulus(modulus),
Attribute::Class(ObjectClass::PUBLIC_KEY),
Attribute::KeyType(KeyType::RSA),
Attribute::Verify(true),
];
let (pkcs11, slot) = init_pins();
// Try out Read-Only session
{
// open a session
let ro_session = pkcs11.open_ro_session(slot)?;
// log in the session
ro_session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
// generate a key pair
// This should NOT work using the Read-Only session
let e = ro_session.create_object(&template).unwrap_err();
if let Error::Pkcs11(RvError::SessionReadOnly) = e {
// as expected
} else {
panic!("Got wrong error code (expecting SessionReadOnly): {}", e);
}
ro_session.logout()?;
}
// Try out Read/Write session
{
// open a session
let rw_session = pkcs11.open_rw_session(slot)?;
// log in the session
rw_session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
// generate a key pair
// This should work using the Read/Write session
let object = rw_session.create_object(&template)?;
// delete keys
rw_session.destroy_object(object)?;
rw_session.logout()?;
}
Ok(())
}
#[test]
#[serial]
fn aes_cbc_encrypt() -> TestResult {
// Encrypt two blocks of zeros with AES-128-CBC, and zero IV
let key = vec![0; 16];
let iv = [0; 16];
let plain = [0; 32];
let expected_cipher = [
0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b, 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b,
0x2e, 0xf7, 0x95, 0xbd, 0x4a, 0x52, 0xe2, 0x9e, 0xd7, 0x13, 0xd3, 0x13, 0xfa, 0x20, 0xe9,
0x8d, 0xbc,
];
let (pkcs11, slot) = init_pins();
let session = pkcs11.open_rw_session(slot)?;
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
let template = [
Attribute::Class(ObjectClass::SECRET_KEY),
Attribute::KeyType(KeyType::AES),
Attribute::Value(key),
Attribute::Encrypt(true),
];
let key_handle = session.create_object(&template)?;
let mechanism = Mechanism::AesCbc(iv);
let cipher = session.encrypt(&mechanism, key_handle, &plain)?;
assert_eq!(expected_cipher[..], cipher[..]);
Ok(())
}
#[test]
#[serial]
fn aes_cbc_pad_encrypt() -> TestResult {
// Encrypt two blocks of zeros with AES-128-CBC and PKCS#7 padding, and zero IV
let key = vec![0; 16];
let iv = [0; 16];
let plain = [0; 32];
let expected_cipher = [
0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b, 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b,
0x2e, 0xf7, 0x95, 0xbd, 0x4a, 0x52, 0xe2, 0x9e, 0xd7, 0x13, 0xd3, 0x13, 0xfa, 0x20, 0xe9,
0x8d, 0xbc, 0x5c, 0x04, 0x76, 0x16, 0x75, 0x6f, 0xdc, 0x1c, 0x32, 0xe0, 0xdf, 0x6e, 0x8c,
0x59, 0xbb, 0x2a,
];
let (pkcs11, slot) = init_pins();
let session = pkcs11.open_rw_session(slot)?;
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
let template = [
Attribute::Class(ObjectClass::SECRET_KEY),
Attribute::KeyType(KeyType::AES),
Attribute::Value(key),
Attribute::Encrypt(true),
];
let key_handle = session.create_object(&template)?;
let mechanism = Mechanism::AesCbcPad(iv);
let cipher = session.encrypt(&mechanism, key_handle, &plain)?;
assert_eq!(expected_cipher[..], cipher[..]);
Ok(())
}
#[test]
#[serial]
fn update_attributes_key() -> 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())))?;
// pub key template
let pub_key_template = vec![
Attribute::Token(true),
Attribute::Private(true),
Attribute::PublicExponent(vec![0x01, 0x00, 0x01]),
Attribute::ModulusBits(1024.into()),
];
// priv key template
let priv_key_template = vec![Attribute::Token(true), Attribute::Extractable(true)];
let (_public_key, private_key) = session.generate_key_pair(
&Mechanism::RsaPkcsKeyPairGen,
&pub_key_template,
&priv_key_template,
)?;
let updated_attributes = vec![Attribute::Extractable(false)];
session.update_attributes(private_key, &updated_attributes)?;
let mut attributes_result =
session.get_attributes(private_key, &[AttributeType::Extractable])?;
if let Some(Attribute::Extractable(ext)) = attributes_result.pop() {
assert!(!ext);
} else {
panic!("Last attribute was not extractable");
}
Ok(())
}
#[test]
#[serial]
fn sha256_digest() -> 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())))?;
// data to digest
let data = vec![0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];
let want = [
0x17, 0x22, 0x6b, 0x1f, 0x68, 0xae, 0xba, 0xcd, 0xef, 0x07, 0x46, 0x45, 0x0f, 0x64, 0x28,
0x74, 0x63, 0x8b, 0x29, 0x57, 0x07, 0xef, 0x73, 0xfb, 0x2c, 0x6b, 0xb7, 0xf8, 0x8e, 0x89,
0x92, 0x9f,
];
let have = session.digest(&Mechanism::Sha256, &data)?;
assert_eq!(want[..], have[..]);
Ok(())
}
#[test]
#[serial]
// Currently empty AAD crashes SoftHSM, see: https://github.com/opendnssec/SoftHSMv2/issues/605
#[ignore]
fn aes_gcm_no_aad() -> TestResult {
// Encrypt two blocks of zeros with AES-128-GCM
let key = vec![0; 16];
let iv = [0; 12];
let aad = [];
let plain = [0; 32];
let expected_cipher_and_tag = [
0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92, 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe,
0x78, 0xf7, 0x95, 0xaa, 0xab, 0x49, 0x4b, 0x59, 0x23, 0xf7, 0xfd, 0x89, 0xff, 0x94, 0x8b,
0xc1, 0xe0, 0x40, 0x49, 0x0a, 0xf4, 0x80, 0x56, 0x06, 0xb2, 0xa3, 0xa2, 0xe7, 0x93,
];
let (pkcs11, slot) = init_pins();
let session = pkcs11.open_rw_session(slot)?;
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
let template = [
Attribute::Class(ObjectClass::SECRET_KEY),
Attribute::KeyType(KeyType::AES),
Attribute::Value(key),
Attribute::Encrypt(true),
];
let key_handle = session.create_object(&template)?;
let mechanism = Mechanism::AesGcm(GcmParams::new(&iv, &aad, 96.into()));
let cipher_and_tag = session.encrypt(&mechanism, key_handle, &plain)?;
assert_eq!(expected_cipher_and_tag[..], cipher_and_tag[..]);
Ok(())
}
#[test]
#[serial]
fn aes_gcm_with_aad() -> TestResult {
// Encrypt a block of zeros with AES-128-GCM.
// Use another block of zeros for AAD.
let key = vec![0; 16];
let iv = [0; 12];
let aad = [0; 16];
let plain = [0; 16];
let expected_cipher_and_tag = [
0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92, 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe,
0x78, 0xd2, 0x4e, 0x50, 0x3a, 0x1b, 0xb0, 0x37, 0x07, 0x1c, 0x71, 0xb3, 0x5d,
];
let (pkcs11, slot) = init_pins();
let session = pkcs11.open_rw_session(slot)?;
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
let template = [
Attribute::Class(ObjectClass::SECRET_KEY),
Attribute::KeyType(KeyType::AES),
Attribute::Value(key),
Attribute::Encrypt(true),
];
let key_handle = session.create_object(&template)?;
let mechanism = Mechanism::AesGcm(GcmParams::new(&iv, &aad, 96.into()));
let cipher_and_tag = session.encrypt(&mechanism, key_handle, &plain)?;
assert_eq!(expected_cipher_and_tag[..], cipher_and_tag[..]);
Ok(())
}
#[test]
#[serial]
fn rsa_pkcs_oaep_empty() -> TestResult {
let (pkcs11, slot) = init_pins();
let session = pkcs11.open_rw_session(slot)?;
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
let pub_key_template = [Attribute::ModulusBits(2048.into())];
let (pubkey, privkey) =
session.generate_key_pair(&Mechanism::RsaPkcsKeyPairGen, &pub_key_template, &[])?;
let oaep = PkcsOaepParams::new(
MechanismType::SHA1,
PkcsMgfType::MGF1_SHA1,
PkcsOaepSource::empty(),
);
let encrypt_mechanism: Mechanism = Mechanism::RsaPkcsOaep(oaep);
let encrypted_data = session.encrypt(&encrypt_mechanism, pubkey, b"Hello")?;
let decrypted_data = session.decrypt(&encrypt_mechanism, privkey, &encrypted_data)?;
let decrypted = String::from_utf8(decrypted_data)?;
assert_eq!("Hello", decrypted);
Ok(())
}
#[test]
#[serial]
#[ignore] // it's not clear why the test with data specified fails
fn rsa_pkcs_oaep_with_data() -> TestResult {
let (pkcs11, slot) = init_pins();
let session = pkcs11.open_rw_session(slot)?;
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
let pub_key_template = [Attribute::ModulusBits(2048.into())];
let (pubkey, privkey) =
session.generate_key_pair(&Mechanism::RsaPkcsKeyPairGen, &pub_key_template, &[])?;
let oaep = PkcsOaepParams::new(
MechanismType::SHA1,
PkcsMgfType::MGF1_SHA1,
PkcsOaepSource::data_specified(&[1, 2, 3, 4, 5, 6, 7, 8]),
);
let encrypt_mechanism: Mechanism = Mechanism::RsaPkcsOaep(oaep);
let encrypted_data = session.encrypt(&encrypt_mechanism, pubkey, b"Hello")?;
let decrypted_data = session.decrypt(&encrypt_mechanism, privkey, &encrypted_data)?;
let decrypted = String::from_utf8(decrypted_data)?;
assert_eq!("Hello", decrypted);
Ok(())
}
#[test]
#[serial]
fn get_slot_event() -> TestResult {
// Not implemented in SoftHSMv2
// https://github.com/opendnssec/SoftHSMv2/issues/370
let (pkcs11, _slot) = init_pins();
let event = pkcs11.get_slot_event()?;
assert_eq!(None, event);
Ok(())
}
#[test]
#[serial]
fn wait_for_slot_event() {
// Not implemented in SoftHSMv2
// https://github.com/opendnssec/SoftHSMv2/issues/370
let (pkcs11, _slot) = init_pins();
let res = pkcs11.wait_for_slot_event();
assert!(
matches!(res, Err(Error::Pkcs11(RvError::FunctionNotSupported))),
"res = {:?}",
res
);
}
|