123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212 |
- #include "optiga/optiga_dtls.h"
- #include "optiga/cmd/CommandLib.h"
- #include "optiga/dtls/AlertProtocol.h"
- #ifdef MODULE_ENABLE_DTLS_MUTUAL_AUTH
- extern Void ConfigHL(fPerformHandshake_d* PfPerformHandshake_d,eConfiguration_d PeConfiguration);
- extern Void ConfigRL(sConfigRL_d* PpsConfigRL,eConfiguration_d PeConfiguration);
- extern Void ConfigTL(sConfigTL_d* PpsConfigTL,eConfiguration_d PeConfiguration);
- extern Void ConfigCL(sConfigCL_d* PpsConfigCL,eConfiguration_d PeConfiguration);
- extern int32_t DtlsHS_VerifyHR(uint8_t* PprgbData, uint16_t PwLen);
- #define SESSIONID_1 0xE100
- #define INUSE 0x4A
- #define NOTUSED 0xA4
- typedef struct sAppOCPCtx_d
- {
-
- fPerformHandshake_d pfPerformHandshake;
-
- sHandshake_d sHandshake;
-
- sConfigRL_d sConfigRL;
-
- sLogger_d sLogger;
-
- eAuthScheme_d eAuthScheme;
-
- uint8_t* pAppDataBuf;
- }sAppOCPCtx_d;
- _STATIC_H Void OCP_Config(sAppOCPCtx_d* PpsAppOCPCntx,eConfiguration_d PeConfiguration)
- {
- ConfigHL(&(PpsAppOCPCntx->pfPerformHandshake),PeConfiguration);
- ConfigRL(&(PpsAppOCPCntx->sConfigRL),PeConfiguration);
- ConfigTL(PpsAppOCPCntx->sConfigRL.sRL.psConfigTL,PeConfiguration);
- ConfigCL(PpsAppOCPCntx->sConfigRL.sRL.psConfigCL,PeConfiguration);
- }
- _STATIC_H int32_t OCPAllocateMemory(sAppOCPCtx_d ** PppsAppOCPCntx)
- {
- int32_t i4Status = (int32_t)OCP_LIB_MALLOC_FAILURE;
- #define PS_APPOCPCNTX (* PppsAppOCPCntx)
- do
- {
-
- PS_APPOCPCNTX = (sAppOCPCtx_d *)OCP_MALLOC(sizeof(sAppOCPCtx_d));
- if(NULL == PS_APPOCPCNTX)
- {
- break;
- }
- (*PS_APPOCPCNTX).pAppDataBuf = NULL;
- (*PS_APPOCPCNTX).sConfigRL.sRL.psConfigTL = NULL;
- (*PS_APPOCPCNTX).sConfigRL.sRL.psConfigCL = NULL;
-
- PS_APPOCPCNTX->sConfigRL.sRL.psConfigTL = (sConfigTL_d*)OCP_MALLOC(sizeof(sConfigTL_d));
- if(NULL == PS_APPOCPCNTX->sConfigRL.sRL.psConfigTL)
- {
- break;
- }
- PS_APPOCPCNTX->sConfigRL.sRL.psConfigTL->sTL.phTLHdl = NULL;
- PS_APPOCPCNTX->sConfigRL.sRL.psConfigCL = (sConfigCL_d*)OCP_MALLOC(sizeof(sConfigCL_d));
- if(NULL == PS_APPOCPCNTX->sConfigRL.sRL.psConfigCL)
- {
- break;
- }
- PS_APPOCPCNTX->sConfigRL.sRL.psConfigCL->sCL.phCryptoHdl = NULL;
- PS_APPOCPCNTX->sConfigRL.sRL.phRLHdl = NULL;
- i4Status = (int32_t)OCP_LIB_OK;
- }while(FALSE);
- #undef PS_APPOCPCNTX
- return i4Status;
- }
- _STATIC_H Void OCPFreeMemory(sAppOCPCtx_d* PpsAppOCPCntx)
- {
- if(NULL != PpsAppOCPCntx)
- {
- if(NULL != (PpsAppOCPCntx)->pAppDataBuf)
- {
- OCP_FREE((PpsAppOCPCntx)->pAppDataBuf);
- }
- if(NULL != (PpsAppOCPCntx)->sConfigRL.sRL.psConfigCL)
- {
- #define S_RL (PpsAppOCPCntx)->sConfigRL
- S_RL.sRL.psConfigCL->pfClose(&(PpsAppOCPCntx)->sConfigRL.sRL.psConfigCL->sCL);
- OCP_FREE((PpsAppOCPCntx)->sConfigRL.sRL.psConfigCL);
- }
- if(NULL != (PpsAppOCPCntx)->sConfigRL.sRL.psConfigTL)
- {
- #define S_TL (PpsAppOCPCntx)->sConfigRL.sRL.psConfigTL->sTL
- if(NULL != S_TL.phTLHdl)
- {
-
- OCP_FREE(S_TL.phTLHdl);
- S_TL.phTLHdl = NULL;
- }
- OCP_FREE((PpsAppOCPCntx)->sConfigRL.sRL.psConfigTL);
- }
-
- S_RL.pfClose(&S_RL.sRL);
- OCP_FREE(PpsAppOCPCntx);
- }
- #undef S_TL
- #undef S_RL
- }
- typedef struct sSessionRegistry_d
- {
-
- uint16_t wSessionId;
-
- hdl_t hOCPHandle;
-
- uint8_t bInUse;
- }sSessionRegistry_d;
- sSessionRegistry_d sSessionRegistry[1] ={
- {SESSIONID_1, (hdl_t)NULL, NOTUSED}
- };
- _STATIC_H int32_t Registry_GetSessionId(uint16_t* PwSessionId)
- {
- int32_t i4Status = (int32_t)OCP_LIB_SESSIONID_UNAVAILABLE;
- uint8_t bCount = 0;
-
- for(bCount= 0;bCount<(sizeof(sSessionRegistry)/sizeof(sSessionRegistry_d));bCount++)
- {
- if(NOTUSED == sSessionRegistry[bCount].bInUse)
- {
-
- *PwSessionId = sSessionRegistry[bCount].wSessionId;
- i4Status = (int32_t)OCP_LIB_OK;
- break;
- }
- }
- return i4Status;
- }
- _STATIC_H int32_t Registry_Update(uint32_t PdwSessionId,const hdl_t PhOCPHandle)
- {
- int32_t i4Status = (int32_t)OCP_LIB_ERROR;
- uint8_t bCount;
-
- for(bCount= 0;bCount<(sizeof(sSessionRegistry)/sizeof(sSessionRegistry_d));bCount++)
- {
- if((PdwSessionId == sSessionRegistry[bCount].wSessionId) &&
- (NOTUSED == sSessionRegistry[bCount].bInUse))
- {
-
- sSessionRegistry[bCount].hOCPHandle = PhOCPHandle;
-
- sSessionRegistry[bCount].bInUse = INUSE;
- i4Status = (int32_t) OCP_LIB_OK;
- break;
- }
- }
- return i4Status;
- }
- _STATIC_H Void Registry_Free(const hdl_t PhOCPHandle)
- {
- uint8_t bCount;
-
- for(bCount= 0;bCount<(sizeof(sSessionRegistry)/sizeof(sSessionRegistry_d));bCount++)
- {
- if(PhOCPHandle == sSessionRegistry[bCount].hOCPHandle)
- {
-
- sSessionRegistry[bCount].bInUse = NOTUSED;
- sSessionRegistry[bCount].hOCPHandle = NULL;
- break;
- }
- }
- }
- _STATIC_H int32_t Registry_GetHandleSessionID(const hdl_t PhOCPHandle, uint16_t *PpwSessionId)
- {
- int32_t i4Status = (int32_t)OCP_LIB_SESSIONID_UNAVAILABLE;
- uint8_t bCount = 0;
-
- for(bCount= 0;bCount<(sizeof(sSessionRegistry)/sizeof(sSessionRegistry_d));bCount++)
- {
- if(PhOCPHandle == sSessionRegistry[bCount].hOCPHandle)
- {
- *PpwSessionId = sSessionRegistry[bCount].wSessionId;
- i4Status = (int32_t)OCP_LIB_OK;
- break;
- }
- }
- return i4Status;
- }
- _STATIC_H int32_t Registry_ValidateHandleSessionID(const hdl_t PhOCPHandle)
- {
- int32_t i4Status = (int32_t)OCP_LIB_SESSIONID_UNAVAILABLE;
- uint8_t bCount;
-
- for(bCount= 0;bCount<(sizeof(sSessionRegistry)/sizeof(sSessionRegistry_d));bCount++)
- {
- if(PhOCPHandle == sSessionRegistry[bCount].hOCPHandle)
- {
- i4Status = (int32_t)OCP_LIB_OK;
- break;
- }
- }
- return i4Status;
- }
- _STATIC_H Void CloseSession(hdl_t PhAppOCPCtx, bool_t PfFatalError, uint16_t PwSessionId)
- {
- sAppOCPCtx_d *psCntx = (sAppOCPCtx_d*)PhAppOCPCtx;
- #define S_CONFIGURATION_TL (psCntx->sConfigRL.sRL.psConfigTL)
-
- if(psCntx->sHandshake.eAuthState != eAuthInitialised)
- {
- if(!PfFatalError)
- {
- SEND_ALERT(&psCntx->sConfigRL,(int32_t) OCP_RL_ERROR);
- }
-
- CmdLib_CloseSession(PwSessionId);
- }
-
- S_CONFIGURATION_TL->pfDisconnect(&S_CONFIGURATION_TL->sTL);
-
- Registry_Free(PhAppOCPCtx);
-
- OCPFreeMemory(psCntx);
- #undef S_CONFIGURATION_TL
- }
- int32_t OCP_Init(const sAppOCPConfig_d* PpsAppOCPConfig,hdl_t* PphAppOCPCtx)
- {
- int32_t i4Status = (int32_t)OCP_LIB_ERROR;
- sAppOCPCtx_d *psAppOCPCntx = NULL;
- eAuthScheme_d eAuthScheme;
- sOpenApp_d sOpenApp;
- uint16_t wSessionKeyId;
- do
- {
-
- if((NULL == PpsAppOCPConfig) || (NULL == PphAppOCPCtx))
- {
- i4Status = (int32_t)OCP_LIB_NULL_PARAM;
- break;
- }
-
- * PphAppOCPCtx = NULL;
-
- i4Status = Registry_GetSessionId(&wSessionKeyId);
- if(OCP_LIB_OK != i4Status)
- {
- break;
- }
-
- if(eDTLS_12_UDP_HWCRYPTO != PpsAppOCPConfig->eConfiguration)
- {
- i4Status = (int32_t)OCP_LIB_UNSUPPORTED_CONFIG;
- break;
- }
-
- if((eClient == PpsAppOCPConfig->eMode) && (eDTLS_12_UDP_HWCRYPTO == PpsAppOCPConfig->eConfiguration))
- {
- eAuthScheme = eDTLSClient;
- }
- else
- {
-
- i4Status = (int32_t)OCP_LIB_UNSUPPORTED_MODE;
- break;
- }
-
- if((PpsAppOCPConfig->sNetworkParams.wMaxPmtu > MAX_PMTU) || (PpsAppOCPConfig->sNetworkParams.wMaxPmtu < MIN_PMTU))
- {
- i4Status = (int32_t)OCP_LIB_UNSUPPORTED_PMTU;
- break;
- }
-
- i4Status = OCPAllocateMemory(&psAppOCPCntx);
- if(OCP_LIB_OK != i4Status)
- {
- break;
- }
- OCP_Config(psAppOCPCntx,PpsAppOCPConfig->eConfiguration);
- sOpenApp.eOpenType = eInit;
-
- i4Status = CmdLib_OpenApplication(&sOpenApp);
- if(CMD_LIB_OK != i4Status)
- {
- break;
- }
-
- psAppOCPCntx->eAuthScheme = eAuthScheme;
-
- psAppOCPCntx->sHandshake.wMaxPmtu = PpsAppOCPConfig->sNetworkParams.wMaxPmtu;
-
- psAppOCPCntx->sHandshake.wOIDDevCertificate = PpsAppOCPConfig->wOIDDevCertificate;
-
- psAppOCPCntx->sHandshake.wOIDDevPrivKey = PpsAppOCPConfig->wOIDDevPrivKey;
-
- psAppOCPCntx->sHandshake.pfGetUnixTIme = PpsAppOCPConfig->pfGetUnixTIme;
-
- psAppOCPCntx->sHandshake.psConfigRL = &psAppOCPCntx->sConfigRL;
- psAppOCPCntx->sHandshake.eMode = PpsAppOCPConfig->eMode;
-
- psAppOCPCntx->sHandshake.fFatalError = FALSE;
-
- psAppOCPCntx->sLogger = PpsAppOCPConfig->sLogger;
-
- psAppOCPCntx->sHandshake.phLogger = PpsAppOCPConfig->sLogger;
-
- psAppOCPCntx->sConfigRL.sRL.sLogger.pHdl = PpsAppOCPConfig->sLogger.pHdl;
- psAppOCPCntx->sConfigRL.sRL.sLogger.phfWriter = PpsAppOCPConfig->sLogger.phfWriter;
- #define S_TL psAppOCPCntx->sConfigRL.sRL.psConfigTL->sTL
-
- S_TL.pzIpAddress = PpsAppOCPConfig->sNetworkParams.pzIpAddress;
-
- S_TL.wPort = PpsAppOCPConfig->sNetworkParams.wPort;
-
- S_TL.wTimeout = 200;
-
- S_TL.sLogger.pHdl = PpsAppOCPConfig->sLogger.pHdl;
- S_TL.sLogger.phfWriter = PpsAppOCPConfig->sLogger.phfWriter;
-
- S_TL.eIsConnected = eDisconnected;
-
- S_TL.eCallType = eNonBlocking;
-
- psAppOCPCntx->sConfigRL.sRL.psConfigCL->sCL.sLogger.pHdl = PpsAppOCPConfig->sLogger.pHdl;
- psAppOCPCntx->sConfigRL.sRL.psConfigCL->sCL.sLogger.phfWriter = PpsAppOCPConfig->sLogger.phfWriter;
-
-
-
- LOG_SETWRITER((pFWriteData2)PpsAppOCPConfig->sLogger.phfWriter,(Void*)PpsAppOCPConfig->sLogger.pHdl);
-
- i4Status = psAppOCPCntx->sConfigRL.pfInit(&psAppOCPCntx->sConfigRL.sRL);
- if(OCP_RL_OK != i4Status)
- {
- break;
- }
-
- i4Status = psAppOCPCntx->sConfigRL.sRL.psConfigTL->pfInit(&S_TL);
- if(OCP_TL_OK != i4Status)
- {
- break;
- }
-
- i4Status = psAppOCPCntx->sConfigRL.sRL.psConfigCL->pfInit(&psAppOCPCntx->sConfigRL.sRL.psConfigCL->sCL, (Void*)&wSessionKeyId);
- if(OCP_CL_OK != i4Status)
- {
- break;
- }
-
- i4Status = Registry_Update(wSessionKeyId,(hdl_t) psAppOCPCntx);
- if(OCP_LIB_OK != i4Status)
- {
- break;
- }
-
- psAppOCPCntx->sHandshake.eAuthState = eAuthInitialised;
- *PphAppOCPCtx = (hdl_t) psAppOCPCntx;
- psAppOCPCntx->sHandshake.PhAppOCPCtx = *PphAppOCPCtx;
- i4Status = (int32_t)OCP_LIB_OK;
- }while(FALSE);
- if((OCP_LIB_OK != i4Status) && ((int32_t)OCP_LIB_NULL_PARAM != i4Status) && ((int32_t)OCP_LIB_SESSIONID_UNAVAILABLE != i4Status))
- {
- OCPFreeMemory(psAppOCPCntx);
- }
- #undef S_TL
- return i4Status;
- }
- int32_t OCP_Connect(const hdl_t PhAppOCPCtx)
- {
- int32_t i4Status = (int32_t)OCP_LIB_ERROR;
- sAuthScheme_d sAuthScheme;
- #define PS_CNTX ((sAppOCPCtx_d*)PhAppOCPCtx)
- #define S_CONFIGURATION_TL (PS_CNTX->sConfigRL.sRL.psConfigTL)
- #define S_CONFIGURATION_CL (PS_CNTX->sConfigRL.sRL.psConfigCL)
- #define S_CONFIGURATION_RL (PS_CNTX->sConfigRL)
- do
- {
-
- if(NULL == PS_CNTX)
- {
- i4Status = (int32_t)OCP_LIB_NULL_PARAM;
- break;
- }
- i4Status = Registry_ValidateHandleSessionID(PhAppOCPCtx);
- if(OCP_LIB_OK != i4Status)
- {
- break;
- }
-
- if((NULL == S_CONFIGURATION_TL) || (NULL== S_CONFIGURATION_TL->pfConnect)|| (NULL == PS_CNTX->pfPerformHandshake)||
- (NULL == S_CONFIGURATION_RL.pfSend)|| (NULL == S_CONFIGURATION_RL.pfRecv) || (NULL == S_CONFIGURATION_RL.pfClose) ||
- (NULL == S_CONFIGURATION_TL->pfSend) || (NULL == S_CONFIGURATION_TL->pfRecv) || (NULL == S_CONFIGURATION_TL->pfDisconnect) ||
- (NULL == S_CONFIGURATION_CL) || (NULL == S_CONFIGURATION_CL->pfEncrypt) || (NULL == S_CONFIGURATION_CL->pfDecrypt) ||
- (NULL == S_CONFIGURATION_CL->pfClose))
- {
- i4Status = (int32_t)OCP_LIB_NULL_PARAM;
- break;
- }
-
- if(eConnected == S_CONFIGURATION_TL->sTL.eIsConnected)
- {
- i4Status = (int32_t)OCP_LIB_CONNECTION_ALREADY_EXISTS;
- break;
- }
-
- i4Status = S_CONFIGURATION_TL->pfConnect(&S_CONFIGURATION_TL->sTL);
- if(OCP_TL_OK != i4Status)
- {
- break;
- }
-
- i4Status = Registry_GetHandleSessionID(PhAppOCPCtx,&(PS_CNTX->sHandshake.wSessionOID));
- if(OCP_LIB_OK != i4Status)
- {
- break;
- }
-
- sAuthScheme.eAuthScheme = PS_CNTX->eAuthScheme;
- sAuthScheme.wDevicePrivKey = PS_CNTX->sHandshake.wOIDDevPrivKey;
- sAuthScheme.wSessionKeyId = PS_CNTX->sHandshake.wSessionOID;
-
- i4Status = CmdLib_SetAuthScheme(&sAuthScheme);
- if(CMD_LIB_OK != i4Status)
- {
- break;
- }
-
- i4Status = PS_CNTX->pfPerformHandshake((hdl_t)&PS_CNTX->sHandshake);
- if(OCP_HL_OK != i4Status)
- {
- break;
- }
- i4Status = (int32_t) OCP_LIB_OK;
- }while(FALSE);
- do
- {
- if((OCP_LIB_OK != i4Status) &&
- ((int32_t)OCP_LIB_CONNECTION_ALREADY_EXISTS != i4Status) &&
- ((int32_t)OCP_LIB_NULL_PARAM != i4Status) &&
- ((int32_t)OCP_LIB_SESSIONID_UNAVAILABLE != i4Status))
- {
-
- CloseSession(PhAppOCPCtx,PS_CNTX->sHandshake.fFatalError, PS_CNTX->sHandshake.wSessionOID);
- LOG_TRANSPORTDBVAL(i4Status,eInfo);
- }
- }while(FALSE);
- #undef PS_CNTX
- #undef S_CONFIGURATION_CL
- #undef S_CONFIGURATION_TL
- #undef S_CONFIGURATION_RL
- return i4Status;
- }
- int32_t OCP_Send(const hdl_t PhAppOCPCtx,const uint8_t* PprgbData,uint16_t PwLen)
- {
- int32_t i4Status = (int32_t)OCP_LIB_ERROR;
- #define PS_CNTX ((sAppOCPCtx_d*)PhAppOCPCtx)
- #define S_CONFIGURATION_RL (PS_CNTX->sConfigRL)
- #define S_HS (PS_CNTX->sHandshake)
- do
- {
-
- if((NULL == PS_CNTX) || (NULL == PprgbData))
- {
- i4Status = (int32_t)OCP_LIB_NULL_PARAM;
- break;
- }
-
- i4Status = Registry_ValidateHandleSessionID(PhAppOCPCtx);
- if(OCP_LIB_OK != i4Status)
- {
- break;
- }
-
- if((NULL == PS_CNTX->sConfigRL.sRL.psConfigTL) || (NULL == S_CONFIGURATION_RL.pfSend) ||
- (NULL == PS_CNTX->sConfigRL.sRL.psConfigTL->pfSend) || (NULL == PS_CNTX->sConfigRL.sRL.psConfigCL) ||
- (NULL == PS_CNTX->sConfigRL.sRL.psConfigCL->pfEncrypt))
- {
- i4Status = (int32_t)OCP_LIB_NULL_PARAM;
- break;
- }
-
- if(S_HS.eAuthState == eAuthSessionClosed)
- {
- i4Status = (int32_t)OCP_LIB_OPERATION_NOT_ALLOWED;
- break;
- }
-
- if(S_HS.eAuthState != eAuthCompleted)
- {
- i4Status = (int32_t)OCP_LIB_AUTHENTICATION_NOTDONE;
- break;
- }
-
- if(0x00 == PwLen)
- {
- i4Status = (int32_t)OCP_LIB_LENZERO_ERROR;
- break;
- }
-
- if(MAX_APP_DATALEN(PhAppOCPCtx) < PwLen)
- {
- i4Status = (int32_t)OCP_LIB_INVALID_LEN;
- break;
- }
-
- S_CONFIGURATION_RL.sRL.bContentType = CONTENTTYPE_APP_DATA;
- S_CONFIGURATION_RL.sRL.bMemoryAllocated = FALSE;
-
- i4Status = S_CONFIGURATION_RL.pfSend(&S_CONFIGURATION_RL.sRL, (uint8_t*)PprgbData, PwLen);
- if(OCP_RL_OK != i4Status)
- {
- break;
- }
- i4Status = (int32_t)OCP_LIB_OK;
- }while(FALSE);
- #undef PS_CNTX
- #undef S_CONFIGURATION_RL
- #undef S_HS
- return i4Status;
- }
- int32_t OCP_Receive(const hdl_t PhAppOCPCtx, uint8_t* PprgbData, uint16_t* PpwLen, uint16_t PwTimeout)
- {
- int32_t i4Status = (int32_t)OCP_LIB_ERROR;
- sbBlob_d sAppData;
- int32_t i4Alert;
- uint32_t dwStarttime;
- #define PS_CNTX ((sAppOCPCtx_d*)PhAppOCPCtx)
- #define S_CONFIGURATION_RL (PS_CNTX->sConfigRL)
- #define S_HS (PS_CNTX->sHandshake)
- do
- {
-
- if((NULL == PS_CNTX) || (NULL == PprgbData) || (NULL == PpwLen))
- {
- i4Status = (int32_t)OCP_LIB_NULL_PARAM;
- break;
- }
-
- i4Status = Registry_ValidateHandleSessionID(PhAppOCPCtx);
- if(OCP_LIB_OK != i4Status)
- {
- break;
- }
-
- if((NULL == PS_CNTX->sConfigRL.sRL.psConfigTL) || (NULL == S_CONFIGURATION_RL.pfRecv) ||
- (NULL == PS_CNTX->sConfigRL.sRL.psConfigTL->pfRecv)|| (NULL == PS_CNTX->sConfigRL.sRL.psConfigCL) ||
- (NULL == PS_CNTX->sConfigRL.sRL.psConfigCL->pfDecrypt))
- {
- i4Status = (int32_t)OCP_LIB_NULL_PARAM;
- break;
- }
-
- if(S_HS.eAuthState == eAuthSessionClosed)
- {
- i4Status = (int32_t)OCP_LIB_OPERATION_NOT_ALLOWED;
- break;
- }
-
- if(S_HS.eAuthState != eAuthCompleted)
- {
- i4Status = (int32_t)OCP_LIB_AUTHENTICATION_NOTDONE;
- break;
- }
-
- if(0x00 == *PpwLen)
- {
- i4Status = (int32_t)OCP_LIB_LENZERO_ERROR;
- break;
- }
- if(0x00 == PwTimeout)
- {
- i4Status = (int32_t)OCP_LIB_INVALID_TIMEOUT;
- break;
- }
- if(NULL == PS_CNTX->pAppDataBuf)
- {
- PS_CNTX->pAppDataBuf = OCP_MALLOC(TLBUFFER_SIZE);
- if(NULL == PS_CNTX->pAppDataBuf)
- {
- i4Status = (int32_t)OCP_LIB_MALLOC_FAILURE;
- break;
- }
- }
- PS_CNTX->sConfigRL.sRL.psConfigTL->sTL.wTimeout = PwTimeout;
-
- dwStarttime = pal_os_timer_get_time_in_milliseconds();
- do
- {
- PS_CNTX->sConfigRL.sRL.bContentType = CONTENTTYPE_APP_DATA;
- sAppData.prgbStream = PS_CNTX->pAppDataBuf;
- sAppData.wLen = TLBUFFER_SIZE;
- i4Status = PS_CNTX->sConfigRL.pfRecv((sRL_d*)&(PS_CNTX->sConfigRL.sRL), sAppData.prgbStream, &sAppData.wLen);
-
- if((int32_t)OCP_RL_APPDATA_RECEIVED == i4Status)
- {
- if(sAppData.wLen > *PpwLen)
- {
- i4Status = (int32_t)OCP_LIB_INSUFFICIENT_MEMORY;
- break;
- }
- *PpwLen = sAppData.wLen;
- Utility_Memmove(PprgbData, sAppData.prgbStream, sAppData.wLen);
- i4Status = OCP_LIB_OK;
- break;
- }
-
- if((int32_t)OCP_RL_ALERT_RECEIVED == i4Status)
- {
- i4Status = Alert_ProcessMsg(&sAppData,&i4Alert);
- if(((int32_t)OCP_AL_FATAL_ERROR == i4Alert))
- {
- S_HS.eAuthState = eAuthSessionClosed;
- i4Status = i4Alert;
- break;
- }
- }
-
- if(((int32_t)OCP_RL_MALLOC_FAILURE == i4Status))
- {
-
- break;
- }
-
- if(((int32_t)CMD_LIB_DECRYPT_FAILURE == i4Status))
- {
- i4Status = (int32_t)OCP_LIB_DECRYPT_FAILURE;
-
- break;
- }
-
- if((int32_t)OCP_RL_OK == i4Status)
- {
- i4Status = DtlsHS_VerifyHR(sAppData.prgbStream, sAppData.wLen);
- if(i4Status == OCP_HL_OK)
- {
- SEND_ALERT(&PS_CNTX->sConfigRL,(int32_t) OCP_LIB_NO_RENEGOTIATE);
- }
- }
- if((uint32_t)(pal_os_timer_get_time_in_milliseconds() - dwStarttime) > (uint32_t)PwTimeout)
- {
- i4Status = (int32_t)OCP_LIB_TIMEOUT;
- break;
- }
-
- PS_CNTX->sConfigRL.sRL.psConfigTL->sTL.wTimeout = (uint16_t)(PwTimeout - (uint16_t)(pal_os_timer_get_time_in_milliseconds() - dwStarttime));
- }while(TRUE);
- }while(FALSE);
- if((OCP_LIB_OK != i4Status) && (NULL != PpwLen))
- {
- *PpwLen = 0x00;
- }
-
- if(((int32_t)OCP_LIB_NULL_PARAM != i4Status) && ((int32_t)OCP_LIB_SESSIONID_UNAVAILABLE != i4Status) &&
- ((int32_t)OCP_LIB_OPERATION_NOT_ALLOWED != i4Status) && ((int32_t)OCP_LIB_AUTHENTICATION_NOTDONE != i4Status) &&
- ((int32_t)OCP_LIB_LENZERO_ERROR != i4Status) && ((int32_t)OCP_LIB_INVALID_TIMEOUT != i4Status) &&
- (NULL != PS_CNTX->pAppDataBuf) && (PS_CNTX->sConfigRL.sRL.bMultipleRecord == 0))
- {
- OCP_FREE(PS_CNTX->pAppDataBuf);
- PS_CNTX->pAppDataBuf = NULL;
- }
- #undef PS_CNTX
- #undef S_CONFIGURATION_RL
- #undef S_HS
- return i4Status;
- }
- int32_t OCP_Disconnect(hdl_t PhAppOCPCtx)
- {
- int32_t i4Status = (int32_t)OCP_LIB_ERROR;
- uint16_t wSessionId = 0;
- #define PS_CNTX ((sAppOCPCtx_d*)PhAppOCPCtx)
- #define S_CONFIGURATION_TL (PS_CNTX->sConfigRL.sRL.psConfigTL)
- #define S_CONFIGURATION_CL (PS_CNTX->sConfigRL.sRL.psConfigCL)
- do
- {
-
- if(NULL == PS_CNTX)
- {
- i4Status = (int32_t)OCP_LIB_NULL_PARAM;
- break;
- }
-
- i4Status = Registry_ValidateHandleSessionID(PhAppOCPCtx);
- if(OCP_LIB_OK != i4Status)
- {
- break;
- }
-
- if((NULL == S_CONFIGURATION_TL) || (NULL == S_CONFIGURATION_CL) || (NULL == S_CONFIGURATION_CL->pfClose) ||
- (NULL == S_CONFIGURATION_TL->pfDisconnect) || (NULL == PS_CNTX->sConfigRL.pfClose))
- {
- i4Status = (int32_t)OCP_LIB_NULL_PARAM;
- break;
- }
-
- i4Status = Registry_GetHandleSessionID(PhAppOCPCtx, &wSessionId);
- if(OCP_LIB_OK != i4Status)
- {
- break;
- }
-
- CloseSession(PhAppOCPCtx, PS_CNTX->sHandshake.fFatalError, wSessionId);
- PhAppOCPCtx = NULL;
- }while(FALSE);
- #undef PS_CNTX
- #undef S_CONFIGURATION_TL
- #undef S_CONFIGURATION_CL
- return i4Status;
- }
- #endif
|