#include "Inc/toolkit/Authority.h" #include namespace fs = std::filesystem; #ifdef WIN32 #include RegMgr* RegMgr::m_instance = nullptr; RegMgr* RegMgr::GetInstance() { if (m_instance == nullptr) { m_instance = new RegMgr; } return m_instance; } bool RegMgr::IsKeyExists(HKEY hRootKey, char* strKey) { HKEY hKey; LONG nError = RegOpenKeyExA(hRootKey, strKey, NULL, KEY_ALL_ACCESS, &hKey); if (nError == ERROR_FILE_NOT_FOUND) { return false; } RegCloseKey(hKey); return true; } HKEY RegMgr::OpenKey(HKEY hRootKey, char* strKey) { HKEY hKey; LONG nError = RegOpenKeyExA(hRootKey, strKey, NULL, KEY_ALL_ACCESS, &hKey); if (nError == ERROR_FILE_NOT_FOUND) { //cout << "Creating registry key: " << strKey << endl; nError = RegCreateKeyExA(hRootKey, strKey, NULL, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL); } if (nError) { //cout << "Error: " << nError << " Could not find and create " << strKey << endl; return NULL; } return hKey; } long RegMgr::SetDwordVal(HKEY hKey, char* lpValueName, DWORD dwValue) { LONG nError = RegSetValueExA(hKey, lpValueName, NULL, REG_DWORD, (LPBYTE)&dwValue, sizeof(DWORD)); if (nError) { //cout << "Error: " << nError << " Could not set registry value: " << (char*)lpValueName << endl; return nError; } return ERROR_SUCCESS; } long RegMgr::GetDwordVal(HKEY hKey, char* lpValueName, DWORD& dwVale) { DWORD type = REG_DWORD, size = sizeof(dwVale); LONG nError = RegQueryValueExA(hKey, lpValueName, NULL, &type, (LPBYTE)&dwVale, &size); if (nError == ERROR_FILE_NOT_FOUND) { dwVale = 0; // The value will be created and set to data next time SetVal() is called. return nError; } else if (nError) { //cout << "Error: " << nError << " Could not get registry value " << (char*)lpValueName << endl; return nError; } else if (type != REG_DWORD) { //cout << "Error: " << nError << " Could not get registry value " << (char*)lpValueName << endl; return ERROR_FILE_NOT_FOUND; } return ERROR_SUCCESS; } long RegMgr::SetStrVal(HKEY hKey, char* lpValueName, char * strValue) { LONG nError = RegSetValueExA(hKey, lpValueName, NULL, REG_BINARY, (LPBYTE)strValue, 32); if (nError) { //cout << "Error: " << nError << " Could not set registry value: " << (char*)lpValueName << endl; return nError; } return ERROR_SUCCESS; } long RegMgr::GetStrVal(HKEY hKey, char* lpValueName, char * strValue, DWORD strLen) { if (strValue == NULL || strlen <= 0) { return -1; } DWORD type = REG_BINARY; LONG nError = RegQueryValueExA(hKey, lpValueName, NULL, &type, (LPBYTE)strValue, &strLen); if (nError == ERROR_FILE_NOT_FOUND) { strValue[0] = '\0'; // The value will be created and set to data next time SetVal() is called. return nError; } else if (nError) { strValue[0] = '\0'; //cout << "Error: " << nError << " Could not get registry value " << (char*)lpValueName << endl; return nError; } else if (type != REG_BINARY) { strValue[0] = '\0'; //cout << "Error: " << nError << " Could not get registry value " << (char*)lpValueName << endl; return ERROR_FILE_NOT_FOUND; } return ERROR_SUCCESS; } long RegMgr::SetLastUsedTime() { time_t curTime; time(&curTime); char srcBuf[64] = { 0 }; char encryptBuf[64] = { 0 }; tm _tm; localtime_s(&_tm, &curTime); strftime(srcBuf, sizeof(srcBuf), "%Y-%m-%d %H:%M:%S", &_tm); if (0 == Authority::GetInstance()->EncryptString(srcBuf, encryptBuf)) { HKEY hKey = OpenKey(DC_ROOT_KEY, DC_XD_FRDLL_LASTUSED_TIME); if (hKey == NULL) { return -2; } long lRsult = SetStrVal(hKey, DC_LAST_TIME_VALUE_NAME, encryptBuf); RegCloseKey(hKey); return lRsult; } return -1; } long RegMgr::GetLastUsedTime(time_t & usedTime) { char srcBuf[64] = { 0 }; char decryptBuf[64] = { 0 }; HKEY hKey = OpenKey(DC_ROOT_KEY, DC_XD_FRDLL_LASTUSED_TIME); if (hKey == NULL) { return -2; } long lRsult = GetStrVal(hKey, DC_LAST_TIME_VALUE_NAME, srcBuf, 32); RegCloseKey(hKey); if (lRsult != ERROR_SUCCESS) { return -4; } if (0 == Authority::GetInstance()->DecryptString(srcBuf, decryptBuf)) { usedTime = StringToDatetime(decryptBuf); if (usedTime > 0) { return 0; } } return -1; } long RegMgr::DeleteAllUsedKeys() { HKEY hKey; LONG nError = RegOpenKeyExA(DC_ROOT_KEY, "SOFTWARE", NULL, KEY_ALL_ACCESS, &hKey); if (nError == ERROR_FILE_NOT_FOUND) { return nError; } ::RegDeleteKeyA(hKey, "HighDeepCamSTc"); ::RegDeleteKeyA(hKey, "XdFrInfoTc"); ::RegDeleteKeyA(hKey, "SweepXdTc"); RegCloseKey(hKey); if (!IsKeyExists(DC_ROOT_KEY, DC_HIGH_DEEPCAM_AUTHORITY) && !IsKeyExists(DC_ROOT_KEY, DC_XD_FRDLL_AUTHORITY_TIME) && !IsKeyExists(DC_ROOT_KEY, DC_XD_FRDLL_LASTUSED_TIME)) { return 0; } return -1; } long RegMgr::SetAuthorityKey(char * strAuthKey) { if (strAuthKey == NULL) { return -1; } char encryptBuf[64] = { 0 }; if (0 == Authority::GetInstance()->EncryptString(strAuthKey, encryptBuf)) { HKEY hKey = OpenKey(DC_ROOT_KEY, DC_HIGH_DEEPCAM_AUTHORITY); if (hKey == NULL) { return -2; } long lRsult = SetStrVal(hKey, DC_KEY_VALUE_NAME, encryptBuf); RegCloseKey(hKey); return lRsult; } return -3; } long RegMgr::GetAuthorityKey(char * strAuthKey, DWORD strLen) { if (strAuthKey == NULL || strLen != 64) { return -1; } char dncryptBuf[64] = { 0 }; HKEY hKey = OpenKey(DC_ROOT_KEY, DC_HIGH_DEEPCAM_AUTHORITY); if (hKey == NULL) { return -2; } long lRsult = GetStrVal(hKey, DC_KEY_VALUE_NAME, dncryptBuf, 32); RegCloseKey(hKey); if (lRsult != ERROR_SUCCESS) { return -4; } if (0 == Authority::GetInstance()->DecryptString(dncryptBuf, strAuthKey)) { return 0; } return -3; } long RegMgr::SetAuthorityByTimeStr(char * strAuthTime) { if (strAuthTime == NULL) { return -1; } //OfficialVersion char tmpTimeStr[64] = { 0 }; strncpy_s(tmpTimeStr, 64, DC_OfficialVersion_FLAG, strnlen_s(DC_OfficialVersion_FLAG, 16)); char strUuid[64] = { 0 }; GetUUID(strUuid); strUuid[12] = '\0'; strncat_s(tmpTimeStr, 64, strUuid, strnlen_s(strUuid, 12)); if (0 != strcmp(strAuthTime, tmpTimeStr)) { time_t authTime = StringToDatetime(strAuthTime); time_t tCurTime; time(&tCurTime); if (tCurTime > authTime) { return -1; } } char encryptBuf[64] = { 0 }; if (0 == EncryptString(strAuthTime, encryptBuf)) { HKEY hKey = OpenKey(DC_ROOT_KEY, DC_XD_FRDLL_AUTHORITY_TIME); if (hKey == NULL) { return -2; } long lRsult = SetStrVal(hKey, DC_AUTH_TIME_VALUE_NAME, encryptBuf); RegCloseKey(hKey); return lRsult; } return -1; } long RegMgr::SetAuthorityTime() { time_t curTime; time(&curTime); char srcBuf[64] = { 0 }; char encryptBuf[64] = { 0 }; tm _tm; localtime_s(&_tm, &curTime); strftime(srcBuf, sizeof(srcBuf), "%Y-%m-%d %H:%M:%S", &_tm); return SetAuthorityByTimeStr(srcBuf); } long RegMgr::GetAuthorityTime(time_t & authTime) { char srcBuf[64] = { 0 }; char decryptBuf[64] = { 0 }; HKEY hKey = OpenKey(DC_ROOT_KEY, DC_XD_FRDLL_AUTHORITY_TIME); if (hKey == NULL) { return -2; } long lRsult = GetStrVal(hKey, DC_AUTH_TIME_VALUE_NAME, srcBuf, 32); RegCloseKey(hKey); if (lRsult != ERROR_SUCCESS) { return -4; } if (0 == DecryptString(srcBuf, decryptBuf)) { //OfficialVersion char tmpTimeStr[64] = { 0 }; strncpy_s(tmpTimeStr, 64, DC_OfficialVersion_FLAG, strnlen_s(DC_OfficialVersion_FLAG, 16)); char strUuid[64] = { 0 }; GetUUID(strUuid); strUuid[12] = '\0'; strncat_s(tmpTimeStr, 64, strUuid, strnlen_s(strUuid, 12)); if (0 == strcmp(decryptBuf, tmpTimeStr)) { return DC_OfficialVersion_YEAR; } int year, month, day, hour, minute, second; sscanf_s(decryptBuf, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second); if (year >= DC_OfficialVersion_YEAR) { return DC_OfficialVersion_YEAR; } authTime = StringToDatetime(decryptBuf); if (authTime > 0) { return 0; } //if (1 == sscanf_s(decryptBuf, "%lu-T", &authTime)) //{ // return 0; //} } return -1; } #endif //WIN32 Authority* Authority::m_instance = nullptr; Authority* Authority::GetInstance() { if (m_instance == nullptr) { m_instance = new Authority; } return m_instance; } bool Authority::EnumFilesForConfigs(AuthorizeConfigs & configs) { std::string configPath = GetCurrDir() + std::string("/config"); std::vector suffixFilters; std::string filterJson(".json"), filterKey(".Key"); suffixFilters.push_back(filterJson); suffixFilters.push_back(filterKey); std::vector fileList; } bool Authority::CheckAuthority() { return false; } long Authority::EncryptString(char * strSrc, char * strEncrypted)//Parameters max lenth is 64 bytes { if (strSrc == NULL || strEncrypted == NULL) { return -2; } int ires = -1;//Default error AES_KEY aes_key; if (AES_set_encrypt_key((const unsigned char *)DC_AES_CRYPTCODE, DC_AES_LEN_IN_BITS, &aes_key) == 0)//set encrpt key (32 bytes) { //int len = strnlen_s(strSrc, 64); //int count = len / 16 + (len % 16 == 0 ? 0 : 1); for (int i = 0; i < 2; i++) { AES_encrypt((const unsigned char *)(strSrc + i * 16), (unsigned char *)(strEncrypted + i * 16), &aes_key); } return 0; } return ires; } long Authority::DecryptString(char * strSrc, char * strDecrypted)//Parameters max lenth is 64 bytes { if (strSrc == NULL || strDecrypted == NULL) { return -2; } int ires = -1;//Default error AES_KEY aes_key; if (AES_set_decrypt_key((const unsigned char *)DC_AES_CRYPTCODE, DC_AES_LEN_IN_BITS, &aes_key) == 0)//set encrpt key (32 bytes) { for (int i = 0; i < 2; i++) { AES_decrypt((const unsigned char *)(strSrc + i * 16), (unsigned char *)(strDecrypted + i * 16), &aes_key); } return 0; } return ires; } std::string Authority::GetCurrDir() { return fs::current_path().generic_u8string(); } bool Authority::FindAllFilesInPath(std::string strPath, std::vector & fileList, std::vector & suffixFilters) { fs::path str(strPath); if (!exists(str)) //必须先检测目录是否存在才能使用文件入口. return false; fs::directory_entry entry(str); //文件入口 if (entry.status().type() == fs::file_type::directory) { fs::directory_iterator list(str); //文件入口容器 for (auto& it : list) { //std::cout << it.path().filename() << " <==> " << it.path().stem() << std::endl; } } return 0; }