blob: 0e7ace649016858a7f7f605792f1cb2fc2a61c39 [file] [log] [blame]
Andres Morales99482122015-03-06 10:55:18 -08001/*
2 * Copyright (C) 2015 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 */
Anand Prasad936d5ba2016-01-07 17:37:58 -080016#include <endian.h>
Andres Morales99482122015-03-06 10:55:18 -080017#include <gtest/gtest.h>
Andres Morales7d0f0402015-03-19 18:02:55 -070018#include <hardware/gatekeeper.h>
Andres Morales4a29dcb2015-03-26 10:21:47 -070019#include <gatekeeper/gatekeeper.h> // For password_handle_t
Andres Morales21223c82015-06-03 14:39:14 -070020#include <unistd.h>
Andres Morales99482122015-03-06 10:55:18 -080021
22using ::testing::Test;
Andres Morales4a29dcb2015-03-26 10:21:47 -070023using ::gatekeeper::password_handle_t;
24using ::gatekeeper::secure_id_t;
Andres Morales99482122015-03-06 10:55:18 -080025
Andres Morales7d0f0402015-03-19 18:02:55 -070026class GateKeeperDeviceTest : public virtual Test {
Andres Morales99482122015-03-06 10:55:18 -080027public:
Andres Morales7d0f0402015-03-19 18:02:55 -070028 GateKeeperDeviceTest() {}
29 virtual ~GateKeeperDeviceTest() {}
Andres Morales99482122015-03-06 10:55:18 -080030
31 virtual void SetUp() {
Andres Morales7d0f0402015-03-19 18:02:55 -070032 gatekeeper_device_initialize(&device);
Andres Morales99482122015-03-06 10:55:18 -080033 }
34
35 virtual void TearDown() {
Andres Morales7d0f0402015-03-19 18:02:55 -070036 gatekeeper_close(device);
Andres Morales99482122015-03-06 10:55:18 -080037 }
38
Andres Morales7d0f0402015-03-19 18:02:55 -070039 static void gatekeeper_device_initialize(gatekeeper_device_t **dev) {
Andres Morales99482122015-03-06 10:55:18 -080040 int ret;
41 const hw_module_t *mod;
Andres Morales7d0f0402015-03-19 18:02:55 -070042 ret = hw_get_module_by_class(GATEKEEPER_HARDWARE_MODULE_ID, NULL, &mod);
Andres Morales99482122015-03-06 10:55:18 -080043
44 ASSERT_EQ(0, ret);
45
Andres Morales7d0f0402015-03-19 18:02:55 -070046 ret = gatekeeper_open(mod, dev);
Andres Morales99482122015-03-06 10:55:18 -080047
48 ASSERT_EQ(0, ret);
49 }
50
Andres Morales7d0f0402015-03-19 18:02:55 -070051 gatekeeper_device_t *device;
Andres Morales99482122015-03-06 10:55:18 -080052};
53
Andres Morales21223c82015-06-03 14:39:14 -070054TEST_F(GateKeeperDeviceTest, EnrollAndVerifyStress) {
Andres Morales11ed52a2015-03-30 16:47:47 -070055 uint32_t password_len = 50;
Andres Morales99482122015-03-06 10:55:18 -080056 uint8_t password_payload[password_len];
57 uint8_t *password_handle;
Andres Morales11ed52a2015-03-30 16:47:47 -070058 uint32_t password_handle_length;
Andres Morales99482122015-03-06 10:55:18 -080059 uint8_t *auth_token;
Andres Morales11ed52a2015-03-30 16:47:47 -070060 uint32_t auth_token_len;
Andres Morales99482122015-03-06 10:55:18 -080061 int ret;
62
Andres Morales21223c82015-06-03 14:39:14 -070063 ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
64 &password_handle, &password_handle_length);
65
66 ASSERT_EQ(0, ret);
67
68 for (int i = 0; i < 1000; i++) {
69 bool should_reenroll;
70 ret = device->verify(device, 400, 0, password_handle, password_handle_length,
71 password_payload, password_len, &auth_token, &auth_token_len, &should_reenroll);
72
73 ASSERT_EQ(0, ret);
74 }
75}
76
77TEST_F(GateKeeperDeviceTest, EnrollAndVerify) {
78 uint32_t password_len = 50;
79 uint8_t password_payload[password_len];
80 uint8_t *password_handle;
81 uint32_t password_handle_length;
82 uint8_t *auth_token;
83 uint32_t auth_token_len;
84 hw_auth_token_t *hat;
85 int ret;
86
87 ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
Andres Morales99482122015-03-06 10:55:18 -080088 &password_handle, &password_handle_length);
89
90 ASSERT_EQ(0, ret);
91
Andres Moralesaedf6052015-05-14 13:10:30 -070092 bool should_reenroll;
Andres Morales21223c82015-06-03 14:39:14 -070093 ret = device->verify(device, 400, 0, password_handle, password_handle_length,
Andres Moralesaedf6052015-05-14 13:10:30 -070094 password_payload, password_len, &auth_token, &auth_token_len, &should_reenroll);
Andres Morales21223c82015-06-03 14:39:14 -070095 ASSERT_EQ(0, should_reenroll);
96 ASSERT_EQ(0, ret);
97
98 hat = reinterpret_cast<hw_auth_token_t *>(auth_token);
99
100 ASSERT_EQ(HW_AUTH_TOKEN_VERSION, hat->version);
Anand Prasad936d5ba2016-01-07 17:37:58 -0800101 ASSERT_EQ(htonl(HW_AUTH_PASSWORD), hat->authenticator_type);
Andres Morales21223c82015-06-03 14:39:14 -0700102}
103
104TEST_F(GateKeeperDeviceTest, EnrollAndVerifyTimeout) {
105 uint32_t password_len = 50;
106 uint8_t password_payload[password_len];
107 uint8_t *password_handle;
108 uint32_t password_handle_length;
109 uint8_t *auth_token = NULL;
110 uint32_t auth_token_len;
111 bool should_reenroll;
112 int ret;
113
114 ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
115 &password_handle, &password_handle_length);
116
117 ASSERT_EQ(0, ret);
118
119 int payload_val = password_payload[0];
120 password_payload[0] = 4;
121
122 int timeout = 0;
123 for (int i = 0; i < 20; i++) {
124 bool should_reenroll;
125 ret = device->verify(device, 400, 0, password_handle, password_handle_length,
126 password_payload, password_len, &auth_token, &auth_token_len,
127 &should_reenroll);
128 ASSERT_NE(0, ret);
129 ASSERT_EQ(NULL, auth_token);
130
131 if (ret > 0) {
132 timeout = ret;
133 }
134 }
135
136 ASSERT_NE(0, timeout);
137
138 sleep((timeout + 999)/ 1000);
139
140 password_payload[0] = payload_val;
141
142 ret = device->verify(device, 400, 0, password_handle, password_handle_length,
143 password_payload, password_len, &auth_token, &auth_token_len,
144 &should_reenroll);
Andres Morales99482122015-03-06 10:55:18 -0800145
146 ASSERT_EQ(0, ret);
147}
148
Andres Morales7d0f0402015-03-19 18:02:55 -0700149TEST_F(GateKeeperDeviceTest, EnrollAndVerifyBadPassword) {
Andres Morales11ed52a2015-03-30 16:47:47 -0700150 uint32_t password_len = 50;
Andres Morales99482122015-03-06 10:55:18 -0800151 uint8_t password_payload[password_len];
152 uint8_t *password_handle;
Andres Morales11ed52a2015-03-30 16:47:47 -0700153 uint32_t password_handle_length;
Andres Morales99482122015-03-06 10:55:18 -0800154 uint8_t *auth_token = NULL;
Andres Morales11ed52a2015-03-30 16:47:47 -0700155 uint32_t auth_token_len;
Andres Morales99482122015-03-06 10:55:18 -0800156 int ret;
157
Andres Morales21223c82015-06-03 14:39:14 -0700158 ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700159 &password_handle, &password_handle_length);
Andres Morales99482122015-03-06 10:55:18 -0800160
161 ASSERT_EQ(0, ret);
162
163 password_payload[0] = 4;
164
Andres Moralesaedf6052015-05-14 13:10:30 -0700165 bool should_reenroll;
Andres Morales21223c82015-06-03 14:39:14 -0700166 ret = device->verify(device, 400, 0, password_handle, password_handle_length,
Andres Moralesaedf6052015-05-14 13:10:30 -0700167 password_payload, password_len, &auth_token, &auth_token_len,
168 &should_reenroll);
Andres Morales99482122015-03-06 10:55:18 -0800169
170 ASSERT_NE(0, ret);
171 ASSERT_EQ(NULL, auth_token);
172}
173
Andres Moralesd14f4722015-12-18 11:57:34 -0800174TEST_F(GateKeeperDeviceTest, MinFailedAttemptsBeforeLockout) {
175 uint32_t password_len = 50;
176 uint8_t password_payload[password_len];
177 uint8_t *password_handle;
178 uint32_t password_handle_length;
179 uint8_t *auth_token = NULL;
180 uint32_t auth_token_len;
181 int ret;
182
183 ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
184 &password_handle, &password_handle_length);
185
186 ASSERT_EQ(0, ret);
187
188 password_payload[0] = 4;
189
190 // User should have at least 4 attempts before being locked out
191 static const int MIN_FAILED_ATTEMPTS = 4;
192
193 bool should_reenroll;
194 for (int i = 0; i < MIN_FAILED_ATTEMPTS; i++) {
195 ret = device->verify(device, 400, 0, password_handle, password_handle_length,
196 password_payload, password_len, &auth_token, &auth_token_len,
197 &should_reenroll);
198 // shoudln't be a timeout
Andres Morales7d0b0ba2016-01-13 12:56:32 -0800199 ASSERT_LT(ret, 0);
Andres Moralesd14f4722015-12-18 11:57:34 -0800200 }
201}
202
Andres Morales4a29dcb2015-03-26 10:21:47 -0700203TEST_F(GateKeeperDeviceTest, UntrustedReEnroll) {
Andres Morales11ed52a2015-03-30 16:47:47 -0700204 uint32_t password_len = 50;
Andres Morales4a29dcb2015-03-26 10:21:47 -0700205 uint8_t password_payload[password_len];
206 uint8_t *password_handle;
Andres Morales11ed52a2015-03-30 16:47:47 -0700207 uint32_t password_handle_length;
Andres Morales4a29dcb2015-03-26 10:21:47 -0700208 int ret;
209
Andres Morales21223c82015-06-03 14:39:14 -0700210 ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
Andres Morales4a29dcb2015-03-26 10:21:47 -0700211 &password_handle, &password_handle_length);
212
213 ASSERT_EQ(0, ret);
214
215 password_handle_t *handle = reinterpret_cast<password_handle_t *>(password_handle);
216 secure_id_t sid = handle->user_id;
217
Andres Morales21223c82015-06-03 14:39:14 -0700218 ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
Andres Morales4a29dcb2015-03-26 10:21:47 -0700219 &password_handle, &password_handle_length);
220
221 ASSERT_EQ(0, ret);
222 handle = reinterpret_cast<password_handle_t *>(password_handle);
223 ASSERT_NE(sid, handle->user_id);
224}
225
226
227TEST_F(GateKeeperDeviceTest, TrustedReEnroll) {
Andres Morales11ed52a2015-03-30 16:47:47 -0700228 uint32_t password_len = 50;
Andres Morales4a29dcb2015-03-26 10:21:47 -0700229 uint8_t password_payload[password_len];
230 uint8_t *password_handle;
Andres Morales11ed52a2015-03-30 16:47:47 -0700231 uint32_t password_handle_length;
Andres Morales4a29dcb2015-03-26 10:21:47 -0700232 int ret;
233
Andres Morales21223c82015-06-03 14:39:14 -0700234 ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
Andres Morales4a29dcb2015-03-26 10:21:47 -0700235 &password_handle, &password_handle_length);
236
237 ASSERT_EQ(0, ret);
238
239 password_handle_t *handle = reinterpret_cast<password_handle_t *>(password_handle);
240 secure_id_t sid = handle->user_id;
241
Andres Morales21223c82015-06-03 14:39:14 -0700242 ret = device->enroll(device, 400, password_handle, password_handle_length, password_payload,
Andres Morales4a29dcb2015-03-26 10:21:47 -0700243 password_len, password_payload, password_len, &password_handle, &password_handle_length);
244
245 ASSERT_EQ(0, ret);
246 handle = reinterpret_cast<password_handle_t *>(password_handle);
247 ASSERT_EQ(sid, handle->user_id);
248}
249