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