blob: 087beffc2dea57965b028fcb3d2825566d1d6b4c [file] [log] [blame]
Chad Brubaker40a1a9b2015-02-20 14:08:13 -08001/*
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
22namespace android {
23OperationMap::OperationMap(IBinder::DeathRecipient* deathRecipient)
24 : mDeathRecipient(deathRecipient) {
25}
26
27sp<IBinder> OperationMap::addOperation(keymaster_operation_handle_t handle,
Shawn Willdenb2ffa422015-06-17 12:18:55 -060028 keymaster_purpose_t purpose,
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080029 const keymaster1_device_t* dev,
Chad Brubaker06801e02015-03-31 15:13:13 -070030 sp<IBinder> appToken,
Chad Brubakerad6514a2015-04-09 14:00:26 -070031 keymaster_key_characteristics_t* characteristics,
32 bool pruneable) {
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080033 sp<IBinder> token = new BBinder();
Shawn Willdenb2ffa422015-06-17 12:18:55 -060034 mMap[token] = std::move(Operation(handle, purpose, dev, characteristics, appToken));
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080035 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
45bool OperationMap::getOperation(sp<IBinder> token, keymaster_operation_handle_t* outHandle,
Shawn Willdenb2ffa422015-06-17 12:18:55 -060046 keymaster_purpose_t* outPurpose,
Chad Brubaker06801e02015-03-31 15:13:13 -070047 const keymaster1_device_t** outDevice,
Chad Brubakerad6514a2015-04-09 14:00:26 -070048 const keymaster_key_characteristics_t** outCharacteristics) {
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080049 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 Willdenb2ffa422015-06-17 12:18:55 -060059 *outPurpose = entry->second.purpose;
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080060 *outDevice = entry->second.device;
Chad Brubakerad6514a2015-04-09 14:00:26 -070061 if (outCharacteristics) {
62 *outCharacteristics = entry->second.characteristics.get();
Chad Brubaker06801e02015-03-31 15:13:13 -070063 }
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080064 return true;
65}
66
67void 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
75bool 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
90void 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
105bool OperationMap::hasPruneableOperation() {
106 return mLru.size() != 0;
107}
108
109sp<IBinder> OperationMap::getOldestPruneableOperation() {
110 if (!hasPruneableOperation()) {
111 return sp<IBinder>(NULL);
112 }
113 return mLru[0];
114}
115
Chad Brubaker0cf34a22015-04-23 11:06:16 -0700116bool 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 Brubaker999f1b02015-06-02 14:32:59 -0700121 *outToken = entry->second.authToken.get();
Chad Brubaker0cf34a22015-04-23 11:06:16 -0700122 return true;
123}
124
125bool 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 Brubaker999f1b02015-06-02 14:32:59 -0700130 entry->second.authToken.reset(new hw_auth_token_t);
131 *entry->second.authToken = *authToken;
Chad Brubaker0cf34a22015-04-23 11:06:16 -0700132 return true;
133}
134
Chad Brubaker40a1a9b2015-02-20 14:08:13 -0800135std::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
144OperationMap::Operation::Operation(keymaster_operation_handle_t handle_,
Shawn Willdenb2ffa422015-06-17 12:18:55 -0600145 keymaster_purpose_t purpose_,
Chad Brubaker40a1a9b2015-02-20 14:08:13 -0800146 const keymaster1_device_t* device_,
Chad Brubakerad6514a2015-04-09 14:00:26 -0700147 keymaster_key_characteristics_t* characteristics_,
Chad Brubaker40a1a9b2015-02-20 14:08:13 -0800148 sp<IBinder> appToken_)
149 : handle(handle_),
Shawn Willdenb2ffa422015-06-17 12:18:55 -0600150 purpose(purpose_),
Chad Brubaker40a1a9b2015-02-20 14:08:13 -0800151 device(device_),
Chad Brubakerad6514a2015-04-09 14:00:26 -0700152 characteristics(characteristics_),
Chad Brubaker40a1a9b2015-02-20 14:08:13 -0800153 appToken(appToken_) {
154}
155
Chad Brubakerad6514a2015-04-09 14:00:26 -0700156OperationMap::Operation::Operation() : handle(0), device(NULL), characteristics(), appToken(NULL) {
Chad Brubaker40a1a9b2015-02-20 14:08:13 -0800157}
158} // namespace android