common.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "def.hpp"
  2. namespace patcher {
  3. BOOL BackupFile(LPCTSTR file_path) {
  4. size_t file_path_len = _tcslen(file_path);
  5. TCHAR* backup_file_path = new TCHAR[file_path_len + 16]();
  6. _stprintf_s(backup_file_path, file_path_len + 16, TEXT("%s%s"), file_path, TEXT(".backup"));
  7. if (!CopyFile(file_path, backup_file_path, TRUE)) {
  8. switch (GetLastError()) {
  9. case ERROR_FILE_NOT_FOUND:
  10. _tprintf_s(TEXT("@[BackupFile]: Cannot find %s.\r\n"), file_path);
  11. break;
  12. case ERROR_FILE_EXISTS:
  13. _tprintf_s(TEXT("@[BackupFile]: Backup file already exists.\r\n"));
  14. break;
  15. case ERROR_ACCESS_DENIED:
  16. _tprintf_s(TEXT("@[BackupFile]: Access denied, please run as administrator.\r\n"));
  17. break;
  18. default:
  19. _tprintf_s(TEXT("Unknown error. CODE: 0x%08x @[BackupFile -> CopyFile]\r\n"), GetLastError());
  20. }
  21. delete[] backup_file_path;
  22. return FALSE;
  23. }
  24. _tprintf_s(TEXT("@[BackupFile]: %s has been backed up.\r\n"), file_path);
  25. delete[] backup_file_path;
  26. return TRUE;
  27. }
  28. RSA* GenerateRSAKey(int bits) {
  29. RSA* ret = RSA_generate_key(bits, RSA_F4, nullptr, nullptr);
  30. if (ret == nullptr) {
  31. _tprintf_s(TEXT("Failed to generate RSA key. CODE: 0x%08x @[GenerateRSAKey -> RSA_generate_key]\r\n"), ERR_get_error());
  32. return nullptr;
  33. }
  34. return ret;
  35. }
  36. BOOL WriteRSAPrivateKeyToFile(LPCTSTR filename, RSA* PrivateKey) {
  37. #ifdef UNICODE
  38. int req_size = WideCharToMultiByte(CP_ACP, 0, filename, -1, nullptr, 0, nullptr, nullptr);
  39. if (req_size == 0) {
  40. _tprintf_s(TEXT("Failed to convert wchar* to char*. CODE: 0x%08x @[WriteRSAPrivateKeyToFile -> WideCharToMultiByte]\r\n"), GetLastError());
  41. return FALSE;
  42. }
  43. char* temp_filename = new char[req_size]();
  44. WideCharToMultiByte(CP_ACP, 0, filename, -1, temp_filename, req_size, nullptr, nullptr);
  45. BIO* b = BIO_new(BIO_s_file());
  46. if (b == nullptr) {
  47. _tprintf_s(TEXT("Failed to create BIO object. CODE: 0x%08x @[WriteRSAPrivateKeyToFile -> BIO_new]\r\n"), ERR_get_error());
  48. delete[] temp_filename;
  49. return FALSE;
  50. }
  51. if (1 != BIO_write_filename(b, temp_filename)) {
  52. _tprintf_s(TEXT("Failed to set target file of BIO. CODE: 0x%08x @[WriteRSAPrivateKeyToFile -> BIO_write_filename]\r\n"), ERR_get_error());
  53. BIO_free_all(b);
  54. delete[] temp_filename;
  55. return FALSE;
  56. }
  57. delete[] temp_filename;
  58. #else
  59. BIO* b = BIO_new(BIO_s_file());
  60. if (b == nullptr) {
  61. _tprintf_s(TEXT("Failed to create BIO object. CODE: 0x%08x @[WriteRSAPrivateKeyToFile -> BIO_new]\r\n"), ERR_get_error());
  62. return FALSE;
  63. }
  64. if (1 != BIO_write_filename(b, filename)) {
  65. _tprintf_s(TEXT("Failed to set target file of BIO. CODE: 0x%08x @[WriteRSAPrivateKeyToFile -> BIO_write_filename]\r\n"), ERR_get_error());
  66. BIO_free_all(b);
  67. return FALSE;
  68. }
  69. #endif
  70. if (1 != PEM_write_bio_RSAPrivateKey(b, PrivateKey, nullptr, nullptr, 0, nullptr, nullptr)) {
  71. _tprintf_s(TEXT("Failed to write RSA private key. CODE: 0x%08x @[WriteRSAPrivateKeyToFile -> PEM_write_bio_RSAPrivateKey]\r\n"), ERR_get_error());
  72. BIO_free_all(b);
  73. return FALSE;
  74. } else {
  75. BIO_free_all(b);
  76. return TRUE;
  77. }
  78. }
  79. RSA* ReadRSAPrivateKeyFromFile(LPCTSTR filename) {
  80. #ifdef UNICODE
  81. int req_size = WideCharToMultiByte(CP_ACP, 0, filename, -1, nullptr, 0, nullptr, nullptr);
  82. if (req_size == 0) {
  83. _tprintf_s(TEXT("Failed to convert wchar* to char*. CODE: 0x%08x @[ReadRSAPrivateKeyFromFile -> WideCharToMultiByte]\r\n"), GetLastError());
  84. return FALSE;
  85. }
  86. char* temp_filename = new char[req_size]();
  87. WideCharToMultiByte(CP_ACP, 0, filename, -1, temp_filename, req_size, nullptr, nullptr);
  88. BIO* b = BIO_new(BIO_s_file());
  89. if (b == nullptr) {
  90. _tprintf_s(TEXT("Failed to create BIO object. CODE: 0x%08x @[ReadRSAPrivateKeyFromFile -> BIO_new]\r\n"), ERR_get_error());
  91. delete[] temp_filename;
  92. return FALSE;
  93. }
  94. if (1 != BIO_read_filename(b, temp_filename)) {
  95. _tprintf_s(TEXT("Failed to set target file of BIO. CODE: 0x%08x @[ReadRSAPrivateKeyFromFile -> BIO_read_filename]\r\n"), ERR_get_error());
  96. BIO_free_all(b);
  97. delete[] temp_filename;
  98. return FALSE;
  99. }
  100. delete[] temp_filename;
  101. #else
  102. BIO* b = BIO_new(BIO_s_file());
  103. if (b == nullptr) {
  104. _tprintf_s(TEXT("Failed to create BIO object. CODE: 0x%08x @[ReadRSAPrivateKeyFromFile -> BIO_new]\r\n"), ERR_get_error());
  105. return FALSE;
  106. }
  107. if (1 != BIO_read_filename(b, filename)) {
  108. _tprintf_s(TEXT("Failed to set target file of BIO. CODE: 0x%08x @[ReadRSAPrivateKeyFromFile -> BIO_read_filename]\r\n"), ERR_get_error());
  109. BIO_free_all(b);
  110. return FALSE;
  111. }
  112. #endif
  113. RSA* ret = PEM_read_bio_RSAPrivateKey(b, nullptr, nullptr, nullptr);
  114. if (ret == nullptr) {
  115. _tprintf_s(TEXT("Failed to read RSA private key. CODE: 0x%08x @[ReadRSAPrivateKeyFromFile -> PEM_read_bio_RSAPrivateKey]\r\n"), ERR_get_error());
  116. BIO_free_all(b);
  117. return nullptr;
  118. } else {
  119. BIO_free_all(b);
  120. return ret;
  121. }
  122. }
  123. BOOL GetNavicatVerion(LPCTSTR exe_path, DWORD* major_ver, DWORD* minor_ver) {
  124. DWORD FileVersionInfoSize = GetFileVersionInfoSize(exe_path, nullptr);
  125. if (FileVersionInfoSize == 0) {
  126. _tprintf_s(TEXT("Failed to get navicat.exe verion info. CODE: 0x%08x @[GetNavicatVerion -> GetFileVersionInfoSize]\r\n"), GetLastError());
  127. return FALSE;
  128. }
  129. LPVOID buf = new unsigned char[FileVersionInfoSize]();
  130. if (FALSE == GetFileVersionInfo(exe_path, 0, FileVersionInfoSize, buf)) {
  131. _tprintf_s(TEXT("Failed to get navicat.exe verion info. CODE: 0x%08x @[GetNavicatVerion -> GetFileVersionInfo]\r\n"), GetLastError());
  132. delete[] buf;
  133. return FALSE;
  134. }
  135. LPVOID info_ptr;
  136. UINT info_len;
  137. if (FALSE == VerQueryValue(buf, TEXT("\\"), &info_ptr, &info_len)) {
  138. _tprintf_s(TEXT("Failed to get navicat.exe verion info. CODE: 0x%08x @[GetNavicatVerion -> VerQueryValue]\r\n"), GetLastError());
  139. delete[] buf;
  140. return FALSE;
  141. }
  142. *major_ver = static_cast<VS_FIXEDFILEINFO*>(info_ptr)->dwFileVersionMS;
  143. *minor_ver = static_cast<VS_FIXEDFILEINFO*>(info_ptr)->dwFileVersionLS;
  144. delete[] buf;
  145. return TRUE;
  146. }
  147. BOOL Check_libcc_Hash(LPCTSTR libcc_dll_path, const uint8_t expected_hash[SHA256_DIGEST_LENGTH]) {
  148. HANDLE hFile = CreateFile(libcc_dll_path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  149. if (hFile == INVALID_HANDLE_VALUE) {
  150. _tprintf_s(TEXT("Failed to open libcc.dll. CODE: 0x%08x @[Check_libcc_Hash -> CreateFile]\r\n"), GetLastError());
  151. return FALSE;
  152. }
  153. LARGE_INTEGER FileSize;
  154. if (FALSE == GetFileSizeEx(hFile, &FileSize)) {
  155. _tprintf_s(TEXT("Failed to get libcc.dll size. CODE: 0x%08x @[Check_libcc_Hash -> GetFileSizeEx]\r\n"), GetLastError());
  156. CloseHandle(hFile);
  157. return FALSE;
  158. }
  159. HANDLE hFileMap = CreateFileMapping(hFile, nullptr, PAGE_READONLY, 0, 0, nullptr);
  160. if (hFileMap == NULL) {
  161. _tprintf_s(TEXT("Failed to create mapping for libcc.dll. CODE: 0x%08x @[Check_libcc_Hash -> CreateFileMapping]\r\n"), GetLastError());
  162. CloseHandle(hFile);
  163. return FALSE;
  164. }
  165. const uint8_t* libcc_data = reinterpret_cast<const uint8_t*>(MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0));
  166. if (libcc_data == nullptr) {
  167. _tprintf_s(TEXT("Failed to map libcc.dll. CODE: 0x%08x @[Check_libcc_Hash -> MapViewOfFile]\r\n"), GetLastError());
  168. CloseHandle(hFileMap);
  169. CloseHandle(hFile);
  170. return FALSE;
  171. }
  172. uint8_t real_hash[SHA256_DIGEST_LENGTH] = { };
  173. SHA256(libcc_data, FileSize.LowPart, real_hash);
  174. UnmapViewOfFile(libcc_data);
  175. CloseHandle(hFileMap);
  176. CloseHandle(hFile);
  177. if (memcmp(expected_hash, real_hash, SHA256_DIGEST_LENGTH) != 0) {
  178. _tprintf_s(TEXT("ERROR: SHA256 do not match.\r\n"));
  179. return FALSE;
  180. }
  181. return TRUE;
  182. }
  183. char* GetPEMText(RSA* PrivateKey) {
  184. BIO* bio = BIO_new(BIO_s_mem());
  185. if (bio == nullptr) {
  186. _tprintf_s(TEXT("Cannot create BIO object. CODE: 0x%08x @[GetPEMText -> BIO_new]\r\n"), ERR_get_error());
  187. return nullptr;
  188. }
  189. if (1 != PEM_write_bio_RSA_PUBKEY(bio, PrivateKey)) {
  190. _tprintf_s(TEXT("Cannot write RSA-2048 public key. CODE: 0x%08x @[GetPEMText -> PEM_write_bio_RSA_PUBKEY]\r\n"), ERR_get_error());
  191. BIO_free_all(bio);
  192. return nullptr;
  193. }
  194. char* pem_data_ptr;
  195. int pem_data_len = BIO_get_mem_data(bio, &pem_data_ptr);
  196. char* ret = new char[pem_data_len + 256]();
  197. for (int i = 0, j = 0; i < pem_data_len; ++i, ++j) {
  198. if (pem_data_ptr[i] == '\n') {
  199. ret[j++] = '\r';
  200. ret[j] = pem_data_ptr[i];
  201. } else {
  202. ret[j] = pem_data_ptr[i];
  203. }
  204. }
  205. BIO_free_all(bio);
  206. return ret;
  207. }
  208. }