Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 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 "base/ios/device_util.h" |
| 6 | |
| 7 | #include <CommonCrypto/CommonDigest.h> |
[email protected] | aebcd0dd | 2012-10-05 17:48:58 | [diff] [blame] | 8 | #import <UIKit/UIKit.h> |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 9 | #include <ifaddrs.h> |
| 10 | #include <net/if_dl.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 11 | #include <stddef.h> |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 12 | #include <string.h> |
| 13 | #include <sys/socket.h> |
| 14 | #include <sys/sysctl.h> |
| 15 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
Avi Drissman | a09d7dd | 2023-08-17 16:26:58 | [diff] [blame^] | 18 | #include "base/apple/scoped_cftyperef.h" |
Hans Wennborg | a47ddf8 | 2020-05-05 18:08:07 | [diff] [blame] | 19 | #include "base/check.h" |
Peter Kasting | 2f61c8b | 2022-07-19 23:43:46 | [diff] [blame] | 20 | #include "base/numerics/safe_conversions.h" |
[email protected] | d1a5a2f | 2013-06-10 21:17:40 | [diff] [blame] | 21 | #include "base/strings/string_util.h" |
| 22 | #include "base/strings/stringprintf.h" |
[email protected] | 9fe1a5b | 2013-02-07 19:18:03 | [diff] [blame] | 23 | #include "base/strings/sys_string_conversions.h" |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 24 | |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | // Client ID key in the user preferences. |
[email protected] | d724b55 | 2012-12-13 17:39:47 | [diff] [blame] | 28 | NSString* const kLegacyClientIdPreferenceKey = @"ChromiumClientID"; |
| 29 | NSString* const kClientIdPreferenceKey = @"ChromeClientID"; |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 30 | // Current hardware type. This is used to detect that a device has been backed |
| 31 | // up and restored to another device, and allows regenerating a new device id. |
| 32 | NSString* const kHardwareTypePreferenceKey = @"ClientIDGenerationHardwareType"; |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 33 | // Default salt for device ids. |
| 34 | const char kDefaultSalt[] = "Salt"; |
[email protected] | d724b55 | 2012-12-13 17:39:47 | [diff] [blame] | 35 | // Zero UUID returned on buggy iOS devices. |
| 36 | NSString* const kZeroUUID = @"00000000-0000-0000-0000-000000000000"; |
| 37 | |
| 38 | NSString* GenerateClientId() { |
| 39 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 40 | |
| 41 | // Try to migrate from legacy client id. |
| 42 | NSString* client_id = [defaults stringForKey:kLegacyClientIdPreferenceKey]; |
| 43 | |
| 44 | // Some iOS6 devices return a buggy identifierForVendor: |
Avi Drissman | b34db7a | 2023-01-04 21:50:15 | [diff] [blame] | 45 | // https://openradar.appspot.com/12377282. If this is the case, revert to |
[email protected] | d724b55 | 2012-12-13 17:39:47 | [diff] [blame] | 46 | // generating a new one. |
| 47 | if (!client_id || [client_id isEqualToString:kZeroUUID]) { |
[email protected] | d01f311 | 2014-06-19 22:49:55 | [diff] [blame] | 48 | client_id = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; |
| 49 | if ([client_id isEqualToString:kZeroUUID]) |
[email protected] | d724b55 | 2012-12-13 17:39:47 | [diff] [blame] | 50 | client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId()); |
[email protected] | d724b55 | 2012-12-13 17:39:47 | [diff] [blame] | 51 | } |
| 52 | return client_id; |
| 53 | } |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 54 | |
| 55 | } // namespace |
| 56 | |
Avi Drissman | 354864c | 2023-06-01 20:06:03 | [diff] [blame] | 57 | namespace ios::device_util { |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 58 | |
| 59 | std::string GetPlatform() { |
Justin Cohen | 518b744 | 2022-09-17 21:26:49 | [diff] [blame] | 60 | #if TARGET_OS_SIMULATOR |
| 61 | return getenv("SIMULATOR_MODEL_IDENTIFIER"); |
| 62 | #elif TARGET_OS_IPHONE |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 63 | std::string platform; |
| 64 | size_t size = 0; |
| 65 | sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
Brett Wilson | e3c4d1a | 2015-07-07 23:38:09 | [diff] [blame] | 66 | sysctlbyname("hw.machine", base::WriteInto(&platform, size), &size, NULL, 0); |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 67 | return platform; |
Justin Cohen | 518b744 | 2022-09-17 21:26:49 | [diff] [blame] | 68 | #endif |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | 4f150ec7 | 2014-03-18 21:54:42 | [diff] [blame] | 71 | bool RamIsAtLeast512Mb() { |
| 72 | // 512MB devices report anywhere from 502-504 MB, use 450 MB just to be safe. |
| 73 | return RamIsAtLeast(450); |
| 74 | } |
| 75 | |
| 76 | bool RamIsAtLeast1024Mb() { |
| 77 | // 1GB devices report anywhere from 975-999 MB, use 900 MB just to be safe. |
| 78 | return RamIsAtLeast(900); |
| 79 | } |
| 80 | |
| 81 | bool RamIsAtLeast(uint64_t ram_in_mb) { |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 82 | uint64_t memory_size = 0; |
| 83 | size_t size = sizeof(memory_size); |
| 84 | if (sysctlbyname("hw.memsize", &memory_size, &size, NULL, 0) == 0) { |
[email protected] | 9317c249 | 2013-12-11 22:18:41 | [diff] [blame] | 85 | // Anything >= 500M, call high ram. |
[email protected] | 4f150ec7 | 2014-03-18 21:54:42 | [diff] [blame] | 86 | return memory_size >= ram_in_mb * 1024 * 1024; |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | bool IsSingleCoreDevice() { |
| 92 | uint64_t cpu_number = 0; |
| 93 | size_t sizes = sizeof(cpu_number); |
| 94 | sysctlbyname("hw.physicalcpu", &cpu_number, &sizes, NULL, 0); |
| 95 | return cpu_number == 1; |
| 96 | } |
| 97 | |
| 98 | std::string GetMacAddress(const std::string& interface_name) { |
| 99 | std::string mac_string; |
| 100 | struct ifaddrs* addresses; |
| 101 | if (getifaddrs(&addresses) == 0) { |
| 102 | for (struct ifaddrs* address = addresses; address; |
| 103 | address = address->ifa_next) { |
| 104 | if ((address->ifa_addr->sa_family == AF_LINK) && |
| 105 | strcmp(interface_name.c_str(), address->ifa_name) == 0) { |
| 106 | const struct sockaddr_dl* found_address_struct = |
| 107 | reinterpret_cast<const struct sockaddr_dl*>(address->ifa_addr); |
| 108 | |
| 109 | // |found_address_struct->sdl_data| contains the interface name followed |
| 110 | // by the interface address. The address part can be accessed based on |
| 111 | // the length of the name, that is, |found_address_struct->sdl_nlen|. |
| 112 | const unsigned char* found_address = |
| 113 | reinterpret_cast<const unsigned char*>( |
| 114 | &found_address_struct->sdl_data[ |
| 115 | found_address_struct->sdl_nlen]); |
| 116 | |
| 117 | int found_address_length = found_address_struct->sdl_alen; |
| 118 | for (int i = 0; i < found_address_length; ++i) { |
| 119 | if (i != 0) |
| 120 | mac_string.push_back(':'); |
| 121 | base::StringAppendF(&mac_string, "%02X", found_address[i]); |
| 122 | } |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | freeifaddrs(addresses); |
| 127 | } |
| 128 | return mac_string; |
| 129 | } |
| 130 | |
| 131 | std::string GetRandomId() { |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 132 | base::ScopedCFTypeRef<CFUUIDRef> uuid_object( |
| 133 | CFUUIDCreate(kCFAllocatorDefault)); |
| 134 | base::ScopedCFTypeRef<CFStringRef> uuid_string( |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 135 | CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); |
| 136 | return base::SysCFStringRefToUTF8(uuid_string); |
| 137 | } |
| 138 | |
| 139 | std::string GetDeviceIdentifier(const char* salt) { |
| 140 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 141 | |
| 142 | NSString* last_seen_hardware = |
| 143 | [defaults stringForKey:kHardwareTypePreferenceKey]; |
| 144 | NSString* current_hardware = base::SysUTF8ToNSString(GetPlatform()); |
| 145 | if (!last_seen_hardware) { |
| 146 | last_seen_hardware = current_hardware; |
| 147 | [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey]; |
| 148 | [defaults synchronize]; |
| 149 | } |
| 150 | |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 151 | NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey]; |
| 152 | |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 153 | if (!client_id || ![last_seen_hardware isEqualToString:current_hardware]) { |
[email protected] | d724b55 | 2012-12-13 17:39:47 | [diff] [blame] | 154 | client_id = GenerateClientId(); |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 155 | [defaults setObject:client_id forKey:kClientIdPreferenceKey]; |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 156 | [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey]; |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 157 | [defaults synchronize]; |
| 158 | } |
| 159 | |
[email protected] | 0644db7 | 2014-06-05 20:05:43 | [diff] [blame] | 160 | return GetSaltedString(base::SysNSStringToUTF8(client_id), |
| 161 | salt ? salt : kDefaultSalt); |
[email protected] | 940b67d | 2014-06-03 14:27:18 | [diff] [blame] | 162 | } |
| 163 | |
Guillaume Jenkins | 74049b1 | 2020-12-01 18:53:46 | [diff] [blame] | 164 | std::string GetVendorId() { |
| 165 | return base::SysNSStringToUTF8( |
| 166 | [[[UIDevice currentDevice] identifierForVendor] UUIDString]); |
| 167 | } |
| 168 | |
[email protected] | 940b67d | 2014-06-03 14:27:18 | [diff] [blame] | 169 | std::string GetSaltedString(const std::string& in_string, |
| 170 | const std::string& salt) { |
[email protected] | 940b67d | 2014-06-03 14:27:18 | [diff] [blame] | 171 | DCHECK(salt.length()); |
[email protected] | 0644db7 | 2014-06-05 20:05:43 | [diff] [blame] | 172 | NSData* hash_data = [base::SysUTF8ToNSString(in_string + salt) |
| 173 | dataUsingEncoding:NSUTF8StringEncoding]; |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 174 | |
| 175 | unsigned char hash[CC_SHA256_DIGEST_LENGTH]; |
Peter Kasting | 2f61c8b | 2022-07-19 23:43:46 | [diff] [blame] | 176 | CC_SHA256([hash_data bytes], base::checked_cast<CC_LONG>([hash_data length]), |
| 177 | hash); |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 178 | CFUUIDBytes* uuid_bytes = reinterpret_cast<CFUUIDBytes*>(hash); |
| 179 | |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 180 | base::ScopedCFTypeRef<CFUUIDRef> uuid_object( |
| 181 | CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes)); |
| 182 | base::ScopedCFTypeRef<CFStringRef> device_id( |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 183 | CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); |
| 184 | return base::SysCFStringRefToUTF8(device_id); |
| 185 | } |
| 186 | |
Avi Drissman | 354864c | 2023-06-01 20:06:03 | [diff] [blame] | 187 | } // namespace ios::device_util |