blob: 2e9391a3105b8195975b4616608ab1929e3b7988 [file] [log] [blame]
Andres Moralesac808182015-02-26 14:11:04 -08001/*
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 */
Andres Moralesb2abaa82015-03-03 09:09:18 -080016#include <UniquePtr.h>
Andres Moralesac808182015-02-26 14:11:04 -080017
Andres Morales7d0f0402015-03-19 18:02:55 -070018#include <gatekeeper/gatekeeper.h>
Andres Moralesac808182015-02-26 14:11:04 -080019
Andres Morales7d0f0402015-03-19 18:02:55 -070020namespace gatekeeper {
Andres Moralesac808182015-02-26 14:11:04 -080021
Andres Morales7d0f0402015-03-19 18:02:55 -070022void GateKeeper::Enroll(const EnrollRequest &request, EnrollResponse *response) {
Andres Moralesac808182015-02-26 14:11:04 -080023 if (response == NULL) return;
24
Andres Moralesb2abaa82015-03-03 09:09:18 -080025 if (!request.provided_password.buffer.get()) {
Andres Morales7d0f0402015-03-19 18:02:55 -070026 response->error = ERROR_INVALID;
Andres Moralesac808182015-02-26 14:11:04 -080027 return;
28 }
Andres Moralesb2abaa82015-03-03 09:09:18 -080029
Andres Moralesedd3e3d2015-03-12 13:30:15 -070030 secure_id_t user_id = 0;
Andres Moralesb2abaa82015-03-03 09:09:18 -080031
Andres Moralesedd3e3d2015-03-12 13:30:15 -070032 if (request.password_handle.buffer.get() == NULL) {
33 // Password handle does not match what is stored, generate new SecureID
34 GetRandom(&user_id, sizeof(secure_id_t));
35 } else {
36 if (!ValidatePasswordFile(request.user_id, request.password_handle)) {
Andres Morales7d0f0402015-03-19 18:02:55 -070037 response->error = ERROR_INVALID;
Andres Moralesedd3e3d2015-03-12 13:30:15 -070038 return;
39 } else {
40 // Password handle matches password file
41 password_handle_t *pw_handle =
42 reinterpret_cast<password_handle_t *>(request.password_handle.buffer.get());
43 if (!DoVerify(pw_handle, request.enrolled_password)) {
44 // incorrect old password
Andres Morales7d0f0402015-03-19 18:02:55 -070045 response->error = ERROR_INVALID;
Andres Moralesedd3e3d2015-03-12 13:30:15 -070046 return;
47 }
Andres Moralesb2abaa82015-03-03 09:09:18 -080048
Andres Moralesedd3e3d2015-03-12 13:30:15 -070049 user_id = pw_handle->user_id;
50 }
51 }
52
53 salt_t salt;
54 GetRandom(&salt, sizeof(salt));
55
56 secure_id_t authenticator_id;
57 GetRandom(&authenticator_id, sizeof(authenticator_id));
58
59
60 SizedBuffer password_handle;
61 if(!CreatePasswordHandle(&password_handle,
62 salt, user_id, authenticator_id, request.provided_password.buffer.get(),
63 request.provided_password.length)) {
Andres Morales7d0f0402015-03-19 18:02:55 -070064 response->error = ERROR_INVALID;
Andres Moralesedd3e3d2015-03-12 13:30:15 -070065 return;
66 }
67
68
69 WritePasswordFile(request.user_id, password_handle);
70
71 response->SetEnrolledPasswordHandle(&password_handle);
Andres Moralesac808182015-02-26 14:11:04 -080072}
73
Andres Morales7d0f0402015-03-19 18:02:55 -070074void GateKeeper::Verify(const VerifyRequest &request, VerifyResponse *response) {
Andres Moralesac808182015-02-26 14:11:04 -080075 if (response == NULL) return;
76
Andres Moralesb2abaa82015-03-03 09:09:18 -080077 if (!request.provided_password.buffer.get() || !request.password_handle.buffer.get()) {
Andres Morales7d0f0402015-03-19 18:02:55 -070078 response->error = ERROR_INVALID;
Andres Moralesac808182015-02-26 14:11:04 -080079 return;
80 }
81
Andres Moralesedd3e3d2015-03-12 13:30:15 -070082 secure_id_t user_id, authenticator_id;
83 password_handle_t *password_handle = reinterpret_cast<password_handle_t *>(
84 request.password_handle.buffer.get());
Andres Moralesb2abaa82015-03-03 09:09:18 -080085
Andres Moralesedd3e3d2015-03-12 13:30:15 -070086 // Sanity check
87 if (password_handle->version != HANDLE_VERSION) {
Andres Morales7d0f0402015-03-19 18:02:55 -070088 response->error = ERROR_INVALID;
Andres Moralesb2abaa82015-03-03 09:09:18 -080089 return;
90 }
91
Andres Moralesedd3e3d2015-03-12 13:30:15 -070092 if (!ValidatePasswordFile(request.user_id, request.password_handle)) {
93 // we don't allow access to keys if we can't validate the file.
94 // we must allow this case to support authentication before we decrypt
95 // /data, however.
96 user_id = 0;
97 authenticator_id = 0;
98 } else {
99 user_id = password_handle->user_id;
100 authenticator_id = password_handle->authenticator_id;
101 }
Andres Moralesb2abaa82015-03-03 09:09:18 -0800102
Andres Morales11c2a512015-03-23 10:10:44 -0700103 uint64_t timestamp = GetNanosecondsSinceBoot();
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700104
105 if (DoVerify(password_handle, request.provided_password)) {
Andres Moralesac808182015-02-26 14:11:04 -0800106 // Signature matches
107 SizedBuffer auth_token;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700108 MintAuthToken(&auth_token.buffer, &auth_token.length, timestamp,
109 user_id, authenticator_id);
Andres Moralesac808182015-02-26 14:11:04 -0800110 response->SetVerificationToken(&auth_token);
111 } else {
Andres Morales7d0f0402015-03-19 18:02:55 -0700112 response->error = ERROR_INVALID;
Andres Moralesac808182015-02-26 14:11:04 -0800113 }
114}
115
Andres Morales7d0f0402015-03-19 18:02:55 -0700116bool GateKeeper::CreatePasswordHandle(SizedBuffer *password_handle_buffer, salt_t salt,
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700117 secure_id_t user_id, secure_id_t authenticator_id, const uint8_t *password,
118 size_t password_length) {
119 password_handle_buffer->buffer.reset(new uint8_t[sizeof(password_handle_t)]);
120 password_handle_buffer->length = sizeof(password_handle_t);
121
122 password_handle_t *password_handle = reinterpret_cast<password_handle_t *>(
123 password_handle_buffer->buffer.get());
124 password_handle->version = HANDLE_VERSION;
125 password_handle->salt = salt;
126 password_handle->user_id = user_id;
127 password_handle->authenticator_id = authenticator_id;
128
129 size_t metadata_length = sizeof(user_id) /* user id */
130 + sizeof(authenticator_id) /* auth id */ + sizeof(uint8_t) /* version */;
131 uint8_t to_sign[password_length + metadata_length];
132 memcpy(to_sign, &password_handle->version, metadata_length);
133 memcpy(to_sign + metadata_length, password, password_length);
134
Andres Moralesf10e2892015-03-23 12:03:56 -0700135 const uint8_t *password_key = NULL;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700136 size_t password_key_length = 0;
137 GetPasswordKey(&password_key, &password_key_length);
138
Andres Moralesf10e2892015-03-23 12:03:56 -0700139 if (!password_key || password_key_length == 0) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700140 return false;
141 }
142
143 ComputePasswordSignature(password_handle->signature, sizeof(password_handle->signature),
Andres Moralesf10e2892015-03-23 12:03:56 -0700144 password_key, password_key_length, to_sign, sizeof(to_sign), salt);
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700145 return true;
146}
147
Andres Morales7d0f0402015-03-19 18:02:55 -0700148bool GateKeeper::DoVerify(const password_handle_t *expected_handle, const SizedBuffer &password) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700149 if (!password.buffer.get()) return false;
150
151 SizedBuffer provided_handle;
152 if (!CreatePasswordHandle(&provided_handle, expected_handle->salt, expected_handle->user_id,
153 expected_handle->authenticator_id, password.buffer.get(), password.length)) {
154 return false;
155 }
156
157 return memcmp_s(provided_handle.buffer.get(), expected_handle, sizeof(*expected_handle)) == 0;
158}
159
Andres Morales7d0f0402015-03-19 18:02:55 -0700160bool GateKeeper::ValidatePasswordFile(uint32_t uid, const SizedBuffer &provided_handle) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700161 SizedBuffer stored_handle;
162 ReadPasswordFile(uid, &stored_handle);
163
164 if (!stored_handle.buffer.get() || stored_handle.length == 0) return false;
165
166 // do we also verify the signature here?
167 return stored_handle.length == provided_handle.length &&
168 memcmp_s(stored_handle.buffer.get(), provided_handle.buffer.get(), stored_handle.length)
169 == 0;
170}
171
Andres Morales7d0f0402015-03-19 18:02:55 -0700172void GateKeeper::MintAuthToken(UniquePtr<uint8_t> *auth_token, size_t *length,
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700173 uint32_t timestamp, secure_id_t user_id, secure_id_t authenticator_id) {
Andres Moralesb2abaa82015-03-03 09:09:18 -0800174 if (auth_token == NULL) return;
175
176 AuthToken *token = new AuthToken;
Andres Moralesac808182015-02-26 14:11:04 -0800177 SizedBuffer serialized_auth_token;
178
Andres Morales5d39a972015-03-23 14:54:01 -0700179 token->auth_token_version = AUTH_TOKEN_VERSION;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700180 token->root_secure_user_id = user_id;
181 token->auxiliary_secure_user_id = authenticator_id;
Andres Morales5d39a972015-03-23 14:54:01 -0700182 token->authenticator_id = 0;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700183 token->timestamp = timestamp;
Andres Moralesac808182015-02-26 14:11:04 -0800184
Andres Moralesf10e2892015-03-23 12:03:56 -0700185 const uint8_t *auth_token_key = NULL;
186 size_t key_len = 0;
Andres Moralesb2abaa82015-03-03 09:09:18 -0800187 GetAuthTokenKey(&auth_token_key, &key_len);
188
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700189 size_t hash_len = (size_t)((uint8_t *)&token->hmac - (uint8_t *)token);
Andres Moralesf10e2892015-03-23 12:03:56 -0700190 ComputeSignature(token->hmac, sizeof(token->hmac), auth_token_key, key_len,
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700191 reinterpret_cast<uint8_t *>(token), hash_len);
Andres Moralesac808182015-02-26 14:11:04 -0800192
Andres Moralesac808182015-02-26 14:11:04 -0800193 if (length != NULL) *length = sizeof(AuthToken);
Andres Moralesb2abaa82015-03-03 09:09:18 -0800194 auth_token->reset(reinterpret_cast<uint8_t *>(token));
Andres Moralesac808182015-02-26 14:11:04 -0800195}
Andres Moralesb2abaa82015-03-03 09:09:18 -0800196
Andres Moralesac808182015-02-26 14:11:04 -0800197}