| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include "azure_c_shared_utility/platform.h" |
| 5 |
#include "azure_c_shared_utility/optimize_size.h" |
| 6 |
#include "azure_c_shared_utility/xio.h" |
| 7 |
#include "azure_c_shared_utility/strings.h" |
| 8 |
#include "azure_c_shared_utility/xlogging.h" |
| 9 |
#include "azure_c_shared_utility/httpapiex.h" |
| 10 |
#include "winsock2.h" |
| 11 |
#include "minwindef.h" |
| 12 |
#include "winnt.h" |
| 13 |
|
| 14 |
#ifdef USE_OPENSSL |
| 15 |
#include "azure_c_shared_utility/tlsio_openssl.h" |
| 16 |
#endif |
| 17 |
#if USE_CYCLONESSL |
| 18 |
#include "azure_c_shared_utility/tlsio_cyclonessl.h" |
| 19 |
#endif |
| 20 |
#if USE_WOLFSSL |
| 21 |
#include "azure_c_shared_utility/tlsio_wolfssl.h" |
| 22 |
#endif |
| 23 |
#if USE_MBEDTLS |
| 24 |
#include "azure_c_shared_utility/tlsio_mbedtls.h" |
| 25 |
#endif |
| 26 |
#if USE_BEARSSL |
| 27 |
#include "azure_c_shared_utility/tlsio_bearssl.h" |
| 28 |
#endif |
| 29 |
|
| 30 |
#include "azure_c_shared_utility/tlsio_schannel.h" |
| 31 |
|
| 32 |
int platform_init(void) |
| 33 |
{ |
| 34 |
int result; |
| 35 |
|
| 36 |
WSADATA wsaData; |
| 37 |
int error_code = WSAStartup(MAKEWORD(2, 2), &wsaData); |
| 38 |
if (error_code != 0) |
| 39 |
{ |
| 40 |
LogError("WSAStartup failed: 0x%x", error_code); |
| 41 |
result = MU_FAILURE; |
| 42 |
} |
| 43 |
else |
| 44 |
{ |
| 45 |
result = 0; |
| 46 |
#ifndef DONT_USE_UPLOADTOBLOB |
| 47 |
if (HTTPAPIEX_Init() == HTTPAPIEX_ERROR) |
| 48 |
{ |
| 49 |
LogError("HTTP for upload to blob failed on initialization."); |
| 50 |
result = MU_FAILURE; |
| 51 |
} |
| 52 |
#endif /* DONT_USE_UPLOADTOBLOB */ |
| 53 |
#ifdef USE_OPENSSL |
| 54 |
if (result == 0) |
| 55 |
{ |
| 56 |
result = tlsio_openssl_init(); |
| 57 |
} |
| 58 |
#endif |
| 59 |
} |
| 60 |
return result; |
| 61 |
} |
| 62 |
|
| 63 |
const IO_INTERFACE_DESCRIPTION* platform_get_default_tlsio(void) |
| 64 |
{ |
| 65 |
#ifdef USE_OPENSSL |
| 66 |
return tlsio_openssl_get_interface_description(); |
| 67 |
#elif USE_CYCLONESSL |
| 68 |
return tlsio_cyclonessl_get_interface_description(); |
| 69 |
#elif USE_WOLFSSL |
| 70 |
return tlsio_wolfssl_get_interface_description(); |
| 71 |
#elif USE_BEARSSL |
| 72 |
return tlsio_bearssl_get_interface_description(); |
| 73 |
#elif USE_MBEDTLS |
| 74 |
return tlsio_mbedtls_get_interface_description(); |
| 75 |
#else |
| 76 |
return tlsio_schannel_get_interface_description(); |
| 77 |
#endif |
| 78 |
} |
| 79 |
|
| 80 |
static char* get_win_sqm_info(void) |
| 81 |
{ |
| 82 |
char* result; |
| 83 |
LONG openKey; |
| 84 |
DWORD options = 0; |
| 85 |
HKEY openResult; |
| 86 |
HKEY hKey = HKEY_LOCAL_MACHINE; |
| 87 |
LPCSTR subKey = "Software\\Microsoft\\SQMClient"; |
| 88 |
LPCSTR lpValue = "MachineId"; |
| 89 |
DWORD dwFlags = RRF_RT_ANY; |
| 90 |
|
| 91 |
LONG getRegValue = ERROR_INVALID_HANDLE; |
| 92 |
DWORD dataType; |
| 93 |
DWORD size = GUID_LENGTH; |
| 94 |
PVOID pvData; |
| 95 |
|
| 96 |
// SQM values are guids in the system, we will allocate enough space to hold that |
| 97 |
if ((result = (char*)malloc(GUID_LENGTH)) == NULL) |
| 98 |
{ |
| 99 |
LogError("Failure allocating sqm info"); |
| 100 |
} |
| 101 |
else if ((openKey = RegOpenKeyExA(hKey, subKey, options, KEY_READ, &openResult)) != ERROR_SUCCESS) |
| 102 |
{ |
| 103 |
LogError("Failure opening registry key: %d:%s", GetLastError(), subKey); |
| 104 |
free(result); |
| 105 |
result = NULL; |
| 106 |
} |
| 107 |
else |
| 108 |
{ |
| 109 |
pvData = result; |
| 110 |
if ((getRegValue = RegGetValueA(openResult, NULL, lpValue, dwFlags, &dataType, pvData, &size)) != ERROR_SUCCESS) |
| 111 |
{ |
| 112 |
// Failed to read value, so try opening the 64-bit reg key |
| 113 |
// in case this is an x86 binary being run on Windows x64 |
| 114 |
if ((openKey = RegOpenKeyExA(hKey, subKey, options, KEY_READ | KEY_WOW64_64KEY, &openResult)) != ERROR_SUCCESS) |
| 115 |
{ |
| 116 |
LogError("Failure opening registry sub key: %d:%s", GetLastError(), subKey); |
| 117 |
free(result); |
| 118 |
result = NULL; |
| 119 |
} |
| 120 |
else if ((getRegValue = RegGetValueA(openResult, NULL, lpValue, dwFlags, &dataType, pvData, &size)) != ERROR_SUCCESS) |
| 121 |
{ |
| 122 |
LogError("Failure opening registry sub key: %d:%s", GetLastError(), subKey); |
| 123 |
free(result); |
| 124 |
result = NULL; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if (getRegValue != ERROR_SUCCESS) |
| 129 |
{ |
| 130 |
LogError("Failure retrieving SQM info Error value: %d", GetLastError()); |
| 131 |
free(result); |
| 132 |
result = NULL; |
| 133 |
} |
| 134 |
RegCloseKey(openResult); |
| 135 |
} |
| 136 |
return result; |
| 137 |
} |
| 138 |
|
| 139 |
STRING_HANDLE platform_get_platform_info(PLATFORM_INFO_OPTION options) |
| 140 |
{ |
| 141 |
// Expected format: "(<runtime name>; <operating system name>; <platform>)" |
| 142 |
STRING_HANDLE result; |
| 143 |
SYSTEM_INFO sys_info; |
| 144 |
OSVERSIONINFO osvi; |
| 145 |
char *arch; |
| 146 |
GetSystemInfo(&sys_info); |
| 147 |
|
| 148 |
switch (sys_info.wProcessorArchitecture) |
| 149 |
{ |
| 150 |
case PROCESSOR_ARCHITECTURE_AMD64: |
| 151 |
arch = "x64"; |
| 152 |
break; |
| 153 |
|
| 154 |
case PROCESSOR_ARCHITECTURE_ARM: |
| 155 |
arch = "ARM"; |
| 156 |
break; |
| 157 |
|
| 158 |
case PROCESSOR_ARCHITECTURE_IA64: |
| 159 |
arch = "IA64"; |
| 160 |
break; |
| 161 |
|
| 162 |
case PROCESSOR_ARCHITECTURE_INTEL: |
| 163 |
arch = "x32"; |
| 164 |
break; |
| 165 |
|
| 166 |
default: |
| 167 |
arch = "UNKNOWN"; |
| 168 |
break; |
| 169 |
} |
| 170 |
|
| 171 |
result = NULL; |
| 172 |
memset(&osvi, 0, sizeof(osvi)); |
| 173 |
osvi.dwOSVersionInfoSize = sizeof(osvi); |
| 174 |
#ifdef _MSC_VER |
| 175 |
#pragma warning(disable:4996 28159) // GetVersionEx is deprecated |
| 176 |
#endif |
| 177 |
if (GetVersionEx(&osvi)) |
| 178 |
{ |
| 179 |
DWORD product_type; |
| 180 |
if (GetProductInfo(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &product_type)) |
| 181 |
{ |
| 182 |
result = STRING_construct_sprintf("(native; WindowsProduct:0x%08x %d.%d; %s", product_type, osvi.dwMajorVersion, osvi.dwMinorVersion, arch); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if (result == NULL) |
| 187 |
{ |
| 188 |
DWORD dwVersion = GetVersion(); |
| 189 |
result = STRING_construct_sprintf("(native; WindowsProduct:Windows NT %d.%d; %s", LOBYTE(LOWORD(dwVersion)), HIBYTE(LOWORD(dwVersion)), arch); |
| 190 |
} |
| 191 |
#ifdef _MSC_VER |
| 192 |
#pragma warning(default:4996 28159) |
| 193 |
#endif |
| 194 |
|
| 195 |
if (result == NULL) |
| 196 |
{ |
| 197 |
LogError("STRING_construct_sprintf failed"); |
| 198 |
} |
| 199 |
else if (options & PLATFORM_INFO_OPTION_RETRIEVE_SQM) |
| 200 |
{ |
| 201 |
// Failure here should continue |
| 202 |
char* sqm_info = get_win_sqm_info(); |
| 203 |
if (sqm_info != NULL) |
| 204 |
{ |
| 205 |
if (STRING_sprintf(result, "; %s)", sqm_info) != 0) |
| 206 |
{ |
| 207 |
LogError("failure concat file"); |
| 208 |
} |
| 209 |
free(sqm_info); |
| 210 |
} |
| 211 |
else |
| 212 |
{ |
| 213 |
if (STRING_concat(result, ")") != 0) |
| 214 |
{ |
| 215 |
LogError("failure concat file"); |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
else |
| 220 |
{ |
| 221 |
if (STRING_concat(result, ")") != 0) |
| 222 |
{ |
| 223 |
LogError("failure concat file"); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
return result; |
| 228 |
} |
| 229 |
|
| 230 |
void platform_deinit(void) |
| 231 |
{ |
| 232 |
(void)WSACleanup(); |
| 233 |
|
| 234 |
#ifndef DONT_USE_UPLOADTOBLOB |
| 235 |
HTTPAPIEX_Deinit(); |
| 236 |
#endif /* DONT_USE_UPLOADTOBLOB */ |
| 237 |
|
| 238 |
#ifdef USE_OPENSSL |
| 239 |
tlsio_openssl_deinit(); |
| 240 |
#endif |
| 241 |
} |