TokenManager.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #define LOG_TAG "hwservicemanager"
  17. #include "TokenManager.h"
  18. #include <functional>
  19. #include <log/log.h>
  20. #include <openssl/hmac.h>
  21. #include <openssl/rand.h>
  22. namespace android {
  23. namespace hidl {
  24. namespace token {
  25. namespace V1_0 {
  26. namespace implementation {
  27. static void ReadRandomBytes(uint8_t *buf, size_t len) {
  28. int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
  29. if (fd == -1) {
  30. ALOGE("%s: cannot read /dev/urandom", __func__);
  31. return;
  32. }
  33. size_t n;
  34. while ((n = TEMP_FAILURE_RETRY(read(fd, buf, len))) > 0) {
  35. len -= n;
  36. buf += n;
  37. }
  38. if (len > 0) {
  39. ALOGW("%s: there are %d bytes skipped", __func__, (int)len);
  40. }
  41. close(fd);
  42. }
  43. TokenManager::TokenManager() {
  44. ReadRandomBytes(mKey.data(), mKey.size());
  45. }
  46. // Methods from ::android::hidl::token::V1_0::ITokenManager follow.
  47. Return<void> TokenManager::createToken(const sp<IBase>& store, createToken_cb hidl_cb) {
  48. TokenInterface interface = generateToken(store);
  49. if (interface.interface == nullptr) {
  50. hidl_cb({});
  51. return Void();
  52. }
  53. uint64_t id = getTokenId(interface.token);
  54. if (id != interface.id) {
  55. ALOGE("Token creation failed.");
  56. hidl_cb({});
  57. return Void();
  58. }
  59. if (id == TOKEN_ID_NONE) {
  60. hidl_cb({});
  61. return Void();
  62. }
  63. mMap[id] = interface;
  64. hidl_cb(interface.token);
  65. return Void();
  66. }
  67. std::unordered_map<uint64_t, TokenManager::TokenInterface>::const_iterator
  68. TokenManager::lookupToken(const hidl_vec<uint8_t> &token) {
  69. uint64_t tokenId = getTokenId(token);
  70. if (tokenId == TOKEN_ID_NONE) {
  71. return mMap.end();
  72. }
  73. auto it = mMap.find(tokenId);
  74. if (it == mMap.end()) {
  75. return mMap.end();
  76. }
  77. const TokenInterface &interface = it->second;
  78. if (!constantTimeCompare(token, interface.token)) {
  79. ALOGE("Fetch of token with invalid hash.");
  80. return mMap.end();
  81. }
  82. return it;
  83. }
  84. Return<bool> TokenManager::unregister(const hidl_vec<uint8_t> &token) {
  85. auto it = lookupToken(token);
  86. if (it == mMap.end()) {
  87. return false;
  88. }
  89. mMap.erase(it);
  90. return true;
  91. }
  92. Return<sp<IBase>> TokenManager::get(const hidl_vec<uint8_t> &token) {
  93. auto it = lookupToken(token);
  94. if (it == mMap.end()) {
  95. return nullptr;
  96. }
  97. return it->second.interface;
  98. }
  99. TokenManager::TokenInterface TokenManager::generateToken(const sp<IBase> &interface) {
  100. uint64_t id = ++mTokenIndex;
  101. std::array<uint8_t, EVP_MAX_MD_SIZE> hmac;
  102. uint32_t hmacSize;
  103. uint8_t *hmacOut = HMAC(EVP_sha256(),
  104. mKey.data(), mKey.size(),
  105. (uint8_t*) &id, sizeof(id),
  106. hmac.data(), &hmacSize);
  107. if (hmacOut == nullptr ||
  108. hmacOut != hmac.data()) {
  109. ALOGE("Generating token failed, got %p.", hmacOut);
  110. return { nullptr, TOKEN_ID_NONE, {} };
  111. }
  112. // only care about the first HMAC_SIZE bytes of the HMAC
  113. const hidl_vec<uint8_t> &token = makeToken(id, hmac.data(), hmacSize);
  114. return { interface, id, token };
  115. }
  116. __attribute__((optnone))
  117. bool TokenManager::constantTimeCompare(const hidl_vec<uint8_t> &t1, const hidl_vec<uint8_t> &t2) {
  118. if (t1.size() != t2.size()) {
  119. return false;
  120. }
  121. uint8_t x = 0;
  122. for (size_t i = 0; i < t1.size(); i++) {
  123. x |= t1[i] ^ t2[i];
  124. }
  125. return x == 0;
  126. }
  127. uint64_t TokenManager::getTokenId(const hidl_vec<uint8_t> &token) {
  128. uint64_t id = 0;
  129. if (token.size() < sizeof(id)) {
  130. return TOKEN_ID_NONE;
  131. }
  132. memcpy(&id, token.data(), sizeof(id));
  133. return id;
  134. }
  135. hidl_vec<uint8_t> TokenManager::makeToken(const uint64_t id, const uint8_t *hmac, uint64_t hmacSize) {
  136. hidl_vec<uint8_t> token;
  137. token.resize(sizeof(id) + hmacSize);
  138. memcpy(token.data(), &id, sizeof(id));
  139. memcpy(token.data() + sizeof(id), hmac, hmacSize);
  140. return token;
  141. }
  142. } // namespace implementation
  143. } // namespace V1_0
  144. } // namespace token
  145. } // namespace hidl
  146. } // namespace android