blob: 1fc3682a80bc7fd0855b0b5ca80c059eb384eaab [file] [log] [blame]
A. Cody Schuffelen097e6252024-04-02 16:53:34 -07001/*
2 * Copyright (C) 2016 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#define LOG_TAG "android.hardware.gatekeeper-service.nonsecure"
17
18#include <endian.h>
19
20#include <android-base/logging.h>
21
22#include "GateKeeper.h"
23
24using ::gatekeeper::EnrollRequest;
25using ::gatekeeper::EnrollResponse;
26using ::gatekeeper::ERROR_NONE;
27using ::gatekeeper::ERROR_RETRY;
28using ::gatekeeper::SizedBuffer;
29using ::gatekeeper::VerifyRequest;
30using ::gatekeeper::VerifyResponse;
31
32namespace aidl::android::hardware::gatekeeper {
33
34SizedBuffer vec2sized_buffer(const std::vector<uint8_t>& vec) {
35 if (vec.size() == 0 || vec.size() > std::numeric_limits<uint32_t>::max()) {
36 return {};
37 }
38 auto unused = new uint8_t[vec.size()];
39 std::copy(vec.begin(), vec.end(), unused);
40 return {unused, static_cast<uint32_t>(vec.size())};
41}
42
43void sizedBuffer2AidlHWToken(SizedBuffer& buffer,
44 android::hardware::security::keymint::HardwareAuthToken* aidlToken) {
45 const hw_auth_token_t* authToken =
46 reinterpret_cast<const hw_auth_token_t*>(buffer.Data<uint8_t>());
47 aidlToken->challenge = authToken->challenge;
48 aidlToken->userId = authToken->user_id;
49 aidlToken->authenticatorId = authToken->authenticator_id;
50 // these are in network order: translate to host
51 aidlToken->authenticatorType =
52 static_cast<android::hardware::security::keymint::HardwareAuthenticatorType>(
53 be32toh(authToken->authenticator_type));
54 aidlToken->timestamp.milliSeconds = be64toh(authToken->timestamp);
55 aidlToken->mac.insert(aidlToken->mac.begin(), std::begin(authToken->hmac),
56 std::end(authToken->hmac));
57}
58
59SoftGateKeeperDevice::SoftGateKeeperDevice(::gatekeeper::SoftGateKeeper& impl) : impl_(impl) {}
60
61::ndk::ScopedAStatus SoftGateKeeperDevice::enroll(int32_t uid,
62 const std::vector<uint8_t>& currentPasswordHandle,
63 const std::vector<uint8_t>& currentPassword,
64 const std::vector<uint8_t>& desiredPassword,
65 GatekeeperEnrollResponse* rsp) {
66 if (desiredPassword.size() == 0) {
67 LOG(ERROR) << "Desired password size is 0";
68 return ndk::ScopedAStatus(AStatus_fromServiceSpecificError(ERROR_GENERAL_FAILURE));
69 }
70
71 if (currentPasswordHandle.size() > 0) {
72 if (currentPasswordHandle.size() != sizeof(::gatekeeper::password_handle_t)) {
73 LOG(ERROR) << "Password handle has wrong length";
74 return ndk::ScopedAStatus(AStatus_fromServiceSpecificError(ERROR_GENERAL_FAILURE));
75 }
76 }
77
78 EnrollRequest request(uid, vec2sized_buffer(currentPasswordHandle),
79 vec2sized_buffer(desiredPassword), vec2sized_buffer(currentPassword));
80 EnrollResponse response;
81 impl_.Enroll(request, &response);
82 if (response.error == ERROR_RETRY) {
83 LOG(ERROR) << "Enroll response has a retry error";
84 *rsp = {ERROR_RETRY_TIMEOUT, static_cast<int32_t>(response.retry_timeout), 0, {}};
85 return ndk::ScopedAStatus::ok();
86 } else if (response.error != ERROR_NONE) {
87 LOG(ERROR) << "Enroll response has an error: " << response.error;
88 return ndk::ScopedAStatus(AStatus_fromServiceSpecificError(ERROR_GENERAL_FAILURE));
89 } else {
90 const ::gatekeeper::password_handle_t* password_handle =
91 response.enrolled_password_handle.Data<::gatekeeper::password_handle_t>();
92 *rsp = {STATUS_OK,
93 0,
94 static_cast<int64_t>(password_handle->user_id),
95 {response.enrolled_password_handle.Data<uint8_t>(),
96 (response.enrolled_password_handle.Data<uint8_t>() +
97 response.enrolled_password_handle.size())}};
98 }
99 return ndk::ScopedAStatus::ok();
100}
101
102::ndk::ScopedAStatus SoftGateKeeperDevice::verify(
103 int32_t uid, int64_t challenge, const std::vector<uint8_t>& enrolledPasswordHandle,
104 const std::vector<uint8_t>& providedPassword, GatekeeperVerifyResponse* rsp) {
105 if (enrolledPasswordHandle.size() == 0) {
106 LOG(ERROR) << "Enrolled password size is 0";
107 return ndk::ScopedAStatus(AStatus_fromServiceSpecificError(ERROR_GENERAL_FAILURE));
108 }
109
110 if (enrolledPasswordHandle.size() > 0) {
111 if (enrolledPasswordHandle.size() != sizeof(::gatekeeper::password_handle_t)) {
112 LOG(ERROR) << "Password handle has wrong length";
113 return ndk::ScopedAStatus(AStatus_fromServiceSpecificError(ERROR_GENERAL_FAILURE));
114 }
115 }
116
117 VerifyRequest request(uid, challenge, vec2sized_buffer(enrolledPasswordHandle),
118 vec2sized_buffer(providedPassword));
119 VerifyResponse response;
120 impl_.Verify(request, &response);
121
122 if (response.error == ERROR_RETRY) {
123 LOG(ERROR) << "Verify request response gave retry error";
124 *rsp = {ERROR_RETRY_TIMEOUT, static_cast<int32_t>(response.retry_timeout), {}};
125 return ndk::ScopedAStatus::ok();
126 } else if (response.error != ERROR_NONE) {
127 LOG(ERROR) << "Verify request response gave error: " << response.error;
128 return ndk::ScopedAStatus(AStatus_fromServiceSpecificError(ERROR_GENERAL_FAILURE));
129 } else {
130 // On Success, return GatekeeperVerifyResponse with Success Status, timeout{0} and
131 // valid HardwareAuthToken.
132 *rsp = {response.request_reenroll ? STATUS_REENROLL : STATUS_OK, 0, {}};
133 // Convert the hw_auth_token_t to HardwareAuthToken in the response.
134 sizedBuffer2AidlHWToken(response.auth_token, &rsp->hardwareAuthToken);
135 }
136 return ndk::ScopedAStatus::ok();
137}
138
139::ndk::ScopedAStatus SoftGateKeeperDevice::deleteUser(int32_t /*uid*/) {
140 LOG(ERROR) << "deleteUser is unimplemented";
141 return ndk::ScopedAStatus(AStatus_fromServiceSpecificError(ERROR_NOT_IMPLEMENTED));
142}
143
144::ndk::ScopedAStatus SoftGateKeeperDevice::deleteAllUsers() {
145 LOG(ERROR) << "deleteAllUsers is unimplemented";
146 return ndk::ScopedAStatus(AStatus_fromServiceSpecificError(ERROR_NOT_IMPLEMENTED));
147}
148
149} // namespace aidl::android::hardware::gatekeeper