Hasini Gunasinghe | 9ec096c | 2020-11-17 00:45:44 +0000 | [diff] [blame] | 1 | // Copyright 2020, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! This module defines the AuthTokenHandler enum and its methods. AuthTokenHandler enum represents |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 16 | //! the different states an auth token and an associated timestamp token can be expressed during |
Hasini Gunasinghe | 9ec096c | 2020-11-17 00:45:44 +0000 | [diff] [blame] | 17 | //! the operation life cycle. |
Hasini Gunasinghe | 9ec096c | 2020-11-17 00:45:44 +0000 | [diff] [blame] | 18 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 19 | HardwareAuthToken::HardwareAuthToken, |
Hasini Gunasinghe | 9ec096c | 2020-11-17 00:45:44 +0000 | [diff] [blame] | 20 | }; |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 21 | use android_hardware_security_secureclock::aidl::android::hardware::security::secureclock::{ |
| 22 | TimeStampToken::TimeStampToken, |
| 23 | }; |
| 24 | |
Hasini Gunasinghe | 9ec096c | 2020-11-17 00:45:44 +0000 | [diff] [blame] | 25 | use anyhow::{Context, Result}; |
| 26 | use std::sync::mpsc::Receiver; |
| 27 | |
| 28 | /// AuthTokenHandler enum has five different variants which are described by the comments above |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 29 | // each variant, as follows. |
| 30 | #[derive(Debug)] |
Hasini Gunasinghe | 9ec096c | 2020-11-17 00:45:44 +0000 | [diff] [blame] | 31 | pub enum AuthTokenHandler { |
| 32 | /// Used when an operation does not require an auth token for authorization. |
| 33 | NoAuthRequired, |
| 34 | /// Used to represent the intermediate state between the time the operation is found to be |
| 35 | /// requiring per-op auth and the time the auth token for the operation is found. |
| 36 | OpAuthRequired, |
| 37 | /// Used to represent the intermediate state between the time the operation is found to be |
| 38 | /// using a time_out key with STRONGBOX keymint, and the time a verficiation token is requested |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 39 | /// from the worker thread which obtains timestamp tokens from the TEE KeyMint. |
| 40 | TimestampRequired(HardwareAuthToken), |
| 41 | /// Used to represent the intermediate state between the time a timestamp token is requested |
| 42 | /// from the worker thread which obtains timestamp tokens from the TEE KeyMint and the time |
| 43 | /// the timestamp token is received from the worker thread. |
| 44 | Channel(Receiver<(HardwareAuthToken, TimeStampToken)>), |
Hasini Gunasinghe | 9ec096c | 2020-11-17 00:45:44 +0000 | [diff] [blame] | 45 | /// Used to represent the final state for all operations requiring an auth token for |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 46 | /// authorization, after the matching auth token (and the associated timestamp token if |
Hasini Gunasinghe | 9ec096c | 2020-11-17 00:45:44 +0000 | [diff] [blame] | 47 | /// required) is found. |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 48 | Token(HardwareAuthToken, Option<TimeStampToken>), |
Hasini Gunasinghe | 9ec096c | 2020-11-17 00:45:44 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | impl AuthTokenHandler { |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 52 | /// If Channel variant, block on it until the timestamp token is sent by the |
| 53 | /// keystore2 worker thread which obtains timestamp tokens from TEE Keymint and converts the |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 54 | /// object from Channel variant to Token variant. |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 55 | /// Retrieve auth token and timestamp token from the Token variant of an AuthTokenHandler |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 56 | /// instance. |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 57 | pub fn retrieve_auth_and_timestamp_tokens( |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 58 | &mut self, |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 59 | ) -> Result<(Option<&HardwareAuthToken>, Option<&TimeStampToken>)> { |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 60 | // Converts to Token variant if Channel variant found, after retrieving the |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 61 | // TimeStampToken |
Hasini Gunasinghe | 9ec096c | 2020-11-17 00:45:44 +0000 | [diff] [blame] | 62 | if let AuthTokenHandler::Channel(recv) = self { |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 63 | let (auth_token, timestamp_token) = |
| 64 | recv.recv().context("In receive_timestamp_token: sender disconnected.")?; |
| 65 | *self = AuthTokenHandler::Token(auth_token, Some(timestamp_token)); |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 66 | } |
| 67 | // get the tokens from the Token variant |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 68 | if let AuthTokenHandler::Token(auth_token, optional_time_stamp_token) = self { |
| 69 | Ok((Some(auth_token), optional_time_stamp_token.as_ref())) |
Hasini Gunasinghe | 9ec096c | 2020-11-17 00:45:44 +0000 | [diff] [blame] | 70 | } else { |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 71 | Ok((None, None)) |
| 72 | } |
| 73 | } |
| 74 | |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 75 | /// Retrieve auth token from TimestampRequired and Token variants of an |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 76 | /// AuthTokenHandler instance. This method is useful when we only expect an auth token and |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 77 | /// do not expect a timestamp token. |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 78 | pub fn get_auth_token(&self) -> Option<&HardwareAuthToken> { |
| 79 | match self { |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 80 | AuthTokenHandler::TimestampRequired(auth_token) => Some(auth_token), |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 81 | AuthTokenHandler::Token(auth_token, _) => Some(auth_token), |
| 82 | _ => None, |
Hasini Gunasinghe | 9ec096c | 2020-11-17 00:45:44 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |