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 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 20 | #include <list> |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 21 | #include <map> |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 22 | #include <memory> |
| 23 | #include <mutex> |
| 24 | #include <optional> |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 27 | #include <binder/Binder.h> |
| 28 | #include <binder/IBinder.h> |
Shawn Willden | eedcfe9 | 2018-01-18 15:35:46 -0700 | [diff] [blame] | 29 | #include <keymasterV4_0/Keymaster.h> |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 30 | #include <utils/StrongPointer.h> |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 31 | |
Shawn Willden | bb22a6c | 2017-12-06 19:35:28 -0700 | [diff] [blame] | 32 | #include <keystore/keymaster_types.h> |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 33 | #include <keystore/keystore_concurrency.h> |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 34 | #include <keystore/keystore_hidl_support.h> |
| 35 | |
| 36 | #include "operation_proto_handler.h" |
| 37 | #include "operation_struct.h" |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 38 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 39 | namespace keystore { |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 40 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 41 | using ::android::IBinder; |
| 42 | using ::android::sp; |
Shawn Willden | eedcfe9 | 2018-01-18 15:35:46 -0700 | [diff] [blame] | 43 | using keymaster::support::Keymaster; |
Chad Brubaker | ad6514a | 2015-04-09 14:00:26 -0700 | [diff] [blame] | 44 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 45 | /** |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 46 | * OperationMap handles the translation of uint64_t's and keymaster2_device_t's to opaque binder |
| 47 | * tokens that can be used to reference that operation at a later time by applications. It also does |
| 48 | * LRU tracking for operation pruning and keeps a mapping of clients to operations to allow for |
| 49 | * graceful handling of application death. |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 50 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 51 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 52 | class OperationMap { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 53 | public: |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 54 | explicit OperationMap(IBinder::DeathRecipient* deathRecipient); |
| 55 | sp<IBinder> addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose, |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 56 | const sp<Keymaster>& dev, const sp<IBinder>& appToken, |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 57 | KeyCharacteristics&& characteristics, |
| 58 | const hidl_vec<KeyParameter>& params, bool pruneable); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 59 | std::shared_ptr<Operation> getOperation(const sp<IBinder>& token); |
| 60 | std::shared_ptr<Operation> removeOperation(const sp<IBinder>& token, bool wasSuccessful); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 61 | size_t getOperationCount() const { return mMap.size(); } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 62 | sp<IBinder> getOldestPruneableOperation(); |
| 63 | std::vector<sp<IBinder>> getOperationsForToken(const sp<IBinder>& appToken); |
| 64 | |
| 65 | private: |
| 66 | void updateLru(const sp<IBinder>& token); |
| 67 | void removeOperationTracking(const sp<IBinder>& token, const sp<IBinder>& appToken); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 68 | |
| 69 | std::map<sp<IBinder>, std::shared_ptr<Operation>> mMap; |
| 70 | std::list<sp<IBinder>> mLru; |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 71 | std::map<sp<IBinder>, std::vector<sp<IBinder>>> mAppTokenMap; |
| 72 | IBinder::DeathRecipient* mDeathRecipient; |
Max Bires | 091ed1b | 2018-12-05 12:13:30 -0800 | [diff] [blame] | 73 | OperationProtoHandler operationUploader; |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 74 | }; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 75 | |
| 76 | } // namespace keystore |
| 77 | |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 78 | #endif |