blob: ac8e9453748f6c635a4fce6c0156bd9fb3b90c35 [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 Brubaker40a1a9b2015-02-20 14:08:13 -080020#include <binder/Binder.h>
21#include <binder/IBinder.h>
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010022#include <keystore/keymaster_tags.h>
23#include <map>
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080024#include <utils/LruCache.h>
25#include <utils/StrongPointer.h>
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080026#include <vector>
27
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010028namespace keystore {
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080029
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010030using ::android::IBinder;
31using ::android::sp;
Chad Brubakerad6514a2015-04-09 14:00:26 -070032
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080033/**
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010034 * OperationMap handles the translation of uint64_t's and keymaster2_device_t's to opaque binder
35 * tokens that can be used to reference that operation at a later time by applications. It also does
36 * LRU tracking for operation pruning and keeps a mapping of clients to operations to allow for
37 * graceful handling of application death.
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080038 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010039
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080040class OperationMap {
Shawn Willdenda6dcc32017-12-03 14:56:05 -070041 typedef sp<::android::hardware::keymaster::V3_0::IKeymasterDevice> km_device_t;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010042
43 public:
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080044 struct Operation {
Shawn Willdenda6dcc32017-12-03 14:56:05 -070045 Operation() = default;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010046 Operation(uint64_t handle, uint64_t keyid, KeyPurpose purpose, const km_device_t& device,
Shawn Willdenda6dcc32017-12-03 14:56:05 -070047 KeyCharacteristics&& characteristics, sp<IBinder> appToken);
48 Operation(Operation&&) = default;
49 Operation(const Operation&) = delete;
50
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010051 uint64_t handle;
Shawn Willden9221bff2015-06-18 18:23:54 -060052 uint64_t keyid;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010053 KeyPurpose purpose;
54 km_device_t device;
55 KeyCharacteristics characteristics;
Shawn Willdenda6dcc32017-12-03 14:56:05 -070056 sp<IBinder> appToken;
Shawn Willdend3ed3a22017-03-28 00:39:16 +000057 std::unique_ptr<HardwareAuthToken> authToken;
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080058 };
Shawn Willdenda6dcc32017-12-03 14:56:05 -070059
60 explicit OperationMap(IBinder::DeathRecipient* deathRecipient);
61 sp<IBinder> addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose,
62 const km_device_t& dev, const sp<IBinder>& appToken,
63 KeyCharacteristics&& characteristics, bool pruneable);
64 NullOr<const Operation&> getOperation(const sp<IBinder>& token);
65 NullOr<Operation> removeOperation(const sp<IBinder>& token);
66 bool hasPruneableOperation() const;
67 size_t getOperationCount() const { return mMap.size(); }
68 size_t getPruneableOperationCount() const;
69 bool setOperationAuthToken(const sp<IBinder>& token, HardwareAuthToken authToken);
70 sp<IBinder> getOldestPruneableOperation();
71 std::vector<sp<IBinder>> getOperationsForToken(const sp<IBinder>& appToken);
72
73 private:
74 void updateLru(const sp<IBinder>& token);
75 void removeOperationTracking(const sp<IBinder>& token, const sp<IBinder>& appToken);
76 std::map<sp<IBinder>, Operation> mMap;
77 std::vector<sp<IBinder>> mLru;
78 std::map<sp<IBinder>, std::vector<sp<IBinder>>> mAppTokenMap;
79 IBinder::DeathRecipient* mDeathRecipient;
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080080};
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010081
82} // namespace keystore
83
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080084#endif