ser_server_state.rs (10125B)
1 use super::{ 2 super::bin::{DecodeBuffer, EncDecErr, Encode, EncodeBuffer, EncodeBufferFallible}, 3 CredentialMediationRequirement, ExtensionInfo, ExtensionReq, Hint, SentChallenge, 4 UserVerificationRequirement, 5 }; 6 use core::{convert::Infallible, time::Duration}; 7 use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH}; 8 /// [`ExtensionInfo::RequireEnforceValue`] tag. 9 const EXT_INFO_REQUIRE_ENFORCE: u8 = 0; 10 /// [`ExtensionInfo::RequireDontEnforceValue`] tag. 11 const EXT_INFO_REQUIRE_DONT_ENFORCE: u8 = 1; 12 /// [`ExtensionInfo::AllowEnforceValue`] tag. 13 const EXT_INFO_ALLOW_ENFORCE: u8 = 2; 14 /// [`ExtensionInfo::AllowDontEnforceValue`] tag. 15 const EXT_INFO_ALLOW_DONT_ENFORCE: u8 = 3; 16 impl EncodeBuffer for ExtensionInfo { 17 fn encode_into_buffer(&self, buffer: &mut Vec<u8>) { 18 match *self { 19 Self::RequireEnforceValue => EXT_INFO_REQUIRE_ENFORCE, 20 Self::RequireDontEnforceValue => EXT_INFO_REQUIRE_DONT_ENFORCE, 21 Self::AllowEnforceValue => EXT_INFO_ALLOW_ENFORCE, 22 Self::AllowDontEnforceValue => EXT_INFO_ALLOW_DONT_ENFORCE, 23 } 24 .encode_into_buffer(buffer); 25 } 26 } 27 impl<'a> DecodeBuffer<'a> for ExtensionInfo { 28 type Err = EncDecErr; 29 fn decode_from_buffer(data: &mut &'a [u8]) -> Result<Self, Self::Err> { 30 u8::decode_from_buffer(data).and_then(|val| match val { 31 EXT_INFO_REQUIRE_ENFORCE => Ok(Self::RequireEnforceValue), 32 EXT_INFO_REQUIRE_DONT_ENFORCE => Ok(Self::RequireDontEnforceValue), 33 EXT_INFO_ALLOW_ENFORCE => Ok(Self::AllowEnforceValue), 34 EXT_INFO_ALLOW_DONT_ENFORCE => Ok(Self::AllowDontEnforceValue), 35 _ => Err(EncDecErr), 36 }) 37 } 38 } 39 /// [`ExtensionReq::Require`] tag. 40 const EXT_REQ_REQUIRE: u8 = 0; 41 /// [`ExtensionReq::Allow`] tag. 42 const EXT_REQ_ALLOW: u8 = 1; 43 impl EncodeBuffer for ExtensionReq { 44 fn encode_into_buffer(&self, buffer: &mut Vec<u8>) { 45 match *self { 46 Self::Require => EXT_REQ_REQUIRE, 47 Self::Allow => EXT_REQ_ALLOW, 48 } 49 .encode_into_buffer(buffer); 50 } 51 } 52 impl<'a> DecodeBuffer<'a> for ExtensionReq { 53 type Err = EncDecErr; 54 fn decode_from_buffer(data: &mut &'a [u8]) -> Result<Self, Self::Err> { 55 u8::decode_from_buffer(data).and_then(|val| match val { 56 EXT_REQ_REQUIRE => Ok(Self::Require), 57 EXT_REQ_ALLOW => Ok(Self::Allow), 58 _ => Err(EncDecErr), 59 }) 60 } 61 } 62 /// [`Hint::None`] tag. 63 const HINT_NONE: u8 = 0; 64 /// [`Hint::SecurityKey`] tag. 65 const HINT_SEC_KEY: u8 = 1; 66 /// [`Hint::ClientDevice`] tag. 67 const HINT_CLIENT_DEV: u8 = 2; 68 /// [`Hint::Hybrid`] tag. 69 const HINT_HYBRID: u8 = 3; 70 /// [`Hint::SecurityKeyClientDevice`] tag. 71 const HINT_SEC_KEY_CLIENT_DEV: u8 = 4; 72 /// [`Hint::ClientDeviceSecurityKey`] tag. 73 const HINT_CLIENT_DEV_SEC_KEY: u8 = 5; 74 /// [`Hint::SecurityKeyHybrid`] tag. 75 const HINT_SEC_KEY_HYBRID: u8 = 6; 76 /// [`Hint::HybridSecurityKey`] tag. 77 const HINT_HYBRID_SEC_KEY: u8 = 7; 78 /// [`Hint::ClientDeviceHybrid`] tag. 79 const HINT_CLIENT_DEV_HYBRID: u8 = 8; 80 /// [`Hint::HybridClientDevice`] tag. 81 const HINT_HYBRID_CLIENT_DEV: u8 = 9; 82 /// [`Hint::SecurityKeyClientDeviceHybrid`] tag. 83 const HINT_SEC_KEY_CLIENT_DEV_HYBRID: u8 = 10; 84 /// [`Hint::SecurityKeyHybridClientDevice`] tag. 85 const HINT_SEC_KEY_HYBRID_CLIENT_DEV: u8 = 11; 86 /// [`Hint::ClientDeviceSecurityKeyHybrid`] tag. 87 const HINT_CLIENT_DEV_SEC_KEY_HYBRID: u8 = 12; 88 /// [`Hint::ClientDeviceHybridSecurityKey`] tag. 89 const HINT_CLIENT_DEV_HYBRID_SEC_KEY: u8 = 13; 90 /// [`Hint::HybridSecurityKeyClientDevice`] tag. 91 const HINT_HYBRID_SEC_KEY_CLIENT_DEV: u8 = 14; 92 /// [`Hint::HybridClientDeviceSecurityKey`] tag. 93 const HINT_HYBRID_CLIENT_DEV_SEC_KEY: u8 = 15; 94 impl EncodeBuffer for Hint { 95 fn encode_into_buffer(&self, buffer: &mut Vec<u8>) { 96 match *self { 97 Self::None => HINT_NONE, 98 Self::SecurityKey => HINT_SEC_KEY, 99 Self::ClientDevice => HINT_CLIENT_DEV, 100 Self::Hybrid => HINT_HYBRID, 101 Self::SecurityKeyClientDevice => HINT_SEC_KEY_CLIENT_DEV, 102 Self::ClientDeviceSecurityKey => HINT_CLIENT_DEV_SEC_KEY, 103 Self::SecurityKeyHybrid => HINT_SEC_KEY_HYBRID, 104 Self::HybridSecurityKey => HINT_HYBRID_SEC_KEY, 105 Self::ClientDeviceHybrid => HINT_CLIENT_DEV_HYBRID, 106 Self::HybridClientDevice => HINT_HYBRID_CLIENT_DEV, 107 Self::SecurityKeyClientDeviceHybrid => HINT_SEC_KEY_CLIENT_DEV_HYBRID, 108 Self::SecurityKeyHybridClientDevice => HINT_SEC_KEY_HYBRID_CLIENT_DEV, 109 Self::ClientDeviceSecurityKeyHybrid => HINT_CLIENT_DEV_SEC_KEY_HYBRID, 110 Self::ClientDeviceHybridSecurityKey => HINT_CLIENT_DEV_HYBRID_SEC_KEY, 111 Self::HybridSecurityKeyClientDevice => HINT_HYBRID_SEC_KEY_CLIENT_DEV, 112 Self::HybridClientDeviceSecurityKey => HINT_HYBRID_CLIENT_DEV_SEC_KEY, 113 } 114 .encode_into_buffer(buffer); 115 } 116 } 117 impl<'a> DecodeBuffer<'a> for Hint { 118 type Err = EncDecErr; 119 fn decode_from_buffer(data: &mut &'a [u8]) -> Result<Self, Self::Err> { 120 u8::decode_from_buffer(data).and_then(|val| match val { 121 HINT_NONE => Ok(Self::None), 122 HINT_SEC_KEY => Ok(Self::SecurityKey), 123 HINT_CLIENT_DEV => Ok(Self::ClientDevice), 124 HINT_HYBRID => Ok(Self::Hybrid), 125 HINT_SEC_KEY_CLIENT_DEV => Ok(Self::SecurityKeyClientDevice), 126 HINT_CLIENT_DEV_SEC_KEY => Ok(Self::ClientDeviceSecurityKey), 127 HINT_SEC_KEY_HYBRID => Ok(Self::SecurityKeyHybrid), 128 HINT_HYBRID_SEC_KEY => Ok(Self::HybridSecurityKey), 129 HINT_CLIENT_DEV_HYBRID => Ok(Self::ClientDeviceHybrid), 130 HINT_HYBRID_CLIENT_DEV => Ok(Self::HybridClientDevice), 131 HINT_SEC_KEY_CLIENT_DEV_HYBRID => Ok(Self::SecurityKeyClientDeviceHybrid), 132 HINT_SEC_KEY_HYBRID_CLIENT_DEV => Ok(Self::SecurityKeyHybridClientDevice), 133 HINT_CLIENT_DEV_SEC_KEY_HYBRID => Ok(Self::ClientDeviceSecurityKeyHybrid), 134 HINT_CLIENT_DEV_HYBRID_SEC_KEY => Ok(Self::ClientDeviceHybridSecurityKey), 135 HINT_HYBRID_SEC_KEY_CLIENT_DEV => Ok(Self::HybridSecurityKeyClientDevice), 136 HINT_HYBRID_CLIENT_DEV_SEC_KEY => Ok(Self::HybridClientDeviceSecurityKey), 137 _ => Err(EncDecErr), 138 }) 139 } 140 } 141 impl EncodeBuffer for SentChallenge { 142 fn encode_into_buffer(&self, buffer: &mut Vec<u8>) { 143 self.0.encode_into_buffer(buffer); 144 } 145 } 146 impl<'a> DecodeBuffer<'a> for SentChallenge { 147 type Err = EncDecErr; 148 fn decode_from_buffer(data: &mut &'a [u8]) -> Result<Self, Self::Err> { 149 u128::decode_from_buffer(data).map(Self) 150 } 151 } 152 impl Encode for SentChallenge { 153 type Output<'a> = u128 where Self: 'a; 154 type Err = Infallible; 155 #[inline] 156 fn encode(&self) -> Result<Self::Output<'_>, Self::Err> { 157 Ok(self.0) 158 } 159 } 160 /// [`UserVerificationRequirement::Required`] tag. 161 const USER_VER_REQ_REQUIRED: u8 = 0; 162 /// [`UserVerificationRequirement::Discouraged`] tag. 163 const USER_VER_REQ_DISCOURAGED: u8 = 1; 164 /// [`UserVerificationRequirement::Preferred`] tag. 165 const USER_VER_REQ_PREFERRED: u8 = 2; 166 impl EncodeBuffer for UserVerificationRequirement { 167 fn encode_into_buffer(&self, buffer: &mut Vec<u8>) { 168 match *self { 169 Self::Required => USER_VER_REQ_REQUIRED, 170 Self::Discouraged => USER_VER_REQ_DISCOURAGED, 171 Self::Preferred => USER_VER_REQ_PREFERRED, 172 } 173 .encode_into_buffer(buffer); 174 } 175 } 176 impl<'a> DecodeBuffer<'a> for UserVerificationRequirement { 177 type Err = EncDecErr; 178 fn decode_from_buffer(data: &mut &'a [u8]) -> Result<Self, Self::Err> { 179 u8::decode_from_buffer(data).and_then(|val| match val { 180 USER_VER_REQ_REQUIRED => Ok(Self::Required), 181 USER_VER_REQ_DISCOURAGED => Ok(Self::Discouraged), 182 USER_VER_REQ_PREFERRED => Ok(Self::Preferred), 183 _ => Err(EncDecErr), 184 }) 185 } 186 } 187 /// [`CredentialMediationRequirement::Silent`] tag. 188 const CRED_MED_REQ_SILENT: u8 = 0; 189 /// [`CredentialMediationRequirement::Optional`] tag. 190 const CRED_MED_REQ_OPTIONAL: u8 = 1; 191 /// [`CredentialMediationRequirement::Conditional`] tag. 192 const CRED_MED_REQ_CONDITIONAL: u8 = 2; 193 /// [`CredentialMediationRequirement::Required`] tag. 194 const CRED_MED_REQ_REQUIRED: u8 = 3; 195 impl EncodeBuffer for CredentialMediationRequirement { 196 fn encode_into_buffer(&self, buffer: &mut Vec<u8>) { 197 match *self { 198 Self::Silent => CRED_MED_REQ_SILENT, 199 Self::Optional => CRED_MED_REQ_OPTIONAL, 200 Self::Conditional => CRED_MED_REQ_CONDITIONAL, 201 Self::Required => CRED_MED_REQ_REQUIRED, 202 } 203 .encode_into_buffer(buffer); 204 } 205 } 206 impl<'a> DecodeBuffer<'a> for CredentialMediationRequirement { 207 type Err = EncDecErr; 208 fn decode_from_buffer(data: &mut &'a [u8]) -> Result<Self, Self::Err> { 209 u8::decode_from_buffer(data).and_then(|val| match val { 210 CRED_MED_REQ_SILENT => Ok(Self::Silent), 211 CRED_MED_REQ_OPTIONAL => Ok(Self::Optional), 212 CRED_MED_REQ_CONDITIONAL => Ok(Self::Conditional), 213 CRED_MED_REQ_REQUIRED => Ok(Self::Required), 214 _ => Err(EncDecErr), 215 }) 216 } 217 } 218 impl EncodeBufferFallible for SystemTime { 219 type Err = SystemTimeError; 220 fn encode_into_buffer(&self, buffer: &mut Vec<u8>) -> Result<(), Self::Err> { 221 self.duration_since(UNIX_EPOCH).map(|dur| { 222 dur.as_secs().encode_into_buffer(buffer); 223 dur.subsec_nanos().encode_into_buffer(buffer); 224 }) 225 } 226 } 227 impl<'a> DecodeBuffer<'a> for SystemTime { 228 type Err = EncDecErr; 229 fn decode_from_buffer(data: &mut &'a [u8]) -> Result<Self, Self::Err> { 230 u64::decode_from_buffer(data).and_then(|secs| { 231 u32::decode_from_buffer(data).and_then(|nanos| { 232 if nanos < 1_000_000_000 { 233 UNIX_EPOCH 234 .checked_add(Duration::new(secs, nanos)) 235 .ok_or(EncDecErr) 236 } else { 237 Err(EncDecErr) 238 } 239 }) 240 }) 241 } 242 }