Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #define LOG_TAG "KeystoreOperation" |
| 17 | |
| 18 | #include "operation.h" |
Hasini Gunasinghe | 242460e | 2020-06-05 14:06:02 +0000 | [diff] [blame] | 19 | #include "key_operation_log_handler.h" |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 20 | |
| 21 | #include <algorithm> |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
| 23 | #include <mutex> |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 24 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 25 | namespace keystore { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 26 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 27 | OperationMap::OperationMap(IBinder::DeathRecipient* deathRecipient) |
Shawn Willden | 715d023 | 2016-01-21 00:45:13 -0700 | [diff] [blame] | 28 | : mDeathRecipient(deathRecipient) {} |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 29 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 30 | sp<IBinder> OperationMap::addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose, |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 31 | const sp<Keymaster>& dev, const sp<IBinder>& appToken, |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 32 | KeyCharacteristics&& characteristics, |
| 33 | const hidl_vec<KeyParameter>& params, bool pruneable) { |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 34 | sp<IBinder> token = new ::android::BBinder(); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 35 | mMap.emplace(token, std::make_shared<Operation>(handle, keyid, purpose, dev, |
| 36 | std::move(characteristics), appToken, params)); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 37 | if (pruneable) mLru.push_back(token); |
| 38 | if (mAppTokenMap.find(appToken) == mAppTokenMap.end()) appToken->linkToDeath(mDeathRecipient); |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 39 | mAppTokenMap[appToken].push_back(token); |
| 40 | return token; |
| 41 | } |
| 42 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 43 | std::shared_ptr<Operation> OperationMap::getOperation(const sp<IBinder>& token) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 44 | auto entry = mMap.find(token); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 45 | if (entry == mMap.end()) return {}; |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 46 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 47 | auto op = entry->second; |
| 48 | |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 49 | updateLru(token); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 50 | return op; |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Chih-Hung Hsieh | 24b2a39 | 2016-07-28 10:35:24 -0700 | [diff] [blame] | 53 | void OperationMap::updateLru(const sp<IBinder>& token) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 54 | auto lruEntry = std::find(mLru.begin(), mLru.end(), token); |
| 55 | if (lruEntry != mLru.end()) { |
| 56 | mLru.erase(lruEntry); |
| 57 | mLru.push_back(token); |
| 58 | } |
| 59 | } |
| 60 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 61 | std::shared_ptr<Operation> OperationMap::removeOperation(const sp<IBinder>& token, |
Hasini Gunasinghe | 242460e | 2020-06-05 14:06:02 +0000 | [diff] [blame] | 62 | bool wasSuccessful, int32_t responseCode) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 63 | auto entry = mMap.find(token); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 64 | if (entry == mMap.end()) return {}; |
| 65 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 66 | auto op = entry->second; |
Hasini Gunasinghe | 242460e | 2020-06-05 14:06:02 +0000 | [diff] [blame] | 67 | logKeystoreKeyOperationEvent(*op, wasSuccessful, responseCode); |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 68 | mMap.erase(entry); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 69 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 70 | auto lruEntry = std::find(mLru.begin(), mLru.end(), token); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 71 | if (lruEntry != mLru.end()) mLru.erase(lruEntry); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 72 | removeOperationTracking(token, op->appToken); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 73 | return op; |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Chih-Hung Hsieh | 24b2a39 | 2016-07-28 10:35:24 -0700 | [diff] [blame] | 76 | void OperationMap::removeOperationTracking(const sp<IBinder>& token, const sp<IBinder>& appToken) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 77 | auto appEntry = mAppTokenMap.find(appToken); |
| 78 | if (appEntry == mAppTokenMap.end()) { |
| 79 | ALOGE("Entry for %p contains unmapped application token %p", token.get(), appToken.get()); |
| 80 | return; |
| 81 | } |
| 82 | auto tokenEntry = std::find(appEntry->second.begin(), appEntry->second.end(), token); |
| 83 | appEntry->second.erase(tokenEntry); |
| 84 | // Stop listening for death if all operations tied to the token have finished. |
| 85 | if (appEntry->second.size() == 0) { |
| 86 | appToken->unlinkToDeath(mDeathRecipient); |
| 87 | mAppTokenMap.erase(appEntry); |
| 88 | } |
| 89 | } |
| 90 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 91 | sp<IBinder> OperationMap::getOldestPruneableOperation() { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 92 | if (mLru.size() == 0) return {}; |
Chad Brubaker | 0cf34a2 | 2015-04-23 11:06:16 -0700 | [diff] [blame] | 93 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 94 | return {mLru.front()}; |
Chad Brubaker | 0cf34a2 | 2015-04-23 11:06:16 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Chih-Hung Hsieh | 24b2a39 | 2016-07-28 10:35:24 -0700 | [diff] [blame] | 97 | std::vector<sp<IBinder>> OperationMap::getOperationsForToken(const sp<IBinder>& appToken) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 98 | auto appEntry = mAppTokenMap.find(appToken); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 99 | if (appEntry == mAppTokenMap.end()) return {}; |
| 100 | return appEntry->second; |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 103 | } // namespace keystore |