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