123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- // RVCComm.cpp : Defines the entry point for the DLL application.
- //
- #include "RVCComm.h"
- #include "ClientComm.h"
- //#include "ServerComm.h"
- #include <assert.h>
- #include "rsa.h"
- #include "md5.h"
- //#include <applink.c>
- #include "pem.h"
- #include <err.h>
- #include <winpr/locale.h>
- CSecureClientBase::CSecureClientBase()
- {
- m_nRefCount = 1; // Entity持有, CClientComm生命期由CSecureClientBase控制,故持有不占用计数
- m_pClientComm = new CClientComm(this);
- }
- bool InitSocket()
- {
- //Initialize Winsock
- WSADATA wsaData;
- int nResult = WSAStartup(MAKEWORD(2,2), &wsaData);
- return nResult ==0;
- }
- void UninitSocket()
- {
- WSACleanup();
- }
- #ifdef _WIN32
- BOOL APIENTRY DllMain( HANDLE hModule,
- DWORD ul_reason_for_call,
- LPVOID lpReserved
- )
- {
- bool bRet(true);
- switch (ul_reason_for_call)
- {
- case DLL_PROCESS_ATTACH:
- OpenSSL_add_all_ciphers();
- OpenSSL_add_all_digests();
- ERR_load_crypto_strings();
- bRet = InitSocket();
- break;
- case DLL_THREAD_ATTACH:
- break;
- case DLL_THREAD_DETACH:
- break;
- case DLL_PROCESS_DETACH:
- UninitSocket();
- break;
- }
- return bRet ? TRUE : FALSE;
- }
- #endif
- // 客户端通过此函数获得通讯类实例
- /*
- RVCCOMM_API IClientCommFunc* GetClientComm(CSecureClientBase *pCallback)
- {
- assert(pCallback != NULL);
- if (pCallback == NULL)
- return NULL;
- pCallback->m_pClientComm = new CClientComm(pCallback);
- return pCallback->m_pClientComm;
- }
- // 服务端通过此函数获得通讯类实例
- RVCCOMM_API CServerCommBase* GetServerComm(CServerCallback *pCallback)
- {
- assert(pCallback != NULL);
- if (pCallback == NULL)
- return NULL;
- pCallback->m_pServerComm = new CServerComm(pCallback);
- return pCallback->m_pServerComm;
- }*/
- string GetSysErrorMsg(int nErrorCode)
- {
- char pMsg[1024];
- memset(pMsg, 0, sizeof(pMsg));
- if(::FormatMessage(
- FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- nErrorCode,
- MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
- pMsg,
- sizeof(pMsg)/sizeof(char),
- NULL))
- {
- pMsg[sizeof(pMsg)/sizeof(char)-1]=0;
- int nLen = strlen(pMsg);
- if (pMsg[nLen-1] == '\n' && pMsg[nLen-2] == '\r')
- pMsg[nLen-2] = 0;
- }
- return pMsg;
- }
- bool CreateRsaKeyPair(BYTE *pPubKeyBuf, int *pPubKeyBufLen, BYTE *pPriKeyBuf, int *pPriKeyBufLen)
- {
- if (*pPubKeyBufLen < 140 || *pPriKeyBufLen <608)
- return false;
- memset(pPubKeyBuf, 0, *pPubKeyBufLen);
- memset(pPriKeyBuf, 0, *pPriKeyBufLen);
- RSA *rsa = RSA_generate_key(1024, RSA_F4, NULL, NULL);
- if (rsa == NULL)
- return false;
- *pPubKeyBufLen = i2d_RSAPublicKey(rsa, &pPubKeyBuf);
- *pPriKeyBufLen = i2d_RSAPrivateKey(rsa, &pPriKeyBuf);
- RSA_free(rsa);
- return (*pPubKeyBufLen >0 && *pPriKeyBufLen >0);
- }
- // 使用RSA公钥加密
- bool EncWithRsaPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, RSA* rsa_pub)
- {
- // 公钥加密
- assert(*pDestLen >= nSourceLen);
- BYTE *pFrom = pSource;
- BYTE *pEnd = pSource + nSourceLen;
- int nTotalLen = 0;
- while(pFrom < pEnd)
- {
- if (*pDestLen - nTotalLen < 128)
- {
- RSA_free(rsa_pub);
- return false;
- }
-
- int nBlockLen = (pEnd - pFrom) <= 117 ? (pEnd - pFrom) : 117;
- int nRet = RSA_public_encrypt(nBlockLen, pFrom, pDest + nTotalLen, rsa_pub, RSA_PKCS1_PADDING);
- if (nRet <= 0)
- {
- RSA_free(rsa_pub);
- return false;
- }
- pFrom += nBlockLen;
- nTotalLen += nRet;
- }
- *pDestLen = nTotalLen;
- RSA_free(rsa_pub);
- return true;
- }
- // 使用RSA公钥加密
- bool EncWithRsaPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, BYTE *pRsaPubKey, int nKeyLen)
- {
- // 还原RSA公钥
- const BYTE *p = pRsaPubKey;
- RSA *rsa_pub = d2i_RSAPublicKey(NULL, &p, nKeyLen);
- if (rsa_pub == NULL)
- return false;
- return EncWithRsaPubKey(pSource, nSourceLen, pDest, pDestLen, rsa_pub);
- }
- // 使用RSA私钥加密
- bool EncWithRsaPriKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, RSA *rsa_pri)
- {
- // 私钥加密
- assert(*pDestLen >= nSourceLen);
- BYTE *pFrom = pSource;
- BYTE *pEnd = pSource + nSourceLen;
- int nTotalLen = 0;
- while(pFrom < pEnd)
- {
- if (*pDestLen - nTotalLen < 128)
- {
- RSA_free(rsa_pri);
- return false;
- }
-
- int nBlockLen = (pEnd - pFrom) <= 117 ? (pEnd - pFrom) : 117;
- int nRet = RSA_private_encrypt(nBlockLen, pFrom, pDest + nTotalLen, rsa_pri, RSA_PKCS1_PADDING);
- if (nRet <= 0)
- {
- RSA_free(rsa_pri);
- return false;
- }
- pFrom += nBlockLen;
- nTotalLen += nRet;
- }
- *pDestLen = nTotalLen;
- RSA_free(rsa_pri);
- return true;
- }
- // 使用RSA私钥加密
- bool EncWithRsaPriKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, BYTE *pRsaPriKey, int nKeyLen)
- {
- // 还原RSA私钥
- const BYTE *p = pRsaPriKey;
- RSA *rsa_pri = d2i_RSAPrivateKey(NULL, &p, nKeyLen);
- if (rsa_pri == NULL)
- return false;
- return EncWithRsaPriKey(pSource, nSourceLen, pDest, pDestLen, rsa_pri);
- }
- // 使用RSA公钥解密
- bool DecWithRsaPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, RSA *rsa_pub)
- {
- // 公钥解密
- assert(*pDestLen >= nSourceLen);
- assert(nSourceLen % 128 ==0);
- BYTE *pFrom = pSource;
- BYTE *pEnd = pSource + nSourceLen;
- int nTotalLen = 0;
- while(pFrom < pEnd)
- {
- if (*pDestLen - nTotalLen < 128)
- {
- RSA_free(rsa_pub);
- return false;
- }
-
- int nRet = RSA_public_decrypt(128, pFrom, pDest + nTotalLen, rsa_pub, RSA_PKCS1_PADDING);
- if (nRet <= 0)
- {
- RSA_free(rsa_pub);
- return false;
- }
- pFrom += 128;
- nTotalLen += nRet;
- }
- *pDestLen = nTotalLen;
- RSA_free(rsa_pub);
- return true;
- }
- // 使用RSA公钥解密
- bool DecWithRsaPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, BYTE *pRsaPubKey, int nKeyLen)
- {
- // 还原RSA公钥
- const BYTE *p = pRsaPubKey;
- RSA *rsa_pub = d2i_RSAPublicKey(NULL, &p, nKeyLen);
- if (rsa_pub == NULL)
- return false;
- return DecWithRsaPubKey(pSource, nSourceLen, pDest, pDestLen, rsa_pub);
- }
- // 使用RSA私钥解密
- bool DecWithRsaPriKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, RSA *rsa_pri)
- {
- // 私钥解密
- assert(*pDestLen >= nSourceLen);
- assert(nSourceLen % 128 ==0);
- BYTE *pFrom = pSource;
- BYTE *pEnd = pSource + nSourceLen;
- int nTotalLen = 0;
- while(pFrom < pEnd)
- {
- if (*pDestLen - nTotalLen < 128)
- {
- RSA_free(rsa_pri);
- return false;
- }
-
- int nRet = RSA_private_decrypt(128, pFrom, pDest + nTotalLen, rsa_pri, RSA_PKCS1_PADDING);
- if (nRet <= 0)
- {
- RSA_free(rsa_pri);
- return false;
- }
- pFrom += 128;
- nTotalLen += nRet;
- }
- *pDestLen = nTotalLen;
- RSA_free(rsa_pri);
- return true;
- }
- // 使用RSA私钥解密
- bool DecWithRsaPriKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen, BYTE *pRsaPriKey, int nKeyLen)
- {
- // 还原RSA私钥
- const BYTE *p = pRsaPriKey;
- RSA *rsa_pri = d2i_RSAPrivateKey(NULL, &p, nKeyLen);
- if (rsa_pri == NULL)
- return false;
- return DecWithRsaPriKey(pSource, nSourceLen, pDest, pDestLen, rsa_pri);
- }
- bool MD5Hash(BYTE *pData, int nLen, BYTE md5[16])
- {
- MD5_CTX context;
- memset(md5, 0, 16);
-
- MD5_Init(&context);
- MD5_Update(&context, pData, nLen);
- MD5_Final(md5, &context);
- return true;
- }
- RSA *GetServerRSAPubKey()
- {
- const char PublicKeyData[] = "-----BEGIN PUBLIC KEY-----\n"
- "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9dYbXjQM77pdpddBS7bdEMVjT\n"
- "rMfvDyy3v+XEMAXBeh1j4M8670VoblikavFtX/xf9kP+byp3MtnnMqAhP0TkuUWw\n"
- "s8S157RuLaM8571TfHztPet4O23+2wTazpPuU/9ZVHDRxROMOCk2O6UYbEPMyQXv\n"
- "Ue2HA8c1ZRuP4+Q8RQIDAQAB\n"
- "-----END PUBLIC KEY-----";
- BIO *bio = NULL;
- if( (bio=BIO_new_mem_buf((void *)PublicKeyData, sizeof(PublicKeyData))) == NULL)
- return NULL;
- RSA *rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL);
- BIO_free(bio);
- return rsa;
- }
- // 使用服务器公钥解密
- bool DecWithServerPubKey(BYTE *pSource, int nSourceLen, BYTE *pDest, int *pDestLen)
- {
- RSA *pkey = GetServerRSAPubKey();
- if (pkey == NULL)
- return false;
- return DecWithRsaPubKey(pSource, nSourceLen, pDest, pDestLen, pkey);
- }
|