0

Refactor getting a serial number on mac to all use a common utility function.

Change-Id: I5eb6b0d6b6d333a705294a3334547268cb4fdd70
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1954640
Reviewed-by: Nico Weber <thakis@chromium.org>
Reviewed-by: Robert Sesek <rsesek@chromium.org>
Reviewed-by: Julian Pastarmov <pastarmovj@chromium.org>
Reviewed-by: Roger Tawa <rogerta@chromium.org>
Commit-Queue: Tim Song <tengs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#723557}
This commit is contained in:
Tim Song
2019-12-10 22:43:13 +00:00
committed by Commit Bot
parent 3aec7250bd
commit 3aa6779760
5 changed files with 37 additions and 68 deletions
base/mac
chrome/browser
extensions
api
enterprise_reporting_private
policy
rlz/mac/lib

@ -198,6 +198,9 @@ BASE_EXPORT bool ParseModelIdentifier(const std::string& ident,
// displaying to the user.
BASE_EXPORT std::string GetOSDisplayName();
// Returns the serial number of the macOS device.
BASE_EXPORT std::string GetPlatformSerialNumber();
} // namespace mac
} // namespace base

@ -493,5 +493,28 @@ std::string GetOSDisplayName() {
return os_name + " " + version_string;
}
std::string GetPlatformSerialNumber() {
base::mac::ScopedIOObject<io_service_t> expert_device(
IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching("IOPlatformExpertDevice")));
if (!expert_device) {
DLOG(ERROR) << "Error retrieving the machine serial number.";
return std::string();
}
base::ScopedCFTypeRef<CFTypeRef> serial_number(
IORegistryEntryCreateCFProperty(expert_device,
CFSTR(kIOPlatformSerialNumberKey),
kCFAllocatorDefault, 0));
CFStringRef serial_number_cfstring =
base::mac::CFCast<CFStringRef>(serial_number);
if (!serial_number_cfstring) {
DLOG(ERROR) << "Error retrieving the machine serial number.";
return std::string();
}
return base::SysCFStringRefToUTF8(serial_number_cfstring);
}
} // namespace mac
} // namespace base

@ -5,12 +5,14 @@
#include "chrome/browser/extensions/api/enterprise_reporting_private/device_info_fetcher_mac.h"
#import <Foundation/Foundation.h>
#import <IOKit/IOKitLib.h>
#import <sys/sysctl.h>
#include <IOKit/IOKitLib.h>
#include <sys/sysctl.h>
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_ioobject.h"
#include "base/process/launch.h"
@ -38,26 +40,8 @@ std::string GetDeviceHostName() {
return net::GetHostName();
}
// This approach was described here:
// https://developer.apple.com/library/content/technotes/tn1103/_index.html
std::string GetSerialNumber() {
std::string serial_number;
base::mac::ScopedIOObject<io_service_t> platform_expert(
IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching("IOPlatformExpertDevice")));
if (platform_expert) {
base::ScopedCFTypeRef<CFTypeRef> serial_number_cf_string(
IORegistryEntryCreateCFProperty(platform_expert,
CFSTR(kIOPlatformSerialNumberKey),
kCFAllocatorDefault, 0));
if (serial_number_cf_string) {
serial_number = base::SysCFStringRefToUTF8(
base::mac::CFCastStrict<CFStringRef>(serial_number_cf_string));
}
}
return serial_number;
return base::mac::GetPlatformSerialNumber();
}
enterprise_reporting_private::SettingValue GetScreenlockSecured() {

@ -15,6 +15,7 @@
#include "base/hash/sha1.h"
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_ioobject.h"
#include "base/no_destructor.h"
@ -179,27 +180,7 @@ BrowserDMTokenStorageMac::BrowserDMTokenStorageMac()
BrowserDMTokenStorageMac::~BrowserDMTokenStorageMac() {}
std::string BrowserDMTokenStorageMac::InitClientId() {
// Returns the device s/n.
base::mac::ScopedIOObject<io_service_t> expert_device(
IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching("IOPlatformExpertDevice")));
if (!expert_device) {
SYSLOG(ERROR) << "Error retrieving the machine serial number.";
return std::string();
}
base::ScopedCFTypeRef<CFTypeRef> serial_number(
IORegistryEntryCreateCFProperty(expert_device,
CFSTR(kIOPlatformSerialNumberKey),
kCFAllocatorDefault, 0));
CFStringRef serial_number_cfstring =
base::mac::CFCast<CFStringRef>(serial_number);
if (!serial_number_cfstring) {
SYSLOG(ERROR) << "Error retrieving the machine serial number.";
return std::string();
}
return base::SysCFStringRefToUTF8(serial_number_cfstring);
return base::mac::GetPlatformSerialNumber();
}
std::string BrowserDMTokenStorageMac::InitEnrollmentToken() {

@ -12,11 +12,11 @@
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_ioobject.h"
#include "base/strings/string16.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
namespace rlz_lib {
@ -97,27 +97,6 @@ bool GetMacAddress(unsigned char* buffer, size_t size) {
return result;
}
CFStringRef CopySerialNumber() {
base::mac::ScopedIOObject<io_service_t> expert_device(
IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching("IOPlatformExpertDevice")));
if (!expert_device)
return NULL;
base::ScopedCFTypeRef<CFTypeRef> serial_number(
IORegistryEntryCreateCFProperty(expert_device,
CFSTR(kIOPlatformSerialNumberKey),
kCFAllocatorDefault,
0));
CFStringRef serial_number_cfstring =
base::mac::CFCast<CFStringRef>(serial_number);
if (!serial_number_cfstring)
return NULL;
ignore_result(serial_number.release());
return serial_number_cfstring;
}
} // namespace
bool GetRawMachineId(base::string16* data, int* more_data) {
@ -134,12 +113,11 @@ bool GetRawMachineId(base::string16* data, int* more_data) {
// A MAC address is enough to uniquely identify a machine, but it's only 6
// bytes, 3 of which are manufacturer-determined. To make brute-forcing the
// SHA1 of this harder, also append the system's serial number.
CFStringRef serial = CopySerialNumber();
if (serial) {
std::string serial = base::mac::GetPlatformSerialNumber();
if (!serial.empty()) {
if (!data->empty())
*data += base::UTF8ToUTF16(" ");
*data += base::UTF8ToUTF16("serial:") + base::SysCFStringRefToUTF16(serial);
CFRelease(serial);
*data += base::UTF8ToUTF16("serial:") + base::UTF8ToUTF16(serial);
}
// On windows, this is set to the volume id. Since it's not scrambled before