diff --git a/src/cred.rs b/src/cred.rs index 21518bf..010ad12 100644 --- a/src/cred.rs +++ b/src/cred.rs @@ -30,12 +30,16 @@ impl Credential { } impl CredentialRef { - pub fn aaguid(&self) -> &[u8] { + pub fn aaguid(&self) -> Option<&[u8]> { unsafe { - std::slice::from_raw_parts( - fido_cred_aaguid_ptr(self.as_ptr()), - fido_cred_aaguid_len(self.as_ptr()), - ) + if fido_cred_aaguid_ptr(self.as_ptr()).is_null() { + None + } else { + Some(std::slice::from_raw_parts( + fido_cred_aaguid_ptr(self.as_ptr()), + fido_cred_aaguid_len(self.as_ptr()), + )) + } } } @@ -48,12 +52,16 @@ impl CredentialRef { } } - pub fn authdata(&self) -> &[u8] { + pub fn authdata(&self) -> Option<&[u8]> { unsafe { - std::slice::from_raw_parts( - fido_cred_authdata_ptr(self.as_ptr()), - fido_cred_authdata_len(self.as_ptr()), - ) + if fido_cred_authdata_ptr(self.as_ptr()).is_null() { + None + } else { + Some(std::slice::from_raw_parts( + fido_cred_authdata_ptr(self.as_ptr()), + fido_cred_authdata_len(self.as_ptr()), + )) + } } } @@ -66,12 +74,16 @@ impl CredentialRef { } } - pub fn clientdata_hash(&self) -> &[u8] { + pub fn clientdata_hash(&self) -> Option<&[u8]> { unsafe { - std::slice::from_raw_parts( - fido_cred_clientdata_hash_ptr(self.as_ptr()), - fido_cred_clientdata_hash_len(self.as_ptr()), - ) + if fido_cred_clientdata_hash_ptr(self.as_ptr()).is_null() { + None + } else { + Some(std::slice::from_raw_parts( + fido_cred_clientdata_hash_ptr(self.as_ptr()), + fido_cred_clientdata_hash_len(self.as_ptr()), + )) + } } } @@ -105,21 +117,29 @@ impl CredentialRef { } } - pub fn cred_id(&self) -> &[u8] { + pub fn cred_id(&self) -> Option<&[u8]> { unsafe { - std::slice::from_raw_parts( - fido_cred_id_ptr(self.as_ptr()), - fido_cred_id_len(self.as_ptr()), - ) + if fido_cred_id_ptr(self.as_ptr()).is_null() { + None + } else { + Some(std::slice::from_raw_parts( + fido_cred_id_ptr(self.as_ptr()), + fido_cred_id_len(self.as_ptr()), + )) + } } } - pub fn largeblob_key(&self) -> &[u8] { + pub fn largeblob_key(&self) -> Option<&[u8]> { unsafe { - std::slice::from_raw_parts( - fido_cred_largeblob_key_ptr(self.as_ptr()), - fido_cred_largeblob_key_len(self.as_ptr()), - ) + if fido_cred_largeblob_key_ptr(self.as_ptr()).is_null() { + None + } else { + Some(std::slice::from_raw_parts( + fido_cred_largeblob_key_ptr(self.as_ptr()), + fido_cred_largeblob_key_len(self.as_ptr()), + )) + } } } @@ -145,12 +165,16 @@ impl CredentialRef { } } - pub fn pubkey(&self) -> &[u8] { + pub fn pubkey(&self) -> Option<&[u8]> { unsafe { - std::slice::from_raw_parts( - fido_cred_pubkey_ptr(self.as_ptr()), - fido_cred_pubkey_len(self.as_ptr()), - ) + if fido_cred_pubkey_ptr(self.as_ptr()).is_null() { + None + } else { + Some(std::slice::from_raw_parts( + fido_cred_pubkey_ptr(self.as_ptr()), + fido_cred_pubkey_len(self.as_ptr()), + )) + } } } @@ -321,12 +345,16 @@ impl CredentialRef { unsafe { check(fido_cred_set_x509(self.as_ptr_mut(), &x509[0], x509.len())) } } - pub fn sig(&self) -> &[u8] { + pub fn sig(&self) -> Option<&[u8]> { unsafe { - std::slice::from_raw_parts( - fido_cred_sig_ptr(self.as_ptr()), - fido_cred_sig_len(self.as_ptr()), - ) + if fido_cred_sig_ptr(self.as_ptr()).is_null() { + None + } else { + Some(std::slice::from_raw_parts( + fido_cred_sig_ptr(self.as_ptr()), + fido_cred_sig_len(self.as_ptr()), + )) + } } } @@ -373,12 +401,16 @@ impl CredentialRef { unsafe { check(fido_cred_verify_self(self.as_ptr())) } } - pub fn x5c(&self) -> &[u8] { + pub fn x5c(&self) -> Option<&[u8]> { unsafe { - std::slice::from_raw_parts( - fido_cred_x5c_ptr(self.as_ptr()), - fido_cred_x5c_len(self.as_ptr()), - ) + if fido_cred_x5c_ptr(self.as_ptr()).is_null() { + None + } else { + Some(std::slice::from_raw_parts( + fido_cred_x5c_ptr(self.as_ptr()), + fido_cred_x5c_len(self.as_ptr()), + )) + } } } }