44 * All rights reserved.
55 **********************************************************************************************/
66
7- use crate :: { ErrorCode , InternalError , json:: JsonValue } ;
7+ use crate :: { ErrorCode , InternalError , json:: JsonValue , memory:: CStringHolder } ;
8+
9+ // ==========================================================================
10+ // Helper macros
11+ // ==========================================================================
12+
13+ macro_rules! not_empty {
14+ ( $value: ident) => {
15+ ( !$value. is_empty( ) )
16+ } ;
17+ }
18+
19+ macro_rules! opt_check {
20+ ( $value: ident) => {
21+ $value. as_ref( ) . is_none_or( |inner| not_empty!( inner) )
22+ } ;
23+ }
824
925// ==========================================================================
1026// Import Data
1127// ==========================================================================
1228
1329/// Variant that holds the actual import data
1430#[ derive( Clone , Copy , Debug ) ]
15- pub ( crate ) enum ImportDataVariant < ' a > {
31+ #[ non_exhaustive]
32+ enum ImportVariant < ' a > {
1633 Pem ( & ' a str ) ,
1734 Json ( & ' a JsonValue ) ,
1835}
@@ -21,7 +38,7 @@ pub(crate) enum ImportDataVariant<'a> {
2138///
2239/// Instances of this struct may be used with the [`FapiContext::import()`](crate::FapiContext::import) function.
2340#[ derive( Clone , Copy , Debug ) ]
24- pub struct ImportData < ' a > ( ImportDataVariant < ' a > ) ;
41+ pub struct ImportData < ' a > ( ImportVariant < ' a > ) ;
2542
2643impl < ' a > ImportData < ' a > {
2744 /// Attempts to create a new `ImportData` from the given `JsonValue` reference.
@@ -31,7 +48,7 @@ impl<'a> ImportData<'a> {
3148 /// The JSON data will be validated, by the FAPI, when it is actually used.
3249 pub fn from_json ( json_value : & ' a JsonValue ) -> Result < Self , ErrorCode > {
3350 if json_value. is_object ( ) && ( !json_value. is_empty ( ) ) {
34- Ok ( Self ( ImportDataVariant :: Json ( json_value) ) )
51+ Ok ( Self ( ImportVariant :: Json ( json_value) ) )
3552 } else {
3653 Err ( ErrorCode :: InternalError ( InternalError :: InvalidArguments ) )
3754 }
@@ -48,14 +65,21 @@ impl<'a> ImportData<'a> {
4865 || pem_data. starts_with ( "-----BEGIN RSA PRIVATE KEY-----" )
4966 || pem_data. starts_with ( "-----BEGIN EC PRIVATE KEY-----" )
5067 {
51- Ok ( Self ( ImportDataVariant :: Pem ( pem_data) ) )
68+ Ok ( Self ( ImportVariant :: Pem ( pem_data) ) )
5269 } else {
5370 Err ( ErrorCode :: InternalError ( InternalError :: InvalidArguments ) )
5471 }
5572 }
73+ }
5674
57- pub ( crate ) fn into_inner ( self ) -> ImportDataVariant < ' a > {
58- self . 0
75+ impl TryFrom < ImportData < ' _ > > for CStringHolder {
76+ type Error = ErrorCode ;
77+
78+ fn try_from ( data : ImportData ) -> Result < Self , Self :: Error > {
79+ match data. 0 {
80+ ImportVariant :: Json ( json_value) => CStringHolder :: try_from ( json_value) ,
81+ ImportVariant :: Pem ( pem_data) => CStringHolder :: try_from ( pem_data) ,
82+ }
5983 }
6084}
6185
@@ -66,17 +90,16 @@ impl<'a> ImportData<'a> {
6690/// Contains the result of a signing operation.
6791///
6892/// Instances of this struct are returned by the [`FapiContext::sign()`](crate::FapiContext::sign) function.
93+ #[ non_exhaustive]
6994pub struct SignResult {
7095 pub sign_value : Vec < u8 > ,
7196 pub public_key : Option < String > ,
7297 pub certificate : Option < String > ,
7398}
7499
75100impl SignResult {
76- pub ( crate ) fn from ( sign_value : Vec < u8 > , public_key : Option < String > , certificate : Option < String > ) -> Self {
77- assert ! ( !sign_value. is_empty( ) ) ;
78- assert ! ( public_key. as_ref( ) . is_none_or( |value| !value. is_empty( ) ) ) ;
79- assert ! ( certificate. as_ref( ) . is_none_or( |value| !value. is_empty( ) ) ) ;
101+ pub fn from ( sign_value : Vec < u8 > , public_key : Option < String > , certificate : Option < String > ) -> Self {
102+ assert ! ( not_empty!( sign_value) && opt_check!( public_key) && opt_check!( certificate) , "A required value is missing!" ) ;
80103 Self { sign_value, public_key, certificate }
81104 }
82105}
@@ -88,6 +111,7 @@ impl SignResult {
88111/// Contains the result of a cryptographic quoting operation.
89112///
90113/// Instances of this struct are returned by the [`FapiContext::quote()`](crate::FapiContext::quote) function.
114+ #[ non_exhaustive]
91115pub struct QuoteResult {
92116 pub quote_info : JsonValue ,
93117 pub signature : Vec < u8 > ,
@@ -96,12 +120,8 @@ pub struct QuoteResult {
96120}
97121
98122impl QuoteResult {
99- pub ( crate ) fn from ( quote_info : JsonValue , signature : Vec < u8 > , prc_log : Option < JsonValue > , certificate : Option < String > ) -> Self {
100- assert ! ( !quote_info. is_empty( ) ) ;
101- assert ! ( !signature. is_empty( ) ) ;
102- assert ! ( prc_log. as_ref( ) . is_none_or( |value| !value. is_empty( ) ) ) ;
103- assert ! ( prc_log. as_ref( ) . is_none_or( |value| !value. is_empty( ) ) ) ;
104- assert ! ( certificate. as_ref( ) . is_none_or( |value| !value. is_empty( ) ) ) ;
123+ pub fn from ( quote_info : JsonValue , signature : Vec < u8 > , prc_log : Option < JsonValue > , certificate : Option < String > ) -> Self {
124+ assert ! ( not_empty!( quote_info) && not_empty!( signature) && opt_check!( prc_log) && opt_check!( certificate) , "A required value is missing!" ) ;
105125 Self { quote_info, signature, prc_log, certificate }
106126 }
107127}
@@ -113,17 +133,16 @@ impl QuoteResult {
113133/// Contains the public and/or private BLOBs of a TPM object.
114134///
115135/// Instances of this struct are returned by the [`FapiContext::get_tpm_blobs()`](crate::FapiContext::get_tpm_blobs) function.
136+ #[ non_exhaustive]
116137pub struct TpmBlobs {
117138 pub public_key : Option < Vec < u8 > > ,
118139 pub private_key : Option < Vec < u8 > > ,
119140 pub policy : Option < JsonValue > ,
120141}
121142
122143impl TpmBlobs {
123- pub ( crate ) fn from ( public_key : Option < Vec < u8 > > , private_key : Option < Vec < u8 > > , policy : Option < JsonValue > ) -> Self {
124- assert ! ( public_key. as_ref( ) . is_none_or( |value| !value. is_empty( ) ) ) ;
125- assert ! ( private_key. as_ref( ) . is_none_or( |value| !value. is_empty( ) ) ) ;
126- assert ! ( policy. as_ref( ) . is_none_or( |value| !value. is_empty( ) ) ) ;
144+ pub fn from ( public_key : Option < Vec < u8 > > , private_key : Option < Vec < u8 > > , policy : Option < JsonValue > ) -> Self {
145+ assert ! ( opt_check!( public_key) && opt_check!( private_key) && opt_check!( policy) , "A required value is missing!" ) ;
127146 Self { public_key, private_key, policy }
128147 }
129148}
0 commit comments