diff --git a/config/vcpe.cfg b/config/vcpe.cfg index d1971ce..67dc4b5 100644 --- a/config/vcpe.cfg +++ b/config/vcpe.cfg @@ -86,7 +86,7 @@ application: # 3:采用3DES加密后的base64编码格式 # 4:采用AES256加密后的base64编码格式 crypto_type = 0; - crypto_key = "AES@YD1X+lI3U75l36yUsOUugw=="; + crypto_key = "AES@rX2qZWVwGVlYTJLf/6X22w=="; }; # DHCP Server Config diff --git a/srcs/libs/protocol/protocol.c b/srcs/libs/protocol/protocol.c index 05f4ef7..28f2133 100644 --- a/srcs/libs/protocol/protocol.c +++ b/srcs/libs/protocol/protocol.c @@ -42,6 +42,8 @@ const char *proto_create_new(cJSON *pMsgCtx, int rspCode) { if (pMsgCtx == NULL) { pro.msgContend = cJSON_CreateObject(); + } else { + pro.msgContend = pMsgCtx; } 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)); cJSON_AddStringToObject(pRoot, "msgContent", base64); free((void *)base64); + cJSON_free(pro.msgContend); } break; case CRYPTO_AES128: case CRYPTO_AES256: case CRYPTO_3DES: { - int cryptoType, ret; - const char *base64; - unsigned char *buf; - int outSize = 0; - const char *pStrMsg = cJSON_Print(pro.msgContend); - const char *pKey = config_get_proto_crypto_key(); + int cryptoType; + const char *pKey = config_get_proto_crypto_key(); + const char *base64; + const char *pStrMsg = cJSON_Print(pro.msgContend); if (pro.cryptoType == CRYPTO_AES128) { - cryptoType = DES3_ECB_PKCS7PADDING; + cryptoType = AES128_ECB_PKCS7PADDING; } else if (pro.cryptoType == CRYPTO_AES256) { cryptoType = AES256_ECB_PKCS7PADDING; } else { - cryptoType = AES128_ECB_PKCS7PADDING; + cryptoType = DES3_ECB_PKCS7PADDING; } - 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); + if (pKey == NULL || strlen(pKey) == 0) { + dzlog_error("Cryptography key empty of algorithm %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); + 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); free((void *)base64); + cJSON_free(pro.msgContend); } break; default: dzlog_error("Unsupported protocol crypto algorithms: %d, Used default algorithm BASE64\n", pro.cryptoType); + cJSON_free(pro.msgContend); cJSON_Delete(pRoot); return NULL; } diff --git a/srcs/open_dhcp/query.cpp b/srcs/open_dhcp/query.cpp index 7e45333..c26b251 100644 --- a/srcs/open_dhcp/query.cpp +++ b/srcs/open_dhcp/query.cpp @@ -21,6 +21,7 @@ using namespace std; #include #include #include "config.h" +#include "proto.h" extern data2 cfig; extern bool kRunning; @@ -68,10 +69,10 @@ static void sendUserList(data19 *req, const char *pRequest) { return; } - cJSON *pRspRoot = cJSON_CreateObject(); - cJSON_AddNumberToObject(pRspRoot, "version", 3); - cJSON_AddNumberToObject(pRspRoot, "cryptoType", 0); - cJSON_AddNumberToObject(pRspRoot, "timeStamp", (unsigned int)time(nullptr)); + // cJSON *pRspRoot = cJSON_CreateObject(); + // cJSON_AddNumberToObject(pRspRoot, "version", 3); + // cJSON_AddNumberToObject(pRspRoot, "cryptoType", 0); + // cJSON_AddNumberToObject(pRspRoot, "timeStamp", (unsigned int)time(nullptr)); cJSON *pRspMsg = cJSON_CreateObject(); cJSON *pMsgArray = cJSON_CreateArray(); @@ -106,16 +107,18 @@ static void sendUserList(data19 *req, const char *pRequest) { 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 *maxData = req->dp + (req->memSize - 512); //fp += sprintf(fp, send200, strlen(rspBuf)); - fp += sprintf(fp, "%s", cJSON_Print(pRspRoot)); + fp += sprintf(fp, "%s", pStrPro); cJSON_Delete(pRoot); - cJSON_Delete(pRspRoot); + //cJSON_Delete(pRspMsg); 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)); 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); SETSTRING(status_code, HTTP_STATUS_200);