Debian Codegraph @

third_party/rust/authenticator/src/ctap2/commands/authenticator_config.rs

raw
1 use super::{Command, CommandError, PinUvAuthCommand, RequestCtap2, StatusCode};
2 use crate::{
3 crypto::{PinUvAuthParam, PinUvAuthToken},
4 ctap2::server::UserVerificationRequirement,
5 errors::AuthenticatorError,
6 transport::errors::HIDError,
7 AuthenticatorInfo, FidoDevice,
8 };
9 use serde::{Deserialize, Serialize, Serializer};
10 use serde_cbor::{de::from_slice, to_vec, Value};
11
12 #[derive(Debug, Clone, Deserialize)]
13 pub struct SetMinPINLength {
14 /// Minimum PIN length in code points
15 pub new_min_pin_length: Option<u64>,
16 /// RP IDs which are allowed to get this information via the minPinLength extension.
17 /// This parameter MUST NOT be used unless the minPinLength extension is supported.
18 pub min_pin_length_rpids: Option<Vec<String>>,
19 /// The authenticator returns CTAP2_ERR_PIN_POLICY_VIOLATION until changePIN is successful.
20 pub force_change_pin: Option<bool>,
21 }
22
23 impl Serialize for SetMinPINLength {
24 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
25 where
26 S: Serializer,
27 {
28 serialize_map_optional!(
29 serializer,
30 &0x01 => self.new_min_pin_length,
31 &0x02 => &self.min_pin_length_rpids,
32 &0x03 => self.force_change_pin,
33 )
34 }
35 }
36
37 #[derive(Debug, Clone, Deserialize, Serialize)]
38 pub enum AuthConfigCommand {
39 EnableEnterpriseAttestation,
40 ToggleAlwaysUv,
41 SetMinPINLength(SetMinPINLength),
42 }
43
44 impl AuthConfigCommand {
45 fn has_params(&self) -> bool {
46 match self {
47 AuthConfigCommand::EnableEnterpriseAttestation => false,
48 AuthConfigCommand::ToggleAlwaysUv => false,
49 AuthConfigCommand::SetMinPINLength(..) => true,
50 }
51 }
52 }
53
54 #[derive(Debug, Serialize)]
55 pub enum AuthConfigResult {
56 Success(AuthenticatorInfo),
57 }
58
59 #[derive(Debug)]
60 pub struct AuthenticatorConfig {
61 subcommand: AuthConfigCommand, // subCommand currently being requested
62 pin_uv_auth_param: Option<PinUvAuthParam>, // First 16 bytes of HMAC-SHA-256 of contents using pinUvAuthToken.
63 }
64
65 impl AuthenticatorConfig {
66 pub(crate) fn new(subcommand: AuthConfigCommand) -> Self {
67 Self {
68 subcommand,
69 pin_uv_auth_param: None,
70 }
71 }
72 }
73
74 impl Serialize for AuthenticatorConfig {
75 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
76 where
77 S: Serializer,
78 {
79 let (entry01, entry02) = match &self.subcommand {
80 AuthConfigCommand::EnableEnterpriseAttestation => (&0x01, None),
81 AuthConfigCommand::ToggleAlwaysUv => (&0x02, None),
82 AuthConfigCommand::SetMinPINLength(params) => (&0x03, Some(params)),
83 };
84
85 serialize_map_optional!(
86 serializer,
87 &0x01 => Some(entry01),
88 &0x02 => entry02,
89 &0x03 => self.pin_uv_auth_param.as_ref().map(|p| p.pin_protocol.id()),
90 &0x04 => &self.pin_uv_auth_param,
91 )
92 }
93 }
94
95 impl RequestCtap2 for AuthenticatorConfig {
96 type Output = ();
97
98 fn command(&self) -> Command {
99 Command::AuthenticatorConfig
100 }
101
102 fn wire_format(&self) -> Result<Vec<u8>, HIDError> {
103 let output = to_vec(&self).map_err(CommandError::Serializing)?;
104 trace!("client subcommmand: {:04X?}", &output);
105 Ok(output)
106 }
107
108 fn handle_response_ctap2<Dev>(
109 &self,
110 _dev: &mut Dev,
111 input: &[u8],
112 ) -> Result<Self::Output, HIDError>
113 where
114 Dev: FidoDevice,
115 {
116 if input.is_empty() {
117 return Err(CommandError::InputTooSmall.into());
118 }
119
120 let status: StatusCode = input[0].into();
121
122 if status.is_ok() {
123 Ok(())
124 } else {
125 let msg = if input.len() > 1 {
126 let data: Value = from_slice(&input[1..]).map_err(CommandError::Deserializing)?;
127 Some(data)
128 } else {
129 None
130 };
131 Err(CommandError::StatusCode(status, msg).into())
132 }
133 }
134
135 fn send_to_virtual_device<Dev: crate::VirtualFidoDevice>(
136 &self,
137 _dev: &mut Dev,
138 ) -> Result<Self::Output, HIDError> {
139 unimplemented!()
140 }
141 }
142
143 impl PinUvAuthCommand for AuthenticatorConfig {
144 fn set_pin_uv_auth_param(
145 &mut self,
146 pin_uv_auth_token: Option<PinUvAuthToken>,
147 ) -> Result<(), AuthenticatorError> {
148 let mut param = None;
149 if let Some(token) = pin_uv_auth_token {
150 // pinUvAuthParam (0x04): the result of calling
151 // authenticate(pinUvAuthToken, 32×0xff || 0x0d || uint8(subCommand) || subCommandParams).
152 let mut data = vec![0xff; 32];
153 data.push(0x0D);
154 match &self.subcommand {
155 AuthConfigCommand::EnableEnterpriseAttestation => {
156 data.push(0x01);
157 }
158 AuthConfigCommand::ToggleAlwaysUv => {
159 data.push(0x02);
160 }
161 AuthConfigCommand::SetMinPINLength(params) => {
162 data.push(0x03);
163 data.extend(to_vec(params).map_err(CommandError::Serializing)?);
164 }
165 }
166 param = Some(token.derive(&data).map_err(CommandError::Crypto)?);
167 }
168 self.pin_uv_auth_param = param;
169 Ok(())
170 }
171
172 fn can_skip_user_verification(
173 &mut self,
174 authinfo: &AuthenticatorInfo,
175 _uv_req: UserVerificationRequirement,
176 ) -> bool {
177 !authinfo.device_is_protected()
178 }
179
180 fn set_uv_option(&mut self, _uv: Option<bool>) {
181 /* No-op */
182 }
183
184 fn get_pin_uv_auth_param(&self) -> Option<&PinUvAuthParam> {
185 self.pin_uv_auth_param.as_ref()
186 }
187
188 fn get_rp_id(&self) -> Option<&String> {
189 None
190 }
191 }
192
193 #[cfg(test)]
194 mod test {
195 use crate::{crypto::PinUvAuthParam, ctap2::commands::client_pin::PinUvAuthTokenPermission};
196
197 use super::{AuthConfigCommand, AuthenticatorConfig, SetMinPINLength};
198
199 #[test]
200 fn test_serialize_set_min_pin_length() {
201 let set_min_pin_length = SetMinPINLength {
202 new_min_pin_length: Some(42),
203 min_pin_length_rpids: Some(vec![
204 "example.org".to_string(),
205 "www.example.org".to_string(),
206 ]),
207 force_change_pin: Some(true),
208 };
209 let serialized =
210 serde_cbor::ser::to_vec(&set_min_pin_length).expect("Failed to serialize to CBOR");
211 assert_eq!(
212 serialized,
213 [
214 // Value copied from test failure output as regression test snapshot
215 163, 1, 24, 42, 2, 130, 107, 101, 120, 97, 109, 112, 108, 101, 46, 111, 114, 103,
216 111, 119, 119, 119, 46, 101, 120, 97, 109, 112, 108, 101, 46, 111, 114, 103, 3,
217 245
218 ]
219 );
220 }
221
222 #[test]
223 fn test_serialize_authenticator_config() {
224 let pin_uv_auth_param = Some(PinUvAuthParam::create_test(
225 2,
226 vec![1, 2, 3, 4],
227 PinUvAuthTokenPermission::CredentialManagement,
228 ));
229
230 {
231 let authenticator_config = AuthenticatorConfig {
232 subcommand: AuthConfigCommand::EnableEnterpriseAttestation,
233 pin_uv_auth_param: pin_uv_auth_param.clone(),
234 };
235 let serialized = serde_cbor::ser::to_vec(&authenticator_config)
236 .expect("Failed to serialize to CBOR");
237 assert_eq!(
238 serialized,
239 [
240 // Value copied from test failure output as regression test snapshot
241 163, 1, 1, 3, 2, 4, 68, 1, 2, 3, 4
242 ]
243 );
244 }
245
246 {
247 let authenticator_config = AuthenticatorConfig {
248 subcommand: AuthConfigCommand::ToggleAlwaysUv,
249 pin_uv_auth_param: pin_uv_auth_param.clone(),
250 };
251 let serialized = serde_cbor::ser::to_vec(&authenticator_config)
252 .expect("Failed to serialize to CBOR");
253 assert_eq!(
254 serialized,
255 [
256 // Value copied from test failure output as regression test snapshot
257 163, 1, 2, 3, 2, 4, 68, 1, 2, 3, 4
258 ]
259 );
260 }
261
262 {
263 let authenticator_config = AuthenticatorConfig {
264 subcommand: AuthConfigCommand::SetMinPINLength(SetMinPINLength {
265 new_min_pin_length: Some(42),
266 min_pin_length_rpids: Some(vec![
267 "example.org".to_string(),
268 "www.example.org".to_string(),
269 ]),
270 force_change_pin: Some(true),
271 }),
272 pin_uv_auth_param,
273 };
274 let serialized = serde_cbor::ser::to_vec(&authenticator_config)
275 .expect("Failed to serialize to CBOR");
276 assert_eq!(
277 serialized,
278 [
279 // Value copied from test failure output as regression test snapshot
280 164, 1, 3, 2, 163, 1, 24, 42, 2, 130, 107, 101, 120, 97, 109, 112, 108, 101, 46,
281 111, 114, 103, 111, 119, 119, 119, 46, 101, 120, 97, 109, 112, 108, 101, 46,
282 111, 114, 103, 3, 245, 3, 2, 4, 68, 1, 2, 3, 4
283 ]
284 );
285 }
286 }
287 }