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 <list> 21 #include <map> 22 #include <memory> 23 #include <mutex> 24 #include <optional> 25 #include <vector> 26 27 #include <binder/Binder.h> 28 #include <binder/IBinder.h> 29 #include <keymasterV4_1/Keymaster.h> 30 #include <utils/StrongPointer.h> 31 32 #include <keystore/keymaster_types.h> 33 #include <keystore/keystore_concurrency.h> 34 #include <keystore/keystore_hidl_support.h> 35 36 #include "operation_struct.h" 37 38 namespace keystore { 39 40 using ::android::IBinder; 41 using ::android::sp; 42 using keymaster::support::Keymaster; 43 44 /** 45 * OperationMap handles the translation of uint64_t's and keymaster2_device_t's to opaque binder 46 * tokens that can be used to reference that operation at a later time by applications. It also does 47 * LRU tracking for operation pruning and keeps a mapping of clients to operations to allow for 48 * graceful handling of application death. 49 */ 50 51 class OperationMap { 52 public: 53 explicit OperationMap(IBinder::DeathRecipient* deathRecipient); 54 sp<IBinder> addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose, 55 const sp<Keymaster>& dev, const sp<IBinder>& appToken, 56 KeyCharacteristics&& characteristics, 57 const hidl_vec<KeyParameter>& params, bool pruneable); 58 std::shared_ptr<Operation> getOperation(const sp<IBinder>& token); 59 std::shared_ptr<Operation> removeOperation(const sp<IBinder>& token, bool wasSuccessful, 60 int32_t responseCode); getOperationCount()61 size_t getOperationCount() const { return mMap.size(); } 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); 68 69 std::map<sp<IBinder>, std::shared_ptr<Operation>> mMap; 70 std::list<sp<IBinder>> mLru; 71 std::map<sp<IBinder>, std::vector<sp<IBinder>>> mAppTokenMap; 72 IBinder::DeathRecipient* mDeathRecipient; 73 }; 74 75 } // namespace keystore 76 77 #endif 78