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