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 |
| 16 | //! the different states an auth token and an associated verification token can be expressed during |
| 17 | //! the operation life cycle. |
| 18 | use crate::error::Error as KeystoreError; |
| 19 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
| 20 | HardwareAuthToken::HardwareAuthToken, VerificationToken::VerificationToken, |
| 21 | }; |
| 22 | use anyhow::{Context, Result}; |
| 23 | use std::sync::mpsc::Receiver; |
| 24 | |
| 25 | /// AuthTokenHandler enum has five different variants which are described by the comments above |
| 26 | // each variant. |
| 27 | pub enum AuthTokenHandler { |
| 28 | /// Used when an operation does not require an auth token for authorization. |
| 29 | NoAuthRequired, |
| 30 | /// Used to represent the intermediate state between the time the operation is found to be |
| 31 | /// requiring per-op auth and the time the auth token for the operation is found. |
| 32 | OpAuthRequired, |
| 33 | /// Used to represent the intermediate state between the time the operation is found to be |
| 34 | /// using a time_out key with STRONGBOX keymint, and the time a verficiation token is requested |
| 35 | /// from the worker thread which obtains verification tokens from the TEE KeyMint. |
| 36 | VerificationRequired(HardwareAuthToken), |
| 37 | /// Used to represent the intermediate state between the time a verification token is requested |
| 38 | /// from the worker thread which obtains verification tokens from the TEE KeyMint and the time |
| 39 | /// the verification token is received from the worker thread. |
| 40 | Channel(Receiver<(HardwareAuthToken, VerificationToken)>), |
| 41 | /// Used to represent the final state for all operations requiring an auth token for |
| 42 | /// authorization, after the matching auth token (and the associated verification token if |
| 43 | /// required) is found. |
| 44 | Token(HardwareAuthToken, Option<VerificationToken>), |
| 45 | } |
| 46 | |
| 47 | impl AuthTokenHandler { |
| 48 | /// Retrieve auth token and verification token from the Token variant of an AuthTokenHandler |
| 49 | /// instance |
| 50 | pub fn get_auth_and_verification_tokens( |
| 51 | &self, |
| 52 | ) -> Option<(&HardwareAuthToken, &VerificationToken)> { |
| 53 | if let AuthTokenHandler::Token(auth_token, Some(verification_token)) = self { |
| 54 | Some((auth_token, verification_token)) |
| 55 | } else { |
| 56 | None |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// Retrieve auth token from the Token variant of an AuthTokenHandler instance |
| 61 | pub fn get_auth_token(&self) -> Option<&HardwareAuthToken> { |
| 62 | if let AuthTokenHandler::Token(auth_token, _) = self { |
| 63 | Some(auth_token) |
| 64 | } else { |
| 65 | None |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// If Channel variant, block on it until the verification token is sent by the |
| 70 | /// keystore2 worker thread which obtains verification tokens from TEE Keymint |
| 71 | pub fn receive_verification_token(&mut self) -> Result<()> { |
| 72 | if let AuthTokenHandler::Channel(recv) = self { |
| 73 | let (auth_token, verification_token) = |
| 74 | recv.recv().context("In receive_verification_token: sender disconnected.")?; |
| 75 | *self = AuthTokenHandler::Token(auth_token, Some(verification_token)); |
| 76 | Ok(()) |
| 77 | } else { |
| 78 | Err(KeystoreError::sys()).context( |
| 79 | "In receive_verification_token: Wrong variant found in the authorization object.", |
| 80 | ) |
| 81 | } |
| 82 | } |
| 83 | } |