blob: 416b795b311b87a836c86a9d66235f096ef82692 [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 Morelandd5f8f082019-06-06 12:59:20 -070017#pragma once
Steven Moreland66ac4012016-12-21 15:06:10 -080018
19#include <android/hidl/token/1.0/ITokenManager.h>
20#include <chrono>
21#include <hidl/MQDescriptor.h>
22#include <hidl/Status.h>
23#include <unordered_map>
Steven Moreland78d1d052017-03-16 02:04:44 -070024#include <array>
Steven Moreland66ac4012016-12-21 15:06:10 -080025
26namespace android {
27namespace hidl {
28namespace token {
29namespace V1_0 {
30namespace implementation {
31
32using ::android::hidl::base::V1_0::IBase;
33using ::android::hidl::token::V1_0::ITokenManager;
34using ::android::hardware::hidl_array;
35using ::android::hardware::hidl_string;
36using ::android::hardware::hidl_vec;
37using ::android::hardware::Return;
38using ::android::hardware::Void;
39using ::android::sp;
40
41struct TokenManager : public ITokenManager {
Steven Moreland78d1d052017-03-16 02:04:44 -070042 TokenManager();
43
Steven Moreland66ac4012016-12-21 15:06:10 -080044 // Methods from ::android::hidl::token::V1_0::ITokenManager follow.
Steven Moreland78d1d052017-03-16 02:04:44 -070045 Return<void> createToken(const sp<IBase>& store, createToken_cb hidl_cb) override;
46 Return<bool> unregister(const hidl_vec<uint8_t> &token) override;
47 Return<sp<IBase>> get(const hidl_vec<uint8_t> &token) override;
Steven Moreland66ac4012016-12-21 15:06:10 -080048
49private:
Steven Moreland78d1d052017-03-16 02:04:44 -070050 static constexpr uint64_t KEY_SIZE = 16;
Steven Moreland66ac4012016-12-21 15:06:10 -080051
Steven Moreland78d1d052017-03-16 02:04:44 -070052 static constexpr uint64_t TOKEN_ID_NONE = 0;
Steven Moreland66ac4012016-12-21 15:06:10 -080053
Steven Moreland78d1d052017-03-16 02:04:44 -070054 static bool constantTimeCompare(const hidl_vec<uint8_t> &t1, const hidl_vec<uint8_t> &t2);
55
Steven Morelandff07cad2018-12-21 12:38:52 -080056 static hidl_vec<uint8_t> makeToken(const uint64_t id, const uint8_t *hmac, uint64_t hmacSize);
Steven Moreland78d1d052017-03-16 02:04:44 -070057 static uint64_t getTokenId(const hidl_vec<uint8_t> &token);
58
59 std::array<uint8_t, KEY_SIZE> mKey;
60
61 struct TokenInterface {
62 sp<IBase> interface;
Steven Morelandff07cad2018-12-21 12:38:52 -080063 uint64_t id;
Steven Moreland78d1d052017-03-16 02:04:44 -070064 hidl_vec<uint8_t> token; // First eight bytes are tokenId. Remaining bytes are hmac.
65 };
66
67 TokenInterface generateToken(const sp<IBase> &interface);
68
69 // verifies token, returns iterator into mMap
70 std::unordered_map<uint64_t, TokenInterface>::const_iterator
71 lookupToken(const hidl_vec<uint8_t> &token);
72
73 std::unordered_map<uint64_t, TokenInterface> mMap; // map getTokenId(i.token) -> i
74 uint64_t mTokenIndex = TOKEN_ID_NONE; // last token index
Steven Moreland66ac4012016-12-21 15:06:10 -080075};
76
77} // namespace implementation
78} // namespace V1_0
79} // namespace token
80} // namespace hidl
81} // namespace android