Commit 32fe0911 by zhanggf4

ssl_api OTA and Smartconfig.

parent 6a38409e
......@@ -136,7 +136,6 @@ int IOTDev_GetAttribute(UINT32 attribute_id, OCTData *attr)
return E_SUCCESS;
}
int iOta = 0;
int IOTDev_ExecuteAction(UINT32 action_id, OCTData *param)
{
UINT32 attribute_id[1];
......@@ -153,9 +152,6 @@ int IOTDev_ExecuteAction(UINT32 action_id, OCTData *param)
log_debug0("zcf execute action(%08x) onoff=%d\n", (unsigned int)action_id, (int)(param->value.Integer.Integer));
printf("##########execute action %d\n", param->value.Integer.Integer);
attribute_id[0] = GARDGET_PLUG_ATTRIBUTE_ONOFF;
if ((iOta ++) > 6) {
IOTOTA_Start("1");
}
break;
case GARDGET_PLUG_ACTION_OTA_CHECK:
log_debug0("execute action(%08x) ota check\n", action_id);
......
......@@ -402,6 +402,23 @@ int IOTSysP_ParamSave(void *param,int size)
return E_SUCCESS;
}
int IOTSysP_GetMemParam(void *mem, int size)
{
return E_FAILED;
}
int IOTSysP_SetMemParam(void *mem, int size)
{
#if 0
if(system_rtc_mem_write(64,mem,size) != 0)
{
return E_SUCCESS;
}
#endif
return E_FAILED;
}
static int IOTSys_Check_Flash_Size(void)
{
return E_SUCCESS;
......
#ifndef __OPENSSL_CERT_H__
#define __OPENSSL_CERT_H__
#if 0
const unsigned char ca_crt[] = {
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x43,
0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d,
0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x58, 0x6a, 0x43, 0x43,
0x41, 0x6b, 0x59, 0x43, 0x43, 0x51, 0x43, 0x42, 0x38, 0x6c, 0x72, 0x2b,
0x43, 0x54, 0x51, 0x34, 0x75, 0x44, 0x41, 0x4e, 0x42, 0x67, 0x6b, 0x71,
0x68, 0x6b, 0x69, 0x47, 0x39, 0x77, 0x30, 0x42, 0x41, 0x51, 0x55, 0x46,
0x41, 0x44, 0x42, 0x78, 0x4d, 0x51, 0x73, 0x77, 0x43, 0x51, 0x59, 0x44,
0x56, 0x51, 0x51, 0x47, 0x45, 0x77, 0x4a, 0x44, 0x0a, 0x4d, 0x54, 0x45,
0x4d, 0x4d, 0x41, 0x6f, 0x47, 0x41, 0x31, 0x55, 0x45, 0x43, 0x41, 0x77,
0x44, 0x53, 0x6c, 0x4d, 0x78, 0x4d, 0x51, 0x77, 0x77, 0x43, 0x67, 0x59,
0x44, 0x56, 0x51, 0x51, 0x48, 0x44, 0x41, 0x4e, 0x58, 0x57, 0x44, 0x45,
0x78, 0x44, 0x54, 0x41, 0x4c, 0x42, 0x67, 0x4e, 0x56, 0x42, 0x41, 0x6f,
0x4d, 0x42, 0x45, 0x56, 0x54, 0x55, 0x44, 0x45, 0x78, 0x44, 0x54, 0x41,
0x4c, 0x0a, 0x42, 0x67, 0x4e, 0x56, 0x42, 0x41, 0x73, 0x4d, 0x42, 0x45,
0x56, 0x54, 0x55, 0x44, 0x45, 0x78, 0x45, 0x7a, 0x41, 0x52, 0x42, 0x67,
0x4e, 0x56, 0x42, 0x41, 0x4d, 0x4d, 0x43, 0x6c, 0x4e, 0x6c, 0x63, 0x6e,
0x5a, 0x6c, 0x63, 0x6a, 0x45, 0x67, 0x51, 0x30, 0x45, 0x78, 0x45, 0x7a,
0x41, 0x52, 0x42, 0x67, 0x6b, 0x71, 0x68, 0x6b, 0x69, 0x47, 0x39, 0x77,
0x30, 0x42, 0x43, 0x51, 0x45, 0x57, 0x0a, 0x42, 0x45, 0x56, 0x54, 0x55,
0x44, 0x45, 0x77, 0x48, 0x68, 0x63, 0x4e, 0x4d, 0x54, 0x67, 0x77, 0x4d,
0x54, 0x41, 0x30, 0x4d, 0x44, 0x55, 0x30, 0x4d, 0x7a, 0x49, 0x31, 0x57,
0x68, 0x63, 0x4e, 0x4d, 0x7a, 0x45, 0x77, 0x4f, 0x54, 0x45, 0x7a, 0x4d,
0x44, 0x55, 0x30, 0x4d, 0x7a, 0x49, 0x31, 0x57, 0x6a, 0x42, 0x78, 0x4d,
0x51, 0x73, 0x77, 0x43, 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x47, 0x0a,
0x45, 0x77, 0x4a, 0x44, 0x4d, 0x54, 0x45, 0x4d, 0x4d, 0x41, 0x6f, 0x47,
0x41, 0x31, 0x55, 0x45, 0x43, 0x41, 0x77, 0x44, 0x53, 0x6c, 0x4d, 0x78,
0x4d, 0x51, 0x77, 0x77, 0x43, 0x67, 0x59, 0x44, 0x56, 0x51, 0x51, 0x48,
0x44, 0x41, 0x4e, 0x58, 0x57, 0x44, 0x45, 0x78, 0x44, 0x54, 0x41, 0x4c,
0x42, 0x67, 0x4e, 0x56, 0x42, 0x41, 0x6f, 0x4d, 0x42, 0x45, 0x56, 0x54,
0x55, 0x44, 0x45, 0x78, 0x0a, 0x44, 0x54, 0x41, 0x4c, 0x42, 0x67, 0x4e,
0x56, 0x42, 0x41, 0x73, 0x4d, 0x42, 0x45, 0x56, 0x54, 0x55, 0x44, 0x45,
0x78, 0x45, 0x7a, 0x41, 0x52, 0x42, 0x67, 0x4e, 0x56, 0x42, 0x41, 0x4d,
0x4d, 0x43, 0x6c, 0x4e, 0x6c, 0x63, 0x6e, 0x5a, 0x6c, 0x63, 0x6a, 0x45,
0x67, 0x51, 0x30, 0x45, 0x78, 0x45, 0x7a, 0x41, 0x52, 0x42, 0x67, 0x6b,
0x71, 0x68, 0x6b, 0x69, 0x47, 0x39, 0x77, 0x30, 0x42, 0x0a, 0x43, 0x51,
0x45, 0x57, 0x42, 0x45, 0x56, 0x54, 0x55, 0x44, 0x45, 0x77, 0x67, 0x67,
0x45, 0x69, 0x4d, 0x41, 0x30, 0x47, 0x43, 0x53, 0x71, 0x47, 0x53, 0x49,
0x62, 0x33, 0x44, 0x51, 0x45, 0x42, 0x41, 0x51, 0x55, 0x41, 0x41, 0x34,
0x49, 0x42, 0x44, 0x77, 0x41, 0x77, 0x67, 0x67, 0x45, 0x4b, 0x41, 0x6f,
0x49, 0x42, 0x41, 0x51, 0x44, 0x51, 0x6f, 0x5a, 0x59, 0x42, 0x2b, 0x65,
0x42, 0x45, 0x0a, 0x63, 0x78, 0x66, 0x79, 0x5a, 0x4c, 0x7a, 0x56, 0x6c,
0x4c, 0x4e, 0x37, 0x39, 0x4c, 0x4e, 0x75, 0x62, 0x58, 0x4c, 0x4d, 0x4c,
0x75, 0x4c, 0x32, 0x4c, 0x46, 0x75, 0x58, 0x4f, 0x71, 0x6e, 0x6e, 0x6b,
0x67, 0x41, 0x52, 0x4f, 0x6c, 0x34, 0x33, 0x49, 0x57, 0x4b, 0x6b, 0x63,
0x4a, 0x77, 0x59, 0x4c, 0x2f, 0x4b, 0x45, 0x51, 0x67, 0x6c, 0x68, 0x77,
0x4e, 0x58, 0x5a, 0x48, 0x48, 0x36, 0x4c, 0x0a, 0x64, 0x64, 0x6a, 0x45,
0x44, 0x7a, 0x79, 0x4c, 0x6d, 0x6d, 0x71, 0x74, 0x57, 0x4b, 0x6f, 0x6f,
0x50, 0x47, 0x57, 0x69, 0x50, 0x48, 0x51, 0x6c, 0x72, 0x46, 0x39, 0x77,
0x62, 0x63, 0x69, 0x42, 0x33, 0x6a, 0x47, 0x2b, 0x39, 0x77, 0x65, 0x79,
0x68, 0x39, 0x36, 0x6a, 0x63, 0x34, 0x39, 0x34, 0x4d, 0x6e, 0x67, 0x73,
0x41, 0x42, 0x4b, 0x56, 0x6e, 0x50, 0x4c, 0x67, 0x65, 0x71, 0x49, 0x50,
0x0a, 0x4c, 0x46, 0x63, 0x73, 0x31, 0x46, 0x6c, 0x6f, 0x4a, 0x57, 0x4e,
0x46, 0x38, 0x4e, 0x70, 0x30, 0x4a, 0x36, 0x34, 0x61, 0x33, 0x36, 0x6f,
0x50, 0x79, 0x5a, 0x46, 0x47, 0x66, 0x78, 0x47, 0x33, 0x35, 0x31, 0x6a,
0x7a, 0x58, 0x52, 0x48, 0x5a, 0x78, 0x62, 0x59, 0x33, 0x55, 0x49, 0x55,
0x6e, 0x52, 0x75, 0x42, 0x46, 0x68, 0x54, 0x73, 0x54, 0x73, 0x42, 0x49,
0x44, 0x37, 0x65, 0x53, 0x73, 0x0a, 0x73, 0x6e, 0x37, 0x76, 0x54, 0x35,
0x38, 0x62, 0x54, 0x67, 0x41, 0x30, 0x30, 0x56, 0x42, 0x4a, 0x46, 0x68,
0x4c, 0x75, 0x46, 0x48, 0x67, 0x7a, 0x5a, 0x75, 0x6b, 0x43, 0x2f, 0x37,
0x73, 0x42, 0x6a, 0x78, 0x52, 0x45, 0x57, 0x72, 0x4e, 0x66, 0x2f, 0x2b,
0x66, 0x54, 0x69, 0x34, 0x35, 0x6a, 0x62, 0x79, 0x37, 0x35, 0x77, 0x64,
0x31, 0x35, 0x75, 0x49, 0x55, 0x47, 0x6f, 0x31, 0x66, 0x6c, 0x0a, 0x57,
0x75, 0x56, 0x4e, 0x69, 0x66, 0x72, 0x2b, 0x2b, 0x75, 0x53, 0x79, 0x79,
0x58, 0x76, 0x4d, 0x54, 0x4b, 0x78, 0x64, 0x78, 0x69, 0x37, 0x52, 0x50,
0x47, 0x61, 0x63, 0x57, 0x64, 0x55, 0x6a, 0x30, 0x53, 0x66, 0x6c, 0x43,
0x67, 0x2b, 0x64, 0x72, 0x37, 0x32, 0x4a, 0x43, 0x4b, 0x6b, 0x4a, 0x6e,
0x45, 0x49, 0x69, 0x4e, 0x59, 0x69, 0x6e, 0x39, 0x69, 0x51, 0x38, 0x77,
0x6d, 0x70, 0x50, 0x0a, 0x42, 0x39, 0x2b, 0x4c, 0x43, 0x45, 0x6c, 0x59,
0x39, 0x6d, 0x51, 0x31, 0x41, 0x67, 0x4d, 0x42, 0x41, 0x41, 0x45, 0x77,
0x44, 0x51, 0x59, 0x4a, 0x4b, 0x6f, 0x5a, 0x49, 0x68, 0x76, 0x63, 0x4e,
0x41, 0x51, 0x45, 0x46, 0x42, 0x51, 0x41, 0x44, 0x67, 0x67, 0x45, 0x42,
0x41, 0x47, 0x4a, 0x2f, 0x39, 0x76, 0x51, 0x58, 0x7a, 0x63, 0x6f, 0x6b,
0x34, 0x47, 0x61, 0x47, 0x33, 0x6f, 0x76, 0x7a, 0x0a, 0x47, 0x36, 0x58,
0x47, 0x49, 0x4d, 0x53, 0x33, 0x62, 0x67, 0x42, 0x36, 0x70, 0x38, 0x77,
0x6d, 0x55, 0x70, 0x32, 0x43, 0x2f, 0x69, 0x59, 0x31, 0x5a, 0x37, 0x30,
0x49, 0x65, 0x74, 0x55, 0x41, 0x35, 0x6e, 0x61, 0x42, 0x67, 0x4c, 0x4c,
0x4c, 0x7a, 0x6f, 0x6e, 0x71, 0x73, 0x58, 0x71, 0x4c, 0x51, 0x64, 0x78,
0x78, 0x48, 0x53, 0x34, 0x47, 0x76, 0x46, 0x63, 0x59, 0x6d, 0x66, 0x30,
0x71, 0x0a, 0x34, 0x61, 0x43, 0x38, 0x4e, 0x77, 0x69, 0x4f, 0x54, 0x4d,
0x63, 0x31, 0x4e, 0x47, 0x56, 0x6e, 0x5a, 0x35, 0x34, 0x70, 0x7a, 0x44,
0x59, 0x6a, 0x49, 0x70, 0x70, 0x45, 0x58, 0x6c, 0x30, 0x47, 0x72, 0x73,
0x6d, 0x71, 0x36, 0x67, 0x4a, 0x2b, 0x61, 0x51, 0x43, 0x52, 0x73, 0x6b,
0x76, 0x68, 0x32, 0x65, 0x41, 0x38, 0x6b, 0x47, 0x47, 0x4d, 0x54, 0x74,
0x58, 0x63, 0x52, 0x69, 0x55, 0x36, 0x0a, 0x41, 0x6a, 0x4d, 0x64, 0x61,
0x75, 0x35, 0x52, 0x2b, 0x35, 0x78, 0x73, 0x33, 0x6f, 0x74, 0x62, 0x76,
0x37, 0x6f, 0x63, 0x65, 0x58, 0x6d, 0x37, 0x31, 0x30, 0x71, 0x79, 0x5a,
0x43, 0x78, 0x44, 0x64, 0x31, 0x63, 0x73, 0x74, 0x62, 0x72, 0x69, 0x7a,
0x77, 0x54, 0x4a, 0x5a, 0x37, 0x65, 0x78, 0x35, 0x64, 0x59, 0x7a, 0x65,
0x65, 0x4a, 0x52, 0x32, 0x50, 0x61, 0x6b, 0x2f, 0x6f, 0x6c, 0x53, 0x0a,
0x38, 0x75, 0x56, 0x4e, 0x31, 0x34, 0x7a, 0x55, 0x77, 0x67, 0x47, 0x56,
0x4c, 0x4b, 0x79, 0x62, 0x37, 0x43, 0x58, 0x47, 0x48, 0x49, 0x31, 0x37,
0x7a, 0x2b, 0x78, 0x59, 0x4c, 0x74, 0x75, 0x61, 0x73, 0x39, 0x6a, 0x4d,
0x42, 0x62, 0x4e, 0x5a, 0x77, 0x53, 0x33, 0x4f, 0x30, 0x52, 0x31, 0x36,
0x7a, 0x63, 0x46, 0x62, 0x43, 0x59, 0x6f, 0x64, 0x57, 0x55, 0x43, 0x68,
0x58, 0x71, 0x52, 0x4d, 0x0a, 0x6a, 0x4e, 0x51, 0x79, 0x70, 0x57, 0x4d,
0x4c, 0x37, 0x30, 0x78, 0x4e, 0x4b, 0x67, 0x48, 0x45, 0x67, 0x6b, 0x6a,
0x47, 0x36, 0x74, 0x55, 0x5a, 0x54, 0x4a, 0x31, 0x68, 0x2b, 0x54, 0x37,
0x61, 0x31, 0x37, 0x4b, 0x70, 0x49, 0x4f, 0x6a, 0x35, 0x59, 0x53, 0x50,
0x46, 0x4e, 0x50, 0x58, 0x72, 0x45, 0x66, 0x69, 0x43, 0x33, 0x4e, 0x50,
0x53, 0x72, 0x55, 0x2f, 0x4b, 0x6a, 0x6f, 0x41, 0x51, 0x0a, 0x7a, 0x45,
0x59, 0x3d, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20,
0x43, 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d,
0x2d, 0x2d, 0x2d, 0x2d, 0x0a
};
unsigned int ca_crt_len = 1229;
#endif
const unsigned char server_crt[] = {
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x43,
0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d,
0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x5a, 0x44, 0x43, 0x43,
0x41, 0x6b, 0x77, 0x43, 0x43, 0x51, 0x44, 0x55, 0x38, 0x4a, 0x7a, 0x35,
0x32, 0x47, 0x55, 0x76, 0x69, 0x6a, 0x41, 0x4e, 0x42, 0x67, 0x6b, 0x71,
0x68, 0x6b, 0x69, 0x47, 0x39, 0x77, 0x30, 0x42, 0x41, 0x51, 0x55, 0x46,
0x41, 0x44, 0x42, 0x78, 0x4d, 0x51, 0x73, 0x77, 0x43, 0x51, 0x59, 0x44,
0x56, 0x51, 0x51, 0x47, 0x45, 0x77, 0x4a, 0x44, 0x0a, 0x4d, 0x54, 0x45,
0x4d, 0x4d, 0x41, 0x6f, 0x47, 0x41, 0x31, 0x55, 0x45, 0x43, 0x41, 0x77,
0x44, 0x53, 0x6c, 0x4d, 0x78, 0x4d, 0x51, 0x77, 0x77, 0x43, 0x67, 0x59,
0x44, 0x56, 0x51, 0x51, 0x48, 0x44, 0x41, 0x4e, 0x58, 0x57, 0x44, 0x45,
0x78, 0x44, 0x54, 0x41, 0x4c, 0x42, 0x67, 0x4e, 0x56, 0x42, 0x41, 0x6f,
0x4d, 0x42, 0x45, 0x56, 0x54, 0x55, 0x44, 0x45, 0x78, 0x44, 0x54, 0x41,
0x4c, 0x0a, 0x42, 0x67, 0x4e, 0x56, 0x42, 0x41, 0x73, 0x4d, 0x42, 0x45,
0x56, 0x54, 0x55, 0x44, 0x45, 0x78, 0x45, 0x7a, 0x41, 0x52, 0x42, 0x67,
0x4e, 0x56, 0x42, 0x41, 0x4d, 0x4d, 0x43, 0x6c, 0x4e, 0x6c, 0x63, 0x6e,
0x5a, 0x6c, 0x63, 0x6a, 0x45, 0x67, 0x51, 0x30, 0x45, 0x78, 0x45, 0x7a,
0x41, 0x52, 0x42, 0x67, 0x6b, 0x71, 0x68, 0x6b, 0x69, 0x47, 0x39, 0x77,
0x30, 0x42, 0x43, 0x51, 0x45, 0x57, 0x0a, 0x42, 0x45, 0x56, 0x54, 0x55,
0x44, 0x45, 0x77, 0x48, 0x68, 0x63, 0x4e, 0x4d, 0x54, 0x67, 0x77, 0x4d,
0x54, 0x41, 0x30, 0x4d, 0x44, 0x55, 0x30, 0x4d, 0x7a, 0x49, 0x31, 0x57,
0x68, 0x63, 0x4e, 0x4d, 0x7a, 0x45, 0x77, 0x4f, 0x54, 0x45, 0x7a, 0x4d,
0x44, 0x55, 0x30, 0x4d, 0x7a, 0x49, 0x31, 0x57, 0x6a, 0x42, 0x33, 0x4d,
0x51, 0x73, 0x77, 0x43, 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x47, 0x0a,
0x45, 0x77, 0x4a, 0x44, 0x4d, 0x6a, 0x45, 0x4e, 0x4d, 0x41, 0x73, 0x47,
0x41, 0x31, 0x55, 0x45, 0x43, 0x41, 0x77, 0x45, 0x53, 0x6c, 0x4d, 0x79,
0x4d, 0x6a, 0x45, 0x4e, 0x4d, 0x41, 0x73, 0x47, 0x41, 0x31, 0x55, 0x45,
0x42, 0x77, 0x77, 0x45, 0x56, 0x31, 0x67, 0x79, 0x4d, 0x6a, 0x45, 0x4f,
0x4d, 0x41, 0x77, 0x47, 0x41, 0x31, 0x55, 0x45, 0x43, 0x67, 0x77, 0x46,
0x52, 0x56, 0x4e, 0x51, 0x0a, 0x4d, 0x6a, 0x49, 0x78, 0x44, 0x6a, 0x41,
0x4d, 0x42, 0x67, 0x4e, 0x56, 0x42, 0x41, 0x73, 0x4d, 0x42, 0x55, 0x56,
0x54, 0x55, 0x44, 0x49, 0x79, 0x4d, 0x52, 0x51, 0x77, 0x45, 0x67, 0x59,
0x44, 0x56, 0x51, 0x51, 0x44, 0x44, 0x41, 0x74, 0x54, 0x5a, 0x58, 0x4a,
0x32, 0x5a, 0x58, 0x49, 0x79, 0x4d, 0x69, 0x42, 0x44, 0x51, 0x54, 0x45,
0x55, 0x4d, 0x42, 0x49, 0x47, 0x43, 0x53, 0x71, 0x47, 0x0a, 0x53, 0x49,
0x62, 0x33, 0x44, 0x51, 0x45, 0x4a, 0x41, 0x52, 0x59, 0x46, 0x52, 0x56,
0x4e, 0x51, 0x4d, 0x6a, 0x49, 0x77, 0x67, 0x67, 0x45, 0x69, 0x4d, 0x41,
0x30, 0x47, 0x43, 0x53, 0x71, 0x47, 0x53, 0x49, 0x62, 0x33, 0x44, 0x51,
0x45, 0x42, 0x41, 0x51, 0x55, 0x41, 0x41, 0x34, 0x49, 0x42, 0x44, 0x77,
0x41, 0x77, 0x67, 0x67, 0x45, 0x4b, 0x41, 0x6f, 0x49, 0x42, 0x41, 0x51,
0x44, 0x6a, 0x0a, 0x71, 0x78, 0x65, 0x54, 0x51, 0x6a, 0x51, 0x79, 0x64,
0x33, 0x37, 0x76, 0x2f, 0x62, 0x67, 0x33, 0x46, 0x64, 0x70, 0x2f, 0x70,
0x77, 0x39, 0x64, 0x6a, 0x43, 0x64, 0x49, 0x69, 0x52, 0x43, 0x35, 0x70,
0x55, 0x65, 0x68, 0x6f, 0x48, 0x4e, 0x75, 0x6e, 0x31, 0x5a, 0x48, 0x59,
0x73, 0x62, 0x45, 0x6a, 0x66, 0x56, 0x49, 0x75, 0x6e, 0x2b, 0x31, 0x55,
0x74, 0x32, 0x59, 0x6d, 0x50, 0x58, 0x74, 0x0a, 0x68, 0x64, 0x4f, 0x36,
0x56, 0x53, 0x65, 0x2f, 0x5a, 0x32, 0x4f, 0x4d, 0x51, 0x65, 0x4e, 0x4b,
0x76, 0x47, 0x7a, 0x5a, 0x64, 0x57, 0x55, 0x4f, 0x72, 0x42, 0x55, 0x58,
0x4e, 0x48, 0x47, 0x77, 0x70, 0x37, 0x4f, 0x44, 0x35, 0x35, 0x34, 0x5a,
0x32, 0x48, 0x34, 0x45, 0x78, 0x47, 0x48, 0x2b, 0x46, 0x5a, 0x38, 0x73,
0x59, 0x47, 0x67, 0x39, 0x37, 0x72, 0x43, 0x70, 0x39, 0x59, 0x33, 0x52,
0x0a, 0x56, 0x52, 0x4a, 0x74, 0x72, 0x33, 0x30, 0x31, 0x55, 0x68, 0x72,
0x34, 0x62, 0x30, 0x6f, 0x59, 0x64, 0x39, 0x67, 0x37, 0x48, 0x33, 0x47,
0x79, 0x6d, 0x51, 0x76, 0x55, 0x79, 0x73, 0x7a, 0x65, 0x62, 0x61, 0x76,
0x36, 0x48, 0x4b, 0x45, 0x6b, 0x74, 0x65, 0x56, 0x35, 0x45, 0x67, 0x64,
0x59, 0x31, 0x65, 0x6f, 0x51, 0x78, 0x65, 0x46, 0x59, 0x36, 0x36, 0x51,
0x7a, 0x43, 0x2b, 0x30, 0x4b, 0x0a, 0x6f, 0x53, 0x32, 0x73, 0x75, 0x4a,
0x31, 0x34, 0x70, 0x30, 0x4b, 0x6f, 0x4f, 0x4f, 0x6f, 0x74, 0x47, 0x4e,
0x6c, 0x41, 0x59, 0x49, 0x31, 0x75, 0x50, 0x57, 0x37, 0x7a, 0x4c, 0x54,
0x31, 0x70, 0x73, 0x6d, 0x4a, 0x76, 0x6c, 0x55, 0x51, 0x57, 0x4e, 0x66,
0x38, 0x70, 0x6e, 0x56, 0x47, 0x36, 0x53, 0x47, 0x30, 0x72, 0x37, 0x73,
0x78, 0x76, 0x59, 0x50, 0x64, 0x52, 0x57, 0x76, 0x53, 0x45, 0x0a, 0x53,
0x58, 0x31, 0x6f, 0x67, 0x52, 0x56, 0x31, 0x69, 0x4f, 0x65, 0x61, 0x47,
0x50, 0x33, 0x43, 0x56, 0x64, 0x53, 0x58, 0x47, 0x70, 0x50, 0x31, 0x31,
0x46, 0x50, 0x33, 0x56, 0x58, 0x46, 0x64, 0x33, 0x56, 0x34, 0x4d, 0x6e,
0x57, 0x55, 0x6c, 0x62, 0x36, 0x56, 0x69, 0x45, 0x59, 0x79, 0x5a, 0x75,
0x34, 0x58, 0x2f, 0x32, 0x33, 0x59, 0x49, 0x45, 0x36, 0x44, 0x46, 0x46,
0x53, 0x67, 0x41, 0x0a, 0x59, 0x33, 0x33, 0x63, 0x59, 0x45, 0x4a, 0x4b,
0x4b, 0x68, 0x62, 0x69, 0x4c, 0x43, 0x38, 0x44, 0x4a, 0x66, 0x35, 0x68,
0x41, 0x67, 0x4d, 0x42, 0x41, 0x41, 0x45, 0x77, 0x44, 0x51, 0x59, 0x4a,
0x4b, 0x6f, 0x5a, 0x49, 0x68, 0x76, 0x63, 0x4e, 0x41, 0x51, 0x45, 0x46,
0x42, 0x51, 0x41, 0x44, 0x67, 0x67, 0x45, 0x42, 0x41, 0x45, 0x46, 0x79,
0x71, 0x5a, 0x48, 0x54, 0x65, 0x70, 0x51, 0x63, 0x0a, 0x49, 0x65, 0x32,
0x43, 0x45, 0x6b, 0x55, 0x50, 0x76, 0x35, 0x41, 0x58, 0x30, 0x6d, 0x35,
0x59, 0x43, 0x38, 0x6b, 0x2f, 0x38, 0x56, 0x6d, 0x64, 0x54, 0x2b, 0x30,
0x49, 0x78, 0x6f, 0x39, 0x47, 0x74, 0x2b, 0x79, 0x6c, 0x62, 0x62, 0x50,
0x64, 0x36, 0x6d, 0x68, 0x67, 0x6f, 0x5a, 0x49, 0x51, 0x49, 0x6c, 0x61,
0x46, 0x79, 0x4a, 0x45, 0x37, 0x54, 0x38, 0x56, 0x2b, 0x6d, 0x38, 0x76,
0x65, 0x0a, 0x77, 0x6f, 0x65, 0x47, 0x59, 0x2b, 0x68, 0x37, 0x62, 0x72,
0x76, 0x50, 0x63, 0x62, 0x51, 0x78, 0x6c, 0x34, 0x6c, 0x30, 0x52, 0x59,
0x44, 0x76, 0x30, 0x76, 0x2b, 0x2f, 0x45, 0x6e, 0x55, 0x53, 0x6e, 0x61,
0x54, 0x32, 0x69, 0x42, 0x37, 0x57, 0x34, 0x54, 0x4d, 0x33, 0x54, 0x67,
0x6a, 0x46, 0x55, 0x55, 0x50, 0x30, 0x4e, 0x42, 0x34, 0x71, 0x72, 0x74,
0x74, 0x74, 0x32, 0x37, 0x42, 0x35, 0x0a, 0x36, 0x56, 0x6d, 0x74, 0x4c,
0x36, 0x71, 0x52, 0x44, 0x30, 0x79, 0x78, 0x6b, 0x48, 0x74, 0x41, 0x33,
0x4e, 0x68, 0x42, 0x70, 0x33, 0x6f, 0x42, 0x4c, 0x4a, 0x4a, 0x47, 0x6d,
0x54, 0x46, 0x2b, 0x63, 0x30, 0x48, 0x6e, 0x63, 0x36, 0x53, 0x41, 0x38,
0x56, 0x43, 0x79, 0x67, 0x79, 0x47, 0x57, 0x4a, 0x75, 0x58, 0x68, 0x2b,
0x51, 0x46, 0x59, 0x4e, 0x30, 0x46, 0x53, 0x35, 0x41, 0x6b, 0x44, 0x0a,
0x4b, 0x46, 0x55, 0x72, 0x6e, 0x30, 0x68, 0x39, 0x7a, 0x57, 0x45, 0x57,
0x4d, 0x6c, 0x68, 0x51, 0x75, 0x63, 0x4b, 0x37, 0x76, 0x36, 0x71, 0x6f,
0x46, 0x6d, 0x53, 0x76, 0x46, 0x6c, 0x78, 0x77, 0x70, 0x70, 0x4e, 0x50,
0x63, 0x48, 0x39, 0x35, 0x31, 0x77, 0x32, 0x2f, 0x6c, 0x66, 0x4a, 0x72,
0x67, 0x61, 0x7a, 0x43, 0x55, 0x4b, 0x64, 0x5a, 0x64, 0x41, 0x30, 0x59,
0x78, 0x43, 0x7a, 0x66, 0x0a, 0x2b, 0x6b, 0x6e, 0x34, 0x75, 0x79, 0x55,
0x50, 0x6f, 0x75, 0x50, 0x38, 0x69, 0x62, 0x62, 0x57, 0x68, 0x65, 0x6c,
0x6b, 0x50, 0x7a, 0x4f, 0x76, 0x72, 0x35, 0x38, 0x66, 0x39, 0x6c, 0x63,
0x69, 0x42, 0x74, 0x42, 0x70, 0x54, 0x44, 0x38, 0x50, 0x68, 0x4e, 0x75,
0x2b, 0x64, 0x55, 0x48, 0x31, 0x30, 0x78, 0x76, 0x47, 0x6c, 0x31, 0x62,
0x47, 0x44, 0x6f, 0x6c, 0x59, 0x52, 0x64, 0x64, 0x43, 0x0a, 0x34, 0x68,
0x2f, 0x77, 0x55, 0x47, 0x58, 0x4c, 0x2f, 0x47, 0x41, 0x3d, 0x0a, 0x2d,
0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x43, 0x45, 0x52, 0x54,
0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d,
0x0a
};
unsigned int server_crt_len = 1237;
const unsigned char server_key[] = {
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x52,
0x53, 0x41, 0x20, 0x50, 0x52, 0x49, 0x56, 0x41, 0x54, 0x45, 0x20, 0x4b,
0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x45,
0x70, 0x41, 0x49, 0x42, 0x41, 0x41, 0x4b, 0x43, 0x41, 0x51, 0x45, 0x41,
0x34, 0x36, 0x73, 0x58, 0x6b, 0x30, 0x49, 0x30, 0x4d, 0x6e, 0x64, 0x2b,
0x37, 0x2f, 0x32, 0x34, 0x4e, 0x78, 0x58, 0x61, 0x66, 0x36, 0x63, 0x50,
0x58, 0x59, 0x77, 0x6e, 0x53, 0x49, 0x6b, 0x51, 0x75, 0x61, 0x56, 0x48,
0x6f, 0x61, 0x42, 0x7a, 0x62, 0x70, 0x39, 0x57, 0x52, 0x32, 0x4c, 0x47,
0x0a, 0x78, 0x49, 0x33, 0x31, 0x53, 0x4c, 0x70, 0x2f, 0x74, 0x56, 0x4c,
0x64, 0x6d, 0x4a, 0x6a, 0x31, 0x37, 0x59, 0x58, 0x54, 0x75, 0x6c, 0x55,
0x6e, 0x76, 0x32, 0x64, 0x6a, 0x6a, 0x45, 0x48, 0x6a, 0x53, 0x72, 0x78,
0x73, 0x32, 0x58, 0x56, 0x6c, 0x44, 0x71, 0x77, 0x56, 0x46, 0x7a, 0x52,
0x78, 0x73, 0x4b, 0x65, 0x7a, 0x67, 0x2b, 0x65, 0x65, 0x47, 0x64, 0x68,
0x2b, 0x42, 0x4d, 0x52, 0x68, 0x0a, 0x2f, 0x68, 0x57, 0x66, 0x4c, 0x47,
0x42, 0x6f, 0x50, 0x65, 0x36, 0x77, 0x71, 0x66, 0x57, 0x4e, 0x30, 0x56,
0x55, 0x53, 0x62, 0x61, 0x39, 0x39, 0x4e, 0x56, 0x49, 0x61, 0x2b, 0x47,
0x39, 0x4b, 0x47, 0x48, 0x66, 0x59, 0x4f, 0x78, 0x39, 0x78, 0x73, 0x70,
0x6b, 0x4c, 0x31, 0x4d, 0x72, 0x4d, 0x33, 0x6d, 0x32, 0x72, 0x2b, 0x68,
0x79, 0x68, 0x4a, 0x4c, 0x58, 0x6c, 0x65, 0x52, 0x49, 0x48, 0x0a, 0x57,
0x4e, 0x58, 0x71, 0x45, 0x4d, 0x58, 0x68, 0x57, 0x4f, 0x75, 0x6b, 0x4d,
0x77, 0x76, 0x74, 0x43, 0x71, 0x45, 0x74, 0x72, 0x4c, 0x69, 0x64, 0x65,
0x4b, 0x64, 0x43, 0x71, 0x44, 0x6a, 0x71, 0x4c, 0x52, 0x6a, 0x5a, 0x51,
0x47, 0x43, 0x4e, 0x62, 0x6a, 0x31, 0x75, 0x38, 0x79, 0x30, 0x39, 0x61,
0x62, 0x4a, 0x69, 0x62, 0x35, 0x56, 0x45, 0x46, 0x6a, 0x58, 0x2f, 0x4b,
0x5a, 0x31, 0x52, 0x0a, 0x75, 0x6b, 0x68, 0x74, 0x4b, 0x2b, 0x37, 0x4d,
0x62, 0x32, 0x44, 0x33, 0x55, 0x56, 0x72, 0x30, 0x68, 0x45, 0x6c, 0x39,
0x61, 0x49, 0x45, 0x56, 0x64, 0x59, 0x6a, 0x6e, 0x6d, 0x68, 0x6a, 0x39,
0x77, 0x6c, 0x58, 0x55, 0x6c, 0x78, 0x71, 0x54, 0x39, 0x64, 0x52, 0x54,
0x39, 0x31, 0x56, 0x78, 0x58, 0x64, 0x31, 0x65, 0x44, 0x4a, 0x31, 0x6c,
0x4a, 0x57, 0x2b, 0x6c, 0x59, 0x68, 0x47, 0x4d, 0x0a, 0x6d, 0x62, 0x75,
0x46, 0x2f, 0x39, 0x74, 0x32, 0x43, 0x42, 0x4f, 0x67, 0x78, 0x52, 0x55,
0x6f, 0x41, 0x47, 0x4e, 0x39, 0x33, 0x47, 0x42, 0x43, 0x53, 0x69, 0x6f,
0x57, 0x34, 0x69, 0x77, 0x76, 0x41, 0x79, 0x58, 0x2b, 0x59, 0x51, 0x49,
0x44, 0x41, 0x51, 0x41, 0x42, 0x41, 0x6f, 0x49, 0x42, 0x41, 0x51, 0x43,
0x63, 0x50, 0x36, 0x58, 0x55, 0x61, 0x4e, 0x46, 0x4e, 0x2b, 0x58, 0x6a,
0x69, 0x0a, 0x32, 0x47, 0x78, 0x2f, 0x55, 0x62, 0x34, 0x39, 0x63, 0x62,
0x67, 0x33, 0x36, 0x61, 0x4a, 0x6b, 0x51, 0x37, 0x31, 0x34, 0x79, 0x35,
0x67, 0x58, 0x73, 0x76, 0x6a, 0x46, 0x36, 0x52, 0x44, 0x62, 0x75, 0x55,
0x7a, 0x56, 0x52, 0x4f, 0x41, 0x63, 0x59, 0x2f, 0x4b, 0x56, 0x7a, 0x53,
0x4e, 0x4d, 0x51, 0x56, 0x34, 0x32, 0x2f, 0x32, 0x76, 0x4b, 0x4b, 0x2f,
0x73, 0x31, 0x66, 0x34, 0x31, 0x77, 0x0a, 0x69, 0x78, 0x2b, 0x6c, 0x41,
0x53, 0x42, 0x72, 0x4e, 0x67, 0x68, 0x2f, 0x51, 0x31, 0x62, 0x4f, 0x4d,
0x43, 0x33, 0x2b, 0x6f, 0x58, 0x6d, 0x77, 0x67, 0x31, 0x75, 0x63, 0x50,
0x56, 0x70, 0x76, 0x68, 0x79, 0x41, 0x42, 0x73, 0x33, 0x45, 0x6b, 0x6b,
0x5a, 0x32, 0x35, 0x4d, 0x34, 0x47, 0x48, 0x67, 0x34, 0x45, 0x72, 0x4d,
0x34, 0x6d, 0x4d, 0x51, 0x61, 0x35, 0x61, 0x30, 0x39, 0x69, 0x79, 0x0a,
0x76, 0x42, 0x35, 0x4d, 0x49, 0x4b, 0x51, 0x45, 0x48, 0x35, 0x5a, 0x32,
0x6a, 0x6c, 0x62, 0x79, 0x57, 0x61, 0x53, 0x30, 0x32, 0x75, 0x76, 0x32,
0x6f, 0x59, 0x4e, 0x75, 0x6e, 0x79, 0x52, 0x47, 0x67, 0x30, 0x72, 0x6a,
0x6d, 0x44, 0x4e, 0x41, 0x36, 0x54, 0x69, 0x68, 0x62, 0x48, 0x50, 0x74,
0x37, 0x6f, 0x43, 0x67, 0x49, 0x48, 0x49, 0x72, 0x35, 0x6a, 0x49, 0x6e,
0x77, 0x74, 0x52, 0x48, 0x0a, 0x6f, 0x42, 0x68, 0x64, 0x74, 0x51, 0x35,
0x78, 0x71, 0x58, 0x4c, 0x44, 0x6b, 0x72, 0x36, 0x39, 0x33, 0x36, 0x6c,
0x6e, 0x4e, 0x52, 0x41, 0x46, 0x44, 0x53, 0x39, 0x7a, 0x52, 0x32, 0x61,
0x73, 0x6b, 0x48, 0x4b, 0x73, 0x49, 0x76, 0x46, 0x47, 0x66, 0x78, 0x37,
0x44, 0x63, 0x54, 0x7a, 0x57, 0x79, 0x53, 0x74, 0x78, 0x55, 0x4f, 0x74,
0x46, 0x75, 0x64, 0x2f, 0x6a, 0x67, 0x42, 0x68, 0x33, 0x0a, 0x68, 0x79,
0x43, 0x33, 0x46, 0x53, 0x75, 0x55, 0x47, 0x67, 0x42, 0x55, 0x71, 0x69,
0x39, 0x61, 0x4c, 0x37, 0x59, 0x66, 0x37, 0x45, 0x69, 0x6a, 0x52, 0x57,
0x6b, 0x39, 0x72, 0x70, 0x57, 0x45, 0x34, 0x4b, 0x4e, 0x35, 0x43, 0x4f,
0x36, 0x72, 0x4b, 0x75, 0x49, 0x5a, 0x33, 0x37, 0x62, 0x51, 0x50, 0x56,
0x47, 0x6a, 0x51, 0x5a, 0x4e, 0x4f, 0x4d, 0x36, 0x79, 0x66, 0x78, 0x66,
0x42, 0x69, 0x0a, 0x79, 0x4a, 0x79, 0x66, 0x35, 0x73, 0x62, 0x70, 0x41,
0x6f, 0x47, 0x42, 0x41, 0x50, 0x5a, 0x59, 0x30, 0x34, 0x68, 0x54, 0x42,
0x31, 0x78, 0x62, 0x46, 0x73, 0x62, 0x5a, 0x49, 0x30, 0x73, 0x2f, 0x49,
0x45, 0x43, 0x75, 0x6b, 0x64, 0x38, 0x45, 0x6d, 0x46, 0x50, 0x6e, 0x51,
0x38, 0x31, 0x4a, 0x48, 0x42, 0x76, 0x4a, 0x33, 0x6a, 0x46, 0x6e, 0x72,
0x44, 0x4f, 0x72, 0x43, 0x44, 0x70, 0x52, 0x0a, 0x57, 0x39, 0x75, 0x31,
0x46, 0x50, 0x52, 0x32, 0x48, 0x76, 0x67, 0x49, 0x4c, 0x4c, 0x46, 0x37,
0x36, 0x44, 0x39, 0x4c, 0x6e, 0x78, 0x6e, 0x4a, 0x78, 0x4e, 0x2f, 0x32,
0x73, 0x30, 0x34, 0x47, 0x49, 0x47, 0x43, 0x37, 0x6a, 0x70, 0x45, 0x39,
0x7a, 0x49, 0x67, 0x57, 0x67, 0x32, 0x72, 0x75, 0x55, 0x4b, 0x64, 0x64,
0x6b, 0x48, 0x64, 0x4e, 0x47, 0x78, 0x56, 0x39, 0x52, 0x59, 0x44, 0x30,
0x0a, 0x4a, 0x45, 0x42, 0x78, 0x30, 0x5a, 0x50, 0x61, 0x58, 0x54, 0x73,
0x39, 0x43, 0x57, 0x58, 0x4c, 0x41, 0x75, 0x65, 0x66, 0x65, 0x54, 0x2b,
0x45, 0x6c, 0x31, 0x66, 0x4d, 0x41, 0x75, 0x6d, 0x43, 0x62, 0x6f, 0x31,
0x44, 0x78, 0x45, 0x42, 0x5a, 0x75, 0x4b, 0x62, 0x72, 0x74, 0x77, 0x4e,
0x39, 0x37, 0x67, 0x68, 0x74, 0x69, 0x61, 0x50, 0x72, 0x41, 0x6f, 0x47,
0x42, 0x41, 0x4f, 0x79, 0x57, 0x0a, 0x35, 0x53, 0x6e, 0x42, 0x6f, 0x78,
0x38, 0x4c, 0x77, 0x44, 0x46, 0x49, 0x38, 0x57, 0x42, 0x77, 0x33, 0x4a,
0x6b, 0x37, 0x5a, 0x52, 0x63, 0x6d, 0x54, 0x41, 0x69, 0x44, 0x4b, 0x6b,
0x53, 0x78, 0x6d, 0x77, 0x43, 0x43, 0x64, 0x50, 0x69, 0x2b, 0x61, 0x38,
0x53, 0x63, 0x74, 0x6c, 0x42, 0x4e, 0x4c, 0x6a, 0x47, 0x31, 0x57, 0x69,
0x74, 0x34, 0x36, 0x68, 0x36, 0x6e, 0x79, 0x55, 0x75, 0x50, 0x0a, 0x75,
0x6f, 0x53, 0x41, 0x58, 0x56, 0x34, 0x32, 0x55, 0x77, 0x4b, 0x71, 0x62,
0x47, 0x37, 0x69, 0x77, 0x34, 0x70, 0x56, 0x53, 0x69, 0x54, 0x33, 0x54,
0x59, 0x37, 0x55, 0x38, 0x51, 0x72, 0x75, 0x53, 0x55, 0x38, 0x79, 0x75,
0x76, 0x32, 0x4d, 0x51, 0x30, 0x70, 0x76, 0x45, 0x70, 0x6b, 0x47, 0x43,
0x78, 0x62, 0x62, 0x4e, 0x54, 0x41, 0x58, 0x56, 0x43, 0x6c, 0x4d, 0x57,
0x34, 0x65, 0x6a, 0x0a, 0x61, 0x45, 0x43, 0x4c, 0x55, 0x49, 0x52, 0x2f,
0x56, 0x52, 0x34, 0x61, 0x36, 0x42, 0x75, 0x4a, 0x56, 0x7a, 0x48, 0x6e,
0x67, 0x65, 0x2b, 0x30, 0x33, 0x47, 0x36, 0x72, 0x63, 0x58, 0x68, 0x45,
0x51, 0x75, 0x4f, 0x36, 0x4a, 0x4b, 0x2f, 0x6a, 0x41, 0x6f, 0x47, 0x41,
0x57, 0x75, 0x54, 0x5a, 0x58, 0x2b, 0x4a, 0x71, 0x77, 0x39, 0x76, 0x6b,
0x49, 0x6f, 0x68, 0x35, 0x71, 0x6b, 0x6a, 0x6e, 0x0a, 0x61, 0x4a, 0x5a,
0x41, 0x77, 0x6b, 0x33, 0x61, 0x75, 0x70, 0x4b, 0x4a, 0x42, 0x4e, 0x53,
0x6c, 0x35, 0x32, 0x62, 0x72, 0x63, 0x63, 0x55, 0x7a, 0x31, 0x33, 0x69,
0x53, 0x74, 0x35, 0x6e, 0x50, 0x54, 0x39, 0x69, 0x58, 0x6d, 0x37, 0x39,
0x39, 0x6d, 0x37, 0x7a, 0x41, 0x61, 0x4e, 0x67, 0x30, 0x45, 0x43, 0x36,
0x68, 0x7a, 0x49, 0x30, 0x53, 0x39, 0x43, 0x50, 0x39, 0x63, 0x70, 0x74,
0x32, 0x0a, 0x46, 0x4e, 0x72, 0x2f, 0x4e, 0x6e, 0x33, 0x62, 0x32, 0x70,
0x61, 0x62, 0x39, 0x4c, 0x64, 0x72, 0x30, 0x4f, 0x55, 0x48, 0x36, 0x63,
0x7a, 0x73, 0x59, 0x6c, 0x68, 0x76, 0x62, 0x61, 0x34, 0x6e, 0x4e, 0x68,
0x51, 0x49, 0x35, 0x51, 0x6e, 0x62, 0x2f, 0x6f, 0x42, 0x39, 0x32, 0x43,
0x34, 0x34, 0x71, 0x79, 0x65, 0x2f, 0x50, 0x2f, 0x31, 0x33, 0x4c, 0x66,
0x44, 0x62, 0x58, 0x6a, 0x63, 0x2b, 0x0a, 0x2f, 0x53, 0x5a, 0x30, 0x34,
0x64, 0x31, 0x34, 0x38, 0x54, 0x58, 0x6c, 0x75, 0x41, 0x63, 0x64, 0x37,
0x63, 0x51, 0x6b, 0x31, 0x66, 0x4d, 0x43, 0x67, 0x59, 0x41, 0x31, 0x4f,
0x41, 0x51, 0x34, 0x6a, 0x6c, 0x77, 0x35, 0x49, 0x5a, 0x57, 0x4d, 0x41,
0x4a, 0x4a, 0x37, 0x56, 0x53, 0x66, 0x71, 0x4f, 0x42, 0x67, 0x58, 0x53,
0x77, 0x41, 0x54, 0x63, 0x68, 0x33, 0x52, 0x38, 0x66, 0x6d, 0x68, 0x0a,
0x39, 0x62, 0x61, 0x47, 0x4e, 0x5a, 0x39, 0x63, 0x54, 0x4c, 0x77, 0x64,
0x2b, 0x42, 0x36, 0x4b, 0x41, 0x2f, 0x64, 0x53, 0x6a, 0x42, 0x4a, 0x74,
0x37, 0x75, 0x51, 0x79, 0x34, 0x52, 0x50, 0x64, 0x74, 0x6c, 0x70, 0x4b,
0x56, 0x51, 0x48, 0x37, 0x4f, 0x59, 0x62, 0x47, 0x73, 0x71, 0x2b, 0x31,
0x73, 0x46, 0x58, 0x4c, 0x56, 0x75, 0x33, 0x56, 0x55, 0x55, 0x55, 0x66,
0x5a, 0x6c, 0x61, 0x4c, 0x0a, 0x51, 0x43, 0x67, 0x62, 0x33, 0x4b, 0x39,
0x4f, 0x55, 0x51, 0x6e, 0x6e, 0x37, 0x68, 0x6f, 0x55, 0x66, 0x42, 0x37,
0x4d, 0x77, 0x4a, 0x4b, 0x6e, 0x37, 0x34, 0x73, 0x76, 0x53, 0x61, 0x79,
0x72, 0x79, 0x43, 0x72, 0x47, 0x67, 0x7a, 0x59, 0x4d, 0x58, 0x5a, 0x55,
0x55, 0x6a, 0x4b, 0x4a, 0x57, 0x6b, 0x58, 0x4d, 0x44, 0x44, 0x72, 0x58,
0x2f, 0x46, 0x77, 0x57, 0x68, 0x50, 0x67, 0x6d, 0x6c, 0x0a, 0x33, 0x44,
0x74, 0x67, 0x62, 0x51, 0x4b, 0x42, 0x67, 0x51, 0x43, 0x59, 0x71, 0x66,
0x51, 0x4f, 0x4f, 0x50, 0x61, 0x71, 0x68, 0x2f, 0x54, 0x73, 0x4f, 0x49,
0x67, 0x73, 0x5a, 0x2f, 0x38, 0x42, 0x34, 0x68, 0x68, 0x31, 0x51, 0x33,
0x50, 0x44, 0x43, 0x33, 0x62, 0x32, 0x6d, 0x44, 0x62, 0x52, 0x5a, 0x58,
0x44, 0x36, 0x57, 0x41, 0x45, 0x56, 0x57, 0x4f, 0x41, 0x37, 0x65, 0x4b,
0x47, 0x49, 0x0a, 0x6a, 0x77, 0x55, 0x62, 0x43, 0x44, 0x4f, 0x71, 0x33,
0x41, 0x6d, 0x61, 0x7a, 0x71, 0x77, 0x45, 0x51, 0x35, 0x41, 0x68, 0x48,
0x4b, 0x50, 0x4c, 0x34, 0x4a, 0x72, 0x46, 0x35, 0x45, 0x53, 0x48, 0x6e,
0x53, 0x57, 0x43, 0x74, 0x6d, 0x62, 0x52, 0x49, 0x67, 0x55, 0x36, 0x7a,
0x53, 0x32, 0x62, 0x4d, 0x67, 0x6e, 0x42, 0x69, 0x4e, 0x62, 0x35, 0x49,
0x58, 0x55, 0x52, 0x7a, 0x2b, 0x33, 0x7a, 0x0a, 0x35, 0x73, 0x42, 0x59,
0x55, 0x76, 0x57, 0x72, 0x52, 0x38, 0x4c, 0x4a, 0x36, 0x44, 0x67, 0x51,
0x50, 0x4c, 0x70, 0x78, 0x63, 0x61, 0x7a, 0x34, 0x38, 0x6d, 0x35, 0x48,
0x47, 0x54, 0x53, 0x50, 0x70, 0x77, 0x5a, 0x35, 0x59, 0x6c, 0x56, 0x4b,
0x4d, 0x63, 0x30, 0x45, 0x77, 0x30, 0x78, 0x73, 0x32, 0x4f, 0x38, 0x77,
0x69, 0x67, 0x3d, 0x3d, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e,
0x44, 0x20, 0x52, 0x53, 0x41, 0x20, 0x50, 0x52, 0x49, 0x56, 0x41, 0x54,
0x45, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a
};
unsigned int server_key_len = 1679;
#define SERVER_CERT server_crt
#define SERVER_CERT_LENGTH server_crt_len
#define SERVER_KEY server_key
#define SERVER_KEY_LENGTH server_key_len
#endif //__REDEFINE_H__
......@@ -629,6 +629,7 @@ int IOTCloudI_Firmware(void)
UINT32 retval = E_FAILED;
cJSON *request = NULL;
SendMsgUnit *sendmsg = NULL;
char version[16];
//check cloud connected status
CheckCloudIfConnected();
......@@ -649,7 +650,8 @@ int IOTCloudI_Firmware(void)
}
//{"version":1000}
cJSON_AddNumberToObject(request, "version", atoi(IOTDev_AdapterVersion()));
memset(version, 0, sizeof(version));
cJSON_AddNumberToObject(request, "version", atoi(IOTDev_AdapterVersion(version)));
cJSON_AddStringToObject(request, "os_version", IOTDEVICE_OSVERSION);
cJSON_AddStringToObject(request, "country_code", IOTDEVICE_COUNTRYCODE);
cJSON_AddItemToObject(sendmsg->root, "request", request);
......
......@@ -136,30 +136,26 @@ int IOTConfRI_CertSize(void)
UINT32 IOTConfRI_HubType(void)
{
return 1000102;//firmInfo.hub_type;hub type (1000101/1000102)
return firmInfo.hub_type;
}
const char *IOTConfRI_HubVendor(void)
{
//return "lenovo rtos wifi";//firmInfo.hub_vendor;
return firmInfo.hub_vendor;
}
UINT32 IOTConfRI_GadgetTypeID(void)
{
//return 203;//firmInfo.gadget_type_id;
return firmInfo.gadget_type_id;
}
const char *IOTConfRI_GadgetVendor(void)
{
//return "lenovo plug";//firmInfo.gadget_vendor;
return firmInfo.gadget_vendor;
}
eRelLevel IOTConfRI_Cloud(void)
{
//return RELEASE_LEVEL_PVT;//firmInfo.cloud;
return firmInfo.cloud;
}
......@@ -173,7 +169,6 @@ const char *IOTConfRI_CloudStr(void)
const char *IOTConfRI_PD(char p[68])
{
char temp = 0;
return "lenovo_rtos_wifi";
memset(p, 0, sizeof(p));
memcpy(&p[0], firmInfo.pd0, 8); memcpy(&p[8], firmInfo.pd1, 8);
memcpy(&p[16], firmInfo.pd2, 8); memcpy(&p[24], firmInfo.pd3, 8);
......@@ -191,7 +186,6 @@ const char *IOTConfRI_PD(char p[68])
const char *IOTConfRI_PK(char p[68])
{
char temp = 0;
//return "5680d0b9613502e2ff8851b7ce914514";
memset(p, 0, sizeof(p));
memcpy(&p[0], firmInfo.pk0, 8); memcpy(&p[8], firmInfo.pk1, 8);
memcpy(&p[16], firmInfo.pk2, 8); memcpy(&p[24], firmInfo.pk3, 8);
......@@ -601,6 +595,72 @@ int IOTConfI_GetRefreshToken(char **token, char **spdid)
return E_SUCCESS;
}
/* ------------------------------------------------- *
* 保存内存的数据,用于热重启
* ------------------------------------------------- */
#define MEMPARAM_DATA_SIZE (512-128)
#define MEMPARAM_MAGICNUM (0xCB267FAA)
typedef struct {
UINT32 magicNumber;
int type;
char data[MEMPARAM_DATA_SIZE];
}MemParam;
int IOTConfMI_Get(int type, void *mem, int size)
{
MemParam memparam;
memset(&memparam, 0, sizeof(MemParam));
if(IOTSysP_GetMemParam((void*)&memparam, size) != E_SUCCESS) {
return E_FAILED;
}
if(memparam.magicNumber != MEMPARAM_MAGICNUM || memparam.type != type){
log_debug0("invalid data\n");
return E_INVALID;
}
if(mem != NULL && size > 0) {
memcpy(mem, memparam.data, size);
}
return E_SUCCESS;
}
int IOTConfMI_Set(int type, void *mem, int size)
{
MemParam memparam;
if(size > MEMPARAM_DATA_SIZE) {
return E_PARAM_ERROR;
}
memset(&memparam, 0, sizeof(MemParam));
memparam.magicNumber = MEMPARAM_MAGICNUM;
memparam.type = type;
if(mem != NULL && size > 0) {
memcpy(memparam.data, mem, size);
}
return IOTSysP_SetMemParam((void*)&memparam, size);
}
int IOTConfMI_Clear(void)
{
MemParam memparam;
memset(&memparam, 0, sizeof(MemParam));
return IOTSysP_SetMemParam((void*)&memparam,sizeof(MemParam));
}
int IOTConf_QuickRestart(void)
{
UINT32 magicNumber = 0x00;
if(IOTSysP_GetMemParam((void *)&magicNumber,sizeof(magicNumber)) != E_SUCCESS)
{
return 0;
}
if(magicNumber == MEMPARAM_MAGICNUM)
{
return 1;
}
return 0;
}
void IOTConfI_Exit(void)
{
if(g_access_token != NULL) {
......@@ -637,8 +697,7 @@ int IOTConfI_Init(void)
return E_FAILED;
}
}
printf ("sdkParam.wifiFlag %d\n", sdkParam.wifiFlag);
g_usrDataSize = sdkParam.usrDataSize <= 0 ? 0 : sdkParam.usrDataSize;
g_wifiFlag = sdkParam.wifiFlag <= 0 ? 0 : sdkParam.wifiFlag;
g_otaFlag = sdkParam.otaFlag <= 0 ? 0 : sdkParam.otaFlag;
......
......@@ -34,5 +34,14 @@ int IOTConfI_SetOtaFlag(int otaResult);
int IOTConfI_SetRefreshToken(char *token, char *spdid);
int IOTConfI_GetRefreshToken(char **token, char **spdid);
/* ====================
* 内存参数读写区(热重启)
*/
#define MEMPARAM_TYPE_NONE (0)
#define MEMPARAM_TYPE_AUTH_FAILED (1)
int IOTConfMI_Get(int type, void *mem, int size);
int IOTConfMI_Set(int type, void *mem, int size);
int IOTConfMI_Clear(void);
#endif //!_CONFIGDATA_HEADER_
......@@ -4,7 +4,7 @@
#include "datatype.h"
#include "rtos.h"
#define VERSION "0.2.0.20180606"
#define VERSION "0.2.4.20180614"
typedef struct {
char uuid[SHORTSTRING];
......
......@@ -13,6 +13,11 @@ void IOTConfI_Exit(void);
int IOTSDK_Init(void)
{
int retval = E_FAILED;
if(IOTConfRI_Cloud()==RELEASE_LEVEL_API) {
IOTDM_LogLevel(-1);
}
retval = IOTConfI_Init();
if(retval == E_SUCCESS) {
int v1,v2,v3;char version[32];
......@@ -25,7 +30,8 @@ int IOTSDK_Init(void)
os_printf("GadgetTypeID: %d\n", IOTConfRI_GadgetTypeID());
os_printf("MAC Address : %s\n", IOTSys_Mac());
os_printf("SDK Version : %s\n", version);
os_printf("ADA Version : %s\n", IOTDev_AdapterVersion());
memset(version, 0, sizeof(version));
os_printf("ADA Version : %s\n", IOTDev_AdapterVersion((version)));
os_printf("Server : %s\n", IOTConfRI_CloudStr());
os_printf("==========================================\n");
} else {
......
......@@ -11,6 +11,7 @@ typedef struct
char serverlevel[8];
char pre_code[128];
char time_zone[32];
char reserved[16];
}ApInfo;
int IOTWifi_Start(int force, ApInfo *apInfo);
......
......@@ -5,10 +5,6 @@
#include "datatype.h"
#include "device.h"
/* Usser configure. */
/* Wifi config 512 bit (0x80000 - 0x200 ).*/
const char *IOTSys_Mac(void);
void IOTSys_Reboot(void);
UINT32 IOTSysP_Random(int max);
......@@ -32,7 +28,7 @@ void IOTDev_Event(DMEvent *event);
DeviceInformation *IOTDev_DeviceInformation(void);
int IOTDev_GetAttribute(UINT32 attribute_id, OCTData *attr);
int IOTDev_ExecuteAction(UINT32 action_id, OCTData *param);
const char * IOTDev_AdapterVersion(void);
const char * IOTDev_AdapterVersion(char *version);
int IOTWebsocket_Init(void);
void IOTWebsocket_Exit(void);
......
......@@ -47,6 +47,7 @@ typedef long long INT64;
#define E_WRITE (-22)
#define E_AUTH_WAIT (-23)
#define E_ACCEPT_TEM_UNAVAIL (-24)
// release version
typedef enum {
......
......@@ -107,54 +107,103 @@ static int content_length(char *buffer,
}
return -1;
}
static char *httpreq_sslreceive(
static int httpreq_sslread(
SSL_Handle handle,
char *recv_buf, UINT32 buff_length)
char *recv_buf, UINT32 buff_length,
int *content_len)
{
int recv_len = 0;
int content_len = 0;
int retval = E_FAILED;
log_assert(handle != NULL);
log_assert(recv_buf != NULL);
log_assert(buff_length > 0);
log_assert(content_len > 0);
recv_len = IOTSSL_Read(handle, buff_length, recv_buf);
if(recv_len < 0) {
if(recv_len <= 0) {
log_error("Read response error(%d)\n", recv_len);
goto failed;
}
log_debug0("recv_data: (%d)%s\n", recv_len, recv_buf);
content_len = content_length(recv_buf, "Content-Length", recv_len);
if(content_len < 0) {
content_len = content_length(recv_buf, "content-cength", recv_len);
if(content_len < 0) {
*content_len = content_length(recv_buf, "Content-Length", recv_len);
if(*content_len < 0) {
*content_len = content_length(recv_buf, "content-cength", recv_len);
if(*content_len < 0) {
log_error("Get Content_Length failed\n");
recv_len = -1;
goto failed;
}
}
log_debug0("Get Content_Length: %d\n", content_len);
if(content_len > 0) {
char *pbody = recv_buf + recv_len - content_len;
log_debug0("Get response body:%s\n", pbody);
return pbody;
log_debug0("Get Content_Length: %d\n", *content_len);
failed:
return recv_len;
}
static int httpreq_sslreadn(
SSL_Handle handle,
int content_len,
int read_sizemax,
writedata_cb cb, void *param)
{
int receive_size;
int receive_reseved = read_sizemax;
char *recv_buff = NULL;
log_assert(handle != NULL);
log_assert(content_len > 0);
log_assert(read_sizemax > 0);
log_assert(cb != NULL);
log_debug0("download file size=%d\n", content_len);
recv_buff = IOTM_Malloc(read_sizemax);
if(recv_buff == NULL) {
log_error("insufficient memory\n");
goto failed;
}
receive_size = (content_len >= read_sizemax)?read_sizemax:content_len;
while(1) {
int received;
// read firmware file context...
memset(recv_buff, 0, read_sizemax);
received = IOTSSL_Readn(handle, receive_size, recv_buff);
if(received <= 0) {
log_error("Read response error(%d)\n", received);
goto failed;
}
log_debug2("read (%d)..\n", received);
cb(recv_buff, received, param);
receive_reseved -= received;
log_debug2("received:%d / %d\n", receive_reseved, content_len);
if(receive_reseved == 0) {
break;
}
log_assert(receive_reseved > 0);
if(receive_reseved >= read_sizemax) {
receive_size = read_sizemax;
} else {
receive_size = receive_reseved;
}
msleep(10);
}
return recv_buf;
return E_SUCCESS;
failed:
return NULL;
return E_FAILED;
}
static int iothttp_request(
const char *server_name, int server_port,
const char *cert, const char *req_url, int req,
char **headers, char *data, int data_len,
char *response)
char *response, int read_sizemax, writedata_cb cb, void *param)
{
#define HTTPS_RECEIVE_BUFFER (1024)
char *recv_buff = NULL;
char *recv_data = NULL;
int recv_len = NULL;
int retval = E_FAILED;
int content_len = 0;
SSL_Handle handle = NULL;
log_assert(req == 0 || req == 1);
......@@ -165,15 +214,16 @@ static int iothttp_request(
// connect
handle = IOTSSL_New(server_name, server_port, cert);
if(handle == NULL) {
return E_FAILED;
log_assert(IOTSSL_GetError() != E_SUCCESS);
return IOTSSL_GetError();
}
// send
// send request
if(httpreq_sslsend(handle, server_name, headers, req_url, req,
data, (data==NULL?0:iots_strlen(data))) != E_SUCCESS) {
log_error("Https send failed\n");
goto failed;
}
// receive
// receive header
recv_buff = IOTM_Malloc(HTTPS_RECEIVE_BUFFER);
if(recv_buff == NULL) {
log_error("insufficient memory\n");
......@@ -181,14 +231,30 @@ static int iothttp_request(
}
memset(recv_buff,0,HTTPS_RECEIVE_BUFFER);
recv_data = httpreq_sslreceive(handle, recv_buff, HTTPS_RECEIVE_BUFFER);
if(recv_data == NULL) {
recv_len = httpreq_sslread(handle, recv_buff, HTTPS_RECEIVE_BUFFER,
&content_len);
if(recv_len <= 0) {
goto failed;
}
log_assert(content_len >= 0);
if(response != NULL) {
iots_strcpy(response, recv_data);
if(content_len > 0) {
char *pbody = recv_buff + recv_len - content_len;
iots_strcpy(response, pbody);
} else {
iots_strcpy(response, recv_buff);
}
}
IOTM_Free(recv_buff);
recv_buff = NULL;
// download files
if(content_len > 0 && read_sizemax > 0) {
if(httpreq_sslreadn(handle, content_len, read_sizemax, cb, param) != E_SUCCESS) {
goto failed;
}
}
retval = E_SUCCESS;
failed:
if(recv_buff != NULL) {
......@@ -208,7 +274,7 @@ int IOTHTTP_Get(
char **headers, char *data, int data_len,
char *response)
{
return iothttp_request(server_name, server_port, cert, req_url, 0, headers, data, data_len, response);
return iothttp_request(server_name, server_port, cert, req_url, 0, headers, data, data_len, response, 0, NULL, NULL);
}
int IOTHTTP_Post(
const char *server_name, int server_port,
......@@ -216,6 +282,13 @@ int IOTHTTP_Post(
char **headers, char *data, int data_len,
char *response)
{
return iothttp_request(server_name, server_port, cert, req_url, 1, headers, data, data_len, response);
return iothttp_request(server_name, server_port, cert, req_url, 1, headers, data, data_len, response, 0, NULL, NULL);
}
int IOTHTTP_Download(
const char *server_name, int server_port, const char *cert,
const char *req_url, int read_sizemax, writedata_cb cb, void *param)
{
return iothttp_request(server_name, server_port, cert, req_url, 0,
NULL, NULL, 0, NULL, read_sizemax, cb, param);
}
......@@ -35,6 +35,7 @@ int IOTQueue_UnitsNumber(void *Qhandle);
int IOTEncode_MD5(const char *inStr, unsigned int inlen, unsigned char *outStr);
int IOTEncode_MD5_Hex(const char *inStr, unsigned int inlen, unsigned char *outStr);
typedef int (*writedata_cb)(void *data, UINT32 size, void *param);
int IOTHTTP_Get(const char *server_name, int server_port,
const char *cert, const char *req_url,
char **headers, char *data, int data_len,
......@@ -43,13 +44,21 @@ int IOTHTTP_Post(const char *server_name, int server_port,
const char *cert, const char *req_url,
char **headers, char *data, int data_len,
char *response);
int IOTHTTP_Download(
const char *server_name, int server_port, const char *cert,
const char *req_url, int read_sizemax, writedata_cb cb, void *param);
typedef void* SSL_Handle;
void IOTSSL_Destroy(SSL_Handle handle);
SSL_Handle IOTSSL_New(const char *server_name, int server_port, const char *cert);
int IOTSSL_GetError(void);
int IOTSSL_Read(SSL_Handle handle, UINT32 read_length, void *read_buf);
int IOTSSL_Readn(SSL_Handle handle, UINT32 read_length, void *read_buf);
int IOTSSL_Write(SSL_Handle handle, char *data, int data_len);
SSL_Handle IOTSSL_ServerNew(const char *server_ip, int server_port,
const char *cert, int cert_len, const char *key, int key_len);
int IOTSSL_ServerAccept(SSL_Handle handle);
int IOTSSL_ServerShutdown(SSL_Handle handle);
#endif //!IOT_COMMON_HEADER
......@@ -16,6 +16,7 @@
#define POLARSSL
#ifdef POLARSSL
#include <platform/platform_stdlib.h>
#include "FreeRTOS.h"
#include "task.h"
#include "datatype.h"
......@@ -37,9 +38,14 @@
#define MODULE_TAG "SSLAPI"
#include "log.h"
#define SSL_CLIENT (1)
#define SSL_SERVER (2)
struct ssl_handle{
int socket;
const char *server;
int type;
struct sockaddr_in sock_addr;
int clientfd; // for accept
#ifdef SSL_OPENSSL
SSL *ssl;
SSL_CTX *ctx;
......@@ -48,17 +54,89 @@ struct ssl_handle{
#ifdef POLARSSL
ssl_context *ssl;
char *ctx;
x509_crt server_x509;
pk_context server_pk;
#endif //!SSL_OPENSSL
};
#define OPENSSL_FRAGMENT_SIZE 2048
static struct ssl_handle *ssl_servernew(const char *server_ip, int server_port)
{
int retval = E_FAILED;
struct ssl_handle *handle = NULL;
socklen_t addr_len = 0;
log_assert(server_ip != NULL);
log_assert(server_port > 0);
handle = IOTM_Malloc(sizeof(struct ssl_handle));
if(handle == NULL) {
goto failed;
}
memset(handle, 0, sizeof(struct ssl_handle));
handle->server = server_ip;
handle->socket = -1;
handle->clientfd = -1;
handle->type = SSL_SERVER;
// socket
handle->socket = socket(AF_INET, SOCK_STREAM,0);
if (handle->socket < 0 ) {
log_error("Create socket failed\n");
goto failed;
}
//============================================
{int opt = 1, flg = 0;
if(setsockopt(handle->socket, SOL_SOCKET, SO_REUSEADDR, (const void*)&opt, sizeof(opt)) < 0) {
log_error("setsockopt SOL_SOCKET SO_REUSEADDR failed\n");
}
flg = fcntl(handle->socket, F_GETFL, 0);
flg |= O_NONBLOCK;
flg |= O_NDELAY;
if(fcntl(handle->socket, F_SETFL, flg) < 0) {
log_error("fctnl failed\n");
}
}
//============================================
memset(&handle->sock_addr, 0, sizeof(handle->sock_addr));
handle->sock_addr.sin_addr.s_addr = inet_addr(server_ip);
handle->sock_addr.sin_family = AF_INET;
handle->sock_addr.sin_port = htons(server_port);
if(bind(handle->socket, (struct sockaddr*)&handle->sock_addr, sizeof(handle->sock_addr)) < 0) {
log_error("bind failed\n");
goto failed;
}
if(listen(handle->socket, 32) < 0) {
log_error("listen failed\n");
goto failed;
}
return handle;
failed:
if(handle != NULL) {
if(handle->socket > 0) {
close(handle->socket);
handle->socket = -1;
}
IOTM_Free(handle);
handle = NULL;
}
return NULL;
}
static struct ssl_handle *ssl_connect(const char *server_name, int server_port)
{
int retval = E_FAILED;
int certSize = 0;
char *cert = NULL;
char server_ip[24];
struct sockaddr_in server_addr;
struct hostent *host = NULL;
struct ssl_handle *handle = NULL;
......@@ -66,12 +144,15 @@ static struct ssl_handle *ssl_connect(const char *server_name, int server_port)
log_assert(server_name != NULL);
log_assert(server_port > 0);
handle = (struct ssl_handle*)IOTM_Malloc(sizeof(struct ssl_handle));
handle = IOTM_Malloc(sizeof(struct ssl_handle));
if(handle == NULL) {
goto failed;
}
memset(handle, 0, sizeof(struct ssl_handle));
handle->server = server_name;
handle->socket = -1;
handle->clientfd = -1;
handle->type = SSL_CLIENT;
// get host ip
host = gethostbyname(server_name);
if(host == NULL) {
......@@ -88,11 +169,11 @@ static struct ssl_handle *ssl_connect(const char *server_name, int server_port)
goto failed;
}
// connect
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(server_port);
server_addr.sin_addr.s_addr = inet_addr(server_ip);
if(connect(handle->socket,(struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
memset(&handle->sock_addr, 0, sizeof(handle->sock_addr));
handle->sock_addr.sin_family = AF_INET;
handle->sock_addr.sin_port = htons(server_port);
handle->sock_addr.sin_addr.s_addr = inet_addr(server_ip);
if(connect(handle->socket,(struct sockaddr *)&handle->sock_addr, sizeof(handle->sock_addr)) < 0) {
log_error("Connect to socket server failed\n");
goto failed;
}
......@@ -110,6 +191,12 @@ failed:
return NULL;
}
static int g_ssl_errno = E_FAILED;
int IOTSSL_GetError(void)
{
return g_ssl_errno;
}
#ifdef SSL_OPENSSL
#define FRAGMENT_SIZE (8192)
......@@ -119,10 +206,17 @@ void IOTSSL_Destroy(SSL_Handle handle)
log_debug0("[%s][%d][size=%u]\n",__FUNCTION__,__LINE__,IOTSysP_GetFreeHeapSize());
struct ssl_handle *ssl = handle;
if(ssl != NULL) {
if(ssl->type == SSL_SERVER) {
IOTSSL_ServerShutdown(ssl);
}
if(ssl->ssl != NULL) {
SSL_free(ssl->ssl);
ssl->ssl = NULL;
}
if(ssl->clientfd > 0) {
close(ssl->clientfd);
ssl->clientfd = -1;
}
if(ssl->ctx != NULL) {
SSL_CTX_free(ssl->ctx);
ssl->ctx = NULL;
......@@ -140,7 +234,7 @@ SSL_Handle IOTSSL_New(const char *server_name, int server_port, const char *cert
{
struct ssl_handle *handle = NULL;
int retval = E_FAILED;
int count = 0;
if(server_name == NULL || server_port < 0) {
log_error("Parameter error\n");
retval = E_PARAM_ERROR;
......@@ -174,11 +268,18 @@ SSL_Handle IOTSSL_New(const char *server_name, int server_port, const char *cert
SSL_CTX_set_default_read_buffer_len(handle->ctx, FRAGMENT_SIZE);
// initialize ssl
handle->ssl = SSL_new(handle->ctx);
log_debug0("[%s][%d][size=%u]\n",__FUNCTION__,__LINE__,IOTSysP_GetFreeHeapSize());
do{
msleep(20);
log_debug0("create ssl count:%d\n", count);
handle->ssl = SSL_new(handle->ctx);
}while(handle->ssl == NULL && ++count <= 3);
if(handle->ssl == NULL ) {
log_error("Create ssl failed\n");
retval = E_NO_MEMORY;
goto failed;
}
log_debug0("[%s][%d][size=%u]\n",__FUNCTION__,__LINE__,IOTSysP_GetFreeHeapSize());
SSL_set_fd(handle->ssl, handle->socket);
if(SSL_connect(handle->ssl) < 0) {
......@@ -186,10 +287,12 @@ SSL_Handle IOTSSL_New(const char *server_name, int server_port, const char *cert
goto failed;
}
log_debug0("[%s][%d][size=%u]\n",__FUNCTION__,__LINE__,IOTSysP_GetFreeHeapSize());
g_ssl_errno = E_SUCCESS;
return handle;
failed:
IOTSSL_Destroy(handle);
log_debug0("[%s][%d][size=%u]\n",__FUNCTION__,__LINE__,IOTSysP_GetFreeHeapSize());
g_ssl_errno = retval;
return NULL;
}
......@@ -279,6 +382,143 @@ failed:
return retval;
}
SSL_Handle IOTSSL_ServerNew(const char *server_ip, int server_port,
const char *cert, int cert_len, const char *key, int key_len)
{
struct ssl_handle *handle = NULL;
int retval = E_FAILED;
if(server_ip == NULL || server_port < 0) {
log_error("Parameter error\n");
goto failed;
}
log_debug0("[%s][%d][size=%u]\n",__FUNCTION__,__LINE__,IOTSysP_GetFreeHeapSize());
// ssl handle, socket
handle = ssl_servernew(server_ip, server_port);
if(handle == NULL) {
goto failed;
}
log_assert(handle->socket >= 0);
log_assert(handle->ssl == NULL);
log_assert(handle->ctx == NULL);
// initialize ctx
SSL_library_init();
handle->ctx = SSL_CTX_new(TLSv1_1_server_method());
if(handle->ctx == NULL) {
log_error("SSL_CTX_new failed(%d)\n", errno);
goto failed;
}
if(cert != NULL && !SSL_CTX_use_certificate_ASN1(handle->ctx, cert_len, cert)) {
goto failed;
}
if(key != NULL && !SSL_CTX_use_PrivateKey_ASN1(0, handle->ctx, key, key_len)) {
goto failed;
}
SSL_CTX_set_default_read_buffer_len(handle->ctx, OPENSSL_FRAGMENT_SIZE);
retval = E_SUCCESS;
failed:
if(retval != E_SUCCESS && handle != NULL) {
IOTSSL_Destroy(handle);
handle = NULL;
}
return handle;
}
int IOTSSL_ServerAccept(SSL_Handle handle)
{
struct ssl_handle *_handle = handle;
socklen_t addr_len = 0;
int retval = E_FAILED;
if(handle == NULL) {
return E_PARAM_NULL;
}
if(_handle->socket < 0 ||
_handle->clientfd >= 0 ||
_handle->ctx == NULL) {
return E_PARAM_ERROR;
}
log_assert(_handle->socket >= 0);
log_assert(_handle->clientfd < 0);
log_assert(_handle->ctx != NULL);
// initialize ssl
if(_handle->ssl != NULL) {
SSL_free(_handle->ssl);
_handle->ssl = NULL;
}
_handle->ssl = SSL_new(_handle->ctx);
if(_handle->ssl == NULL) {
log_error("SSL_new failed(%d)\n", errno);
goto failed;
}
memset(&_handle->sock_addr, 0, sizeof(_handle->sock_addr));
_handle->clientfd = accept(_handle->socket, (struct sockaddr*)&_handle->sock_addr, &addr_len);
if (_handle->clientfd < 0) {
if(errno == 11) {
retval = E_ACCEPT_TEM_UNAVAIL;
} else {
log_error("accept failed(%d)\n", errno);
}
goto failed;
}
//============================================
{
int setval=1, keepAlive=1, keepIdle=5, keepInterval=1, keepCount=3;
if(setsockopt(_handle->clientfd, IPPROTO_TCP, TCP_NODELAY ,(const char *)&setval, sizeof(setval)) != 0 ||
setsockopt(_handle->clientfd,SOL_SOCKET , SO_KEEPALIVE ,(const char *)&keepAlive , sizeof(keepAlive)) != 0 ||
setsockopt(_handle->clientfd,IPPROTO_TCP, TCP_KEEPIDLE ,(const char *)&keepIdle , sizeof(keepIdle)) != 0 ||
setsockopt(_handle->clientfd,IPPROTO_TCP, TCP_KEEPINTVL,(const char *)&keepInterval, sizeof(keepInterval)) != 0 ||
setsockopt(_handle->clientfd,IPPROTO_TCP, TCP_KEEPCNT ,(const char *)&keepCount , sizeof(keepCount))) {
log_error("setsockopt failed(%d)\n", errno);
goto failed;
}
}
//============================================
SSL_set_fd(_handle->ssl, _handle->clientfd);
if(SSL_accept(_handle->ssl) == 0) {
log_error("SSL_accept failed(%d)\n", errno);
goto failed;
} else {
struct timeval timeout = {10,0};
setsockopt(_handle->clientfd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(struct timeval));
}
return E_SUCCESS;
failed:
if(_handle->ssl != NULL) {
SSL_free(_handle->ssl);
_handle->ssl = NULL;
}
if(_handle->clientfd > 0) {
close(_handle->clientfd);
_handle->clientfd = -1;
}
return retval;
}
int IOTSSL_ServerShutdown(SSL_Handle handle)
{
struct ssl_handle *_handle = handle;
if(_handle->type == SSL_SERVER && _handle->ssl != NULL) {
SSL_shutdown(_handle->ssl);
SSL_free(_handle->ssl);
_handle->ssl = NULL;
}
if(_handle->type == SSL_SERVER && _handle->clientfd > 0) {
close(_handle->clientfd);
_handle->clientfd = -1;
}
return E_SUCCESS;
}
#endif //!SSL_OPENSSL
......@@ -294,6 +534,44 @@ static int _random_func(void *p_rng, unsigned char *output, size_t output_len)
ssl_context g_stSSl;
static struct ssl_handle *polarssl_servernew(const char *server_ip, int server_port)
{
int retval = E_FAILED;
struct ssl_handle *handle = NULL;
socklen_t addr_len = 0;
log_assert(server_ip != NULL);
log_assert(server_port > 0);
handle = IOTM_Malloc(sizeof(struct ssl_handle));
if(handle == NULL) {
goto failed;
}
memset(handle, 0, sizeof(struct ssl_handle));
handle->server = server_ip;
handle->socket = -1;
handle->clientfd = -1;
handle->type = SSL_SERVER;
// socket
if((retval = net_bind(&handle->socket, NULL, server_port)) != 0) {
log_error(" failed\n ! net_bind returned %d\n\n", retval);
goto failed;
}
return handle;
failed:
if(handle != NULL) {
if(handle->socket > 0) {
close(handle->socket);
handle->socket = -1;
}
IOTM_Free(handle);
handle = NULL;
}
return NULL;
}
void IOTSSL_Destroy(SSL_Handle handle)
......@@ -301,11 +579,16 @@ void IOTSSL_Destroy(SSL_Handle handle)
log_debug0("[%s][%d][size=%u]\n",__FUNCTION__,__LINE__,IOTSysP_GetFreeHeapSize());
struct ssl_handle *ssl = handle;
if(ssl != NULL) {
ssl_close_notify(&g_stSSl);
ssl_free(&g_stSSl);
if(ssl->type == SSL_SERVER) {
IOTSSL_ServerShutdown(ssl);
}
ssl_close_notify(ssl->ssl);
if (NULL != ssl->ssl) {
free(ssl->ssl);
ssl_free(ssl->ssl);
}
if (NULL != ssl->ssl) {
IOTM_Free(ssl->ssl);
}
if(ssl->socket > 0) {
......@@ -335,25 +618,26 @@ SSL_Handle IOTSSL_New(const char *server_name, int server_port, const char *cert
log_assert(handle->socket >= 0);
memory_set_own(pvPortMalloc, vPortFree);
#if 0
handle->ssl = malloc(sizeof(ssl_context));
#if 1
handle->ssl = IOTM_Malloc(sizeof(ssl_context));
if (NULL == handle->ssl) {
log_error("IOTM_Malloc failed\n");
goto failed;
}
memset(handle->ssl, 0, sizeof(ssl_context));
#endif
if((retval = ssl_init(&g_stSSl)) != 0){
if((retval = ssl_init(handle->ssl)) != 0){
printf("\n[WSCLIENT] ERROR: ssl_init %d\n", retval);
goto failed;
}
ssl_set_endpoint(&g_stSSl, 0);
ssl_set_authmode(&g_stSSl, 0);
ssl_set_rng(&g_stSSl, _random_func, NULL);
ssl_set_bio(&g_stSSl, net_recv, &(handle->socket), net_send, &(handle->socket));
ssl_set_endpoint(handle->ssl, 0);
ssl_set_authmode(handle->ssl, 0);
ssl_set_rng(handle->ssl, _random_func, NULL);
ssl_set_bio(handle->ssl, net_recv, &(handle->socket), net_send, &(handle->socket));
if((retval = ssl_handshake(&g_stSSl)) != 0) {
if((retval = ssl_handshake(handle->ssl)) != 0) {
printf(" failed ssl_handshake returned %d\n\n", retval);
goto failed;
} else {
......@@ -412,7 +696,7 @@ int IOTSSL_Read(
}
do {
nread = ssl_read(&g_stSSl, read_buf, read_length);
nread = ssl_read(sslhandle->ssl, read_buf, read_length);
if(nread >= 0) {
break;
}
......@@ -437,7 +721,7 @@ int IOTSSL_Write(SSL_Handle handle, char *data, int data_len)
goto failed;
}
//printf("IOTSSL_Write %s(len=%d)\n", data, data_len);
if(ssl_write(&g_stSSl, data, data_len) != data_len) {
if(ssl_write(sslhandle->ssl, data, data_len) != data_len) {
log_error("SSL_write failed\n");
goto failed;
}
......@@ -446,5 +730,178 @@ failed:
return retval;
}
SSL_Handle IOTSSL_ServerNew(const char *server_ip, int server_port,
const char *cert, int cert_len, const char *key, int key_len)
{
struct ssl_handle *handle = NULL;
int retval = E_FAILED;
x509_crt server_x509;
pk_context server_pk;
if(server_ip == NULL || server_port < 0) {
log_error("Parameter error\n");
goto failed;
}
log_debug0("[%s][%d][size=%u]\n",__FUNCTION__,__LINE__,IOTSysP_GetFreeHeapSize());
// ssl handle, socket
handle = polarssl_servernew(server_ip, server_port);
if(handle == NULL) {
goto failed;
}
log_assert(handle->socket >= 0);
log_assert(handle->ssl == NULL);
memory_set_own(pvPortMalloc, vPortFree);
memset(&(handle->server_x509), 0, sizeof(x509_crt));
memset(&(handle->server_pk), 0, sizeof(pk_context));
memset(handle->ssl, 0, sizeof(ssl_context));
/*
* 1. Prepare the certificate and key
*/
printf("\n\r . Preparing the certificate and key...");
x509_crt_init(&(handle->server_x509));
pk_init(&(handle->server_pk));
if((retval = x509_crt_parse(&(handle->server_x509), (const unsigned char *)test_srv_crt, strlen(test_srv_crt))) != 0) {
log_error(" failed\n ! x509_crt_parse returned %d\n\n", retval);
goto failed;
}
if((retval = x509_crt_parse(&(handle->server_x509), (const unsigned char *)test_ca_list, strlen(test_ca_list))) != 0) {
log_error(" failed\n ! x509_crt_parse returned %d\n\n", retval);
goto failed;
}
if((retval = pk_parse_key(&(handle->server_pk), test_srv_key, strlen(test_srv_key), NULL, 0)) != 0) {
log_error(" failed\n ! pk_parse_key returned %d\n\n", retval);
goto failed;
}
retval = E_SUCCESS;
failed:
if(retval != E_SUCCESS && handle != NULL) {
IOTSSL_Destroy(handle);
handle = NULL;
}
return handle;
}
int IOTSSL_ServerAccept(SSL_Handle handle)
{
struct ssl_handle *_handle = handle;
socklen_t addr_len = 0;
int retval = E_FAILED;
log_error("enter IOTSSL_ServerAccept\n");
if(handle == NULL) {
log_error("enter IOTSSL_ServerAccept 1\n");
return E_PARAM_NULL;
}
if(_handle->socket < 0 ||
_handle->clientfd >= 0) {
log_error("enter IOTSSL_ServerAccept 2\n");
return E_PARAM_ERROR;
}
log_assert(_handle->socket >= 0);
log_assert(_handle->clientfd < 0);
// initialize ssl
if(_handle->ssl != NULL) {
ssl_free(_handle->ssl);
_handle->ssl = NULL;
}
_handle->ssl = IOTM_Malloc(sizeof(ssl_context));
if (NULL == _handle->ssl) {
log_error("IOTM_Malloc failed\n");
goto failed;
}
if((retval = ssl_init(_handle->ssl)) != 0) {
printf(" failed\n ! ssl_init returned %d\n\n", retval);
goto failed;
}
ssl_set_endpoint(_handle->ssl, SSL_IS_SERVER);
ssl_set_ca_chain(_handle->ssl, _handle->server_x509.next, NULL, NULL);
ssl_set_authmode(_handle->ssl, SSL_VERIFY_NONE);
ssl_set_rng(_handle->ssl, _random_func, NULL);
if (retval = net_accept(_handle->socket, &_handle->clientfd, NULL) == 0) {
log_debug0("\n\r . A client is connecting\n\r");
} else {
log_error("enter IOTSSL_ServerAccept 3\n");
}
if (_handle->clientfd < 0) {
if(errno == 11) {
retval = E_ACCEPT_TEM_UNAVAIL;
} else {
log_error("accept failed(%d)\n", errno);
}
goto failed;
}
//============================================
{
int setval=1, keepAlive=1, keepIdle=5, keepInterval=1, keepCount=3;
if(setsockopt(_handle->clientfd, IPPROTO_TCP, TCP_NODELAY ,(const char *)&setval, sizeof(setval)) != 0 ||
setsockopt(_handle->clientfd,SOL_SOCKET , SO_KEEPALIVE ,(const char *)&keepAlive , sizeof(keepAlive)) != 0 ||
setsockopt(_handle->clientfd,IPPROTO_TCP, TCP_KEEPIDLE ,(const char *)&keepIdle , sizeof(keepIdle)) != 0 ||
setsockopt(_handle->clientfd,IPPROTO_TCP, TCP_KEEPINTVL,(const char *)&keepInterval, sizeof(keepInterval)) != 0 ||
setsockopt(_handle->clientfd,IPPROTO_TCP, TCP_KEEPCNT ,(const char *)&keepCount , sizeof(keepCount))) {
log_error("setsockopt failed(%d)\n", errno);
goto failed;
}
}
//============================================
ssl_set_bio(_handle->ssl, net_recv, &(_handle->clientfd), net_send, &(_handle->clientfd));
if((retval = ssl_set_own_cert(_handle->ssl, &(_handle->server_x509), &(_handle->server_pk))) != 0) {
printf(" failed\n ! ssl_set_own_cert returned %d\n\n", retval);
goto failed;
}
if((retval = ssl_handshake(_handle->ssl)) != 0) {
printf(" failed\n ! ssl_handshake returned %d\n\n", retval);
goto failed;
} else {
struct timeval timeout = {10,0};
setsockopt(_handle->clientfd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(struct timeval));
}
log_debug0("Ssl connect OK\n");
return E_SUCCESS;
failed:
if(_handle->ssl != NULL) {
ssl_free(_handle->ssl);
_handle->ssl = NULL;
}
if(_handle->clientfd > 0) {
close(_handle->clientfd);
_handle->clientfd = -1;
}
return retval;
}
int IOTSSL_ServerShutdown(SSL_Handle handle)
{
struct ssl_handle *_handle = handle;
if(_handle->type == SSL_SERVER && _handle->ssl != NULL) {
ssl_close_notify(_handle->ssl);
ssl_free(_handle->ssl);
IOTM_Free(_handle->ssl);
_handle->ssl = NULL;
}
if(_handle->type == SSL_SERVER && _handle->clientfd > 0) {
close(_handle->clientfd);
_handle->clientfd = -1;
}
return E_SUCCESS;
}
#endif //!SSL_OPENSSL
......@@ -26,6 +26,7 @@
#include "log.h"
#include "md5c.h"
#include "commcloud.h"
#include "iotcommon.h"
#include "security_func.h"
......@@ -45,42 +46,16 @@
#define OTA_FW_RECV_BUFLEN (512)
#define HTTPS_CONTEXT_MAXLEN (512)
#define FRAGMENT_SIZE (8192)
#define HTTPS 1
#define OTA_SERVER_PORT (443)
int IOTConfRI_GetCert(void *buf,int bufSize);
int IOTConfRI_CertSize(void);
static int g_firmFlag = E_SUCCESS;
static const char *g_check_version_url = "/v2.0/ota/check_update_version";
//static const char *g_doneload_result_url = "/v2.0/third_download_result";
//static const char *g_upgrade_rusult_url = "/v2.0/third_update_result";
static const int g_server_port = 443;
static int g_socket = -1;
ssl_context stSSL;
char *strip_blank(char *buf, int len)
{
char *buf1 = NULL;
int i, j;
len += 1; // '\0'
buf1 = malloc(len);
if (!buf1)
return NULL;
// Strip the blanks.
memset(buf1, 0, len);
for (i = 0, j = 0; i < len; i++) {
if (buf[i] != 0x20)
buf1[j++] = buf[i];
}
memcpy(buf, buf1, j+1);
free(buf1);
return buf;
}
//static SSL_CTX *g_ctx = NULL;
typedef enum{e_get,e_post} g_req;
......@@ -92,11 +67,8 @@ void TimerCallBack(void *arg)
IOTSys_Reboot();
}
static void iotota_Disconnect(void);
static int Get_Server_IP(const char *server_name/*in*/,char * server_ip/*out*/)
{
#if 1
struct hostent *host = NULL;
host = gethostbyname(server_name);
if(host != NULL )
......@@ -106,118 +78,74 @@ static int Get_Server_IP(const char *server_name/*in*/,char * server_ip/*out*/)
return E_SUCCESS;
}
log_error("Get the ip of\" %s\" failed\n",server_name);
#endif
return E_FAILED;
}
static int _random_func(void *p_rng, unsigned char *output, size_t output_len)
{
rtw_get_random_bytes(output, output_len);
return 0;
}
static int iotota_Connect(const char *server_name, int server_port)
static SSL_Handle iotota_Connect(const char *server_name, int server_port)
{
int retval = E_FAILED;
int cert_len = 0;
char server_ip[24];
struct sockaddr_in server_addr;
#if HTTPS
int iRet = -1;
memory_set_own(pvPortMalloc, vPortFree);
#endif
int certSize = 0;
char *cert = NULL;
SSL_Handle handle = NULL;
if(server_name == NULL || server_port < 0)
{
log_error("Parameter error\n");
goto failed;
}
memset(server_ip,0,sizeof(server_ip));
memset(&server_addr, 0, sizeof(server_addr));
if(Get_Server_IP(server_name,server_ip) < 0)
certSize = IOTConfRI_CertSize();
if(certSize <= 0 || certSize >= 4096)
{
log_error("Get ota server ip failed\n");
log_error("Get CertSize error(%d)\n",certSize);
goto failed;
}
g_socket = socket(AF_INET, SOCK_STREAM,0);
if (g_socket < 0 )
cert = IOTM_Malloc(certSize + 1);
if(cert == NULL)
{
log_error("Create socket failed\n");
log_error("insufficient memory\n");
goto failed;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(server_port);
server_addr.sin_addr.s_addr = inet_addr(server_ip);
if(connect(g_socket,(struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
memset(cert,0,certSize + 1);
if(IOTConfRI_GetCert(cert,certSize + 1) != E_SUCCESS)
{
log_error("Connect to socket server failed\n");
log_error("Get root cert failed\n");
goto failed;
}
#if HTTPS
printf("Available heap [0x%x]\n", xPortGetFreeHeapSize());
if((iRet = ssl_init(&stSSL)) != 0){
printf("\n[WSCLIENT] ERROR: ssl_init %d\n", iRet);
goto failed;
}
ssl_set_endpoint(&stSSL, 0);
ssl_set_authmode(&stSSL, 0);
ssl_set_rng(&stSSL, _random_func, NULL);
ssl_set_bio(&stSSL, net_recv, &g_socket, net_send, &g_socket);
if((iRet = ssl_handshake(&stSSL)) != 0) {
printf(" failed ssl_handshake returned %d\n\n", iRet);
goto failed;
} else {
printf(" SSL handshake OK\n\n");
}
#endif
retval = E_SUCCESS;
return retval;
failed:
iotota_Disconnect();
return retval;
}
handle = IOTSSL_New(server_name, server_port, cert);
if(handle == NULL) {
log_error("IOTSSL_New failed\n");
goto failed;
}
static void iotota_Disconnect(void)
{
log_debug0("[%s][%d][size=%u]\n",__FUNCTION__,__LINE__,IOTSysP_GetFreeHeapSize());
log_debug0("SSL connect to server ok\n");
retval = E_SUCCESS;
if(&stSSL != NULL) {
ssl_close_notify(&stSSL);
ssl_free(&stSSL);
failed:
if(cert != NULL) {
IOTM_Free(cert);
cert = NULL;
}
if(g_socket >= 0) {
close(g_socket);
g_socket = -1;
if(retval != E_SUCCESS && handle != NULL) {
IOTSSL_Destroy(handle);
handle = NULL;
}
return handle;
}
static int Https_Send(ssl_context *ssl,const char *server_name,const char *url, char *data,int data_len,g_req req )
static int Https_Send(SSL_Handle handle,const char *server_name,const char *url, char *data,int data_len,g_req req )
{
char *context = NULL;
char *tempbuf = NULL;
int tempsize = 256;
int retval = E_FAILED;
if(NULL == url || NULL == server_name )
{
log_error("Parameter error\n");
goto failed;
}
log_assert(handle != NULL);
log_assert(url != NULL);
log_assert(server_name != NULL);
context = (char *)IOTM_Malloc(HTTPS_CONTEXT_MAXLEN);
if(NULL == context) {
......@@ -264,7 +192,7 @@ static int Https_Send(ssl_context *ssl,const char *server_name,const char *url,
}
log_debug0("\nHttps Request:\n%s\r\n",context);
if(ssl_write(ssl, context, iots_strlen(context)) != iots_strlen(context))
if(IOTSSL_Write(handle,context, iots_strlen(context)) != E_SUCCESS)
{
log_error("Https client send failed\n");
goto failed;
......@@ -284,7 +212,7 @@ failed:
return retval;
}
static int SSL_Readn(ssl_context *ssl, void *pbuf,unsigned int leng_byte,unsigned int maxlen)
static int SSL_Readn(SSL_Handle handle, void *pbuf,unsigned int leng_byte,unsigned int maxlen)
{
int nleft = 0; //usigned int 剩余未读取的字节数
int nread = 0; //int 实际读到的字节数
......@@ -295,25 +223,21 @@ static int SSL_Readn(ssl_context *ssl, void *pbuf,unsigned int leng_byte,unsigne
while (nleft > 0)
{
nread = ssl_read(ssl, ptr, nleft);
nread = IOTSSL_Read(handle,nleft,ptr);
if(nread < 0)
{
log_debugx("get error code(%d)\n",nread);
if (errno == EINTR) {
nread = 0;
}
else {
HasRead = 0;
return -1;
}
} else if(nread == 0) {
HasRead = 0;
return -1;
}
else if(nread == 0)
{
break;
}
nleft -= nread;
ptr += nread;
HasRead += nread;
if(HasRead == maxlen) {
if(HasRead == maxlen)
{
HasRead = 0;
break;
}
......@@ -332,7 +256,6 @@ static int Get_Content_Length(char *buffer,const char *substr,int length)
char *pbuf = buffer;
int len = length;
int Content_Length = 0;
//strip_blank(buffer, length);
while(1)
{
ptr = memchr(pbuf,substr[0],len);
......@@ -353,7 +276,7 @@ static int Get_Content_Length(char *buffer,const char *substr,int length)
}
}
static int iotota_UpgradeStart(ssl_context *ssl,const char *server_name,const char *url,const char *md5)
static int iotota_UpgradeStart(SSL_Handle handle,const char *server_name,const char *url,const char *md5)
{
int retval = E_FAILED;
int read_len = 0;
......@@ -362,23 +285,21 @@ static int iotota_UpgradeStart(ssl_context *ssl,const char *server_name,const ch
int Content_Length = 0;
char *data_buf = NULL;
char *tempbuf = NULL;
MD5_CTX ctx;
//timer_t timer;
if(url == NULL || ssl == NULL || server_name == NULL || md5 == NULL )
{
log_error("parameter error\n");
goto failed;
}
if(Https_Send(ssl,server_name,url, NULL,0,e_get) != E_SUCCESS)
log_assert(handle != NULL);
log_assert(url != NULL);
log_assert(server_name != NULL);
log_assert(md5 != NULL);
if(Https_Send(handle,server_name,url, NULL,0,e_get) != E_SUCCESS)
{
log_error(" send OTA request failed\n");
goto failed;
}
log_debug0("Request success\n");
//memset(&timer,0,sizeof(timer));
data_buf = (char *)IOTM_Malloc(OTA_FW_RECV_BUFLEN);
data_buf = IOTM_Malloc(OTA_FW_RECV_BUFLEN);
if(data_buf == NULL)
{
log_error("insufficient memory\n");
......@@ -389,7 +310,7 @@ static int iotota_UpgradeStart(ssl_context *ssl,const char *server_name,const ch
IOTOtaP_ReceiveDataPrepare();
//recv https head
head_len = ssl_read(ssl,data_buf,OTA_FW_RECV_BUFLEN - 1);
head_len = IOTSSL_Read(handle,OTA_FW_RECV_BUFLEN-1,data_buf);
if (head_len>0)
{
log_debug2("=== head len:%d ===\n",head_len);
......@@ -412,7 +333,10 @@ static int iotota_UpgradeStart(ssl_context *ssl,const char *server_name,const ch
goto failed;
}
unsigned char sout[MD5_HASHBYTES];
MD5_CTX ctx;
unsigned char sout[MD5_HASHBYTES];
memset(&ctx,0,sizeof(ctx));
memset(&sout,0,MD5_HASHBYTES);
MD5Init(&ctx);
......@@ -420,9 +344,9 @@ static int iotota_UpgradeStart(ssl_context *ssl,const char *server_name,const ch
{
// read firmware file context...
memset(data_buf,0,OTA_FW_RECV_BUFLEN);
//IOTDM_TimerStart(&timer,5000,TimerCallBack,NULL,0);
read_len = SSL_Readn(ssl, data_buf,OTA_FW_RECV_BUFLEN - 1,Content_Length);
//IOTDM_TimerStop(&timer);
//IOTSysP_TimerStart(&timer,5000,TimerCallBack,NULL,0);
read_len = SSL_Readn(handle, data_buf,OTA_FW_RECV_BUFLEN - 1,Content_Length);
//IOTSysP_TimerStop(&timer);
log_debug2("read (%d)..\n", read_len);
if( read_len > 0 )
{
......@@ -480,9 +404,7 @@ failed:
return retval;
}
//static int iotota_CheckUpdateVersion(ssl_context *ssl,const char *server_name,const char *url,char **pbuf/*out*/,char *md5/*out*/)
static int iotota_CheckUpdateVersion(ssl_context *ssl,const char *server_name,const char *url,UINT32 version,char **pbuf/*out*/,char *md5/*out*/)
static int iotota_CheckUpdateVersion(SSL_Handle handle,const char *server_name,const char *url,UINT32 version,char **pbuf/*out*/,char *md5/*out*/)
{
int Content_Length = 0;
int new_version = 0;
......@@ -497,11 +419,11 @@ static int iotota_CheckUpdateVersion(ssl_context *ssl,const char *server_name,co
char *temp = NULL;
int retval = E_FAILED;
if (ssl == NULL || server_name == NULL || url == NULL || pbuf == NULL || md5 == NULL)
{
log_error("Parameter error\n");
goto failed;
}
log_assert(handle != NULL);
log_assert(server_name != NULL);
log_assert(url != NULL);
log_assert(pbuf != NULL);
log_assert(md5 != NULL);
root = cJSON_CreateObject();
if(root == NULL)
......@@ -535,7 +457,7 @@ static int iotota_CheckUpdateVersion(ssl_context *ssl,const char *server_name,co
cJSON_Delete(root);root = NULL;
int data_len = iots_strlen(data);
log_debug0("Json data len:%d\n",data_len);
if(Https_Send(ssl,server_name,url,data,iots_strlen(data),e_post) != E_SUCCESS)
if(Https_Send(handle,server_name,url,data,iots_strlen(data),e_post) != E_SUCCESS)
{
log_error("Https send failed\n");
goto failed;
......@@ -549,7 +471,7 @@ static int iotota_CheckUpdateVersion(ssl_context *ssl,const char *server_name,co
goto failed;
}
memset(recv_buf,0,1024 * 1);
int recv_len = ssl_read(ssl,recv_buf,1024 * 1);
int recv_len = IOTSSL_Read(handle,1024 * 1,recv_buf);
if (recv_len < 0)
{
log_error("Read response error\n");
......@@ -737,7 +659,7 @@ static int iotota_ReportDoneloadResult(SSL *ssl,const char *server_name,const ch
goto failed;
}
memset(recv_buf,0,1024 * 1);
int recv_len = ssl_read(ssl,recv_buf,1024 * 1);
int recv_len = SSL_read(ssl,recv_buf,1024 * 1);
if (recv_len < 0)
{
log_error("Read response failed\n");
......@@ -951,13 +873,15 @@ int IOTOTAI_StatusCheck(void)
{
UINT32 fwVersion = 0;
int otaflag = 0;
char version[16];
otaflag = IOTConfI_GetOtaFlag();
log_debug0("ota flag:%d\n",otaflag);
if(otaflag > 0 && otaflag <= 3)
{
log_debug0("Go on to ota %dtimes\n", otaflag);
if(sscanf(IOTDev_AdapterVersion(),"%u",&fwVersion) == -1)
memset(version, 0, sizeof(version));
if(sscanf(IOTDev_AdapterVersion(version),"%u",&fwVersion) == -1)
{
log_error("Change version to UINT32 failed\n");
fwVersion = 0;
......@@ -978,6 +902,7 @@ int IOTOTAI_Start(UINT32 version)
char *fw_server_url = NULL; // "webpage/rtk/ota_all.bin";
char *md5 = NULL;
const char *server_name = NULL;
SSL_Handle handle = NULL;
DMEvent event;
memset(&event,0,sizeof(event));
......@@ -992,18 +917,19 @@ int IOTOTAI_Start(UINT32 version)
}
IOTOtaP_Prepare();
//ready source
md5 = (char *)IOTM_Malloc(OTA_FW_MD5_BUFLEN);
md5 = IOTM_Malloc(OTA_FW_MD5_BUFLEN);
if(md5 == NULL )
{
log_error("insufficient memory\n");
goto failed;
}
memset(md5,0,OTA_FW_MD5_BUFLEN);
server_name = IOTCloudI_GetOtaUrl();
log_assert(server_name != NULL);
server_name = IOTCloudI_GetOtaUrl();
log_assert(server_name != NULL);
//https connect 1st
if(iotota_Connect(server_name,g_server_port) != E_SUCCESS)
handle = iotota_Connect(server_name,OTA_SERVER_PORT);
if(handle == NULL)
{
log_error(" OTA connect failed\n");
goto failed;
......@@ -1011,7 +937,7 @@ int IOTOTAI_Start(UINT32 version)
log_debug0("OTA connect success\n");
//POST request,fetch host url and md5,save host+url to pbuf
if(iotota_CheckUpdateVersion(&stSSL,server_name,g_check_version_url,version,&pbuf,md5) != E_SUCCESS)
if(iotota_CheckUpdateVersion(handle,server_name,g_check_version_url,version,&pbuf,md5) != E_SUCCESS)
{
log_error("Check OTA fireware version failedd\n");
goto failed;
......@@ -1024,24 +950,23 @@ int IOTOTAI_Start(UINT32 version)
log_error("Parse url failed\n");
goto failed;
}
log_debug0("firmware host_name:%s\n",fw_server_name);
log_debug0("firmware url:%s\n",fw_server_url);
log_debug0("SSL disconnect......\n");
iotota_Disconnect();
//fw_server_name="deviot.lenovo.com.cn";
//fw_server_url="webpage/rtk/ota_all.bin";
log_debug0("firmware host_name:%s,url:%s\n",fw_server_name,fw_server_url);
IOTSSL_Destroy(handle);
handle = NULL;
//https connect 2st
if(iotota_Connect(fw_server_name,g_server_port) != E_SUCCESS)
handle = iotota_Connect(fw_server_name,OTA_SERVER_PORT);
if(handle == NULL)
{
log_error(" OTA connect failed\n");
goto failed;
}
log_debug0("OTA connect success\n");
log_debug0("OTA connect2 success\n");
//GET request,fetch firmware
//if(iotota_UpgradeStart(&stSSL, "deviot.lenovo.com.cn", "webpage/rtk/ota_all.bin", md5) != E_SUCCESS)
if(iotota_UpgradeStart(&stSSL, fw_server_name,fw_server_url,md5) != E_SUCCESS)
if(iotota_UpgradeStart(handle,fw_server_name,fw_server_url,md5) != E_SUCCESS)
{
log_error(" Https OTA upgrade fail!!\n");
goto failed;
......@@ -1049,7 +974,10 @@ int IOTOTAI_Start(UINT32 version)
retval = E_SUCCESS;
failed:
iotota_Disconnect();
if(handle != NULL) {
IOTSSL_Destroy(handle);
handle = NULL;
}
if(pbuf != NULL)
{
IOTM_Free(pbuf);
......
//#include "redefine.h"
//#include "ssl_server_crt.h"
#include "FreeRTOS.h"
#include "task.h"
#include "diag.h"
#include "redefine.h"
#include "ssl_server_crt.h"
#include "datatype.h"
#define MODULE_TAG "HTTPS"
#include "log.h"
......@@ -12,6 +10,7 @@
#include "https.h"
#include "leiot/cJSON.h"
#include "iotsdk_wifi.h"
#include "iotcommon.h"
#include "security_func.h"
#include "redefine.h"
......@@ -26,12 +25,6 @@
#include "polarssl/certs.h"
#include "polarssl/error.h"
#define LOG_NHTTP(...) log_debug0(__VA_ARGS__)
#define SERVER_PORT 8443
#define STACKSIZE 1150
extern struct netif xnetif[NET_IF_NUM];
extern struct netif xnetif[NET_IF_NUM];
extern const char *test_ca_crt;
......@@ -61,24 +54,8 @@ static int g_recvedSetwifi = 0;
static ApInfo *g_apInfo = NULL;
static const char *g_response_data = NULL;
#ifdef PLATFORM_LINUX
static const char *g_cert_file = NULL;
static const char *g_cert_key = NULL;
void Wificonfig_SetCert(const char *cert, const char *key)
{
g_cert_file = cert;
g_cert_key = key;
}
#endif //!PLATFORM_LINUX
static int random(void *p_rng, unsigned char *output, size_t output_len)
{
rtw_get_random_bytes(output, output_len);
return 0;
}
Request_Type parse_request_type(char* data)
static Request_Type parse_request_type(char* data)
{
if (data == NULL) return RQ_ERR;
......@@ -88,7 +65,7 @@ Request_Type parse_request_type(char* data)
return RQ_ERR;
}
URI_Entry* get_uri_entry(unsigned int hash)
static URI_Entry* get_uri_entry(unsigned int hash)
{
unsigned int i = 0;
for (i = 0; i < _nhttp->uri_count; ++i)
......@@ -97,7 +74,7 @@ URI_Entry* get_uri_entry(unsigned int hash)
return NULL;
}
unsigned int fnv_hash(const char* str, unsigned int length)
static unsigned int fnv_hash(const char* str, unsigned int length)
{
unsigned int h = 2166136261UL;
unsigned int i = 0;
......@@ -107,7 +84,7 @@ unsigned int fnv_hash(const char* str, unsigned int length)
}
// Get URI
int parse_uri(HTTP_Request* rq, char* data)
static int parse_uri(HTTP_Request* rq, char* data)
{
// Search / in request
// HTTP/1.1 /testest \r\n
......@@ -132,7 +109,7 @@ int parse_uri(HTTP_Request* rq, char* data)
return 1;
}
int parse_request(HTTP_Request* request, char* data)
static int parse_request(HTTP_Request* request, char* data)
{
//char *token;
char *rest = data;
......@@ -142,28 +119,28 @@ int parse_request(HTTP_Request* request, char* data)
request->type = parse_request_type(data);
if (request->type == RQ_ERR) {
LOG_NHTTP("Unknown request type\r\n");
log_debug0("Unknown request type\r\n");
return 0;
}
// parse uri
if (!parse_uri(request, data)) {
LOG_NHTTP("Failed to parse URI\r\n");
log_debug0("Failed to parse URI\r\n");
return 0;
}
return 1;
}
void dump_request(HTTP_Request* rq)
static void dump_request(HTTP_Request* rq)
{
LOG_NHTTP("Type:%d\r\n", rq->type);
LOG_NHTTP("URI:%s\r\n", rq->uri);
LOG_NHTTP("URI Len:%d\r\n", rq->uri_length);
LOG_NHTTP("hash:%x\r\n", rq->uri_hash);
log_debug0("Type:%d\r\n", rq->type);
log_debug0("URI:%s\r\n", rq->uri);
log_debug0("URI Len:%d\r\n", rq->uri_length);
log_debug0("hash:%x\r\n", rq->uri_hash);
}
int parse_json_data(char *buffer,char *msgType)
static int parse_json_data(char *buffer,char *msgType)
{
cJSON *root = NULL;
cJSON *ssid = NULL;
......@@ -200,7 +177,7 @@ int parse_json_data(char *buffer,char *msgType)
goto failed;
}
iots_memcpy(g_apInfo->ssid,ssid->valuestring,iots_strlen(ssid->valuestring));
LOG_NHTTP("ssid : %s\n",g_apInfo->ssid);
log_debug0("ssid : %s\n",g_apInfo->ssid);
pwd = cJSON_GetObjectItem(root,"pwd");
if(pwd == NULL || pwd->valuestring == NULL)
......@@ -214,7 +191,7 @@ int parse_json_data(char *buffer,char *msgType)
goto failed;
}
iots_memcpy(g_apInfo->pwd,pwd->valuestring,iots_strlen(pwd->valuestring));
LOG_NHTTP("pwd : %s\n",g_apInfo->pwd);
log_debug0("pwd : %s\n",g_apInfo->pwd);
utc = cJSON_GetObjectItem(root,"utc");
if(utc != NULL && utc->valuestring != NULL)
{
......@@ -224,7 +201,7 @@ int parse_json_data(char *buffer,char *msgType)
goto failed;
}
iots_memcpy(g_apInfo->utc,utc->valuestring,iots_strlen(utc->valuestring));
LOG_NHTTP("utc : %s\n",g_apInfo->utc);
log_debug0("utc : %s\n",g_apInfo->utc);
}
localserver = cJSON_GetObjectItem(root,"localserver");
......@@ -236,7 +213,7 @@ int parse_json_data(char *buffer,char *msgType)
goto failed;
}
iots_memcpy(g_apInfo->localserver,localserver->valuestring,iots_strlen(localserver->valuestring));
LOG_NHTTP("localserver : %s\n",g_apInfo->localserver);
log_debug0("localserver : %s\n",g_apInfo->localserver);
}
serverlevel = cJSON_GetObjectItem(root,"serverlevel");
......@@ -248,7 +225,7 @@ int parse_json_data(char *buffer,char *msgType)
goto failed;
}
iots_memcpy(g_apInfo->serverlevel,serverlevel->valuestring,iots_strlen(serverlevel->valuestring));
LOG_NHTTP("serverlevel : %s\n",g_apInfo->serverlevel);
log_debug0("serverlevel : %s\n",g_apInfo->serverlevel);
}
pre_code = cJSON_GetObjectItem(root,"pre_code");
......@@ -260,7 +237,7 @@ int parse_json_data(char *buffer,char *msgType)
goto failed;
}
iots_memcpy(g_apInfo->pre_code,pre_code->valuestring,iots_strlen(pre_code->valuestring));
LOG_NHTTP("pre_code : %s\n",g_apInfo->pre_code);
log_debug0("pre_code : %s\n",g_apInfo->pre_code);
}
time_zone = cJSON_GetObjectItem(root,"time_zone");
......@@ -272,7 +249,7 @@ int parse_json_data(char *buffer,char *msgType)
goto failed;
}
iots_memcpy(g_apInfo->time_zone,time_zone->valuestring,iots_strlen(time_zone->valuestring));
LOG_NHTTP("time_zone :%s \n",g_apInfo->time_zone);
log_debug0("time_zone :%s \n",g_apInfo->time_zone);
}
}
else if(strcmp(msgType,"changeNetwork") == 0)
......@@ -317,24 +294,24 @@ struct record
const char *address, *city, *state, *zip, *country;
};
int SendHttpsResp(ssl_context* ssl, Nano_Http* _nhttp, const char* data)
int SendHttpsResp(SSL_Handle handle, Nano_Http* _nhttp, const char* data)
{
HTTP_Response* resp = &_nhttp->response;
resp->rtype = OK_;
resp->ctype = text_html;
resp->chunks_sent = 0;
resp->send_zero = 0;
LOG_NHTTP("send response data: %s\n", g_response_data);
log_debug0("send response data: %s\n", g_response_data);
int cnt = iots_sprintf((char *)_nhttp->data, "HTTP/1.1 %s\r\n", responses_str[resp->rtype]);
cnt += iots_sprintf((char *)_nhttp->data + cnt, "Content-Type: %s\r\n\r\n", ctypes_str[resp->ctype]);
cnt += iots_sprintf((char *)_nhttp->data + cnt, "%s", g_response_data);
LOG_NHTTP("Chunks: %d\r\nRest: %d\r\n", resp->chunks_left, resp->rest);
log_debug0("Chunks: %d\r\nRest: %d\r\n", resp->chunks_left, resp->rest);
_nhttp->processing = 1;
LOG_NHTTP("=== EOF_REQ ===\r\n");
LOG_NHTTP("Res data: %d\n", iots_strlen((const char *)_nhttp->data));
if(ssl_write(ssl, _nhttp->data, iots_strlen((const char *)_nhttp->data)) > 0)
log_debug0("=== EOF_REQ ===\r\n");
log_debug0("Res data: %d\n", iots_strlen((const char *)_nhttp->data));
if(IOTSSL_Write(handle, _nhttp->data, iots_strlen((const char *)_nhttp->data)) == E_SUCCESS)
{
return E_SUCCESS;
}
......@@ -352,7 +329,7 @@ static int GetSetwifiStatus(void)
}
int process_request(char* data, ssl_context* ssl, int client_socket)
static int process_request(char* data, SSL_Handle handle)
{
int j = 0;
int ret = 0;
......@@ -361,19 +338,19 @@ int process_request(char* data, ssl_context* ssl, int client_socket)
char *json_packet = NULL;
HTTP_Request* req = NULL;
recv_data = (char *)IOTM_Malloc(2048);
recv_data = IOTM_Malloc(2048);
if(recv_data == NULL) {
LOG_NHTTP("malloc failed\n");
log_debug0("malloc failed\n");
goto out;
}
iots_strcpy(recv_data, data);
LOG_NHTTP("=== REQUEST ===\r\n");
log_debug0("=== REQUEST ===\r\n");
_nhttp = &nhttp;
req = &_nhttp->request;
if (!parse_request(req, data))
{
LOG_NHTTP("parsing failed\r\n");
log_debug0("parsing failed\r\n");
//while (1) {};
goto out;
}
......@@ -389,7 +366,7 @@ int process_request(char* data, ssl_context* ssl, int client_socket)
if(g_respResult != 1) //是否需要再次response,若app没收到response会多次发送setwifi
{
log_debug0("response false,response again......\n");
if(SendHttpsResp(ssl, _nhttp, g_response_data) == E_SUCCESS)
if(SendHttpsResp(handle, _nhttp, g_response_data) == E_SUCCESS)
{
g_respResult = 1;
}
......@@ -398,33 +375,33 @@ int process_request(char* data, ssl_context* ssl, int client_socket)
goto out;
}
LOG_NHTTP("POSCIK: %s\r\n", data);
log_debug0("POSCIK: %s\r\n", data);
// LOG_NHTTP ("recv_data : %s\n", recv_data);
// log_debug0 ("recv_data : %s\n", recv_data);
for (j = 0; j < iots_strlen(recv_data); j++)
if (recv_data[j] == '\r' && recv_data[j + 1] == '\n' && recv_data[j + 2] == '\r' && recv_data[j + 3] == '\n')
{
json_packet = &recv_data[j + 4];
break;
}
LOG_NHTTP("Received data : %s\n", (char*) json_packet);
log_debug0("Received data : %s\n", (char*) json_packet);
if( parse_json_data(json_packet,"setWifi") != E_SUCCESS)
{
LOG_NHTTP("parse json data failed \n");
log_debug0("parse json data failed \n");
goto out;
}
if(GetSetwifiStatus() == E_SUCCESS) //收到所需信息后才响应
{
log_debug0("Softap get needed info\n");
g_recvedSetwifi = 1;
if(SendHttpsResp(ssl, _nhttp, g_response_data) == E_SUCCESS)
if(SendHttpsResp(handle, _nhttp, g_response_data) == E_SUCCESS)
{
g_respResult = 1;
LOG_NHTTP("Send response success!\n");
log_debug0("Send response success!\n");
}
else
{
LOG_NHTTP("Send response failed!\n");
log_debug0("Send response failed!\n");
}
}
ret = 0;
......@@ -437,7 +414,7 @@ int process_request(char* data, ssl_context* ssl, int client_socket)
ret = 0;
goto out;
}
LOG_NHTTP("POSCIK: %s\r\n", data);
log_debug0("POSCIK: %s\r\n", data);
for (j = 0; j < iots_strlen(recv_data); j++)
if (recv_data[j] == '\r' && recv_data[j + 1] == '\n' && recv_data[j + 2] == '\r' && recv_data[j + 3] == '\n')
......@@ -446,20 +423,20 @@ int process_request(char* data, ssl_context* ssl, int client_socket)
break;
}
LOG_NHTTP("Received data : %s\n", (char*) json_packet);
log_debug0("Received data : %s\n", (char*) json_packet);
if(parse_json_data(json_packet,"changeNetwork") != E_SUCCESS)
{
ret = 0;
goto out;
}
ret = 1;
LOG_NHTTP("changeNetwork OK\n");
LOG_NHTTP("SendHttpsResp......\n");
//ret = SendHttpsResp(ssl, _nhttp, g_response_data);
log_debug0("changeNetwork OK\n");
log_debug0("SendHttpsResp......\n");
//ret = SendHttpsResp(handle, _nhttp, g_response_data);
//if (ret > 0)
// LOG_NHTTP("ok\n");
// log_debug0("ok\n");
//else
// LOG_NHTTP("Setwifi error...\n");
// log_debug0("Setwifi error...\n");
//if(1 == ret_parse_json) {
// ret = 1;
//}
......@@ -479,300 +456,161 @@ out:
return ret;
}
int SetNonBlock(int iSock)
static int https_checkDataIsValid(char *pszBuf)
{
int iFlags;
iFlags = fcntl(iSock, F_GETFL, 0);
iFlags |= O_NONBLOCK;
iFlags |= O_NDELAY;
int ret = fcntl(iSock, F_SETFL, iFlags);
return ret;
int iRet = 0;
int left_flag = 0, right_flag = 0;
char* offset_l = pszBuf;
char* offset_r = pszBuf;
while (!(offset_l == NULL && offset_r == NULL)) {
offset_l = strstr(offset_l+1, "{");
if (offset_l != NULL)
left_flag ++;
offset_r = strstr(offset_r+1, "}");
if (offset_r != NULL)
right_flag ++;
if (left_flag > 0 && left_flag == right_flag)
{
iRet = 1;
break;
}
}
return iRet;
}
#if 0
static int load_server_cert(SSL_CTX* ctx)
//int Wificonfig_StartHttpsServer(const char *response, char *ssid, char *password, char *utc)
int Wificonfig_StartHttpsServer(const char *response,ApInfo *apInfo /*char *ssid, char *password, char *utc*/)
{
int retval = E_FAILED;
#ifndef PLATFORM_LINUX
int s_flag = 0;
int loopNumber = 0;
int retval = E_FAILED;
char *pszRecvBuf = NULL;
int iRecvBufOft = 0;
int cer_len = (SERVER_CERT_LENGTH+3)&~3;
int key_len = (SERVER_KEY_LENGTH+3)&~3;
int buffer_len = (cer_len>key_len)?cer_len:key_len;
char *buffer = NULL;
char *cer_buf = NULL;
char *key_buf = NULL;
SSL_Handle handle = NULL;
int iRet = -1;
int iLogIndex = 0;
int recv_flag = 0;
buffer = IOTM_Malloc(buffer_len);
if(buffer == NULL) {
log_error("insufficient memory\n");
goto failed;
}
log_assert(apInfo != NULL && response != NULL);
LOG_NHTTP("load server crt ......len=%d/%d\n", cer_len, buffer_len);
memset(buffer, 0, buffer_len);
log_assert(cer_len <= buffer_len);
iots_memcpy(buffer, SERVER_CERT, cer_len/*fixme.. SERVER_CERT_LENGTH*/);
if(!SSL_CTX_use_certificate_ASN1(ctx, SERVER_CERT_LENGTH, buffer)) {
log_error("use_certificate failed\n");
goto failed;
}
g_apInfo = apInfo;
memset(g_apInfo,0,sizeof(ApInfo));
g_response_data = response;
LOG_NHTTP("load server private key ......len=%d/%d\n", key_len, buffer_len);
memset(buffer, 0, buffer_len);
log_assert(key_len <= buffer_len);
iots_memcpy(buffer, SERVER_KEY, key_len/*fixme.. SERVER_KEY_LENGTH*/);
if(!SSL_CTX_use_PrivateKey_ASN1(0, ctx, buffer, SERVER_KEY_LENGTH)) {
log_error("use_PrivateKey failed\n");
goto failed;
}
retval = E_SUCCESS;
g_respResult = 0;
g_recvedSetwifi = 0;
loopNumber = ALL_SLEEP_TIME / SLEEP_TIME;
log_debug0("set SSL context read buffer size ......OK\n");
SSL_CTX_set_default_read_buffer_len(ctx, OPENSSL_FRAGMENT_SIZE);
log_debug0("OpenSSL thread start...\n");
log_debug0("create SSL context ......\n");
failed:
if(buffer != NULL) {
IOTM_Free(buffer);
buffer = NULL;
}
#else
if(g_cert_file == NULL || g_cert_key == NULL) {
log_error("certificate file unknown\n");
goto failed;
cer_buf = IOTM_Malloc(cer_len);
if(cer_buf == NULL) {
log_error("insufficient memory\n");
retval = E_NO_MEMORY;
goto end;
}
LOG_NHTTP("load server crt ......file=%s\n", g_cert_file);
if(!SSL_CTX_use_certificate_file(ctx, g_cert_file, SSL_FILETYPE_PEM)) {
log_error("use_certificate_file failed\n");
goto failed;
key_buf = IOTM_Malloc(key_len);
if(key_buf == NULL) {
log_error("insufficient memory\n");
IOTM_Free(cer_buf); cer_buf = NULL;
retval = E_NO_MEMORY;
goto end;
}
LOG_NHTTP("load server private key ......file=%s\n", g_cert_key);
if (!SSL_CTX_use_PrivateKey_file(ctx, g_cert_key, SSL_FILETYPE_PEM)) {
log_error("use_PrivateKey_file failed\n");
goto failed;
memset(cer_buf, 0, cer_len);
memset(key_buf, 0, key_len);
log_debug0("load server crt ......len=%d/%d\n", cer_len, key_len);
iots_memcpy(cer_buf, SERVER_CERT, cer_len);
iots_memcpy(key_buf, SERVER_KEY, key_len);
handle = IOTSSL_ServerNew("192.168.199.1", OPENSSL_LOCAL_TCP_PORT,
cer_buf, cer_len, key_buf, key_len);
IOTM_Free(cer_buf); cer_buf = NULL;
IOTM_Free(key_buf); key_buf = NULL;
if(handle == NULL) {
log_error("handle is null\n");
goto end;
}
retval = E_SUCCESS;
failed:
#endif //!PLATFORM_LINUX
return retval;
}
#endif
static int Setsockopt(int socket,int keepIdle,int keepInterval,int keepCount)
{
int retval = 0;
int setval = 1;
int keepAlive = 1;
retval |= setsockopt(socket,IPPROTO_TCP, TCP_NODELAY ,(const char *)&setval ,sizeof(setval));
retval |= setsockopt(socket,SOL_SOCKET ,SO_KEEPALIVE ,(const char *)&keepAlive ,sizeof(keepAlive));
retval |= setsockopt(socket,IPPROTO_TCP,TCP_KEEPIDLE ,(const char *)&keepIdle ,sizeof(keepIdle));
retval |= setsockopt(socket,IPPROTO_TCP,TCP_KEEPINTVL,(const char *)&keepInterval,sizeof(keepInterval));
retval |= setsockopt(socket,IPPROTO_TCP,TCP_KEEPCNT ,(const char *)&keepCount ,sizeof(keepCount));
return retval;
}
/*char *ssid, char *password, char *utc*/
int Wificonfig_StartHttpsServer(const char *response, ApInfo *apInfo)
{
int ret, server_fd = -1, client_fd = -1;
x509_crt server_x509;
pk_context server_pk;
ssl_context ssl;
unsigned char szRecvBuf[OPENSSL_RECV_BUF_ONE_LEN + 1];
int iRrecvFlag = 0;
int left_flag = 0, right_flag = 0;
char *offset_l = NULL;
char *offset_r = NULL;
int times = 0;
int loopStop = 0;
int loopNumber = 0;
int s_flag = 0;
int iOffset;
uint8_t *ip;
//char *response = "<HTML><BODY>SSL OK</BODY></HTML>";
if(apInfo == NULL) {
log_error("Parameter error\n");
return E_FAILED;
}
g_apInfo = apInfo;
memset(g_apInfo,0,sizeof(ApInfo));
g_response_data = response;
g_respResult = 0;
g_recvedSetwifi = 0;
loopNumber = ALL_SLEEP_TIME / SLEEP_TIME;
pszRecvBuf = IOTM_Malloc(OPENSSL_RECV_BUF_ALL_LEN);
if (NULL == pszRecvBuf) {
log_error("insufficient memory\n");
retval = E_NO_MEMORY;
goto end;
}
// Delay to wait for IP by DHCP
vTaskDelay(10000);
log_debug0("SSL server Start\n");
memory_set_own(pvPortMalloc, vPortFree);
memset(&server_x509, 0, sizeof(x509_crt));
memset(&server_pk, 0, sizeof(pk_context));
memset(&ssl, 0, sizeof(ssl_context));
x509_crt_init(&server_x509);
pk_init(&server_pk);
if((ret = x509_crt_parse(&server_x509, (const unsigned char *)test_srv_crt, strlen(test_srv_crt))) != 0) {
printf(" failed\n ! x509_crt_parse returned %d\n\n", ret);
goto exit;
}
if((ret = x509_crt_parse(&server_x509, (const unsigned char *)test_ca_list, strlen(test_ca_list))) != 0) {
printf(" failed\n ! x509_crt_parse returned %d\n\n", ret);
goto exit;
}
if((ret = pk_parse_key(&server_pk, test_srv_key, strlen(test_srv_key), NULL, 0)) != 0) {
printf(" failed\n ! pk_parse_key returned %d\n\n", ret);
goto exit;
}
/*
* 2. Start the connection
*/
ip = LwIP_GetIP(&xnetif[0]);
printf("\n\r . Starting tcp server /%d.%d.%d.%d/%d...", ip[0], ip[1], ip[2], ip[3], SERVER_PORT);
if((ret = net_bind(&server_fd, NULL, SERVER_PORT)) != 0) {
log_error(" failed\n ! net_bind returned %d\n\n", ret);
goto exit;
}
printf(" ok\n");
/*
* 3. Waiting for client to connect
*/
reconnect:
log_debug0("Waiting for client to connect...\n\r");
while((ret = net_accept(server_fd, &client_fd, NULL)) == 0) {
log_debug0("A client is connecting\n\r");
retval = IOTSSL_ServerAccept(handle);
if(retval == E_ACCEPT_TEM_UNAVAIL) {
goto checkloop;
} else if(retval != E_SUCCESS) {
log_debug0("IOTSSL_ServerAccept falied\n");
goto end;
}
retval = E_FAILED;
log_debug0("SSL server socket accept client ......\n");
iRecvBufOft = 0;
recv_flag = 0;
memset(pszRecvBuf, 0, OPENSSL_RECV_BUF_ALL_LEN);
log_debug0("ssl_read...\n");
do {
msleep(10);
iRet = IOTSSL_Read(handle, OPENSSL_RECV_BUF_ALL_LEN, pszRecvBuf + iRecvBufOft);
if (iRet < 0)
continue;
iRecvBufOft += iRet;
if (0 == https_checkDataIsValid(pszRecvBuf)) {
continue;
}
//struct timeval timeout = {10,0};
//setsockopt(client_fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(struct timeval));
/*
* 4. Setup stuff
*/
printf("\n\r . Setting up the SSL/TLS structure...");
if((ret = ssl_init(&ssl)) != 0) {
log_error(" failed\n ! ssl_init returned %d\n\n", ret);
goto close_ssl;
log_debug0 ("recv over...SSL read len : %d, SSL read: \n%s\n",
iots_strlen(pszRecvBuf), pszRecvBuf);
if(1 == (iRet = process_request(pszRecvBuf, handle))) {
log_debug0 ("Get passwd, Ret : %d, re_num : %d, I will leave softap.\n", iRet, loopNumber);
goto end;
} else {
log_debug0("Get Ssid Passwd %s\n", (g_recvedSetwifi == 1)?"Ok":"False");
IOTSSL_ServerShutdown(handle);
goto reconnect;
}
} while (++recv_flag < 100);
ssl_set_endpoint(&ssl, SSL_IS_SERVER);
ssl_set_ca_chain(&ssl, server_x509.next, NULL, NULL);
ssl_set_authmode(&ssl, SSL_VERIFY_NONE);
ssl_set_rng(&ssl, random, NULL);
ssl_set_bio(&ssl, net_recv, &client_fd, net_send, &client_fd);
if((ret = ssl_set_own_cert(&ssl, &server_x509, &server_pk)) != 0) {
printf(" failed\n ! ssl_set_own_cert returned %d\n\n", ret);
goto close_ssl;
}
/*
* 5. Handshake
*/
printf("\n\r . Performing the SSL/TLS handshake...");
if(recv_flag >= 100) {
log_debug0 ("recv error, recv buf len:%d\n", iots_strlen(pszRecvBuf));
}
if((ret = ssl_handshake(&ssl)) != 0) {
log_error(" failed\n ! ssl_handshake returned %d\n\n", ret);
goto close_ssl;
}
log_debug0("ok. Use ciphersuite %s\n", ssl_get_ciphersuite(&ssl));
checkloop:
if(loopNumber) {
if(g_recvedSetwifi == 1) {
loopNumber --;
}
iOffset = 0;
iRrecvFlag = 0;
memset(szRecvBuf, 0, (OPENSSL_RECV_BUF_ONE_LEN));
do {
msleep(1);
ret = ssl_read(&ssl, &szRecvBuf[iOffset], OPENSSL_RECV_BUF_ONE_LEN);
iRrecvFlag ++;
if (iRrecvFlag > 5000) {
printf ("recv ret : %d, recv buf len...\n", ret, iots_strlen(szRecvBuf) - iOffset);
break;
}
if ( ret > 0) {
iOffset += ret;
}
if (ret <= -1)
continue;
times = 0;
left_flag = 0;
right_flag = 0;
offset_l = szRecvBuf;
offset_r = szRecvBuf;
while (!(offset_l == NULL && offset_r == NULL)) {
offset_l = strstr(offset_l+1, "{");
if (offset_l != NULL)
left_flag ++;
offset_r = strstr(offset_r+1, "}");
if (offset_r != NULL)
right_flag ++;
if (left_flag > 0 && left_flag == right_flag)
{
times = 1;
break;
}
}
if ( times > 0) {
log_debug0("recv over...SSL read len : %d, SSL read: \n%s\n", iots_strlen(szRecvBuf), szRecvBuf);
loopStop = process_request(szRecvBuf, &ssl, client_fd);
log_debug0("Ret : %d, re_num : %d\n",loopStop,loopNumber);
break;
}
}while(1);
close_ssl:
#ifdef POLARSSL_ERROR_C
if(ret != 0) {
char error_buf[100];
polarssl_strerror(ret, error_buf, 100);
log_debug0("\n\rLast error was: %d - %s\n", ret, error_buf);
}
#endif
ssl_close_notify(&ssl);
close_fd:
net_close(client_fd);
ssl_free(&ssl);
if(loopStop != 1 && loopNumber) {
s_flag ++;
if(g_recvedSetwifi == 1) {
loopNumber --;
}
if (s_flag % (5 * (1000 / SLEEP_TIME)) == 0) {
log_debug0("wait connect second %d...\n", s_flag / (1000 / SLEEP_TIME));
}
msleep(SLEEP_TIME);
goto reconnect;
} else {
break;
}
}
log_debug0("%s", ((iLogIndex++%40) == 0)?"wait connect second ...\n":"");
msleep(SLEEP_TIME);
IOTSSL_ServerShutdown(handle);
goto reconnect;
}
net_close(server_fd);
end:
if(handle != NULL) {
IOTSSL_Destroy(handle);
handle = NULL;
}
exit:
x509_crt_free(&server_x509);
pk_free(&server_pk);
//vTaskDelete(NULL);
return (g_recvedSetwifi) ? E_SUCCESS : E_FAILED;
if (NULL != pszRecvBuf) {
IOTM_Free(pszRecvBuf);
pszRecvBuf = NULL;
}
log_debug0("Closet https server sucess\n");
return (g_recvedSetwifi) ? E_SUCCESS : retval;
}
......@@ -8,13 +8,11 @@
#define BUFFER_SIZE 2048
#define OPENSSL_RECV_BUF_ONE_LEN 1024
#define OPENSSL_RECV_BUF_ALL_LEN 4096
#define OPENSSL_RECV_BUF_ALL_LEN 2048
#define OPENSSL_LOCAL_TCP_PORT 8443
#define OPENSSL_THREAD_NAME "openssl"
#define OPENSSL_THREAD_STACK_WORDS 2048
#define OPENSSL_THREAD_PRORIOTY 6
#define OPENSSL_FRAGMENT_SIZE 2048
typedef enum {
GET,
......
......@@ -2,16 +2,15 @@
//#include <stdlib.h>
//#include <stdio.h>
#include "cJSON.h"
#include "main.h"
//#include "iotcommon.h"
#include "iotcommon.h"
#include "redefine.h"
#include "datatype.h"
#include "iotsdk.h"
#include "rtos.h"
#include "include/device.h"
#include "iotsdk.h"
#include "configdata.h"
#define MODULE_TAG "WifiConfig"
#include "log.h"
#include "iotsdk_wifi.h"
#include "datatype.h"
#include "security_func.h"
#include "cmsis_os.h"
......@@ -37,11 +36,18 @@ int IOTWifi_Start(int force,ApInfo *apInfo)
int retval = E_FAILED;
char code_challenge[36];
char code_verifier[16];
char version[16];
DMEvent event;
char *country_code = "cn"; //US.
log_assert(apInfo != NULL);
if(IOTConfMI_Get(MEMPARAM_TYPE_AUTH_FAILED, apInfo, sizeof(ApInfo)) == E_SUCCESS) {
log_assert(sizeof(apInfo->reserved) == sizeof(code_verifier));
memcpy(code_verifier, apInfo->reserved, sizeof(apInfo->reserved));
log_debug0("get mem param: %s->%s->%s->%s\n", apInfo->ssid, apInfo->pwd, apInfo->reserved, apInfo->pre_code);
IOTConfMI_Clear();
goto sta_auth;
}
event.event = IOTDM_EVENT_NETWORK_CONFIG;
if(!force && IOTConf_GetWifiFlag()) {
log_debug0("wifi is already configed\n");
......@@ -66,11 +72,11 @@ int IOTWifi_Start(int force,ApInfo *apInfo)
memset(code_verifier, 0, sizeof(code_verifier));
iots_sprintfs(code_verifier, sizeof(code_verifier), "%d%d", IOTSysP_Random(10000), IOTSysP_Random(10000));
printf("code_verifier %s\n", code_verifier);
memset(code_challenge, 0, sizeof(code_challenge));
IOTEncode_MD5_Hex(code_verifier, iots_strlen(code_verifier), code_challenge);
memset(buffer, 0, BUFFER_LENGTH);
memset(version, 0, sizeof(version));
iots_sprintfs(buffer, sizeof(buffer),
"{\"hubtype\":\"%d\", "
"\"macaddr\":\"%s\", "
......@@ -82,7 +88,7 @@ int IOTWifi_Start(int force,ApInfo *apInfo)
(const unsigned char *)IOTSys_Mac(),
country_code,
IOTConfRI_CloudStr(),
IOTDev_AdapterVersion(), code_challenge);
IOTDev_AdapterVersion(version), code_challenge);
log_debug0("softap started, then start https server, response=%s\n", buffer);
if(Wificonfig_StartHttpsServer(buffer,apInfo) != E_SUCCESS) {
......@@ -95,6 +101,7 @@ int IOTWifi_Start(int force,ApInfo *apInfo)
log_error("softap stop failed\n");
}
sta_auth:
log_debug0("softap stoped, then do staticon config\n");
if(IOTWifiP_StationConfig(apInfo->ssid,apInfo->pwd) != E_SUCCESS) {
log_error("station config failed\n");
......@@ -116,7 +123,7 @@ int IOTWifi_Start(int force,ApInfo *apInfo)
ssleep(2);
}
log_debug0("station connect success, then set wificonfig flag\n");
log_debug0("station connect success, then get access token\n");
{
int retry = 10;
while(retry-- > 0) {
......@@ -135,6 +142,14 @@ int IOTWifi_Start(int force,ApInfo *apInfo)
}
if(retval == E_SUCCESS) {
retval = IOTConfI_SetWifiFlag(1);
} else if(retval == E_NO_MEMORY) {
log_assert(sizeof(apInfo->reserved) == sizeof(code_verifier));
memcpy(apInfo->reserved, code_verifier, sizeof(apInfo->reserved));
log_debug0("set mem param: %s->%s->%s->%s\n", apInfo->ssid, apInfo->pwd, apInfo->reserved, apInfo->pre_code);
if(IOTConfMI_Set(MEMPARAM_TYPE_AUTH_FAILED, apInfo, sizeof(ApInfo)) == E_SUCCESS) {
log_debug0("check no memeory to restart\n");
IOTSys_Reboot();
}
}
failed:
if(buffer != NULL) {
......
......@@ -108,78 +108,10 @@ void user_task(void *arg)
return;
}
if(xTaskCreate(main_task, ((const char*)"main_task"), 1024*4, NULL, tskIDLE_PRIORITY + 3 + PRIORITIE_OFFSET, NULL) != pdPASS)
if(xTaskCreate(main_task, ((const char*)"main_task"), 1024*1 + 512, NULL, tskIDLE_PRIORITY + 3 + PRIORITIE_OFFSET, NULL) != pdPASS)
printf("\n\r%s xTaskCreate(init_thread) failed", __FUNCTION__);
for(;;) {
rtw_msleep_os(6000);
}
#if 0
IOTDM_LogLevel(2);
rtw_msleep_os(3000);
rtc_write(1528804180);
if(IOTRtos_init()!= 0) {
printf("RTOS init failed\n");
return;
}
if(IOTSDK_Init()!= 0) {
printf("confit init failed\n");
return;
}
rtw_msleep_os(3000);
if(!IOTConf_GetWifiFlag()) {
softap:
printf("Enter softap_task\r\n");
g_pstInfo = malloc(sizeof(ApInfo));
if(g_pstInfo == NULL)
{
printf("Malloc failed\n");
return;
}
memset(g_pstInfo, 0, sizeof(ApInfo));
IOTWifi_Start(0,g_pstInfo);
log_debug0("delete softap task......\n");
if(strlen(g_pstInfo->utc) != 0)
{
memset(g_utc,0,sizeof(g_utc));
memcpy(g_utc,g_pstInfo->utc, strlen(g_pstInfo->utc));
}
if(g_pstInfo != NULL)
{
free(g_pstInfo);
g_pstInfo = NULL;
}
}else {
while (1) {
if (0 != IOTWifi_ConnectWifiByConf()) {
printf("Connect Falied\r\n");
//goto softap;
} else {
break;
}
if (!IOTConf_GetWifiFlag()) {
goto softap;
}
}
rtw_msleep_os(3000);
}
//xSemaphoreGive(mutex);
//vTaskDelete(NULL);
IOTDM_Loop();
for(;;) {
rtw_msleep_os(6000);
printf("user_thread alive \n");
}
#endif
vTaskDelete(NULL);
}
void user_thread(void *param)
......
......@@ -157,6 +157,7 @@ static void ApUpgradeTask(void *arg)
IOTOTA_ApOTAStart();
log_debug0("delete ap upgrade task\n");
vTaskDelete(NULL);
IOT_GetHeapSize();
}
int IOTWifiP_SoftapStart(const char *hotspot)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment