gatekeeper_device_test.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  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. */
  16. #include <endian.h>
  17. #include <gtest/gtest.h>
  18. #include <hardware/gatekeeper.h>
  19. #include <gatekeeper/gatekeeper.h> // For password_handle_t
  20. #include <unistd.h>
  21. using ::testing::Test;
  22. using ::gatekeeper::password_handle_t;
  23. using ::gatekeeper::secure_id_t;
  24. class GateKeeperDeviceTest : public virtual Test {
  25. public:
  26. GateKeeperDeviceTest() {}
  27. virtual ~GateKeeperDeviceTest() {}
  28. virtual void SetUp() {
  29. gatekeeper_device_initialize(&device);
  30. }
  31. virtual void TearDown() {
  32. gatekeeper_close(device);
  33. }
  34. static void gatekeeper_device_initialize(gatekeeper_device_t **dev) {
  35. int ret;
  36. const hw_module_t *mod;
  37. ret = hw_get_module_by_class(GATEKEEPER_HARDWARE_MODULE_ID, NULL, &mod);
  38. ASSERT_EQ(0, ret);
  39. ret = gatekeeper_open(mod, dev);
  40. ASSERT_EQ(0, ret);
  41. }
  42. gatekeeper_device_t *device;
  43. };
  44. TEST_F(GateKeeperDeviceTest, EnrollAndVerifyStress) {
  45. uint32_t password_len = 50;
  46. uint8_t password_payload[password_len];
  47. uint8_t *password_handle;
  48. uint32_t password_handle_length;
  49. uint8_t *auth_token;
  50. uint32_t auth_token_len;
  51. int ret;
  52. ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
  53. &password_handle, &password_handle_length);
  54. ASSERT_EQ(0, ret);
  55. for (int i = 0; i < 1000; i++) {
  56. bool should_reenroll;
  57. ret = device->verify(device, 400, 0, password_handle, password_handle_length,
  58. password_payload, password_len, &auth_token, &auth_token_len, &should_reenroll);
  59. ASSERT_EQ(0, ret);
  60. }
  61. }
  62. TEST_F(GateKeeperDeviceTest, EnrollAndVerify) {
  63. uint32_t password_len = 50;
  64. uint8_t password_payload[password_len];
  65. uint8_t *password_handle;
  66. uint32_t password_handle_length;
  67. uint8_t *auth_token;
  68. uint32_t auth_token_len;
  69. hw_auth_token_t *hat;
  70. int ret;
  71. ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
  72. &password_handle, &password_handle_length);
  73. ASSERT_EQ(0, ret);
  74. bool should_reenroll;
  75. ret = device->verify(device, 400, 0, password_handle, password_handle_length,
  76. password_payload, password_len, &auth_token, &auth_token_len, &should_reenroll);
  77. ASSERT_EQ(0, should_reenroll);
  78. ASSERT_EQ(0, ret);
  79. hat = reinterpret_cast<hw_auth_token_t *>(auth_token);
  80. ASSERT_EQ(HW_AUTH_TOKEN_VERSION, hat->version);
  81. ASSERT_EQ(htobe32(HW_AUTH_PASSWORD), hat->authenticator_type);
  82. }
  83. TEST_F(GateKeeperDeviceTest, EnrollAndVerifyTimeout) {
  84. uint32_t password_len = 50;
  85. uint8_t password_payload[password_len];
  86. uint8_t *password_handle;
  87. uint32_t password_handle_length;
  88. uint8_t *auth_token = NULL;
  89. uint32_t auth_token_len;
  90. bool should_reenroll;
  91. int ret;
  92. ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
  93. &password_handle, &password_handle_length);
  94. ASSERT_EQ(0, ret);
  95. int payload_val = password_payload[0];
  96. password_payload[0] = 4;
  97. int timeout = 0;
  98. for (int i = 0; i < 20; i++) {
  99. bool should_reenroll;
  100. ret = device->verify(device, 400, 0, password_handle, password_handle_length,
  101. password_payload, password_len, &auth_token, &auth_token_len,
  102. &should_reenroll);
  103. ASSERT_NE(0, ret);
  104. ASSERT_EQ(NULL, auth_token);
  105. if (ret > 0) {
  106. timeout = ret;
  107. }
  108. }
  109. ASSERT_NE(0, timeout);
  110. sleep((timeout + 999)/ 1000);
  111. password_payload[0] = payload_val;
  112. ret = device->verify(device, 400, 0, password_handle, password_handle_length,
  113. password_payload, password_len, &auth_token, &auth_token_len,
  114. &should_reenroll);
  115. ASSERT_EQ(0, ret);
  116. }
  117. TEST_F(GateKeeperDeviceTest, EnrollAndVerifyBadPassword) {
  118. uint32_t password_len = 50;
  119. uint8_t password_payload[password_len];
  120. uint8_t *password_handle;
  121. uint32_t password_handle_length;
  122. uint8_t *auth_token = NULL;
  123. uint32_t auth_token_len;
  124. int ret;
  125. ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
  126. &password_handle, &password_handle_length);
  127. ASSERT_EQ(0, ret);
  128. password_payload[0] = 4;
  129. bool should_reenroll;
  130. ret = device->verify(device, 400, 0, password_handle, password_handle_length,
  131. password_payload, password_len, &auth_token, &auth_token_len,
  132. &should_reenroll);
  133. ASSERT_NE(0, ret);
  134. ASSERT_EQ(NULL, auth_token);
  135. }
  136. TEST_F(GateKeeperDeviceTest, MinFailedAttemptsBeforeLockout) {
  137. uint32_t password_len = 50;
  138. uint8_t password_payload[password_len];
  139. uint8_t *password_handle;
  140. uint32_t password_handle_length;
  141. uint8_t *auth_token = NULL;
  142. uint32_t auth_token_len;
  143. int ret;
  144. ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
  145. &password_handle, &password_handle_length);
  146. ASSERT_EQ(0, ret);
  147. password_payload[0] = 4;
  148. // User should have at least 4 attempts before being locked out
  149. static const int MIN_FAILED_ATTEMPTS = 4;
  150. bool should_reenroll;
  151. for (int i = 0; i < MIN_FAILED_ATTEMPTS; i++) {
  152. ret = device->verify(device, 400, 0, password_handle, password_handle_length,
  153. password_payload, password_len, &auth_token, &auth_token_len,
  154. &should_reenroll);
  155. // shoudln't be a timeout
  156. ASSERT_LT(ret, 0);
  157. }
  158. }
  159. TEST_F(GateKeeperDeviceTest, UntrustedReEnroll) {
  160. uint32_t password_len = 50;
  161. uint8_t password_payload[password_len];
  162. uint8_t *password_handle;
  163. uint32_t password_handle_length;
  164. int ret;
  165. ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
  166. &password_handle, &password_handle_length);
  167. ASSERT_EQ(0, ret);
  168. password_handle_t *handle = reinterpret_cast<password_handle_t *>(password_handle);
  169. secure_id_t sid = handle->user_id;
  170. ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
  171. &password_handle, &password_handle_length);
  172. ASSERT_EQ(0, ret);
  173. handle = reinterpret_cast<password_handle_t *>(password_handle);
  174. ASSERT_NE(sid, handle->user_id);
  175. }
  176. TEST_F(GateKeeperDeviceTest, TrustedReEnroll) {
  177. uint32_t password_len = 50;
  178. uint8_t password_payload[password_len];
  179. uint8_t *password_handle;
  180. uint32_t password_handle_length;
  181. int ret;
  182. ret = device->enroll(device, 400, NULL, 0, NULL, 0, password_payload, password_len,
  183. &password_handle, &password_handle_length);
  184. ASSERT_EQ(0, ret);
  185. password_handle_t *handle = reinterpret_cast<password_handle_t *>(password_handle);
  186. secure_id_t sid = handle->user_id;
  187. ret = device->enroll(device, 400, password_handle, password_handle_length, password_payload,
  188. password_len, password_payload, password_len, &password_handle, &password_handle_length);
  189. ASSERT_EQ(0, ret);
  190. handle = reinterpret_cast<password_handle_t *>(password_handle);
  191. ASSERT_EQ(sid, handle->user_id);
  192. }