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