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> |
| 21 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 22 | namespace keystore { |
| 23 | using namespace android; |
| 24 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 25 | OperationMap::OperationMap(IBinder::DeathRecipient* deathRecipient) |
Shawn Willden | 715d023 | 2016-01-21 00:45:13 -0700 | [diff] [blame] | 26 | : mDeathRecipient(deathRecipient) {} |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 27 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 28 | sp<IBinder> OperationMap::addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose, |
| 29 | const OperationMap::km_device_t& dev, |
Chih-Hung Hsieh | 24b2a39 | 2016-07-28 10:35:24 -0700 | [diff] [blame] | 30 | const sp<IBinder>& appToken, |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 31 | KeyCharacteristics&& characteristics, bool pruneable) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 32 | sp<IBinder> token = new BBinder(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 33 | mMap[token] = Operation(handle, keyid, purpose, dev, std::move(characteristics), appToken); |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 34 | if (pruneable) { |
| 35 | mLru.push_back(token); |
| 36 | } |
| 37 | if (mAppTokenMap.find(appToken) == mAppTokenMap.end()) { |
| 38 | appToken->linkToDeath(mDeathRecipient); |
| 39 | } |
| 40 | mAppTokenMap[appToken].push_back(token); |
| 41 | return token; |
| 42 | } |
| 43 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 44 | bool OperationMap::getOperation(const sp<IBinder>& token, uint64_t* outHandle, uint64_t* outKeyid, |
| 45 | KeyPurpose* outPurpose, km_device_t* outDevice, |
| 46 | const KeyCharacteristics** outCharacteristics) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 47 | if (!outHandle || !outDevice) { |
| 48 | return false; |
| 49 | } |
| 50 | auto entry = mMap.find(token); |
| 51 | if (entry == mMap.end()) { |
| 52 | return false; |
| 53 | } |
| 54 | updateLru(token); |
| 55 | |
| 56 | *outHandle = entry->second.handle; |
Shawn Willden | 9221bff | 2015-06-18 18:23:54 -0600 | [diff] [blame] | 57 | *outKeyid = entry->second.keyid; |
Shawn Willden | b2ffa42 | 2015-06-17 12:18:55 -0600 | [diff] [blame] | 58 | *outPurpose = entry->second.purpose; |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 59 | *outDevice = entry->second.device; |
Chad Brubaker | ad6514a | 2015-04-09 14:00:26 -0700 | [diff] [blame] | 60 | if (outCharacteristics) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 61 | *outCharacteristics = &entry->second.characteristics; |
Chad Brubaker | 06801e0 | 2015-03-31 15:13:13 -0700 | [diff] [blame] | 62 | } |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 63 | return true; |
| 64 | } |
| 65 | |
Chih-Hung Hsieh | 24b2a39 | 2016-07-28 10:35:24 -0700 | [diff] [blame] | 66 | void OperationMap::updateLru(const sp<IBinder>& token) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 67 | auto lruEntry = std::find(mLru.begin(), mLru.end(), token); |
| 68 | if (lruEntry != mLru.end()) { |
| 69 | mLru.erase(lruEntry); |
| 70 | mLru.push_back(token); |
| 71 | } |
| 72 | } |
| 73 | |
Chih-Hung Hsieh | 24b2a39 | 2016-07-28 10:35:24 -0700 | [diff] [blame] | 74 | bool OperationMap::removeOperation(const sp<IBinder>& token) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 75 | auto entry = mMap.find(token); |
| 76 | if (entry == mMap.end()) { |
| 77 | return false; |
| 78 | } |
| 79 | sp<IBinder> appToken = entry->second.appToken; |
| 80 | mMap.erase(entry); |
| 81 | auto lruEntry = std::find(mLru.begin(), mLru.end(), token); |
| 82 | if (lruEntry != mLru.end()) { |
| 83 | mLru.erase(lruEntry); |
| 84 | } |
| 85 | removeOperationTracking(token, appToken); |
| 86 | return true; |
| 87 | } |
| 88 | |
Chih-Hung Hsieh | 24b2a39 | 2016-07-28 10:35:24 -0700 | [diff] [blame] | 89 | void OperationMap::removeOperationTracking(const sp<IBinder>& token, const sp<IBinder>& appToken) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 90 | auto appEntry = mAppTokenMap.find(appToken); |
| 91 | if (appEntry == mAppTokenMap.end()) { |
| 92 | ALOGE("Entry for %p contains unmapped application token %p", token.get(), appToken.get()); |
| 93 | return; |
| 94 | } |
| 95 | auto tokenEntry = std::find(appEntry->second.begin(), appEntry->second.end(), token); |
| 96 | appEntry->second.erase(tokenEntry); |
| 97 | // Stop listening for death if all operations tied to the token have finished. |
| 98 | if (appEntry->second.size() == 0) { |
| 99 | appToken->unlinkToDeath(mDeathRecipient); |
| 100 | mAppTokenMap.erase(appEntry); |
| 101 | } |
| 102 | } |
| 103 | |
Alex Klyubin | 700c1a3 | 2015-06-23 15:21:51 -0700 | [diff] [blame] | 104 | bool OperationMap::hasPruneableOperation() const { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 105 | return mLru.size() != 0; |
| 106 | } |
| 107 | |
Alex Klyubin | 700c1a3 | 2015-06-23 15:21:51 -0700 | [diff] [blame] | 108 | size_t OperationMap::getPruneableOperationCount() const { |
| 109 | return mLru.size(); |
| 110 | } |
| 111 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 112 | sp<IBinder> OperationMap::getOldestPruneableOperation() { |
| 113 | if (!hasPruneableOperation()) { |
| 114 | return sp<IBinder>(NULL); |
| 115 | } |
| 116 | return mLru[0]; |
| 117 | } |
| 118 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 119 | bool OperationMap::getOperationAuthToken(const sp<IBinder>& token, |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 120 | const HardwareAuthToken** outToken) { |
Chad Brubaker | 0cf34a2 | 2015-04-23 11:06:16 -0700 | [diff] [blame] | 121 | auto entry = mMap.find(token); |
| 122 | if (entry == mMap.end()) { |
| 123 | return false; |
| 124 | } |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 125 | *outToken = entry->second.authToken.get(); |
Chad Brubaker | 0cf34a2 | 2015-04-23 11:06:16 -0700 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
Shawn Willden | debb61e | 2017-12-03 12:51:19 -0700 | [diff] [blame^] | 129 | bool OperationMap::setOperationAuthToken(const sp<IBinder>& token, HardwareAuthToken authToken) { |
Chad Brubaker | 0cf34a2 | 2015-04-23 11:06:16 -0700 | [diff] [blame] | 130 | auto entry = mMap.find(token); |
| 131 | if (entry == mMap.end()) { |
| 132 | return false; |
| 133 | } |
Shawn Willden | debb61e | 2017-12-03 12:51:19 -0700 | [diff] [blame^] | 134 | entry->second.authToken = std::make_unique<HardwareAuthToken>(std::move(authToken)); |
Chad Brubaker | 0cf34a2 | 2015-04-23 11:06:16 -0700 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | |
Chih-Hung Hsieh | 24b2a39 | 2016-07-28 10:35:24 -0700 | [diff] [blame] | 138 | std::vector<sp<IBinder>> OperationMap::getOperationsForToken(const sp<IBinder>& appToken) { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 139 | auto appEntry = mAppTokenMap.find(appToken); |
| 140 | if (appEntry != mAppTokenMap.end()) { |
| 141 | return appEntry->second; |
| 142 | } else { |
| 143 | return std::vector<sp<IBinder>>(); |
| 144 | } |
| 145 | } |
| 146 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 147 | OperationMap::Operation::Operation(uint64_t handle_, uint64_t keyid_, KeyPurpose purpose_, |
| 148 | const OperationMap::km_device_t& device_, |
| 149 | KeyCharacteristics&& characteristics_, sp<IBinder> appToken_) |
Shawn Willden | 715d023 | 2016-01-21 00:45:13 -0700 | [diff] [blame] | 150 | : handle(handle_), keyid(keyid_), purpose(purpose_), device(device_), |
| 151 | characteristics(characteristics_), appToken(appToken_) {} |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 152 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 153 | OperationMap::Operation::Operation() |
| 154 | : handle(0), keyid(0), device(nullptr), characteristics(), appToken(nullptr) {} |
Shawn Willden | 715d023 | 2016-01-21 00:45:13 -0700 | [diff] [blame] | 155 | |
| 156 | } // namespace android |