Solution0.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "def.hpp"
  2. // Solution0 is for navicat premium of which the version < 12.0.25
  3. namespace patcher::Solution0 {
  4. BOOL Do(LPCTSTR navicat_exe_path, LPCTSTR prepared_key_file) {
  5. if (!BackupFile(navicat_exe_path))
  6. return FALSE;
  7. RSA* PrivateKey = nullptr;
  8. if (prepared_key_file == nullptr) {
  9. PrivateKey = GenerateRSAKey();
  10. if (PrivateKey == nullptr)
  11. return FALSE;
  12. if (!WriteRSAPrivateKeyToFile(TEXT("RegPrivateKey.pem"), PrivateKey)) {
  13. RSA_free(PrivateKey);
  14. return FALSE;
  15. }
  16. } else {
  17. PrivateKey = ReadRSAPrivateKeyFromFile(prepared_key_file);
  18. if (PrivateKey == nullptr)
  19. return FALSE;
  20. }
  21. char* pem_pubkey = GetPEMText(PrivateKey);
  22. if (pem_pubkey == nullptr)
  23. return FALSE;
  24. RSA_free(PrivateKey); // we do not need it anymore
  25. HANDLE hUpdater = BeginUpdateResource(navicat_exe_path, FALSE);
  26. if (hUpdater == NULL) {
  27. _tprintf_s(TEXT("Cannot modify navicat.exe. CODE: 0x%08x @[patcher::Solution0::Do -> BeginUpdateResource]\r\n"), GetLastError());
  28. delete[] pem_pubkey;
  29. return FALSE;
  30. }
  31. if (!UpdateResource(hUpdater,
  32. RT_RCDATA,
  33. TEXT("ACTIVATIONPUBKEY"),
  34. MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
  35. pem_pubkey, strlen(pem_pubkey))) {
  36. _tprintf_s(TEXT("Cannot replace public key. CODE: 0x%08x @[patcher::Solution0::Do -> UpdateResource]\r\n"), GetLastError());
  37. EndUpdateResource(hUpdater, TRUE);
  38. delete[] pem_pubkey;
  39. return FALSE;
  40. } else {
  41. _tprintf_s(TEXT("@[patcher::Solution0::Do]: Public key has been replaced by:\r\n%hs"), pem_pubkey);
  42. EndUpdateResource(hUpdater, FALSE);
  43. delete[] pem_pubkey;
  44. return TRUE;
  45. }
  46. }
  47. }