/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef KEYSTORE_OPERATION_H_ #define KEYSTORE_OPERATION_H_ #include #include #include #include #include #include #include namespace keystore { using ::android::IBinder; using ::android::sp; /** * OperationMap handles the translation of uint64_t's and keymaster2_device_t's to opaque binder * tokens that can be used to reference that operation at a later time by applications. It also does * LRU tracking for operation pruning and keeps a mapping of clients to operations to allow for * graceful handling of application death. */ class OperationMap { typedef ::android::sp<::android::hardware::keymaster::V3_0::IKeymasterDevice> km_device_t; public: explicit OperationMap(IBinder::DeathRecipient* deathRecipient); android::sp addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose, const km_device_t& dev, const android::sp& appToken, KeyCharacteristics&& characteristics, bool pruneable); bool getOperation(const android::sp& token, uint64_t* outHandle, uint64_t* outKeyid, KeyPurpose* outPurpose, km_device_t* outDev, const KeyCharacteristics** outCharacteristics); bool removeOperation(const android::sp& token); bool hasPruneableOperation() const; size_t getOperationCount() const { return mMap.size(); } size_t getPruneableOperationCount() const; bool getOperationAuthToken(const android::sp& token, const HardwareAuthToken** outToken); bool setOperationAuthToken(const android::sp& token, const HardwareAuthToken* authToken); android::sp getOldestPruneableOperation(); std::vector> getOperationsForToken(const android::sp& appToken); private: void updateLru(const android::sp& token); void removeOperationTracking(const android::sp& token, const android::sp& appToken); struct Operation { Operation(); Operation(uint64_t handle, uint64_t keyid, KeyPurpose purpose, const km_device_t& device, KeyCharacteristics&& characteristics, android::sp appToken); uint64_t handle; uint64_t keyid; KeyPurpose purpose; km_device_t device; KeyCharacteristics characteristics; android::sp appToken; std::unique_ptr authToken; }; std::map, Operation> mMap; std::vector> mLru; std::map, std::vector>> mAppTokenMap; android::IBinder::DeathRecipient* mDeathRecipient; }; } // namespace keystore #endif