RVCComm.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. // RVCComm.cpp : Defines the entry point for the DLL application.
  2. //
  3. #include "stdafx.h"
  4. #include "RVCComm.h"
  5. #include "CMBSMDLL.h"
  6. #include "ClientComm.h"
  7. #if defined(_MSC_VER)
  8. #include <winsock2.h>
  9. #endif //_MSC_VER
  10. #include <assert.h>
  11. #include "rsa.h"
  12. #include "md5.h"
  13. #include "pem.h"
  14. #include <err.h>
  15. #include "dbgutil.h"
  16. #if defined(_MSC_VER)
  17. #pragma comment(lib, "Ws2_32.lib")
  18. #else
  19. #include <winpr/locale.h>
  20. #include "utils.h"
  21. #endif //_MSC_VER
  22. #define TAG RVCCOMM_TAG("rvccomm")
  23. CSecureClientBase::CSecureClientBase()
  24. {
  25. m_nRefCount = 1; // Entity持有, CClientComm生命期由CSecureClientBase控制,故持有不占用计数
  26. m_pClientComm = new CClientComm(this);
  27. }
  28. bool InitSocket()
  29. {
  30. //Initialize Winsock
  31. WSADATA wsaData;
  32. int nResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  33. return nResult ==0;
  34. }
  35. void UninitSocket()
  36. {
  37. WSACleanup();
  38. }
  39. #ifdef _WIN32
  40. BOOL APIENTRY DllMain( HANDLE hModule,
  41. DWORD ul_reason_for_call,
  42. LPVOID lpReserved
  43. )
  44. {
  45. bool bRet(true);
  46. switch (ul_reason_for_call)
  47. {
  48. case DLL_PROCESS_ATTACH:
  49. OpenSSL_add_all_ciphers();
  50. OpenSSL_add_all_digests();
  51. ERR_load_crypto_strings();
  52. bRet = InitSocket();
  53. break;
  54. case DLL_THREAD_ATTACH:
  55. break;
  56. case DLL_THREAD_DETACH:
  57. break;
  58. case DLL_PROCESS_DETACH:
  59. UninitSocket();
  60. break;
  61. }
  62. return bRet ? TRUE : FALSE;
  63. }
  64. #endif
  65. // 客户端通过此函数获得通讯类实例
  66. /*
  67. RVCCOMM_API IClientCommFunc* GetClientComm(CSecureClientBase *pCallback)
  68. {
  69. TOOLKIT_ASSERT(pCallback != NULL);
  70. if (pCallback == NULL)
  71. return NULL;
  72. pCallback->m_pClientComm = new CClientComm(pCallback);
  73. return pCallback->m_pClientComm;
  74. }
  75. // 服务端通过此函数获得通讯类实例
  76. RVCCOMM_API CServerCommBase* GetServerComm(CServerCallback *pCallback)
  77. {
  78. TOOLKIT_ASSERT(pCallback != NULL);
  79. if (pCallback == NULL)
  80. return NULL;
  81. pCallback->m_pServerComm = new CServerComm(pCallback);
  82. return pCallback->m_pServerComm;
  83. }*/
  84. string GetSysErrorMsg(int nErrorCode)
  85. {
  86. char pMsg[1024];
  87. memset(pMsg, 0, sizeof(pMsg));
  88. if(::FormatMessage(
  89. FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  90. NULL,
  91. nErrorCode,
  92. MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
  93. pMsg,
  94. sizeof(pMsg)/sizeof(char),
  95. NULL))
  96. {
  97. pMsg[sizeof(pMsg)/sizeof(char)-1]=0;
  98. int nLen = strlen(pMsg);
  99. if (pMsg[nLen-1] == '\n' && pMsg[nLen-2] == '\r')
  100. pMsg[nLen-2] = 0;
  101. }
  102. return pMsg;
  103. }
  104. bool CreateRsaKeyPair(BYTE *pPubKeyBuf, int *pPubKeyBufLen, BYTE *pPriKeyBuf, int *pPriKeyBufLen)
  105. {
  106. if (*pPubKeyBufLen < 140 || *pPriKeyBufLen <608)
  107. return false;
  108. memset(pPubKeyBuf, 0, *pPubKeyBufLen);
  109. memset(pPriKeyBuf, 0, *pPriKeyBufLen);
  110. RSA *rsa = RSA_generate_key(1024, RSA_F4, NULL, NULL);
  111. if (rsa == NULL)
  112. return false;
  113. *pPubKeyBufLen = i2d_RSAPublicKey(rsa, &pPubKeyBuf);
  114. *pPriKeyBufLen = i2d_RSAPrivateKey(rsa, &pPriKeyBuf);
  115. RSA_free(rsa);
  116. return (*pPubKeyBufLen >0 && *pPriKeyBufLen >0);
  117. }
  118. // 使用RSA公钥加密
  119. bool EncWithRsaPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, RSA* rsa_pub)
  120. {
  121. // 公钥加密
  122. TOOLKIT_ASSERT(*pDestLen >= nSourceLen);
  123. BYTE *pFrom = pSource;
  124. BYTE *pEnd = pSource + nSourceLen;
  125. int nTotalLen = 0;
  126. while(pFrom < pEnd)
  127. {
  128. if (*pDestLen - nTotalLen < 128)
  129. {
  130. RSA_free(rsa_pub);
  131. return false;
  132. }
  133. int nBlockLen = (pEnd - pFrom) <= 117 ? (pEnd - pFrom) : 117;
  134. int nRet = RSA_public_encrypt(nBlockLen, pFrom, pDest + nTotalLen, rsa_pub, RSA_PKCS1_PADDING);
  135. if (nRet <= 0)
  136. {
  137. RSA_free(rsa_pub);
  138. return false;
  139. }
  140. pFrom += nBlockLen;
  141. nTotalLen += nRet;
  142. }
  143. *pDestLen = nTotalLen;
  144. RSA_free(rsa_pub);
  145. return true;
  146. }
  147. // 使用RSA公钥加密
  148. bool EncWithRsaPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, BYTE *pRsaPubKey, int nKeyLen)
  149. {
  150. // 还原RSA公钥
  151. const BYTE *p = pRsaPubKey;
  152. RSA *rsa_pub = d2i_RSAPublicKey(NULL, &p, nKeyLen);
  153. if (rsa_pub == NULL)
  154. return false;
  155. return EncWithRsaPubKey(pSource, nSourceLen, pDest, pDestLen, rsa_pub);
  156. }
  157. // 使用RSA私钥加密
  158. bool EncWithRsaPriKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, RSA *rsa_pri)
  159. {
  160. // 私钥加密
  161. TOOLKIT_ASSERT(*pDestLen >= nSourceLen);
  162. BYTE *pFrom = pSource;
  163. BYTE *pEnd = pSource + nSourceLen;
  164. int nTotalLen = 0;
  165. while(pFrom < pEnd)
  166. {
  167. if (*pDestLen - nTotalLen < 128)
  168. {
  169. RSA_free(rsa_pri);
  170. return false;
  171. }
  172. int nBlockLen = (pEnd - pFrom) <= 117 ? (pEnd - pFrom) : 117;
  173. int nRet = RSA_private_encrypt(nBlockLen, pFrom, pDest + nTotalLen, rsa_pri, RSA_PKCS1_PADDING);
  174. if (nRet <= 0)
  175. {
  176. RSA_free(rsa_pri);
  177. return false;
  178. }
  179. pFrom += nBlockLen;
  180. nTotalLen += nRet;
  181. }
  182. *pDestLen = nTotalLen;
  183. RSA_free(rsa_pri);
  184. return true;
  185. }
  186. // 使用RSA私钥加密
  187. bool EncWithRsaPriKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, BYTE *pRsaPriKey, int nKeyLen)
  188. {
  189. // 还原RSA私钥
  190. const BYTE *p = pRsaPriKey;
  191. RSA *rsa_pri = d2i_RSAPrivateKey(NULL, &p, nKeyLen);
  192. if (rsa_pri == NULL)
  193. return false;
  194. return EncWithRsaPriKey(pSource, nSourceLen, pDest, pDestLen, rsa_pri);
  195. }
  196. // 使用RSA公钥解密
  197. bool DecWithRsaPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, RSA *rsa_pub)
  198. {
  199. // 公钥解密
  200. TOOLKIT_ASSERT(*pDestLen >= nSourceLen);
  201. TOOLKIT_ASSERT(nSourceLen % 128 ==0);
  202. BYTE *pFrom = pSource;
  203. BYTE *pEnd = pSource + nSourceLen;
  204. int nTotalLen = 0;
  205. while(pFrom < pEnd)
  206. {
  207. if (*pDestLen - nTotalLen < 128)
  208. {
  209. RSA_free(rsa_pub);
  210. return false;
  211. }
  212. int nRet = RSA_public_decrypt(128, pFrom, pDest + nTotalLen, rsa_pub, RSA_PKCS1_PADDING);
  213. if (nRet <= 0)
  214. {
  215. RSA_free(rsa_pub);
  216. return false;
  217. }
  218. pFrom += 128;
  219. nTotalLen += nRet;
  220. }
  221. *pDestLen = nTotalLen;
  222. RSA_free(rsa_pub);
  223. return true;
  224. }
  225. // 使用RSA公钥解密
  226. bool DecWithRsaPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, BYTE *pRsaPubKey, int nKeyLen)
  227. {
  228. // 还原RSA公钥
  229. const BYTE *p = pRsaPubKey;
  230. RSA *rsa_pub = d2i_RSAPublicKey(NULL, &p, nKeyLen);
  231. if (rsa_pub == NULL)
  232. return false;
  233. return DecWithRsaPubKey(pSource, nSourceLen, pDest, pDestLen, rsa_pub);
  234. }
  235. // 使用RSA私钥解密
  236. bool DecWithRsaPriKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, RSA *rsa_pri)
  237. {
  238. // 私钥解密
  239. TOOLKIT_ASSERT(*pDestLen >= nSourceLen);
  240. TOOLKIT_ASSERT(nSourceLen % 128 ==0);
  241. BYTE *pFrom = pSource;
  242. BYTE *pEnd = pSource + nSourceLen;
  243. int nTotalLen = 0;
  244. while(pFrom < pEnd)
  245. {
  246. if (*pDestLen - nTotalLen < 128)
  247. {
  248. RSA_free(rsa_pri);
  249. return false;
  250. }
  251. int nRet = RSA_private_decrypt(128, pFrom, pDest + nTotalLen, rsa_pri, RSA_PKCS1_PADDING);
  252. if (nRet <= 0)
  253. {
  254. RSA_free(rsa_pri);
  255. return false;
  256. }
  257. pFrom += 128;
  258. nTotalLen += nRet;
  259. }
  260. *pDestLen = nTotalLen;
  261. RSA_free(rsa_pri);
  262. return true;
  263. }
  264. // 使用RSA私钥解密
  265. bool DecWithRsaPriKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, BYTE *pRsaPriKey, int nKeyLen)
  266. {
  267. // 还原RSA私钥
  268. const BYTE *p = pRsaPriKey;
  269. RSA *rsa_pri = d2i_RSAPrivateKey(NULL, &p, nKeyLen);
  270. if (rsa_pri == NULL)
  271. return false;
  272. return DecWithRsaPriKey(pSource, nSourceLen, pDest, pDestLen, rsa_pri);
  273. }
  274. bool MD5Hash(BYTE *pData, int nLen, BYTE md5[16])
  275. {
  276. MD5_CTX context;
  277. memset(md5, 0, 16);
  278. MD5_Init(&context);
  279. MD5_Update(&context, pData, nLen);
  280. MD5_Final(md5, &context);
  281. return true;
  282. }
  283. ///*TODO(80374374@3/22/2023): Bcdhex_2_aschex 的移除,涉及到 util.cpp 文件的删除*/
  284. bool SM3Hash(BYTE* pData, int nLen, BYTE hash[32])
  285. {
  286. #if defined(_MSC_VER)
  287. return 0 == CMBSM3Digest((unsigned char*)pData, nLen, hash);
  288. #else
  289. WLog_DBG(TAG, "Enter %s", __FUNCTION__);
  290. unsigned char msg[1024] = { 0 };
  291. //Bcdhex_2_aschex(pData, nLen, msg);
  292. //WLog_DBG(TAG, "SM3Plain:%s", msg);
  293. WLog_DBG(TAG, "to CMBSM3Digest, msglen: %d, %d, %d", nLen, sizeof(unsigned char), sizeof(BYTE));
  294. unsigned char outmsgbcd[33] = { 0 };
  295. int ret = CMBSM3Digest((unsigned char*)pData, nLen, outmsgbcd);
  296. if (ret != 0) {
  297. WLog_ERR(TAG, "CMBSM3Digest failed: %d", ret);
  298. return false;
  299. } else {
  300. unsigned char outmsg[65] = { 0 };
  301. Bcdhex_2_aschex(outmsgbcd, 32, outmsg);
  302. WLog_DBG(TAG, "SM3:%s", outmsg);
  303. memcpy(hash, outmsgbcd, sizeof(BYTE) * 32);
  304. return true;
  305. }
  306. #endif //_MSC_VER
  307. }
  308. char* GetSMVersion()
  309. {
  310. return CMBSMGetVersion();
  311. }
  312. bool CreateSM2KeyPair(BYTE* pPubKeyBuf, int* pPubKeyBufLen, BYTE* pPriKeyBuf, int* pPriKeyBufLen)
  313. {
  314. return 0 == CMBSM2KeyGen(pPubKeyBuf, pPubKeyBufLen, pPriKeyBuf, pPriKeyBufLen);
  315. }
  316. bool EncWithSM2PubKey(BYTE* pSource, int nSourceLen, BYTE* pDest, int* pDestLen, BYTE* pPubKey, int nKeyLen)
  317. {
  318. return 0 == CMBSM2Encrypt(pPubKey, nKeyLen, pSource, nSourceLen, pDest, pDestLen);
  319. }
  320. bool DecWithSM2PriKey(BYTE* pSource, int nSourceLen, BYTE* pDest, int* pDestLen, BYTE* pPriKey, int nKeyLen)
  321. {
  322. return 0 == CMBSM2Decrypt(pPriKey, nKeyLen, pSource, nSourceLen, pDest, pDestLen);
  323. }
  324. bool SM2SignWithSM3(unsigned char* privkey, int privkey_len, unsigned char* msg, int msg_len, unsigned char* signature, int* sig_len)
  325. {
  326. return 0 == CMBSM2SignWithSM3(privkey, privkey_len, msg, msg_len, signature, sig_len);
  327. }
  328. bool SM2VerifyWithSM3(unsigned char* pubkey, int pubkey_len, unsigned char* msg, int msg_len, unsigned char* signature, int sig_len)
  329. {
  330. return 0 == CMBSM2VerifyWithSM3(pubkey, pubkey_len, msg, msg_len, signature, sig_len);
  331. }
  332. void GenerateSM4_ECBkey(std::string keyStr, BYTE key[16])
  333. {
  334. BYTE tempKey[32] = { 0 };
  335. if (keyStr.length() > 0)
  336. {
  337. SM3Hash((BYTE*)keyStr.c_str(), keyStr.length() > 32 ? 32 : keyStr.length(), tempKey);
  338. memcpy(key, tempKey, 16);
  339. }
  340. else
  341. memset(key, 0, 16);
  342. //Aschex_2_bcdhex(tempKey, 32, key);
  343. }
  344. bool subByteArr(BYTE* src, BYTE* dst, int offset, int size)
  345. {
  346. if (nullptr == src || nullptr == dst)
  347. return false;
  348. memcpy(dst, src + offset, size);
  349. return true;
  350. }
  351. RVCCOMM_API bool EncWithSM4_ECB(std::string keyStr, BYTE* input, int length, BYTE* output, int* output_len)
  352. {
  353. unsigned char key[17] = { 0 };
  354. GenerateSM4_ECBkey(keyStr, key);
  355. //printfHEX(key, 16);
  356. return 0 == CMBSM4EncryptWithECB(key, input, length, output, output_len);
  357. }
  358. bool DecWithSM4_ECB(std::string keyStr, BYTE* input, int length, BYTE* output, int* output_len)
  359. {
  360. unsigned char key[17] = { 0 };
  361. GenerateSM4_ECBkey(keyStr, key);
  362. return 0 == CMBSM4DecryptWithECB(key, input, length, output, output_len);
  363. }
  364. bool EncWithSM4_ECB(BYTE key[16], BYTE* input, int length, BYTE* output, int* output_len)
  365. {
  366. return 0 == CMBSM4EncryptWithECB(key, input, length, output, output_len);
  367. }
  368. bool DecWithSM4_ECB(BYTE key[16], BYTE* input, int length, BYTE* output, int* output_len)
  369. {
  370. return 0 == CMBSM4DecryptWithECB(key, input, length, output, output_len);
  371. }
  372. bool SM3File(char* file, BYTE hash[32])
  373. {
  374. if (0 == CMBSM3FileDigest(file, hash))
  375. return true;
  376. else
  377. return false;
  378. }
  379. RSA *GetServerRSAPubKey()
  380. {
  381. const char PublicKeyData[] = "-----BEGIN PUBLIC KEY-----\n"
  382. "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9dYbXjQM77pdpddBS7bdEMVjT\n"
  383. "rMfvDyy3v+XEMAXBeh1j4M8670VoblikavFtX/xf9kP+byp3MtnnMqAhP0TkuUWw\n"
  384. "s8S157RuLaM8571TfHztPet4O23+2wTazpPuU/9ZVHDRxROMOCk2O6UYbEPMyQXv\n"
  385. "Ue2HA8c1ZRuP4+Q8RQIDAQAB\n"
  386. "-----END PUBLIC KEY-----";
  387. BIO *bio = NULL;
  388. if( (bio=BIO_new_mem_buf((void *)PublicKeyData, sizeof(PublicKeyData))) == NULL)
  389. return NULL;
  390. RSA *rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL);
  391. BIO_free(bio);
  392. return rsa;
  393. }
  394. // 使用服务器公钥解密
  395. bool DecWithServerPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen)
  396. {
  397. RSA *pkey = GetServerRSAPubKey();
  398. if (pkey == NULL)
  399. return false;
  400. return DecWithRsaPubKey(pSource, nSourceLen, pDest, pDestLen, pkey);
  401. }