1/*
2 * Copyright (C) 2012 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 */
16#include <errno.h>
17#include <string.h>
18#include <stdint.h>
19
20#include <keymaster/softkeymaster.h>
21
22#include <keystore/keystore.h>
23
24#include <hardware/hardware.h>
25#include <hardware/keymaster.h>
26
27#include <openssl/err.h>
28
29#include <utils/UniquePtr.h>
30
31// For debugging
32//#define LOG_NDEBUG 0
33
34#define LOG_TAG "OpenSSLKeyMaster"
35#include <cutils/log.h>
36
37typedef UniquePtr<keymaster_device_t> Unique_keymaster_device_t;
38
39/* Close an opened OpenSSL instance */
40static int openssl_close(hw_device_t *dev) {
41    delete dev;
42    return 0;
43}
44
45/*
46 * Generic device handling
47 */
48static int openssl_open(const hw_module_t* module, const char* name,
49        hw_device_t** device) {
50    if (strcmp(name, KEYSTORE_KEYMASTER) != 0)
51        return -EINVAL;
52
53    Unique_keymaster_device_t dev(new keymaster_device_t);
54    if (dev.get() == NULL)
55        return -ENOMEM;
56
57    dev->common.tag = HARDWARE_DEVICE_TAG;
58    dev->common.version = 1;
59    dev->common.module = (struct hw_module_t*) module;
60    dev->common.close = openssl_close;
61
62    dev->flags = KEYMASTER_SOFTWARE_ONLY;
63
64    dev->generate_keypair = openssl_generate_keypair;
65    dev->import_keypair = openssl_import_keypair;
66    dev->get_keypair_public = openssl_get_keypair_public;
67    dev->delete_keypair = NULL;
68    dev->delete_all = NULL;
69    dev->sign_data = openssl_sign_data;
70    dev->verify_data = openssl_verify_data;
71
72    ERR_load_crypto_strings();
73    ERR_load_BIO_strings();
74
75    *device = reinterpret_cast<hw_device_t*>(dev.release());
76
77    return 0;
78}
79
80static struct hw_module_methods_t keystore_module_methods = {
81    open: openssl_open,
82};
83
84struct keystore_module HAL_MODULE_INFO_SYM
85__attribute__ ((visibility ("default"))) = {
86    common: {
87        tag: HARDWARE_MODULE_TAG,
88        module_api_version: KEYMASTER_MODULE_API_VERSION_0_2,
89        hal_api_version: HARDWARE_HAL_API_VERSION,
90        id: KEYSTORE_HARDWARE_MODULE_ID,
91        name: "Keymaster OpenSSL HAL",
92        author: "The Android Open Source Project",
93        methods: &keystore_module_methods,
94        dso: 0,
95        reserved: {},
96    },
97};
98