|
@@ -908,92 +908,89 @@ int Char2Int(char * ch) {
|
|
|
}
|
|
|
return num;
|
|
|
}
|
|
|
-bool CAccessAuthEntity::SaveAuthVerAndKey(int nAuthVer, BYTE *pKey)
|
|
|
+bool CAccessAuthEntity::SaveAuthKey(BYTE *pKey)
|
|
|
{
|
|
|
LOG_FUNCTION();
|
|
|
|
|
|
- m_nAuthVersion = nAuthVer;
|
|
|
+ //m_nAuthVersion = nAuthVer;
|
|
|
memset(m_AuthSessionKey, 0, 140);
|
|
|
|
|
|
- if (m_nAuthVersion == 2)
|
|
|
- {
|
|
|
- CSimpleString runInfoPath, iniPath;
|
|
|
- auto rc = GetFunction()->GetPath("runinfo", runInfoPath);
|
|
|
- if (rc != Error_Succeed) {
|
|
|
- Dbg("GetPath runinfo error=%d.", rc);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- char privateKey[BUF_SIZE] = { 0 };
|
|
|
- iniPath = runInfoPath + SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "AccessAuthorization.ini";
|
|
|
- //runInfoPath += SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "Initializer.ini";
|
|
|
+ CSimpleString runInfoPath, iniPath;
|
|
|
+ auto rc = GetFunction()->GetPath("runinfo", runInfoPath);
|
|
|
+ if (rc != Error_Succeed) {
|
|
|
+ Dbg("GetPath runinfo error=%d.", rc);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ char privateKey[BUF_SIZE] = { 0 };
|
|
|
+ iniPath = runInfoPath + SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "AccessAuthorization.ini";
|
|
|
+ //runInfoPath += SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "Initializer.ini";
|
|
|
|
|
|
#ifdef RVC_OS_WIN
|
|
|
- GetPrivateProfileString("TerminalPD", "PrivateKey", "", privateKey, BUF_SIZE, runInfoPath.GetData());
|
|
|
+ GetPrivateProfileString("TerminalPD", "PrivateKey", "", privateKey, BUF_SIZE, runInfoPath.GetData());
|
|
|
#else
|
|
|
- Dbg("path ex:%s", iniPath.GetData());
|
|
|
- char* tmp = inifile_read_str(iniPath.GetData(),"TerminalPD", "PrivateKey", "");
|
|
|
- strcpy(privateKey, tmp);
|
|
|
- delete tmp;
|
|
|
+ Dbg("path ex:%s", iniPath.GetData());
|
|
|
+ char* tmp = inifile_read_str(iniPath.GetData(), "TerminalPD", "PrivateKey", "");
|
|
|
+ strcpy(privateKey, tmp);
|
|
|
+ delete tmp;
|
|
|
#endif // RVC_OS_WIN
|
|
|
-
|
|
|
- if (strlen(privateKey) <= 0) {
|
|
|
- iniPath = runInfoPath + SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "Initializer.ini";
|
|
|
- char* tmp2 = inifile_read_str(iniPath.GetData(), "TerminalPD", "PrivateKey", "");
|
|
|
- strcpy(privateKey, tmp2);
|
|
|
- delete tmp2;
|
|
|
|
|
|
- if (strlen(privateKey) <= 0)
|
|
|
- {
|
|
|
- printPasswdError();
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
- Dbg("privateKey=%s,%d", privateKey, strlen(privateKey));
|
|
|
-
|
|
|
- int decodedPrivateKeyLen;
|
|
|
+ if (strlen(privateKey) <= 0) {
|
|
|
+ iniPath = runInfoPath + SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "Initializer.ini";
|
|
|
+ char* tmp2 = inifile_read_str(iniPath.GetData(), "TerminalPD", "PrivateKey", "");
|
|
|
+ strcpy(privateKey, tmp2);
|
|
|
+ delete tmp2;
|
|
|
|
|
|
- char* pDecodedPrivateKey = Hex2Str(privateKey, decodedPrivateKeyLen);
|
|
|
- Dbg("decodedPrivateKeyLen=%d", decodedPrivateKeyLen);
|
|
|
-
|
|
|
- char pDecryptPrivateKey[BUF_SIZE] = { 0 };
|
|
|
- int decryprtLen = BUF_SIZE;
|
|
|
- if (!DecWithSM4_ECB("s5da69gnh4!963@6s5da69gnh4!963@6", (BYTE*)pDecodedPrivateKey, decodedPrivateKeyLen, (BYTE*)pDecryptPrivateKey, &decryprtLen)) {
|
|
|
- Dbg("DecWithSM4_ECB decrypt privateKey error.");
|
|
|
+ if (strlen(privateKey) <= 0)
|
|
|
+ {
|
|
|
printPasswdError();
|
|
|
- delete[] pDecodedPrivateKey;
|
|
|
return false;
|
|
|
}
|
|
|
+ }
|
|
|
+ Dbg("privateKey=%s,%d", privateKey, strlen(privateKey));
|
|
|
+
|
|
|
+ int decodedPrivateKeyLen;
|
|
|
+
|
|
|
+ char* pDecodedPrivateKey = Hex2Str(privateKey, decodedPrivateKeyLen);
|
|
|
+ Dbg("decodedPrivateKeyLen=%d", decodedPrivateKeyLen);
|
|
|
+
|
|
|
+ char pDecryptPrivateKey[BUF_SIZE] = { 0 };
|
|
|
+ int decryprtLen = BUF_SIZE;
|
|
|
+ if (!DecWithSM4_ECB("s5da69gnh4!963@6s5da69gnh4!963@6", (BYTE*)pDecodedPrivateKey, decodedPrivateKeyLen, (BYTE*)pDecryptPrivateKey, &decryprtLen)) {
|
|
|
+ Dbg("DecWithSM4_ECB decrypt privateKey error.");
|
|
|
+ printPasswdError();
|
|
|
delete[] pDecodedPrivateKey;
|
|
|
- //添加调试信息
|
|
|
- char * pEncPriKey = Str2Hex((char*)pDecryptPrivateKey, decryprtLen);
|
|
|
- Dbg("DecWithSM4_ECB succeess.privateKey=%s", pEncPriKey);
|
|
|
- delete pEncPriKey;
|
|
|
-
|
|
|
- char pPlainKey[KEY_SIZE];
|
|
|
- int plainKeyLen = KEY_SIZE;
|
|
|
- char pKeyLen[4] = { 0 };
|
|
|
- memcpy(pKeyLen, pKey, 4);
|
|
|
- int kenLen = Char2Int(pKeyLen);
|
|
|
- Dbg("kenLen=%d", kenLen);
|
|
|
- char* pEncodeKey = Str2Hex((char*)pKey,kenLen + 4);
|
|
|
- Dbg("pEncodeKey=%s", pEncodeKey);
|
|
|
- delete pEncodeKey;
|
|
|
- char* key = new char[kenLen + 1];
|
|
|
- memset(key, 0, kenLen + 1);
|
|
|
- memcpy(key, pKey + 4, kenLen);
|
|
|
- if (!DecWithSM2PriKey((BYTE*)key, kenLen, (BYTE*)pPlainKey, &plainKeyLen, (BYTE*)pDecryptPrivateKey, decryprtLen)) {
|
|
|
- Dbg("使用私钥解密失败!");
|
|
|
- printPasswdError();
|
|
|
- return false;
|
|
|
- }
|
|
|
- Dbg("使用私钥解密成功");
|
|
|
- if (plainKeyLen != KEY_SIZE) {
|
|
|
- Dbg("私钥解密后的会话密钥长度不等于16!");
|
|
|
- }
|
|
|
-
|
|
|
- memcpy(m_AuthSessionKey, pPlainKey, KEY_SIZE);
|
|
|
+ return false;
|
|
|
}
|
|
|
+ delete[] pDecodedPrivateKey;
|
|
|
+ //添加调试信息
|
|
|
+ char* pEncPriKey = Str2Hex((char*)pDecryptPrivateKey, decryprtLen);
|
|
|
+ Dbg("DecWithSM4_ECB succeess.privateKey=%s", pEncPriKey);
|
|
|
+ delete pEncPriKey;
|
|
|
+
|
|
|
+ char pPlainKey[KEY_SIZE];
|
|
|
+ int plainKeyLen = KEY_SIZE;
|
|
|
+ char pKeyLen[4] = { 0 };
|
|
|
+ memcpy(pKeyLen, pKey, 4);
|
|
|
+ int kenLen = Char2Int(pKeyLen);
|
|
|
+ Dbg("kenLen=%d", kenLen);
|
|
|
+ char* pEncodeKey = Str2Hex((char*)pKey, kenLen + 4);
|
|
|
+ Dbg("pEncodeKey=%s", pEncodeKey);
|
|
|
+ delete pEncodeKey;
|
|
|
+ char* key = new char[kenLen + 1];
|
|
|
+ memset(key, 0, kenLen + 1);
|
|
|
+ memcpy(key, pKey + 4, kenLen);
|
|
|
+ if (!DecWithSM2PriKey((BYTE*)key, kenLen, (BYTE*)pPlainKey, &plainKeyLen, (BYTE*)pDecryptPrivateKey, decryprtLen)) {
|
|
|
+ Dbg("使用私钥解密失败!");
|
|
|
+ printPasswdError();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Dbg("使用私钥解密成功");
|
|
|
+ if (plainKeyLen != KEY_SIZE) {
|
|
|
+ Dbg("私钥解密后的会话密钥长度不等于16!");
|
|
|
+ }
|
|
|
+
|
|
|
+ memcpy(m_AuthSessionKey, pPlainKey, KEY_SIZE);
|
|
|
|
|
|
return true;
|
|
|
}
|
|
@@ -1026,7 +1023,7 @@ static BYTE* ConvertHexStrToBytes(const char *pszStr)
|
|
|
ErrorCodeEnum CAccessAuthEntity::EncryptDataWithSessionKey(const CBlob &raw, CBlob &enc)
|
|
|
{
|
|
|
LOG_FUNCTION();
|
|
|
- assert(m_nAuthVersion ==2);
|
|
|
+ //assert(m_nAuthVersion ==2);
|
|
|
//这里不需要delete,由CBlob析构函数去执行
|
|
|
BYTE* pEncData = new BYTE[1024];
|
|
|
int pEncDataSize = 1024;
|