forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_to_device_secure_context.cc
129 lines (105 loc) · 4.92 KB
/
device_to_device_secure_context.cc
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
// 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/cryptauth/device_to_device_secure_context.h"
#include <utility>
#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 cryptauth {
namespace {
// The version to put in the GcmMetadata field.
const int kGcmMetadataVersion = 1;
// The sequence number of the last message sent during authentication. These
// messages are sent and received before the SecureContext is created.
const int kAuthenticationEncodeSequenceNumber = 1;
// The sequence number of the last message received during authentication. These
// messages are sent and received before the SecureContext is created.
const int kAuthenticationDecodeSequenceNumber = 1;
} // namespace
DeviceToDeviceSecureContext::DeviceToDeviceSecureContext(
std::unique_ptr<SecureMessageDelegate> secure_message_delegate,
const SessionKeys& session_keys,
const std::string& responder_auth_message,
ProtocolVersion protocol_version)
: secure_message_delegate_(std::move(secure_message_delegate)),
encryption_key_(session_keys.initiator_encode_key()),
decryption_key_(session_keys.responder_encode_key()),
responder_auth_message_(responder_auth_message),
protocol_version_(protocol_version),
last_encode_sequence_number_(kAuthenticationEncodeSequenceNumber),
last_decode_sequence_number_(kAuthenticationDecodeSequenceNumber),
weak_ptr_factory_(this) {}
DeviceToDeviceSecureContext::~DeviceToDeviceSecureContext() {}
void DeviceToDeviceSecureContext::Decode(const std::string& encoded_message,
const MessageCallback& callback) {
SecureMessageDelegate::UnwrapOptions unwrap_options;
unwrap_options.encryption_scheme = securemessage::AES_256_CBC;
unwrap_options.signature_scheme = securemessage::HMAC_SHA256;
secure_message_delegate_->UnwrapSecureMessage(
encoded_message, decryption_key_, unwrap_options,
base::Bind(&DeviceToDeviceSecureContext::HandleUnwrapResult,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void DeviceToDeviceSecureContext::Encode(const std::string& message,
const MessageCallback& callback) {
// Create a GcmMetadata field to put in the header.
GcmMetadata gcm_metadata;
gcm_metadata.set_type(DEVICE_TO_DEVICE_MESSAGE);
gcm_metadata.set_version(kGcmMetadataVersion);
// Wrap |message| inside a DeviceToDeviceMessage proto.
securemessage::DeviceToDeviceMessage device_to_device_message;
device_to_device_message.set_sequence_number(++last_encode_sequence_number_);
device_to_device_message.set_message(message);
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(), encryption_key_,
create_options, callback);
}
std::string DeviceToDeviceSecureContext::GetChannelBindingData() const {
return responder_auth_message_;
}
SecureContext::ProtocolVersion DeviceToDeviceSecureContext::GetProtocolVersion()
const {
return protocol_version_;
}
void DeviceToDeviceSecureContext::HandleUnwrapResult(
const DeviceToDeviceSecureContext::MessageCallback& callback,
bool verified,
const std::string& payload,
const securemessage::Header& header) {
// The payload should contain a DeviceToDeviceMessage proto.
securemessage::DeviceToDeviceMessage device_to_device_message;
if (!verified || !device_to_device_message.ParseFromString(payload)) {
PA_LOG(ERROR) << "Failed to unwrap secure message.";
callback.Run(std::string());
return;
}
// Check that the sequence number matches the expected sequence number.
if (device_to_device_message.sequence_number() !=
last_decode_sequence_number_ + 1) {
PA_LOG(ERROR) << "Expected sequence_number="
<< last_decode_sequence_number_ + 1 << ", but got "
<< device_to_device_message.sequence_number();
callback.Run(std::string());
return;
}
// Validate the GcmMetadata proto in the header.
GcmMetadata gcm_metadata;
if (!gcm_metadata.ParseFromString(header.public_metadata()) ||
gcm_metadata.type() != DEVICE_TO_DEVICE_MESSAGE ||
gcm_metadata.version() != kGcmMetadataVersion) {
PA_LOG(ERROR) << "Failed to validate GcmMetadata.";
callback.Run(std::string());
return;
}
last_decode_sequence_number_++;
callback.Run(device_to_device_message.message());
}
} // cryptauth