blob: 8e09144c87d4f374aa7a2e3600b2e8a6b55532c9 [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
15//! This module implements IKeystoreUserManager AIDL interface.
16
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;
23use android_security_usermanager::aidl::android::security::usermanager::IKeystoreUserManager::{
24 BnKeystoreUserManager, IKeystoreUserManager,
25};
26use android_security_usermanager::binder::{Interface, Result as BinderResult};
27use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode;
28use anyhow::{Context, Result};
29use binder::{IBinder, Strong};
30
31/// This struct is defined to implement the aforementioned AIDL interface.
32/// As of now, it is an empty struct.
33pub struct UserManager;
34
35impl UserManager {
36 /// Create a new instance of Keystore User Manager service.
37 pub fn new_native_binder() -> Result<Strong<dyn IKeystoreUserManager>> {
38 let result = BnKeystoreUserManager::new_binder(Self);
39 result.as_binder().set_requesting_sid(true);
40 Ok(result)
41 }
42
43 fn on_user_password_changed(user_id: i32, password: Option<&[u8]>) -> Result<()> {
44 //Check permission. Function should return if this failed. Therefore having '?' at the end
45 //is very important.
46 check_keystore_permission(KeystorePerm::change_password())
47 .context("In on_user_password_changed.")?;
48
49 match DB
50 .with(|db| {
51 UserState::get_with_password_changed(
52 &mut db.borrow_mut(),
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +000053 &LEGACY_MIGRATOR,
Hasini Gunasingheda895552021-01-27 19:34:37 +000054 &SUPER_KEY,
55 user_id as u32,
56 password,
57 )
58 })
59 .context("In on_user_password_changed.")?
60 {
61 UserState::LskfLocked => {
Janis Danisevskiseed69842021-02-18 20:04:10 -080062 // Error - password can not be changed when the device is locked
Hasini Gunasingheda895552021-01-27 19:34:37 +000063 Err(KeystoreError::Rc(ResponseCode::LOCKED))
64 .context("In on_user_password_changed. Device is locked.")
65 }
66 _ => {
Janis Danisevskiseed69842021-02-18 20:04:10 -080067 // LskfLocked is the only error case for password change
Hasini Gunasingheda895552021-01-27 19:34:37 +000068 Ok(())
69 }
70 }
71 }
72
73 fn add_or_remove_user(user_id: i32) -> Result<()> {
74 // Check permission. Function should return if this failed. Therefore having '?' at the end
75 // is very important.
76 check_keystore_permission(KeystorePerm::change_user()).context("In add_or_remove_user.")?;
Janis Danisevskiseed69842021-02-18 20:04:10 -080077 DB.with(|db| {
78 UserState::reset_user(
79 &mut db.borrow_mut(),
80 &SUPER_KEY,
81 &LEGACY_MIGRATOR,
82 user_id as u32,
83 false,
84 )
85 })
86 .context("In add_or_remove_user: Trying to delete keys from db.")
Hasini Gunasingheda895552021-01-27 19:34:37 +000087 }
88}
89
90impl Interface for UserManager {}
91
92impl IKeystoreUserManager for UserManager {
93 fn onUserPasswordChanged(&self, user_id: i32, password: Option<&[u8]>) -> BinderResult<()> {
94 map_or_log_err(Self::on_user_password_changed(user_id, password), Ok)
95 }
96
97 fn onUserAdded(&self, user_id: i32) -> BinderResult<()> {
98 map_or_log_err(Self::add_or_remove_user(user_id), Ok)
99 }
100
101 fn onUserRemoved(&self, user_id: i32) -> BinderResult<()> {
102 map_or_log_err(Self::add_or_remove_user(user_id), Ok)
103 }
104}