| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 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/payments/content/android/utility/payment_manifest_parser.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/json/json_reader.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "base/strings/string_util.h" | |
| 15 #include "base/values.h" | |
| 16 #include "components/payments/content/android/utility/fingerprint_parser.h" | |
| 17 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 18 | |
| 19 namespace payments { | |
| 20 | |
| 21 // static | |
| 22 void PaymentManifestParser::Create( | |
| 23 mojom::PaymentManifestParserRequest request) { | |
| 24 mojo::MakeStrongBinding(base::MakeUnique<PaymentManifestParser>(), | |
| 25 std::move(request)); | |
| 26 } | |
| 27 | |
| 28 // static | |
| 29 std::vector<mojom::PaymentManifestSectionPtr> | |
| 30 PaymentManifestParser::ParseIntoVector(const std::string& input) { | |
| 31 std::vector<mojom::PaymentManifestSectionPtr> output; | |
| 32 std::unique_ptr<base::Value> value(base::JSONReader::Read(input)); | |
| 33 if (!value) | |
| 34 return output; | |
| 35 | |
| 36 std::unique_ptr<base::DictionaryValue> dict = | |
| 37 base::DictionaryValue::From(std::move(value)); | |
| 38 if (!dict) | |
| 39 return output; | |
| 40 | |
| 41 base::ListValue* list = nullptr; | |
| 42 if (!dict->GetList("android", &list) || !list) | |
| 43 return output; | |
| 44 | |
| 45 size_t sections_size = list->GetSize(); | |
| 46 const size_t kMaximumNumberOfSections = 100U; | |
| 47 if (sections_size > kMaximumNumberOfSections) | |
| 48 return output; | |
| 49 | |
| 50 const char* const kVersion = "version"; | |
| 51 const char* const kFingerprints = "sha256_cert_fingerprints"; | |
| 52 for (size_t i = 0; i < sections_size; ++i) { | |
| 53 base::DictionaryValue* item = nullptr; | |
| 54 if (!list->GetDictionary(i, &item) || !item) { | |
| 55 output.clear(); | |
| 56 return output; | |
| 57 } | |
| 58 | |
| 59 mojom::PaymentManifestSectionPtr section = | |
| 60 mojom::PaymentManifestSection::New(); | |
| 61 section->version = 0; | |
| 62 | |
| 63 if (!item->GetString("package", §ion->package_name) || | |
| 64 section->package_name.empty() || | |
| 65 !base::IsStringASCII(section->package_name)) { | |
| 66 output.clear(); | |
| 67 return output; | |
| 68 } | |
| 69 | |
| 70 if (section->package_name == "*") { | |
| 71 output.clear(); | |
| 72 // If there's a section with "package": "*", then it must be the only | |
| 73 // section and it should not have "version" or "sha256_cert_fingerprints". | |
| 74 // (Any deviations from a correct format cause the full file to be | |
| 75 // rejected.) | |
| 76 if (!item->HasKey(kVersion) && !item->HasKey(kFingerprints) && | |
| 77 sections_size == 1U) { | |
| 78 output.push_back(std::move(section)); | |
| 79 } | |
| 80 return output; | |
| 81 } | |
| 82 | |
| 83 if (!item->HasKey(kVersion) || !item->HasKey(kFingerprints)) { | |
| 84 output.clear(); | |
| 85 return output; | |
| 86 } | |
| 87 | |
| 88 int version = 0; | |
| 89 if (!item->GetInteger(kVersion, &version)) { | |
| 90 output.clear(); | |
| 91 return output; | |
| 92 } | |
| 93 | |
| 94 section->version = static_cast<int64_t>(version); | |
| 95 | |
| 96 base::ListValue* fingerprints = nullptr; | |
| 97 if (!item->GetList(kFingerprints, &fingerprints) || !fingerprints || | |
| 98 fingerprints->empty()) { | |
| 99 output.clear(); | |
| 100 return output; | |
| 101 } | |
| 102 | |
| 103 size_t fingerprints_size = fingerprints->GetSize(); | |
| 104 const size_t kMaximumNumberOfFingerprints = 100U; | |
| 105 if (fingerprints_size > kMaximumNumberOfFingerprints) { | |
| 106 output.clear(); | |
| 107 return output; | |
| 108 } | |
| 109 | |
| 110 for (size_t j = 0; j < fingerprints_size; ++j) { | |
| 111 std::string fingerprint; | |
| 112 if (!fingerprints->GetString(j, &fingerprint) || fingerprint.empty()) { | |
| 113 output.clear(); | |
| 114 return output; | |
| 115 } | |
| 116 | |
| 117 std::vector<uint8_t> fingerprint_bytes = | |
| 118 FingerprintStringToByteArray(fingerprint); | |
| 119 if (32U != fingerprint_bytes.size()) { | |
| 120 output.clear(); | |
| 121 return output; | |
| 122 } | |
| 123 | |
| 124 section->sha256_cert_fingerprints.push_back(fingerprint_bytes); | |
| 125 } | |
| 126 | |
| 127 output.push_back(std::move(section)); | |
| 128 } | |
| 129 | |
| 130 return output; | |
| 131 } | |
| 132 | |
| 133 PaymentManifestParser::PaymentManifestParser() {} | |
| 134 | |
| 135 PaymentManifestParser::~PaymentManifestParser() {} | |
| 136 | |
| 137 void PaymentManifestParser::Parse(const std::string& content, | |
| 138 const ParseCallback& callback) { | |
| 139 callback.Run(ParseIntoVector(content)); | |
| 140 } | |
| 141 | |
| 142 } // namespace payments | |
| OLD | NEW |