blob: c17fa72ae16ad7de0186fe2d734e96e44ee87c29 [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};
31use binder::{IBinder, Strong};
32
33/// This struct is defined to implement the aforementioned AIDL interface.
34/// As of now, it is an empty struct.
Janis Danisevskis34a0cf22021-03-08 09:19:03 -080035pub struct Maintenance;
Hasini Gunasingheda895552021-01-27 19:34:37 +000036
Janis Danisevskis34a0cf22021-03-08 09:19:03 -080037impl Maintenance {
Hasini Gunasingheda895552021-01-27 19:34:37 +000038 /// Create a new instance of Keystore User Manager service.
Janis Danisevskis34a0cf22021-03-08 09:19:03 -080039 pub fn new_native_binder() -> Result<Strong<dyn IKeystoreMaintenance>> {
40 let result = BnKeystoreMaintenance::new_binder(Self);
Hasini Gunasingheda895552021-01-27 19:34:37 +000041 result.as_binder().set_requesting_sid(true);
42 Ok(result)
43 }
44
45 fn on_user_password_changed(user_id: i32, password: Option<&[u8]>) -> Result<()> {
46 //Check permission. Function should return if this failed. Therefore having '?' at the end
47 //is very important.
48 check_keystore_permission(KeystorePerm::change_password())
49 .context("In on_user_password_changed.")?;
50
51 match DB
52 .with(|db| {
53 UserState::get_with_password_changed(
54 &mut db.borrow_mut(),
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +000055 &LEGACY_MIGRATOR,
Hasini Gunasingheda895552021-01-27 19:34:37 +000056 &SUPER_KEY,
57 user_id as u32,
58 password,
59 )
60 })
61 .context("In on_user_password_changed.")?
62 {
63 UserState::LskfLocked => {
Janis Danisevskiseed69842021-02-18 20:04:10 -080064 // Error - password can not be changed when the device is locked
Hasini Gunasingheda895552021-01-27 19:34:37 +000065 Err(KeystoreError::Rc(ResponseCode::LOCKED))
66 .context("In on_user_password_changed. Device is locked.")
67 }
68 _ => {
Janis Danisevskiseed69842021-02-18 20:04:10 -080069 // LskfLocked is the only error case for password change
Hasini Gunasingheda895552021-01-27 19:34:37 +000070 Ok(())
71 }
72 }
73 }
74
75 fn add_or_remove_user(user_id: i32) -> Result<()> {
76 // Check permission. Function should return if this failed. Therefore having '?' at the end
77 // is very important.
78 check_keystore_permission(KeystorePerm::change_user()).context("In add_or_remove_user.")?;
Janis Danisevskiseed69842021-02-18 20:04:10 -080079 DB.with(|db| {
80 UserState::reset_user(
81 &mut db.borrow_mut(),
82 &SUPER_KEY,
83 &LEGACY_MIGRATOR,
84 user_id as u32,
85 false,
86 )
87 })
88 .context("In add_or_remove_user: Trying to delete keys from db.")
Hasini Gunasingheda895552021-01-27 19:34:37 +000089 }
Janis Danisevskisddd6e752021-02-22 18:46:55 -080090
91 fn clear_namespace(domain: Domain, nspace: i64) -> Result<()> {
92 // Permission check. Must return on error. Do not touch the '?'.
93 check_keystore_permission(KeystorePerm::clear_uid()).context("In clear_namespace.")?;
94
95 LEGACY_MIGRATOR
96 .bulk_delete_uid(domain, nspace)
97 .context("In clear_namespace: Trying to delete legacy keys.")?;
98 DB.with(|db| db.borrow_mut().unbind_keys_for_namespace(domain, nspace))
99 .context("In clear_namespace: Trying to delete keys from db.")
100 }
Hasini Gunasinghe9ee18412021-03-11 20:12:44 +0000101
102 fn get_state(user_id: i32) -> Result<AidlUserState> {
103 // Check permission. Function should return if this failed. Therefore having '?' at the end
104 // is very important.
105 check_keystore_permission(KeystorePerm::get_state()).context("In get_state.")?;
106 let state = DB
107 .with(|db| {
108 UserState::get(&mut db.borrow_mut(), &LEGACY_MIGRATOR, &SUPER_KEY, user_id as u32)
109 })
110 .context("In get_state. Trying to get UserState.")?;
111
112 match state {
113 UserState::Uninitialized => Ok(AidlUserState::UNINITIALIZED),
114 UserState::LskfUnlocked(_) => Ok(AidlUserState::LSKF_UNLOCKED),
115 UserState::LskfLocked => Ok(AidlUserState::LSKF_LOCKED),
116 }
117 }
Hasini Gunasingheda895552021-01-27 19:34:37 +0000118}
119
Janis Danisevskis34a0cf22021-03-08 09:19:03 -0800120impl Interface for Maintenance {}
Hasini Gunasingheda895552021-01-27 19:34:37 +0000121
Janis Danisevskis34a0cf22021-03-08 09:19:03 -0800122impl IKeystoreMaintenance for Maintenance {
Hasini Gunasingheda895552021-01-27 19:34:37 +0000123 fn onUserPasswordChanged(&self, user_id: i32, password: Option<&[u8]>) -> BinderResult<()> {
124 map_or_log_err(Self::on_user_password_changed(user_id, password), Ok)
125 }
126
127 fn onUserAdded(&self, user_id: i32) -> BinderResult<()> {
128 map_or_log_err(Self::add_or_remove_user(user_id), Ok)
129 }
130
131 fn onUserRemoved(&self, user_id: i32) -> BinderResult<()> {
132 map_or_log_err(Self::add_or_remove_user(user_id), Ok)
133 }
Janis Danisevskisddd6e752021-02-22 18:46:55 -0800134
135 fn clearNamespace(&self, domain: Domain, nspace: i64) -> BinderResult<()> {
136 map_or_log_err(Self::clear_namespace(domain, nspace), Ok)
137 }
Hasini Gunasinghe9ee18412021-03-11 20:12:44 +0000138
139 fn getState(&self, user_id: i32) -> binder::public_api::Result<AidlUserState> {
140 map_or_log_err(Self::get_state(user_id), Ok)
141 }
Hasini Gunasingheda895552021-01-27 19:34:37 +0000142}