blob: bedec50242ffdb9a4cd95701f6b1227d826b2d5d [file] [log] [blame]
Hasini Gunasinghe9ec096c2020-11-17 00:45:44 +00001// 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 Danisevskisc3a496b2021-01-05 10:37:22 -080016//! the different states an auth token and an associated timestamp token can be expressed during
Hasini Gunasinghe9ec096c2020-11-17 00:45:44 +000017//! the operation life cycle.
Hasini Gunasinghe9ec096c2020-11-17 00:45:44 +000018use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080019 HardwareAuthToken::HardwareAuthToken,
Hasini Gunasinghe9ec096c2020-11-17 00:45:44 +000020};
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080021use android_hardware_security_secureclock::aidl::android::hardware::security::secureclock::{
22 TimeStampToken::TimeStampToken,
23};
24
Hasini Gunasinghe9ec096c2020-11-17 00:45:44 +000025use anyhow::{Context, Result};
26use std::sync::mpsc::Receiver;
27
28/// AuthTokenHandler enum has five different variants which are described by the comments above
Hasini Gunasinghe888dd352020-11-17 23:08:39 +000029// each variant, as follows.
30#[derive(Debug)]
Hasini Gunasinghe9ec096c2020-11-17 00:45:44 +000031pub 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 Danisevskisc3a496b2021-01-05 10:37:22 -080039 /// 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 Gunasinghe9ec096c2020-11-17 00:45:44 +000045 /// Used to represent the final state for all operations requiring an auth token for
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080046 /// authorization, after the matching auth token (and the associated timestamp token if
Hasini Gunasinghe9ec096c2020-11-17 00:45:44 +000047 /// required) is found.
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080048 Token(HardwareAuthToken, Option<TimeStampToken>),
Hasini Gunasinghe9ec096c2020-11-17 00:45:44 +000049}
50
51impl AuthTokenHandler {
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080052 /// 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 Gunasinghe888dd352020-11-17 23:08:39 +000054 /// object from Channel variant to Token variant.
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080055 /// Retrieve auth token and timestamp token from the Token variant of an AuthTokenHandler
Hasini Gunasinghe888dd352020-11-17 23:08:39 +000056 /// instance.
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080057 pub fn retrieve_auth_and_timestamp_tokens(
Hasini Gunasinghe888dd352020-11-17 23:08:39 +000058 &mut self,
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080059 ) -> Result<(Option<&HardwareAuthToken>, Option<&TimeStampToken>)> {
Hasini Gunasinghe888dd352020-11-17 23:08:39 +000060 // Converts to Token variant if Channel variant found, after retrieving the
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080061 // TimeStampToken
Hasini Gunasinghe9ec096c2020-11-17 00:45:44 +000062 if let AuthTokenHandler::Channel(recv) = self {
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080063 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 Gunasinghe888dd352020-11-17 23:08:39 +000066 }
67 // get the tokens from the Token variant
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080068 if let AuthTokenHandler::Token(auth_token, optional_time_stamp_token) = self {
69 Ok((Some(auth_token), optional_time_stamp_token.as_ref()))
Hasini Gunasinghe9ec096c2020-11-17 00:45:44 +000070 } else {
Hasini Gunasinghe888dd352020-11-17 23:08:39 +000071 Ok((None, None))
72 }
73 }
74
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080075 /// Retrieve auth token from TimestampRequired and Token variants of an
Hasini Gunasinghe888dd352020-11-17 23:08:39 +000076 /// AuthTokenHandler instance. This method is useful when we only expect an auth token and
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080077 /// do not expect a timestamp token.
Hasini Gunasinghe888dd352020-11-17 23:08:39 +000078 pub fn get_auth_token(&self) -> Option<&HardwareAuthToken> {
79 match self {
Janis Danisevskisc3a496b2021-01-05 10:37:22 -080080 AuthTokenHandler::TimestampRequired(auth_token) => Some(auth_token),
Hasini Gunasinghe888dd352020-11-17 23:08:39 +000081 AuthTokenHandler::Token(auth_token, _) => Some(auth_token),
82 _ => None,
Hasini Gunasinghe9ec096c2020-11-17 00:45:44 +000083 }
84 }
85}