Skip to content

Commit f290856

Browse files
committed
refactor(policysigned)!: expiration type change with code cleanup
- Add Duration parser and use it for expiration CLI argument - Remove unnecessary to_vec() in qualification data parsing - Use marshall to serialise ticket more safely Signed-off-by: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com>
1 parent 2f45b9c commit f290856

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

src/cmd/policysigned.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
use std::path::PathBuf;
4+
use std::time::Duration;
45

56
use anyhow::Context;
67
use clap::Parser;
78
use log::info;
89
use tss_esapi::constants::SessionType;
910
use tss_esapi::handles::{ObjectHandle, SessionHandle};
1011
use tss_esapi::structures::{Digest, Nonce, Signature};
11-
use tss_esapi::traits::UnMarshall;
12+
use tss_esapi::traits::{Marshall, UnMarshall};
1213
use tss_esapi::tss2_esys::TPMT_TK_AUTH;
1314

1415
use crate::cli::GlobalOpts;
1516
use crate::context::create_context;
1617
use crate::handle::{ContextSource, load_object_from_source};
17-
use crate::parse::parse_context_source;
18+
use crate::parse::{parse_context_source, parse_duration};
1819
use crate::session::load_session_from_file;
1920

2021
/// Authorize a policy with a signed authorization.
@@ -35,8 +36,8 @@ pub struct PolicySignedCmd {
3536
pub signature: PathBuf,
3637

3738
/// Expiration time in seconds (0 = no expiration)
38-
#[arg(short = 'x', long = "expiration", default_value = "0")]
39-
pub expiration: i32,
39+
#[arg(short = 'x', long = "expiration", value_parser = parse_duration, default_value = "0")]
40+
pub expiration: Option<Duration>,
4041

4142
/// cpHash file (optional)
4243
#[arg(long = "cphash-input")]
@@ -84,25 +85,19 @@ impl PolicySignedCmd {
8485
};
8586

8687
let policy_ref = match &self.qualification {
87-
Some(bytes) => Nonce::try_from(bytes.as_slice().to_vec())
88+
Some(bytes) => Nonce::try_from(bytes.as_slice())
8889
.map_err(|e| anyhow::anyhow!("qualifying data: {e}"))?,
8990
None => Nonce::default(),
9091
};
9192

92-
let expiration = if self.expiration == 0 {
93-
None
94-
} else {
95-
Some(std::time::Duration::from_secs(self.expiration as u64))
96-
};
97-
9893
let (timeout, ticket) = ctx
9994
.policy_signed(
10095
policy_session,
10196
auth_object,
10297
Nonce::default(), // nonce_tpm
10398
cp_hash,
10499
policy_ref,
105-
expiration,
100+
self.expiration,
106101
signature,
107102
)
108103
.context("TPM2_PolicySigned failed")?;
@@ -118,12 +113,9 @@ impl PolicySignedCmd {
118113
let tss_ticket: TPMT_TK_AUTH = ticket
119114
.try_into()
120115
.map_err(|e| anyhow::anyhow!("failed to convert ticket: {e:?}"))?;
121-
let bytes = unsafe {
122-
std::slice::from_raw_parts(
123-
&tss_ticket as *const TPMT_TK_AUTH as *const u8,
124-
std::mem::size_of::<TPMT_TK_AUTH>(),
125-
)
126-
};
116+
let bytes = ticket
117+
.marshall()
118+
.map_err(|e| anyhow::anyhow!("failed to marshall ticket: {e}"))?;
127119
std::fs::write(path, bytes)
128120
.with_context(|| format!("writing ticket to {}", path.display()))?;
129121
}

src/parse.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ pub fn parse_hex_u32(s: &str) -> Result<u32, String> {
4646
.map_err(|_| format!("expected a hex value (e.g. 0x01400001), got: '{s}'"))
4747
}
4848

49+
// ---------------------------------------------------------------------------
50+
// Duration
51+
// ---------------------------------------------------------------------------
52+
53+
pub fn parse_duration(s: &str) -> Result<Option<std::time::Duration>, String> {
54+
let secs: u64 = s
55+
.parse()
56+
.map_err(|_| format!("expected a u64 value, got: '{s}'"))?;
57+
let duration = match secs {
58+
0 => None,
59+
_ => Some(std::time::Duration::from_secs(secs)),
60+
};
61+
Ok(duration)
62+
}
63+
4964
// ---------------------------------------------------------------------------
5065
// Context source
5166
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)