OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 "components/autofill/content/browser/wallet/full_wallet.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <utility> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "base/values.h" | |
14 #include "components/autofill/core/browser/autofill_type.h" | |
15 #include "components/autofill/core/browser/credit_card.h" | |
16 | |
17 namespace { | |
18 | |
19 const size_t kPanSize = 16; | |
20 const size_t kBinSize = 6; | |
21 const size_t kCvnSize = 3; | |
22 const size_t kEncryptedRestSize = 12; | |
23 | |
24 } // anonymous namespace | |
25 | |
26 namespace autofill { | |
27 namespace wallet { | |
28 | |
29 FullWallet::FullWallet(int expiration_month, | |
30 int expiration_year, | |
31 const std::string& iin, | |
32 const std::string& encrypted_rest, | |
33 std::unique_ptr<Address> billing_address, | |
34 std::unique_ptr<Address> shipping_address) | |
35 : expiration_month_(expiration_month), | |
36 expiration_year_(expiration_year), | |
37 iin_(iin), | |
38 encrypted_rest_(encrypted_rest), | |
39 billing_address_(std::move(billing_address)), | |
40 shipping_address_(std::move(shipping_address)) { | |
41 DCHECK(billing_address_.get()); | |
42 } | |
43 | |
44 FullWallet::~FullWallet() {} | |
45 | |
46 // static | |
47 std::unique_ptr<FullWallet> FullWallet::CreateFullWalletFromClearText( | |
48 int expiration_month, | |
49 int expiration_year, | |
50 const std::string& pan, | |
51 const std::string& cvn, | |
52 std::unique_ptr<Address> billing_address, | |
53 std::unique_ptr<Address> shipping_address) { | |
54 DCHECK(billing_address); | |
55 DCHECK(!pan.empty()); | |
56 DCHECK(!cvn.empty()); | |
57 | |
58 std::unique_ptr<FullWallet> wallet(new FullWallet( | |
59 expiration_month, expiration_year, | |
60 std::string(), // no iin -- clear text pan/cvn are set below. | |
61 std::string(), // no encrypted_rest -- clear text pan/cvn are set below. | |
62 std::move(billing_address), std::move(shipping_address))); | |
63 wallet->pan_ = pan; | |
64 wallet->cvn_ = cvn; | |
65 return wallet; | |
66 } | |
67 | |
68 base::string16 FullWallet::GetInfo(const std::string& app_locale, | |
69 const AutofillType& type) { | |
70 switch (type.GetStorableType()) { | |
71 case CREDIT_CARD_NUMBER: | |
72 return base::ASCIIToUTF16(GetPan()); | |
73 | |
74 case CREDIT_CARD_NAME_FULL: | |
75 return billing_address()->recipient_name(); | |
76 | |
77 case CREDIT_CARD_VERIFICATION_CODE: | |
78 return base::ASCIIToUTF16(GetCvn()); | |
79 | |
80 case CREDIT_CARD_EXP_MONTH: | |
81 if (expiration_month() == 0) | |
82 return base::string16(); | |
83 return base::IntToString16(expiration_month()); | |
84 | |
85 case CREDIT_CARD_EXP_4_DIGIT_YEAR: | |
86 if (expiration_year() == 0) | |
87 return base::string16(); | |
88 return base::IntToString16(expiration_year()); | |
89 | |
90 case CREDIT_CARD_EXP_2_DIGIT_YEAR: | |
91 if (expiration_year() == 0) | |
92 return base::string16(); | |
93 return base::IntToString16(expiration_year() % 100); | |
94 | |
95 case CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR: | |
96 if (expiration_month() == 0 || expiration_year() == 0) | |
97 return base::string16(); | |
98 return base::IntToString16(expiration_month()) + base::ASCIIToUTF16("/") + | |
99 base::IntToString16(expiration_year() % 100); | |
100 | |
101 case CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR: | |
102 if (expiration_month() == 0 || expiration_year() == 0) | |
103 return base::string16(); | |
104 return base::IntToString16(expiration_month()) + base::ASCIIToUTF16("/") + | |
105 base::IntToString16(expiration_year()); | |
106 | |
107 case CREDIT_CARD_TYPE: { | |
108 const char* const internal_type = | |
109 CreditCard::GetCreditCardType(base::ASCIIToUTF16(GetPan())); | |
110 if (internal_type == kGenericCard) | |
111 return base::string16(); | |
112 return CreditCard::TypeForDisplay(internal_type); | |
113 } | |
114 | |
115 default: { | |
116 switch (type.group()) { | |
117 case NAME_BILLING: | |
118 case PHONE_BILLING: | |
119 case ADDRESS_BILLING: | |
120 return billing_address_->GetInfo(type, app_locale); | |
121 | |
122 case CREDIT_CARD: | |
123 NOTREACHED(); | |
124 | |
125 default: | |
126 return shipping_address_->GetInfo(type, app_locale); | |
127 } | |
128 } | |
129 } | |
130 } | |
131 | |
132 base::string16 FullWallet::TypeAndLastFourDigits() { | |
133 CreditCard card; | |
134 card.SetRawInfo(CREDIT_CARD_NUMBER, base::ASCIIToUTF16(GetPan())); | |
135 return card.TypeAndLastFourDigits(); | |
136 } | |
137 | |
138 const std::string& FullWallet::GetPan() { | |
139 if (pan_.empty()) | |
140 DecryptCardInfo(); | |
141 return pan_; | |
142 } | |
143 | |
144 bool FullWallet::operator==(const FullWallet& other) const { | |
145 if (expiration_month_ != other.expiration_month_) | |
146 return false; | |
147 | |
148 if (expiration_year_ != other.expiration_year_) | |
149 return false; | |
150 | |
151 if (iin_ != other.iin_) | |
152 return false; | |
153 | |
154 if (encrypted_rest_ != other.encrypted_rest_) | |
155 return false; | |
156 | |
157 if (billing_address_.get() && other.billing_address_.get()) { | |
158 if (*billing_address_.get() != *other.billing_address_.get()) | |
159 return false; | |
160 } else if (billing_address_.get() || other.billing_address_.get()) { | |
161 return false; | |
162 } | |
163 | |
164 if (shipping_address_.get() && other.shipping_address_.get()) { | |
165 if (*shipping_address_.get() != *other.shipping_address_.get()) | |
166 return false; | |
167 } else if (shipping_address_.get() || other.shipping_address_.get()) { | |
168 return false; | |
169 } | |
170 | |
171 return true; | |
172 } | |
173 | |
174 bool FullWallet::operator!=(const FullWallet& other) const { | |
175 return !(*this == other); | |
176 } | |
177 | |
178 void FullWallet::DecryptCardInfo() { | |
179 // |encrypted_rest_| must be of length |kEncryptedRestSize| in order for | |
180 // decryption to succeed and the server will not pad it with zeros. | |
181 while (encrypted_rest_.size() < kEncryptedRestSize) { | |
182 encrypted_rest_ = '0' + encrypted_rest_; | |
183 } | |
184 | |
185 DCHECK_EQ(kEncryptedRestSize, encrypted_rest_.size()); | |
186 | |
187 std::vector<uint8_t> operating_data; | |
188 // Convert |encrypted_rest_| to bytes so we can decrypt it with |otp|. | |
189 if (!base::HexStringToBytes(encrypted_rest_, &operating_data)) { | |
190 DLOG(ERROR) << "Failed to parse encrypted rest"; | |
191 return; | |
192 } | |
193 | |
194 // Ensure |one_time_pad_| and |encrypted_rest_| are of the same length | |
195 // otherwise something has gone wrong and we can't decrypt the data. | |
196 DCHECK_EQ(one_time_pad_.size(), operating_data.size()); | |
197 | |
198 std::vector<uint8_t> results; | |
199 // XOR |otp| with the encrypted data to decrypt. | |
200 for (size_t i = 0; i < one_time_pad_.size(); ++i) | |
201 results.push_back(one_time_pad_[i] ^ operating_data[i]); | |
202 | |
203 // There is no uint8_t* to int64_t so convert the decrypted data to hex and | |
204 // then parse the hex to an int64_t before getting the int64_t as a string. | |
205 std::string hex_decrypted = base::HexEncode(&(results[0]), results.size()); | |
206 | |
207 int64_t decrypted; | |
208 if (!base::HexStringToInt64(hex_decrypted, &decrypted)) { | |
209 DLOG(ERROR) << "Failed to parse decrypted data in hex to int64_t"; | |
210 return; | |
211 } | |
212 std::string card_info = base::Int64ToString(decrypted); | |
213 | |
214 size_t padded_length = kPanSize - kBinSize + kCvnSize; | |
215 // |card_info| is PAN without the IIN concatenated with the CVN, i.e. | |
216 // PANPANPANPCVN. If what was decrypted is not of that size the front needs | |
217 // to be padded with 0's until it is. | |
218 if (card_info.size() != padded_length) | |
219 card_info.insert(card_info.begin(), padded_length - card_info.size(), '0'); | |
220 | |
221 // Separate out the PAN from the CVN. | |
222 size_t split = kPanSize - kBinSize; | |
223 cvn_ = card_info.substr(split); | |
224 pan_ = iin_ + card_info.substr(0, split); | |
225 } | |
226 | |
227 const std::string& FullWallet::GetCvn() { | |
228 if (cvn_.empty()) | |
229 DecryptCardInfo(); | |
230 return cvn_; | |
231 } | |
232 | |
233 } // namespace wallet | |
234 } // namespace autofill | |
OLD | NEW |