Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ newgrp tss
ls -l /dev/tpm*

# Set TPM device path used by rust-tpm2-cli
export TPM2TOOLS_TCTI="device:/dev/tpm0"
export RUST_TPM2_CLI_TCTI="device:/dev/tpm0"
```

### Set up swtpm (software TPM simulator)
Expand All @@ -85,7 +85,7 @@ swtpm socket \
--flags startup-clear

# In another terminal:
export TPM2TOOLS_TCTI="swtpm:host=localhost,port=2321"
export RUST_TPM2_CLI_TCTI="swtpm:host=localhost,port=2321"
```

### Run integration tests
Expand Down
4 changes: 2 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub struct Cli {

#[derive(Parser)]
pub struct GlobalOpts {
/// TCTI configuration (e.g. device:/dev/tpm0, mssim:host=localhost,port=2321)
#[arg(short = 'T', long = "tcti", env = "TPM2TOOLS_TCTI")]
/// TCTI configuration (e.g. device:/dev/tpm0, swtpm:host=localhost,port=2321)
#[arg(short = 'T', long = "tcti", env = "RUST_TPM2_CLI_TCTI")]
pub tcti: Option<String>,

/// Enable errata fixups
Expand Down
6 changes: 3 additions & 3 deletions src/raw_esys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use std::ptr::{null, null_mut};
use anyhow::{Context, bail};
use tss_esapi::tss2_esys::*;

use crate::tcti::DEFAULT_TCTI;

// -----------------------------------------------------------------------
// Raw context helpers
// -----------------------------------------------------------------------
Expand All @@ -27,9 +29,7 @@ impl RawEsysContext {
pub(crate) fn new(tcti: Option<&str>) -> anyhow::Result<Self> {
let tcti_str = match tcti {
Some(s) => s.to_owned(),
None => {
std::env::var("TPM2TOOLS_TCTI").unwrap_or_else(|_| "device:/dev/tpm0".to_owned())
}
None => std::env::var("RUST_TPM2_CLI_TCTI").unwrap_or_else(|_| DEFAULT_TCTI.to_owned()),
};
let c_str = CString::new(tcti_str.as_str()).context("TCTI string contains NUL")?;

Expand Down
6 changes: 3 additions & 3 deletions src/tcti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ pub(crate) const DEFAULT_DEVICE_PATH: &str = "/dev/tpm0";

/// Parse a TCTI configuration string into a [`TctiNameConf`].
///
/// If `tcti` is `None`, falls back to the `TPM2TOOLS_TCTI` environment
/// If `tcti` is `None`, falls back to the `RUST_TPM2_CLI_TCTI` environment
/// variable, then to `device:/dev/tpm0`.
pub fn parse_tcti(tcti: Option<&str>) -> Result<TctiNameConf, Tpm2Error> {
let tcti_str = match tcti {
Some(s) => s.to_owned(),
None => std::env::var("TPM2TOOLS_TCTI").unwrap_or_else(|_| DEFAULT_TCTI.to_owned()),
None => std::env::var("RUST_TPM2_CLI_TCTI").unwrap_or_else(|_| DEFAULT_TCTI.to_owned()),
};
Comment on lines 20 to 23

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic for resolving the TCTI string is duplicated in extract_device_path on lines 31-34. To improve maintainability, consider extracting this block into a private helper function within this module. This would also be beneficial for raw_esys.rs which has similar logic.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposed refactoring looks good to me, while it is irrelevant to this PR. It will likely be implemented during a future refactoring phase.

TctiNameConf::from_str(&tcti_str).map_err(|e| Tpm2Error::InvalidTcti(e.to_string()))
}
Expand All @@ -30,7 +30,7 @@ pub fn parse_tcti(tcti: Option<&str>) -> Result<TctiNameConf, Tpm2Error> {
pub(crate) fn extract_device_path(tcti: Option<&str>) -> String {
let tcti_str = match tcti {
Some(s) => s.to_owned(),
None => std::env::var("TPM2TOOLS_TCTI").unwrap_or_else(|_| DEFAULT_TCTI.to_owned()),
None => std::env::var("RUST_TPM2_CLI_TCTI").unwrap_or_else(|_| DEFAULT_TCTI.to_owned()),
};
if let Some(rest) = tcti_str.strip_prefix("device:") {
rest.to_owned()
Expand Down
4 changes: 2 additions & 2 deletions tests/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ summary() {
return 0
}

# Start an swtpm simulator and set TPM2TOOLS_TCTI.
# Start an swtpm simulator and set RUST_TPM2_CLI_TCTI.
start_swtpm() {
TEST_TMPDIR="$(mktemp -d)"
export TEST_TMPDIR
Expand All @@ -84,7 +84,7 @@ start_swtpm() {
sleep 0.1
done

export TPM2TOOLS_TCTI="swtpm:host=localhost,port=${SWTPM_PORT}"
export RUST_TPM2_CLI_TCTI="swtpm:host=localhost,port=${SWTPM_PORT}"
}

# Stop swtpm and clean up.
Expand Down