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" |
| 19 | |
| 20 | #include <algorithm> |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 21 | #include <android-base/logging.h> |
| 22 | #include <mutex> |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 23 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 24 | namespace keystore { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 25 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 26 | OperationMap::OperationMap(IBinder::DeathRecipient* deathRecipient) |
Shawn Willden | 715d023 | 2016-01-21 00:45:13 -0700 | [diff] [blame] | 27 | : mDeathRecipient(deathRecipient) {} |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 28 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 29 | sp<IBinder> OperationMap::addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose, |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 30 | const sp<Keymaster>& dev, const sp<IBinder>& appToken, |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 31 | KeyCharacteristics&& characteristics, |
| 32 | const hidl_vec<KeyParameter>& params, bool pruneable) { |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 33 | sp<IBinder> token = new ::android::BBinder(); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 34 | mMap.emplace(token, std::make_shared<Operation>(handle, keyid, purpose, dev, |
| 35 | std::move(characteristics), appToken, params)); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 36 | if (pruneable) mLru.push_back(token); |
| 37 | if (mAppTokenMap.find(appToken) == mAppTokenMap.end()) appToken->linkToDeath(mDeathRecipient); |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 38 | mAppTokenMap[appToken].push_back(token); |
| 39 | return token; |
| 40 | } |
| 41 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 42 | std::shared_ptr<Operation> OperationMap::getOperation(const sp<IBinder>& token) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 43 | auto entry = mMap.find(token); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 44 | if (entry == mMap.end()) return {}; |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 45 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 46 | auto op = entry->second; |
| 47 | |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 48 | updateLru(token); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 49 | return op; |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Chih-Hung Hsieh | 24b2a39 | 2016-07-28 10:35:24 -0700 | [diff] [blame] | 52 | void OperationMap::updateLru(const sp<IBinder>& token) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 53 | auto lruEntry = std::find(mLru.begin(), mLru.end(), token); |
| 54 | if (lruEntry != mLru.end()) { |
| 55 | mLru.erase(lruEntry); |
| 56 | mLru.push_back(token); |
| 57 | } |
| 58 | } |
| 59 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 60 | std::shared_ptr<Operation> OperationMap::removeOperation(const sp<IBinder>& token, |
| 61 | bool wasSuccessful) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 62 | auto entry = mMap.find(token); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 63 | if (entry == mMap.end()) return {}; |
| 64 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 65 | auto op = entry->second; |
Max Bires | 091ed1b | 2018-12-05 12:13:30 -0800 | [diff] [blame] | 66 | operationUploader.uploadOpAsProto(*op, wasSuccessful); |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 67 | mMap.erase(entry); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 68 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 69 | auto lruEntry = std::find(mLru.begin(), mLru.end(), token); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 70 | if (lruEntry != mLru.end()) mLru.erase(lruEntry); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 71 | removeOperationTracking(token, op->appToken); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 72 | return op; |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Chih-Hung Hsieh | 24b2a39 | 2016-07-28 10:35:24 -0700 | [diff] [blame] | 75 | void OperationMap::removeOperationTracking(const sp<IBinder>& token, const sp<IBinder>& appToken) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 76 | auto appEntry = mAppTokenMap.find(appToken); |
| 77 | if (appEntry == mAppTokenMap.end()) { |
| 78 | ALOGE("Entry for %p contains unmapped application token %p", token.get(), appToken.get()); |
| 79 | return; |
| 80 | } |
| 81 | auto tokenEntry = std::find(appEntry->second.begin(), appEntry->second.end(), token); |
| 82 | appEntry->second.erase(tokenEntry); |
| 83 | // Stop listening for death if all operations tied to the token have finished. |
| 84 | if (appEntry->second.size() == 0) { |
| 85 | appToken->unlinkToDeath(mDeathRecipient); |
| 86 | mAppTokenMap.erase(appEntry); |
| 87 | } |
| 88 | } |
| 89 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 90 | sp<IBinder> OperationMap::getOldestPruneableOperation() { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 91 | if (mLru.size() == 0) return {}; |
Chad Brubaker | 0cf34a2 | 2015-04-23 11:06:16 -0700 | [diff] [blame] | 92 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 93 | return {mLru.front()}; |
Chad Brubaker | 0cf34a2 | 2015-04-23 11:06:16 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Chih-Hung Hsieh | 24b2a39 | 2016-07-28 10:35:24 -0700 | [diff] [blame] | 96 | std::vector<sp<IBinder>> OperationMap::getOperationsForToken(const sp<IBinder>& appToken) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 97 | auto appEntry = mAppTokenMap.find(appToken); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 98 | if (appEntry == mAppTokenMap.end()) return {}; |
| 99 | return appEntry->second; |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 102 | } // namespace keystore |