1 /*
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
22 #include <log/log.h>
23
24 #include <AndroidKeymaster3Device.h>
25 #include <hardware/keymaster0.h>
26 #include <hardware/keymaster1.h>
27 #include <hardware/keymaster2.h>
28 #include <hardware/keymaster_defs.h>
29
30 namespace android {
31 namespace hardware {
32 namespace keymaster {
33 namespace V3_0 {
34 namespace implementation {
35
get_keymaster0_dev(keymaster0_device_t ** dev,const hw_module_t * mod)36 static int get_keymaster0_dev(keymaster0_device_t** dev, const hw_module_t* mod) {
37 int rc = keymaster0_open(mod, dev);
38 if (rc) {
39 ALOGE("Error opening keystore keymaster0 device.");
40 *dev = nullptr;
41 return rc;
42 }
43 return 0;
44 }
45
get_keymaster1_dev(keymaster1_device_t ** dev,const hw_module_t * mod)46 static int get_keymaster1_dev(keymaster1_device_t** dev, const hw_module_t* mod) {
47 int rc = keymaster1_open(mod, dev);
48 if (rc) {
49 ALOGE("Error %d opening keystore keymaster1 device", rc);
50 if (*dev) {
51 (*dev)->common.close(&(*dev)->common);
52 *dev = nullptr;
53 }
54 }
55 return rc;
56 }
57
get_keymaster2_dev(keymaster2_device_t ** dev,const hw_module_t * mod)58 static int get_keymaster2_dev(keymaster2_device_t** dev, const hw_module_t* mod) {
59 int rc = keymaster2_open(mod, dev);
60 if (rc) {
61 ALOGE("Error %d opening keystore keymaster2 device", rc);
62 *dev = nullptr;
63 }
64 return rc;
65 }
66
createKeymaster3Device()67 static IKeymasterDevice* createKeymaster3Device() {
68 const hw_module_t* mod = nullptr;
69
70 int rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod);
71 if (rc) {
72 ALOGI("Could not find any keystore module, using software-only implementation.");
73 // SoftKeymasterDevice will be deleted by keymaster_device_release()
74 return ::keymaster::ng::CreateKeymasterDevice();
75 }
76
77 if (mod->module_api_version < KEYMASTER_MODULE_API_VERSION_1_0) {
78 keymaster0_device_t* dev = nullptr;
79 if (get_keymaster0_dev(&dev, mod)) {
80 return nullptr;
81 }
82 return ::keymaster::ng::CreateKeymasterDevice(dev);
83 } else if (mod->module_api_version == KEYMASTER_MODULE_API_VERSION_1_0) {
84 keymaster1_device_t* dev = nullptr;
85 if (get_keymaster1_dev(&dev, mod)) {
86 return nullptr;
87 }
88 return ::keymaster::ng::CreateKeymasterDevice(dev);
89 } else {
90 keymaster2_device_t* dev = nullptr;
91 if (get_keymaster2_dev(&dev, mod)) {
92 return nullptr;
93 }
94 return ::keymaster::ng::CreateKeymasterDevice(dev);
95 }
96 }
97
HIDL_FETCH_IKeymasterDevice(const char * name)98 IKeymasterDevice* HIDL_FETCH_IKeymasterDevice(const char* name) {
99 ALOGI("Fetching keymaster device name %s", name);
100
101 if (name && strcmp(name, "softwareonly") == 0) {
102 return ::keymaster::ng::CreateKeymasterDevice();
103 } else if (name && strcmp(name, "default") == 0) {
104 return createKeymaster3Device();
105 }
106 return nullptr;
107 }
108
109 } // namespace implementation
110 } // namespace V3_0
111 } // namespace keymaster
112 } // namespace hardware
113 } // namespace android
114