blob: 607683624a1c1fc1b087e2e0a98d4aadee9efeb3 [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
30/**
31 * OperationMap handles the translation of keymaster_operation_handle_t's and
32 * keymaster1_device_t's to opaque binder tokens that can be used to reference
33 * that operation at a later time by applications. It also does LRU tracking
34 * for operation pruning and keeps a mapping of clients to operations to allow
35 * for graceful handling of application death.
36 */
37class OperationMap {
38public:
39 OperationMap(IBinder::DeathRecipient* deathRecipient);
40 sp<IBinder> addOperation(keymaster_operation_handle_t handle,
41 const keymaster1_device_t* dev, sp<IBinder> appToken,
Chad Brubaker06801e02015-03-31 15:13:13 -070042 const keymaster_key_blob_t& key, bool pruneable);
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080043 bool getOperation(sp<IBinder> token, keymaster_operation_handle_t* outHandle,
Chad Brubaker06801e02015-03-31 15:13:13 -070044 const keymaster1_device_t** outDev, keymaster_key_blob_t* outKey);
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080045 bool removeOperation(sp<IBinder> token);
46 bool hasPruneableOperation();
47 sp<IBinder> getOldestPruneableOperation();
48 std::vector<sp<IBinder>> getOperationsForToken(sp<IBinder> appToken);
49
50private:
51 void updateLru(sp<IBinder> token);
52 void removeOperationTracking(sp<IBinder> token, sp<IBinder> appToken);
53 struct Operation {
54 Operation();
55 Operation(keymaster_operation_handle_t handle, const keymaster1_device_t* device,
Chad Brubaker06801e02015-03-31 15:13:13 -070056 const keymaster_key_blob_t& key, sp<IBinder> appToken);
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080057 keymaster_operation_handle_t handle;
58 const keymaster1_device_t* device;
Chad Brubaker06801e02015-03-31 15:13:13 -070059 keymaster_key_blob_t key;
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080060 sp<IBinder> appToken;
61 };
62 std::map<sp<IBinder>, struct Operation> mMap;
63 std::vector<sp<IBinder>> mLru;
64 std::map<sp<IBinder>, std::vector<sp<IBinder>>> mAppTokenMap;
65 IBinder::DeathRecipient* mDeathRecipient;
66};
67} // namespace android
68#endif