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