RVCComm.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // RVCComm.cpp : Defines the entry point for the DLL application.
  2. //
  3. #include "RVCComm.h"
  4. #include "ClientComm.h"
  5. //#include "ServerComm.h"
  6. #include <assert.h>
  7. #include "rsa.h"
  8. #include "md5.h"
  9. //#include <applink.c>
  10. #include "pem.h"
  11. #include <err.h>
  12. #include <winpr/locale.h>
  13. CSecureClientBase::CSecureClientBase()
  14. {
  15. m_nRefCount = 1; // Entity持有, CClientComm生命期由CSecureClientBase控制,故持有不占用计数
  16. m_pClientComm = new CClientComm(this);
  17. }
  18. bool InitSocket()
  19. {
  20. //Initialize Winsock
  21. WSADATA wsaData;
  22. int nResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  23. return nResult ==0;
  24. }
  25. void UninitSocket()
  26. {
  27. WSACleanup();
  28. }
  29. #ifdef _WIN32
  30. BOOL APIENTRY DllMain( HANDLE hModule,
  31. DWORD ul_reason_for_call,
  32. LPVOID lpReserved
  33. )
  34. {
  35. bool bRet(true);
  36. switch (ul_reason_for_call)
  37. {
  38. case DLL_PROCESS_ATTACH:
  39. OpenSSL_add_all_ciphers();
  40. OpenSSL_add_all_digests();
  41. ERR_load_crypto_strings();
  42. bRet = InitSocket();
  43. break;
  44. case DLL_THREAD_ATTACH:
  45. break;
  46. case DLL_THREAD_DETACH:
  47. break;
  48. case DLL_PROCESS_DETACH:
  49. UninitSocket();
  50. break;
  51. }
  52. return bRet ? TRUE : FALSE;
  53. }
  54. #endif
  55. // 客户端通过此函数获得通讯类实例
  56. /*
  57. RVCCOMM_API IClientCommFunc* GetClientComm(CSecureClientBase *pCallback)
  58. {
  59. assert(pCallback != NULL);
  60. if (pCallback == NULL)
  61. return NULL;
  62. pCallback->m_pClientComm = new CClientComm(pCallback);
  63. return pCallback->m_pClientComm;
  64. }
  65. // 服务端通过此函数获得通讯类实例
  66. RVCCOMM_API CServerCommBase* GetServerComm(CServerCallback *pCallback)
  67. {
  68. assert(pCallback != NULL);
  69. if (pCallback == NULL)
  70. return NULL;
  71. pCallback->m_pServerComm = new CServerComm(pCallback);
  72. return pCallback->m_pServerComm;
  73. }*/
  74. string GetSysErrorMsg(int nErrorCode)
  75. {
  76. char pMsg[1024];
  77. memset(pMsg, 0, sizeof(pMsg));
  78. if(::FormatMessage(
  79. FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  80. NULL,
  81. nErrorCode,
  82. MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
  83. pMsg,
  84. sizeof(pMsg)/sizeof(char),
  85. NULL))
  86. {
  87. pMsg[sizeof(pMsg)/sizeof(char)-1]=0;
  88. int nLen = strlen(pMsg);
  89. if (pMsg[nLen-1] == '\n' && pMsg[nLen-2] == '\r')
  90. pMsg[nLen-2] = 0;
  91. }
  92. return pMsg;
  93. }
  94. bool CreateRsaKeyPair(BYTE *pPubKeyBuf, int *pPubKeyBufLen, BYTE *pPriKeyBuf, int *pPriKeyBufLen)
  95. {
  96. if (*pPubKeyBufLen < 140 || *pPriKeyBufLen <608)
  97. return false;
  98. memset(pPubKeyBuf, 0, *pPubKeyBufLen);
  99. memset(pPriKeyBuf, 0, *pPriKeyBufLen);
  100. RSA *rsa = RSA_generate_key(1024, RSA_F4, NULL, NULL);
  101. if (rsa == NULL)
  102. return false;
  103. *pPubKeyBufLen = i2d_RSAPublicKey(rsa, &pPubKeyBuf);
  104. *pPriKeyBufLen = i2d_RSAPrivateKey(rsa, &pPriKeyBuf);
  105. RSA_free(rsa);
  106. return (*pPubKeyBufLen >0 && *pPriKeyBufLen >0);
  107. }
  108. // 使用RSA公钥加密
  109. bool EncWithRsaPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, RSA* rsa_pub)
  110. {
  111. // 公钥加密
  112. assert(*pDestLen >= nSourceLen);
  113. BYTE *pFrom = pSource;
  114. BYTE *pEnd = pSource + nSourceLen;
  115. int nTotalLen = 0;
  116. while(pFrom < pEnd)
  117. {
  118. if (*pDestLen - nTotalLen < 128)
  119. {
  120. RSA_free(rsa_pub);
  121. return false;
  122. }
  123. int nBlockLen = (pEnd - pFrom) <= 117 ? (pEnd - pFrom) : 117;
  124. int nRet = RSA_public_encrypt(nBlockLen, pFrom, pDest + nTotalLen, rsa_pub, RSA_PKCS1_PADDING);
  125. if (nRet <= 0)
  126. {
  127. RSA_free(rsa_pub);
  128. return false;
  129. }
  130. pFrom += nBlockLen;
  131. nTotalLen += nRet;
  132. }
  133. *pDestLen = nTotalLen;
  134. RSA_free(rsa_pub);
  135. return true;
  136. }
  137. // 使用RSA公钥加密
  138. bool EncWithRsaPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, BYTE *pRsaPubKey, int nKeyLen)
  139. {
  140. // 还原RSA公钥
  141. const BYTE *p = pRsaPubKey;
  142. RSA *rsa_pub = d2i_RSAPublicKey(NULL, &p, nKeyLen);
  143. if (rsa_pub == NULL)
  144. return false;
  145. return EncWithRsaPubKey(pSource, nSourceLen, pDest, pDestLen, rsa_pub);
  146. }
  147. // 使用RSA私钥加密
  148. bool EncWithRsaPriKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, RSA *rsa_pri)
  149. {
  150. // 私钥加密
  151. assert(*pDestLen >= nSourceLen);
  152. BYTE *pFrom = pSource;
  153. BYTE *pEnd = pSource + nSourceLen;
  154. int nTotalLen = 0;
  155. while(pFrom < pEnd)
  156. {
  157. if (*pDestLen - nTotalLen < 128)
  158. {
  159. RSA_free(rsa_pri);
  160. return false;
  161. }
  162. int nBlockLen = (pEnd - pFrom) <= 117 ? (pEnd - pFrom) : 117;
  163. int nRet = RSA_private_encrypt(nBlockLen, pFrom, pDest + nTotalLen, rsa_pri, RSA_PKCS1_PADDING);
  164. if (nRet <= 0)
  165. {
  166. RSA_free(rsa_pri);
  167. return false;
  168. }
  169. pFrom += nBlockLen;
  170. nTotalLen += nRet;
  171. }
  172. *pDestLen = nTotalLen;
  173. RSA_free(rsa_pri);
  174. return true;
  175. }
  176. // 使用RSA私钥加密
  177. bool EncWithRsaPriKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, BYTE *pRsaPriKey, int nKeyLen)
  178. {
  179. // 还原RSA私钥
  180. const BYTE *p = pRsaPriKey;
  181. RSA *rsa_pri = d2i_RSAPrivateKey(NULL, &p, nKeyLen);
  182. if (rsa_pri == NULL)
  183. return false;
  184. return EncWithRsaPriKey(pSource, nSourceLen, pDest, pDestLen, rsa_pri);
  185. }
  186. // 使用RSA公钥解密
  187. bool DecWithRsaPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, RSA *rsa_pub)
  188. {
  189. // 公钥解密
  190. assert(*pDestLen >= nSourceLen);
  191. assert(nSourceLen % 128 ==0);
  192. BYTE *pFrom = pSource;
  193. BYTE *pEnd = pSource + nSourceLen;
  194. int nTotalLen = 0;
  195. while(pFrom < pEnd)
  196. {
  197. if (*pDestLen - nTotalLen < 128)
  198. {
  199. RSA_free(rsa_pub);
  200. return false;
  201. }
  202. int nRet = RSA_public_decrypt(128, pFrom, pDest + nTotalLen, rsa_pub, RSA_PKCS1_PADDING);
  203. if (nRet <= 0)
  204. {
  205. RSA_free(rsa_pub);
  206. return false;
  207. }
  208. pFrom += 128;
  209. nTotalLen += nRet;
  210. }
  211. *pDestLen = nTotalLen;
  212. RSA_free(rsa_pub);
  213. return true;
  214. }
  215. // 使用RSA公钥解密
  216. bool DecWithRsaPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, BYTE *pRsaPubKey, int nKeyLen)
  217. {
  218. // 还原RSA公钥
  219. const BYTE *p = pRsaPubKey;
  220. RSA *rsa_pub = d2i_RSAPublicKey(NULL, &p, nKeyLen);
  221. if (rsa_pub == NULL)
  222. return false;
  223. return DecWithRsaPubKey(pSource, nSourceLen, pDest, pDestLen, rsa_pub);
  224. }
  225. // 使用RSA私钥解密
  226. bool DecWithRsaPriKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, RSA *rsa_pri)
  227. {
  228. // 私钥解密
  229. assert(*pDestLen >= nSourceLen);
  230. assert(nSourceLen % 128 ==0);
  231. BYTE *pFrom = pSource;
  232. BYTE *pEnd = pSource + nSourceLen;
  233. int nTotalLen = 0;
  234. while(pFrom < pEnd)
  235. {
  236. if (*pDestLen - nTotalLen < 128)
  237. {
  238. RSA_free(rsa_pri);
  239. return false;
  240. }
  241. int nRet = RSA_private_decrypt(128, pFrom, pDest + nTotalLen, rsa_pri, RSA_PKCS1_PADDING);
  242. if (nRet <= 0)
  243. {
  244. RSA_free(rsa_pri);
  245. return false;
  246. }
  247. pFrom += 128;
  248. nTotalLen += nRet;
  249. }
  250. *pDestLen = nTotalLen;
  251. RSA_free(rsa_pri);
  252. return true;
  253. }
  254. // 使用RSA私钥解密
  255. bool DecWithRsaPriKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, BYTE *pRsaPriKey, int nKeyLen)
  256. {
  257. // 还原RSA私钥
  258. const BYTE *p = pRsaPriKey;
  259. RSA *rsa_pri = d2i_RSAPrivateKey(NULL, &p, nKeyLen);
  260. if (rsa_pri == NULL)
  261. return false;
  262. return DecWithRsaPriKey(pSource, nSourceLen, pDest, pDestLen, rsa_pri);
  263. }
  264. bool MD5Hash(BYTE *pData, int nLen, BYTE md5[16])
  265. {
  266. MD5_CTX context;
  267. memset(md5, 0, 16);
  268. MD5_Init(&context);
  269. MD5_Update(&context, pData, nLen);
  270. MD5_Final(md5, &context);
  271. return true;
  272. }
  273. RSA *GetServerRSAPubKey()
  274. {
  275. const char PublicKeyData[] = "-----BEGIN PUBLIC KEY-----\n"
  276. "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9dYbXjQM77pdpddBS7bdEMVjT\n"
  277. "rMfvDyy3v+XEMAXBeh1j4M8670VoblikavFtX/xf9kP+byp3MtnnMqAhP0TkuUWw\n"
  278. "s8S157RuLaM8571TfHztPet4O23+2wTazpPuU/9ZVHDRxROMOCk2O6UYbEPMyQXv\n"
  279. "Ue2HA8c1ZRuP4+Q8RQIDAQAB\n"
  280. "-----END PUBLIC KEY-----";
  281. BIO *bio = NULL;
  282. if( (bio=BIO_new_mem_buf((void *)PublicKeyData, sizeof(PublicKeyData))) == NULL)
  283. return NULL;
  284. RSA *rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL);
  285. BIO_free(bio);
  286. return rsa;
  287. }
  288. // 使用服务器公钥解密
  289. bool DecWithServerPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen)
  290. {
  291. RSA *pkey = GetServerRSAPubKey();
  292. if (pkey == NULL)
  293. return false;
  294. return DecWithRsaPubKey(pSource, nSourceLen, pDest, pDestLen, pkey);
  295. }