123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- // RVCComm.cpp : Defines the entry point for the DLL application.
- //
- #include "stdafx.h"
- #include "RVCComm.h"
- #include "CMBSMDLL.h"
- #include "ClientComm.h"
- #if defined(_MSC_VER)
- #include <winsock2.h>
- #endif //_MSC_VER
- #include <assert.h>
- #include "rsa.h"
- #include "md5.h"
- #include "pem.h"
- #include <err.h>
- #include "dbgutil.h"
- #if defined(_MSC_VER)
- #pragma comment(lib, "Ws2_32.lib")
- #else
- #include <winpr/locale.h>
- #include "utils.h"
- #endif //_MSC_VER
- #define TAG RVCCOMM_TAG("rvccomm")
- 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)
- {
- TOOLKIT_ASSERT(pCallback != NULL);
- if (pCallback == NULL)
- return NULL;
- pCallback->m_pClientComm = new CClientComm(pCallback);
- return pCallback->m_pClientComm;
- }
- // 服务端通过此函数获得通讯类实例
- RVCCOMM_API CServerCommBase* GetServerComm(CServerCallback *pCallback)
- {
- TOOLKIT_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)
- {
- // 公钥加密
- TOOLKIT_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)
- {
- // 私钥加密
- TOOLKIT_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)
- {
- // 公钥解密
- TOOLKIT_ASSERT(*pDestLen >= nSourceLen);
- TOOLKIT_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)
- {
- // 私钥解密
- TOOLKIT_ASSERT(*pDestLen >= nSourceLen);
- TOOLKIT_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;
- }
- ///*TODO(80374374@3/22/2023): Bcdhex_2_aschex 的移除,涉及到 util.cpp 文件的删除*/
- bool SM3Hash(BYTE* pData, int nLen, BYTE hash[32])
- {
- #if defined(_MSC_VER)
- return 0 == CMBSM3Digest((unsigned char*)pData, nLen, hash);
- #else
- WLog_DBG(TAG, "Enter %s", __FUNCTION__);
- unsigned char msg[1024] = { 0 };
- //Bcdhex_2_aschex(pData, nLen, msg);
- //WLog_DBG(TAG, "SM3Plain:%s", msg);
- WLog_DBG(TAG, "to CMBSM3Digest, msglen: %d, %d, %d", nLen, sizeof(unsigned char), sizeof(BYTE));
- unsigned char outmsgbcd[33] = { 0 };
- int ret = CMBSM3Digest((unsigned char*)pData, nLen, outmsgbcd);
- if (ret != 0) {
- WLog_ERR(TAG, "CMBSM3Digest failed: %d", ret);
- return false;
- } else {
- unsigned char outmsg[65] = { 0 };
- Bcdhex_2_aschex(outmsgbcd, 32, outmsg);
- WLog_DBG(TAG, "SM3:%s", outmsg);
- memcpy(hash, outmsgbcd, sizeof(BYTE) * 32);
- return true;
- }
- #endif //_MSC_VER
- }
- char* GetSMVersion()
- {
- return CMBSMGetVersion();
- }
- bool CreateSM2KeyPair(BYTE* pPubKeyBuf, int* pPubKeyBufLen, BYTE* pPriKeyBuf, int* pPriKeyBufLen)
- {
- return 0 == CMBSM2KeyGen(pPubKeyBuf, pPubKeyBufLen, pPriKeyBuf, pPriKeyBufLen);
- }
- bool EncWithSM2PubKey(BYTE* pSource, int nSourceLen, BYTE* pDest, int* pDestLen, BYTE* pPubKey, int nKeyLen)
- {
- return 0 == CMBSM2Encrypt(pPubKey, nKeyLen, pSource, nSourceLen, pDest, pDestLen);
- }
- bool DecWithSM2PriKey(BYTE* pSource, int nSourceLen, BYTE* pDest, int* pDestLen, BYTE* pPriKey, int nKeyLen)
- {
- return 0 == CMBSM2Decrypt(pPriKey, nKeyLen, pSource, nSourceLen, pDest, pDestLen);
- }
- bool SM2SignWithSM3(unsigned char* privkey, int privkey_len, unsigned char* msg, int msg_len, unsigned char* signature, int* sig_len)
- {
- return 0 == CMBSM2SignWithSM3(privkey, privkey_len, msg, msg_len, signature, sig_len);
- }
- bool SM2VerifyWithSM3(unsigned char* pubkey, int pubkey_len, unsigned char* msg, int msg_len, unsigned char* signature, int sig_len)
- {
- return 0 == CMBSM2VerifyWithSM3(pubkey, pubkey_len, msg, msg_len, signature, sig_len);
- }
- void GenerateSM4_ECBkey(std::string keyStr, BYTE key[16])
- {
- BYTE tempKey[32] = { 0 };
- if (keyStr.length() > 0)
- {
- SM3Hash((BYTE*)keyStr.c_str(), keyStr.length() > 32 ? 32 : keyStr.length(), tempKey);
- memcpy(key, tempKey, 16);
- }
- else
- memset(key, 0, 16);
-
- //Aschex_2_bcdhex(tempKey, 32, key);
- }
- bool subByteArr(BYTE* src, BYTE* dst, int offset, int size)
- {
- if (nullptr == src || nullptr == dst)
- return false;
- memcpy(dst, src + offset, size);
- return true;
- }
- RVCCOMM_API bool EncWithSM4_ECB(std::string keyStr, BYTE* input, int length, BYTE* output, int* output_len)
- {
- unsigned char key[17] = { 0 };
- GenerateSM4_ECBkey(keyStr, key);
- //printfHEX(key, 16);
- return 0 == CMBSM4EncryptWithECB(key, input, length, output, output_len);
-
- }
- bool DecWithSM4_ECB(std::string keyStr, BYTE* input, int length, BYTE* output, int* output_len)
- {
- unsigned char key[17] = { 0 };
- GenerateSM4_ECBkey(keyStr, key);
- return 0 == CMBSM4DecryptWithECB(key, input, length, output, output_len);
- }
- bool EncWithSM4_ECB(BYTE key[16], BYTE* input, int length, BYTE* output, int* output_len)
- {
- return 0 == CMBSM4EncryptWithECB(key, input, length, output, output_len);
- }
- bool DecWithSM4_ECB(BYTE key[16], BYTE* input, int length, BYTE* output, int* output_len)
- {
- return 0 == CMBSM4DecryptWithECB(key, input, length, output, output_len);
- }
- bool SM3File(char* file, BYTE hash[32])
- {
- if (0 == CMBSM3FileDigest(file, hash))
- return true;
- else
- return false;
- }
- 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);
- }
|