Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 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 | */ |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 16 | #define LOG_TAG "android.hardware.biometrics.fingerprint@2.1-service" |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 17 | |
Jim Miller | 7f7eb47 | 2017-01-18 21:03:45 -0800 | [diff] [blame] | 18 | // For communication with Keystore binder interface |
| 19 | #include <binder/IServiceManager.h> |
| 20 | #include <keystore/IKeystoreService.h> |
| 21 | #include <keystore/keystore.h> // for error codes |
| 22 | #include <hardware/hw_auth_token.h> |
| 23 | |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 24 | #include <hardware/hardware.h> |
| 25 | #include <hardware/fingerprint.h> |
| 26 | #include "BiometricsFingerprint.h" |
| 27 | |
Janis Danisevskis | a47afa9 | 2017-01-20 13:50:10 +0000 | [diff] [blame] | 28 | #include <inttypes.h> |
| 29 | |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace hardware { |
| 32 | namespace biometrics { |
| 33 | namespace fingerprint { |
| 34 | namespace V2_1 { |
| 35 | namespace implementation { |
| 36 | |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 37 | // Supported fingerprint HAL version |
| 38 | static const uint16_t kVersion = HARDWARE_MODULE_API_VERSION(2, 1); |
| 39 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 40 | using RequestStatus = |
| 41 | android::hardware::biometrics::fingerprint::V2_1::RequestStatus; |
| 42 | |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 43 | sp<IBiometricsFingerprintClientCallback> |
| 44 | BiometricsFingerprint::mClientCallback = nullptr; |
| 45 | |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 46 | // TODO: This is here because HAL 2.1 doesn't have a way to propagate a |
| 47 | // unique token for its driver. Subsequent versions should send a unique |
| 48 | // token for each call to notify(). This is fine as long as there's only |
| 49 | // one fingerprint device on the platform. |
| 50 | fingerprint_device_t *BiometricsFingerprint::sDevice = nullptr; |
| 51 | |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 52 | BiometricsFingerprint::BiometricsFingerprint(fingerprint_device_t *device) |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 53 | : mDevice(device) { |
| 54 | sDevice = mDevice; // keep track of the most recent instance |
| 55 | } |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 56 | |
| 57 | BiometricsFingerprint::~BiometricsFingerprint() { |
| 58 | ALOG(LOG_VERBOSE, LOG_TAG, "nativeCloseHal()\n"); |
| 59 | if (mDevice == NULL) { |
| 60 | ALOGE("No valid device"); |
| 61 | return; |
| 62 | } |
| 63 | int err; |
| 64 | if (0 != (err = mDevice->common.close( |
| 65 | reinterpret_cast<hw_device_t*>(mDevice)))) { |
| 66 | ALOGE("Can't close fingerprint module, error: %d", err); |
| 67 | return; |
| 68 | } |
| 69 | mDevice = NULL; |
| 70 | } |
| 71 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 72 | Return<RequestStatus> BiometricsFingerprint::ErrorFilter(int32_t error) { |
| 73 | switch(error) { |
| 74 | case 0: return RequestStatus::SYS_OK; |
| 75 | case -2: return RequestStatus::SYS_ENOENT; |
| 76 | case -4: return RequestStatus::SYS_EINTR; |
| 77 | case -5: return RequestStatus::SYS_EIO; |
| 78 | case -11: return RequestStatus::SYS_EAGAIN; |
| 79 | case -12: return RequestStatus::SYS_ENOMEM; |
| 80 | case -13: return RequestStatus::SYS_EACCES; |
| 81 | case -14: return RequestStatus::SYS_EFAULT; |
| 82 | case -16: return RequestStatus::SYS_EBUSY; |
| 83 | case -22: return RequestStatus::SYS_EINVAL; |
| 84 | case -28: return RequestStatus::SYS_ENOSPC; |
| 85 | case -110: return RequestStatus::SYS_ETIMEDOUT; |
| 86 | default: |
| 87 | ALOGE("An unknown error returned from fingerprint vendor library"); |
| 88 | return RequestStatus::SYS_UNKNOWN; |
| 89 | } |
| 90 | } |
| 91 | |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 92 | // Translate from errors returned by traditional HAL (see fingerprint.h) to |
| 93 | // HIDL-compliant FingerprintError. |
| 94 | FingerprintError BiometricsFingerprint::VendorErrorFilter(int32_t error, |
| 95 | int32_t* vendorCode) { |
| 96 | *vendorCode = 0; |
| 97 | switch(error) { |
| 98 | case FINGERPRINT_ERROR_HW_UNAVAILABLE: |
| 99 | return FingerprintError::ERROR_HW_UNAVAILABLE; |
| 100 | case FINGERPRINT_ERROR_UNABLE_TO_PROCESS: |
| 101 | return FingerprintError::ERROR_UNABLE_TO_PROCESS; |
| 102 | case FINGERPRINT_ERROR_TIMEOUT: |
| 103 | return FingerprintError::ERROR_TIMEOUT; |
| 104 | case FINGERPRINT_ERROR_NO_SPACE: |
| 105 | return FingerprintError::ERROR_NO_SPACE; |
| 106 | case FINGERPRINT_ERROR_CANCELED: |
| 107 | return FingerprintError::ERROR_CANCELED; |
| 108 | case FINGERPRINT_ERROR_UNABLE_TO_REMOVE: |
| 109 | return FingerprintError::ERROR_UNABLE_TO_REMOVE; |
| 110 | default: |
| 111 | if (error >= FINGERPRINT_ERROR_VENDOR_BASE) { |
| 112 | // vendor specific code. |
| 113 | *vendorCode = error - FINGERPRINT_ERROR_VENDOR_BASE; |
| 114 | return FingerprintError::ERROR_VENDOR; |
| 115 | } |
| 116 | } |
| 117 | ALOGE("Unknown error from fingerprint vendor library"); |
| 118 | return FingerprintError::ERROR_UNABLE_TO_PROCESS; |
| 119 | } |
| 120 | |
| 121 | // Translate acquired messages returned by traditional HAL (see fingerprint.h) |
| 122 | // to HIDL-compliant FingerprintAcquiredInfo. |
| 123 | FingerprintAcquiredInfo BiometricsFingerprint::VendorAcquiredFilter( |
| 124 | int32_t info, int32_t* vendorCode) { |
| 125 | *vendorCode = 0; |
| 126 | switch(info) { |
| 127 | case FINGERPRINT_ACQUIRED_GOOD: |
| 128 | return FingerprintAcquiredInfo::ACQUIRED_GOOD; |
| 129 | case FINGERPRINT_ACQUIRED_PARTIAL: |
| 130 | return FingerprintAcquiredInfo::ACQUIRED_PARTIAL; |
| 131 | case FINGERPRINT_ACQUIRED_INSUFFICIENT: |
| 132 | return FingerprintAcquiredInfo::ACQUIRED_INSUFFICIENT; |
| 133 | case FINGERPRINT_ACQUIRED_IMAGER_DIRTY: |
| 134 | return FingerprintAcquiredInfo::ACQUIRED_IMAGER_DIRTY; |
| 135 | case FINGERPRINT_ACQUIRED_TOO_SLOW: |
| 136 | return FingerprintAcquiredInfo::ACQUIRED_TOO_SLOW; |
| 137 | case FINGERPRINT_ACQUIRED_TOO_FAST: |
| 138 | return FingerprintAcquiredInfo::ACQUIRED_TOO_FAST; |
| 139 | default: |
| 140 | if (info >= FINGERPRINT_ACQUIRED_VENDOR_BASE) { |
| 141 | // vendor specific code. |
| 142 | *vendorCode = info - FINGERPRINT_ACQUIRED_VENDOR_BASE; |
| 143 | return FingerprintAcquiredInfo::ACQUIRED_VENDOR; |
| 144 | } |
| 145 | } |
| 146 | ALOGE("Unknown acquiredmsg from fingerprint vendor library"); |
| 147 | return FingerprintAcquiredInfo::ACQUIRED_INSUFFICIENT; |
| 148 | } |
| 149 | |
| 150 | Return<uint64_t> BiometricsFingerprint::setNotify( |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 151 | const sp<IBiometricsFingerprintClientCallback>& clientCallback) { |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 152 | mClientCallback = clientCallback; |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 153 | return reinterpret_cast<uint64_t>(mDevice); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | Return<uint64_t> BiometricsFingerprint::preEnroll() { |
| 157 | return mDevice->pre_enroll(mDevice); |
| 158 | } |
| 159 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 160 | Return<RequestStatus> BiometricsFingerprint::enroll(const hidl_array<uint8_t, 69>& hat, |
| 161 | uint32_t gid, uint32_t timeoutSec) { |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 162 | const hw_auth_token_t* authToken = |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 163 | reinterpret_cast<const hw_auth_token_t*>(hat.data()); |
| 164 | return ErrorFilter(mDevice->enroll(mDevice, authToken, gid, timeoutSec)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 167 | Return<RequestStatus> BiometricsFingerprint::postEnroll() { |
| 168 | return ErrorFilter(mDevice->post_enroll(mDevice)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | Return<uint64_t> BiometricsFingerprint::getAuthenticatorId() { |
| 172 | return mDevice->get_authenticator_id(mDevice); |
| 173 | } |
| 174 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 175 | Return<RequestStatus> BiometricsFingerprint::cancel() { |
| 176 | return ErrorFilter(mDevice->cancel(mDevice)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 179 | Return<RequestStatus> BiometricsFingerprint::enumerate() { |
| 180 | return ErrorFilter(mDevice->enumerate(mDevice)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 183 | Return<RequestStatus> BiometricsFingerprint::remove(uint32_t gid, uint32_t fid) { |
| 184 | return ErrorFilter(mDevice->remove(mDevice, gid, fid)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 187 | Return<RequestStatus> BiometricsFingerprint::setActiveGroup(uint32_t gid, |
| 188 | const hidl_string& storePath) { |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 189 | if (storePath.size() >= PATH_MAX || storePath.size() <= 0) { |
| 190 | ALOGE("Bad path length: %zd", storePath.size()); |
| 191 | } |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 192 | return ErrorFilter(mDevice->set_active_group(mDevice, gid, |
| 193 | storePath.c_str())); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 196 | Return<RequestStatus> BiometricsFingerprint::authenticate(uint64_t operationId, |
| 197 | uint32_t gid) { |
| 198 | return ErrorFilter(mDevice->authenticate(mDevice, operationId, gid)); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 201 | IBiometricsFingerprint* BiometricsFingerprint::getInstance() { |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 202 | int err; |
| 203 | const hw_module_t *hw_mdl = NULL; |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 204 | ALOGE("Opening fingerprint hal library..."); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 205 | if (0 != (err = hw_get_module(FINGERPRINT_HARDWARE_MODULE_ID, &hw_mdl))) { |
| 206 | ALOGE("Can't open fingerprint HW Module, error: %d", err); |
| 207 | return nullptr; |
| 208 | } |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 209 | |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 210 | if (hw_mdl == NULL) { |
| 211 | ALOGE("No valid fingerprint module"); |
| 212 | return nullptr; |
| 213 | } |
| 214 | |
| 215 | fingerprint_module_t const *module = |
| 216 | reinterpret_cast<const fingerprint_module_t*>(hw_mdl); |
| 217 | if (module->common.methods->open == NULL) { |
| 218 | ALOGE("No valid open method"); |
| 219 | return nullptr; |
| 220 | } |
| 221 | |
| 222 | hw_device_t *device = NULL; |
| 223 | |
| 224 | if (0 != (err = module->common.methods->open(hw_mdl, NULL, &device))) { |
| 225 | ALOGE("Can't open fingerprint methods, error: %d", err); |
| 226 | return nullptr; |
| 227 | } |
| 228 | |
Jim Miller | 902de51 | 2016-12-15 19:42:19 -0800 | [diff] [blame] | 229 | if (kVersion != device->version) { |
| 230 | ALOGE("Wrong fp version. Expected %d, got %d", kVersion, device->version); |
| 231 | return 0; // enforce this on new devices because of HIDL translation layer |
| 232 | } |
| 233 | |
Sasha Levitskiy | 52640ee | 2016-11-10 14:15:33 -0800 | [diff] [blame] | 234 | fingerprint_device_t* fp_device = |
| 235 | reinterpret_cast<fingerprint_device_t*>(device); |
| 236 | |
| 237 | if (0 != (err = |
| 238 | fp_device->set_notify(fp_device, BiometricsFingerprint::notify))) { |
| 239 | ALOGE("Can't register fingerprint module callback, error: %d", err); |
| 240 | return nullptr; |
| 241 | } |
| 242 | |
| 243 | return new BiometricsFingerprint(fp_device); |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Jim Miller | 7f7eb47 | 2017-01-18 21:03:45 -0800 | [diff] [blame] | 246 | void BiometricsFingerprint::notifyKeystore(const uint8_t *auth_token, const size_t auth_token_length) { |
| 247 | if (auth_token != nullptr && auth_token_length > 0) { |
| 248 | // TODO: cache service? |
| 249 | sp<IServiceManager> sm = android::defaultServiceManager(); |
Janis Danisevskis | a47afa9 | 2017-01-20 13:50:10 +0000 | [diff] [blame] | 250 | sp<::android::IBinder> binder = sm->getService(String16("android.security.keystore")); |
Jim Miller | 7f7eb47 | 2017-01-18 21:03:45 -0800 | [diff] [blame] | 251 | sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder); |
| 252 | if (service != nullptr) { |
Janis Danisevskis | a47afa9 | 2017-01-20 13:50:10 +0000 | [diff] [blame] | 253 | auto ret = service->addAuthToken(auth_token, auth_token_length); |
| 254 | if (!ret.isOk()) { |
| 255 | ALOGE("Failure sending auth token to KeyStore: %" PRId32, int32_t(ret)); |
Jim Miller | 7f7eb47 | 2017-01-18 21:03:45 -0800 | [diff] [blame] | 256 | } |
| 257 | } else { |
| 258 | ALOGE("Unable to communicate with KeyStore"); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
Sasha Levitskiy | 965bd32 | 2016-10-21 10:55:25 -0700 | [diff] [blame] | 263 | } // namespace implementation |
| 264 | } // namespace V2_1 |
| 265 | } // namespace fingerprint |
| 266 | } // namespace biometrics |
| 267 | } // namespace hardware |
| 268 | } // namespace android |