operation.h 2.7 KB

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