gatekeeper_test.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 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 <arpa/inet.h>
  17. #include <iostream>
  18. #include <gtest/gtest.h>
  19. #include <hardware/hw_auth_token.h>
  20. #include "../SoftGateKeeper.h"
  21. using ::gatekeeper::SizedBuffer;
  22. using ::testing::Test;
  23. using ::gatekeeper::EnrollRequest;
  24. using ::gatekeeper::EnrollResponse;
  25. using ::gatekeeper::VerifyRequest;
  26. using ::gatekeeper::VerifyResponse;
  27. using ::gatekeeper::SoftGateKeeper;
  28. using ::gatekeeper::secure_id_t;
  29. static void do_enroll(SoftGateKeeper &gatekeeper, EnrollResponse *response) {
  30. SizedBuffer password;
  31. password.buffer.reset(new uint8_t[16]);
  32. password.length = 16;
  33. memset(password.buffer.get(), 0, 16);
  34. EnrollRequest request(0, NULL, &password, NULL);
  35. gatekeeper.Enroll(request, response);
  36. }
  37. TEST(GateKeeperTest, EnrollSuccess) {
  38. SoftGateKeeper gatekeeper;
  39. EnrollResponse response;
  40. do_enroll(gatekeeper, &response);
  41. ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, response.error);
  42. }
  43. TEST(GateKeeperTest, EnrollBogusData) {
  44. SoftGateKeeper gatekeeper;
  45. SizedBuffer password;
  46. EnrollResponse response;
  47. EnrollRequest request(0, NULL, &password, NULL);
  48. gatekeeper.Enroll(request, &response);
  49. ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_INVALID, response.error);
  50. }
  51. TEST(GateKeeperTest, VerifySuccess) {
  52. SoftGateKeeper gatekeeper;
  53. SizedBuffer provided_password;
  54. EnrollResponse enroll_response;
  55. provided_password.buffer.reset(new uint8_t[16]);
  56. provided_password.length = 16;
  57. memset(provided_password.buffer.get(), 0, 16);
  58. do_enroll(gatekeeper, &enroll_response);
  59. ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, enroll_response.error);
  60. VerifyRequest request(0, 1, &enroll_response.enrolled_password_handle,
  61. &provided_password);
  62. VerifyResponse response;
  63. gatekeeper.Verify(request, &response);
  64. ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, response.error);
  65. hw_auth_token_t *auth_token =
  66. reinterpret_cast<hw_auth_token_t *>(response.auth_token.buffer.get());
  67. ASSERT_EQ((uint32_t) HW_AUTH_PASSWORD, ntohl(auth_token->authenticator_type));
  68. ASSERT_EQ((uint64_t) 1, auth_token->challenge);
  69. ASSERT_NE(~((uint32_t) 0), auth_token->timestamp);
  70. ASSERT_NE((uint64_t) 0, auth_token->user_id);
  71. ASSERT_NE((uint64_t) 0, auth_token->authenticator_id);
  72. }
  73. TEST(GateKeeperTest, TrustedReEnroll) {
  74. SoftGateKeeper gatekeeper;
  75. SizedBuffer provided_password;
  76. EnrollResponse enroll_response;
  77. SizedBuffer password_handle;
  78. // do_enroll enrolls an all 0 password
  79. provided_password.buffer.reset(new uint8_t[16]);
  80. provided_password.length = 16;
  81. memset(provided_password.buffer.get(), 0, 16);
  82. do_enroll(gatekeeper, &enroll_response);
  83. ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, enroll_response.error);
  84. // keep a copy of the handle
  85. password_handle.buffer.reset(new uint8_t[enroll_response.enrolled_password_handle.length]);
  86. password_handle.length = enroll_response.enrolled_password_handle.length;
  87. memcpy(password_handle.buffer.get(), enroll_response.enrolled_password_handle.buffer.get(),
  88. password_handle.length);
  89. // verify first password
  90. VerifyRequest request(0, 0, &enroll_response.enrolled_password_handle,
  91. &provided_password);
  92. VerifyResponse response;
  93. gatekeeper.Verify(request, &response);
  94. ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, response.error);
  95. hw_auth_token_t *auth_token =
  96. reinterpret_cast<hw_auth_token_t *>(response.auth_token.buffer.get());
  97. secure_id_t secure_id = auth_token->user_id;
  98. // enroll new password
  99. provided_password.buffer.reset(new uint8_t[16]);
  100. provided_password.length = 16;
  101. memset(provided_password.buffer.get(), 0, 16);
  102. SizedBuffer password;
  103. password.buffer.reset(new uint8_t[16]);
  104. memset(password.buffer.get(), 1, 16);
  105. password.length = 16;
  106. EnrollRequest enroll_request(0, &password_handle, &password, &provided_password);
  107. gatekeeper.Enroll(enroll_request, &enroll_response);
  108. ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, enroll_response.error);
  109. // verify new password
  110. password.buffer.reset(new uint8_t[16]);
  111. memset(password.buffer.get(), 1, 16);
  112. password.length = 16;
  113. VerifyRequest new_request(0, 0, &enroll_response.enrolled_password_handle,
  114. &password);
  115. gatekeeper.Verify(new_request, &response);
  116. ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, response.error);
  117. ASSERT_EQ(secure_id,
  118. reinterpret_cast<hw_auth_token_t *>(response.auth_token.buffer.get())->user_id);
  119. }
  120. TEST(GateKeeperTest, UntrustedReEnroll) {
  121. SoftGateKeeper gatekeeper;
  122. SizedBuffer provided_password;
  123. EnrollResponse enroll_response;
  124. // do_enroll enrolls an all 0 password
  125. provided_password.buffer.reset(new uint8_t[16]);
  126. provided_password.length = 16;
  127. memset(provided_password.buffer.get(), 0, 16);
  128. do_enroll(gatekeeper, &enroll_response);
  129. ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, enroll_response.error);
  130. // verify first password
  131. VerifyRequest request(0, 0, &enroll_response.enrolled_password_handle,
  132. &provided_password);
  133. VerifyResponse response;
  134. gatekeeper.Verify(request, &response);
  135. ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, response.error);
  136. hw_auth_token_t *auth_token =
  137. reinterpret_cast<hw_auth_token_t *>(response.auth_token.buffer.get());
  138. secure_id_t secure_id = auth_token->user_id;
  139. // enroll new password
  140. SizedBuffer password;
  141. password.buffer.reset(new uint8_t[16]);
  142. memset(password.buffer.get(), 1, 16);
  143. password.length = 16;
  144. EnrollRequest enroll_request(0, NULL, &password, NULL);
  145. gatekeeper.Enroll(enroll_request, &enroll_response);
  146. ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, enroll_response.error);
  147. // verify new password
  148. password.buffer.reset(new uint8_t[16]);
  149. memset(password.buffer.get(), 1, 16);
  150. password.length = 16;
  151. VerifyRequest new_request(0, 0, &enroll_response.enrolled_password_handle,
  152. &password);
  153. gatekeeper.Verify(new_request, &response);
  154. ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_NONE, response.error);
  155. ASSERT_NE(secure_id,
  156. reinterpret_cast<hw_auth_token_t *>(response.auth_token.buffer.get())->user_id);
  157. }
  158. TEST(GateKeeperTest, VerifyBogusData) {
  159. SoftGateKeeper gatekeeper;
  160. SizedBuffer provided_password;
  161. SizedBuffer password_handle;
  162. VerifyResponse response;
  163. VerifyRequest request(0, 0, &provided_password, &password_handle);
  164. gatekeeper.Verify(request, &response);
  165. ASSERT_EQ(::gatekeeper::gatekeeper_error_t::ERROR_INVALID, response.error);
  166. }