Skip to content

Commit cff5dbd

Browse files
committed
refactor(policysigned)!: add Duration parser and use it
Signed-off-by: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com>
1 parent 2f45b9c commit cff5dbd

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/cmd/policysigned.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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;
@@ -14,7 +15,7 @@ use tss_esapi::tss2_esys::TPMT_TK_AUTH;
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,33 +85,27 @@ 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")?;
109104

110105
info!("policy signed succeeded");
111106

112107
if let Some(ref path) = self.timeout_out {
113-
std::fs::write(path, timeout.as_bytes())
108+
std::fs::write(path, timeout.as_bytes().to_vec())
114109
.with_context(|| format!("writing timeout to {}", path.display()))?;
115110
}
116111

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)