blob: e1ba71333ef56e31b4f46ca6cb012f32e2dfc595 [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
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010022namespace keystore {
23using namespace android;
24
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080025OperationMap::OperationMap(IBinder::DeathRecipient* deathRecipient)
Shawn Willden715d0232016-01-21 00:45:13 -070026 : mDeathRecipient(deathRecipient) {}
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080027
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010028sp<IBinder> OperationMap::addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose,
29 const OperationMap::km_device_t& dev,
Chih-Hung Hsieh24b2a392016-07-28 10:35:24 -070030 const sp<IBinder>& appToken,
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010031 KeyCharacteristics&& characteristics, bool pruneable) {
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080032 sp<IBinder> token = new BBinder();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010033 mMap[token] = Operation(handle, keyid, purpose, dev, std::move(characteristics), appToken);
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080034 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 Danisevskisc7a9fa22016-10-13 18:43:45 +010044bool OperationMap::getOperation(const sp<IBinder>& token, uint64_t* outHandle, uint64_t* outKeyid,
45 KeyPurpose* outPurpose, km_device_t* outDevice,
46 const KeyCharacteristics** outCharacteristics) {
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080047 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 Willden9221bff2015-06-18 18:23:54 -060057 *outKeyid = entry->second.keyid;
Shawn Willdenb2ffa422015-06-17 12:18:55 -060058 *outPurpose = entry->second.purpose;
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080059 *outDevice = entry->second.device;
Chad Brubakerad6514a2015-04-09 14:00:26 -070060 if (outCharacteristics) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010061 *outCharacteristics = &entry->second.characteristics;
Chad Brubaker06801e02015-03-31 15:13:13 -070062 }
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080063 return true;
64}
65
Chih-Hung Hsieh24b2a392016-07-28 10:35:24 -070066void OperationMap::updateLru(const sp<IBinder>& token) {
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080067 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 Hsieh24b2a392016-07-28 10:35:24 -070074bool OperationMap::removeOperation(const sp<IBinder>& token) {
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080075 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 Hsieh24b2a392016-07-28 10:35:24 -070089void OperationMap::removeOperationTracking(const sp<IBinder>& token, const sp<IBinder>& appToken) {
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080090 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 Klyubin700c1a32015-06-23 15:21:51 -0700104bool OperationMap::hasPruneableOperation() const {
Chad Brubaker40a1a9b2015-02-20 14:08:13 -0800105 return mLru.size() != 0;
106}
107
Alex Klyubin700c1a32015-06-23 15:21:51 -0700108size_t OperationMap::getPruneableOperationCount() const {
109 return mLru.size();
110}
111
Chad Brubaker40a1a9b2015-02-20 14:08:13 -0800112sp<IBinder> OperationMap::getOldestPruneableOperation() {
113 if (!hasPruneableOperation()) {
114 return sp<IBinder>(NULL);
115 }
116 return mLru[0];
117}
118
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100119bool OperationMap::getOperationAuthToken(const sp<IBinder>& token,
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000120 const HardwareAuthToken** outToken) {
Chad Brubaker0cf34a22015-04-23 11:06:16 -0700121 auto entry = mMap.find(token);
122 if (entry == mMap.end()) {
123 return false;
124 }
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000125 *outToken = entry->second.authToken.get();
Chad Brubaker0cf34a22015-04-23 11:06:16 -0700126 return true;
127}
128
Shawn Willdendebb61e2017-12-03 12:51:19 -0700129bool OperationMap::setOperationAuthToken(const sp<IBinder>& token, HardwareAuthToken authToken) {
Chad Brubaker0cf34a22015-04-23 11:06:16 -0700130 auto entry = mMap.find(token);
131 if (entry == mMap.end()) {
132 return false;
133 }
Shawn Willdendebb61e2017-12-03 12:51:19 -0700134 entry->second.authToken = std::make_unique<HardwareAuthToken>(std::move(authToken));
Chad Brubaker0cf34a22015-04-23 11:06:16 -0700135 return true;
136}
137
Chih-Hung Hsieh24b2a392016-07-28 10:35:24 -0700138std::vector<sp<IBinder>> OperationMap::getOperationsForToken(const sp<IBinder>& appToken) {
Chad Brubaker40a1a9b2015-02-20 14:08:13 -0800139 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 Danisevskisc7a9fa22016-10-13 18:43:45 +0100147OperationMap::Operation::Operation(uint64_t handle_, uint64_t keyid_, KeyPurpose purpose_,
148 const OperationMap::km_device_t& device_,
149 KeyCharacteristics&& characteristics_, sp<IBinder> appToken_)
Shawn Willden715d0232016-01-21 00:45:13 -0700150 : handle(handle_), keyid(keyid_), purpose(purpose_), device(device_),
151 characteristics(characteristics_), appToken(appToken_) {}
Chad Brubaker40a1a9b2015-02-20 14:08:13 -0800152
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100153OperationMap::Operation::Operation()
154 : handle(0), keyid(0), device(nullptr), characteristics(), appToken(nullptr) {}
Shawn Willden715d0232016-01-21 00:45:13 -0700155
156} // namespace android