blob: 389f9f83b698dd96edd63a8df15502c38be02456 [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
Shawn Willden0329a822017-12-04 13:55:14 -070020#include <map>
21#include <vector>
22
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080023#include <binder/Binder.h>
24#include <binder/IBinder.h>
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080025#include <utils/StrongPointer.h>
Shawn Willden0329a822017-12-04 13:55:14 -070026
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070027#include <keystore/keymaster_types.h>
Shawn Willden0329a822017-12-04 13:55:14 -070028
29#include "Keymaster.h"
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080030
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010031namespace keystore {
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080032
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010033using ::android::IBinder;
34using ::android::sp;
Chad Brubakerad6514a2015-04-09 14:00:26 -070035
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080036/**
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010037 * OperationMap handles the translation of uint64_t's and keymaster2_device_t's to opaque binder
38 * tokens that can be used to reference that operation at a later time by applications. It also does
39 * LRU tracking for operation pruning and keeps a mapping of clients to operations to allow for
40 * graceful handling of application death.
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080041 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010042
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080043class OperationMap {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010044 public:
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080045 struct Operation {
Shawn Willdenda6dcc32017-12-03 14:56:05 -070046 Operation() = default;
Shawn Willden0329a822017-12-04 13:55:14 -070047 Operation(uint64_t handle, uint64_t keyid, KeyPurpose purpose, const sp<Keymaster>& device,
Shawn Willdenda6dcc32017-12-03 14:56:05 -070048 KeyCharacteristics&& characteristics, sp<IBinder> appToken);
49 Operation(Operation&&) = default;
50 Operation(const Operation&) = delete;
51
Shawn Willden0329a822017-12-04 13:55:14 -070052 bool hasAuthToken() const { return authToken.mac.size() == 0; }
53
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010054 uint64_t handle;
Shawn Willden9221bff2015-06-18 18:23:54 -060055 uint64_t keyid;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010056 KeyPurpose purpose;
Shawn Willden0329a822017-12-04 13:55:14 -070057 sp<Keymaster> device;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010058 KeyCharacteristics characteristics;
Shawn Willdenda6dcc32017-12-03 14:56:05 -070059 sp<IBinder> appToken;
Shawn Willden0329a822017-12-04 13:55:14 -070060 HardwareAuthToken authToken;
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080061 };
Shawn Willdenda6dcc32017-12-03 14:56:05 -070062
63 explicit OperationMap(IBinder::DeathRecipient* deathRecipient);
64 sp<IBinder> addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose,
Shawn Willden0329a822017-12-04 13:55:14 -070065 const sp<Keymaster>& dev, const sp<IBinder>& appToken,
Shawn Willdenda6dcc32017-12-03 14:56:05 -070066 KeyCharacteristics&& characteristics, bool pruneable);
67 NullOr<const Operation&> getOperation(const sp<IBinder>& token);
68 NullOr<Operation> removeOperation(const sp<IBinder>& token);
69 bool hasPruneableOperation() const;
70 size_t getOperationCount() const { return mMap.size(); }
71 size_t getPruneableOperationCount() const;
72 bool setOperationAuthToken(const sp<IBinder>& token, HardwareAuthToken authToken);
73 sp<IBinder> getOldestPruneableOperation();
74 std::vector<sp<IBinder>> getOperationsForToken(const sp<IBinder>& appToken);
75
76 private:
77 void updateLru(const sp<IBinder>& token);
78 void removeOperationTracking(const sp<IBinder>& token, const sp<IBinder>& appToken);
79 std::map<sp<IBinder>, Operation> mMap;
80 std::vector<sp<IBinder>> mLru;
81 std::map<sp<IBinder>, std::vector<sp<IBinder>>> mAppTokenMap;
82 IBinder::DeathRecipient* mDeathRecipient;
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080083};
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010084
85} // namespace keystore
86
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080087#endif