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