blob: 25f16b57df010616a98b62c3e6d1ef68cb8d8d2b [file] [log] [blame]
Janis Danisevskis0f35e5a2016-10-12 11:33:13 +01001/*
2 **
3 ** Copyright 2016, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18#define LOG_TAG "android.hardware.keymaster@3.0-impl"
19
20#include "KeymasterDevice.h"
21
Sasha Smundak769c0532019-01-31 17:19:58 -080022#include <log/log.h>
Janis Danisevskis0f35e5a2016-10-12 11:33:13 +010023
Janis Danisevskis5a07ed42017-05-23 16:11:06 -070024#include <AndroidKeymaster3Device.h>
Janis Danisevskis5a07ed42017-05-23 16:11:06 -070025#include <hardware/keymaster2.h>
Janis Danisevskis0f35e5a2016-10-12 11:33:13 +010026#include <hardware/keymaster_defs.h>
Janis Danisevskis0f35e5a2016-10-12 11:33:13 +010027
28namespace android {
29namespace hardware {
30namespace keymaster {
31namespace V3_0 {
32namespace implementation {
33
Janis Danisevskis5a07ed42017-05-23 16:11:06 -070034static int get_keymaster2_dev(keymaster2_device_t** dev, const hw_module_t* mod) {
35 int rc = keymaster2_open(mod, dev);
Janis Danisevskis0f35e5a2016-10-12 11:33:13 +010036 if (rc) {
37 ALOGE("Error %d opening keystore keymaster2 device", rc);
Janis Danisevskis5a07ed42017-05-23 16:11:06 -070038 *dev = nullptr;
Janis Danisevskis0f35e5a2016-10-12 11:33:13 +010039 }
Janis Danisevskis0f35e5a2016-10-12 11:33:13 +010040 return rc;
41}
42
Janis Danisevskis5a07ed42017-05-23 16:11:06 -070043static IKeymasterDevice* createKeymaster3Device() {
44 const hw_module_t* mod = nullptr;
Janis Danisevskis0f35e5a2016-10-12 11:33:13 +010045
46 int rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod);
47 if (rc) {
48 ALOGI("Could not find any keystore module, using software-only implementation.");
49 // SoftKeymasterDevice will be deleted by keymaster_device_release()
Janis Danisevskis5a07ed42017-05-23 16:11:06 -070050 return ::keymaster::ng::CreateKeymasterDevice();
Janis Danisevskis0f35e5a2016-10-12 11:33:13 +010051 }
52
Shawn Willden50f70d72021-10-07 09:15:38 -060053 if (mod->module_api_version < KEYMASTER_MODULE_API_VERSION_2_0) {
Shawn Willden7751d102020-12-01 19:07:41 -070054 return nullptr;
Janis Danisevskis0f35e5a2016-10-12 11:33:13 +010055 } else {
Janis Danisevskis5a07ed42017-05-23 16:11:06 -070056 keymaster2_device_t* dev = nullptr;
57 if (get_keymaster2_dev(&dev, mod)) {
58 return nullptr;
Janis Danisevskis0f35e5a2016-10-12 11:33:13 +010059 }
Janis Danisevskis5a07ed42017-05-23 16:11:06 -070060 return ::keymaster::ng::CreateKeymasterDevice(dev);
Janis Danisevskis0f35e5a2016-10-12 11:33:13 +010061 }
Janis Danisevskis0f35e5a2016-10-12 11:33:13 +010062}
63
Shawn Willden63e15f02017-03-29 21:27:12 -060064IKeymasterDevice* HIDL_FETCH_IKeymasterDevice(const char* name) {
Shawn Willden63e15f02017-03-29 21:27:12 -060065 ALOGI("Fetching keymaster device name %s", name);
66
Shawn Willden63e15f02017-03-29 21:27:12 -060067 if (name && strcmp(name, "softwareonly") == 0) {
Janis Danisevskis5a07ed42017-05-23 16:11:06 -070068 return ::keymaster::ng::CreateKeymasterDevice();
Shawn Willden63e15f02017-03-29 21:27:12 -060069 } else if (name && strcmp(name, "default") == 0) {
Janis Danisevskis5a07ed42017-05-23 16:11:06 -070070 return createKeymaster3Device();
Shawn Willden63e15f02017-03-29 21:27:12 -060071 }
Janis Danisevskis5a07ed42017-05-23 16:11:06 -070072 return nullptr;
Janis Danisevskis0f35e5a2016-10-12 11:33:13 +010073}
74
75} // namespace implementation
76} // namespace V3_0
77} // namespace keymaster
78} // namespace hardware
79} // namespace android