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