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 | |
| 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 | |
| 28 | namespace 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 | */ |
| 37 | class OperationMap { |
| 38 | public: |
| 39 | OperationMap(IBinder::DeathRecipient* deathRecipient); |
| 40 | sp<IBinder> addOperation(keymaster_operation_handle_t handle, |
| 41 | const keymaster1_device_t* dev, sp<IBinder> appToken, |
Chad Brubaker | 06801e0 | 2015-03-31 15:13:13 -0700 | [diff] [blame^] | 42 | const keymaster_key_blob_t& key, bool pruneable); |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 43 | bool getOperation(sp<IBinder> token, keymaster_operation_handle_t* outHandle, |
Chad Brubaker | 06801e0 | 2015-03-31 15:13:13 -0700 | [diff] [blame^] | 44 | const keymaster1_device_t** outDev, keymaster_key_blob_t* outKey); |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 45 | bool removeOperation(sp<IBinder> token); |
| 46 | bool hasPruneableOperation(); |
| 47 | sp<IBinder> getOldestPruneableOperation(); |
| 48 | std::vector<sp<IBinder>> getOperationsForToken(sp<IBinder> appToken); |
| 49 | |
| 50 | private: |
| 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 Brubaker | 06801e0 | 2015-03-31 15:13:13 -0700 | [diff] [blame^] | 56 | const keymaster_key_blob_t& key, sp<IBinder> appToken); |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 57 | keymaster_operation_handle_t handle; |
| 58 | const keymaster1_device_t* device; |
Chad Brubaker | 06801e0 | 2015-03-31 15:13:13 -0700 | [diff] [blame^] | 59 | keymaster_key_blob_t key; |
Chad Brubaker | 40a1a9b | 2015-02-20 14:08:13 -0800 | [diff] [blame] | 60 | 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 |