2016-10-12 12:33:13 +02:00
|
|
|
/*
|
|
|
|
**
|
|
|
|
** Copyright 2016, The Android Open Source Project
|
|
|
|
**
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
**
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
**
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "android.hardware.keymaster@3.0-impl"
|
|
|
|
|
|
|
|
#include "KeymasterDevice.h"
|
|
|
|
|
2019-02-01 02:19:58 +01:00
|
|
|
#include <log/log.h>
|
2016-10-12 12:33:13 +02:00
|
|
|
|
2017-05-24 01:11:06 +02:00
|
|
|
#include <AndroidKeymaster3Device.h>
|
2021-10-11 15:38:59 +02:00
|
|
|
#include <hardware/keymaster1.h>
|
2017-05-24 01:11:06 +02:00
|
|
|
#include <hardware/keymaster2.h>
|
2016-10-12 12:33:13 +02:00
|
|
|
#include <hardware/keymaster_defs.h>
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace hardware {
|
|
|
|
namespace keymaster {
|
|
|
|
namespace V3_0 {
|
|
|
|
namespace implementation {
|
|
|
|
|
2021-10-11 15:38:59 +02:00
|
|
|
static int get_keymaster1_dev(keymaster1_device_t** dev, const hw_module_t* mod) {
|
|
|
|
int rc = keymaster1_open(mod, dev);
|
|
|
|
if (rc) {
|
|
|
|
ALOGE("Error %d opening keystore keymaster1 device", rc);
|
|
|
|
if (*dev) {
|
|
|
|
(*dev)->common.close(&(*dev)->common);
|
|
|
|
*dev = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-05-24 01:11:06 +02:00
|
|
|
static int get_keymaster2_dev(keymaster2_device_t** dev, const hw_module_t* mod) {
|
|
|
|
int rc = keymaster2_open(mod, dev);
|
2016-10-12 12:33:13 +02:00
|
|
|
if (rc) {
|
|
|
|
ALOGE("Error %d opening keystore keymaster2 device", rc);
|
2017-05-24 01:11:06 +02:00
|
|
|
*dev = nullptr;
|
2016-10-12 12:33:13 +02:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-05-24 01:11:06 +02:00
|
|
|
static IKeymasterDevice* createKeymaster3Device() {
|
|
|
|
const hw_module_t* mod = nullptr;
|
2016-10-12 12:33:13 +02:00
|
|
|
|
|
|
|
int rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod);
|
|
|
|
if (rc) {
|
|
|
|
ALOGI("Could not find any keystore module, using software-only implementation.");
|
|
|
|
// SoftKeymasterDevice will be deleted by keymaster_device_release()
|
2017-05-24 01:11:06 +02:00
|
|
|
return ::keymaster::ng::CreateKeymasterDevice();
|
2016-10-12 12:33:13 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 15:38:59 +02:00
|
|
|
if (mod->module_api_version < KEYMASTER_MODULE_API_VERSION_1_0) {
|
2020-12-02 03:07:41 +01:00
|
|
|
return nullptr;
|
2021-10-11 15:38:59 +02:00
|
|
|
} else if (mod->module_api_version == KEYMASTER_MODULE_API_VERSION_1_0) {
|
|
|
|
keymaster1_device_t* dev = nullptr;
|
|
|
|
if (get_keymaster1_dev(&dev, mod)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ::keymaster::ng::CreateKeymasterDevice(dev);
|
2017-05-24 01:11:06 +02:00
|
|
|
} else {
|
|
|
|
keymaster2_device_t* dev = nullptr;
|
|
|
|
if (get_keymaster2_dev(&dev, mod)) {
|
|
|
|
return nullptr;
|
2017-01-23 13:51:10 +01:00
|
|
|
}
|
2017-05-24 01:11:06 +02:00
|
|
|
return ::keymaster::ng::CreateKeymasterDevice(dev);
|
2017-01-23 13:51:10 +01:00
|
|
|
}
|
2016-10-12 12:33:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-30 05:27:12 +02:00
|
|
|
IKeymasterDevice* HIDL_FETCH_IKeymasterDevice(const char* name) {
|
|
|
|
ALOGI("Fetching keymaster device name %s", name);
|
|
|
|
|
|
|
|
if (name && strcmp(name, "softwareonly") == 0) {
|
2017-05-24 01:11:06 +02:00
|
|
|
return ::keymaster::ng::CreateKeymasterDevice();
|
2017-03-30 05:27:12 +02:00
|
|
|
} else if (name && strcmp(name, "default") == 0) {
|
2017-05-24 01:11:06 +02:00
|
|
|
return createKeymaster3Device();
|
2017-03-30 05:27:12 +02:00
|
|
|
}
|
2017-05-24 01:11:06 +02:00
|
|
|
return nullptr;
|
2016-10-12 12:33:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace implementation
|
|
|
|
} // namespace V3_0
|
|
|
|
} // namespace keymaster
|
|
|
|
} // namespace hardware
|
|
|
|
} // namespace android
|