ash/printing: base/hash/md5 -> crypto/obsolete/md5
Bug: 406729261 Change-Id: Iea6444f90de9524da1ac3f7b6f475316659e4353 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6553089 Reviewed-by: Benjamin Gordon <bmgordon@chromium.org> Commit-Queue: Elly FJ <ellyjones@chromium.org> Cr-Commit-Position: refs/heads/main@{#1462986}
This commit is contained in:
chrome/browser/ash/printing
crypto/obsolete
@ -16,7 +16,6 @@
|
||||
#include "base/feature_list.h"
|
||||
#include "base/functional/bind.h"
|
||||
#include "base/functional/callback.h"
|
||||
#include "base/hash/md5.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/memory/ptr_util.h"
|
||||
#include "base/memory/raw_ptr.h"
|
||||
@ -36,11 +35,19 @@
|
||||
#include "chromeos/printing/printer_configuration.h"
|
||||
#include "components/device_event_log/device_event_log.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "crypto/obsolete/md5.h"
|
||||
#include "printing/printing_features.h"
|
||||
#include "third_party/cros_system_api/dbus/debugd/dbus-constants.h"
|
||||
|
||||
namespace ash {
|
||||
|
||||
namespace printing {
|
||||
// Not placed in namespace {} so it can be friended from //crypto.
|
||||
crypto::obsolete::Md5 MakeMd5HasherForPrinterConfigurer() {
|
||||
return {};
|
||||
}
|
||||
} // namespace printing
|
||||
|
||||
namespace {
|
||||
|
||||
using ::chromeos::PpdProvider;
|
||||
@ -239,7 +246,7 @@ class PrinterConfigurerImpl : public PrinterConfigurer {
|
||||
<< printer.uri().GetNormalized(
|
||||
/*always_print_port=*/true);
|
||||
if (base::FeatureList::IsEnabled(
|
||||
printing::features::kAddPrinterViaPrintscanmgr)) {
|
||||
::printing::features::kAddPrinterViaPrintscanmgr)) {
|
||||
printscanmgr::CupsAddAutoConfiguredPrinterRequest request;
|
||||
request.set_name(printer.id());
|
||||
request.set_uri(printer.uri().GetNormalized(/*always_print_port=*/true));
|
||||
@ -305,7 +312,7 @@ class PrinterConfigurerImpl : public PrinterConfigurer {
|
||||
<< printer.uri().GetNormalized(
|
||||
/*always_print_port=*/true);
|
||||
if (base::FeatureList::IsEnabled(
|
||||
printing::features::kAddPrinterViaPrintscanmgr)) {
|
||||
::printing::features::kAddPrinterViaPrintscanmgr)) {
|
||||
printscanmgr::CupsAddManuallyConfiguredPrinterRequest request;
|
||||
request.set_name(printer.id());
|
||||
request.set_uri(printer.uri().GetNormalized(/*always_print_port=*/true));
|
||||
@ -447,17 +454,14 @@ class PrinterConfigurerImpl : public PrinterConfigurer {
|
||||
|
||||
// static
|
||||
std::string PrinterConfigurer::SetupFingerprint(const Printer& printer) {
|
||||
base::MD5Context ctx;
|
||||
base::MD5Init(&ctx);
|
||||
base::MD5Update(&ctx, printer.id());
|
||||
base::MD5Update(&ctx, printer.uri().GetNormalized(false));
|
||||
base::MD5Update(&ctx, printer.ppd_reference().user_supplied_ppd_url);
|
||||
base::MD5Update(&ctx, printer.ppd_reference().effective_make_and_model);
|
||||
auto md5 = ash::printing::MakeMd5HasherForPrinterConfigurer();
|
||||
md5.Update(printer.id());
|
||||
md5.Update(printer.uri().GetNormalized(false));
|
||||
md5.Update(printer.ppd_reference().user_supplied_ppd_url);
|
||||
md5.Update(printer.ppd_reference().effective_make_and_model);
|
||||
char autoconf = printer.ppd_reference().autoconf ? 1 : 0;
|
||||
base::MD5Update(&ctx, std::string(&autoconf, sizeof(autoconf)));
|
||||
base::MD5Digest digest;
|
||||
base::MD5Final(&digest, &ctx);
|
||||
return std::string(reinterpret_cast<char*>(&digest.a[0]), sizeof(digest.a));
|
||||
md5.Update(base::byte_span_from_ref(autoconf));
|
||||
return std::string(base::as_string_view(md5.Finish()));
|
||||
}
|
||||
|
||||
// static
|
||||
|
@ -8,7 +8,6 @@
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
#include "base/hash/md5.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/memory/raw_ptr.h"
|
||||
#include "base/memory/scoped_refptr.h"
|
||||
@ -20,6 +19,7 @@
|
||||
#include "chrome/browser/net/system_network_context_manager.h"
|
||||
#include "chrome/browser/profiles/profile.h"
|
||||
#include "components/device_event_log/device_event_log.h"
|
||||
#include "crypto/obsolete/md5.h"
|
||||
#include "net/base/load_flags.h"
|
||||
#include "net/traffic_annotation/network_traffic_annotation.h"
|
||||
#include "services/network/public/cpp/resource_request.h"
|
||||
@ -33,6 +33,16 @@
|
||||
|
||||
namespace ash {
|
||||
|
||||
namespace printing {
|
||||
|
||||
// Not in namespace {} so it can be friended by crypto/obsolete/md5.
|
||||
std::string ServerPrinterId(const std::string& url) {
|
||||
return "server-" +
|
||||
base::ToLowerASCII(base::HexEncode(crypto::obsolete::Md5::Hash(url)));
|
||||
}
|
||||
|
||||
} // namespace printing
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr net::NetworkTrafficAnnotationTag kServerPrintersFetcherNetworkTag =
|
||||
@ -61,15 +71,6 @@ constexpr net::NetworkTrafficAnnotationTag kServerPrintersFetcherNetworkTag =
|
||||
}
|
||||
})");
|
||||
|
||||
std::string ServerPrinterId(const std::string& url) {
|
||||
base::MD5Context ctx;
|
||||
base::MD5Init(&ctx);
|
||||
base::MD5Update(&ctx, url);
|
||||
base::MD5Digest digest;
|
||||
base::MD5Final(&digest, &ctx);
|
||||
return "server-" + base::MD5DigestToBase16(digest);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
class ServerPrintersFetcher::PrivateImplementation
|
||||
@ -256,7 +257,7 @@ class ServerPrintersFetcher::PrivateImplementation
|
||||
// Complete building the printer's URI.
|
||||
url.SetPath({"printers", name});
|
||||
printer->SetUri(url);
|
||||
printer->set_id(ServerPrinterId(url.GetNormalized()));
|
||||
printer->set_id(printing::ServerPrinterId(url.GetNormalized()));
|
||||
}
|
||||
|
||||
raw_ptr<const ServerPrintersFetcher, DanglingUntriaged> owner_;
|
||||
|
@ -17,7 +17,6 @@
|
||||
#include "base/containers/fixed_flat_set.h"
|
||||
#include "base/containers/span.h"
|
||||
#include "base/functional/callback_helpers.h"
|
||||
#include "base/hash/md5.h"
|
||||
#include "base/numerics/byte_conversions.h"
|
||||
#include "base/numerics/safe_conversions.h"
|
||||
#include "base/strings/strcat.h"
|
||||
@ -28,6 +27,7 @@
|
||||
#include "chromeos/printing/printer_configuration.h"
|
||||
#include "chromeos/printing/usb_printer_id.h"
|
||||
#include "components/device_event_log/device_event_log.h"
|
||||
#include "crypto/obsolete/md5.h"
|
||||
#include "services/device/public/cpp/usb/usb_utils.h"
|
||||
#include "services/device/public/mojom/usb_device.mojom.h"
|
||||
#include "services/device/public/mojom/usb_enumeration_options.mojom.h"
|
||||
@ -35,6 +35,13 @@
|
||||
#include "ui/base/l10n/l10n_util.h"
|
||||
|
||||
namespace ash {
|
||||
|
||||
namespace printing {
|
||||
crypto::obsolete::Md5 MakeMd5HasherForUsbPrinterUtil() {
|
||||
return {};
|
||||
}
|
||||
} // namespace printing
|
||||
|
||||
namespace {
|
||||
|
||||
using device::mojom::UsbDeviceInfo;
|
||||
@ -164,16 +171,16 @@ void OnDeviceOpen(mojo::Remote<device::mojom::UsbDevice> device,
|
||||
target, std::move(cb)));
|
||||
}
|
||||
|
||||
// Incorporate the bytes of |val| into the incremental hash carried in |ctx| in
|
||||
// big-endian order. |val| must be a simple integer type
|
||||
void MD5UpdateU8BigEndian(base::MD5Context* ctx,
|
||||
base::StrictNumeric<uint8_t> val) {
|
||||
// Incorporate the byte |val| into the incremental hash carried in |md5|.
|
||||
void Md5UpdateU8(crypto::obsolete::Md5& md5, base::StrictNumeric<uint8_t> val) {
|
||||
uint8_t tmp = val;
|
||||
base::MD5Update(ctx, base::span_from_ref(tmp));
|
||||
md5.Update(base::span_from_ref(tmp));
|
||||
}
|
||||
void MD5UpdateU16BigEndian(base::MD5Context* ctx,
|
||||
|
||||
// Incorporate |val| into |md5| as a big-endian value.
|
||||
void Md5UpdateU16BigEndian(crypto::obsolete::Md5& md5,
|
||||
base::StrictNumeric<uint16_t> val) {
|
||||
base::MD5Update(ctx, base::U16ToBigEndian(val));
|
||||
md5.Update(base::U16ToBigEndian(val));
|
||||
}
|
||||
|
||||
// Update the hash with the contents of |str|.
|
||||
@ -184,8 +191,8 @@ void MD5UpdateU16BigEndian(base::MD5Context* ctx,
|
||||
//
|
||||
// This is a long way to say "UTF-16 is hard to hash, let's just convert
|
||||
// to UTF-8 and hash that", which avoids all of these issues.
|
||||
void MD5UpdateString16(base::MD5Context* ctx, const std::u16string& str) {
|
||||
base::MD5Update(ctx, base::UTF16ToUTF8(str));
|
||||
void Md5UpdateString16(crypto::obsolete::Md5& md5, const std::u16string& str) {
|
||||
md5.Update(base::UTF16ToUTF8(str));
|
||||
}
|
||||
|
||||
// Get the usb printer id for |device|. This is used both as the identifier for
|
||||
@ -209,20 +216,18 @@ std::string CreateUsbPrinterId(const UsbDeviceInfo& device_info) {
|
||||
static_assert(sizeof(device::GetDeviceVersion(device_info)) == 2,
|
||||
"Version size changed");
|
||||
|
||||
base::MD5Context ctx;
|
||||
base::MD5Init(&ctx);
|
||||
MD5UpdateU8BigEndian(&ctx, device_info.class_code);
|
||||
MD5UpdateU8BigEndian(&ctx, device_info.subclass_code);
|
||||
MD5UpdateU8BigEndian(&ctx, device_info.protocol_code);
|
||||
MD5UpdateU16BigEndian(&ctx, device_info.vendor_id);
|
||||
MD5UpdateU16BigEndian(&ctx, device_info.product_id);
|
||||
MD5UpdateU16BigEndian(&ctx, device::GetDeviceVersion(device_info));
|
||||
base::MD5Update(&ctx, GetManufacturerName(device_info));
|
||||
base::MD5Update(&ctx, GetProductName(device_info));
|
||||
MD5UpdateString16(&ctx, GetSerialNumber(device_info));
|
||||
base::MD5Digest digest;
|
||||
base::MD5Final(&digest, &ctx);
|
||||
return base::StringPrintf("usb-%s", base::MD5DigestToBase16(digest).c_str());
|
||||
auto md5 = ash::printing::MakeMd5HasherForUsbPrinterUtil();
|
||||
Md5UpdateU8(md5, device_info.class_code);
|
||||
Md5UpdateU8(md5, device_info.subclass_code);
|
||||
Md5UpdateU8(md5, device_info.protocol_code);
|
||||
Md5UpdateU16BigEndian(md5, device_info.vendor_id);
|
||||
Md5UpdateU16BigEndian(md5, device_info.product_id);
|
||||
Md5UpdateU16BigEndian(md5, device::GetDeviceVersion(device_info));
|
||||
md5.Update(GetManufacturerName(device_info));
|
||||
md5.Update(GetProductName(device_info));
|
||||
Md5UpdateString16(md5, GetSerialNumber(device_info));
|
||||
return base::StringPrintf("usb-%s",
|
||||
base::ToLowerASCII(base::HexEncode(md5.Finish())));
|
||||
}
|
||||
|
||||
// Creates a mojom filter which can be used to identify a basic USB printer.
|
||||
|
@ -17,7 +17,6 @@
|
||||
|
||||
#include "base/containers/contains.h"
|
||||
#include "base/containers/fixed_flat_set.h"
|
||||
#include "base/hash/md5.h"
|
||||
#include "base/strings/string_split.h"
|
||||
#include "base/strings/string_util.h"
|
||||
#include "base/strings/stringprintf.h"
|
||||
@ -26,9 +25,16 @@
|
||||
#include "chrome/browser/local_discovery/service_discovery_shared_client.h"
|
||||
#include "chrome/browser/profiles/profile.h"
|
||||
#include "components/device_event_log/device_event_log.h"
|
||||
#include "crypto/obsolete/md5.h"
|
||||
|
||||
namespace ash {
|
||||
|
||||
namespace printing {
|
||||
crypto::obsolete::Md5 MakeMd5HasherForZeroconf() {
|
||||
return {};
|
||||
}
|
||||
} // namespace printing
|
||||
|
||||
// Supported service names for printers.
|
||||
const char ZeroconfPrinterDetector::kIppServiceName[] = "_ipp._tcp.local";
|
||||
const char ZeroconfPrinterDetector::kIppsServiceName[] = "_ipps._tcp.local";
|
||||
@ -144,19 +150,16 @@ class ParsedMetadata {
|
||||
// all to be considered the same printer.
|
||||
std::string ZeroconfPrinterId(const ServiceDescription& service,
|
||||
const ParsedMetadata& metadata) {
|
||||
base::MD5Context ctx;
|
||||
base::MD5Init(&ctx);
|
||||
base::MD5Update(&ctx, service.instance_name());
|
||||
base::MD5Update(&ctx, metadata.product);
|
||||
base::MD5Update(&ctx, metadata.UUID);
|
||||
base::MD5Update(&ctx, metadata.usb_MFG);
|
||||
base::MD5Update(&ctx, metadata.usb_MDL);
|
||||
base::MD5Update(&ctx, metadata.ty);
|
||||
base::MD5Update(&ctx, metadata.rp);
|
||||
base::MD5Digest digest;
|
||||
base::MD5Final(&digest, &ctx);
|
||||
auto md5 = ash::printing::MakeMd5HasherForZeroconf();
|
||||
md5.Update(service.instance_name());
|
||||
md5.Update(metadata.product);
|
||||
md5.Update(metadata.UUID);
|
||||
md5.Update(metadata.usb_MFG);
|
||||
md5.Update(metadata.usb_MDL);
|
||||
md5.Update(metadata.ty);
|
||||
md5.Update(metadata.rp);
|
||||
return base::StringPrintf("zeroconf-%s",
|
||||
base::MD5DigestToBase16(digest).c_str());
|
||||
base::ToLowerASCII(base::HexEncode(md5.Finish())));
|
||||
}
|
||||
|
||||
// Attempt to fill |detected_printer| using the information in
|
||||
|
@ -9,6 +9,11 @@
|
||||
|
||||
namespace crypto::obsolete {
|
||||
|
||||
// static
|
||||
std::array<uint8_t, Md5::kSize> Md5::Hash(std::string_view data) {
|
||||
return Hash(base::as_byte_span(data));
|
||||
}
|
||||
|
||||
// static
|
||||
std::array<uint8_t, Md5::kSize> Md5::Hash(base::span<const uint8_t> data) {
|
||||
std::array<uint8_t, Md5::kSize> result;
|
||||
@ -51,6 +56,10 @@ std::array<uint8_t, Md5::kSize> Md5::HashForTesting(
|
||||
return Hash(data);
|
||||
}
|
||||
|
||||
void Md5::Update(std::string_view data) {
|
||||
Update(base::as_byte_span(data));
|
||||
}
|
||||
|
||||
void Md5::Update(base::span<const uint8_t> data) {
|
||||
CHECK(EVP_DigestUpdate(ctx_.get(), data.data(), data.size()));
|
||||
}
|
||||
|
@ -16,6 +16,13 @@ namespace crypto::obsolete {
|
||||
class Md5;
|
||||
}
|
||||
|
||||
namespace ash::printing {
|
||||
crypto::obsolete::Md5 MakeMd5HasherForPrinterConfigurer();
|
||||
crypto::obsolete::Md5 MakeMd5HasherForUsbPrinterUtil();
|
||||
crypto::obsolete::Md5 MakeMd5HasherForZeroconf();
|
||||
std::string ServerPrinterId(const std::string& url);
|
||||
} // namespace ash::printing
|
||||
|
||||
namespace extensions::image_writer {
|
||||
crypto::obsolete::Md5 MakeMd5HasherForImageWriter();
|
||||
}
|
||||
@ -45,6 +52,7 @@ class CRYPTO_EXPORT Md5 {
|
||||
Md5& operator=(Md5&& other);
|
||||
~Md5();
|
||||
|
||||
void Update(std::string_view data);
|
||||
void Update(base::span<const uint8_t> data);
|
||||
|
||||
void Finish(base::span<uint8_t, kSize> result);
|
||||
@ -62,10 +70,17 @@ class CRYPTO_EXPORT Md5 {
|
||||
friend Md5 policy::MakeMd5HasherForPolicyEventId();
|
||||
friend Md5 extensions::image_writer::MakeMd5HasherForImageWriter();
|
||||
|
||||
// TODO(b/298652869): get rid of these.
|
||||
friend Md5 ash::printing::MakeMd5HasherForPrinterConfigurer();
|
||||
friend Md5 ash::printing::MakeMd5HasherForUsbPrinterUtil();
|
||||
friend Md5 ash::printing::MakeMd5HasherForZeroconf();
|
||||
friend std::string ash::printing::ServerPrinterId(const std::string& url);
|
||||
|
||||
// TODO(https://crbug.com/416304903): get rid of this.
|
||||
friend Md5 web_app::internals::MakeMd5HasherForWebAppShortcutIcon();
|
||||
|
||||
Md5();
|
||||
static std::array<uint8_t, kSize> Hash(std::string_view data);
|
||||
static std::array<uint8_t, kSize> Hash(base::span<const uint8_t> data);
|
||||
|
||||
bssl::ScopedEVP_MD_CTX ctx_;
|
||||
|
Reference in New Issue
Block a user