|
| 1 | +// Copyright 2021 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +#![cfg(feature = "tpm-provider")] |
| 4 | +use e2e_tests::auto_test_keyname; |
| 5 | +use e2e_tests::parsec_client::core::basic_client::PrepareActivateCredential; |
| 6 | +use e2e_tests::TestClient; |
| 7 | +use parsec_client::core::interface::requests::Opcode; |
| 8 | +use std::{ |
| 9 | + convert::{TryFrom, TryInto}, |
| 10 | + env, |
| 11 | + str::FromStr, |
| 12 | +}; |
| 13 | +use tss_esapi::{ |
| 14 | + abstraction::ek, |
| 15 | + abstraction::transient::MakeCredParams, |
| 16 | + interface_types::{algorithm::AsymmetricAlgorithm, resource_handles::Hierarchy}, |
| 17 | + structures::{Public, PublicKeyRsa}, |
| 18 | + tcti_ldr::{NetworkTPMConfig, TctiNameConf}, |
| 19 | + utils::PublicKey, |
| 20 | + Context, |
| 21 | +}; |
| 22 | + |
| 23 | +const DEFAULT_HELPER_TPM_CONF: &str = "port=4321"; |
| 24 | +const CREDENTIAL: [u8; 16] = [0x11; 16]; |
| 25 | + |
| 26 | +fn create_tcti() -> TctiNameConf { |
| 27 | + match env::var("TEST_TCTI") { |
| 28 | + Err(_) => TctiNameConf::Mssim( |
| 29 | + NetworkTPMConfig::from_str(DEFAULT_HELPER_TPM_CONF) |
| 30 | + .expect("Failed to parse default TPM config"), |
| 31 | + ), |
| 32 | + Ok(tctistr) => TctiNameConf::from_str(&tctistr).expect("Error parsing TEST_TCTI"), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +fn make_credential(prep_activ_cred: PrepareActivateCredential) -> (Vec<u8>, Vec<u8>) { |
| 37 | + let make_cred_params = MakeCredParams { |
| 38 | + name: prep_activ_cred.name, |
| 39 | + public: prep_activ_cred.public, |
| 40 | + attesting_key_pub: PublicKey::Rsa(prep_activ_cred.attesting_key_pub), |
| 41 | + }; |
| 42 | + let mut basic_ctx = Context::new(create_tcti()).expect("Failed to start TPM context"); |
| 43 | + // the public part of the EK is used, so we retrieve the parameters |
| 44 | + let key_pub = |
| 45 | + ek::create_ek_public_from_default_template(AsymmetricAlgorithm::Rsa, None).unwrap(); |
| 46 | + let key_pub = if let Public::Rsa { |
| 47 | + object_attributes, |
| 48 | + name_hashing_algorithm, |
| 49 | + auth_policy, |
| 50 | + parameters, |
| 51 | + .. |
| 52 | + } = key_pub |
| 53 | + { |
| 54 | + Public::Rsa { |
| 55 | + object_attributes, |
| 56 | + name_hashing_algorithm, |
| 57 | + auth_policy, |
| 58 | + parameters, |
| 59 | + unique: if let PublicKey::Rsa(val) = make_cred_params.attesting_key_pub { |
| 60 | + PublicKeyRsa::try_from(val).unwrap() |
| 61 | + } else { |
| 62 | + panic!("Wrong public key type"); |
| 63 | + }, |
| 64 | + } |
| 65 | + } else { |
| 66 | + panic!("Wrong Public type"); |
| 67 | + }; |
| 68 | + let pub_handle = basic_ctx |
| 69 | + .load_external_public(&key_pub, Hierarchy::Owner) |
| 70 | + .unwrap(); |
| 71 | + |
| 72 | + let (cred, secret) = basic_ctx |
| 73 | + .make_credential( |
| 74 | + pub_handle, |
| 75 | + CREDENTIAL.to_vec().try_into().unwrap(), |
| 76 | + make_cred_params.name.to_vec().try_into().unwrap(), |
| 77 | + ) |
| 78 | + .unwrap(); |
| 79 | + (cred.value().to_vec(), secret.value().to_vec()) |
| 80 | +} |
| 81 | + |
| 82 | +#[test] |
| 83 | +fn activate_credential() { |
| 84 | + let key_name = auto_test_keyname!(); |
| 85 | + let mut client = TestClient::new(); |
| 86 | + if !client.is_operation_supported(Opcode::PrepareKeyAttestation) { |
| 87 | + return; |
| 88 | + } |
| 89 | + client |
| 90 | + .generate_rsa_sign_key(key_name.clone()) |
| 91 | + .expect("Failed to generate key"); |
| 92 | + let prep_activ_cred = client |
| 93 | + .prepare_activate_credential(key_name.clone()) |
| 94 | + .expect("Failed to get parameters for MakeCredential"); |
| 95 | + |
| 96 | + let (cred, secret) = make_credential(prep_activ_cred); |
| 97 | + |
| 98 | + let cred_out = client |
| 99 | + .activate_credential(key_name, cred, secret) |
| 100 | + .expect("Failed to activate credential"); |
| 101 | + |
| 102 | + assert_eq!(cred_out, CREDENTIAL.to_vec()); |
| 103 | +} |
0 commit comments