Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 1 | #define LOG_TAG "hwservicemanager" |
| 2 | |
| 3 | #include "TokenManager.h" |
| 4 | |
| 5 | #include <android-base/logging.h> |
| 6 | #include <functional> |
Steven Moreland | 79a9e32 | 2017-04-06 12:32:46 -0700 | [diff] [blame] | 7 | #include <log/log.h> |
Steven Moreland | d4530e4 | 2017-03-16 02:04:44 -0700 | [diff] [blame] | 8 | #include <openssl/hmac.h> |
| 9 | #include <openssl/rand.h> |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 10 | |
| 11 | namespace android { |
| 12 | namespace hidl { |
| 13 | namespace token { |
| 14 | namespace V1_0 { |
| 15 | namespace implementation { |
| 16 | |
bohu | 6967382 | 2017-04-06 22:05:06 -0700 | [diff] [blame] | 17 | static void ReadRandomBytes(uint8_t *buf, size_t len) { |
| 18 | int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
| 19 | if (fd == -1) { |
| 20 | ALOGE("%s: cannot read /dev/urandom", __func__); |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | size_t n; |
| 25 | while ((n = TEMP_FAILURE_RETRY(read(fd, buf, len))) > 0) { |
| 26 | len -= n; |
| 27 | buf += n; |
| 28 | } |
| 29 | if (len > 0) { |
| 30 | ALOGW("%s: there are %d bytes skipped", __func__, (int)len); |
| 31 | } |
| 32 | close(fd); |
| 33 | } |
| 34 | |
Steven Moreland | d4530e4 | 2017-03-16 02:04:44 -0700 | [diff] [blame] | 35 | TokenManager::TokenManager() { |
bohu | 6967382 | 2017-04-06 22:05:06 -0700 | [diff] [blame] | 36 | ReadRandomBytes(mKey.data(), mKey.size()); |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Steven Moreland | d4530e4 | 2017-03-16 02:04:44 -0700 | [diff] [blame] | 39 | // Methods from ::android::hidl::token::V1_0::ITokenManager follow. |
| 40 | Return<void> TokenManager::createToken(const sp<IBase>& store, createToken_cb hidl_cb) { |
| 41 | TokenInterface interface = generateToken(store); |
| 42 | |
| 43 | if (interface.interface == nullptr) { |
| 44 | hidl_cb({}); |
| 45 | return Void(); |
| 46 | } |
| 47 | |
| 48 | uint64_t id = getTokenId(interface.token); |
| 49 | |
| 50 | if (id == TOKEN_ID_NONE) { |
| 51 | hidl_cb({}); |
| 52 | return Void(); |
| 53 | } |
| 54 | |
| 55 | mMap[id] = interface; |
| 56 | |
| 57 | hidl_cb(interface.token); |
| 58 | return Void(); |
| 59 | } |
| 60 | |
| 61 | std::unordered_map<uint64_t, TokenManager::TokenInterface>::const_iterator |
| 62 | TokenManager::lookupToken(const hidl_vec<uint8_t> &token) { |
| 63 | uint64_t tokenId = getTokenId(token); |
| 64 | |
| 65 | if (tokenId == TOKEN_ID_NONE) { |
| 66 | return mMap.end(); |
| 67 | } |
| 68 | |
| 69 | auto it = mMap.find(tokenId); |
| 70 | |
| 71 | if (it == mMap.end()) { |
| 72 | return mMap.end(); |
| 73 | } |
| 74 | |
| 75 | const TokenInterface &interface = it->second; |
| 76 | |
| 77 | if (!constantTimeCompare(token, interface.token)) { |
| 78 | ALOGE("Fetch of token with invalid hash."); |
| 79 | return mMap.end(); |
| 80 | } |
| 81 | |
| 82 | return it; |
| 83 | } |
| 84 | |
| 85 | Return<bool> TokenManager::unregister(const hidl_vec<uint8_t> &token) { |
| 86 | auto it = lookupToken(token); |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 87 | |
| 88 | if (it == mMap.end()) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | mMap.erase(it); |
| 93 | return true; |
| 94 | } |
| 95 | |
Steven Moreland | d4530e4 | 2017-03-16 02:04:44 -0700 | [diff] [blame] | 96 | Return<sp<IBase>> TokenManager::get(const hidl_vec<uint8_t> &token) { |
| 97 | auto it = lookupToken(token); |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 98 | |
| 99 | if (it == mMap.end()) { |
Martijn Coenen | 7b02bf9 | 2017-01-02 15:17:58 +0100 | [diff] [blame] | 100 | return nullptr; |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Steven Moreland | d4530e4 | 2017-03-16 02:04:44 -0700 | [diff] [blame] | 103 | return it->second.interface; |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Steven Moreland | d4530e4 | 2017-03-16 02:04:44 -0700 | [diff] [blame] | 106 | |
| 107 | TokenManager::TokenInterface TokenManager::generateToken(const sp<IBase> &interface) { |
| 108 | uint64_t id = ++mTokenIndex; |
| 109 | |
| 110 | std::array<uint8_t, EVP_MAX_MD_SIZE> hmac; |
| 111 | uint32_t hmacSize; |
| 112 | |
| 113 | uint8_t *hmacOut = HMAC(EVP_sha256(), |
| 114 | mKey.data(), mKey.size(), |
| 115 | (uint8_t*) &id, ID_SIZE, |
| 116 | hmac.data(), &hmacSize); |
| 117 | |
| 118 | if (hmacOut == nullptr || |
| 119 | hmacOut != hmac.data()) { |
| 120 | ALOGE("Generating token failed, got %p.", hmacOut); |
| 121 | return { nullptr, {} }; |
| 122 | } |
| 123 | |
| 124 | // only care about the first HMAC_SIZE bytes of the HMAC |
| 125 | const hidl_vec<uint8_t> &token = TokenManager::getToken(id, hmac.data(), hmacSize); |
| 126 | |
| 127 | return { interface, token }; |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Steven Moreland | d4530e4 | 2017-03-16 02:04:44 -0700 | [diff] [blame] | 130 | __attribute__((optnone)) |
| 131 | bool TokenManager::constantTimeCompare(const hidl_vec<uint8_t> &t1, const hidl_vec<uint8_t> &t2) { |
| 132 | if (t1.size() != t2.size()) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | uint8_t x = 0; |
| 137 | for (size_t i = 0; i < t1.size(); i++) { |
| 138 | x |= t1[i] ^ t2[i]; |
| 139 | } |
| 140 | |
| 141 | return x == 0; |
| 142 | } |
| 143 | |
Steven Moreland | d4530e4 | 2017-03-16 02:04:44 -0700 | [diff] [blame] | 144 | uint64_t TokenManager::getTokenId(const hidl_vec<uint8_t> &token) { |
| 145 | if (token.size() < ID_SIZE) { |
| 146 | return TOKEN_ID_NONE; |
| 147 | } |
| 148 | |
| 149 | uint64_t id = 0; |
| 150 | for (size_t i = 0; i < ID_SIZE; i++) { |
| 151 | id |= token[i] << i; |
| 152 | } |
| 153 | |
| 154 | return id; |
| 155 | } |
| 156 | |
Steven Moreland | d4530e4 | 2017-03-16 02:04:44 -0700 | [diff] [blame] | 157 | hidl_vec<uint8_t> TokenManager::getToken(const uint64_t id, const uint8_t *hmac, uint64_t hmacSize) { |
| 158 | hidl_vec<uint8_t> token; |
| 159 | token.resize(ID_SIZE + hmacSize); |
| 160 | |
| 161 | for (size_t i = 0; i < ID_SIZE; i++) { |
| 162 | token[i] = (id >> i) & 0xFF; |
| 163 | } |
| 164 | |
| 165 | for (size_t i = 0; i < hmacSize; i++) { |
| 166 | token[i + ID_SIZE] = hmac[i]; |
| 167 | } |
| 168 | |
| 169 | return token; |
| 170 | } |
| 171 | |
| 172 | |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 173 | } // namespace implementation |
| 174 | } // namespace V1_0 |
| 175 | } // namespace token |
| 176 | } // namespace hidl |
| 177 | } // namespace android |