bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 Team Win Recovery 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 | |
| 17 | /* To the best of my knowledge there is no native implementation for |
| 18 | * Weaver so I made this by looking at the IWeaver.h file that gets |
| 19 | * compiled by the build system. I took the information from this header |
| 20 | * file and looked at keymaster source to get an idea of the proper way |
| 21 | * to write the functions. |
| 22 | */ |
| 23 | |
| 24 | #include "Weaver1.h" |
| 25 | |
| 26 | //#include <android-base/logging.h> |
| 27 | //#include <keystore/keymaster_tags.h> |
| 28 | //#include <keystore/authorization_set.h> |
| 29 | //#include <keystore/keystore_hidl_support.h> |
| 30 | |
| 31 | #include <android/hardware/weaver/1.0/IWeaver.h> |
| 32 | |
| 33 | #include <iostream> |
| 34 | #define ERROR 1 |
| 35 | #define LOG(x) std::cout |
| 36 | |
| 37 | using namespace android::hardware::weaver; |
| 38 | using android::hardware::hidl_string; |
| 39 | using ::android::hardware::weaver::V1_0::IWeaver; |
| 40 | using ::android::hardware::weaver::V1_0::WeaverConfig; |
| 41 | using ::android::hardware::weaver::V1_0::WeaverReadStatus; |
| 42 | using ::android::hardware::weaver::V1_0::WeaverReadResponse; |
| 43 | using ::android::hardware::weaver::V1_0::WeaverStatus; |
| 44 | using ::android::hardware::Return; |
| 45 | using ::android::sp; |
| 46 | |
| 47 | namespace android { |
| 48 | namespace vold { |
| 49 | |
| 50 | Weaver::Weaver() { |
| 51 | mDevice = ::android::hardware::weaver::V1_0::IWeaver::getService(); |
| 52 | GottenConfig = false; |
| 53 | } |
| 54 | |
| 55 | bool Weaver::GetConfig() { |
| 56 | if (GottenConfig) |
| 57 | return true; |
| 58 | |
| 59 | WeaverStatus status; |
| 60 | WeaverConfig cfg; |
| 61 | |
| 62 | bool callbackCalled = false; |
| 63 | auto ret = mDevice->getConfig([&](WeaverStatus s, WeaverConfig c) { |
| 64 | callbackCalled = true; |
| 65 | status = s; |
| 66 | cfg = c; |
| 67 | }); |
| 68 | if (ret.isOk() && callbackCalled && status == WeaverStatus::OK) { |
| 69 | config = cfg; |
| 70 | GottenConfig = true; |
| 71 | return true; |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | bool Weaver::GetSlots(uint32_t* slots) { |
| 77 | if (!GetConfig()) |
| 78 | return false; |
| 79 | *slots = config.slots; |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | bool Weaver::GetKeySize(uint32_t* keySize) { |
| 84 | if (!GetConfig()) |
| 85 | return false; |
| 86 | *keySize = config.keySize; |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool Weaver::GetValueSize(uint32_t* valueSize) { |
| 91 | if (!GetConfig()) |
| 92 | return false; |
| 93 | *valueSize = config.valueSize; |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | // TODO: we should return more information about the status including time delays before the next retry |
| 98 | bool Weaver::WeaverVerify(const uint32_t slot, const void* weaver_key, std::vector<uint8_t>* payload) { |
| 99 | bool callbackCalled = false; |
| 100 | WeaverReadStatus status; |
| 101 | std::vector<uint8_t> readValue; |
| 102 | uint32_t timeout; |
| 103 | uint32_t keySize; |
| 104 | if (!GetKeySize(&keySize)) |
| 105 | return false; |
| 106 | std::vector<uint8_t> key; |
| 107 | key.resize(keySize); |
| 108 | uint32_t index = 0; |
| 109 | unsigned char* ptr = (unsigned char*)weaver_key; |
| 110 | for (index = 0; index < keySize; index++) { |
| 111 | key[index] = *ptr; |
| 112 | ptr++; |
| 113 | } |
| 114 | const auto readRet = mDevice->read(slot, key, [&](WeaverReadStatus s, WeaverReadResponse r) { |
| 115 | callbackCalled = true; |
| 116 | status = s; |
| 117 | readValue = r.value; |
| 118 | timeout = r.timeout; |
| 119 | }); |
| 120 | if (readRet.isOk() && callbackCalled && status == WeaverReadStatus::OK && timeout == 0) { |
| 121 | *payload = readValue; |
| 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | } // namespace vold |
| 128 | } // namespace android |