blob: d82f841c5cdd16c04758281570c54e8695316b8b [file] [log] [blame]
Steven Morelandfe66b732019-02-01 14:29:45 -08001/*
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
Steven Moreland66ac4012016-12-21 15:06:10 -080017#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>
Steven Moreland78d1d052017-03-16 02:04:44 -070025#include <array>
Steven Moreland66ac4012016-12-21 15:06:10 -080026
27namespace android {
28namespace hidl {
29namespace token {
30namespace V1_0 {
31namespace implementation {
32
33using ::android::hidl::base::V1_0::IBase;
34using ::android::hidl::token::V1_0::ITokenManager;
35using ::android::hardware::hidl_array;
36using ::android::hardware::hidl_string;
37using ::android::hardware::hidl_vec;
38using ::android::hardware::Return;
39using ::android::hardware::Void;
40using ::android::sp;
41
42struct TokenManager : public ITokenManager {
Steven Moreland78d1d052017-03-16 02:04:44 -070043 TokenManager();
44
Steven Moreland66ac4012016-12-21 15:06:10 -080045 // Methods from ::android::hidl::token::V1_0::ITokenManager follow.
Steven Moreland78d1d052017-03-16 02:04:44 -070046 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;
Steven Moreland66ac4012016-12-21 15:06:10 -080049
50private:
Steven Moreland78d1d052017-03-16 02:04:44 -070051 static constexpr uint64_t KEY_SIZE = 16;
Steven Moreland66ac4012016-12-21 15:06:10 -080052
Steven Moreland78d1d052017-03-16 02:04:44 -070053 static constexpr uint64_t TOKEN_ID_NONE = 0;
Steven Moreland66ac4012016-12-21 15:06:10 -080054
Steven Moreland78d1d052017-03-16 02:04:44 -070055 static bool constantTimeCompare(const hidl_vec<uint8_t> &t1, const hidl_vec<uint8_t> &t2);
56
Steven Morelandff07cad2018-12-21 12:38:52 -080057 static hidl_vec<uint8_t> makeToken(const uint64_t id, const uint8_t *hmac, uint64_t hmacSize);
Steven Moreland78d1d052017-03-16 02:04:44 -070058 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;
Steven Morelandff07cad2018-12-21 12:38:52 -080064 uint64_t id;
Steven Moreland78d1d052017-03-16 02:04:44 -070065 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
Steven Moreland66ac4012016-12-21 15:06:10 -080076};
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