summaryrefslogtreecommitdiffstats
path: root/gatekeeper/module.cpp
blob: a3d678c52ca8f1a5dc972694228607f95dd7d5b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (C) 2015 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.
 */

#include <hardware/hardware.h>
#include <hardware/gatekeeper.h>
#define LOG_TAG "I9305Gatekeeper"
#include <cutils/log.h>

#include <string.h>
#include <errno.h>
#include <stdlib.h>

#include "SoftGateKeeper.h"
#include "SoftGateKeeperDevice.h"

using i9305::SoftGateKeeperDevice;

struct i9305_gatekeeper_device {
    gatekeeper_device device;
    SoftGateKeeperDevice *s_gatekeeper;
};

static i9305_gatekeeper_device s_device;

static int enroll(const struct gatekeeper_device *dev __unused, uint32_t uid,
            const uint8_t *current_password_handle, uint32_t current_password_handle_length,
            const uint8_t *current_password, uint32_t current_password_length,
            const uint8_t *desired_password, uint32_t desired_password_length,
            uint8_t **enrolled_password_handle, uint32_t *enrolled_password_handle_length) {

    SoftGateKeeperDevice *s_gatekeeper = ((i9305_gatekeeper_device*)(dev))->s_gatekeeper;
    ALOGE("called %s with gate keeper %p device %p\n", __func__, s_gatekeeper, dev);
    if (s_gatekeeper == nullptr)  {
        abort();
        return -EINVAL;
    }

    return s_gatekeeper->enroll(uid,
            current_password_handle, current_password_handle_length,
            current_password, current_password_length,
            desired_password, desired_password_length,
            enrolled_password_handle, enrolled_password_handle_length);
}

static int verify(const struct gatekeeper_device *dev __unused, uint32_t uid, uint64_t challenge,
            const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length,
            const uint8_t *provided_password, uint32_t provided_password_length,
            uint8_t **auth_token, uint32_t *auth_token_length, bool *request_reenroll) {
    SoftGateKeeperDevice *s_gatekeeper = ((i9305_gatekeeper_device*)(dev))->s_gatekeeper;
    ALOGE("called %s with gate keeper %p device %p\n", __func__, s_gatekeeper, dev);
    if (s_gatekeeper == nullptr) return -EINVAL;
    return s_gatekeeper->verify(uid, challenge,
            enrolled_password_handle, enrolled_password_handle_length,
            provided_password, provided_password_length,
            auth_token, auth_token_length, request_reenroll);
}

static int close_device(hw_device_t* dev __unused) {
    SoftGateKeeperDevice *s_gatekeeper = ((i9305_gatekeeper_device*)(dev))->s_gatekeeper;
    if (s_gatekeeper == nullptr) return 0;
    delete s_gatekeeper;
    s_gatekeeper = nullptr;
    ALOGE("called %s with gate keeper %p device %p\n", __func__, s_gatekeeper, dev);
    return 0;
}

static int i9305_gatekeeper_open(const hw_module_t *module, const char *name,
        hw_device_t **device) {

    if (strcmp(name, HARDWARE_GATEKEEPER) != 0) {
        abort();
        return -EINVAL;
    }

    memset(&s_device, 0, sizeof(s_device));

    SoftGateKeeperDevice *s_gatekeeper = new SoftGateKeeperDevice();
    if (s_gatekeeper == nullptr) return -ENOMEM;

    s_device.s_gatekeeper = s_gatekeeper;

    s_device.device.common.tag = HARDWARE_DEVICE_TAG;
    s_device.device.common.version = 1;
    s_device.device.common.module = const_cast<hw_module_t *>(module);
    s_device.device.common.close = close_device;

    s_device.device.enroll = enroll;
    s_device.device.verify = verify;
    s_device.device.delete_user = nullptr;
    s_device.device.delete_all_users = nullptr;

    *device = &s_device.device.common;
    ALOGE("called %s with gate keeper %p device %p\n", __func__, s_gatekeeper, *device);

    return 0;
}

static struct hw_module_methods_t gatekeeper_module_methods = {
    .open = i9305_gatekeeper_open,
};

struct gatekeeper_module HAL_MODULE_INFO_SYM __attribute__((visibility("default"))) = {
    .common = {
        .tag = HARDWARE_MODULE_TAG,
        .module_api_version = GATEKEEPER_MODULE_API_VERSION_0_1,
        .hal_api_version = HARDWARE_HAL_API_VERSION,
        .id = GATEKEEPER_HARDWARE_MODULE_ID,
        .name = "I9305 GateKeeper HAL",
        .author = "The Android Open Source Project",
        .methods = &gatekeeper_module_methods,
        .dso = 0,
        .reserved = {}
    },
};