blob: f16175b8a197d0115761ddaa5581176565823d73 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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 <CoreFoundation/CoreFoundation.h>
6#include <IOKit/IOKitLib.h>
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01007#include <IOKit/network/IOEthernetController.h>
Torne (Richard Coles)58218062012-11-14 11:43:16 +00008#include <IOKit/network/IOEthernetInterface.h>
9#include <IOKit/network/IONetworkInterface.h>
Torne (Richard Coles)58218062012-11-14 11:43:16 +000010
11#include "base/logging.h"
12#include "base/mac/foundation_util.h"
13#include "base/mac/scoped_cftyperef.h"
14#include "base/mac/scoped_ioobject.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010015#include "base/strings/string16.h"
16#include "base/strings/stringprintf.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010017#include "base/strings/sys_string_conversions.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010018#include "base/strings/utf_string_conversions.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019
20namespace rlz_lib {
21
22namespace {
23
24// See http://developer.apple.com/library/mac/#technotes/tn1103/_index.html
25
26// The caller is responsible for freeing |matching_services|.
27bool FindEthernetInterfaces(io_iterator_t* matching_services) {
Ben Murdocheb525c52013-07-10 11:40:50 +010028 base::ScopedCFTypeRef<CFMutableDictionaryRef> matching_dict(
Torne (Richard Coles)58218062012-11-14 11:43:16 +000029 IOServiceMatching(kIOEthernetInterfaceClass));
30 if (!matching_dict)
31 return false;
32
Ben Murdocheb525c52013-07-10 11:40:50 +010033 base::ScopedCFTypeRef<CFMutableDictionaryRef> primary_interface(
34 CFDictionaryCreateMutable(kCFAllocatorDefault,
35 0,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000036 &kCFTypeDictionaryKeyCallBacks,
37 &kCFTypeDictionaryValueCallBacks));
38 if (!primary_interface)
39 return false;
40
41 CFDictionarySetValue(
42 primary_interface, CFSTR(kIOPrimaryInterface), kCFBooleanTrue);
43 CFDictionarySetValue(
44 matching_dict, CFSTR(kIOPropertyMatchKey), primary_interface);
45
46 kern_return_t kern_result = IOServiceGetMatchingServices(
47 kIOMasterPortDefault, matching_dict.release(), matching_services);
48
49 return kern_result == KERN_SUCCESS;
50}
51
52bool GetMACAddressFromIterator(io_iterator_t primary_interface_iterator,
53 uint8_t* buffer, size_t buffer_size) {
54 if (buffer_size < kIOEthernetAddressSize)
55 return false;
56
57 bool success = false;
58
59 bzero(buffer, buffer_size);
60 base::mac::ScopedIOObject<io_object_t> primary_interface;
61 while (primary_interface.reset(IOIteratorNext(primary_interface_iterator)),
62 primary_interface) {
63 io_object_t primary_interface_parent;
64 kern_return_t kern_result = IORegistryEntryGetParentEntry(
65 primary_interface, kIOServicePlane, &primary_interface_parent);
66 base::mac::ScopedIOObject<io_object_t> primary_interface_parent_deleter(
67 primary_interface_parent);
68 success = kern_result == KERN_SUCCESS;
69
70 if (!success)
71 continue;
72
Ben Murdocheb525c52013-07-10 11:40:50 +010073 base::ScopedCFTypeRef<CFTypeRef> mac_data(
Torne (Richard Coles)58218062012-11-14 11:43:16 +000074 IORegistryEntryCreateCFProperty(primary_interface_parent,
75 CFSTR(kIOMACAddress),
76 kCFAllocatorDefault,
77 0));
78 CFDataRef mac_data_data = base::mac::CFCast<CFDataRef>(mac_data);
79 if (mac_data_data) {
80 CFDataGetBytes(
81 mac_data_data, CFRangeMake(0, kIOEthernetAddressSize), buffer);
82 }
83 }
84
85 return success;
86}
87
88bool GetMacAddress(unsigned char* buffer, size_t size) {
89 io_iterator_t primary_interface_iterator;
90 if (!FindEthernetInterfaces(&primary_interface_iterator))
91 return false;
92 bool result = GetMACAddressFromIterator(
93 primary_interface_iterator, buffer, size);
94 IOObjectRelease(primary_interface_iterator);
95 return result;
96}
97
98CFStringRef CopySerialNumber() {
99 base::mac::ScopedIOObject<io_service_t> expert_device(
100 IOServiceGetMatchingService(kIOMasterPortDefault,
101 IOServiceMatching("IOPlatformExpertDevice")));
102 if (!expert_device)
103 return NULL;
104
Ben Murdocheb525c52013-07-10 11:40:50 +0100105 base::ScopedCFTypeRef<CFTypeRef> serial_number(
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000106 IORegistryEntryCreateCFProperty(expert_device,
107 CFSTR(kIOPlatformSerialNumberKey),
108 kCFAllocatorDefault,
109 0));
110 CFStringRef serial_number_cfstring =
111 base::mac::CFCast<CFStringRef>(serial_number);
112 if (!serial_number_cfstring)
113 return NULL;
114
115 ignore_result(serial_number.release());
116 return serial_number_cfstring;
117}
118
119} // namespace
120
121bool GetRawMachineId(string16* data, int* more_data) {
122 uint8_t mac_address[kIOEthernetAddressSize];
123
124 data->clear();
125 if (GetMacAddress(mac_address, sizeof(mac_address))) {
126 *data += ASCIIToUTF16(base::StringPrintf("mac:%02x%02x%02x%02x%02x%02x",
127 mac_address[0], mac_address[1], mac_address[2],
128 mac_address[3], mac_address[4], mac_address[5]));
129 }
130
131 // A MAC address is enough to uniquely identify a machine, but it's only 6
132 // bytes, 3 of which are manufacturer-determined. To make brute-forcing the
133 // SHA1 of this harder, also append the system's serial number.
134 CFStringRef serial = CopySerialNumber();
135 if (serial) {
136 if (!data->empty())
137 *data += UTF8ToUTF16(" ");
138 *data += UTF8ToUTF16("serial:") + base::SysCFStringRefToUTF16(serial);
139 CFRelease(serial);
140 }
141
142 // On windows, this is set to the volume id. Since it's not scrambled before
143 // being sent, just set it to 1.
144 *more_data = 1;
145 return true;
146}
147
148} // namespace rlz_lib