Skip to content

Commit c6d339b

Browse files
committed
Improve TPM provider
This commit includes a couple of fixes in the TPM provider. * the TSS crate used has been updated to the latest one, including a few functionality updates; this now allows the session hash algorithm and the cipher used for sessions and primary key * more control over the format of the authentication value is now offered to admins; more precisely, they can provide string versions of hex values, prefixed by "hex:" Signed-off-by: Ionut Mihalcea <ionut.mihalcea@arm.com>
1 parent 4d5594f commit c6d339b

File tree

8 files changed

+107
-30
lines changed

8 files changed

+107
-30
lines changed

Cargo.lock

Lines changed: 44 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ log = { version = "0.4.8", features = ["serde"] }
3333
pkcs11 = { version = "0.4.0", optional = true }
3434
picky-asn1-der = { version = "0.2.2", optional = true }
3535
picky-asn1 = { version = "0.2.1", optional = true }
36-
tss-esapi = { version = "2.0.0", optional = true }
36+
tss-esapi = { version = "4.0.0-alpha.1", optional = true }
3737
bincode = "1.1.4"
3838
structopt = "0.3.5"
3939
derivative = "2.1.1"
4040
version = "3.0.0"
41+
hex = "0.4.2"
4142

4243
[dev-dependencies]
4344
ring = "0.16.12"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ This project uses the following third party crates:
116116
* lazy_static (MIT and Apache-2.0)
117117
* version (MIT and Apache-2.0)
118118
* sha2 (MIT and Apache-2.0)
119+
* hex (MIT and Apache-2.0)
119120

120121
This project uses the following third party libraries:
121122
* [**Mbed Crypto**](https://github.com/ARMmbed/mbed-crypto) (Apache-2.0)

config.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,8 @@ key_info_manager = "on-disk-manager"
7474
# - "tabrmd": uses the TPM2 Access Broker & Resource Management Daemon
7575
#tcti = "mssim"
7676
# (Required) Authentication value for performing operations on the TPM Owner Hierarchy. The string can
77-
# be empty, however we strongly suggest that you use a secure password.
77+
# be empty, however we strongly suggest that you use a secure passcode.
78+
# To align with TPM tooling, PARSEC allows "owner_hierarchy_auth" to have a prefix indicating a string value,
79+
# e.g. "str:password", or to represent a string version of a hex value, e.g. "hex:1a2b3c". If no prefix is
80+
# provided, the value is considered to be a string.
7881
#owner_hierarchy_auth = "password"

e2e_tests/provider_cfg/all/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ RUN cd mbed-crypto-mbedcrypto-2.0.0 \
1616

1717
WORKDIR /tmp
1818
# Download and install TSS 2.0
19-
RUN git clone https://github.com/tpm2-software/tpm2-tss.git --branch 2.3.1
19+
RUN git clone https://github.com/tpm2-software/tpm2-tss.git --branch 2.3.3
2020
RUN cd tpm2-tss \
2121
&& ./bootstrap \
2222
&& ./configure \

e2e_tests/provider_cfg/tpm/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FROM tpm2software/tpm2-tss:ubuntu-18.04
33
ENV PKG_CONFIG_PATH /usr/local/lib/pkgconfig
44

55
# Download and install TSS 2.0
6-
RUN git clone https://github.com/tpm2-software/tpm2-tss.git --branch 2.3.1
6+
RUN git clone https://github.com/tpm2-software/tpm2-tss.git --branch 2.3.3
77
RUN cd tpm2-tss \
88
&& ./bootstrap \
99
&& ./configure \

e2e_tests/provider_cfg/tpm/config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ manager_type = "OnDisk"
1616
provider_type = "Tpm"
1717
key_info_manager = "on-disk-manager"
1818
tcti = "mssim"
19-
owner_hierarchy_auth = "tpm_pass"
19+
owner_hierarchy_auth = "hex:74706d5f70617373" # "tpm_pass" in hex

src/providers/tpm_provider/mod.rs

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ const SUPPORTED_OPCODES: [Opcode; 7] = [
3434
Opcode::ListOpcodes,
3535
];
3636

37-
const ROOT_KEY_SIZE: usize = 2048;
37+
const ROOT_KEY_SIZE: u16 = 2048;
3838
const ROOT_KEY_AUTH_SIZE: usize = 32;
39+
const AUTH_STRING_PREFIX: &str = "str:";
40+
const AUTH_HEX_PREFIX: &str = "hex:";
3941

4042
/// Provider for Trusted Platform Modules
4143
///
@@ -49,7 +51,7 @@ pub struct TpmProvider {
4951
// The Mutex is needed both because interior mutability is needed to the ESAPI Context
5052
// structure that is shared between threads and because two threads are not allowed the same
5153
// ESAPI context simultaneously.
52-
esapi_context: Mutex<tss_esapi::TransientObjectContext>,
54+
esapi_context: Mutex<tss_esapi::TransientKeyContext>,
5355
// The Key Info Manager stores the key context and its associated authValue (a PasswordContext
5456
// structure).
5557
#[derivative(Debug = "ignore")]
@@ -60,7 +62,7 @@ impl TpmProvider {
6062
// Creates and initialise a new instance of TpmProvider.
6163
fn new(
6264
key_info_store: Arc<RwLock<dyn ManageKeyInfo + Send + Sync>>,
63-
esapi_context: tss_esapi::TransientObjectContext,
65+
esapi_context: tss_esapi::TransientKeyContext,
6466
) -> Option<TpmProvider> {
6567
Some(TpmProvider {
6668
esapi_context: Mutex::new(esapi_context),
@@ -192,35 +194,64 @@ impl TpmProviderBuilder {
192194
self
193195
}
194196

197+
fn get_hierarchy_auth(&mut self) -> std::io::Result<Vec<u8>> {
198+
match self.owner_hierarchy_auth.take() {
199+
None => Err(std::io::Error::new(
200+
ErrorKind::InvalidData,
201+
"missing owner hierarchy auth",
202+
)),
203+
Some(mut auth) if auth.starts_with(AUTH_STRING_PREFIX) => {
204+
Ok(auth.split_off(AUTH_STRING_PREFIX.len()).into())
205+
}
206+
Some(mut auth) if auth.starts_with(AUTH_HEX_PREFIX) => Ok(hex::decode(
207+
auth.split_off(AUTH_STRING_PREFIX.len()),
208+
)
209+
.or_else(|_| {
210+
Err(std::io::Error::new(
211+
ErrorKind::InvalidData,
212+
"invalid hex owner hierarchy auth",
213+
))
214+
})?),
215+
Some(auth) => Ok(auth.into()),
216+
}
217+
}
218+
195219
/// Create an instance of TpmProvider
196220
///
197221
/// # Safety
198222
///
199223
/// Undefined behaviour might appear if two instances of TransientObjectContext are created
200224
/// using a same TCTI that does not handle multiple applications concurrently.
201-
pub unsafe fn build(self) -> std::io::Result<TpmProvider> {
225+
pub unsafe fn build(mut self) -> std::io::Result<TpmProvider> {
226+
let hierarchy_auth = self.get_hierarchy_auth()?;
202227
TpmProvider::new(
203228
self.key_info_store.ok_or_else(|| {
204229
std::io::Error::new(ErrorKind::InvalidData, "missing key info store")
205230
})?,
206-
tss_esapi::TransientObjectContext::new(
207-
self.tcti
208-
.ok_or_else(|| std::io::Error::new(ErrorKind::InvalidData, "missing TCTI"))?,
209-
ROOT_KEY_SIZE,
210-
ROOT_KEY_AUTH_SIZE,
211-
self.owner_hierarchy_auth
212-
.ok_or_else(|| {
213-
std::io::Error::new(ErrorKind::InvalidData, "missing owner hierarchy auth")
214-
})?
215-
.as_bytes(),
216-
)
217-
.or_else(|e| {
218-
error!("Error creating TSS Transient Object Context ({}).", e);
219-
Err(std::io::Error::new(
220-
ErrorKind::InvalidData,
221-
"failed initializing TSS context",
222-
))
223-
})?,
231+
tss_esapi::abstraction::transient::TransientKeyContextBuilder::new()
232+
.with_tcti(
233+
self.tcti.ok_or_else(|| {
234+
std::io::Error::new(ErrorKind::InvalidData, "missing TCTI")
235+
})?,
236+
)
237+
.with_root_key_size(ROOT_KEY_SIZE)
238+
.with_root_key_auth_size(ROOT_KEY_AUTH_SIZE)
239+
.with_hierarchy_auth(hierarchy_auth)
240+
.with_hierarchy(tss_esapi::utils::Hierarchy::Owner)
241+
.with_session_hash_alg(
242+
tss_esapi::utils::algorithm_specifiers::HashingAlgorithm::Sha256.into(),
243+
)
244+
.with_default_context_cipher(
245+
tss_esapi::utils::algorithm_specifiers::Cipher::aes_256_cfb(),
246+
)
247+
.build()
248+
.or_else(|e| {
249+
error!("Error creating TSS Transient Object Context ({}).", e);
250+
Err(std::io::Error::new(
251+
ErrorKind::InvalidData,
252+
"failed initializing TSS context",
253+
))
254+
})?,
224255
)
225256
.ok_or_else(|| {
226257
std::io::Error::new(ErrorKind::InvalidData, "failed initializing TPM provider")

0 commit comments

Comments
 (0)