OCT 1. 更新查询用户信息接口

2. 协议加密秘钥异常时默认BASE64加密
This commit is contained in:
huangxin 2022-12-03 18:00:55 +08:00
parent 1c0e72bf85
commit 0433658d72
3 changed files with 38 additions and 22 deletions

View File

@ -86,7 +86,7 @@ application:
# 3采用3DES加密后的base64编码格式 # 3采用3DES加密后的base64编码格式
# 4采用AES256加密后的base64编码格式 # 4采用AES256加密后的base64编码格式
crypto_type = 0; crypto_type = 0;
crypto_key = "AES@YD1X+lI3U75l36yUsOUugw=="; crypto_key = "AES@rX2qZWVwGVlYTJLf/6X22w==";
}; };
# DHCP Server Config # DHCP Server Config

View File

@ -42,6 +42,8 @@ const char *proto_create_new(cJSON *pMsgCtx, int rspCode) {
if (pMsgCtx == NULL) { if (pMsgCtx == NULL) {
pro.msgContend = cJSON_CreateObject(); pro.msgContend = cJSON_CreateObject();
} else {
pro.msgContend = pMsgCtx;
} }
switch (pro.cryptoType) { switch (pro.cryptoType) {
@ -53,40 +55,51 @@ const char *proto_create_new(cJSON *pMsgCtx, int rspCode) {
const char *base64 = base64_encode((unsigned char *)pStrMsg, strlen(pStrMsg)); const char *base64 = base64_encode((unsigned char *)pStrMsg, strlen(pStrMsg));
cJSON_AddStringToObject(pRoot, "msgContent", base64); cJSON_AddStringToObject(pRoot, "msgContent", base64);
free((void *)base64); free((void *)base64);
cJSON_free(pro.msgContend);
} break; } break;
case CRYPTO_AES128: case CRYPTO_AES128:
case CRYPTO_AES256: case CRYPTO_AES256:
case CRYPTO_3DES: { case CRYPTO_3DES: {
int cryptoType, ret; int cryptoType;
const char *base64; const char *pKey = config_get_proto_crypto_key();
unsigned char *buf; const char *base64;
int outSize = 0; const char *pStrMsg = cJSON_Print(pro.msgContend);
const char *pStrMsg = cJSON_Print(pro.msgContend);
const char *pKey = config_get_proto_crypto_key();
if (pro.cryptoType == CRYPTO_AES128) { if (pro.cryptoType == CRYPTO_AES128) {
cryptoType = DES3_ECB_PKCS7PADDING; cryptoType = AES128_ECB_PKCS7PADDING;
} else if (pro.cryptoType == CRYPTO_AES256) { } else if (pro.cryptoType == CRYPTO_AES256) {
cryptoType = AES256_ECB_PKCS7PADDING; cryptoType = AES256_ECB_PKCS7PADDING;
} else { } else {
cryptoType = AES128_ECB_PKCS7PADDING; cryptoType = DES3_ECB_PKCS7PADDING;
} }
ret = symmetric_encrypto(cryptoType, (unsigned char *)pStrMsg, strlen(pStrMsg), &buf, &outSize, pKey); if (pKey == NULL || strlen(pKey) == 0) {
dzlog_error("Cryptography key empty of algorithm %d, Used default algorithm BASE64\n", cryptoType);
if (ret != ERR_SUCCESS) {
dzlog_error("Unsupported protocol crypto : %d, Used default algorithm BASE64\n", cryptoType);
base64 = base64_encode((unsigned char *)pStrMsg, strlen(pStrMsg)); base64 = base64_encode((unsigned char *)pStrMsg, strlen(pStrMsg));
pro.cryptoType = CRYPTO_BASE64; pro.cryptoType = CRYPTO_BASE64;
} else { } else {
base64 = base64_encode((unsigned char *)buf, outSize); int ret;
unsigned char *buf;
int outSize = 0;
ret = symmetric_encrypto(cryptoType, (unsigned char *)pStrMsg, strlen(pStrMsg), &buf, &outSize, pKey);
if (ret != ERR_SUCCESS) {
dzlog_error("Unsupported protocol crypto : %d, Used default algorithm BASE64\n", cryptoType);
base64 = base64_encode((unsigned char *)pStrMsg, strlen(pStrMsg));
pro.cryptoType = CRYPTO_BASE64;
} else {
base64 = base64_encode((unsigned char *)buf, outSize);
}
} }
cJSON_AddStringToObject(pRoot, "msgContent", base64); cJSON_AddStringToObject(pRoot, "msgContent", base64);
free((void *)base64); free((void *)base64);
cJSON_free(pro.msgContend);
} break; } break;
default: default:
dzlog_error("Unsupported protocol crypto algorithms: %d, Used default algorithm BASE64\n", pro.cryptoType); dzlog_error("Unsupported protocol crypto algorithms: %d, Used default algorithm BASE64\n", pro.cryptoType);
cJSON_free(pro.msgContend);
cJSON_Delete(pRoot); cJSON_Delete(pRoot);
return NULL; return NULL;
} }

View File

@ -21,6 +21,7 @@ using namespace std;
#include <libconfig.h> #include <libconfig.h>
#include <zlog.h> #include <zlog.h>
#include "config.h" #include "config.h"
#include "proto.h"
extern data2 cfig; extern data2 cfig;
extern bool kRunning; extern bool kRunning;
@ -68,10 +69,10 @@ static void sendUserList(data19 *req, const char *pRequest) {
return; return;
} }
cJSON *pRspRoot = cJSON_CreateObject(); // cJSON *pRspRoot = cJSON_CreateObject();
cJSON_AddNumberToObject(pRspRoot, "version", 3); // cJSON_AddNumberToObject(pRspRoot, "version", 3);
cJSON_AddNumberToObject(pRspRoot, "cryptoType", 0); // cJSON_AddNumberToObject(pRspRoot, "cryptoType", 0);
cJSON_AddNumberToObject(pRspRoot, "timeStamp", (unsigned int)time(nullptr)); // cJSON_AddNumberToObject(pRspRoot, "timeStamp", (unsigned int)time(nullptr));
cJSON *pRspMsg = cJSON_CreateObject(); cJSON *pRspMsg = cJSON_CreateObject();
cJSON *pMsgArray = cJSON_CreateArray(); cJSON *pMsgArray = cJSON_CreateArray();
@ -106,16 +107,18 @@ static void sendUserList(data19 *req, const char *pRequest) {
cJSON_AddItemToArray(pMsgArray, pRspItem); cJSON_AddItemToArray(pMsgArray, pRspItem);
} }
cJSON_AddItemToObject(pRspRoot, "msgContent", pRspMsg); const char *pStrPro = proto_create_new(pRspMsg, 200);
//cJSON_AddItemToObject(pRspRoot, "msgContent", pRspMsg);
char *fp = req->dp; char *fp = req->dp;
//char *maxData = req->dp + (req->memSize - 512); //char *maxData = req->dp + (req->memSize - 512);
//fp += sprintf(fp, send200, strlen(rspBuf)); //fp += sprintf(fp, send200, strlen(rspBuf));
fp += sprintf(fp, "%s", cJSON_Print(pRspRoot)); fp += sprintf(fp, "%s", pStrPro);
cJSON_Delete(pRoot); cJSON_Delete(pRoot);
cJSON_Delete(pRspRoot); //cJSON_Delete(pRspMsg);
req->bytes = (int)(fp - req->dp); req->bytes = (int)(fp - req->dp);
} }
@ -498,7 +501,7 @@ static void opendhcp_http_get_userinfo(http_request *request, hw_http_response *
memset(req, 0, sizeof(struct data19)); memset(req, 0, sizeof(struct data19));
SETSTRING(content_type_name, "Content-Type"); SETSTRING(content_type_name, "Content-Type");
SETSTRING(content_type_value, "text/html"); SETSTRING(content_type_value, "application/json");
hw_set_response_header(response, &content_type_name, &content_type_value); hw_set_response_header(response, &content_type_name, &content_type_value);
SETSTRING(status_code, HTTP_STATUS_200); SETSTRING(status_code, HTTP_STATUS_200);