TokenManager.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (C) 2016 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 ANDROID_HIDL_TOKEN_V1_0_TOKENMANAGER_H
  17. #define ANDROID_HIDL_TOKEN_V1_0_TOKENMANAGER_H
  18. #include <android/hidl/token/1.0/ITokenManager.h>
  19. #include <chrono>
  20. #include <hidl/MQDescriptor.h>
  21. #include <hidl/Status.h>
  22. #include <unordered_map>
  23. #include <array>
  24. namespace android {
  25. namespace hidl {
  26. namespace token {
  27. namespace V1_0 {
  28. namespace implementation {
  29. using ::android::hidl::base::V1_0::IBase;
  30. using ::android::hidl::token::V1_0::ITokenManager;
  31. using ::android::hardware::hidl_array;
  32. using ::android::hardware::hidl_string;
  33. using ::android::hardware::hidl_vec;
  34. using ::android::hardware::Return;
  35. using ::android::hardware::Void;
  36. using ::android::sp;
  37. struct TokenManager : public ITokenManager {
  38. TokenManager();
  39. // Methods from ::android::hidl::token::V1_0::ITokenManager follow.
  40. Return<void> createToken(const sp<IBase>& store, createToken_cb hidl_cb) override;
  41. Return<bool> unregister(const hidl_vec<uint8_t> &token) override;
  42. Return<sp<IBase>> get(const hidl_vec<uint8_t> &token) override;
  43. private:
  44. static constexpr uint64_t KEY_SIZE = 16;
  45. static constexpr uint64_t TOKEN_ID_NONE = 0;
  46. static bool constantTimeCompare(const hidl_vec<uint8_t> &t1, const hidl_vec<uint8_t> &t2);
  47. static hidl_vec<uint8_t> makeToken(const uint64_t id, const uint8_t *hmac, uint64_t hmacSize);
  48. static uint64_t getTokenId(const hidl_vec<uint8_t> &token);
  49. std::array<uint8_t, KEY_SIZE> mKey;
  50. struct TokenInterface {
  51. sp<IBase> interface;
  52. uint64_t id;
  53. hidl_vec<uint8_t> token; // First eight bytes are tokenId. Remaining bytes are hmac.
  54. };
  55. TokenInterface generateToken(const sp<IBase> &interface);
  56. // verifies token, returns iterator into mMap
  57. std::unordered_map<uint64_t, TokenInterface>::const_iterator
  58. lookupToken(const hidl_vec<uint8_t> &token);
  59. std::unordered_map<uint64_t, TokenInterface> mMap; // map getTokenId(i.token) -> i
  60. uint64_t mTokenIndex = TOKEN_ID_NONE; // last token index
  61. };
  62. } // namespace implementation
  63. } // namespace V1_0
  64. } // namespace token
  65. } // namespace hidl
  66. } // namespace android
  67. #endif // ANDROID_HIDL_TOKEN_V1_0_TOKENMANAGER_H