1// Copyright 2012 The Chromium Authors 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> 8#import <UIKit/UIKit.h> 9#include <ifaddrs.h> 10#include <net/if_dl.h> 11#include <stddef.h> 12#include <string.h> 13#include <sys/socket.h> 14#include <sys/sysctl.h> 15 16#include <memory> 17 18#include "base/apple/scoped_cftyperef.h" 19#include "base/check.h" 20#include "base/numerics/safe_conversions.h" 21#include "base/posix/sysctl.h" 22#include "base/strings/stringprintf.h" 23#include "base/strings/sys_string_conversions.h" 24 25namespace { 26 27// Client ID key in the user preferences. 28NSString* const kLegacyClientIdPreferenceKey = @"ChromiumClientID"; 29NSString* const kClientIdPreferenceKey = @"ChromeClientID"; 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. 32NSString* const kHardwareTypePreferenceKey = @"ClientIDGenerationHardwareType"; 33// Default salt for device ids. 34const char kDefaultSalt[] = "Salt"; 35// Zero UUID returned on buggy iOS devices. 36NSString* const kZeroUUID = @"00000000-0000-0000-0000-000000000000"; 37 38NSString* 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: 45 // https://openradar.appspot.com/12377282. If this is the case, revert to 46 // generating a new one. 47 if (!client_id || [client_id isEqualToString:kZeroUUID]) { 48 client_id = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; 49 if ([client_id isEqualToString:kZeroUUID]) 50 client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId()); 51 } 52 return client_id; 53} 54 55} // namespace 56 57namespace ios::device_util { 58 59std::string GetPlatform() { 60#if TARGET_OS_SIMULATOR 61 return getenv("SIMULATOR_MODEL_IDENTIFIER"); 62#elif TARGET_OS_IPHONE 63 return base::StringSysctl({CTL_HW, HW_MACHINE}).value(); 64#endif 65} 66 67bool RamIsAtLeast512Mb() { 68 // 512MB devices report anywhere from 502-504 MB, use 450 MB just to be safe. 69 return RamIsAtLeast(450); 70} 71 72bool RamIsAtLeast1024Mb() { 73 // 1GB devices report anywhere from 975-999 MB, use 900 MB just to be safe. 74 return RamIsAtLeast(900); 75} 76 77bool RamIsAtLeast(uint64_t ram_in_mb) { 78 uint64_t memory_size = 0; 79 size_t size = sizeof(memory_size); 80 if (sysctlbyname("hw.memsize", &memory_size, &size, NULL, 0) == 0) { 81 // Anything >= 500M, call high ram. 82 return memory_size >= ram_in_mb * 1024 * 1024; 83 } 84 return false; 85} 86 87bool IsSingleCoreDevice() { 88 uint64_t cpu_number = 0; 89 size_t sizes = sizeof(cpu_number); 90 sysctlbyname("hw.physicalcpu", &cpu_number, &sizes, NULL, 0); 91 return cpu_number == 1; 92} 93 94std::string GetMacAddress(const std::string& interface_name) { 95 std::string mac_string; 96 struct ifaddrs* addresses; 97 if (getifaddrs(&addresses) == 0) { 98 for (struct ifaddrs* address = addresses; address; 99 address = address->ifa_next) { 100 if ((address->ifa_addr->sa_family == AF_LINK) && 101 strcmp(interface_name.c_str(), address->ifa_name) == 0) { 102 const struct sockaddr_dl* found_address_struct = 103 reinterpret_cast<const struct sockaddr_dl*>(address->ifa_addr); 104 105 // |found_address_struct->sdl_data| contains the interface name followed 106 // by the interface address. The address part can be accessed based on 107 // the length of the name, that is, |found_address_struct->sdl_nlen|. 108 const unsigned char* found_address = 109 reinterpret_cast<const unsigned char*>( 110 &found_address_struct->sdl_data[ 111 found_address_struct->sdl_nlen]); 112 113 int found_address_length = found_address_struct->sdl_alen; 114 for (int i = 0; i < found_address_length; ++i) { 115 if (i != 0) 116 mac_string.push_back(':'); 117 base::StringAppendF(&mac_string, "%02X", found_address[i]); 118 } 119 break; 120 } 121 } 122 freeifaddrs(addresses); 123 } 124 return mac_string; 125} 126 127std::string GetRandomId() { 128 base::apple::ScopedCFTypeRef<CFUUIDRef> uuid_object( 129 CFUUIDCreate(kCFAllocatorDefault)); 130 base::apple::ScopedCFTypeRef<CFStringRef> uuid_string( 131 CFUUIDCreateString(kCFAllocatorDefault, uuid_object.get())); 132 return base::SysCFStringRefToUTF8(uuid_string.get()); 133} 134 135std::string GetDeviceIdentifier(const char* salt) { 136 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; 137 138 NSString* last_seen_hardware = 139 [defaults stringForKey:kHardwareTypePreferenceKey]; 140 NSString* current_hardware = base::SysUTF8ToNSString(GetPlatform()); 141 if (!last_seen_hardware) { 142 last_seen_hardware = current_hardware; 143 [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey]; 144 [defaults synchronize]; 145 } 146 147 NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey]; 148 149 if (!client_id || ![last_seen_hardware isEqualToString:current_hardware]) { 150 client_id = GenerateClientId(); 151 [defaults setObject:client_id forKey:kClientIdPreferenceKey]; 152 [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey]; 153 [defaults synchronize]; 154 } 155 156 return GetSaltedString(base::SysNSStringToUTF8(client_id), 157 salt ? salt : kDefaultSalt); 158} 159 160std::string GetVendorId() { 161 return base::SysNSStringToUTF8( 162 [[[UIDevice currentDevice] identifierForVendor] UUIDString]); 163} 164 165std::string GetSaltedString(const std::string& in_string, 166 const std::string& salt) { 167 DCHECK(salt.length()); 168 NSData* hash_data = [base::SysUTF8ToNSString(in_string + salt) 169 dataUsingEncoding:NSUTF8StringEncoding]; 170 171 unsigned char hash[CC_SHA256_DIGEST_LENGTH]; 172 CC_SHA256([hash_data bytes], base::checked_cast<CC_LONG>([hash_data length]), 173 hash); 174 CFUUIDBytes* uuid_bytes = reinterpret_cast<CFUUIDBytes*>(hash); 175 176 base::apple::ScopedCFTypeRef<CFUUIDRef> uuid_object( 177 CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes)); 178 base::apple::ScopedCFTypeRef<CFStringRef> device_id( 179 CFUUIDCreateString(kCFAllocatorDefault, uuid_object.get())); 180 return base::SysCFStringRefToUTF8(device_id.get()); 181} 182 183} // namespace ios::device_util 184