1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/proximity_auth/device_to_device_initiator_operations.h"
#include "base/bind.h"
#include "base/callback.h"
#include "components/cryptauth/proto/cryptauth_api.pb.h"
#include "components/cryptauth/proto/securemessage.pb.h"
#include "components/cryptauth/secure_message_delegate.h"
#include "components/proximity_auth/logging/logging.h"
namespace proximity_auth {
namespace {
// TODO(tengs): Due to a bug with the ChromeOS secure message daemon, we cannot
// create SecureMessages with empty payloads. To workaround this bug, this value
// is put into the payload if it would otherwise be empty.
// See crbug.com/512894.
const char kPayloadFiller[] = "\xae";
// The version to put in the GcmMetadata field.
const int kGcmMetadataVersion = 1;
// Callback for DeviceToDeviceInitiatorOperations::CreateInitiatorAuthMessage(),
// after the inner message is created.
void OnInnerMessageCreatedForInitiatorAuth(
const std::string& session_symmetric_key,
cryptauth::SecureMessageDelegate* secure_message_delegate,
const DeviceToDeviceInitiatorOperations::MessageCallback& callback,
const std::string& inner_message) {
if (inner_message.empty()) {
PA_LOG(INFO) << "Failed to create inner message for [Initiator Auth].";
callback.Run(std::string());
return;
}
cryptauth::GcmMetadata gcm_metadata;
gcm_metadata.set_type(cryptauth::DEVICE_TO_DEVICE_MESSAGE);
gcm_metadata.set_version(kGcmMetadataVersion);
// Store the inner message inside a DeviceToDeviceMessage proto.
securemessage::DeviceToDeviceMessage device_to_device_message;
device_to_device_message.set_message(inner_message);
device_to_device_message.set_sequence_number(2);
// Create and return the outer message, which wraps the inner message.
cryptauth::SecureMessageDelegate::CreateOptions create_options;
create_options.encryption_scheme = securemessage::AES_256_CBC;
create_options.signature_scheme = securemessage::HMAC_SHA256;
gcm_metadata.SerializeToString(&create_options.public_metadata);
secure_message_delegate->CreateSecureMessage(
device_to_device_message.SerializeAsString(), session_symmetric_key,
create_options, callback);
}
// Helper struct containing all the context needed to validate the
// [Responder Auth] message.
struct ValidateResponderAuthMessageContext {
std::string responder_auth_message;
std::string persistent_responder_public_key;
std::string persistent_symmetric_key;
std::string session_private_key;
std::string hello_message;
cryptauth::SecureMessageDelegate* secure_message_delegate;
DeviceToDeviceInitiatorOperations::ValidateResponderAuthCallback callback;
std::string responder_session_public_key;
std::string session_symmetric_key;
};
// Forward declarations of functions used in the [Responder Auth] validation
// flow. These functions are declared in order in which they are called during
// the flow.
void BeginResponderAuthValidation(ValidateResponderAuthMessageContext context);
void OnSessionSymmetricKeyDerived(ValidateResponderAuthMessageContext context,
const std::string& session_symmetric_key);
void OnOuterMessageUnwrappedForResponderAuth(
const ValidateResponderAuthMessageContext& context,
bool verified,
const std::string& payload,
const securemessage::Header& header);
void OnMiddleMessageUnwrappedForResponderAuth(
const ValidateResponderAuthMessageContext& context,
bool verified,
const std::string& payload,
const securemessage::Header& header);
void OnInnerMessageUnwrappedForResponderAuth(
const ValidateResponderAuthMessageContext& context,
bool verified,
const std::string& payload,
const securemessage::Header& header);
// Begins the [Responder Auth] validation flow by validating the header.
void BeginResponderAuthValidation(ValidateResponderAuthMessageContext context) {
// Parse the encrypted SecureMessage so we can get plaintext data from the
// header. Note that the payload will be encrypted.
securemessage::SecureMessage encrypted_message;
securemessage::HeaderAndBody header_and_body;
if (!encrypted_message.ParseFromString(context.responder_auth_message) ||
!header_and_body.ParseFromString(encrypted_message.header_and_body())) {
PA_LOG(WARNING) << "Failed to parse [Responder Hello] message";
context.callback.Run(false, std::string());
return;
}
// Check that header public_metadata contains the correct metadata fields.
securemessage::Header header = header_and_body.header();
cryptauth::GcmMetadata gcm_metadata;
if (!gcm_metadata.ParseFromString(header.public_metadata()) ||
gcm_metadata.type() !=
cryptauth::DEVICE_TO_DEVICE_RESPONDER_HELLO_PAYLOAD ||
gcm_metadata.version() != kGcmMetadataVersion) {
PA_LOG(WARNING) << "Failed to validate GcmMetadata in "
<< "[Responder Auth] header.";
context.callback.Run(false, std::string());
return;
}
// Extract responder session public key from |decryption_key_id| field.
securemessage::ResponderHello responder_hello;
if (!responder_hello.ParseFromString(header.decryption_key_id()) ||
!responder_hello.public_dh_key().SerializeToString(
&context.responder_session_public_key)) {
PA_LOG(INFO) << "Failed to extract responder session public key in "
<< "[Responder Auth] header.";
context.callback.Run(false, std::string());
return;
}
// Perform a Diffie-Helmann key exchange to get the session symmetric key.
context.secure_message_delegate->DeriveKey(
context.session_private_key, context.responder_session_public_key,
base::Bind(&OnSessionSymmetricKeyDerived, context));
}
// Called after the session symmetric key is derived, so now we can unwrap the
// outer message of [Responder Auth].
void OnSessionSymmetricKeyDerived(ValidateResponderAuthMessageContext context,
const std::string& session_symmetric_key) {
context.session_symmetric_key = session_symmetric_key;
// Unwrap the outer message, using symmetric key encryption and signature.
cryptauth::SecureMessageDelegate::UnwrapOptions unwrap_options;
unwrap_options.encryption_scheme = securemessage::AES_256_CBC;
unwrap_options.signature_scheme = securemessage::HMAC_SHA256;
context.secure_message_delegate->UnwrapSecureMessage(
context.responder_auth_message, session_symmetric_key, unwrap_options,
base::Bind(&OnOuterMessageUnwrappedForResponderAuth, context));
}
// Called after the outer-most layer of [Responder Auth] is unwrapped.
void OnOuterMessageUnwrappedForResponderAuth(
const ValidateResponderAuthMessageContext& context,
bool verified,
const std::string& payload,
const securemessage::Header& header) {
if (!verified) {
PA_LOG(INFO) << "Failed to unwrap outer [Responder Auth] message.";
context.callback.Run(false, std::string());
return;
}
// Parse the decrypted payload.
securemessage::DeviceToDeviceMessage device_to_device_message;
if (!device_to_device_message.ParseFromString(payload) ||
device_to_device_message.sequence_number() != 1) {
PA_LOG(INFO) << "Failed to validate DeviceToDeviceMessage payload.";
context.callback.Run(false, std::string());
return;
}
// Unwrap the middle level SecureMessage, using symmetric key encryption and
// signature.
cryptauth::SecureMessageDelegate::UnwrapOptions unwrap_options;
unwrap_options.encryption_scheme = securemessage::AES_256_CBC;
unwrap_options.signature_scheme = securemessage::HMAC_SHA256;
unwrap_options.associated_data = context.hello_message;
context.secure_message_delegate->UnwrapSecureMessage(
device_to_device_message.message(), context.persistent_symmetric_key,
unwrap_options,
base::Bind(&OnMiddleMessageUnwrappedForResponderAuth, context));
}
// Called after the middle layer of [Responder Auth] is unwrapped.
void OnMiddleMessageUnwrappedForResponderAuth(
const ValidateResponderAuthMessageContext& context,
bool verified,
const std::string& payload,
const securemessage::Header& header) {
if (!verified) {
PA_LOG(INFO) << "Failed to unwrap middle [Responder Auth] message.";
context.callback.Run(false, std::string());
return;
}
// Unwrap the inner-most SecureMessage, using no encryption and an asymmetric
// key signature.
cryptauth::SecureMessageDelegate::UnwrapOptions unwrap_options;
unwrap_options.encryption_scheme = securemessage::NONE;
unwrap_options.signature_scheme = securemessage::ECDSA_P256_SHA256;
unwrap_options.associated_data = context.hello_message;
context.secure_message_delegate->UnwrapSecureMessage(
payload, context.persistent_responder_public_key, unwrap_options,
base::Bind(&OnInnerMessageUnwrappedForResponderAuth, context));
}
// Called after the inner-most layer of [Responder Auth] is unwrapped.
void OnInnerMessageUnwrappedForResponderAuth(
const ValidateResponderAuthMessageContext& context,
bool verified,
const std::string& payload,
const securemessage::Header& header) {
if (!verified)
PA_LOG(INFO) << "Failed to unwrap inner [Responder Auth] message.";
// Note: The GMS Core implementation does not properly set the metadata
// version, so we only check that the type is UNLOCK_KEY_SIGNED_CHALLENGE.
cryptauth::GcmMetadata gcm_metadata;
if (!gcm_metadata.ParseFromString(header.public_metadata()) ||
gcm_metadata.type() != cryptauth::UNLOCK_KEY_SIGNED_CHALLENGE) {
PA_LOG(WARNING) << "Failed to validate GcmMetadata in inner-most "
<< "[Responder Auth] message.";
context.callback.Run(false, std::string());
return;
}
context.callback.Run(verified, context.session_symmetric_key);
}
} // namespace
// static
void DeviceToDeviceInitiatorOperations::CreateHelloMessage(
const std::string& session_public_key,
const std::string& persistent_symmetric_key,
cryptauth::SecureMessageDelegate* secure_message_delegate,
const MessageCallback& callback) {
// Decode public key into the |initator_hello| proto.
securemessage::InitiatorHello initator_hello;
if (!initator_hello.mutable_public_dh_key()->ParseFromString(
session_public_key)) {
PA_LOG(ERROR) << "Unable to parse user's public key";
callback.Run(std::string());
return;
}
// The [Hello] message has the structure:
// {
// header: <session_public_key>,
// Sig(<session_public_key>, persistent_symmetric_key)
// payload: ""
// }
cryptauth::SecureMessageDelegate::CreateOptions create_options;
create_options.encryption_scheme = securemessage::NONE;
create_options.signature_scheme = securemessage::HMAC_SHA256;
initator_hello.SerializeToString(&create_options.public_metadata);
secure_message_delegate->CreateSecureMessage(
kPayloadFiller, persistent_symmetric_key, create_options, callback);
}
// static
void DeviceToDeviceInitiatorOperations::ValidateResponderAuthMessage(
const std::string& responder_auth_message,
const std::string& persistent_responder_public_key,
const std::string& persistent_symmetric_key,
const std::string& session_private_key,
const std::string& hello_message,
cryptauth::SecureMessageDelegate* secure_message_delegate,
const ValidateResponderAuthCallback& callback) {
// The [Responder Auth] message has the structure:
// {
// header: <responder_public_key>,
// Sig(<responder_public_key> + payload1,
// session_symmetric_key),
// payload1: Enc({
// header: Sig(payload2 + <hello_message>, persistent_symmetric_key),
// payload2: {
// sequence_number: 1,
// body: Enc({
// header: Sig(payload3 + <hello_message>,
// persistent_responder_public_key),
// payload3: ""
// }, persistent_symmetric_key)
// }
// }, session_symmetric_key),
// }
struct ValidateResponderAuthMessageContext context = {
responder_auth_message,
persistent_responder_public_key,
persistent_symmetric_key,
session_private_key,
hello_message,
secure_message_delegate,
callback};
BeginResponderAuthValidation(context);
}
// static
void DeviceToDeviceInitiatorOperations::CreateInitiatorAuthMessage(
const std::string& session_symmetric_key,
const std::string& persistent_symmetric_key,
const std::string& responder_auth_message,
cryptauth::SecureMessageDelegate* secure_message_delegate,
const MessageCallback& callback) {
// The [Initiator Auth] message has the structure:
// {
// header: Sig(payload1, session_symmetric_key)
// payload1: Enc({
// sequence_number: 2,
// body: {
// header: Sig(payload2 + responder_auth_message,
// persistent_symmetric_key)
// payload2: ""
// }
// }, session_symmetric_key)
// }
cryptauth::SecureMessageDelegate::CreateOptions create_options;
create_options.encryption_scheme = securemessage::AES_256_CBC;
create_options.signature_scheme = securemessage::HMAC_SHA256;
create_options.associated_data = responder_auth_message;
secure_message_delegate->CreateSecureMessage(
kPayloadFiller, persistent_symmetric_key, create_options,
base::Bind(&OnInnerMessageCreatedForInitiatorAuth, session_symmetric_key,
secure_message_delegate, callback));
}
} // proximity_auth
|