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
|
// Copyright 2013 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/autofill/browser/wallet/encryption_escrow_client.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/stringprintf.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/utf_string_conversions.h"
#include "components/autofill/browser/wallet/encryption_escrow_client_observer.h"
#include "components/autofill/browser/wallet/instrument.h"
#include "components/autofill/browser/wallet/wallet_service_url.h"
#include "googleurl/src/gurl.h"
#include "net/base/escape.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_context_getter.h"
namespace {
const char kEncryptOtpBodyFormat[] = "cvv=%s:%s";
const char kEscrowInstrumentInformationFormat[] = "gid=%s&cardNumber=%s&cvv=%s";
const char kEscrowCardVerficationNumberFormat[] = "gid=%s&cvv=%s";
const char kApplicationMimeType[] = "application/x-www-form-urlencoded";
// The maximum number of bits in the one time pad that the server is willing to
// accept.
const size_t kMaxBits = 56;
// The minimum number of bits in the one time pad that the server is willing to
// accept.
const size_t kMinBits = 40;
} // anonymous namespace
namespace autofill {
namespace wallet {
EncryptionEscrowClient::EncryptionEscrowClient(
net::URLRequestContextGetter* context_getter,
EncryptionEscrowClientObserver* observer)
: context_getter_(context_getter),
observer_(observer),
request_type_(NO_PENDING_REQUEST) {
DCHECK(context_getter_);
DCHECK(observer_);
}
EncryptionEscrowClient::~EncryptionEscrowClient() {}
void EncryptionEscrowClient::EncryptOneTimePad(
const std::vector<uint8>& one_time_pad) {
DCHECK_EQ(NO_PENDING_REQUEST, request_type_);
size_t num_bits = one_time_pad.size() * 8;
DCHECK_LE(num_bits, kMaxBits);
DCHECK_GE(num_bits, kMinBits);
request_type_ = ENCRYPT_ONE_TIME_PAD;
std::string post_body = base::StringPrintf(
kEncryptOtpBodyFormat,
base::HexEncode(&num_bits, 1).c_str(),
base::HexEncode(&(one_time_pad[0]), one_time_pad.size()).c_str());
MakeRequest(GetEncryptionUrl(), post_body);
}
void EncryptionEscrowClient::EscrowInstrumentInformation(
const Instrument& new_instrument,
const std::string& obfuscated_gaia_id) {
DCHECK_EQ(NO_PENDING_REQUEST, request_type_);
request_type_ = ESCROW_INSTRUMENT_INFORMATION;
const std::string& primary_account_number =
net::EscapeUrlEncodedData(
UTF16ToUTF8(new_instrument.primary_account_number()), true);
const std::string& card_verification_number =
net::EscapeUrlEncodedData(
UTF16ToUTF8(new_instrument.card_verification_number()), true);
std::string post_body = base::StringPrintf(
kEscrowInstrumentInformationFormat,
obfuscated_gaia_id.c_str(),
primary_account_number.c_str(),
card_verification_number.c_str());
MakeRequest(GetEscrowUrl(), post_body);
}
void EncryptionEscrowClient::EscrowCardVerificationNumber(
const std::string& card_verification_number,
const std::string& obfuscated_gaia_id) {
DCHECK_EQ(NO_PENDING_REQUEST, request_type_);
request_type_ = ESCROW_CARD_VERIFICATION_NUMBER;
std::string post_body = base::StringPrintf(
kEscrowCardVerficationNumberFormat,
obfuscated_gaia_id.c_str(),
card_verification_number.c_str());
MakeRequest(GetEscrowUrl(), post_body);
}
void EncryptionEscrowClient::CancelRequest() {
request_.reset();
request_type_ = NO_PENDING_REQUEST;
}
void EncryptionEscrowClient::MakeRequest(const GURL& url,
const std::string& post_body) {
DCHECK(!request_.get());
request_.reset(net::URLFetcher::Create(
1, url, net::URLFetcher::POST, this));
request_->SetRequestContext(context_getter_);
DVLOG(1) << "url=" << url << ", post_body=" << post_body;
request_->SetUploadData(kApplicationMimeType, post_body);
request_->Start();
observer_->OnDidMakeRequest();
}
// TODO(ahutter): Add manual retry logic if it's necessary.
void EncryptionEscrowClient::OnURLFetchComplete(
const net::URLFetcher* source) {
DCHECK(observer_);
scoped_ptr<net::URLFetcher> old_request = request_.Pass();
DCHECK_EQ(source, old_request.get());
DVLOG(1) << "Got response from " << source->GetOriginalURL();
RequestType type = request_type_;
request_type_ = NO_PENDING_REQUEST;
std::string data;
source->GetResponseAsString(&data);
DVLOG(1) << "Response body: " << data;
if (source->GetResponseCode() != net::HTTP_OK) {
observer_->OnNetworkError(source->GetResponseCode());
return;
}
if (data.empty()) {
HandleMalformedResponse(old_request.get());
return;
}
switch (type) {
case ENCRYPT_ONE_TIME_PAD: {
std::vector<std::string> splits;
// The response from the server should be formatted as
// "<session material>|<encrypted one time pad>".
base::SplitString(data, '|', &splits);
if (splits.size() == 2)
observer_->OnDidEncryptOneTimePad(splits[1], splits[0]);
else
HandleMalformedResponse(old_request.get());
break;
}
case ESCROW_INSTRUMENT_INFORMATION:
observer_->OnDidEscrowInstrumentInformation(data);
break;
case ESCROW_CARD_VERIFICATION_NUMBER:
observer_->OnDidEscrowCardVerificationNumber(data);
break;
case NO_PENDING_REQUEST:
NOTREACHED();
}
}
void EncryptionEscrowClient::HandleMalformedResponse(net::URLFetcher* request) {
// Called to inform exponential backoff logic of the error.
request->ReceivedContentWasMalformed();
observer_->OnMalformedResponse();
}
} // namespace wallet
} // namespace autofill
|