2025-03-12 11:08:50 +08:00
|
|
|
|
/**
|
|
|
|
|
|
@file DogAuthImpl.cpp
|
|
|
|
|
|
@brief 加密狗认证接口类的实现文件
|
|
|
|
|
|
@author 周正龙
|
|
|
|
|
|
*/
|
|
|
|
|
|
#include "DogAuthImpl.h"
|
|
|
|
|
|
#include "Common.h"
|
|
|
|
|
|
#include "public/pub_logger_api/logger.h"
|
|
|
|
|
|
#include "pub_utility_api/CommonConfigParse.h"
|
|
|
|
|
|
#include "pub_utility_api/FileUtil.h"
|
|
|
|
|
|
#include <QString>
|
|
|
|
|
|
#include <QDate>
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef DISABLE_DOG_AUTH
|
|
|
|
|
|
#ifdef KBD_X86
|
|
|
|
|
|
#include "sys_dog_auth_api/vendor_code.h"
|
|
|
|
|
|
#endif
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define AUTH_MODEL_LICENCE 1 //licence模式
|
|
|
|
|
|
#define AUTH_MODEL_SUPPERDOG 2 //硬件狗授权模式
|
|
|
|
|
|
|
|
|
|
|
|
#define AUTH_STATUS_OK 1 //授权状态-成功
|
|
|
|
|
|
#define AUTH_STATUS_EXPIRED 0 //授权状态-过期
|
|
|
|
|
|
#define AUTH_STATUS_FAILD -1 //授权状态-失败
|
|
|
|
|
|
|
|
|
|
|
|
#define MEM_MEMBUFFER_SIZE 128
|
|
|
|
|
|
|
|
|
|
|
|
#define PRODUCT_ID_PLAT 40
|
|
|
|
|
|
#define PRODUCT_ID_ISCS 41
|
|
|
|
|
|
#define PRODUCT_ID_PSCADA 42
|
|
|
|
|
|
#define PRODUCT_ID_EMS 43
|
|
|
|
|
|
#define PRODUCT_ID_PSMS 44
|
|
|
|
|
|
|
2025-03-12 14:17:01 +08:00
|
|
|
|
iot_sys::CDogAuthImpl::CDogAuthImpl()
|
2025-03-12 11:08:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
#ifndef DISABLE_DOG_AUTH
|
|
|
|
|
|
m_nProductId = PRODUCT_ID_PSCADA;
|
|
|
|
|
|
m_ptrLicenseAuth = NULL;
|
|
|
|
|
|
m_nAuthModel = AUTH_MODEL_SUPPERDOG;//默认硬件狗授权
|
|
|
|
|
|
#ifdef KBD_X86
|
|
|
|
|
|
m_dogStatus = DOG_NOT_FOUND ;
|
|
|
|
|
|
m_dogHandle = 0 ;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-12 14:17:01 +08:00
|
|
|
|
iot_sys::CDogAuthImpl::~CDogAuthImpl()
|
2025-03-12 11:08:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
#ifndef DISABLE_DOG_AUTH
|
|
|
|
|
|
#ifdef KBD_X86
|
|
|
|
|
|
if (m_dogHandle )
|
|
|
|
|
|
{
|
|
|
|
|
|
dog_logout(m_dogHandle);
|
|
|
|
|
|
m_dogStatus = DOG_NOT_FOUND ;
|
|
|
|
|
|
m_dogHandle = 0 ;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//检查系统授权状态
|
2025-03-12 14:17:01 +08:00
|
|
|
|
int iot_sys::CDogAuthImpl::checkAuthStatus()
|
2025-03-12 11:08:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
if(m_nAuthModel == AUTH_MODEL_LICENCE)
|
|
|
|
|
|
{
|
|
|
|
|
|
return checLicenseAuthStatus();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(m_nAuthModel == AUTH_MODEL_SUPPERDOG)
|
|
|
|
|
|
{
|
|
|
|
|
|
return checkDogAuthStatus();
|
|
|
|
|
|
}
|
|
|
|
|
|
return AUTH_STATUS_FAILD;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
@brief 检查License授权状态
|
|
|
|
|
|
@return 成功返回kbdSucces,失败返回相应错误码
|
|
|
|
|
|
*/
|
2025-03-12 14:17:01 +08:00
|
|
|
|
int iot_sys::CDogAuthImpl::checLicenseAuthStatus()
|
2025-03-12 11:08:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
#ifndef DISABLE_DOG_AUTH
|
|
|
|
|
|
|
|
|
|
|
|
QString strTemp ;
|
|
|
|
|
|
QDate objCurDate = QDate::currentDate(); //当前日期
|
|
|
|
|
|
|
2025-03-12 14:17:01 +08:00
|
|
|
|
std::string strLicensePath = iot_public::CFileUtil::getAbsolutePath( "../../product/common/license/" ) ;
|
2025-03-12 11:08:50 +08:00
|
|
|
|
if(!m_ptrLicenseAuth->checkRegistration(QString::fromStdString(strLicensePath), "license.txt",strTemp) )
|
|
|
|
|
|
{
|
|
|
|
|
|
LOGINFO("授权校验失败,AppId:Time=[%s]!\n",strTemp.toStdString().c_str());
|
|
|
|
|
|
return AUTH_STATUS_FAILD;
|
|
|
|
|
|
}
|
|
|
|
|
|
QStringList listTemp = strTemp.split(":") ;//appName:授权截至时间
|
|
|
|
|
|
if(listTemp.size()<2)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOGINFO("授权license 解析错误 [%s]!\n",strTemp.toStdString().c_str());
|
|
|
|
|
|
return AUTH_STATUS_FAILD;
|
|
|
|
|
|
}
|
|
|
|
|
|
if((objCurDate.daysTo(QDate::fromString(listTemp.at(1),"yyyy-MM-dd")) < 0)
|
|
|
|
|
|
&&(objCurDate.daysTo(QDate::fromString(listTemp.at(1),"yyyy-MM-dd")) >= -90)) //授权截至时间到
|
|
|
|
|
|
{
|
|
|
|
|
|
return AUTH_STATUS_EXPIRED;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(objCurDate.daysTo(QDate::fromString(listTemp.at(1),"yyyy-MM-dd")) < -90) //3个月未授权,停用
|
|
|
|
|
|
{
|
|
|
|
|
|
return AUTH_STATUS_FAILD;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString strAppId = strTemp.split(":").at(0) ;
|
|
|
|
|
|
if(strAppId !="PSCADA")
|
|
|
|
|
|
{
|
|
|
|
|
|
LOGINFO("授权产品ID不匹配,AppId=[%s]!\n",strAppId.toStdString().c_str());
|
|
|
|
|
|
return AUTH_STATUS_FAILD;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
return AUTH_STATUS_OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
@brief 检查超级狗授权状态
|
|
|
|
|
|
@return 成功返回kbdSucces,失败返回相应错误码
|
|
|
|
|
|
*/
|
2025-03-12 14:17:01 +08:00
|
|
|
|
int iot_sys::CDogAuthImpl::checkDogAuthStatus()
|
2025-03-12 11:08:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
#ifndef DISABLE_DOG_AUTH
|
|
|
|
|
|
#ifdef KBD_X86
|
|
|
|
|
|
unsigned char membuffer[MEM_MEMBUFFER_SIZE];
|
|
|
|
|
|
|
|
|
|
|
|
//如果狗状态异常,进行一次登录操作
|
|
|
|
|
|
//====================================================================================
|
|
|
|
|
|
if (DOG_STATUS_OK != m_dogStatus) //读写失败返回错误码
|
|
|
|
|
|
{
|
|
|
|
|
|
loginSupperDog(m_nProductId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//< //狗状态正常:dog_read read from data file
|
|
|
|
|
|
//====================================================================================
|
|
|
|
|
|
m_dogStatus = dog_read(m_dogHandle,DOG_FILEID_RW, /* file ID */0, /* offset */
|
|
|
|
|
|
MEM_MEMBUFFER_SIZE,/* length */&membuffer[0]); /* file data */
|
|
|
|
|
|
switch (m_dogStatus)
|
|
|
|
|
|
{
|
|
|
|
|
|
case DOG_STATUS_OK:
|
|
|
|
|
|
LOGDEBUG("超级狗文件读写状态:OK\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DOG_INV_HND:
|
|
|
|
|
|
LOGDEBUG("超级狗文件读写状态:handle not active\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DOG_INV_FILEID:
|
|
|
|
|
|
LOGDEBUG("超级狗文件读写状态:invalid file id\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DOG_MEM_RANGE:
|
|
|
|
|
|
LOGDEBUG("超级狗文件读写状态:exceeds data file range\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DOG_NOT_FOUND:
|
|
|
|
|
|
LOGDEBUG("超级狗文件读写状态:key/license container not available\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DOG_FEATURE_EXPIRED:
|
|
|
|
|
|
LOGDEBUG("超级狗文件读写状态:Feature has expired \n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
LOGDEBUG("超级狗文件读写状态:read memory failed\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (DOG_STATUS_OK != m_dogStatus) { //读写失败返回错误码
|
|
|
|
|
|
return AUTH_STATUS_FAILD; //
|
|
|
|
|
|
}
|
|
|
|
|
|
#else
|
|
|
|
|
|
return AUTH_STATUS_FAILD;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
return AUTH_STATUS_OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* @brief 初始化 */
|
2025-03-12 14:17:01 +08:00
|
|
|
|
int iot_sys::CDogAuthImpl::initialize()
|
2025-03-12 11:08:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
#ifndef DISABLE_DOG_AUTH
|
|
|
|
|
|
//获取软件产品编号 不同编号对应不同ID
|
|
|
|
|
|
//====================================================================================
|
|
|
|
|
|
|
2025-03-12 14:17:01 +08:00
|
|
|
|
iot_public::CCommonConfigParse objCfgParse;
|
2025-03-12 11:08:50 +08:00
|
|
|
|
//加载配置文件,失败则使用默认值,构造函数中已设置 m_nProductId = PRODUCT_ID_PSCADA
|
|
|
|
|
|
if(kbdSuccess == objCfgParse.load(
|
2025-03-12 14:17:01 +08:00
|
|
|
|
iot_public::CFileUtil::getPathOfCfgFile("setup_config.xml",CN_DIR_PRODUCT)))
|
2025-03-12 11:08:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
//获取产品ID
|
|
|
|
|
|
if(kbdSuccess != objCfgParse.getIntValue("setup_config","software_product_id", m_nProductId))
|
|
|
|
|
|
{
|
|
|
|
|
|
LOGWARN("CDogAuthImpl::initialize() get product id fail, use default.\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
//获取授权模式
|
|
|
|
|
|
if (objCfgParse.getIntValue("setup_config", "authModel", m_nAuthModel) != kbdSuccess)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_nAuthModel = AUTH_MODEL_SUPPERDOG;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
LOGWARN("CDogAuthImpl::initialize() load setup_config.xml failed, use default.\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_ptrLicenseAuth = getLicenseAuthInstance(); //
|
|
|
|
|
|
if(NULL == m_ptrLicenseAuth)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOGERROR("CDogAuthImpl::initialize() getLicenseAuthInstance fail .\n");
|
|
|
|
|
|
return kbdFailed;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
return kbdSuccess;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief :CDogAuthImpl::loginSupperDog()
|
|
|
|
|
|
* 超级狗登录
|
|
|
|
|
|
* @return 成功返回kbdSuccess,失败返回kbdFailed
|
|
|
|
|
|
*/
|
2025-03-12 14:17:01 +08:00
|
|
|
|
int iot_sys::CDogAuthImpl::loginSupperDog(int nProductId)
|
2025-03-12 11:08:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
#ifndef DISABLE_DOG_AUTH
|
|
|
|
|
|
#ifdef KBD_X86
|
|
|
|
|
|
//超级狗登陆状态不正常,退出
|
|
|
|
|
|
//====================================================================================
|
|
|
|
|
|
if (m_dogStatus != DOG_STATUS_OK) //登陆状态不正常
|
|
|
|
|
|
{
|
|
|
|
|
|
//< 重新登陆
|
|
|
|
|
|
LOGDEBUG("初始化加密狗授权信息,产品ID[%d].\n ", nProductId);
|
|
|
|
|
|
m_dogStatus = dog_login(nProductId, (dog_vendor_code_t *)vendorCode, &m_dogHandle);
|
|
|
|
|
|
switch (m_dogStatus)
|
|
|
|
|
|
{
|
|
|
|
|
|
case DOG_STATUS_OK:
|
|
|
|
|
|
LOGDEBUG("超级狗登陆状态:OK\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DOG_FEATURE_NOT_FOUND:
|
|
|
|
|
|
LOGDEBUG("超级狗登陆状态:login to default feature failed\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DOG_NOT_FOUND:
|
|
|
|
|
|
LOGDEBUG("超级狗登陆状态:no SuperDog with vendor code DEMOMA found\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DOG_INV_VCODE:
|
|
|
|
|
|
LOGDEBUG("超级狗登陆状态:invalid vendor code\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DOG_LOCAL_COMM_ERR:
|
|
|
|
|
|
LOGDEBUG("超级狗登陆状态:communication error between API and local SuperDog License Manager\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
LOGDEBUG("超级狗登陆状态:login to default feature failed with status %d\n", m_dogStatus);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (DOG_STATUS_OK != m_dogStatus) //登陆失败,句柄=-1;
|
|
|
|
|
|
{
|
|
|
|
|
|
dog_logout(m_dogHandle);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#else
|
|
|
|
|
|
return kbdFailed;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
#endif
|
|
|
|
|
|
return kbdSuccess;
|
|
|
|
|
|
}
|