419 lines
10 KiB
C++
419 lines
10 KiB
C++
// test_auth.cpp : 定义控制台应用程序的入口点。
|
|
//
|
|
|
|
#include "stdafx.h"
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <sstream>
|
|
#include <openssl/aes.h>
|
|
#include <json/json.h>
|
|
#include "Request.h"
|
|
using namespace std;
|
|
|
|
struct Mac_Key
|
|
{
|
|
string _Mac;
|
|
string _MacKey;
|
|
};
|
|
|
|
struct AuthorizeConfigs
|
|
{
|
|
string _Type; //1:Local without Mac 2:Local with Macs 3:Network
|
|
string _LimitTime;
|
|
string _PackageName;
|
|
string _AuthorizeKey;
|
|
string _ToolKey; //5th line of Key file.(When _Type "2")
|
|
vector<Mac_Key> _MacKeys; //6th-End lines of Key file.(When _Type "2")
|
|
string _ActivateUrl; //5th line of Key file.(When _Type "3")
|
|
string _ActivateLocalCipher; //6th line of Key file.(When _Type "3")
|
|
};
|
|
|
|
int GetAuthorizeConfigs(string filename, AuthorizeConfigs & configs)
|
|
{
|
|
int ires = 0;
|
|
AES_KEY aes_key;
|
|
long long last, lastlen = 0;
|
|
unsigned char key[16] = { 0 };
|
|
unsigned char inbuf[16] = { 0 };
|
|
unsigned char outbuf[16] = { 0 };
|
|
unsigned char szHeader[256] = { 0 };
|
|
|
|
FILE *pFile = nullptr;
|
|
fopen_s(&pFile, filename.c_str(), "rb");
|
|
if (!pFile)
|
|
{
|
|
ires = 1;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
return ires;
|
|
}
|
|
|
|
fseek(pFile, 0, SEEK_END); //point the file end
|
|
int len = ftell(pFile);
|
|
rewind(pFile);
|
|
|
|
////count the decrpt times of file
|
|
unsigned long count = (len - 256) / 16;
|
|
if (count <= 0)
|
|
{
|
|
fclose(pFile);
|
|
ires = 2;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
return ires;
|
|
}
|
|
//get key from file
|
|
|
|
if (0 == fread(szHeader, 1, 256, pFile))
|
|
{
|
|
fclose(pFile);
|
|
ires = 2;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
return ires;
|
|
}
|
|
|
|
memcpy_s(key, 16, szHeader + 100, 16);
|
|
|
|
//start to decrpt file
|
|
if (AES_set_decrypt_key(key, 128, &aes_key) == 0)//set decrypt key
|
|
{
|
|
//computer the file size before encrpt
|
|
memcpy_s(inbuf, 16, szHeader, 16);
|
|
AES_decrypt(inbuf, outbuf, &aes_key);
|
|
memcpy_s(&lastlen, sizeof(unsigned long), outbuf, sizeof(unsigned long));
|
|
last = lastlen % 16; //the part less 16 bytes before encprt
|
|
//check file if the file is able to be decrpted or not
|
|
bool ret = last == 0 ? lastlen == len - 256 : lastlen + 16 - last == len - 256;
|
|
if (!ret)
|
|
{
|
|
fclose(pFile);
|
|
ires = 3;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
return ires;
|
|
}
|
|
|
|
//decrpt file contents
|
|
std::stringstream tmpStram;
|
|
for (unsigned long k = 0; k < count; k++)
|
|
{
|
|
memset(inbuf, 0x0, 16);
|
|
memset(outbuf, 0x0, 16);
|
|
//SecureZeroMemory(inbuf, 16);
|
|
//SecureZeroMemory(outbuf, 16);
|
|
|
|
if (0 < fread(inbuf, 1, 16, pFile))//read 16 bytes from file
|
|
{
|
|
AES_decrypt(inbuf, outbuf, &aes_key);//decrpt 16 bytes
|
|
if (k == count - 1)
|
|
{
|
|
tmpStram.write((char *)outbuf, last == 0 ? 16 : last);
|
|
}
|
|
else
|
|
{
|
|
tmpStram.write((char *)outbuf, 16);
|
|
}
|
|
}
|
|
}
|
|
|
|
//Get authorize configs
|
|
{
|
|
const int LINE_LENGTH = 256;
|
|
char lineBuf[LINE_LENGTH];
|
|
tmpStram.getline(lineBuf, LINE_LENGTH);
|
|
//Get file Type
|
|
string tmpType(lineBuf);
|
|
{
|
|
if (tmpType[tmpType.length() - 1] == '\n')
|
|
{
|
|
tmpType[tmpType.length() - 1] = '\0';
|
|
}
|
|
if (tmpType.length() < 1)
|
|
{
|
|
fclose(pFile);
|
|
ires = 5;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
return ires;
|
|
}
|
|
if (tmpType != string("1") && tmpType != string("2") && tmpType != string("3"))
|
|
{
|
|
fclose(pFile);
|
|
ires = 5;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
return ires;
|
|
}
|
|
configs._Type = tmpType;
|
|
}
|
|
//Get limit Time
|
|
tmpStram.getline(lineBuf, LINE_LENGTH);
|
|
string tmpTime(lineBuf);
|
|
{
|
|
if (tmpTime[tmpTime.length() - 1] == '\n')
|
|
{
|
|
tmpTime[tmpTime.length() - 1] = '\0';
|
|
}
|
|
if (tmpTime.length() < 1)
|
|
{
|
|
fclose(pFile);
|
|
ires = 6;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
return ires;
|
|
}
|
|
configs._LimitTime = tmpTime;
|
|
}
|
|
//Get package name
|
|
tmpStram.getline(lineBuf, LINE_LENGTH);
|
|
string tmpPackage(lineBuf);
|
|
{
|
|
if (tmpPackage[tmpPackage.length() - 1] == '\n')
|
|
{
|
|
tmpPackage[tmpPackage.length() - 1] = '\0';
|
|
}
|
|
if (tmpPackage.length() < 1)
|
|
{
|
|
fclose(pFile);
|
|
ires = 7;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
return ires;
|
|
}
|
|
configs._PackageName = tmpPackage;
|
|
}
|
|
//Get authorize Key
|
|
tmpStram.getline(lineBuf, LINE_LENGTH);
|
|
string tmpAuthorKey(lineBuf);
|
|
{
|
|
if (tmpAuthorKey[tmpAuthorKey.length() - 1] == '\n')
|
|
{
|
|
tmpAuthorKey[tmpAuthorKey.length() - 1] = '\0';
|
|
}
|
|
if (tmpAuthorKey.length() < 1)
|
|
{
|
|
fclose(pFile);
|
|
ires = 8;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
return ires;
|
|
}
|
|
configs._AuthorizeKey = tmpAuthorKey;
|
|
}
|
|
if (tmpType == string("3"))
|
|
{
|
|
//Get activate Url
|
|
tmpStram.getline(lineBuf, LINE_LENGTH);
|
|
string tmpActivateUrl(lineBuf);
|
|
{
|
|
if (tmpActivateUrl[tmpActivateUrl.length() - 1] == '\n')
|
|
{
|
|
tmpActivateUrl[tmpActivateUrl.length() - 1] = '\0';
|
|
}
|
|
if (tmpActivateUrl.length() < 1)
|
|
{
|
|
fclose(pFile);
|
|
ires = 9;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
return ires;
|
|
}
|
|
configs._ActivateUrl = tmpActivateUrl;
|
|
}
|
|
//Get activate local cipher
|
|
tmpStram.getline(lineBuf, LINE_LENGTH);
|
|
string tmpLocalCipher(lineBuf);
|
|
{
|
|
if (tmpLocalCipher[tmpLocalCipher.length() - 1] == '\n')
|
|
{
|
|
tmpLocalCipher[tmpLocalCipher.length() - 1] = '\0';
|
|
}
|
|
if (tmpLocalCipher.length() < 1)
|
|
{
|
|
fclose(pFile);
|
|
ires = 10;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
return ires;
|
|
}
|
|
configs._ActivateLocalCipher = tmpLocalCipher;
|
|
}
|
|
}
|
|
else if (tmpType == string("2"))
|
|
{
|
|
//Get ToolKey
|
|
tmpStram.getline(lineBuf, LINE_LENGTH);
|
|
string tmpToolKey(lineBuf);
|
|
{
|
|
if (tmpToolKey[tmpToolKey.length() - 1] == '\n')
|
|
{
|
|
tmpToolKey[tmpToolKey.length() - 1] = '\0';
|
|
}
|
|
if (tmpToolKey.length() < 1)
|
|
{
|
|
fclose(pFile);
|
|
ires = 11;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
return ires;
|
|
}
|
|
configs._ToolKey = tmpToolKey;
|
|
}
|
|
|
|
while (tmpStram.getline(lineBuf, LINE_LENGTH))
|
|
{
|
|
string tmpMacKey(lineBuf);
|
|
if (tmpMacKey[tmpMacKey.length() - 1] == '\n')
|
|
{
|
|
tmpMacKey[tmpMacKey.length() - 1] = '\0';
|
|
}
|
|
if (tmpMacKey.length() < 1)
|
|
{
|
|
continue;
|
|
}
|
|
if (tmpMacKey.length() != 45)
|
|
{
|
|
fclose(pFile);
|
|
ires = 12;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
return ires;
|
|
}
|
|
Mac_Key tmpMac_Key;
|
|
tmpMac_Key._Mac = tmpMacKey.substr(0, 12);
|
|
tmpMac_Key._MacKey = tmpMacKey.substr(13, 32);
|
|
configs._MacKeys.push_back(tmpMac_Key);
|
|
}
|
|
if (configs._MacKeys.size() < 1)
|
|
{
|
|
fclose(pFile);
|
|
ires = 13;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
return ires;
|
|
}
|
|
}
|
|
|
|
fclose(pFile);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ires = 4;
|
|
std::cout << "GetAuthorizeConfigs Failed , ErrCode:" << ires << " ." << std::endl;
|
|
fclose(pFile);
|
|
}
|
|
return ires;
|
|
}
|
|
|
|
bool Auth(AuthorizeConfigs& configs)
|
|
{
|
|
bool bCheckOK = false;
|
|
//if (!CheckAuthority())
|
|
//{
|
|
// std::cout << "CheckAuthority False ..." << std::endl;
|
|
// bCheckOK = false;
|
|
//}
|
|
//else
|
|
//{
|
|
// bCheckOK = true;
|
|
//}
|
|
|
|
|
|
if (configs._Type == string("3"))//网络验证
|
|
{
|
|
std::string uuid = "06E3D42B59D72D8968311044173B959F";
|
|
string tempStr = configs._AuthorizeKey + uuid + configs._PackageName;
|
|
|
|
string encryptStr;
|
|
if (!Encrypt_AES_CBC(configs._ActivateLocalCipher, tempStr, encryptStr))
|
|
{
|
|
//LOGGER_ERROR("AES encrypt Str:" << tempStr.c_str() << " Failed ...");
|
|
return 0;
|
|
}
|
|
|
|
Json::Value Jdata;
|
|
Jdata["token"] = encryptStr;
|
|
std::string data = Jdata.toStyledString();
|
|
cout << data << endl;
|
|
std::string files("");
|
|
|
|
std::string out;
|
|
int retval = Request().post(configs._ActivateUrl, data, files, out, 5);
|
|
//{"msg": "c82126ee467bfc53940191147b5cb648b2b48f698ddea28450705b95670c36d98181521637888f0a4d78e59dc4f38fb45c50ecd252060ae28f41ea93b8c73d18", "code": 1000}
|
|
|
|
std::cout << out << std::endl;
|
|
|
|
//
|
|
Json::Reader reader;
|
|
Json::Value resValue;
|
|
if (!reader.parse(out, resValue))
|
|
{
|
|
//LOGGER_DEBUG("Parse result string failed ...");
|
|
return false;
|
|
}
|
|
|
|
if (!resValue.isMember("code"))
|
|
{
|
|
//LOGGER_DEBUG("resStr format error ...");
|
|
return false;
|
|
}
|
|
|
|
int result = resValue["code"].asInt();
|
|
if (result != 1000)
|
|
{
|
|
//LOGGER_DEBUG("resStr.code != 1000");
|
|
return false;
|
|
}
|
|
string msg = resValue["msg"].asString();
|
|
string DeCryptStr;
|
|
//char chUuid[64] = { 0 };
|
|
//GetUUID(chUuid);
|
|
string tempUUID(uuid);
|
|
if (!Decrypt_AES_CBC(tempUUID, msg, DeCryptStr))
|
|
{
|
|
//LOGGER_ERROR("Decrypt_AES_CBC Msg[" << msg.c_str() << "] Failed");
|
|
return false;
|
|
}
|
|
|
|
if (-1 == DeCryptStr.find(configs._AuthorizeKey))
|
|
{
|
|
//LOGGER_ERROR("Reply msg Error 1!");
|
|
return false;
|
|
}
|
|
|
|
}
|
|
else if (configs._Type == string("2"))//Local Mac address authorizing
|
|
{
|
|
vector<string> tmpMacS;
|
|
//GetMacAddressByAdapters(tmpMacS);
|
|
bool bVerifyMacOK = false;
|
|
for (int i = 0; i < configs._MacKeys.size(); i++)
|
|
{
|
|
for (int j = 0; j < tmpMacS.size(); j++)
|
|
{
|
|
if (configs._MacKeys[i]._Mac == tmpMacS[j])
|
|
{
|
|
bVerifyMacOK = true;
|
|
break;
|
|
}
|
|
}
|
|
if (bVerifyMacOK)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
|
|
}
|
|
|
|
//if (0 != AuthorizingKeySave(configs._LimitTime))
|
|
//{
|
|
// std::cout << "AuthorizingKeySave Failed ..." << std::endl;
|
|
// return false;
|
|
//}
|
|
|
|
bCheckOK = true;
|
|
|
|
return bCheckOK;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
AuthorizeConfigs configs;
|
|
GetAuthorizeConfigs(R"(D:\KeyFiles\KeyFiles\deepcam.20201204092201.0.XLhdL3QzhBpOy4xHTcqm.Key)",configs);
|
|
Auth(configs);
|
|
return 0;
|
|
}
|
|
|