blob: d8d1b181d0f0f211293d27e522c5000947a08445 [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
17#ifndef KEYSTORE_OPERATION_H_
18#define KEYSTORE_OPERATION_H_
19
Chad Brubaker0cf34a22015-04-23 11:06:16 -070020#include <hardware/hw_auth_token.h>
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080021#include <hardware/keymaster1.h>
22#include <binder/Binder.h>
23#include <binder/IBinder.h>
24#include <utils/LruCache.h>
25#include <utils/StrongPointer.h>
26#include <map>
27#include <vector>
28
29namespace android {
30
Chad Brubakerad6514a2015-04-09 14:00:26 -070031struct keymaster_key_characteristics_t_Delete {
32 void operator()(keymaster_key_characteristics_t* characteristics) const {
33 keymaster_free_characteristics(characteristics);
34 delete characteristics;
35 }
36};
37typedef std::unique_ptr<keymaster_key_characteristics_t, keymaster_key_characteristics_t_Delete>
38 Unique_keymaster_key_characteristics;
39
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080040/**
41 * OperationMap handles the translation of keymaster_operation_handle_t's and
42 * keymaster1_device_t's to opaque binder tokens that can be used to reference
43 * that operation at a later time by applications. It also does LRU tracking
44 * for operation pruning and keeps a mapping of clients to operations to allow
45 * for graceful handling of application death.
46 */
47class OperationMap {
48public:
49 OperationMap(IBinder::DeathRecipient* deathRecipient);
Shawn Willden9221bff2015-06-18 18:23:54 -060050 sp<IBinder> addOperation(keymaster_operation_handle_t handle, uint64_t keyid,
51 keymaster_purpose_t purpose, const keymaster1_device_t* dev,
52 sp<IBinder> appToken, keymaster_key_characteristics_t* characteristics,
53 bool pruneable);
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080054 bool getOperation(sp<IBinder> token, keymaster_operation_handle_t* outHandle,
Shawn Willden9221bff2015-06-18 18:23:54 -060055 uint64_t* outKeyid, keymaster_purpose_t* outPurpose,
56 const keymaster1_device_t** outDev,
Chad Brubakerad6514a2015-04-09 14:00:26 -070057 const keymaster_key_characteristics_t** outCharacteristics);
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080058 bool removeOperation(sp<IBinder> token);
Alex Klyubin700c1a32015-06-23 15:21:51 -070059 bool hasPruneableOperation() const;
Shawn Willden447095f2015-10-30 10:05:43 -060060 size_t getOperationCount() const { return mMap.size(); }
Alex Klyubin700c1a32015-06-23 15:21:51 -070061 size_t getPruneableOperationCount() const;
Chad Brubaker0cf34a22015-04-23 11:06:16 -070062 bool getOperationAuthToken(sp<IBinder> token, const hw_auth_token_t** outToken);
63 bool setOperationAuthToken(sp<IBinder> token, const hw_auth_token_t* authToken);
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080064 sp<IBinder> getOldestPruneableOperation();
65 std::vector<sp<IBinder>> getOperationsForToken(sp<IBinder> appToken);
66
67private:
68 void updateLru(sp<IBinder> token);
69 void removeOperationTracking(sp<IBinder> token, sp<IBinder> appToken);
70 struct Operation {
71 Operation();
Shawn Willden9221bff2015-06-18 18:23:54 -060072 Operation(keymaster_operation_handle_t handle, uint64_t keyid, keymaster_purpose_t purpose,
Shawn Willdenb2ffa422015-06-17 12:18:55 -060073 const keymaster1_device_t* device,
Chad Brubakerad6514a2015-04-09 14:00:26 -070074 keymaster_key_characteristics_t* characteristics, sp<IBinder> appToken);
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080075 keymaster_operation_handle_t handle;
Shawn Willden9221bff2015-06-18 18:23:54 -060076 uint64_t keyid;
Shawn Willdenb2ffa422015-06-17 12:18:55 -060077 keymaster_purpose_t purpose;
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080078 const keymaster1_device_t* device;
Chad Brubakerad6514a2015-04-09 14:00:26 -070079 Unique_keymaster_key_characteristics characteristics;
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080080 sp<IBinder> appToken;
Chad Brubaker999f1b02015-06-02 14:32:59 -070081 std::unique_ptr<hw_auth_token_t> authToken;
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080082 };
83 std::map<sp<IBinder>, struct Operation> mMap;
84 std::vector<sp<IBinder>> mLru;
85 std::map<sp<IBinder>, std::vector<sp<IBinder>>> mAppTokenMap;
86 IBinder::DeathRecipient* mDeathRecipient;
87};
88} // namespace android
89#endif