blob: 0cc2e92acb0a85dcc67812e2c1a42b46571a7c65 [file] [log] [blame]
Hasini Gunasingheda895552021-01-27 19:34:37 +00001// Copyright 2021, 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
Janis Danisevskis34a0cf22021-03-08 09:19:03 -080015//! This module implements IKeystoreMaintenance AIDL interface.
Hasini Gunasingheda895552021-01-27 19:34:37 +000016
17use crate::error::map_or_log_err;
18use crate::error::Error as KeystoreError;
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +000019use crate::globals::{DB, LEGACY_MIGRATOR, SUPER_KEY};
Hasini Gunasingheda895552021-01-27 19:34:37 +000020use crate::permission::KeystorePerm;
21use crate::super_key::UserState;
22use crate::utils::check_keystore_permission;
Hasini Gunasinghe9ee18412021-03-11 20:12:44 +000023use android_security_maintenance::aidl::android::security::maintenance::{
24 IKeystoreMaintenance::{BnKeystoreMaintenance, IKeystoreMaintenance},
25 UserState::UserState as AidlUserState,
Hasini Gunasingheda895552021-01-27 19:34:37 +000026};
Janis Danisevskis34a0cf22021-03-08 09:19:03 -080027use android_security_maintenance::binder::{Interface, Result as BinderResult};
Janis Danisevskisddd6e752021-02-22 18:46:55 -080028use android_system_keystore2::aidl::android::system::keystore2::Domain::Domain;
Hasini Gunasingheda895552021-01-27 19:34:37 +000029use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode;
30use anyhow::{Context, Result};
Andrew Walbran808e8602021-03-16 13:58:28 +000031use binder::{IBinderInternal, Strong};
Paul Crowleyf61fee72021-03-17 14:38:44 -070032use keystore2_crypto::Password;
Hasini Gunasingheda895552021-01-27 19:34:37 +000033
34/// This struct is defined to implement the aforementioned AIDL interface.
35/// As of now, it is an empty struct.
Janis Danisevskis34a0cf22021-03-08 09:19:03 -080036pub struct Maintenance;
Hasini Gunasingheda895552021-01-27 19:34:37 +000037
Janis Danisevskis34a0cf22021-03-08 09:19:03 -080038impl Maintenance {
Hasini Gunasingheda895552021-01-27 19:34:37 +000039 /// Create a new instance of Keystore User Manager service.
Janis Danisevskis34a0cf22021-03-08 09:19:03 -080040 pub fn new_native_binder() -> Result<Strong<dyn IKeystoreMaintenance>> {
41 let result = BnKeystoreMaintenance::new_binder(Self);
Hasini Gunasingheda895552021-01-27 19:34:37 +000042 result.as_binder().set_requesting_sid(true);
43 Ok(result)
44 }
45
Paul Crowleyf61fee72021-03-17 14:38:44 -070046 fn on_user_password_changed(user_id: i32, password: Option<Password>) -> Result<()> {
Hasini Gunasingheda895552021-01-27 19:34:37 +000047 //Check permission. Function should return if this failed. Therefore having '?' at the end
48 //is very important.
49 check_keystore_permission(KeystorePerm::change_password())
50 .context("In on_user_password_changed.")?;
51
52 match DB
53 .with(|db| {
54 UserState::get_with_password_changed(
55 &mut db.borrow_mut(),
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +000056 &LEGACY_MIGRATOR,
Hasini Gunasingheda895552021-01-27 19:34:37 +000057 &SUPER_KEY,
58 user_id as u32,
Paul Crowleyf61fee72021-03-17 14:38:44 -070059 password.as_ref(),
Hasini Gunasingheda895552021-01-27 19:34:37 +000060 )
61 })
62 .context("In on_user_password_changed.")?
63 {
64 UserState::LskfLocked => {
Janis Danisevskiseed69842021-02-18 20:04:10 -080065 // Error - password can not be changed when the device is locked
Hasini Gunasingheda895552021-01-27 19:34:37 +000066 Err(KeystoreError::Rc(ResponseCode::LOCKED))
67 .context("In on_user_password_changed. Device is locked.")
68 }
69 _ => {
Janis Danisevskiseed69842021-02-18 20:04:10 -080070 // LskfLocked is the only error case for password change
Hasini Gunasingheda895552021-01-27 19:34:37 +000071 Ok(())
72 }
73 }
74 }
75
76 fn add_or_remove_user(user_id: i32) -> Result<()> {
77 // Check permission. Function should return if this failed. Therefore having '?' at the end
78 // is very important.
79 check_keystore_permission(KeystorePerm::change_user()).context("In add_or_remove_user.")?;
Janis Danisevskiseed69842021-02-18 20:04:10 -080080 DB.with(|db| {
81 UserState::reset_user(
82 &mut db.borrow_mut(),
83 &SUPER_KEY,
84 &LEGACY_MIGRATOR,
85 user_id as u32,
86 false,
87 )
88 })
89 .context("In add_or_remove_user: Trying to delete keys from db.")
Hasini Gunasingheda895552021-01-27 19:34:37 +000090 }
Janis Danisevskisddd6e752021-02-22 18:46:55 -080091
92 fn clear_namespace(domain: Domain, nspace: i64) -> Result<()> {
93 // Permission check. Must return on error. Do not touch the '?'.
94 check_keystore_permission(KeystorePerm::clear_uid()).context("In clear_namespace.")?;
95
96 LEGACY_MIGRATOR
97 .bulk_delete_uid(domain, nspace)
98 .context("In clear_namespace: Trying to delete legacy keys.")?;
99 DB.with(|db| db.borrow_mut().unbind_keys_for_namespace(domain, nspace))
100 .context("In clear_namespace: Trying to delete keys from db.")
101 }
Hasini Gunasinghe9ee18412021-03-11 20:12:44 +0000102
103 fn get_state(user_id: i32) -> Result<AidlUserState> {
104 // Check permission. Function should return if this failed. Therefore having '?' at the end
105 // is very important.
106 check_keystore_permission(KeystorePerm::get_state()).context("In get_state.")?;
107 let state = DB
108 .with(|db| {
109 UserState::get(&mut db.borrow_mut(), &LEGACY_MIGRATOR, &SUPER_KEY, user_id as u32)
110 })
111 .context("In get_state. Trying to get UserState.")?;
112
113 match state {
114 UserState::Uninitialized => Ok(AidlUserState::UNINITIALIZED),
115 UserState::LskfUnlocked(_) => Ok(AidlUserState::LSKF_UNLOCKED),
116 UserState::LskfLocked => Ok(AidlUserState::LSKF_LOCKED),
117 }
118 }
Hasini Gunasingheda895552021-01-27 19:34:37 +0000119}
120
Janis Danisevskis34a0cf22021-03-08 09:19:03 -0800121impl Interface for Maintenance {}
Hasini Gunasingheda895552021-01-27 19:34:37 +0000122
Janis Danisevskis34a0cf22021-03-08 09:19:03 -0800123impl IKeystoreMaintenance for Maintenance {
Hasini Gunasingheda895552021-01-27 19:34:37 +0000124 fn onUserPasswordChanged(&self, user_id: i32, password: Option<&[u8]>) -> BinderResult<()> {
Paul Crowleyf61fee72021-03-17 14:38:44 -0700125 map_or_log_err(Self::on_user_password_changed(user_id, password.map(|pw| pw.into())), Ok)
Hasini Gunasingheda895552021-01-27 19:34:37 +0000126 }
127
128 fn onUserAdded(&self, user_id: i32) -> BinderResult<()> {
129 map_or_log_err(Self::add_or_remove_user(user_id), Ok)
130 }
131
132 fn onUserRemoved(&self, user_id: i32) -> BinderResult<()> {
133 map_or_log_err(Self::add_or_remove_user(user_id), Ok)
134 }
Janis Danisevskisddd6e752021-02-22 18:46:55 -0800135
136 fn clearNamespace(&self, domain: Domain, nspace: i64) -> BinderResult<()> {
137 map_or_log_err(Self::clear_namespace(domain, nspace), Ok)
138 }
Hasini Gunasinghe9ee18412021-03-11 20:12:44 +0000139
140 fn getState(&self, user_id: i32) -> binder::public_api::Result<AidlUserState> {
141 map_or_log_err(Self::get_state(user_id), Ok)
142 }
Hasini Gunasingheda895552021-01-27 19:34:37 +0000143}