SoftGateKeeperDevice.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "SoftGateKeeper.h"
  17. #include "SoftGateKeeperDevice.h"
  18. namespace android {
  19. int SoftGateKeeperDevice::enroll(uint32_t uid,
  20. const uint8_t *current_password_handle, uint32_t current_password_handle_length,
  21. const uint8_t *current_password, uint32_t current_password_length,
  22. const uint8_t *desired_password, uint32_t desired_password_length,
  23. uint8_t **enrolled_password_handle, uint32_t *enrolled_password_handle_length) {
  24. if (enrolled_password_handle == NULL || enrolled_password_handle_length == NULL ||
  25. desired_password == NULL || desired_password_length == 0)
  26. return -EINVAL;
  27. // Current password and current password handle go together
  28. if (current_password_handle == NULL || current_password_handle_length == 0 ||
  29. current_password == NULL || current_password_length == 0) {
  30. current_password_handle = NULL;
  31. current_password_handle_length = 0;
  32. current_password = NULL;
  33. current_password_length = 0;
  34. }
  35. SizedBuffer desired_password_buffer(desired_password_length);
  36. memcpy(desired_password_buffer.buffer.get(), desired_password, desired_password_length);
  37. SizedBuffer current_password_handle_buffer(current_password_handle_length);
  38. if (current_password_handle) {
  39. memcpy(current_password_handle_buffer.buffer.get(), current_password_handle,
  40. current_password_handle_length);
  41. }
  42. SizedBuffer current_password_buffer(current_password_length);
  43. if (current_password) {
  44. memcpy(current_password_buffer.buffer.get(), current_password, current_password_length);
  45. }
  46. EnrollRequest request(uid, &current_password_handle_buffer, &desired_password_buffer,
  47. &current_password_buffer);
  48. EnrollResponse response;
  49. impl_->Enroll(request, &response);
  50. if (response.error == ERROR_RETRY) {
  51. return response.retry_timeout;
  52. } else if (response.error != ERROR_NONE) {
  53. return -EINVAL;
  54. }
  55. *enrolled_password_handle = response.enrolled_password_handle.buffer.release();
  56. *enrolled_password_handle_length = response.enrolled_password_handle.length;
  57. return 0;
  58. }
  59. int SoftGateKeeperDevice::verify(uint32_t uid,
  60. uint64_t challenge, const uint8_t *enrolled_password_handle,
  61. uint32_t enrolled_password_handle_length, const uint8_t *provided_password,
  62. uint32_t provided_password_length, uint8_t **auth_token, uint32_t *auth_token_length,
  63. bool *request_reenroll) {
  64. if (enrolled_password_handle == NULL ||
  65. provided_password == NULL) {
  66. return -EINVAL;
  67. }
  68. SizedBuffer password_handle_buffer(enrolled_password_handle_length);
  69. memcpy(password_handle_buffer.buffer.get(), enrolled_password_handle,
  70. enrolled_password_handle_length);
  71. SizedBuffer provided_password_buffer(provided_password_length);
  72. memcpy(provided_password_buffer.buffer.get(), provided_password, provided_password_length);
  73. VerifyRequest request(uid, challenge, &password_handle_buffer, &provided_password_buffer);
  74. VerifyResponse response;
  75. impl_->Verify(request, &response);
  76. if (response.error == ERROR_RETRY) {
  77. return response.retry_timeout;
  78. } else if (response.error != ERROR_NONE) {
  79. return -EINVAL;
  80. }
  81. if (auth_token != NULL && auth_token_length != NULL) {
  82. *auth_token = response.auth_token.buffer.release();
  83. *auth_token_length = response.auth_token.length;
  84. }
  85. if (request_reenroll != NULL) {
  86. *request_reenroll = response.request_reenroll;
  87. }
  88. return 0;
  89. }
  90. } // namespace android