blob: 77e14e2b9333ed45864be6468df6a0bfdf832352 [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 Morales7d0f0402015-03-19 18:02:55 -070017#include <gatekeeper/gatekeeper.h>
Andres Moralesac808182015-02-26 14:11:04 -080018
Andres Moralesec9fd1d2015-04-20 07:45:50 -070019#include <endian.h>
20
Andres Morales7d0f0402015-03-19 18:02:55 -070021namespace gatekeeper {
Andres Moralesac808182015-02-26 14:11:04 -080022
Andres Morales7d0f0402015-03-19 18:02:55 -070023void GateKeeper::Enroll(const EnrollRequest &request, EnrollResponse *response) {
Andres Moralesac808182015-02-26 14:11:04 -080024 if (response == NULL) return;
25
Andres Moralesb2abaa82015-03-03 09:09:18 -080026 if (!request.provided_password.buffer.get()) {
Andres Morales7d0f0402015-03-19 18:02:55 -070027 response->error = ERROR_INVALID;
Andres Moralesac808182015-02-26 14:11:04 -080028 return;
29 }
Andres Moralesb2abaa82015-03-03 09:09:18 -080030
Andres Moralesedd3e3d2015-03-12 13:30:15 -070031 secure_id_t user_id = 0;
Andres Moralesb2abaa82015-03-03 09:09:18 -080032
Andres Moralesedd3e3d2015-03-12 13:30:15 -070033 if (request.password_handle.buffer.get() == NULL) {
34 // Password handle does not match what is stored, generate new SecureID
35 GetRandom(&user_id, sizeof(secure_id_t));
36 } else {
Andres Morales426fcfb2015-04-01 13:33:45 -070037 password_handle_t *pw_handle =
38 reinterpret_cast<password_handle_t *>(request.password_handle.buffer.get());
39 if (!DoVerify(pw_handle, request.enrolled_password)) {
40 // incorrect old password
41 response->error = ERROR_INVALID;
42 return;
Andres Moralesedd3e3d2015-03-12 13:30:15 -070043 }
Andres Morales426fcfb2015-04-01 13:33:45 -070044
45 user_id = pw_handle->user_id;
Andres Moralesedd3e3d2015-03-12 13:30:15 -070046 }
47
48 salt_t salt;
49 GetRandom(&salt, sizeof(salt));
50
51 secure_id_t authenticator_id;
52 GetRandom(&authenticator_id, sizeof(authenticator_id));
53
54
55 SizedBuffer password_handle;
56 if(!CreatePasswordHandle(&password_handle,
57 salt, user_id, authenticator_id, request.provided_password.buffer.get(),
58 request.provided_password.length)) {
Andres Morales7d0f0402015-03-19 18:02:55 -070059 response->error = ERROR_INVALID;
Andres Moralesedd3e3d2015-03-12 13:30:15 -070060 return;
61 }
62
Andres Moralesedd3e3d2015-03-12 13:30:15 -070063 response->SetEnrolledPasswordHandle(&password_handle);
Andres Moralesac808182015-02-26 14:11:04 -080064}
65
Andres Morales7d0f0402015-03-19 18:02:55 -070066void GateKeeper::Verify(const VerifyRequest &request, VerifyResponse *response) {
Andres Moralesac808182015-02-26 14:11:04 -080067 if (response == NULL) return;
68
Andres Moralesb2abaa82015-03-03 09:09:18 -080069 if (!request.provided_password.buffer.get() || !request.password_handle.buffer.get()) {
Andres Morales7d0f0402015-03-19 18:02:55 -070070 response->error = ERROR_INVALID;
Andres Moralesac808182015-02-26 14:11:04 -080071 return;
72 }
73
Andres Moralesedd3e3d2015-03-12 13:30:15 -070074 password_handle_t *password_handle = reinterpret_cast<password_handle_t *>(
75 request.password_handle.buffer.get());
Andres Moralesb2abaa82015-03-03 09:09:18 -080076
Andres Moralesedd3e3d2015-03-12 13:30:15 -070077 // Sanity check
78 if (password_handle->version != HANDLE_VERSION) {
Andres Morales7d0f0402015-03-19 18:02:55 -070079 response->error = ERROR_INVALID;
Andres Moralesb2abaa82015-03-03 09:09:18 -080080 return;
81 }
82
Andres Morales426fcfb2015-04-01 13:33:45 -070083 secure_id_t user_id = password_handle->user_id;
84 secure_id_t authenticator_id = password_handle->authenticator_id;
Andres Moralesb2abaa82015-03-03 09:09:18 -080085
Andres Moralesec9fd1d2015-04-20 07:45:50 -070086 uint64_t timestamp = GetMillisecondsSinceBoot();
Andres Moralesedd3e3d2015-03-12 13:30:15 -070087
88 if (DoVerify(password_handle, request.provided_password)) {
Andres Moralesac808182015-02-26 14:11:04 -080089 // Signature matches
90 SizedBuffer auth_token;
Andres Moralesedd3e3d2015-03-12 13:30:15 -070091 MintAuthToken(&auth_token.buffer, &auth_token.length, timestamp,
Andres Morales60343092015-04-09 19:01:10 -070092 user_id, authenticator_id, request.challenge);
Andres Moralesac808182015-02-26 14:11:04 -080093 response->SetVerificationToken(&auth_token);
94 } else {
Andres Morales7d0f0402015-03-19 18:02:55 -070095 response->error = ERROR_INVALID;
Andres Moralesac808182015-02-26 14:11:04 -080096 }
97}
98
Andres Morales7d0f0402015-03-19 18:02:55 -070099bool GateKeeper::CreatePasswordHandle(SizedBuffer *password_handle_buffer, salt_t salt,
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700100 secure_id_t user_id, secure_id_t authenticator_id, const uint8_t *password,
Andres Morales11ed52a2015-03-30 16:47:47 -0700101 uint32_t password_length) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700102 password_handle_buffer->buffer.reset(new uint8_t[sizeof(password_handle_t)]);
103 password_handle_buffer->length = sizeof(password_handle_t);
104
105 password_handle_t *password_handle = reinterpret_cast<password_handle_t *>(
106 password_handle_buffer->buffer.get());
107 password_handle->version = HANDLE_VERSION;
108 password_handle->salt = salt;
109 password_handle->user_id = user_id;
110 password_handle->authenticator_id = authenticator_id;
111
Andres Morales11ed52a2015-03-30 16:47:47 -0700112 uint32_t metadata_length = sizeof(user_id) /* user id */
Andres Morales94b201e2015-04-16 13:47:47 -0700113 + sizeof(authenticator_id) /* auth id */ + sizeof(HANDLE_VERSION) /* version */;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700114 uint8_t to_sign[password_length + metadata_length];
Andres Morales94b201e2015-04-16 13:47:47 -0700115 memcpy(to_sign, password_handle, metadata_length);
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700116 memcpy(to_sign + metadata_length, password, password_length);
117
Andres Moralesf10e2892015-03-23 12:03:56 -0700118 const uint8_t *password_key = NULL;
Andres Morales11ed52a2015-03-30 16:47:47 -0700119 uint32_t password_key_length = 0;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700120 GetPasswordKey(&password_key, &password_key_length);
121
Andres Moralesf10e2892015-03-23 12:03:56 -0700122 if (!password_key || password_key_length == 0) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700123 return false;
124 }
125
126 ComputePasswordSignature(password_handle->signature, sizeof(password_handle->signature),
Andres Moralesf10e2892015-03-23 12:03:56 -0700127 password_key, password_key_length, to_sign, sizeof(to_sign), salt);
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700128 return true;
129}
130
Andres Morales7d0f0402015-03-19 18:02:55 -0700131bool GateKeeper::DoVerify(const password_handle_t *expected_handle, const SizedBuffer &password) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700132 if (!password.buffer.get()) return false;
133
134 SizedBuffer provided_handle;
135 if (!CreatePasswordHandle(&provided_handle, expected_handle->salt, expected_handle->user_id,
136 expected_handle->authenticator_id, password.buffer.get(), password.length)) {
137 return false;
138 }
139
140 return memcmp_s(provided_handle.buffer.get(), expected_handle, sizeof(*expected_handle)) == 0;
141}
142
Andres Morales11ed52a2015-03-30 16:47:47 -0700143void GateKeeper::MintAuthToken(UniquePtr<uint8_t> *auth_token, uint32_t *length,
Andres Morales652f0762015-04-13 10:23:20 -0700144 uint64_t timestamp, secure_id_t user_id, secure_id_t authenticator_id,
Andres Morales60343092015-04-09 19:01:10 -0700145 uint64_t challenge) {
Andres Moralesb2abaa82015-03-03 09:09:18 -0800146 if (auth_token == NULL) return;
147
Andres Moralesfa104e12015-04-08 12:36:44 -0700148 hw_auth_token_t *token = new hw_auth_token_t;
Andres Moralesac808182015-02-26 14:11:04 -0800149 SizedBuffer serialized_auth_token;
150
Andres Moralesfa104e12015-04-08 12:36:44 -0700151 token->version = HW_AUTH_TOKEN_VERSION;
Andres Morales60343092015-04-09 19:01:10 -0700152 token->challenge = challenge;
Andres Moralesfa104e12015-04-08 12:36:44 -0700153 token->user_id = user_id;
154 token->authenticator_id = authenticator_id;
155 token->authenticator_type = htonl(HW_AUTH_PASSWORD);
Andres Moralesec9fd1d2015-04-20 07:45:50 -0700156 token->timestamp = htobe64(timestamp);
Andres Moralesac808182015-02-26 14:11:04 -0800157
Andres Moralesf10e2892015-03-23 12:03:56 -0700158 const uint8_t *auth_token_key = NULL;
Andres Morales11ed52a2015-03-30 16:47:47 -0700159 uint32_t key_len = 0;
Andres Morales58bb2462015-04-10 18:27:50 -0700160 if (GetAuthTokenKey(&auth_token_key, &key_len)) {
161 uint32_t hash_len = (uint32_t)((uint8_t *)&token->hmac - (uint8_t *)token);
162 ComputeSignature(token->hmac, sizeof(token->hmac), auth_token_key, key_len,
163 reinterpret_cast<uint8_t *>(token), hash_len);
164 } else {
165 memset(token->hmac, 0, sizeof(token->hmac));
166 }
Andres Moralesac808182015-02-26 14:11:04 -0800167
Andres Moralesfa104e12015-04-08 12:36:44 -0700168 if (length != NULL) *length = sizeof(*token);
Andres Moralesb2abaa82015-03-03 09:09:18 -0800169 auth_token->reset(reinterpret_cast<uint8_t *>(token));
Andres Moralesac808182015-02-26 14:11:04 -0800170}
Andres Moralesb2abaa82015-03-03 09:09:18 -0800171
Andres Moralesac808182015-02-26 14:11:04 -0800172}