Alexey Polyudov | 586a32f | 2016-08-29 12:08:44 -0700 | [diff] [blame^] | 1 | #define LOG_TAG "android.hardware.gatekeeper@1.0-service" |
| 2 | |
| 3 | #include <utils/Log.h> |
| 4 | #include <dlfcn.h> |
| 5 | |
| 6 | #include "Gatekeeper.h" |
| 7 | |
| 8 | namespace android { |
| 9 | namespace hardware { |
| 10 | namespace gatekeeper { |
| 11 | namespace V1_0 { |
| 12 | namespace implementation { |
| 13 | |
| 14 | Gatekeeper::Gatekeeper() |
| 15 | { |
| 16 | int ret = hw_get_module_by_class(GATEKEEPER_HARDWARE_MODULE_ID, NULL, &module); |
| 17 | device = NULL; |
| 18 | |
| 19 | if (!ret) { |
| 20 | ret = gatekeeper_open(module, &device); |
| 21 | } |
| 22 | if (ret < 0) { |
| 23 | LOG_ALWAYS_FATAL_IF(ret < 0, "Unable to open GateKeeper HAL"); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | Gatekeeper::~Gatekeeper() |
| 28 | { |
| 29 | if (device != nullptr) { |
| 30 | int ret = gatekeeper_close(device); |
| 31 | if (ret < 0) { |
| 32 | ALOGE("Unable to close GateKeeper HAL"); |
| 33 | } |
| 34 | } |
| 35 | dlclose(module->dso); |
| 36 | } |
| 37 | |
| 38 | // Methods from ::android::hardware::gatekeeper::V1_0::IGatekeeper follow. |
| 39 | Return<void> Gatekeeper::enroll(uint32_t uid, |
| 40 | const hidl_vec<uint8_t>& currentPasswordHandle, |
| 41 | const hidl_vec<uint8_t>& currentPassword, |
| 42 | const hidl_vec<uint8_t>& desiredPassword, |
| 43 | enroll_cb cb) |
| 44 | { |
| 45 | GatekeeperResponse rsp; |
| 46 | uint8_t *enrolled_password_handle = nullptr; |
| 47 | uint32_t enrolled_password_handle_length = 0; |
| 48 | |
| 49 | int ret = device->enroll(device, uid, |
| 50 | currentPasswordHandle.data(), currentPasswordHandle.size(), |
| 51 | currentPassword.data(), currentPassword.size(), |
| 52 | desiredPassword.data(), desiredPassword.size(), |
| 53 | &enrolled_password_handle, &enrolled_password_handle_length); |
| 54 | if (!ret) { |
| 55 | rsp.data.setToExternal(enrolled_password_handle, |
| 56 | enrolled_password_handle_length, |
| 57 | true); |
| 58 | rsp.code = GatekeeperStatusCode::STATUS_OK; |
| 59 | } else if (ret > 0) { |
| 60 | rsp.timeout = ret; |
| 61 | rsp.code = GatekeeperStatusCode::ERROR_RETRY_TIMEOUT; |
| 62 | } else { |
| 63 | rsp.code = GatekeeperStatusCode::ERROR_GENERAL_FAILURE; |
| 64 | } |
| 65 | cb(rsp); |
| 66 | return Void(); |
| 67 | } |
| 68 | |
| 69 | Return<void> Gatekeeper::verify(uint32_t uid, |
| 70 | uint64_t challenge, |
| 71 | const hidl_vec<uint8_t>& enrolledPasswordHandle, |
| 72 | const hidl_vec<uint8_t>& providedPassword, |
| 73 | verify_cb cb) |
| 74 | { |
| 75 | GatekeeperResponse rsp; |
| 76 | uint8_t *auth_token = nullptr; |
| 77 | uint32_t auth_token_length = 0; |
| 78 | bool request_reenroll = false; |
| 79 | |
| 80 | int ret = device->verify(device, uid, challenge, |
| 81 | enrolledPasswordHandle.data(), enrolledPasswordHandle.size(), |
| 82 | providedPassword.data(), providedPassword.size(), |
| 83 | &auth_token, &auth_token_length, |
| 84 | &request_reenroll); |
| 85 | if (!ret) { |
| 86 | rsp.data.setToExternal(auth_token, auth_token_length, true); |
| 87 | if (request_reenroll) { |
| 88 | rsp.code = GatekeeperStatusCode::STATUS_REENROLL; |
| 89 | } else { |
| 90 | rsp.code = GatekeeperStatusCode::STATUS_OK; |
| 91 | } |
| 92 | } else if (ret > 0) { |
| 93 | rsp.timeout = ret; |
| 94 | rsp.code = GatekeeperStatusCode::ERROR_RETRY_TIMEOUT; |
| 95 | } else { |
| 96 | rsp.code = GatekeeperStatusCode::ERROR_GENERAL_FAILURE; |
| 97 | } |
| 98 | cb(rsp); |
| 99 | return Void(); |
| 100 | } |
| 101 | |
| 102 | Return<void> Gatekeeper::deleteUser(uint32_t uid, deleteUser_cb cb) { |
| 103 | GatekeeperResponse rsp; |
| 104 | |
| 105 | if (device->delete_user != nullptr) { |
| 106 | int ret = device->delete_user(device, uid); |
| 107 | if (!ret) { |
| 108 | rsp.code = GatekeeperStatusCode::STATUS_OK; |
| 109 | } else if (ret > 0) { |
| 110 | rsp.timeout = ret; |
| 111 | rsp.code = GatekeeperStatusCode::ERROR_RETRY_TIMEOUT; |
| 112 | } else { |
| 113 | rsp.code = GatekeeperStatusCode::ERROR_GENERAL_FAILURE; |
| 114 | } |
| 115 | } else { |
| 116 | rsp.code = GatekeeperStatusCode::ERROR_NOT_IMPLEMENTED; |
| 117 | } |
| 118 | cb(rsp); |
| 119 | return Void(); |
| 120 | } |
| 121 | |
| 122 | Return<void> Gatekeeper::deleteAllUsers(deleteAllUsers_cb cb) { |
| 123 | GatekeeperResponse rsp; |
| 124 | if (device->delete_all_users != nullptr) { |
| 125 | int ret = device->delete_all_users(device); |
| 126 | if (!ret) { |
| 127 | rsp.code = GatekeeperStatusCode::STATUS_OK; |
| 128 | } else if (ret > 0) { |
| 129 | rsp.timeout = ret; |
| 130 | rsp.code = GatekeeperStatusCode::ERROR_RETRY_TIMEOUT; |
| 131 | } else { |
| 132 | rsp.code = GatekeeperStatusCode::ERROR_GENERAL_FAILURE; |
| 133 | } |
| 134 | } else { |
| 135 | rsp.code = GatekeeperStatusCode::ERROR_NOT_IMPLEMENTED; |
| 136 | } |
| 137 | cb(rsp); |
| 138 | return Void(); |
| 139 | } |
| 140 | |
| 141 | IGatekeeper* HIDL_FETCH_IGatekeeper(const char* /* name */) { |
| 142 | return new Gatekeeper(); |
| 143 | } |
| 144 | |
| 145 | } // namespace implementation |
| 146 | } // namespace V1_0 |
| 147 | } // namespace gatekeeper |
| 148 | } // namespace hardware |
| 149 | } // namespace android |