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