Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 1 | /* |
| 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 Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 20 | #include <map> |
| 21 | #include <vector> |
| 22 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 23 | #include <binder/Binder.h> |
| 24 | #include <binder/IBinder.h> |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 25 | #include <utils/StrongPointer.h> |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 26 | |
Shawn Willden | bb22a6c | 2017-12-06 19:35:28 -0700 | [diff] [blame^] | 27 | #include <keystore/keymaster_types.h> |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 28 | |
| 29 | #include "Keymaster.h" |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 30 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 31 | namespace keystore { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 32 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 33 | using ::android::IBinder; |
| 34 | using ::android::sp; |
Chad Brubaker | ad6514a | 2015-04-09 14:00:26 -0700 | [diff] [blame] | 35 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 36 | /** |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 37 | * 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 Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 41 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 42 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 43 | class OperationMap { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 44 | public: |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 45 | struct Operation { |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 46 | Operation() = default; |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 47 | Operation(uint64_t handle, uint64_t keyid, KeyPurpose purpose, const sp<Keymaster>& device, |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 48 | KeyCharacteristics&& characteristics, sp<IBinder> appToken); |
| 49 | Operation(Operation&&) = default; |
| 50 | Operation(const Operation&) = delete; |
| 51 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 52 | bool hasAuthToken() const { return authToken.mac.size() == 0; } |
| 53 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 54 | uint64_t handle; |
Shawn Willden | 9221bff | 2015-06-18 18:23:54 -0600 | [diff] [blame] | 55 | uint64_t keyid; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 56 | KeyPurpose purpose; |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 57 | sp<Keymaster> device; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 58 | KeyCharacteristics characteristics; |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 59 | sp<IBinder> appToken; |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 60 | HardwareAuthToken authToken; |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 61 | }; |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 62 | |
| 63 | explicit OperationMap(IBinder::DeathRecipient* deathRecipient); |
| 64 | sp<IBinder> addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose, |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 65 | const sp<Keymaster>& dev, const sp<IBinder>& appToken, |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 66 | 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 Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 83 | }; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 84 | |
| 85 | } // namespace keystore |
| 86 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 87 | #endif |