@@ -33,6 +33,8 @@ use log::{debug, error, info};
3333use std:: env;
3434use std:: fs;
3535use std:: process;
36+ mod error;
37+ use error:: QuoteGeneratorError ;
3638
3739const REPORT_DATA_SIZE : usize = 64 ;
3840const REPORT_SIZE : usize = 1024 ;
@@ -49,18 +51,17 @@ const QUOTE_FILE_NAME: &str = "quote.dat";
4951///
5052/// # Returns
5153///
52- /// A `tdx_report_data_t` structure containing the input bytes.
54+ /// A `Result` containing the `tdx_report_data_t` structure, or a `QuoteGeneratorError`
55+ /// if the input bytes cannot be converted.
5356///
54- /// # Panics
57+ /// # Errors
5558///
56- /// Panics if `input_bytes` length doesn't match `REPORT_DATA_SIZE` (due to `try_into().unwrap()`) .
57- fn create_report_data ( input_bytes : & [ u8 ] ) -> tdx_attest_rs:: tdx_report_data_t {
59+ /// Returns `QuoteGeneratorError::ReportDataConversion` if input bytes length doesn't match `REPORT_DATA_SIZE`.
60+ fn create_report_data ( input_bytes : & [ u8 ] ) -> Result < tdx_attest_rs:: tdx_report_data_t , QuoteGeneratorError > {
5861 let report_data = tdx_attest_rs:: tdx_report_data_t {
59- d : input_bytes. try_into ( ) . unwrap ( ) ,
62+ d : input_bytes. try_into ( ) ? ,
6063 } ;
61- debug ! ( "TDX report data: {:?}" , report_data. d) ;
62-
63- report_data
64+ Ok ( report_data)
6465}
6566
6667/// Generates and displays a TDX report for the given report data.
@@ -130,7 +131,7 @@ fn create_quote(report_data: &tdx_attest_rs::tdx_report_data_t) -> Vec<u8> {
130131 }
131132}
132133
133- fn main ( ) {
134+ fn main ( ) -> Result < ( ) , QuoteGeneratorError > {
134135 // Initialize the logger (defaults to INFO level, override with RUST_LOG env var)
135136 env_logger:: init ( ) ;
136137
@@ -154,9 +155,11 @@ fn main() {
154155 let mut report_bytes = [ 0u8 ; REPORT_DATA_SIZE ] ;
155156 report_bytes[ ..input_bytes. len ( ) ] . copy_from_slice ( input_bytes) ;
156157
157- let report_data = create_report_data ( & report_bytes) ;
158+ let report_data = create_report_data ( & report_bytes) ? ;
158159 display_tdx_report ( & report_data) ; // Report is only displayed on debug mode - this function is optional
159160 let quote = create_quote ( & report_data) ;
160161 fs:: write ( QUOTE_FILE_NAME , quote) . expect ( "Unable to write quote file" ) ;
161162 info ! ( "Quote successfully written to {}" , QUOTE_FILE_NAME ) ;
163+
164+ Ok ( ( ) )
162165}
0 commit comments