GenerateLicense.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "exception.hpp"
  2. #include "exceptions/operation_canceled_exception.hpp"
  3. #include "exceptions/win32_exception.hpp"
  4. #include "resource_wrapper.hpp"
  5. #include "resource_traits/win32/file_handle.hpp"
  6. #include "cp_converter.hpp"
  7. #include "base64_rfc4648.hpp"
  8. #include "navicat_serial_generator.hpp"
  9. #include "rsa_cipher.hpp"
  10. #include <iostream>
  11. #include <ctime>
  12. #include <rapidjson/document.h>
  13. #include <rapidjson/writer.h>
  14. #include <rapidjson/stringbuffer.h>
  15. #define NKG_CURRENT_SOURCE_FILE() u8".\\navicat-keygen\\GenerateLicense.cpp"
  16. #define NKG_CURRENT_SOURCE_LINE() __LINE__
  17. namespace nkg {
  18. void GenerateLicenseText(const rsa_cipher& cipher, const navicat_serial_generator& sn_generator) {
  19. std::wstring username;
  20. std::wstring organization;
  21. std::string u8_username;
  22. std::string u8_organization;
  23. std::wstring b64_request_code;
  24. std::vector<std::uint8_t> request_code;
  25. std::string u8_request_info;
  26. std::string u8_response_info;
  27. std::vector<std::uint8_t> response_code;
  28. std::wstring b64_response_code;
  29. std::wcout << L"[*] Your name: ";
  30. if (!std::getline(std::wcin, username)) {
  31. throw exceptions::operation_canceled_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"Operation is canceled by user.");
  32. } else {
  33. u8_username = cp_converter<-1, CP_UTF8>::convert(username);
  34. }
  35. std::wcout << L"[*] Your organization: ";
  36. if (!std::getline(std::wcin, organization)) {
  37. throw exceptions::operation_canceled_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"Operation is canceled by user.");
  38. } else {
  39. u8_organization = cp_converter<-1, CP_UTF8>::convert(organization);
  40. }
  41. std::wcout << std::endl;
  42. std::wcout << L"[*] Input request code in Base64: (Input empty line to end)" << std::endl;
  43. while (true) {
  44. std::wstring s;
  45. if (!std::getline(std::wcin, s)) {
  46. throw exceptions::operation_canceled_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"Operation is canceled by user.");
  47. }
  48. if (s.empty()) {
  49. break;
  50. }
  51. b64_request_code.append(s);
  52. }
  53. if (b64_request_code.empty()) {
  54. throw exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"Nothing inputs, abort!");
  55. }
  56. request_code = base64_rfc4648::decode(cp_converter<-1, CP_UTF8>::convert(b64_request_code));
  57. u8_request_info.resize((cipher.bits() + 7) / 8);
  58. u8_request_info.resize(cipher.private_decrypt(request_code.data(), request_code.size(), u8_request_info.data(), RSA_PKCS1_PADDING));
  59. while (u8_request_info.back() == '\x00') {
  60. u8_request_info.pop_back();
  61. }
  62. std::wcout << L"[*] Request Info:" << std::endl;
  63. std::wcout << cp_converter<CP_UTF8, -1>::convert(u8_request_info) << std::endl;
  64. std::wcout << std::endl;
  65. rapidjson::Document json;
  66. rapidjson::Value N_Key;
  67. rapidjson::Value N_Value;
  68. rapidjson::Value O_Key;
  69. rapidjson::Value O_Value;
  70. rapidjson::Value T_Key;
  71. rapidjson::Value T_Value;
  72. rapidjson::StringBuffer buffer;
  73. rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  74. //
  75. // begin to parse
  76. //
  77. json.Parse(u8_request_info.c_str());
  78. //
  79. // remove "Platform" info
  80. //
  81. json.RemoveMember(u8"P");
  82. //
  83. // set "Name" info
  84. //
  85. N_Key.SetString(u8"N", 1);
  86. N_Value.SetString(u8_username.c_str(), static_cast<rapidjson::SizeType>(u8_username.length()));
  87. //
  88. // set "Organization" info
  89. //
  90. O_Key.SetString(u8"O", 1);
  91. O_Value.SetString(u8_organization.c_str(), static_cast<rapidjson::SizeType>(u8_organization.length()));
  92. //
  93. // set "Time" info
  94. //
  95. T_Key.SetString(u8"T", 1);
  96. T_Value.SetUint(static_cast<unsigned int>(std::time(nullptr)));
  97. //
  98. // add "Name", "Organization" and "Time"
  99. //
  100. json.AddMember(N_Key, N_Value, json.GetAllocator());
  101. json.AddMember(O_Key, O_Value, json.GetAllocator());
  102. json.AddMember(T_Key, T_Value, json.GetAllocator());
  103. //
  104. // flush
  105. //
  106. json.Accept(writer);
  107. if (buffer.GetSize() > 240) {
  108. throw exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"Response Info is too long.");
  109. }
  110. u8_response_info.assign(buffer.GetString(), buffer.GetSize());
  111. std::wcout << L"[*] Response Info:" << std::endl;
  112. std::wcout << cp_converter<CP_UTF8, -1>::convert(u8_response_info) << std::endl;
  113. std::wcout << std::endl;
  114. response_code.resize((cipher.bits() + 7) / 8);
  115. response_code.resize(cipher.private_encrypt(u8_response_info.data(), u8_response_info.size(), response_code.data(), RSA_PKCS1_PADDING));
  116. b64_response_code = cp_converter<CP_UTF8, -1>::convert(base64_rfc4648::encode(response_code));
  117. std::wcout << L"[*] Activation Code:" << std::endl;
  118. std::wcout << b64_response_code << std::endl;
  119. std::wcout << std::endl;
  120. }
  121. void GenerateLicenseBinary(const rsa_cipher& cipher, const navicat_serial_generator& sn_generator) {
  122. std::string utf8SerialNumber = sn_generator.serial_number();
  123. std::wstring username;
  124. std::wstring organization;
  125. std::string u8_username;
  126. std::string u8_organization;
  127. std::string u8_response_info;
  128. std::vector<std::uint8_t> response_code;
  129. std::wcout << L"[*] Your name: ";
  130. if (!std::getline(std::wcin, username)) {
  131. throw exceptions::operation_canceled_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"Operation is canceled by user.");
  132. } else {
  133. u8_username = cp_converter<-1, CP_UTF8>::convert(username);
  134. }
  135. std::wcout << L"[*] Your organization: ";
  136. if (!std::getline(std::wcin, organization)) {
  137. throw exceptions::operation_canceled_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"Operation is canceled by user.");
  138. } else {
  139. u8_organization = cp_converter<-1, CP_UTF8>::convert(organization);
  140. }
  141. rapidjson::Document json;
  142. rapidjson::Value N_Key;
  143. rapidjson::Value N_Value;
  144. rapidjson::Value O_Key;
  145. rapidjson::Value O_Value;
  146. rapidjson::Value T_Key;
  147. rapidjson::Value T_Value;
  148. rapidjson::Value K_Key;
  149. rapidjson::Value K_Value;
  150. rapidjson::StringBuffer buffer;
  151. rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  152. json.Parse("{}");
  153. K_Key.SetString("K", 1);
  154. K_Value.SetString(utf8SerialNumber.c_str(), static_cast<rapidjson::SizeType>(utf8SerialNumber.length()));
  155. N_Key.SetString("N", 1);
  156. N_Value.SetString(u8_username.c_str(), static_cast<rapidjson::SizeType>(u8_username.length()));
  157. O_Key.SetString("O", 1);
  158. O_Value.SetString(u8_organization.c_str(), static_cast<rapidjson::SizeType>(u8_organization.length()));
  159. T_Key.SetString("T", 1);
  160. T_Value.SetUint(static_cast<unsigned int>(std::time(nullptr)));
  161. json.AddMember(K_Key, K_Value, json.GetAllocator());
  162. json.AddMember(N_Key, N_Value, json.GetAllocator());
  163. json.AddMember(O_Key, O_Value, json.GetAllocator());
  164. json.AddMember(T_Key, T_Value, json.GetAllocator());
  165. //
  166. // flush
  167. //
  168. json.Accept(writer);
  169. if (buffer.GetSize() > 240) {
  170. throw exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"Response Info is too long.");
  171. }
  172. u8_response_info.assign(buffer.GetString(), buffer.GetSize());
  173. std::wcout << L"[*] Response Info:" << std::endl;
  174. std::wcout << cp_converter<CP_UTF8, -1>::convert(u8_response_info) << std::endl;
  175. std::wcout << std::endl;
  176. response_code.resize((cipher.bits() + 7) / 8);
  177. response_code.resize(cipher.private_encrypt(u8_response_info.data(), u8_response_info.size(), response_code.data(), RSA_PKCS1_PADDING));
  178. resource_wrapper license_file{ resource_traits::win32::file_handle{}, CreateFileW(L"license_file", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) };
  179. if (license_file.is_valid() == false) {
  180. throw exceptions::win32_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), GetLastError(), u8"CreateFileW failed.");
  181. }
  182. if (DWORD _; WriteFile(license_file.get(), response_code.data(), static_cast<DWORD>(response_code.size()), &_, NULL) == FALSE) {
  183. throw exceptions::win32_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), GetLastError(), u8"WriteFile failed.");
  184. }
  185. std::wcout << L"[+] license_file has been generated." << std::endl;
  186. }
  187. }
  188. #undef NKG_CURRENT_SOURCE_FILE
  189. #undef NKG_CURRENT_SOURCE_LINE