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_proto_handler.h" 37 #include "operation_struct.h" 38 39 namespace keystore { 40 41 using ::android::IBinder; 42 using ::android::sp; 43 using keymaster::support::Keymaster; 44 45 /** 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. 50 */ 51 52 class OperationMap { 53 public: 54 explicit OperationMap(IBinder::DeathRecipient* deathRecipient); 55 sp<IBinder> addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose, 56 const sp<Keymaster>& dev, const sp<IBinder>& appToken, 57 KeyCharacteristics&& characteristics, 58 const hidl_vec<KeyParameter>& params, bool pruneable); 59 std::shared_ptr<Operation> getOperation(const sp<IBinder>& token); 60 std::shared_ptr<Operation> removeOperation(const sp<IBinder>& token, bool wasSuccessful); 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 OperationProtoHandler operationUploader; 74 }; 75 76 } // namespace keystore 77 78 #endif 79