From 02eb7df2e166a1df1f024fa4c2392ac41d0f5db3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Thu, 16 Mar 2023 10:30:03 +0100 Subject: [PATCH] Add reset_pin_key This syscall allows resetting a pin. Unlike `set_pin`, it takes a key as parameter. This key will be returned by future calls to `get_pin_key`. Unlike `change_pin` this doesn't require knowledge of the current value of the PIN. The goal is to allow resetting a PIN from another source. For example, OpenPGP smartcards need to be able to reset the user pin given an admin pin With this patch, this can be done by using the admin key to wrap the user key. --- src/backend.rs | 18 ++++++++++++++ src/backend/data.rs | 33 +++++++++++++++++++++++++ src/extension.rs | 26 +++++++++++++++++++- src/extension/reply.rs | 20 ++++++++++++++++ src/extension/request.rs | 16 +++++++++++++ tests/backend.rs | 52 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 164 insertions(+), 1 deletion(-) diff --git a/src/backend.rs b/src/backend.rs index 219ce9a..02eb7b6 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -284,6 +284,24 @@ impl ExtensionImpl for AuthBackend { .save(fs, self.location)?; Ok(reply::SetPin.into()) } + AuthRequest::ResetPinKey(request) => { + let app_key = self.get_app_key(client_id, trussed_fs, ctx, rng)?; + let key_to_wrap = + keystore.load_key(Secrecy::Secret, Some(Kind::Symmetric(32)), &request.key)?; + let key_to_wrap = (&*key_to_wrap.material) + .try_into() + .map_err(|_| Error::ReadFailed)?; + PinData::reset_given_key( + request.id, + &request.pin, + request.retries, + rng, + &app_key, + key_to_wrap, + ) + .save(fs, self.location)?; + Ok(reply::ResetPinKey.into()) + } AuthRequest::DeletePin(request) => { let path = request.id.path(); if fs.exists(&path, self.location) { diff --git a/src/backend/data.rs b/src/backend/data.rs index 661c0df..4d2f53f 100644 --- a/src/backend/data.rs +++ b/src/backend/data.rs @@ -171,6 +171,39 @@ impl PinData { } } + pub fn reset_given_key( + id: PinId, + pin: &Pin, + retries: Option, + rng: &mut R, + application_key: &Key, + mut key_to_wrap: Key, + ) -> Self + where + R: CryptoRng + RngCore, + { + use chacha20poly1305::{AeadInPlace, KeyInit}; + let mut salt = Salt::default(); + rng.fill_bytes(salt.as_mut()); + let pin_key = derive_key(id, pin, &salt, application_key); + let aead = ChaCha8Poly1305::new((&*pin_key).into()); + let nonce = Default::default(); + #[allow(clippy::expect_used)] + let tag: [u8; CHACHA_TAG_LEN] = aead + .encrypt_in_place_detached(&nonce, &[u8::from(id)], &mut *key_to_wrap) + .expect("Wrapping the key should always work, length are acceptable") + .into(); + Self { + id, + retries: retries.map(From::from), + salt, + data: KeyOrHash::Key(WrappedKeyData { + wrapped_key: key_to_wrap, + tag: tag.into(), + }), + } + } + pub fn load(fs: &mut S, location: Location, id: PinId) -> Result { let path = id.path(); if !fs.exists(&path, location) { diff --git a/src/extension.rs b/src/extension.rs index 4f4dbb6..f77b645 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -7,7 +7,10 @@ pub mod reply; pub mod request; use serde::{Deserialize, Serialize}; -use trussed::serde_extensions::{Extension, ExtensionClient, ExtensionResult}; +use trussed::{ + serde_extensions::{Extension, ExtensionClient, ExtensionResult}, + types::KeyId, +}; use crate::{Pin, PinId}; @@ -32,6 +35,7 @@ pub enum AuthRequest { CheckPin(request::CheckPin), GetPinKey(request::GetPinKey), SetPin(request::SetPin), + ResetPinKey(request::ResetPinKey), ChangePin(request::ChangePin), DeletePin(request::DeletePin), DeleteAllPins(request::DeleteAllPins), @@ -45,6 +49,7 @@ pub enum AuthReply { CheckPin(reply::CheckPin), GetPinKey(reply::GetPinKey), SetPin(reply::SetPin), + ResetPinKey(reply::ResetPinKey), ChangePin(reply::ChangePin), DeletePin(reply::DeletePin), DeleteAllPins(reply::DeleteAllPins), @@ -114,6 +119,25 @@ pub trait AuthClient: ExtensionClient { }) } + /// Reset a pin. + /// + /// Similar to [`set_pin`](AuthClient::set_pin), but allows the key that the pin will unwrap to be configured. + /// This allows for example backing up the key for a pin, to be able to restore it from another source. + fn reset_set_pin_key>( + &mut self, + id: I, + pin: Pin, + retries: Option, + key: KeyId, + ) -> AuthResult<'_, reply::ResetPinKey, Self> { + self.extension(request::ResetPinKey { + id: id.into(), + pin, + retries, + key, + }) + } + /// Change the given PIN and resets its retry counter. /// /// The key obtained by [`get_pin_key`](AuthClient::get_pin_key) will stay the same diff --git a/src/extension/reply.rs b/src/extension/reply.rs index 0777ca1..e5e5d1b 100644 --- a/src/extension/reply.rs +++ b/src/extension/reply.rs @@ -99,6 +99,26 @@ impl TryFrom for SetPin { } } +#[derive(Debug, Deserialize, Serialize)] +pub struct ResetPinKey; + +impl From for AuthReply { + fn from(reply: ResetPinKey) -> Self { + Self::ResetPinKey(reply) + } +} + +impl TryFrom for ResetPinKey { + type Error = Error; + + fn try_from(reply: AuthReply) -> Result { + match reply { + AuthReply::ResetPinKey(reply) => Ok(reply), + _ => Err(Error::InternalError), + } + } +} + #[derive(Debug, Deserialize, Serialize)] pub struct ChangePin { pub success: bool, diff --git a/src/extension/request.rs b/src/extension/request.rs index 04d8117..d0d0da0 100644 --- a/src/extension/request.rs +++ b/src/extension/request.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 or MIT use serde::{Deserialize, Serialize}; +use trussed::types::KeyId; use super::AuthRequest; use crate::{Pin, PinId}; @@ -56,6 +57,21 @@ impl From for AuthRequest { } } +#[derive(Debug, Deserialize, Serialize)] +pub struct ResetPinKey { + pub id: PinId, + pub pin: Pin, + pub retries: Option, + /// If true, the PIN can be used to wrap/unwrap an application key + pub key: KeyId, +} + +impl From for AuthRequest { + fn from(request: ResetPinKey) -> Self { + Self::ResetPinKey(request) + } +} + #[derive(Debug, Deserialize, Serialize)] pub struct ChangePin { pub id: PinId, diff --git a/tests/backend.rs b/tests/backend.rs index 340e5c2..3be39aa 100644 --- a/tests/backend.rs +++ b/tests/backend.rs @@ -431,6 +431,58 @@ fn pin_key() { ) } +#[test] +fn reset_pin_key() { + run_with_hw_key( + BACKENDS, + Bytes::from_slice(b"Some HW ikm").unwrap(), + |client| { + let pin1 = Bytes::from_slice(b"12345678").unwrap(); + let pin2 = Bytes::from_slice(b"123456").unwrap(); + let pin3 = Bytes::from_slice(b"1234567890").unwrap(); + + syscall!(client.set_pin(Pin::User, pin1.clone(), Some(3), true)); + assert!(syscall!(client.get_pin_key(Pin::User, pin2.clone())) + .result + .is_none()); + assert_eq!(syscall!(client.pin_retries(Pin::User)).retries, Some(2)); + assert!(!syscall!(client.check_pin(Pin::User, pin2.clone())).success); + assert_eq!(syscall!(client.pin_retries(Pin::User)).retries, Some(1)); + assert!(syscall!(client.check_pin(Pin::User, pin1.clone())).success); + let key = syscall!(client.get_pin_key(Pin::User, pin1)) + .result + .unwrap(); + assert_eq!(syscall!(client.pin_retries(Pin::User)).retries, Some(3)); + let mac = syscall!(client.sign_hmacsha256(key, b"Some data")).signature; + + syscall!(client.reset_set_pin_key(Pin::User, pin3.clone(), Some(3), key)); + + let key2 = syscall!(client.get_pin_key(Pin::User, pin3.clone())) + .result + .unwrap(); + let mac2 = syscall!(client.sign_hmacsha256(key2, b"Some data")).signature; + assert_eq!(mac, mac2); + + assert!(syscall!(client.change_pin(Pin::User, pin3.clone(), pin2.clone())).success); + + let key3 = syscall!(client.get_pin_key(Pin::User, pin2.clone())) + .result + .unwrap(); + let mac3 = syscall!(client.sign_hmacsha256(key3, b"Some data")).signature; + assert_eq!(mac, mac3); + + assert!(!syscall!(client.check_pin(Pin::User, pin3.clone())).success); + assert!(!syscall!(client.check_pin(Pin::User, pin3.clone())).success); + assert!(!syscall!(client.check_pin(Pin::User, pin3)).success); + assert!(!syscall!(client.check_pin(Pin::User, pin2.clone())).success); + assert!(syscall!(client.get_pin_key(Pin::User, pin2)) + .result + .is_none()); + assert_eq!(syscall!(client.pin_retries(Pin::User)).retries, Some(0)); + }, + ) +} + #[test] fn blocked_pin() { run(BACKENDS, |client| {