DtlsHandshakeProtocol.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  1. /**
  2. * MIT License
  3. *
  4. * Copyright (c) 2018 Infineon Technologies AG
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE
  23. *
  24. *
  25. * \file DtlsHandshakeProtocol.c
  26. *
  27. * \brief This file implements the DTLS Handshake protocol.
  28. *
  29. * \addtogroup grMutualAuth
  30. * @{
  31. */
  32. #include "optiga/dtls/DtlsHandshakeProtocol.h"
  33. #include "optiga/dtls/AlertProtocol.h"
  34. #include "optiga/optiga_dtls.h"
  35. #include "optiga/dtls/DtlsRecordLayer.h"
  36. #include "optiga/dtls/DtlsFlightHandler.h"
  37. #ifdef MODULE_ENABLE_DTLS_MUTUAL_AUTH
  38. ///Flight retransmission timeout default value for the first time
  39. #define DEFAULT_TIMEOUT 2
  40. ///Maximum Timeout value
  41. #define MAX_FLIGHT_TIMEOUT 60
  42. /// @cond hidden
  43. ///Offset for message type
  44. #define OFFSET_MSG_TYPE (0)
  45. ///Offset for Total length
  46. #define OFFSET_MSG_TOTAL_LENGTH (OFFSET_MSG_TYPE + 1) //1
  47. ///Offset for message sequence
  48. #define OFFSET_MSG_SEQUENCE (OFFSET_MSG_TOTAL_LENGTH + 3) //4
  49. ///Offset for fragment offset
  50. #define OFFSET_MSG_FRAGMENT_OFFSET (OFFSET_MSG_SEQUENCE + 2) //6
  51. ///Offset for message fragment length
  52. #define OFFSET_MSG_FRAG_LENGTH (OFFSET_MSG_FRAGMENT_OFFSET+ 3) //9
  53. ///Offset for message data
  54. #define OFFSET_MSG_DATA (OFFSET_MSG_FRAG_LENGTH + 3) //12
  55. ///Message header length
  56. #define LENGTH_MSG_HEADER (OFFSET_MSG_DATA)
  57. ///Offset for second byte of message fragment length field
  58. #define OFFSET_MSG_FRAG_LENGTH_2BYTE (OFFSET_MSG_FRAG_LENGTH + 1)
  59. ///Macro for Receive Flight
  60. #ifndef DISABLE_RECEIVE_FLIGHT
  61. #define REC_FLIGHT_INITIALIZE(PbLastProcFlight, PppsFlightHead, PpsMessageLayer) DtlsHS_RFlightInitialise(PbLastProcFlight, PppsFlightHead, PpsMessageLayer)
  62. #define REC_FLIGHT_PROCESS(PpbLastProcFlight, PppsRFlightHead, PpsMessageLayer, PbFlightTimeout) DtlsHS_RFlightProcess(PpbLastProcFlight, PppsRFlightHead, PpsMessageLayer, PbFlightTimeout)
  63. #else
  64. extern int32_t StubRFlightInitialise(uint8_t PbLastProcFlight, sFlightDetails_d** PppsFlightHead, sMsgLyr_d* PpsMessageLayer);
  65. extern int32_t StubRFlightProcess(uint8_t* PpbLastProcFlight, sFlightDetails_d** PppsRFlightHead, sMsgLyr_d* PpsMessageLayer, uint8_t PbFlightTimeout);
  66. #define REC_FLIGHT_INITIALIZE(PbLastProcFlight, PppsFlightHead, PpsMessageLayer) StubRFlightInitialise(PbLastProcFlight, PppsFlightHead, PpsMessageLayer)
  67. #define REC_FLIGHT_PROCESS(PpbLastProcFlight, PppsRFlightHead, PpsMessageLayer, PbFlightTimeout) StubRFlightProcess(PpbLastProcFlight, PppsRFlightHead, PpsMessageLayer, PbFlightTimeout)
  68. #endif
  69. ///Macro for Send Flight
  70. #ifndef DISABLE_SEND_FLIGHT
  71. #define SEND_FLIGHT_INITIALIZE(PbLastProcFlight, PppsFlightHead, PpsMessageLayer) DtlsHS_SFlightInitialise(PbLastProcFlight, PppsFlightHead, PpsMessageLayer)
  72. #define SEND_FLIGHT_PROCESS(PpbLastProcFlight, PpsSFlightHead, PpsMessageLayer) DtlsHS_SFlightProcess(PpbLastProcFlight, PpsSFlightHead, PpsMessageLayer)
  73. #else
  74. extern int32_t StubSFlightInitialise(uint8_t PbLastProcFlight, sFlightDetails_d** PppsFlightHead, sMsgLyr_d* PpsMessageLayer);
  75. extern int32_t StubSFlightProcess(uint8_t *PpbLastProcFlight, sFlightDetails_d* PpsSFlightHead, sMsgLyr_d* PpsMessageLayer);
  76. #define SEND_FLIGHT_INITIALIZE(PbLastProcFlight, PppsFlightHead, PpsMessageLayer) StubSFlightInitialise(PbLastProcFlight, PppsFlightHead, PpsMessageLayer)
  77. #define SEND_FLIGHT_PROCESS(PpbLastProcFlight, PpsSFlightHead, PpsMessageLayer) StubSFlightProcess(PpbLastProcFlight, PpsSFlightHead, PpsMessageLayer)
  78. #endif
  79. #define FLIGHTLoHi(x,y) ((x) | (y<<8))
  80. #define MSGLoHi(x,y) ((x) | (y<<8))
  81. #define IsEVEN_FLIGHT(X) (((X%2) == 0) ? 1 : 0)
  82. ///Flight mapping table for the send flights
  83. const sFlightTable_d rgsSFlightInfo[] = {
  84. {FLIGHTLoHi((uint8_t)eFlight1, \
  85. (uint8_t)eMandatory), {MSGLoHi((uint8_t)eClientHello, (uint8_t)eMandatory), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF)},\
  86. DtlsHS_Flight1Handler},
  87. {FLIGHTLoHi((uint8_t)eFlight3, \
  88. (uint8_t)eOptional), {MSGLoHi((uint8_t)eClientHello, (uint8_t)eOptional), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF)}, \
  89. DtlsHS_Flight3Handler},
  90. {FLIGHTLoHi((uint8_t)eFlight5, (uint8_t)eMandatory), \
  91. {MSGLoHi((uint8_t)eClientCertificate, (uint8_t)eOptional), MSGLoHi((uint8_t)eClientKeyExchange, (uint8_t)eMandatory), MSGLoHi((uint8_t)eCertificateVerify, (uint8_t)eOptional), MSGLoHi((uint8_t)eChangeCipherSpec, (uint8_t)eMandatory), MSGLoHi((uint8_t)eClientFinished, (uint8_t)eMandatory), MSGLoHi(0xFF, 0xFF)}, \
  92. DtlsHS_Flight5Handler},
  93. };
  94. ///Flight mapping table for the receive flights
  95. const sFlightTable_d rgsRFlightInfo[] = {
  96. {FLIGHTLoHi((uint8_t)eFlight2, (uint8_t)eOptional), \
  97. {MSGLoHi((uint8_t)eHelloVerifyRequest, (uint8_t)eOptional), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF)}, \
  98. DtlsHS_Flight2Handler},
  99. {FLIGHTLoHi((uint8_t)eFlight4, (uint8_t)eMandatory), \
  100. {MSGLoHi((uint8_t)eServerHello, (uint8_t)eMandatory), MSGLoHi((uint8_t)eServerCertificate, (uint8_t)eMandatory), MSGLoHi((uint8_t)eServerKeyExchange, (uint8_t)eMandatory), MSGLoHi((uint8_t)eCertificateRequest, (uint8_t)eOptional), MSGLoHi((uint8_t)eServerHelloDone, (uint8_t)eMandatory), MSGLoHi(0xFF, 0xFF)}, \
  101. DtlsHS_Flight4Handler},
  102. {FLIGHTLoHi((uint8_t)eFlight6, (uint8_t)eMandatory), \
  103. {MSGLoHi((uint8_t)eChangeCipherSpec, (uint8_t)eMandatory), MSGLoHi((uint8_t)eServerFinished, (uint8_t)eMandatory), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF), MSGLoHi(0xFF, 0xFF)}, \
  104. DtlsHS_Flight6Handler},
  105. };
  106. /// @endcond
  107. /**
  108. * \brief Fragments a handshake message into smaller fragments.<br>
  109. */
  110. _STATIC_H int32_t DtlsHS_FragmentMsg(sFragmentMsg_d* PpsFragmentMsg);
  111. /**
  112. * \brief Frees the complete message list of a flight.<br>
  113. */
  114. _STATIC_H void DtlsHS_FreeMessageList(sMsgInfo_d **PppsMsgListPtr);
  115. /**
  116. * \brief Frees flight list except the flight node of interest.<br>
  117. */
  118. _STATIC_H void DtlsHS_FreeFlightList(uint8_t PbFlightID, sFlightDetails_d** PppsFlightHead);
  119. /**
  120. * \brief Frees specified flight node.<br>
  121. */
  122. _STATIC_H void DtlsHS_FreeFlightNode(uint8_t PbFlightID, sFlightDetails_d** PppsFlightHead);
  123. /**
  124. * \brief Receives a handshake messages from the server.<br>
  125. */
  126. _STATIC_H int32_t DtlsHS_ReceiveFlightMessage(uint8_t* PpbLastProcFlight, sFlightDetails_d** PppsRFlightHead, sMsgLyr_d* PpsMessageLayer, uint8_t PbFlightTimeout,uint32_t PdwBasetime);
  127. /**
  128. * \brief Frees flight node.<br>
  129. */
  130. _STATIC_H void DtlsHS_ClearBuffer(sFlightDetails_d** PppsFlightHead);
  131. /**
  132. * \brief Checks a flight is in the list or not.<br>
  133. */
  134. _STATIC_H int32_t DtlsHS_CheckFlightList(sFlightDetails_d* PpsFlightHead, uint8_t PbFlightID);
  135. /**
  136. * \brief Initialises the Flight List for Send.<br>
  137. */
  138. _STATIC_H int32_t DtlsHS_SFlightInitialise(uint8_t PbLastProcFlight, sFlightDetails_d** PppsFlightHead, sMsgLyr_d* PpsMessageLayer);
  139. /**
  140. * \brief Initialises the Flight List for Receive.<br>
  141. */
  142. _STATIC_H int32_t DtlsHS_RFlightInitialise(uint8_t PbLastProcFlight, sFlightDetails_d** PppsFlightHead, sMsgLyr_d* PpsMessageLayer);
  143. /**
  144. * \brief Processes the send Flight.<br>
  145. */
  146. _STATIC_H int32_t DtlsHS_SFlightProcess(uint8_t *PpbLastProcFlight, sFlightDetails_d* PpsSFlightHead, sMsgLyr_d* PpsMessageLayer);
  147. /**
  148. * \brief Processes the receive Flight.<br>
  149. */
  150. _STATIC_H int32_t DtlsHS_RFlightProcess(uint8_t* PpbLastProcFlight, sFlightDetails_d** PppsRFlightHead, sMsgLyr_d* PpsMessageLayer, uint8_t PbFlightTimeout);
  151. /**
  152. * \brief Appends a Flight Node to the end of the list.<br>
  153. */
  154. _STATIC_H void DtlsHS_CreateFlightList(sFlightDetails_d** PppsFlightHead, sFlightDetails_d* PpsFlightNode);
  155. /**
  156. * \brief Creates a flight node based on the last processed flight and inserts the node to the head node.<br>
  157. */
  158. _STATIC_H int32_t DtlsHS_CreateFlightNode(uint8_t PbLastProcFlight, sFlightDetails_d** PppsFlightHead, sMsgLyr_d* PpsMessageLayer);
  159. /**
  160. * Fragments a handshake message into smaller fragments.<br>
  161. * Returns a fragment of the handshake message.
  162. *
  163. * \param[in,out] PpsFragmentMsg Pointer to a structure containing handshake message, fragment size and other
  164. * information required for fragmentation
  165. *
  166. * \retval #OCP_HL_OK Successful execution
  167. * \retval #OCP_HL_ERROR Failure in execution
  168. \if ENABLE_NULL_CHECKS
  169. * \retval #OCP_HL_NULL_PARAM Null parameter(s)
  170. \endif
  171. * \retval #OCP_HL_LENZERO_ERROR Length of input parameter is zero
  172. * \retval #OCP_HL_INVALID_FRAGMENT_SIZE Invalid fragment size
  173. * \retval #OCP_HL_INVALID_OFFSET_LEN Invalid offset length
  174. * \retval #OCP_HL_INSUFFICIENT_MEMORY Insufficient buffer size
  175. */
  176. _STATIC_H int32_t DtlsHS_FragmentMsg(sFragmentMsg_d* PpsFragmentMsg)
  177. {
  178. int32_t i4Status = (int32_t)OCP_HL_ERROR;
  179. puint8_t prgbSrcBuf;
  180. puint8_t prgbDesBuf;
  181. uint16_t wLength;
  182. do
  183. {
  184. #ifdef ENABLE_NULL_CHECKS
  185. //Null Check for input parameters
  186. if((NULL == PpsFragmentMsg) || (NULL == PpsFragmentMsg->psCompleteMsg) || (NULL == PpsFragmentMsg->psMsgFrag)
  187. || (NULL == PpsFragmentMsg->psCompleteMsg->prgbStream) ||(NULL == PpsFragmentMsg->psMsgFrag->prgbStream))
  188. {
  189. i4Status = (int32_t)OCP_HL_NULL_PARAM;
  190. break;
  191. }
  192. #endif
  193. //Length validation for input parameters
  194. if((0 == PpsFragmentMsg->psCompleteMsg->wLen) || (0 == PpsFragmentMsg->psMsgFrag->wLen))
  195. {
  196. i4Status = (int32_t)OCP_HL_LENZERO_ERROR;
  197. break;
  198. }
  199. //Invalid Fragment size
  200. if(0 == PpsFragmentMsg->wFragmentSize)
  201. {
  202. i4Status = (int32_t)OCP_HL_INVALID_FRAGMENT_SIZE;
  203. break;
  204. }
  205. //Check for sufficient buffer size
  206. if(PpsFragmentMsg->psMsgFrag->wLen < PpsFragmentMsg->wFragmentSize)
  207. {
  208. i4Status = (int32_t)OCP_HL_INSUFFICIENT_MEMORY;
  209. break;
  210. }
  211. //Current offset check
  212. if(PpsFragmentMsg->wCurrentOffset >= PpsFragmentMsg->psCompleteMsg->wLen)
  213. {
  214. i4Status = (int32_t)OCP_HL_INVALID_OFFSET_LEN;
  215. break;
  216. }
  217. if(0 == PpsFragmentMsg->wRemainingLen)
  218. {
  219. i4Status = (int32_t)OCP_HL_LENZERO_ERROR;
  220. break;
  221. }
  222. if( 0 == PpsFragmentMsg->wCurrentOffset)
  223. {
  224. //Assign the address to copy the message
  225. prgbSrcBuf = PpsFragmentMsg->psCompleteMsg->prgbStream;
  226. prgbDesBuf = PpsFragmentMsg->psMsgFrag->prgbStream;
  227. //If the fragment size is greater than complete message size
  228. //Length is set to message size
  229. //else
  230. //Length is set to fragment size
  231. wLength = (PpsFragmentMsg->wFragmentSize > PpsFragmentMsg->psCompleteMsg->wLen)? PpsFragmentMsg->psCompleteMsg->wLen : PpsFragmentMsg->wFragmentSize;
  232. PpsFragmentMsg->psMsgFrag->wLen = wLength;
  233. }
  234. else
  235. {
  236. //Copy the message header to psMsgFrag
  237. Utility_Memmove(PpsFragmentMsg->psMsgFrag->prgbStream, PpsFragmentMsg->psCompleteMsg->prgbStream,
  238. LENGTH_MSG_HEADER);
  239. prgbSrcBuf = PpsFragmentMsg->psCompleteMsg->prgbStream + OFFSET_MSG_DATA + PpsFragmentMsg->wCurrentOffset;
  240. prgbDesBuf = PpsFragmentMsg->psMsgFrag->prgbStream + OFFSET_MSG_DATA;
  241. //If the remaining length is less than fragment size(Last fragment)
  242. if(PpsFragmentMsg->wFragmentSize > (PpsFragmentMsg->wRemainingLen + LENGTH_MSG_HEADER))
  243. {
  244. //Assign the address to copy the message of size (wRemainingLen) from current offset to psMsgFrag at index msgHeaderLen
  245. wLength = (uint16_t)PpsFragmentMsg->wRemainingLen;
  246. PpsFragmentMsg->psMsgFrag->wLen = (uint16_t)(PpsFragmentMsg->wRemainingLen + LENGTH_MSG_HEADER);
  247. }
  248. else
  249. {
  250. //Assign the address to copy the message of size (wFragmentSize - msgHeaderLen) from current offset to psMsgFrag at index msgHeaderLen
  251. wLength = PpsFragmentMsg->wFragmentSize - LENGTH_MSG_HEADER;
  252. PpsFragmentMsg->psMsgFrag->wLen = PpsFragmentMsg->wFragmentSize;
  253. }
  254. }
  255. //To copy the Fragmented message to psMsgFrag
  256. Utility_Memmove(prgbDesBuf, prgbSrcBuf, wLength);
  257. if(PpsFragmentMsg->wFragmentSize < PpsFragmentMsg->psCompleteMsg->wLen)
  258. {
  259. //Update the fragment header with current offset at index fragment offset
  260. Utility_SetUint24 ((PpsFragmentMsg->psMsgFrag->prgbStream + OFFSET_MSG_FRAGMENT_OFFSET),PpsFragmentMsg->wCurrentOffset);
  261. if(PpsFragmentMsg->wFragmentSize > (PpsFragmentMsg->wRemainingLen + LENGTH_MSG_HEADER))
  262. {
  263. //Update the fragment length with wRemainingLen at index fragment length(Last Fragment)
  264. Utility_SetUint16((PpsFragmentMsg->psMsgFrag->prgbStream + OFFSET_MSG_FRAG_LENGTH_2BYTE),(uint16_t)PpsFragmentMsg->wRemainingLen);
  265. }
  266. else
  267. {
  268. //Update the fragment length with (fragment size - header length) at index fragment length
  269. Utility_SetUint16((PpsFragmentMsg->psMsgFrag->prgbStream + OFFSET_MSG_FRAG_LENGTH_2BYTE),(PpsFragmentMsg->wFragmentSize - LENGTH_MSG_HEADER));
  270. }
  271. }
  272. //Update the current offset and remaining length
  273. //If Last Fragment
  274. if(PpsFragmentMsg->wFragmentSize > (PpsFragmentMsg->wRemainingLen + LENGTH_MSG_HEADER))
  275. {
  276. PpsFragmentMsg->wCurrentOffset += PpsFragmentMsg->wRemainingLen;
  277. PpsFragmentMsg->wRemainingLen = 0 ;
  278. }
  279. //For other fragment
  280. else
  281. {
  282. PpsFragmentMsg->wCurrentOffset += (uint16_t)(PpsFragmentMsg->wFragmentSize - LENGTH_MSG_HEADER);
  283. PpsFragmentMsg->wRemainingLen -= (uint16_t)(PpsFragmentMsg->wFragmentSize - LENGTH_MSG_HEADER);
  284. }
  285. i4Status = (int32_t)OCP_HL_OK;
  286. }while(0);
  287. return i4Status;
  288. }
  289. /**
  290. * Frees the complete message list of a flight.<br>
  291. *
  292. * \param[in,out] PppsMsgListPtr Pointer to Message list
  293. *
  294. */
  295. _STATIC_H void DtlsHS_FreeMessageList(sMsgInfo_d **PppsMsgListPtr)
  296. {
  297. sMsgInfo_d *pMsgNodeAPtr;
  298. sMsgInfo_d *pMsgNodeBPtr = NULL;
  299. do
  300. {
  301. if((NULL == PppsMsgListPtr) || (NULL == *PppsMsgListPtr))
  302. {
  303. break;
  304. }
  305. pMsgNodeAPtr = *PppsMsgListPtr;
  306. do
  307. {
  308. if(NULL != pMsgNodeAPtr->psMsgMapPtr)
  309. {
  310. OCP_FREE(pMsgNodeAPtr->psMsgMapPtr);
  311. pMsgNodeAPtr->psMsgMapPtr = NULL;
  312. }
  313. if(NULL != pMsgNodeAPtr->psMsgHolder)
  314. {
  315. OCP_FREE(pMsgNodeAPtr->psMsgHolder);
  316. pMsgNodeAPtr->psMsgHolder = NULL;
  317. }
  318. pMsgNodeBPtr = pMsgNodeAPtr->psNext;
  319. OCP_FREE(pMsgNodeAPtr);
  320. pMsgNodeAPtr = pMsgNodeBPtr;
  321. }while(pMsgNodeBPtr != NULL);
  322. *PppsMsgListPtr = NULL;
  323. }while(0);
  324. }
  325. /**
  326. * Frees all flight node from flight list except the flight node of interest.<br>
  327. *
  328. * \param[in] PbFlightID Flight ID of the flight to be freed
  329. * \param[in,out] PppsFlightHead Pointer to beginning of flight list
  330. *
  331. */
  332. _STATIC_H void DtlsHS_FreeFlightList(uint8_t PbFlightID, sFlightDetails_d** PppsFlightHead)
  333. {
  334. sFlightDetails_d *pNodeToFreePtr = NULL;
  335. sFlightDetails_d *pCurrentNode = NULL, *pPreviousNode = NULL;
  336. do
  337. {
  338. if(NULL != PppsFlightHead)
  339. {
  340. pCurrentNode = *PppsFlightHead;
  341. pPreviousNode = pCurrentNode;
  342. if(NULL != pCurrentNode)
  343. {
  344. do
  345. {
  346. if(FLIGHTID(pCurrentNode->wFlightDecp) != PbFlightID)
  347. {
  348. if(NULL != pCurrentNode->sFlightStats.psMessageList)
  349. {
  350. DtlsHS_FreeMessageList(&(pCurrentNode->sFlightStats.psMessageList));
  351. }
  352. pNodeToFreePtr = pCurrentNode;
  353. if(pNodeToFreePtr == *PppsFlightHead)
  354. {
  355. *PppsFlightHead = pNodeToFreePtr->psNext;
  356. }
  357. else
  358. {
  359. pPreviousNode->psNext = pCurrentNode->psNext;
  360. }
  361. OCP_FREE(pNodeToFreePtr);
  362. break;
  363. }
  364. pPreviousNode = pCurrentNode;
  365. pCurrentNode = pCurrentNode->psNext;
  366. }while(NULL != pCurrentNode);
  367. }
  368. }
  369. }while(NULL != pCurrentNode);
  370. }
  371. /**
  372. * Frees a flight node from the flight list which as two flight node at a time.<br>
  373. *
  374. * \param[in] PbFlightID Flight ID of the flight to be freed
  375. * \param[in,out] PppsFlightHead Pointer to beginning of flight list
  376. */
  377. _STATIC_H void DtlsHS_FreeFlightNode(uint8_t PbFlightID, sFlightDetails_d** PppsFlightHead)
  378. {
  379. sFlightDetails_d* pFlightTrav = NULL;
  380. sFlightDetails_d *pNodeToFreePtr = NULL;
  381. if((NULL != PppsFlightHead) && (NULL != *PppsFlightHead))
  382. {
  383. pFlightTrav = *PppsFlightHead;
  384. while(NULL != pFlightTrav)
  385. {
  386. if(FLIGHTID(pFlightTrav->wFlightDecp) == PbFlightID)
  387. {
  388. if(NULL != pFlightTrav->sFlightStats.psMessageList)
  389. {
  390. DtlsHS_FreeMessageList(&(pFlightTrav->sFlightStats.psMessageList));
  391. }
  392. pNodeToFreePtr = pFlightTrav;
  393. if(pNodeToFreePtr == *PppsFlightHead)
  394. {
  395. *PppsFlightHead = pNodeToFreePtr->psNext;
  396. }
  397. OCP_FREE(pNodeToFreePtr);
  398. break;
  399. }
  400. pFlightTrav = pFlightTrav->psNext;
  401. }
  402. }
  403. }
  404. /**
  405. * Receives a handshake message from the server.<br>
  406. * Under some erroneous conditions, error codes from Record Layer and Handshake Layer can also be returned.<br>
  407. *
  408. * \param[in] PpbLastProcFlight pointer to the last processed flight number
  409. * \param[in] PppsRFlightHead Flight head node for the receive message
  410. * \param[in,out] PpsMessageLayer Pointer to structure containing information required for Message Layer
  411. * \param[in] PbFlightTimeout Flight timeout value
  412. * \param[in] PdwBasetime Time at which State changed to receive mode
  413. *
  414. * \retval #OCP_HL_OK Successful Execution
  415. * \retval #OCP_HL_ERROR Failure Execution
  416. */
  417. _STATIC_H int32_t DtlsHS_ReceiveFlightMessage(uint8_t* PpbLastProcFlight, sFlightDetails_d** PppsRFlightHead, sMsgLyr_d* PpsMessageLayer, uint8_t PbFlightTimeout,uint32_t PdwBasetime)
  418. {
  419. int32_t i4Status = (int32_t)OCP_HL_OK;
  420. int32_t i4Alert ;
  421. uint32_t dwFragLen = 0;
  422. uint16_t wTotalMsgLen;
  423. sbBlob_d sMessage;
  424. uint8_t bRecvCCSRecord;
  425. sbBlob_d sBlobMessage;
  426. sFlightDetails_d *pRNextFlight;
  427. sFlightDetails_d *pRFlightTrav;
  428. /// @cond hidden
  429. #define B_MULTIPLERECORD (PpsMessageLayer->psConfigRL->sRL.bMultipleRecord)
  430. /// @endcond
  431. do
  432. {
  433. //Assign buffer to store the received message from record layer interface
  434. sMessage.prgbStream = PpsMessageLayer->sTLMsg.prgbStream;
  435. sMessage.wLen = PpsMessageLayer->sTLMsg.wLen;
  436. do
  437. {
  438. PpsMessageLayer->psConfigRL->sRL.bContentType = CONTENTTYPE_HANDSHAKE;
  439. //Invoke Record Layer interface to get the message over UDP
  440. i4Status = PpsMessageLayer->psConfigRL->pfRecv(&PpsMessageLayer->psConfigRL->sRL, sMessage.prgbStream, &sMessage.wLen);
  441. //Enter Flight Handler only if its a valid record
  442. if((int32_t)OCP_RL_OK == i4Status)
  443. {
  444. wTotalMsgLen = sMessage.wLen;
  445. sBlobMessage.prgbStream = sMessage.prgbStream;
  446. sBlobMessage.wLen = wTotalMsgLen;
  447. i4Status = DtlsHS_MsgCheck(*PpbLastProcFlight, &sBlobMessage, PpsMessageLayer);
  448. if(OCP_FL_OK != i4Status)
  449. {
  450. i4Status = (int32_t)OCP_HL_IGNORE_RECORD;
  451. }
  452. if(OCP_FL_OK == i4Status)
  453. {
  454. bRecvCCSRecord = PpsMessageLayer->psConfigRL->sRL.bRecvCCSRecord;
  455. while(0 != wTotalMsgLen)
  456. {
  457. pRNextFlight = *PppsRFlightHead;
  458. if(bRecvCCSRecord != CCS_RECORD_RECV)
  459. {
  460. dwFragLen = Utility_GetUint24(sBlobMessage.prgbStream + OFFSET_MSG_FRAG_LENGTH);
  461. PpsMessageLayer->sMsg.wLen = (uint16_t)dwFragLen + LENGTH_MSG_HEADER;
  462. }
  463. else
  464. {
  465. PpsMessageLayer->sMsg.wLen = 0x01;
  466. }
  467. PpsMessageLayer->sMsg.prgbStream = sBlobMessage.prgbStream;
  468. do
  469. {
  470. pRFlightTrav = pRNextFlight ;
  471. //Invoke the Flight Handler
  472. i4Status = pRFlightTrav->pFlightHndlr(*PpbLastProcFlight, &(pRFlightTrav->sFlightStats), PpsMessageLayer);
  473. if((int32_t)OCP_FL_MSG_NOT_IN_FLIGHT != i4Status)
  474. {
  475. DtlsHS_FreeFlightList(FLIGHTID(pRFlightTrav->wFlightDecp), PppsRFlightHead);
  476. }
  477. pRNextFlight = pRFlightTrav->psNext;
  478. }while(NULL != pRNextFlight);
  479. //Error checks to be added
  480. if(((int32_t)OCP_HL_MALLOC_FAILURE == i4Status) || ((int32_t)OCP_FL_MSG_MAXCOUNT == i4Status) || ((int32_t)OCP_HL_BUFFER_OVERFLOW == i4Status) ||
  481. ((int32_t)OCP_FL_MALLOC_FAILURE == i4Status) || (((int32_t)OCP_FL_ERROR == i4Status)) || ((int32_t)OCP_FL_INT_ERROR == i4Status) || ((int32_t)OCP_FL_HS_ERROR == i4Status))
  482. {
  483. break;
  484. }
  485. if((((int32_t)DEV_ERROR_CODE_MASK & i4Status) == (int32_t)CMD_DEV_ERROR) || ((int32_t)OCP_FL_SEND_MSG_TO_OPTIGA_ERROR == i4Status))
  486. {
  487. break;
  488. }
  489. if(((int32_t)OCP_FL_OK == i4Status))
  490. {
  491. break;
  492. }
  493. if(bRecvCCSRecord != CCS_RECORD_RECV)
  494. {
  495. wTotalMsgLen -= (uint16_t)dwFragLen + LENGTH_MSG_HEADER;
  496. if(wTotalMsgLen != 0x00)
  497. {
  498. sBlobMessage.prgbStream += dwFragLen + LENGTH_MSG_HEADER;
  499. }
  500. }
  501. else
  502. {
  503. wTotalMsgLen-= PpsMessageLayer->sMsg.wLen;
  504. }
  505. sBlobMessage.wLen = wTotalMsgLen;
  506. }
  507. }
  508. //Error checks to be added
  509. if(((int32_t)OCP_HL_MALLOC_FAILURE == i4Status) || ((int32_t)OCP_FL_MSG_MAXCOUNT == i4Status) || ((int32_t)OCP_HL_BUFFER_OVERFLOW == i4Status) ||
  510. ((int32_t)OCP_FL_MALLOC_FAILURE == i4Status) || (((int32_t)OCP_FL_ERROR == i4Status)) || ((int32_t)OCP_FL_INT_ERROR == i4Status) || ((int32_t)OCP_FL_HS_ERROR == i4Status))
  511. {
  512. break;
  513. }
  514. //Device error from Security Chip
  515. if((((int32_t)DEV_ERROR_CODE_MASK & i4Status) == (int32_t)CMD_DEV_ERROR) || ((int32_t)OCP_FL_SEND_MSG_TO_OPTIGA_ERROR == i4Status))
  516. {
  517. break;
  518. }
  519. //If complete flight is received come out of the loop
  520. if((int32_t)OCP_FL_OK == i4Status)
  521. {
  522. *PpbLastProcFlight = FLIGHTID((*PppsRFlightHead)->wFlightDecp);
  523. B_MULTIPLERECORD = 0;
  524. i4Status = (int32_t)OCP_HL_OK;
  525. break;
  526. }
  527. }
  528. //Malloc failure
  529. if(((int32_t)OCP_RL_MALLOC_FAILURE == i4Status))
  530. {
  531. //Exit the state machine
  532. break;
  533. }
  534. //Alert record received
  535. if((int32_t)OCP_RL_ALERT_RECEIVED == i4Status)
  536. {
  537. i4Status = Alert_ProcessMsg(&sMessage,&i4Alert);
  538. if(((int32_t)OCP_AL_FATAL_ERROR == i4Alert))
  539. {
  540. i4Status = i4Alert;
  541. break;
  542. }
  543. }
  544. //Sequence overflow
  545. if((int32_t)OCP_RL_SEQUENCE_OVERFLOW == i4Status)
  546. {
  547. //Exit the state machine
  548. break;
  549. }
  550. if(((int32_t)OCP_RL_NO_DATA != i4Status) && ((int32_t)OCP_FL_RXING != i4Status))
  551. {
  552. i4Status = (int32_t)OCP_HL_IGNORE_RECORD;
  553. }
  554. //If timeout expired return timeout error and exit if flight status is not efreceived
  555. if(!TIMEELAPSED(PdwBasetime, PbFlightTimeout) && (((*PppsRFlightHead)->sFlightStats.bFlightState < (uint8_t)efReceived) || ((*PppsRFlightHead)->sFlightStats.bFlightState == (uint8_t)efReReceive)
  556. || ((*PppsRFlightHead)->sFlightStats.bFlightState == (uint8_t)efProcessed)))
  557. {
  558. i4Status = (int32_t)OCP_HL_TIMEOUT;
  559. break;
  560. }
  561. //Dynamically setting the UDP timeout
  562. PpsMessageLayer->psConfigRL->sRL.psConfigTL->sTL.wTimeout = (uint16_t)((PbFlightTimeout*1000) - (uint32_t)(pal_os_timer_get_time_in_milliseconds() - PdwBasetime));
  563. //If multiple record is received in a single datagram loop back and receive other records
  564. }while(0 != B_MULTIPLERECORD);
  565. }while(FALSE);
  566. /// @cond hidden
  567. #undef B_MULTIPLERECORD
  568. /// @endcond
  569. return i4Status;
  570. }
  571. /**
  572. * Prepares Handshake message header<br>
  573. *
  574. * \param[in,out] PpbMsgHeader Pointer to buffer where handshake message header is formed
  575. * \param[in] PpsMsgInfo Pointer to #sMsgInfo_d
  576. *
  577. * \retval #OCP_HL_OK Successful execution
  578. \if ENABLE_NULL_CHECKS
  579. * \retval #OCP_HL_NULL_PARAM Null Parameters
  580. \endif
  581. */
  582. int32_t DtlsHS_PrepareMsgHeader(uint8_t* PpbMsgHeader, const sMsgInfo_d *PpsMsgInfo)
  583. {
  584. int32_t i4Status = (int32_t)OCP_HL_NULL_PARAM;
  585. do
  586. {
  587. #ifdef ENABLE_NULL_CHECKS
  588. if((NULL == PpbMsgHeader)||(NULL == PpsMsgInfo))
  589. {
  590. break;
  591. }
  592. #endif
  593. *(PpbMsgHeader+OFFSET_MSG_TYPE) = PpsMsgInfo->bMsgType;
  594. Utility_SetUint24(PpbMsgHeader+OFFSET_MSG_TOTAL_LENGTH, PpsMsgInfo->dwMsgLength);
  595. Utility_SetUint16(PpbMsgHeader+OFFSET_MSG_SEQUENCE,PpsMsgInfo->wMsgSequence);
  596. Utility_SetUint24(PpbMsgHeader+OFFSET_MSG_FRAGMENT_OFFSET,(uint32_t)0x00);
  597. Utility_SetUint24(PpbMsgHeader+OFFSET_MSG_FRAG_LENGTH, PpsMsgInfo->dwMsgLength);
  598. i4Status = (int32_t)OCP_HL_OK;
  599. }while(FALSE);
  600. return i4Status;
  601. }
  602. /**
  603. * Frees flight node.<br>
  604. *
  605. * \param[in,out] PppsFlightHead Pointer to beginning of flight list
  606. *
  607. */
  608. _STATIC_H void DtlsHS_ClearBuffer(sFlightDetails_d** PppsFlightHead)
  609. {
  610. sFlightDetails_d* pFlightTrav = *PppsFlightHead;
  611. sFlightDetails_d *pNodeToFreePtr = NULL;
  612. do
  613. {
  614. if(NULL != pFlightTrav)
  615. {
  616. if(NULL != pFlightTrav->sFlightStats.psMessageList)
  617. {
  618. DtlsHS_FreeMessageList(&(pFlightTrav->sFlightStats.psMessageList));
  619. }
  620. pNodeToFreePtr = pFlightTrav;
  621. pFlightTrav = pFlightTrav->psNext;
  622. OCP_FREE(pNodeToFreePtr);
  623. }
  624. }while(NULL != pFlightTrav);
  625. *PppsFlightHead = NULL ;
  626. }
  627. /**
  628. * Checks a flight is in the list or not.<br>
  629. *
  630. * \param[in] PpsFlightHead Pointer to list of Flights
  631. * \param[in] PbFlightID Flight number
  632. *
  633. * \retval #OCP_HL_OK Successful Execution
  634. * \retval #OCP_HL_ERROR Failure Execution
  635. */
  636. _STATIC_H int32_t DtlsHS_CheckFlightList(sFlightDetails_d* PpsFlightHead, uint8_t PbFlightID)
  637. {
  638. int32_t i4Status = (int32_t)OCP_HL_ERROR;
  639. sFlightDetails_d* psFlightList = PpsFlightHead;
  640. do
  641. {
  642. if(NULL == PpsFlightHead)
  643. {
  644. break;
  645. }
  646. do
  647. {
  648. if(FLIGHTID(psFlightList->wFlightDecp) == PbFlightID)
  649. {
  650. i4Status = (int32_t)OCP_HL_OK;
  651. }
  652. psFlightList = psFlightList->psNext;
  653. }while(NULL != psFlightList);
  654. }while(0);
  655. return i4Status;
  656. }
  657. /**
  658. * Appends a Flight Node to the end of the list.<br>
  659. *
  660. * \param[in,out] PppsFlightHead Pointer to Flight head node list
  661. * \param[in] PpsFlightNode Pointer to new Flight node
  662. *
  663. */
  664. _STATIC_H void DtlsHS_CreateFlightList(sFlightDetails_d** PppsFlightHead, sFlightDetails_d* PpsFlightNode)
  665. {
  666. sFlightDetails_d* psFlightTrav = *PppsFlightHead;
  667. do
  668. {
  669. if(NULL == psFlightTrav)
  670. {
  671. PpsFlightNode->psNext = NULL;
  672. *PppsFlightHead = PpsFlightNode;
  673. }
  674. else
  675. {
  676. while(psFlightTrav->psNext != NULL)
  677. {
  678. psFlightTrav = psFlightTrav->psNext;
  679. }
  680. psFlightTrav->psNext = PpsFlightNode;
  681. PpsFlightNode->psNext = NULL;
  682. }
  683. }while(0);
  684. }
  685. /**
  686. * Creates a flight node based on the last processed flight and inserts the node to the head node..<br>
  687. *
  688. * \param[in,out] PbLastProcFlight Last processed flight number
  689. * \param[in] PppsFlightHead Pointer to the flight head node
  690. * \param[in] PpsMessageLayer Pointer to message information
  691. *
  692. * \retval #OCP_HL_OK Successful Execution
  693. * \retval #OCP_HL_MALLOC_FAILURE Malloc failure
  694. * \retval #OCP_HL_ERROR Failure Execution
  695. */
  696. _STATIC_H int32_t DtlsHS_CreateFlightNode(uint8_t PbLastProcFlight, sFlightDetails_d** PppsFlightHead, sMsgLyr_d* PpsMessageLayer)
  697. {
  698. int32_t i4Status = (int32_t)OCP_HL_OK;
  699. sFlightDetails_d* pFlightNode = NULL;
  700. do
  701. {
  702. pFlightNode = (sFlightDetails_d*)OCP_MALLOC(sizeof(sFlightDetails_d));
  703. if(NULL == pFlightNode)
  704. {
  705. i4Status = (int32_t)OCP_HL_MALLOC_FAILURE;
  706. break;
  707. }
  708. else
  709. {
  710. if((int32_t)OCP_FL_ERROR == DtlsHS_FlightNodeInit(pFlightNode, PbLastProcFlight))
  711. {
  712. OCP_FREE(pFlightNode);
  713. i4Status = (int32_t)OCP_HL_ERROR;
  714. break;
  715. }
  716. i4Status = pFlightNode->pFlightHndlr(PbLastProcFlight, &pFlightNode->sFlightStats, PpsMessageLayer);
  717. if(OCP_FL_OK != i4Status)
  718. {
  719. break;
  720. }
  721. DtlsHS_CreateFlightList(PppsFlightHead, pFlightNode);
  722. i4Status = (int32_t)OCP_HL_OK;
  723. }
  724. }while(0);
  725. return i4Status;
  726. }
  727. /**
  728. * Initialises the Flight List for Send.<br>
  729. *
  730. * \param[in] PbLastProcFlight Last processed flight ID
  731. * \param[in,out] PppsFlightHead Pointer to list of Flight nodes
  732. * \param[in] PpsMessageLayer Pointer to message information
  733. *
  734. * \retval #OCP_HL_OK Successful Execution
  735. * \retval #OCP_HL_ERROR Failure Execution
  736. */
  737. _STATIC_H int32_t DtlsHS_SFlightInitialise(uint8_t PbLastProcFlight, sFlightDetails_d** PppsFlightHead, sMsgLyr_d* PpsMessageLayer)
  738. {
  739. sFlightDetails_d* pFlightNode = NULL;
  740. sFlightDetails_d* pFlightTrav ;
  741. int32_t i4Status = (int32_t)OCP_HL_OK;
  742. do
  743. {
  744. #ifdef ENABLE_NULL_CHECKS
  745. if((NULL == PppsFlightHead) || (NULL == PpsMessageLayer) ||(NULL == PpsMessageLayer->psConfigRL->pfSend))
  746. {
  747. i4Status = (int32_t)OCP_HL_NULL_PARAM;
  748. break;
  749. }
  750. #endif
  751. pFlightTrav = *PppsFlightHead;
  752. if((NULL != pFlightTrav))
  753. {
  754. do
  755. {
  756. pFlightNode = pFlightTrav;
  757. pFlightTrav = pFlightTrav->psNext;
  758. if(PbLastProcFlight > FLIGHTID(pFlightNode->wFlightDecp))
  759. {
  760. if((int32_t)OCP_FL_OK == pFlightNode->pFlightHndlr(PbLastProcFlight, &pFlightNode->sFlightStats, PpsMessageLayer))
  761. {
  762. if(efDone == (eFlightState_d)pFlightNode->sFlightStats.bFlightState)
  763. {
  764. DtlsHS_FreeFlightNode(FLIGHTID(pFlightNode->wFlightDecp), PppsFlightHead);
  765. }
  766. }
  767. }
  768. else if(PbLastProcFlight == FLIGHTID(pFlightNode->wFlightDecp))
  769. {
  770. if(efTransmitted == (eFlightState_d)pFlightNode->sFlightStats.bFlightState)
  771. {
  772. pFlightNode->sFlightStats.bFlightState = (uint8_t)efReTransmit;
  773. }
  774. }
  775. }while(NULL != pFlightTrav);
  776. }
  777. //Create Flight List
  778. if(IsEVEN_FLIGHT(PbLastProcFlight))
  779. {
  780. if((int32_t)OCP_HL_ERROR == DtlsHS_CheckFlightList(*PppsFlightHead, PbLastProcFlight+1))
  781. {
  782. i4Status = DtlsHS_CreateFlightNode(PbLastProcFlight, PppsFlightHead, PpsMessageLayer);
  783. if(OCP_HL_OK != i4Status)
  784. {
  785. break;
  786. }
  787. }
  788. }
  789. }while(0);
  790. return i4Status;
  791. }
  792. /**
  793. * Initialises the Flight List for Receive.<br>
  794. *
  795. * \param[in] PbLastProcFlight Last processed flight ID
  796. * \param[in,out] PppsFlightHead Pointer to list of Flight nodes
  797. * \param[in] PpsMessageLayer Pointer to message information
  798. *
  799. * \retval #OCP_HL_OK Successful Execution
  800. * \retval #OCP_HL_ERROR Failure Execution
  801. */
  802. _STATIC_H int32_t DtlsHS_RFlightInitialise(uint8_t PbLastProcFlight, sFlightDetails_d** PppsFlightHead, sMsgLyr_d* PpsMessageLayer)
  803. {
  804. uint8_t bFlightA = 0;
  805. uint8_t bFlightB = 0;
  806. int32_t i4Status = (int32_t)OCP_HL_OK;
  807. do
  808. {
  809. //Create Flight List
  810. if((PbLastProcFlight == 1) || (PbLastProcFlight == 3))
  811. {
  812. bFlightA = (uint8_t)eFlight2;
  813. bFlightB = (uint8_t)eFlight4;
  814. }
  815. else if(PbLastProcFlight == 5)
  816. {
  817. bFlightA = (uint8_t)eFlight4;
  818. bFlightB = (uint8_t)eFlight6;
  819. }
  820. else
  821. {
  822. i4Status = (int32_t)OCP_HL_ERROR;
  823. break;
  824. }
  825. if((int32_t)OCP_HL_ERROR == DtlsHS_CheckFlightList(*PppsFlightHead, bFlightA))
  826. {
  827. //CreateFlightNode expects last processed flight ID, hence flight corrected with offset 1
  828. i4Status = DtlsHS_CreateFlightNode(bFlightA-1, PppsFlightHead, PpsMessageLayer);
  829. if(OCP_HL_OK != i4Status)
  830. {
  831. break;
  832. }
  833. if((int32_t)OCP_HL_ERROR == DtlsHS_CheckFlightList(*PppsFlightHead, bFlightB))
  834. {
  835. //CreateFlightNode expects last processed flight ID, hence flight corrected with offset 1
  836. i4Status = DtlsHS_CreateFlightNode(bFlightB-1, PppsFlightHead, PpsMessageLayer);
  837. if(OCP_HL_OK != i4Status)
  838. {
  839. break;
  840. }
  841. }
  842. }
  843. else if((uint8_t)efProcessed ==(*PppsFlightHead)->sFlightStats.bFlightState)
  844. {
  845. if((int32_t)OCP_HL_ERROR == DtlsHS_CheckFlightList(*PppsFlightHead, bFlightB))
  846. {
  847. //CreateFlightNode expects last processed flight ID, hence flight corrected with offset 1
  848. i4Status = DtlsHS_CreateFlightNode(bFlightB-1, PppsFlightHead, PpsMessageLayer);
  849. if(OCP_HL_OK != i4Status)
  850. {
  851. break;
  852. }
  853. }
  854. }
  855. }while(0);
  856. return i4Status;
  857. }
  858. /**
  859. * Processes the send Flight.<br>
  860. *
  861. * \param[in] PpbLastProcFlight Pointer to last processed flight ID
  862. * \param[in,out] PpsSFlightHead Pointer to list of Send Flight list
  863. * \param[in] PpsMessageLayer Message layer information
  864. *
  865. * \retval #OCP_HL_OK Successful Execution
  866. * \retval #OCP_HL_ERROR Failure Execution
  867. * \retval #OCP_FL_NOT_LISTED Flight not found in the list
  868. */
  869. _STATIC_H int32_t DtlsHS_SFlightProcess(uint8_t *PpbLastProcFlight, sFlightDetails_d* PpsSFlightHead, sMsgLyr_d* PpsMessageLayer)
  870. {
  871. int32_t i4Status = (int32_t)OCP_HL_ERROR;
  872. sFlightDetails_d* pSFlightTrav = PpsSFlightHead;
  873. do
  874. {
  875. if(NULL == pSFlightTrav)
  876. {
  877. i4Status = (int32_t)OCP_FL_NOT_LISTED;
  878. break;
  879. }
  880. do
  881. {
  882. i4Status = pSFlightTrav->pFlightHndlr(*PpbLastProcFlight, &pSFlightTrav->sFlightStats, PpsMessageLayer);
  883. if((int32_t)OCP_FL_OK != i4Status)
  884. {
  885. break;
  886. }
  887. if((uint8_t)efTransmitted == pSFlightTrav->sFlightStats.bFlightState)
  888. {
  889. *PpbLastProcFlight = FLIGHTID(pSFlightTrav->wFlightDecp);
  890. }
  891. pSFlightTrav = pSFlightTrav->psNext;
  892. }while(NULL != pSFlightTrav);
  893. if((int32_t)OCP_FL_OK == i4Status)
  894. {
  895. i4Status = (int32_t)OCP_HL_OK;
  896. }
  897. }while(0);
  898. return i4Status;
  899. }
  900. /**
  901. * Processes the receive Flight.<br>
  902. * Under some erroneous conditions, error codes from respective layer can also be returned.<br>
  903. *
  904. * \param[in] PpbLastProcFlight pointer to the last processed flight ID
  905. * \param[in] PppsRFlightHead Pointer to list of receivable Flight list
  906. * \param[in] PpsMessageLayer Message layer information
  907. * \param[in] PbFlightTimeout Flight time out value
  908. *
  909. * \retval #OCP_HL_OK Successful Execution
  910. * \retval #OCP_HL_ERROR Failure Execution
  911. \if ENABLE_NULL_CHECKS
  912. * \retval #OCP_HL_NULL_PARAM NULL parameters
  913. \endif
  914. */
  915. _STATIC_H int32_t DtlsHS_RFlightProcess(uint8_t* PpbLastProcFlight, sFlightDetails_d** PppsRFlightHead, sMsgLyr_d* PpsMessageLayer, uint8_t PbFlightTimeout)
  916. {
  917. int32_t i4Status = (int32_t)OCP_HL_ERROR;
  918. uint32_t dwBasetime;
  919. do
  920. {
  921. #ifdef ENABLE_NULL_CHECKS
  922. //NULL check
  923. if((NULL == PppsRFlightHead) || (NULL == PpsMessageLayer) || (NULL == PpsMessageLayer->psConfigRL) || (NULL == PpsMessageLayer->psConfigRL->pfRecv))
  924. {
  925. i4Status = (int32_t)OCP_HL_NULL_PARAM;
  926. break;
  927. }
  928. #endif
  929. //Start value for the Flight timeout
  930. dwBasetime = (uint32_t)pal_os_timer_get_time_in_milliseconds();
  931. do
  932. {
  933. i4Status = DtlsHS_ReceiveFlightMessage(PpbLastProcFlight, PppsRFlightHead, PpsMessageLayer, PbFlightTimeout, dwBasetime);
  934. //If timeout expired and complete flight is not received then return timeout error and come out of loop
  935. if((!TIMEELAPSED(dwBasetime, PbFlightTimeout) || ((int32_t)OCP_HL_TIMEOUT == i4Status)) && \
  936. ((int32_t)OCP_HL_OK != i4Status) && (((*PppsRFlightHead)->sFlightStats.bFlightState < (uint8_t)efReceived) ||
  937. ((*PppsRFlightHead)->sFlightStats.bFlightState == (uint8_t)efReReceive) || ((*PppsRFlightHead)->sFlightStats.bFlightState == (uint8_t)efProcessed)))
  938. {
  939. i4Status = (int32_t)OCP_HL_TIMEOUT;
  940. break;
  941. }
  942. //If complete flight message is not received or if no data is received from record layer loop back to get remaining message
  943. }while((i4Status == (int32_t)OCP_FL_RXING) || (i4Status == (int32_t)OCP_RL_NO_DATA) || (i4Status == (int32_t)OCP_HL_IGNORE_RECORD));
  944. }while(FALSE);
  945. return i4Status;
  946. }
  947. /**
  948. * Sends Handshake message to the server.Fragments the message if the message is greater than PMTU.<br>
  949. * Under some erroneous conditions, error codes from respective Layer can also be returned. <br>
  950. *
  951. * \param[in] PpsMsgPtr pointer to the structure containing message information
  952. * \param[in] PpsMessageLayer Pointer to the structure containing list of message
  953. *
  954. * \retval #OCP_HL_OK Successful Execution
  955. * \retval #OCP_HL_ERROR Failure Execution
  956. */
  957. int32_t DtlsHS_FSendMessage(const sMsgInfo_d* PpsMsgPtr, const sMsgLyr_d* PpsMessageLayer)
  958. {
  959. int32_t i4Status = (int32_t)OCP_HL_ERROR;
  960. uint8_t* pbTotalFragMem = NULL;
  961. sFragmentMsg_d sFragmentMsg;
  962. sbBlob_d sMessage;
  963. sbBlob_d sbBlobMessage;
  964. /// @cond hidden
  965. #define CHANGE_CIPHERSPEC_MSGSIZE 1
  966. /// @endcond
  967. do{
  968. if(PpsMsgPtr->bMsgType == (uint8_t)eChangeCipherSpec)
  969. {
  970. pbTotalFragMem = (uint8_t*)OCP_MALLOC(CHANGE_CIPHERSPEC_MSGSIZE + LENGTH_RL_HEADER);
  971. if(NULL == pbTotalFragMem)
  972. {
  973. i4Status = (int32_t)OCP_HL_MALLOC_FAILURE;
  974. break;
  975. }
  976. OCP_MEMCPY((pbTotalFragMem + LENGTH_RL_HEADER), PpsMsgPtr->psMsgHolder, PpsMsgPtr->dwMsgLength);
  977. PpsMessageLayer->psConfigRL->sRL.bContentType = CONTENTTYPE_CIPHER_SPEC;
  978. PpsMessageLayer->psConfigRL->sRL.bMemoryAllocated = TRUE;
  979. //Send Data
  980. i4Status = PpsMessageLayer->psConfigRL->pfSend(&PpsMessageLayer->psConfigRL->sRL,
  981. pbTotalFragMem,(uint16_t)(PpsMsgPtr->dwMsgLength + LENGTH_RL_HEADER));
  982. if(OCP_RL_OK != i4Status)
  983. {
  984. break;
  985. }
  986. i4Status = (int32_t)OCP_HL_OK;
  987. }
  988. else
  989. {
  990. //Assign pointer of the buffer where the message is buffered
  991. sbBlobMessage.prgbStream = PpsMsgPtr->psMsgHolder;
  992. sbBlobMessage.wLen = (uint16_t)PpsMsgPtr->dwMsgLength + OVERHEAD_LEN;
  993. //Move the pointer to the Handshake message header location
  994. sbBlobMessage.prgbStream+= (OVERHEAD_LEN - MSG_HEADER_LEN);
  995. sbBlobMessage.wLen-= (OVERHEAD_LEN - MSG_HEADER_LEN);
  996. //If the length of the message is within the given PMTU value
  997. if(sbBlobMessage.wLen <= PpsMessageLayer->wMaxPmtu - UDP_RECORD_OVERHEAD)
  998. {
  999. //Assign Buffer
  1000. pbTotalFragMem = (uint8_t*)OCP_MALLOC(sbBlobMessage.wLen + LENGTH_RL_HEADER);
  1001. if(NULL == pbTotalFragMem)
  1002. {
  1003. i4Status = (int32_t)OCP_HL_MALLOC_FAILURE;
  1004. break;
  1005. }
  1006. Utility_Memmove(pbTotalFragMem + LENGTH_RL_HEADER, sbBlobMessage.prgbStream, sbBlobMessage.wLen);
  1007. PpsMessageLayer->psConfigRL->sRL.bContentType = CONTENTTYPE_HANDSHAKE;
  1008. PpsMessageLayer->psConfigRL->sRL.bMemoryAllocated = TRUE;
  1009. //Send Data
  1010. i4Status = PpsMessageLayer->psConfigRL->pfSend(&PpsMessageLayer->psConfigRL->sRL,
  1011. pbTotalFragMem,sbBlobMessage.wLen + LENGTH_RL_HEADER);
  1012. if(OCP_RL_OK != i4Status)
  1013. {
  1014. break;
  1015. }
  1016. i4Status = (int32_t)OCP_HL_OK;
  1017. break;
  1018. }
  1019. else
  1020. {
  1021. sFragmentMsg.psCompleteMsg = &sbBlobMessage;
  1022. sFragmentMsg.wFragmentSize = PpsMessageLayer->wMaxPmtu - UDP_RECORD_OVERHEAD;
  1023. //Assign Buffer
  1024. pbTotalFragMem = (uint8_t*)OCP_MALLOC(sFragmentMsg.wFragmentSize + LENGTH_RL_HEADER);
  1025. if(NULL == pbTotalFragMem)
  1026. {
  1027. i4Status = (int32_t)OCP_HL_MALLOC_FAILURE;
  1028. break;
  1029. }
  1030. sFragmentMsg.psMsgFrag = &sMessage;
  1031. sMessage.prgbStream = pbTotalFragMem + LENGTH_RL_HEADER;
  1032. sMessage.wLen = sFragmentMsg.wFragmentSize;
  1033. sFragmentMsg.wRemainingLen = sbBlobMessage.wLen - LENGTH_MSG_HEADER;
  1034. sFragmentMsg.wCurrentOffset = 0;
  1035. do
  1036. {
  1037. i4Status = (int32_t)OCP_HL_ERROR;
  1038. //Fragment the message
  1039. i4Status = DtlsHS_FragmentMsg(&sFragmentMsg);
  1040. if(OCP_HL_OK != i4Status)
  1041. {
  1042. break;
  1043. }
  1044. PpsMessageLayer->psConfigRL->sRL.bContentType = CONTENTTYPE_HANDSHAKE;
  1045. PpsMessageLayer->psConfigRL->sRL.bMemoryAllocated = TRUE;
  1046. //Send Data
  1047. i4Status = PpsMessageLayer->psConfigRL->pfSend(&PpsMessageLayer->psConfigRL->sRL,
  1048. pbTotalFragMem,sMessage.wLen + LENGTH_RL_HEADER);
  1049. if(OCP_RL_OK != i4Status)
  1050. {
  1051. break;
  1052. }
  1053. i4Status = (int32_t)OCP_HL_OK;
  1054. }while(0 != sFragmentMsg.wRemainingLen);
  1055. }
  1056. }
  1057. }while(FALSE);
  1058. //Clear the message header list
  1059. if(NULL != pbTotalFragMem)
  1060. {
  1061. OCP_FREE(pbTotalFragMem);
  1062. }
  1063. /// @cond hidden
  1064. #undef CHANGE_CIPHERSPEC_MSGSIZE
  1065. /// @endcond
  1066. return i4Status;
  1067. }
  1068. /**
  1069. *
  1070. * Process and validates Handshake message header.<br>
  1071. *
  1072. * \param[in] PsBlobMessage Pointer to a blob with Message including header.
  1073. *
  1074. * \retval #OCP_HL_OK Successful execution
  1075. * \retval #OCP_HL_ERROR Failure in execution
  1076. * \retval #OCP_HL_LEN_MISMATCH Fragment length mismatch
  1077. * \retval #OCP_HL_TOTALLEN_MISMATCH Total length mismatch
  1078. * \retval #OCP_HL_BUFFER_OVERFLOW Message length exceeded the limit
  1079. *
  1080. */
  1081. int32_t DtlsHS_ProcHeader(sbBlob_d PsBlobMessage)
  1082. {
  1083. int32_t i4Status = (int32_t)OCP_HL_ERROR;
  1084. uint32_t dwFragLen;
  1085. uint32_t dwFragOffset;
  1086. do
  1087. {
  1088. if(!((((uint8_t)eServer_Certificate <=*PsBlobMessage.prgbStream) && ((uint8_t)eServer_Hello_Done >= *PsBlobMessage.prgbStream)) ||
  1089. ((uint8_t)eServer_Hello == *PsBlobMessage.prgbStream) || ((uint8_t)eHello_Verify_Request == *PsBlobMessage.prgbStream)||
  1090. ((uint8_t)eServer_Finished == *PsBlobMessage.prgbStream)))
  1091. {
  1092. i4Status = (int32_t)OCP_HL_INVALID_MSGTYPE;
  1093. break;
  1094. }
  1095. dwFragLen = Utility_GetUint24(PsBlobMessage.prgbStream + OFFSET_MSG_FRAG_LENGTH);
  1096. //check if length fragment len + hmsg header is less than total stream length
  1097. if((dwFragLen + LENGTH_MSG_HEADER) > PsBlobMessage.wLen)
  1098. {
  1099. //Length indicated by fragment length not available
  1100. i4Status = (int32_t)OCP_HL_LEN_MISMATCH;
  1101. break;
  1102. }
  1103. //Fragment offset,3 bytes
  1104. dwFragOffset = Utility_GetUint24(PsBlobMessage.prgbStream+OFFSET_MSG_FRAGMENT_OFFSET);
  1105. if((dwFragOffset+ dwFragLen) > Utility_GetUint24(PsBlobMessage.prgbStream+OFFSET_MSG_TOTAL_LENGTH))
  1106. {
  1107. i4Status = (int32_t)OCP_HL_TOTALLEN_MISMATCH;
  1108. break;
  1109. }
  1110. i4Status = (int32_t)OCP_HL_OK;
  1111. }while(0);
  1112. return i4Status;
  1113. }
  1114. /**
  1115. *
  1116. * Validates the Hello request message.<br>
  1117. *
  1118. * \param[in] PprgbData Pointer to the Hello Request Message.
  1119. * \param[in] PwLen Length of the Message.
  1120. *
  1121. * \retval #OCP_HL_OK Successful execution
  1122. * \retval #OCP_HL_INVALID_HRMSG Invalid Handshake message
  1123. *
  1124. */
  1125. int32_t DtlsHS_VerifyHR(const uint8_t* PprgbData, uint16_t PwLen)
  1126. {
  1127. int32_t i4Status = (int32_t)OCP_HL_INVALID_HRMSG;
  1128. uint32_t dwFragLen;
  1129. uint32_t dwFragOffset;
  1130. uint32_t dwTotalLen;
  1131. do
  1132. {
  1133. if(0x00 != *PprgbData)
  1134. {
  1135. //Invalid Msg type
  1136. break;
  1137. }
  1138. //Fragment Length,3 bytes
  1139. dwFragLen = Utility_GetUint24(PprgbData + OFFSET_MSG_FRAG_LENGTH);
  1140. //Fragment offset,3 bytes
  1141. dwFragOffset = Utility_GetUint24(PprgbData + OFFSET_MSG_FRAGMENT_OFFSET);
  1142. //Total Length,3 bytes
  1143. dwTotalLen = Utility_GetUint24(PprgbData + OFFSET_MSG_TOTAL_LENGTH);
  1144. if(PwLen != LENGTH_MSG_HEADER)
  1145. {
  1146. //Incorrect length
  1147. break;
  1148. }
  1149. if(0x00 != Utility_GetUint24(PprgbData + OFFSET_MSG_SEQUENCE))
  1150. {
  1151. //Incorrect Msg sequence
  1152. break;
  1153. }
  1154. //check if length fragment len + fragment 0ffset + total len is less than total stream length
  1155. if((dwFragLen + dwFragOffset + dwTotalLen) > 0x00)
  1156. {
  1157. //Length fields indicated in the message are incorrect
  1158. break;
  1159. }
  1160. i4Status = (int32_t)OCP_HL_OK;
  1161. }while(0);
  1162. return i4Status;
  1163. }
  1164. /**
  1165. * Performs a DTLS handshake.<br>
  1166. * The state machine is configurable as a client or as a server based on the selected protocol.Currently server configuration is not supported.<br>
  1167. *
  1168. * \param[in,out] PphHandshake Pointer to structure containing data to perform handshake
  1169. *
  1170. * \retval #OCP_HL_OK Successful Execution
  1171. * \retval #OCP_HL_ERROR Failure Execution
  1172. */
  1173. int32_t DtlsHS_Handshake(sHandshake_d* PphHandshake)
  1174. {
  1175. /// @cond hidden
  1176. #define STATE_SEND 0x11
  1177. #define STATE_RECV 0x22
  1178. #define STATE_EXIT 0x33
  1179. /// @endcond
  1180. uint8_t bLastProcFlight=0;
  1181. uint8_t bSmMode = STATE_RECV;
  1182. uint8_t bIndex;
  1183. uint8_t bFlightTimeout = DEFAULT_TIMEOUT;
  1184. sFlightDetails_d* pSFlightHead=NULL;
  1185. sFlightDetails_d* pRFlightHead=NULL;
  1186. sMsgLyr_d sMessageLayer;
  1187. int32_t i4Status = (int32_t)OCP_HL_ERROR;
  1188. if(eClient == PphHandshake->eMode)
  1189. {
  1190. bSmMode = STATE_SEND;
  1191. }
  1192. else
  1193. {
  1194. bSmMode = STATE_EXIT;
  1195. }
  1196. //Populate structure to be passed to MessageLayer
  1197. sMessageLayer.psConfigRL = PphHandshake->psConfigRL;
  1198. sMessageLayer.wSessionID = PphHandshake->wSessionOID;
  1199. ((sRecordLayer_d*)PphHandshake->psConfigRL->sRL.phRLHdl)->wSessionKeyOID = PphHandshake->wSessionOID;
  1200. sMessageLayer.wMaxPmtu = PphHandshake->wMaxPmtu;
  1201. sMessageLayer.wOIDDevCertificate = PphHandshake->wOIDDevCertificate;
  1202. sMessageLayer.pfGetUnixTIme = PphHandshake->pfGetUnixTIme;
  1203. sMessageLayer.eFlight = eFlight0;
  1204. sMessageLayer.dwRMsgSeqNum = 0xFFFFFFFF;
  1205. sMessageLayer.sTLMsg.prgbStream = (uint8_t*)OCP_MALLOC(TLBUFFER_SIZE);
  1206. if(NULL == sMessageLayer.sTLMsg.prgbStream)
  1207. {
  1208. i4Status = (int32_t)OCP_LIB_MALLOC_FAILURE;
  1209. bSmMode = STATE_EXIT;
  1210. }
  1211. sMessageLayer.sTLMsg.wLen = (uint16_t)TLBUFFER_SIZE;
  1212. for(bIndex = 0; bIndex < (sizeof(sMessageLayer.rgbOptMsgList)/sizeof(sMessageLayer.rgbOptMsgList[0])); bIndex++)
  1213. {
  1214. sMessageLayer.rgbOptMsgList[bIndex] = 0xFF;
  1215. }
  1216. //Start state machine
  1217. do
  1218. {
  1219. switch(bSmMode)
  1220. {
  1221. case STATE_SEND:
  1222. {
  1223. i4Status = SEND_FLIGHT_INITIALIZE(bLastProcFlight, &pSFlightHead, &sMessageLayer);
  1224. if((int32_t)OCP_HL_OK != i4Status)
  1225. {
  1226. if(PphHandshake->eAuthState == eAuthStarted)
  1227. {
  1228. PphHandshake->fFatalError = TRUE;
  1229. SEND_ALERT(sMessageLayer.psConfigRL, i4Status);
  1230. }
  1231. DtlsHS_ClearBuffer(&pRFlightHead);
  1232. DtlsHS_ClearBuffer(&pSFlightHead);
  1233. bSmMode = STATE_EXIT;
  1234. break;
  1235. }
  1236. i4Status = SEND_FLIGHT_PROCESS(&bLastProcFlight, pSFlightHead, &sMessageLayer);
  1237. if(OCP_HL_OK == i4Status)
  1238. {
  1239. if(PphHandshake->eAuthState == eAuthInitialised)
  1240. {
  1241. PphHandshake->eAuthState = eAuthStarted;
  1242. }
  1243. bSmMode = STATE_RECV;
  1244. }
  1245. else
  1246. {
  1247. if(PphHandshake->eAuthState == eAuthStarted)
  1248. {
  1249. PphHandshake->fFatalError = TRUE;
  1250. SEND_ALERT(sMessageLayer.psConfigRL, i4Status);
  1251. }
  1252. bSmMode = STATE_EXIT;
  1253. DtlsHS_ClearBuffer(&pRFlightHead);
  1254. DtlsHS_ClearBuffer(&pSFlightHead);
  1255. }
  1256. break;
  1257. }
  1258. case STATE_RECV:
  1259. {
  1260. i4Status = REC_FLIGHT_INITIALIZE(bLastProcFlight, &pRFlightHead, &sMessageLayer);
  1261. if((int32_t)OCP_HL_OK != i4Status)
  1262. {
  1263. PphHandshake->fFatalError = TRUE;
  1264. DtlsHS_ClearBuffer(&pRFlightHead);
  1265. DtlsHS_ClearBuffer(&pSFlightHead);
  1266. SEND_ALERT(sMessageLayer.psConfigRL, i4Status);
  1267. bSmMode = STATE_EXIT;
  1268. break;
  1269. }
  1270. i4Status = REC_FLIGHT_PROCESS(&bLastProcFlight, &pRFlightHead, &sMessageLayer, bFlightTimeout);
  1271. if ((int32_t)OCP_HL_TIMEOUT == i4Status)
  1272. {
  1273. bFlightTimeout = ((bFlightTimeout*2) == 64)?(uint8_t)MAX_FLIGHT_TIMEOUT: (uint8_t)(bFlightTimeout*2);
  1274. //Check for Maximum Flight timeout value
  1275. if(bFlightTimeout > MAX_FLIGHT_TIMEOUT)
  1276. {
  1277. PphHandshake->fFatalError = FALSE;
  1278. DtlsHS_ClearBuffer(&pRFlightHead);
  1279. DtlsHS_ClearBuffer(&pSFlightHead);
  1280. bSmMode = STATE_EXIT;
  1281. break;
  1282. }
  1283. sMessageLayer.psConfigRL->sRL.psConfigTL->sTL.wTimeout = (uint16_t)(bFlightTimeout * 1000);
  1284. bSmMode = STATE_SEND;
  1285. }
  1286. //Fatal Alert received
  1287. else if((int32_t)OCP_AL_FATAL_ERROR == i4Status)
  1288. {
  1289. PphHandshake->fFatalError = FALSE;
  1290. DtlsHS_ClearBuffer(&pRFlightHead);
  1291. DtlsHS_ClearBuffer(&pSFlightHead);
  1292. bSmMode = STATE_EXIT;
  1293. }
  1294. else if(OCP_HL_OK != i4Status)
  1295. {
  1296. PphHandshake->fFatalError = TRUE;
  1297. SEND_ALERT(sMessageLayer.psConfigRL, i4Status);
  1298. DtlsHS_ClearBuffer(&pRFlightHead);
  1299. DtlsHS_ClearBuffer(&pSFlightHead);
  1300. bSmMode = STATE_EXIT;
  1301. }
  1302. else if(bLastProcFlight != (uint8_t)eFlight6)
  1303. {
  1304. bFlightTimeout = DEFAULT_TIMEOUT;
  1305. //Initial UDP Time out
  1306. sMessageLayer.psConfigRL->sRL.psConfigTL->sTL.wTimeout = 200;
  1307. Dtls_SlideWindow(&sMessageLayer.psConfigRL->sRL, PphHandshake->eAuthState);
  1308. bSmMode = STATE_SEND;
  1309. }
  1310. else
  1311. {
  1312. //state machine is over
  1313. PphHandshake->eAuthState = eAuthCompleted;
  1314. Dtls_SlideWindow(&sMessageLayer.psConfigRL->sRL, PphHandshake->eAuthState);
  1315. PphHandshake->fFatalError = FALSE;
  1316. bSmMode = STATE_EXIT;
  1317. DtlsHS_ClearBuffer(&pRFlightHead);
  1318. DtlsHS_ClearBuffer(&pSFlightHead);
  1319. }
  1320. break;
  1321. }
  1322. default:
  1323. {
  1324. PphHandshake->fFatalError = TRUE;
  1325. bSmMode = STATE_EXIT;
  1326. }
  1327. break;
  1328. }
  1329. }while(STATE_EXIT != bSmMode);
  1330. /// @cond hidden
  1331. #undef STATE_SEND
  1332. #undef STATE_RECV
  1333. #undef STATE_EXIT
  1334. /// @endcond
  1335. if(sMessageLayer.sTLMsg.prgbStream != NULL)
  1336. {
  1337. OCP_FREE(sMessageLayer.sTLMsg.prgbStream);
  1338. }
  1339. return i4Status;
  1340. }
  1341. /**
  1342. * @}
  1343. */
  1344. #endif /*MODULE_ENABLE_DTLS_MUTUAL_AUTH*/