OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/autofill/wallet/full_wallet.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/strings/string_number_conversions.h" | |
9 #include "base/values.h" | |
10 | |
11 namespace { | |
12 | |
13 const size_t kPanSize = 16; | |
14 const size_t kBinSize = 6; | |
15 const size_t kCvnSize = 3; | |
16 | |
17 } // anonymous namespace | |
18 | |
19 namespace autofill { | |
20 namespace wallet { | |
21 | |
22 FullWallet::FullWallet(int expiration_month, | |
23 int expiration_year, | |
24 const std::string& iin, | |
25 const std::string& encrypted_rest, | |
26 scoped_ptr<Address> billing_address, | |
27 scoped_ptr<Address> shipping_address, | |
28 const std::vector<RequiredAction>& required_actions) | |
29 : expiration_month_(expiration_month), | |
30 expiration_year_(expiration_year), | |
31 iin_(iin), | |
32 encrypted_rest_(encrypted_rest), | |
33 billing_address_(billing_address.Pass()), | |
34 shipping_address_(shipping_address.Pass()), | |
35 required_actions_(required_actions) { | |
36 DCHECK(required_actions_.size() > 0 || billing_address_.get()); | |
37 } | |
38 | |
39 FullWallet::~FullWallet() {} | |
40 | |
41 scoped_ptr<FullWallet> | |
42 FullWallet::CreateFullWallet(const DictionaryValue& dictionary) { | |
43 const ListValue* required_actions_list; | |
44 std::vector<RequiredAction> required_actions; | |
45 if (dictionary.GetList("required_action", &required_actions_list)) { | |
46 for (size_t i = 0; i < required_actions_list->GetSize(); ++i) { | |
47 std::string action_string; | |
48 if (required_actions_list->GetString(i, &action_string)) { | |
49 RequiredAction action = ParseRequiredActionFromString(action_string); | |
50 if (!ActionAppliesToFullWallet(action)) { | |
51 DLOG(ERROR) << "Response from Google wallet with bad required action:" | |
52 " \"" << action_string << "\""; | |
53 return scoped_ptr<FullWallet>(); | |
54 } | |
55 required_actions.push_back(action); | |
56 } | |
57 } | |
58 if (required_actions.size() > 0) { | |
59 return scoped_ptr<FullWallet>(new FullWallet(-1, | |
60 -1, | |
61 std::string(), | |
62 std::string(), | |
63 scoped_ptr<Address>(), | |
64 scoped_ptr<Address>(), | |
65 required_actions)); | |
66 } | |
67 } else { | |
68 DVLOG(1) << "Response from Google wallet missing required actions"; | |
69 } | |
70 | |
71 int expiration_month; | |
72 if (!dictionary.GetInteger("expiration_month", &expiration_month)) { | |
73 DLOG(ERROR) << "Response from Google wallet missing expiration month"; | |
74 return scoped_ptr<FullWallet>(); | |
75 } | |
76 | |
77 int expiration_year; | |
78 if (!dictionary.GetInteger("expiration_year", &expiration_year)) { | |
79 DLOG(ERROR) << "Response from Google wallet missing expiration year"; | |
80 return scoped_ptr<FullWallet>(); | |
81 } | |
82 | |
83 std::string iin; | |
84 if (!dictionary.GetString("iin", &iin)) { | |
85 DLOG(ERROR) << "Response from Google wallet missing iin"; | |
86 return scoped_ptr<FullWallet>(); | |
87 } | |
88 | |
89 std::string encrypted_rest; | |
90 if (!dictionary.GetString("rest", &encrypted_rest)) { | |
91 DLOG(ERROR) << "Response from Google wallet missing rest"; | |
92 return scoped_ptr<FullWallet>(); | |
93 } | |
94 | |
95 const DictionaryValue* billing_address_dict; | |
96 if (!dictionary.GetDictionary("billing_address", &billing_address_dict)) { | |
97 DLOG(ERROR) << "Response from Google wallet missing billing address"; | |
98 return scoped_ptr<FullWallet>(); | |
99 } | |
100 | |
101 scoped_ptr<Address> billing_address = | |
102 Address::CreateAddress(*billing_address_dict); | |
103 if (!billing_address.get()) { | |
104 DLOG(ERROR) << "Response from Google wallet has malformed billing address"; | |
105 return scoped_ptr<FullWallet>(); | |
106 } | |
107 | |
108 const DictionaryValue* shipping_address_dict; | |
109 scoped_ptr<Address> shipping_address; | |
110 if (dictionary.GetDictionary("shipping_address", &shipping_address_dict)) { | |
111 shipping_address = | |
112 Address::CreateAddressWithID(*shipping_address_dict); | |
113 } else { | |
114 DVLOG(1) << "Response from Google wallet missing shipping address"; | |
115 } | |
116 | |
117 return scoped_ptr<FullWallet>(new FullWallet(expiration_month, | |
118 expiration_year, | |
119 iin, | |
120 encrypted_rest, | |
121 billing_address.Pass(), | |
122 shipping_address.Pass(), | |
123 required_actions)); | |
124 } | |
125 | |
126 bool FullWallet::HasRequiredAction(RequiredAction action) const { | |
127 DCHECK(ActionAppliesToFullWallet(action)); | |
128 return std::find(required_actions_.begin(), | |
129 required_actions_.end(), | |
130 action) != required_actions_.end(); | |
131 } | |
132 | |
133 bool FullWallet::operator==(const FullWallet& other) const { | |
134 if (expiration_month_ != other.expiration_month_) | |
135 return false; | |
136 | |
137 if (expiration_year_ != other.expiration_year_) | |
138 return false; | |
139 | |
140 if (iin_ != other.iin_) | |
141 return false; | |
142 | |
143 if (encrypted_rest_ != other.encrypted_rest_) | |
144 return false; | |
145 | |
146 if (billing_address_.get() && other.billing_address_.get()) { | |
147 if (*billing_address_.get() != *other.billing_address_.get()) | |
148 return false; | |
149 } else if (billing_address_.get() || other.billing_address_.get()) { | |
150 return false; | |
151 } | |
152 | |
153 if (shipping_address_.get() && other.shipping_address_.get()) { | |
154 if (*shipping_address_.get() != *other.shipping_address_.get()) | |
155 return false; | |
156 } else if (shipping_address_.get() || other.shipping_address_.get()) { | |
157 return false; | |
158 } | |
159 | |
160 if (required_actions_ != other.required_actions_) | |
161 return false; | |
162 | |
163 return true; | |
164 } | |
165 | |
166 bool FullWallet::operator!=(const FullWallet& other) const { | |
167 return !(*this == other); | |
168 } | |
169 | |
170 const std::string& FullWallet::GetPan() { | |
171 if (pan_.empty()) | |
172 DecryptCardInfo(); | |
173 return pan_; | |
174 } | |
175 | |
176 const std::string& FullWallet::GetCvn() { | |
177 if (cvn_.empty()) | |
178 DecryptCardInfo(); | |
179 return cvn_; | |
180 } | |
181 | |
182 void FullWallet::DecryptCardInfo() { | |
183 std::vector<uint8> operating_data; | |
184 // Convert |encrypted_rest_| to bytes so we can decrypt it with |otp|. | |
185 if (!base::HexStringToBytes(encrypted_rest_, &operating_data)) { | |
186 DLOG(ERROR) << "Failed to parse encrypted rest"; | |
187 return; | |
188 } | |
189 | |
190 // Ensure |one_time_pad_| and |encrypted_rest_| are of the same length | |
191 // otherwise something has gone wrong and we can't decrypt the data. | |
192 DCHECK_EQ(one_time_pad_.size(), operating_data.size()); | |
193 | |
194 std::vector<uint8> results; | |
195 // XOR |otp| with the encrypted data to decrypt. | |
196 for (size_t i = 0; i < one_time_pad_.size(); ++i) | |
197 results.push_back(one_time_pad_[i] ^ operating_data[i]); | |
198 | |
199 // There is no uint8* to int64 so convert the decrypted data to hex and then | |
200 // parse the hex to an int64 before getting the int64 as a string. | |
201 std::string hex_decrypted = base::HexEncode(&(results[0]), results.size()); | |
202 | |
203 int64 decrypted; | |
204 if (!base::HexStringToInt64(hex_decrypted, &decrypted)) { | |
205 DLOG(ERROR) << "Failed to parse decrypted data in hex to int64"; | |
206 return; | |
207 } | |
208 std::string card_info = base::Int64ToString(decrypted); | |
209 | |
210 size_t padded_length = kPanSize - kBinSize + kCvnSize; | |
211 // |card_info| is PAN without the IIN concatenated with the CVN, i.e. | |
212 // PANPANPANPCVN. If what was decrypted is not of that size the front needs | |
213 // to be padded with 0's until it is. | |
214 if (card_info.size() != padded_length) | |
215 card_info.insert(card_info.begin(), padded_length - card_info.size(), '0'); | |
216 | |
217 // Separate out the PAN from the CVN. | |
218 size_t split = kPanSize - kBinSize; | |
219 cvn_ = card_info.substr(split); | |
220 pan_ = iin_ + card_info.substr(0, split); | |
221 } | |
222 | |
223 } // namespace wallet | |
224 } // namespace autofill | |
OLD | NEW |