11// SPDX-License-Identifier: Apache-2.0
22
33use std:: path:: PathBuf ;
4+ use std:: time:: Duration ;
45
56use anyhow:: Context ;
67use clap:: Parser ;
78use log:: info;
89use tss_esapi:: constants:: SessionType ;
910use tss_esapi:: handles:: { ObjectHandle , SessionHandle } ;
1011use tss_esapi:: structures:: { Digest , Nonce , Signature } ;
11- use tss_esapi:: traits:: UnMarshall ;
12+ use tss_esapi:: traits:: { Marshall , UnMarshall } ;
1213use tss_esapi:: tss2_esys:: TPMT_TK_AUTH ;
1314
1415use crate :: cli:: GlobalOpts ;
1516use crate :: context:: create_context;
1617use crate :: handle:: { ContextSource , load_object_from_source} ;
17- use crate :: parse:: parse_context_source;
18+ use crate :: parse:: { parse_context_source, parse_duration } ;
1819use 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 }
0 commit comments