operation.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #define LOG_TAG "KeystoreOperation"
  17. #include "operation.h"
  18. #include <algorithm>
  19. #include <android-base/logging.h>
  20. #include <mutex>
  21. namespace keystore {
  22. OperationMap::OperationMap(IBinder::DeathRecipient* deathRecipient)
  23. : mDeathRecipient(deathRecipient) {}
  24. sp<IBinder> OperationMap::addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose,
  25. const sp<Keymaster>& dev, const sp<IBinder>& appToken,
  26. KeyCharacteristics&& characteristics,
  27. const hidl_vec<KeyParameter>& params, bool pruneable) {
  28. sp<IBinder> token = new ::android::BBinder();
  29. mMap.emplace(token, std::make_shared<Operation>(handle, keyid, purpose, dev,
  30. std::move(characteristics), appToken, params));
  31. if (pruneable) mLru.push_back(token);
  32. if (mAppTokenMap.find(appToken) == mAppTokenMap.end()) appToken->linkToDeath(mDeathRecipient);
  33. mAppTokenMap[appToken].push_back(token);
  34. return token;
  35. }
  36. std::shared_ptr<Operation> OperationMap::getOperation(const sp<IBinder>& token) {
  37. auto entry = mMap.find(token);
  38. if (entry == mMap.end()) return {};
  39. auto op = entry->second;
  40. updateLru(token);
  41. return op;
  42. }
  43. void OperationMap::updateLru(const sp<IBinder>& token) {
  44. auto lruEntry = std::find(mLru.begin(), mLru.end(), token);
  45. if (lruEntry != mLru.end()) {
  46. mLru.erase(lruEntry);
  47. mLru.push_back(token);
  48. }
  49. }
  50. std::shared_ptr<Operation> OperationMap::removeOperation(const sp<IBinder>& token,
  51. bool wasSuccessful) {
  52. auto entry = mMap.find(token);
  53. if (entry == mMap.end()) return {};
  54. auto op = entry->second;
  55. operationUploader.uploadOpAsProto(*op, wasSuccessful);
  56. mMap.erase(entry);
  57. auto lruEntry = std::find(mLru.begin(), mLru.end(), token);
  58. if (lruEntry != mLru.end()) mLru.erase(lruEntry);
  59. removeOperationTracking(token, op->appToken);
  60. return op;
  61. }
  62. void OperationMap::removeOperationTracking(const sp<IBinder>& token, const sp<IBinder>& appToken) {
  63. auto appEntry = mAppTokenMap.find(appToken);
  64. if (appEntry == mAppTokenMap.end()) {
  65. ALOGE("Entry for %p contains unmapped application token %p", token.get(), appToken.get());
  66. return;
  67. }
  68. auto tokenEntry = std::find(appEntry->second.begin(), appEntry->second.end(), token);
  69. appEntry->second.erase(tokenEntry);
  70. // Stop listening for death if all operations tied to the token have finished.
  71. if (appEntry->second.size() == 0) {
  72. appToken->unlinkToDeath(mDeathRecipient);
  73. mAppTokenMap.erase(appEntry);
  74. }
  75. }
  76. sp<IBinder> OperationMap::getOldestPruneableOperation() {
  77. if (mLru.size() == 0) return {};
  78. return {mLru.front()};
  79. }
  80. std::vector<sp<IBinder>> OperationMap::getOperationsForToken(const sp<IBinder>& appToken) {
  81. auto appEntry = mAppTokenMap.find(appToken);
  82. if (appEntry == mAppTokenMap.end()) return {};
  83. return appEntry->second;
  84. }
  85. } // namespace keystore