blob: a3125281b13cf6c873e5bd752ea99ac6e0909555 [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
20#include <hardware/keymaster1.h>
21#include <binder/Binder.h>
22#include <binder/IBinder.h>
23#include <utils/LruCache.h>
24#include <utils/StrongPointer.h>
25#include <map>
26#include <vector>
27
28namespace android {
29
Chad Brubakerad6514a2015-04-09 14:00:26 -070030struct keymaster_key_characteristics_t_Delete {
31 void operator()(keymaster_key_characteristics_t* characteristics) const {
32 keymaster_free_characteristics(characteristics);
33 delete characteristics;
34 }
35};
36typedef std::unique_ptr<keymaster_key_characteristics_t, keymaster_key_characteristics_t_Delete>
37 Unique_keymaster_key_characteristics;
38
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080039/**
40 * OperationMap handles the translation of keymaster_operation_handle_t's and
41 * keymaster1_device_t's to opaque binder tokens that can be used to reference
42 * that operation at a later time by applications. It also does LRU tracking
43 * for operation pruning and keeps a mapping of clients to operations to allow
44 * for graceful handling of application death.
45 */
46class OperationMap {
47public:
48 OperationMap(IBinder::DeathRecipient* deathRecipient);
49 sp<IBinder> addOperation(keymaster_operation_handle_t handle,
50 const keymaster1_device_t* dev, sp<IBinder> appToken,
Chad Brubakerad6514a2015-04-09 14:00:26 -070051 keymaster_key_characteristics_t* characteristics, bool pruneable);
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080052 bool getOperation(sp<IBinder> token, keymaster_operation_handle_t* outHandle,
Chad Brubakerad6514a2015-04-09 14:00:26 -070053 const keymaster1_device_t** outDev,
54 const keymaster_key_characteristics_t** outCharacteristics);
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080055 bool removeOperation(sp<IBinder> token);
56 bool hasPruneableOperation();
57 sp<IBinder> getOldestPruneableOperation();
58 std::vector<sp<IBinder>> getOperationsForToken(sp<IBinder> appToken);
59
60private:
61 void updateLru(sp<IBinder> token);
62 void removeOperationTracking(sp<IBinder> token, sp<IBinder> appToken);
63 struct Operation {
64 Operation();
65 Operation(keymaster_operation_handle_t handle, const keymaster1_device_t* device,
Chad Brubakerad6514a2015-04-09 14:00:26 -070066 keymaster_key_characteristics_t* characteristics, sp<IBinder> appToken);
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080067 keymaster_operation_handle_t handle;
68 const keymaster1_device_t* device;
Chad Brubakerad6514a2015-04-09 14:00:26 -070069 Unique_keymaster_key_characteristics characteristics;
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080070 sp<IBinder> appToken;
71 };
72 std::map<sp<IBinder>, struct Operation> mMap;
73 std::vector<sp<IBinder>> mLru;
74 std::map<sp<IBinder>, std::vector<sp<IBinder>>> mAppTokenMap;
75 IBinder::DeathRecipient* mDeathRecipient;
76};
77} // namespace android
78#endif