blob: 378ee16b3d885bb80f2282e5a0cb21334927b888 [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#define LOG_TAG "hwservicemanager"
18
19#include "TokenManager.h"
20
Steven Moreland66ac4012016-12-21 15:06:10 -080021#include <functional>
Steven Moreland79a9e322017-04-06 12:32:46 -070022#include <log/log.h>
Steven Morelandd4530e42017-03-16 02:04:44 -070023#include <openssl/hmac.h>
24#include <openssl/rand.h>
Steven Moreland66ac4012016-12-21 15:06:10 -080025
26namespace android {
27namespace hidl {
28namespace token {
29namespace V1_0 {
30namespace implementation {
31
bohu69673822017-04-06 22:05:06 -070032static void ReadRandomBytes(uint8_t *buf, size_t len) {
33 int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
34 if (fd == -1) {
35 ALOGE("%s: cannot read /dev/urandom", __func__);
36 return;
37 }
38
39 size_t n;
40 while ((n = TEMP_FAILURE_RETRY(read(fd, buf, len))) > 0) {
41 len -= n;
42 buf += n;
43 }
44 if (len > 0) {
45 ALOGW("%s: there are %d bytes skipped", __func__, (int)len);
46 }
47 close(fd);
48}
49
Steven Morelandd4530e42017-03-16 02:04:44 -070050TokenManager::TokenManager() {
bohu69673822017-04-06 22:05:06 -070051 ReadRandomBytes(mKey.data(), mKey.size());
Steven Moreland66ac4012016-12-21 15:06:10 -080052}
53
Steven Morelandd4530e42017-03-16 02:04:44 -070054// Methods from ::android::hidl::token::V1_0::ITokenManager follow.
55Return<void> TokenManager::createToken(const sp<IBase>& store, createToken_cb hidl_cb) {
56 TokenInterface interface = generateToken(store);
57
58 if (interface.interface == nullptr) {
59 hidl_cb({});
60 return Void();
61 }
62
63 uint64_t id = getTokenId(interface.token);
64
Steven Morelandff07cad2018-12-21 12:38:52 -080065 if (id != interface.id) {
66 ALOGE("Token creation failed.");
67 hidl_cb({});
68 return Void();
69 }
70
Steven Morelandd4530e42017-03-16 02:04:44 -070071 if (id == TOKEN_ID_NONE) {
72 hidl_cb({});
73 return Void();
74 }
75
76 mMap[id] = interface;
77
78 hidl_cb(interface.token);
79 return Void();
80}
81
82std::unordered_map<uint64_t, TokenManager::TokenInterface>::const_iterator
83 TokenManager::lookupToken(const hidl_vec<uint8_t> &token) {
84 uint64_t tokenId = getTokenId(token);
85
86 if (tokenId == TOKEN_ID_NONE) {
87 return mMap.end();
88 }
89
90 auto it = mMap.find(tokenId);
91
92 if (it == mMap.end()) {
93 return mMap.end();
94 }
95
96 const TokenInterface &interface = it->second;
97
98 if (!constantTimeCompare(token, interface.token)) {
99 ALOGE("Fetch of token with invalid hash.");
100 return mMap.end();
101 }
102
103 return it;
104}
105
106Return<bool> TokenManager::unregister(const hidl_vec<uint8_t> &token) {
107 auto it = lookupToken(token);
Steven Moreland66ac4012016-12-21 15:06:10 -0800108
109 if (it == mMap.end()) {
110 return false;
111 }
112
113 mMap.erase(it);
114 return true;
115}
116
Steven Morelandd4530e42017-03-16 02:04:44 -0700117Return<sp<IBase>> TokenManager::get(const hidl_vec<uint8_t> &token) {
118 auto it = lookupToken(token);
Steven Moreland66ac4012016-12-21 15:06:10 -0800119
120 if (it == mMap.end()) {
Martijn Coenen7b02bf92017-01-02 15:17:58 +0100121 return nullptr;
Steven Moreland66ac4012016-12-21 15:06:10 -0800122 }
123
Steven Morelandd4530e42017-03-16 02:04:44 -0700124 return it->second.interface;
Steven Moreland66ac4012016-12-21 15:06:10 -0800125}
126
Steven Morelandd4530e42017-03-16 02:04:44 -0700127
128TokenManager::TokenInterface TokenManager::generateToken(const sp<IBase> &interface) {
129 uint64_t id = ++mTokenIndex;
130
131 std::array<uint8_t, EVP_MAX_MD_SIZE> hmac;
132 uint32_t hmacSize;
133
134 uint8_t *hmacOut = HMAC(EVP_sha256(),
135 mKey.data(), mKey.size(),
Steven Morelandff07cad2018-12-21 12:38:52 -0800136 (uint8_t*) &id, sizeof(id),
Steven Morelandd4530e42017-03-16 02:04:44 -0700137 hmac.data(), &hmacSize);
138
139 if (hmacOut == nullptr ||
140 hmacOut != hmac.data()) {
141 ALOGE("Generating token failed, got %p.", hmacOut);
Steven Morelandff07cad2018-12-21 12:38:52 -0800142 return { nullptr, TOKEN_ID_NONE, {} };
Steven Morelandd4530e42017-03-16 02:04:44 -0700143 }
144
145 // only care about the first HMAC_SIZE bytes of the HMAC
Steven Morelandff07cad2018-12-21 12:38:52 -0800146 const hidl_vec<uint8_t> &token = makeToken(id, hmac.data(), hmacSize);
Steven Morelandd4530e42017-03-16 02:04:44 -0700147
Steven Morelandff07cad2018-12-21 12:38:52 -0800148 return { interface, id, token };
Steven Moreland66ac4012016-12-21 15:06:10 -0800149}
150
Steven Morelandd4530e42017-03-16 02:04:44 -0700151__attribute__((optnone))
152bool TokenManager::constantTimeCompare(const hidl_vec<uint8_t> &t1, const hidl_vec<uint8_t> &t2) {
153 if (t1.size() != t2.size()) {
154 return false;
155 }
156
157 uint8_t x = 0;
158 for (size_t i = 0; i < t1.size(); i++) {
159 x |= t1[i] ^ t2[i];
160 }
161
162 return x == 0;
163}
164
Steven Morelandd4530e42017-03-16 02:04:44 -0700165uint64_t TokenManager::getTokenId(const hidl_vec<uint8_t> &token) {
Steven Morelandff07cad2018-12-21 12:38:52 -0800166 uint64_t id = 0;
167
168 if (token.size() < sizeof(id)) {
Steven Morelandd4530e42017-03-16 02:04:44 -0700169 return TOKEN_ID_NONE;
170 }
171
Steven Morelandff07cad2018-12-21 12:38:52 -0800172 memcpy(&id, token.data(), sizeof(id));
Steven Morelandd4530e42017-03-16 02:04:44 -0700173
174 return id;
175}
176
Steven Morelandff07cad2018-12-21 12:38:52 -0800177hidl_vec<uint8_t> TokenManager::makeToken(const uint64_t id, const uint8_t *hmac, uint64_t hmacSize) {
Steven Morelandd4530e42017-03-16 02:04:44 -0700178 hidl_vec<uint8_t> token;
Steven Morelandff07cad2018-12-21 12:38:52 -0800179 token.resize(sizeof(id) + hmacSize);
Steven Morelandd4530e42017-03-16 02:04:44 -0700180
Steven Morelandff07cad2018-12-21 12:38:52 -0800181 memcpy(token.data(), &id, sizeof(id));
182 memcpy(token.data() + sizeof(id), hmac, hmacSize);
Steven Morelandd4530e42017-03-16 02:04:44 -0700183
184 return token;
185}
186
187
Steven Moreland66ac4012016-12-21 15:06:10 -0800188} // namespace implementation
189} // namespace V1_0
190} // namespace token
191} // namespace hidl
192} // namespace android