DtlsFlightHandler.c 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896
  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 DtlsFlightHandler.c
  26. *
  27. * \brief This file implements the DTLS Flight handling details.
  28. *
  29. * \addtogroup grMutualAuth
  30. * @{
  31. */
  32. #include "optiga/dtls/DtlsFlightHandler.h"
  33. #ifdef MODULE_ENABLE_DTLS_MUTUAL_AUTH
  34. /// @cond hidden
  35. #define LSBREM8(x) (x & 0x07)
  36. #define LSTBYTE(x) ( ((LSBREM8(x))) ? 1 : 0)
  37. #define DIVBY8(x) (x >> 3)
  38. #define DIVBY4(x) (x >> 2)
  39. #define UPDATEGRANLRTY 32
  40. #define MSG_BITMAP8SET(dwNumBits, pbByte2Set, dwStartBit) (*pbByte2Set |= (uint8_t)((uint8_t)((1 << dwNumBits)-1) << dwStartBit))
  41. #define MSG_BITMAP32SET(dwNumBits, pbWord2Set, dwStartBit) (*pbWord2Set |= ((uint32_t)(0-1)) << dwStartBit)
  42. #define MSG_ID(X) (X & 0xFF)
  43. #define FLIGHT_IDLIMITCHK(LL, X, UL) (((X)>=(LL) && (X)<=(UL)) ? OCP_FL_OK : OCP_FL_ERROR)
  44. ///Maximum message payload
  45. #define MAX_MSG_PAYLOAD 1536 //1558-5(APDU)-12(Msg header)-5(Session ID)
  46. ///Offset for handshake message
  47. #define HANDSHAKE_HEADER_OFFSET 0x00
  48. ///Offset for Message ID in handshake header
  49. #define OFFSET_HS_MSG_TYPE (HANDSHAKE_HEADER_OFFSET + 0)
  50. ///Offset for Message length in handshake header
  51. #define OFFSET_HS_MSG_LENGTH (OFFSET_HS_MSG_TYPE + 1)
  52. ///Offset for Message Sequence number in handshake header
  53. #define OFFSET_HS_MSG_SEQNUM (OFFSET_HS_MSG_LENGTH + 3)
  54. ///Offset for Fragment offset in handshake header
  55. #define OFFSET_HS_MSG_FRAGOFFSET (OFFSET_HS_MSG_SEQNUM + 2)
  56. ///Offset for Fragment length in handshake header
  57. #define OFFSET_HS_MSG_FRAGLEN (OFFSET_HS_MSG_FRAGOFFSET + 3)
  58. ///Offset for message data
  59. #define OFFSET_HS_MSG_DATA (OFFSET_HS_MSG_FRAGLEN + 3) //12
  60. ///Message header length
  61. #define LENGTH_HS_MSG_HEADER (OFFSET_HS_MSG_DATA)
  62. ///Value of Message type in handshake header
  63. #define HS_MESSAGE_TYPE(X) (*((X)+(OFFSET_HS_MSG_TYPE)))
  64. ///Value of Message length in handshake header
  65. #define HS_MESSAGE_LENGTH(X) (Utility_GetUint24((X)+(OFFSET_HS_MSG_LENGTH)))
  66. ///Value of Message sequence number in handshake header
  67. #define HS_MESSAGE_SEQNUM(X) (Utility_GetUint16((X)+(OFFSET_HS_MSG_SEQNUM)))
  68. ///Value of Message fragment offset in handshake header
  69. #define HS_MESSAGE_FRAGOFFSET(X) (Utility_GetUint24((X)+(OFFSET_HS_MSG_FRAGOFFSET)))
  70. ///Value of Message fragment length in handshake header
  71. #define HS_MESSAGE_FRAGLEN(X) (Utility_GetUint24((X)+(OFFSET_HS_MSG_FRAGLEN)))
  72. #define UPDATE_RX_MSGSEQNUM(X,Y) (X=Y)
  73. #define OCP_FLIGHT_TABLE_MAX_SIZE 3
  74. #define UPDATE_MSGSTATE(X,Y) (X=Y)
  75. /// @endcond
  76. ///Maximum number of retransmission of a flight in a session during the handshake protocol
  77. #define OCP_MSGRX_MAX_COUNT 6
  78. /**
  79. * \brief Initializes the pointer to bit map representing message status.<br>
  80. */
  81. _STATIC_H int32_t DtlsHS_MsgCompleteInit(uint32_t PdwMsgLen, uint8_t** PppbMapPtr);
  82. /**
  83. * \brief Sets the number of bits in bit map equal to the number of bytes received in message/ fragment.<br>
  84. */
  85. _STATIC_H int32_t DtlsHS_MsgUptBitMsk(uint32_t PdwOffset, uint32_t PdwFragLen, uint8_t* PprgbMapPtr, uint32_t PdwMsgLen);
  86. /**
  87. * \brief Checks if all the bits in the bitmap are set for the message completion.<br>
  88. */
  89. _STATIC_H int32_t DtlsHS_MsgCompleteCheck(uint8_t* PprgbMapPtr, uint32_t PdwMsgLen);
  90. /**
  91. * \brief Clears all the bits in the bitmap.<br>
  92. */
  93. _STATIC_H int32_t DtlsHS_MsgClearBitMap(uint8_t* PprgbMapPtr, uint32_t PdwMsgLen);
  94. /**
  95. * \brief Searches the look-up table and returns the flight descriptor.<br>
  96. */
  97. _STATIC_H int32_t DtlsHS_GetFlightDescriptor(uint8_t PbFlightID, uint16_t* PpwFlightID);
  98. /**
  99. * \brief Searches the look-up table and returns the flight handler.<br>
  100. */
  101. _STATIC_H int32_t DtlsHS_GetFlightHandler(uint8_t PeFlightID, fFlightHandler * PfHndler);
  102. /**
  103. * \brief Inserts the message node to flight head node.<br>
  104. */
  105. _STATIC_H void DtlsHS_InsertMsgNode(sMsgInfo_d** PppsNodeHead, sMsgInfo_d* PpsNewNode);
  106. /**
  107. * \brief Adds a message node to the end of the message list.<br>
  108. */
  109. _STATIC_H void DtlsHS_AddMsgNode(sMsgInfo_d** PppsNodeHead, sMsgInfo_d* PpsNewNode);
  110. /**
  111. * \brief Initialises message node to respective parameters.<br>
  112. */
  113. _STATIC_H int32_t DtlsHS_SInit_MessageNode(sMsgInfo_d* PpsMsgNode, const sMsgLyr_d* PpsMessageLayer);
  114. /**
  115. * \brief Checks if given optional message is in the list.<br>
  116. */
  117. _STATIC_H int32_t DtlsHS_CheckOptMsg(uint8_t PbMsgID, const uint8_t* PpbOptMsgList);
  118. /**
  119. * \brief Checks if the given message can be sent to the server or not.<br>
  120. */
  121. _STATIC_H int32_t DtlsHS_Flight5_CheckOptMsg(uint8_t PbMsgID, const uint8_t* PpbOptMsgList, const sMsgLyr_d* PpsMessageLayer);
  122. /**
  123. * \brief Forms the change cipher spec message.<br>
  124. */
  125. _STATIC_H int32_t DtlsHS_SInit_ChangeCipherSpec(sMsgInfo_d* PpsMsgNode);
  126. /**
  127. * \brief Returns the sequence number of the last message in a flight.<br>
  128. */
  129. _STATIC_H void DtlsHS_FlightGetLastMsgSeqNum(const sMsgInfo_d *PpsMessageList, uint16_t *PwLastMsgSeqNum);
  130. /**
  131. * \brief Initializes the message node with respective parameters and updates the bit map.<br>
  132. */
  133. _STATIC_H int32_t DtlsHS_RInit_MessageNode(sMsgInfo_d* PpsMsgNode, sMsgLyr_d* PpsMessageLayer);
  134. /**
  135. * \brief Returns the total number of messages in a flight.<br>
  136. */
  137. _STATIC_H void DtlsHS_GetTotalMsgCount(const uint16_t *PwMsgDescList, uint8_t *PbMsgCount);
  138. /**
  139. * \brief Returns the number of optional messages in a flight.<br>
  140. */
  141. _STATIC_H void DtlsHS_GetOptMsgCount(const uint16_t *PwMsgDescList, uint8_t *PbOptMsgCount);
  142. /**
  143. * \brief Returns the message descriptor from the list of messages.<br>
  144. */
  145. _STATIC_H void DtlsHS_GetMsgDesc(const uint16_t *PwMsgDescList, uint8_t PbMsgType, uint16_t *PwMsgDesc);
  146. /**
  147. * \brief Adds a optional message to the list of optional messages.<br>
  148. */
  149. _STATIC_H void DtlsHS_Update_OptList(sMsgLyr_d* PpsMessageLayer, uint8_t PbMsgType, uint8_t PeFlightID);
  150. /**
  151. * \brief Checks if all the messages of a flight are completely received.<br>
  152. */
  153. _STATIC_H int32_t DtlsHS_CheckFlight_Compl(sMsgInfo_d *PpsMessageList, uint8_t PeFlightID, const sMsgLyr_d* PpsMessageLayer);
  154. /**
  155. * \brief Checks if a message is received more than or equal to six times.<br>
  156. */
  157. _STATIC_H int32_t DtlsHS_MessageLimitCheck(sMsgInfo_d *PpsMsgNode);
  158. /**
  159. * \brief Buffers the received message/ fragment.<br>
  160. */
  161. _STATIC_H int32_t DtlsHS_FlightMsgChkAndBuffer(sMsgInfo_d *PpsMessageList, uint8_t bMsgID, const sbBlob_d* PpsMsgIn, sMsgLyr_d* PpsMessageLayer, uint8_t PeFlightID);
  162. /**
  163. * \brief Checks if the message type of the received message/ fragment belongs to the flight.<br>
  164. */
  165. _STATIC_H int32_t DtlsHS_Flight_LimitChk(uint8_t PbFlightID, uint8_t PbMsgID);
  166. /**
  167. * \brief Sends the message to be processed to Security Chip.<br>
  168. */
  169. _STATIC_H int32_t DtlsHS_SendFlightToOptiga(sMsgInfo_d *PpsMessageList, const sMsgLyr_d* PpsMessageLayer);
  170. /**
  171. * \brief Resets the flight 2 node.<br>
  172. */
  173. _STATIC_H void DtlsHS_ResetFlight2MsgNode(const sFlightStats_d* PpsThisFlight);
  174. /**
  175. * \brief Checks if message sequence number of received message/ fragment of flight4 is correct.<br>
  176. */
  177. _STATIC_H int32_t DtlsHS_Flight4CheckMsgSeqNum(const sMsgLyr_d* PpsMessageLayer, uint8_t PbRxMsgID, uint16_t PwRxMsgSeqNum);
  178. /**
  179. * \brief Checks if message sequence number and length of received message/ fragment of flight4 is the same as the buffered one.<br>
  180. */
  181. _STATIC_H int32_t DtlsHS_Flight4CheckBuffMsg(uint8_t PbMsgType, uint16_t PwMsgSeqNum, uint32_t PdwMsgLen, sMsgInfo_d *PpsMsgList);
  182. /**
  183. * \brief Checks for flight4 completion.<br>
  184. */
  185. _STATIC_H int32_t DtlsHS_Flight4ReRxCompl(sMsgInfo_d *PpsMsgList);
  186. /**
  187. * \brief Clears the messages of a flight and resets the bit map.<br>
  188. */
  189. _STATIC_H void DtlsHS_Flight4ClearMsgsInList(sMsgInfo_d *PpsMsgList);
  190. /**
  191. * \brief Updates bit map and sets the message state.<br>
  192. */
  193. _STATIC_H int32_t DtlsHS_Flight4UpdateMsgStat(uint8_t PbMsgID, sMsgInfo_d *PpsMsgList, const sMsgLyr_d* PpsMessageLayer);
  194. /**
  195. * \brief Checks whether flight six is received.<br>
  196. */
  197. _STATIC_H int32_t DtlsHS_CheckFlight6_Compl(sMsgInfo_d *PpsMessageList);
  198. /**
  199. * \brief Checks Message Sequence number of flight six messages.<br>
  200. */
  201. _STATIC_H int32_t DtlsHS_Flight6CheckMsgSeqNum(const sMsgLyr_d* PpsMessageLayer, uint8_t PbMsgID, uint16_t PwMsgSeqNum);
  202. /**
  203. * \brief Frees a node and all the pointers in it .<br>
  204. */
  205. _STATIC_H void DtlsHS_FreeMsgNode(sMsgInfo_d *PpsMsgNode);
  206. /**
  207. * Initializes the pointer to bit map representing message status.<br>
  208. *
  209. * \param[in] PdwMsgLen Total length of the message received.
  210. * \param[out] PppbMapPtr Pointer container to map pointer.
  211. *
  212. * \retval #OCP_FL_OK Successful execution
  213. * \retval #OCP_FL_MSG_ERROR Failure in execution
  214. \if ENABLE_NULL_CHECKS
  215. * \retval #OCP_FL_NULL_PARAM Null parameter
  216. \endif
  217. */
  218. _STATIC_H int32_t DtlsHS_MsgCompleteInit(uint32_t PdwMsgLen, uint8_t** PppbMapPtr)
  219. {
  220. int32_t i4Status = (int32_t)OCP_FL_MSG_ERROR;
  221. uint32_t dwMapSize;
  222. uint8_t* pbMapPtr = NULL;
  223. uint8_t bLbyteMsk;
  224. do
  225. {
  226. #ifdef ENABLE_NULL_CHECKS
  227. if(NULL == PppbMapPtr)
  228. {
  229. i4Status = (int32_t)OCP_FL_NULL_PARAM;
  230. break;
  231. }
  232. #endif
  233. dwMapSize = DIVBY8(PdwMsgLen) + LSTBYTE(PdwMsgLen);
  234. if(*PppbMapPtr == NULL)
  235. {
  236. pbMapPtr = (uint8_t*)OCP_CALLOC(dwMapSize, sizeof(uint8_t));
  237. if(pbMapPtr == NULL)
  238. {
  239. i4Status = (int32_t)OCP_FL_MALLOC_FAILURE;
  240. break;
  241. }
  242. *PppbMapPtr = pbMapPtr;
  243. // Set remaining bits in last byte
  244. bLbyteMsk = (uint8_t)((uint8_t)( (1 << (8 - (LSBREM8(PdwMsgLen) ))) - (LSTBYTE(PdwMsgLen)) ) << (LSBREM8(PdwMsgLen)));
  245. *(pbMapPtr+(dwMapSize-1)) |= bLbyteMsk;
  246. }
  247. i4Status = (int32_t)OCP_FL_OK;
  248. }while(0);
  249. return i4Status;
  250. }
  251. /**
  252. * Sets the number of bits in bit map equal to the number of bytes received in message/ fragment.<br>
  253. *
  254. * \param[in] PdwOffset Offset of the message/ fragment received.
  255. * \param[in] PdwFragLen Length of the message/ fragment received.
  256. * \param[in,out] PprgbMapPtr Pointer to bit map.
  257. * \param[in] PdwMsgLen Length of the message/ fragment received.
  258. *
  259. * \retval #OCP_FL_OK Successful execution
  260. * \retval #OCP_FL_MSG_ERROR Failure in execution
  261. \if ENABLE_NULL_CHECKS
  262. * \retval #OCP_FL_NULL_PARAM Null parameter
  263. \endif
  264. */
  265. _STATIC_H int32_t DtlsHS_MsgUptBitMsk(uint32_t PdwOffset, uint32_t PdwFragLen, uint8_t* PprgbMapPtr, uint32_t PdwMsgLen)
  266. {
  267. int32_t i4Status = (int32_t)OCP_FL_MSG_ERROR;
  268. uint32_t dwStartByte, dwStartBit, dwNumofBit2Set, dwLoopCount, dwCount;
  269. uint8_t* prgbByte2Set = NULL;
  270. do
  271. {
  272. #ifdef ENABLE_NULL_CHECKS
  273. if(NULL == PprgbMapPtr)
  274. {
  275. i4Status = (int32_t)OCP_FL_NULL_PARAM;
  276. break;
  277. }
  278. #endif
  279. dwStartByte = DIVBY8(PdwOffset);
  280. dwStartBit = LSBREM8(PdwOffset);
  281. dwNumofBit2Set = PdwFragLen;
  282. prgbByte2Set = PprgbMapPtr + dwStartByte;
  283. if(dwStartBit != 0)
  284. {
  285. if(dwNumofBit2Set < (8-dwStartBit))
  286. {
  287. MSG_BITMAP8SET(dwNumofBit2Set, prgbByte2Set, dwStartBit);
  288. dwNumofBit2Set -= dwNumofBit2Set;
  289. }
  290. else
  291. {
  292. MSG_BITMAP8SET((8-dwStartBit), prgbByte2Set, dwStartBit);
  293. dwNumofBit2Set -= (8-dwStartBit);
  294. }
  295. prgbByte2Set++;
  296. }
  297. if(prgbByte2Set < ( PprgbMapPtr+(DIVBY8(PdwMsgLen)+LSTBYTE(PdwMsgLen))) )
  298. {
  299. dwLoopCount = dwNumofBit2Set/UPDATEGRANLRTY;
  300. for(dwCount=0; dwCount < dwLoopCount; dwCount++)
  301. {
  302. //lint --e{826} suppress "Implicit type casting to 32 bit pointer for 32 bit granularity check"
  303. MSG_BITMAP32SET(UPDATEGRANLRTY, (uint32_t*)prgbByte2Set, 0);
  304. prgbByte2Set += (UPDATEGRANLRTY >> 3);
  305. dwNumofBit2Set -= UPDATEGRANLRTY;
  306. }
  307. if(dwNumofBit2Set >= 0x08)
  308. {
  309. dwLoopCount = DIVBY8(dwNumofBit2Set);
  310. for(dwCount=0; dwCount < dwLoopCount; dwCount++)
  311. {
  312. MSG_BITMAP8SET(8, prgbByte2Set, 0);
  313. prgbByte2Set++;
  314. dwNumofBit2Set -= 8;
  315. }
  316. }
  317. if(dwNumofBit2Set)
  318. {
  319. MSG_BITMAP8SET(dwNumofBit2Set, prgbByte2Set, 0);
  320. dwNumofBit2Set -= dwNumofBit2Set;
  321. }
  322. }
  323. i4Status = (int32_t)OCP_FL_OK;
  324. }while(0);
  325. return i4Status;
  326. }
  327. /**
  328. * Checks if all the bits in the bitmap is set for the message completion.<br>
  329. *
  330. * \param[in] PprgbMapPtr Pointer to bit map.
  331. * \param[in] PdwMsgLen Total Length of the message received.
  332. *
  333. * \retval #OCP_FL_OK Successful execution
  334. * \retval #OCP_FL_MSG_ERROR Failure in execution
  335. \if ENABLE_NULL_CHECKS
  336. * \retval #OCP_FL_NULL_PARAM Null parameter
  337. \endif
  338. */
  339. _STATIC_H int32_t DtlsHS_MsgCompleteCheck(uint8_t* PprgbMapPtr, uint32_t PdwMsgLen)
  340. {
  341. uint32_t dwLoopCount;
  342. int32_t i4Status = (int32_t)OCP_FL_OK;
  343. uint32_t* pdwMapptr = NULL;
  344. uint8_t* pbMapptr = NULL;
  345. dwLoopCount = DIVBY4(DIVBY8(PdwMsgLen));
  346. /// @cond hidden
  347. #define DWVAL 0xFFFFFFFF
  348. /// @endcond
  349. do
  350. {
  351. #ifdef ENABLE_NULL_CHECKS
  352. if(NULL == PprgbMapPtr)
  353. {
  354. i4Status = (int32_t)OCP_FL_NULL_PARAM;
  355. break;
  356. }
  357. #endif
  358. //lint --e{826} suppress "Implicit type casting to 32 bit pointer for 32 bit granularity check"
  359. pdwMapptr = (uint32_t*)PprgbMapPtr;
  360. while(dwLoopCount--)
  361. {
  362. if(*pdwMapptr++ < DWVAL )
  363. {
  364. i4Status = (int32_t)OCP_FL_MSG_ERROR;
  365. break;
  366. }
  367. PdwMsgLen -= UPDATEGRANLRTY;
  368. }
  369. if(i4Status != (int32_t)OCP_FL_MSG_ERROR)
  370. {
  371. dwLoopCount = DIVBY8(PdwMsgLen)+LSTBYTE(PdwMsgLen);
  372. pbMapptr = (uint8_t*)pdwMapptr;
  373. while(dwLoopCount--)
  374. {
  375. if(*pbMapptr++ < ((1<<8)-1))
  376. {
  377. i4Status = (int32_t)OCP_FL_MSG_ERROR;
  378. break;
  379. }
  380. }
  381. }
  382. }while(0);
  383. /// @cond hidden
  384. #undef DWVAL
  385. /// @endcond
  386. return i4Status;
  387. }
  388. /**
  389. * Clears all the bits in the bitmap.<br>
  390. *
  391. * \param[in,out] PprgbMapPtr Pointer to bit map.
  392. * \param[in] PdwMsgLen Total length of the message received.
  393. *
  394. * \retval #OCP_FL_OK Successful execution
  395. * \retval #OCP_FL_MSG_ERROR Failure in execution
  396. \if ENABLE_NULL_CHECKS
  397. * \retval #OCP_FL_NULL_PARAM Null parameter
  398. \endif
  399. */
  400. _STATIC_H int32_t DtlsHS_MsgClearBitMap(uint8_t* PprgbMapPtr, uint32_t PdwMsgLen)
  401. {
  402. uint32_t dwLoopCount, dwMapSize;
  403. int32_t i4Status = (int32_t)OCP_FL_MSG_ERROR;
  404. uint32_t* pdwMapptr = NULL;
  405. uint8_t* pbMapptr = NULL;
  406. uint8_t bLbyteMsk;
  407. do
  408. {
  409. #ifdef ENABLE_NULL_CHECKS
  410. if(NULL == PprgbMapPtr)
  411. {
  412. i4Status = (int32_t)OCP_FL_NULL_PARAM;
  413. break;
  414. }
  415. #endif
  416. //lint --e{826} suppress "Implicit type casting to 32 bit pointer for 32 bit granularity check"
  417. pdwMapptr = (uint32_t*)PprgbMapPtr;
  418. dwMapSize = DIVBY8(PdwMsgLen) + LSTBYTE(PdwMsgLen);
  419. dwLoopCount = DIVBY4(DIVBY8(PdwMsgLen));
  420. while(dwLoopCount--)
  421. {
  422. *pdwMapptr++ = 0x00;
  423. PdwMsgLen -= UPDATEGRANLRTY;
  424. }
  425. dwLoopCount = DIVBY8(PdwMsgLen)+LSTBYTE(PdwMsgLen);
  426. pbMapptr = (uint8_t*)pdwMapptr;
  427. while(dwLoopCount--)
  428. {
  429. *pbMapptr++ = 0x00;
  430. }
  431. bLbyteMsk = (uint8_t)((uint8_t)((1 << (8 - (LSBREM8(PdwMsgLen) ))) - (LSTBYTE(PdwMsgLen)) ) << (LSBREM8(PdwMsgLen)));
  432. *(PprgbMapPtr+(dwMapSize-1)) |= bLbyteMsk;
  433. i4Status = (int32_t)OCP_FL_OK;
  434. }while(0);
  435. return i4Status;
  436. }
  437. /**
  438. * Searches the look-up table and returns the flight descriptor.<br>
  439. *
  440. * \param[in] PbFlightID Flight number
  441. * \param[in,out] PpwFlightID Container to flight descriptor
  442. *
  443. * \retval #OCP_HL_OK Successful Execution
  444. * \retval #OCP_HL_ERROR Failure Execution
  445. */
  446. _STATIC_H int32_t DtlsHS_GetFlightDescriptor(uint8_t PbFlightID, uint16_t* PpwFlightID)
  447. {
  448. int32_t i4Status = (int32_t)OCP_FL_ERROR;
  449. uint8_t bIndex = 0;
  450. do
  451. {
  452. for(bIndex = 0; bIndex < OCP_FLIGHT_TABLE_MAX_SIZE; bIndex++)
  453. {
  454. if(((rgsSFlightInfo[bIndex].wFlightDesc) & 0xFF) == (PbFlightID))
  455. {
  456. *PpwFlightID = rgsSFlightInfo[bIndex].wFlightDesc;
  457. i4Status = (int32_t)OCP_FL_OK;
  458. break;
  459. }
  460. }
  461. if((int32_t)OCP_FL_ERROR == i4Status)
  462. {
  463. for(bIndex = 0; bIndex < OCP_FLIGHT_TABLE_MAX_SIZE; bIndex++)
  464. {
  465. if((rgsRFlightInfo[bIndex].wFlightDesc & 0xFF) == (PbFlightID))
  466. {
  467. *PpwFlightID = rgsRFlightInfo[bIndex].wFlightDesc;
  468. i4Status = (int32_t)OCP_FL_OK;
  469. break;
  470. }
  471. }
  472. }
  473. }while(0);
  474. return i4Status;
  475. }
  476. /**
  477. * Searches the look-up table and returns the flight handler.<br>
  478. *
  479. * \param[in] PeFlightID Flight number
  480. * \param[in,out] PfHndler Container to flight handler
  481. *
  482. * \retval #OCP_HL_OK Successful Execution
  483. * \retval #OCP_HL_ERROR Failure Execution
  484. */
  485. _STATIC_H int32_t DtlsHS_GetFlightHandler(uint8_t PeFlightID, fFlightHandler * PfHndler)
  486. {
  487. int32_t i4Status = (int32_t)OCP_FL_ERROR;
  488. uint8_t bIndex;
  489. do
  490. {
  491. for(bIndex = 0; bIndex < OCP_FLIGHT_TABLE_MAX_SIZE; bIndex++)
  492. {
  493. if(((rgsSFlightInfo[bIndex].wFlightDesc) & 0xFF) == (uint8_t)PeFlightID)
  494. {
  495. *PfHndler = (rgsSFlightInfo[bIndex].pFlightHndlr);
  496. i4Status = (int32_t)OCP_FL_OK;
  497. break;
  498. }
  499. }
  500. if((int32_t)OCP_FL_ERROR == i4Status)
  501. {
  502. for(bIndex = 0; bIndex < OCP_FLIGHT_TABLE_MAX_SIZE; bIndex++)
  503. {
  504. if(((rgsRFlightInfo[bIndex].wFlightDesc) & 0xFF) == (uint8_t)PeFlightID)
  505. {
  506. *PfHndler = (rgsRFlightInfo[bIndex].pFlightHndlr);
  507. i4Status = (int32_t)OCP_FL_OK;
  508. break;
  509. }
  510. }
  511. }
  512. }while(0);
  513. return i4Status;
  514. }
  515. /**
  516. * Inserts the message node to flight head node .<br>
  517. *
  518. * \param[in,out] PppsNodeHead Pointer container holding head node pointer.
  519. * \param[in] PpsNewNode Pointer to the message node to be inserted.
  520. *
  521. */
  522. _STATIC_H void DtlsHS_InsertMsgNode(sMsgInfo_d** PppsNodeHead, sMsgInfo_d* PpsNewNode)
  523. {
  524. sMsgInfo_d* pTravA = *PppsNodeHead;
  525. sMsgInfo_d* pTravB = NULL;
  526. do
  527. {
  528. if(NULL == pTravA)
  529. {
  530. *PppsNodeHead = PpsNewNode;
  531. }
  532. else
  533. {
  534. do
  535. {
  536. pTravB = pTravA->psNext;
  537. if (PpsNewNode->bMsgType < pTravA->bMsgType)
  538. {
  539. PpsNewNode->psNext = pTravA;
  540. *PppsNodeHead = PpsNewNode;
  541. break;
  542. }
  543. else if (NULL != pTravB)
  544. {
  545. if((PpsNewNode->bMsgType > pTravA->bMsgType) && (PpsNewNode->bMsgType < pTravB->bMsgType))
  546. {
  547. pTravA->psNext = PpsNewNode;
  548. PpsNewNode->psNext = pTravB;
  549. break;
  550. }
  551. }
  552. else
  553. {
  554. if(PpsNewNode->bMsgType > pTravA->bMsgType)
  555. {
  556. pTravA->psNext = PpsNewNode;
  557. PpsNewNode->psNext = NULL;
  558. }
  559. break;
  560. }
  561. pTravA = pTravA->psNext;
  562. }while(NULL != pTravA);
  563. }
  564. }while(0);
  565. }
  566. /**
  567. * Adds a message node to the end of the message list.<br>
  568. *
  569. * \param[in,out] PppsNodeHead Pointer container holding head node pointer.
  570. * \param[in] PpsNewNode Pointer to the message node to be added.
  571. *
  572. */
  573. _STATIC_H void DtlsHS_AddMsgNode(sMsgInfo_d** PppsNodeHead, sMsgInfo_d* PpsNewNode)
  574. {
  575. sMsgInfo_d* psTrav = *PppsNodeHead;
  576. do
  577. {
  578. if(NULL == psTrav)
  579. {
  580. *PppsNodeHead = PpsNewNode;
  581. }
  582. else
  583. {
  584. while(NULL != psTrav->psNext)
  585. {
  586. psTrav = psTrav->psNext;
  587. }
  588. psTrav->psNext = PpsNewNode;
  589. }
  590. }while(0);
  591. }
  592. /**
  593. * Initialises message node to respective parameters.<br>
  594. * - Calls message layer to get the message.<br>
  595. * - Checks the header of the message received from message layer.<br>
  596. * - Updates the bit map.<br>
  597. *
  598. * \param[in,out] PpsMsgNode Pointer to the message node.
  599. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  600. *
  601. * \retval #OCP_FL_OK Successful Execution
  602. * \retval #OCP_FL_ERROR Failure Execution
  603. */
  604. _STATIC_H int32_t DtlsHS_SInit_MessageNode(sMsgInfo_d* PpsMsgNode, const sMsgLyr_d* PpsMessageLayer)
  605. {
  606. int32_t i4Status = (int32_t)OCP_FL_OK;
  607. sbBlob_d sbBlobMessage;
  608. sbBlob_d sMessage;
  609. sMessageLayer_d sMessageLayer;
  610. uint32_t dwTotalLength;
  611. sMessageLayer.wOIDDevCertificate = PpsMessageLayer->wOIDDevCertificate;
  612. sMessageLayer.pfGetUnixTIme = PpsMessageLayer->pfGetUnixTIme;
  613. sMessageLayer.psConfigRL = PpsMessageLayer->psConfigRL;
  614. sMessageLayer.wMaxPmtu = PpsMessageLayer->wMaxPmtu;
  615. sMessageLayer.wSessionID = PpsMessageLayer->wSessionID;
  616. do
  617. {
  618. i4Status = MsgLayer_FormMessage((eMsgType_d)PpsMsgNode->bMsgType, &sMessageLayer, &sbBlobMessage);
  619. if(OCP_ML_OK != i4Status)
  620. {
  621. break;
  622. }
  623. PpsMsgNode->psMsgHolder = sbBlobMessage.prgbStream;
  624. //Assign the pointer from Handshake message header to local blob
  625. sMessage.prgbStream = sbBlobMessage.prgbStream + (OVERHEAD_LEN - MSG_HEADER_LEN);
  626. sMessage.wLen = sbBlobMessage.wLen - (OVERHEAD_LEN - MSG_HEADER_LEN);
  627. dwTotalLength = HS_MESSAGE_LENGTH(sMessage.prgbStream);
  628. if(sMessage.wLen != (dwTotalLength + MSG_HEADER_LEN))
  629. {
  630. break;
  631. }
  632. PpsMsgNode->psMsgMapPtr = NULL;
  633. PpsMsgNode->dwMsgLength = dwTotalLength;
  634. PpsMsgNode->wMsgSequence = HS_MESSAGE_SEQNUM(sMessage.prgbStream);
  635. PpsMsgNode->eMsgState = eComplete;
  636. i4Status = (int32_t)OCP_FL_OK;
  637. }while(0);
  638. return i4Status;
  639. }
  640. /**
  641. * Checks if given optional message is in the list.<br>
  642. *
  643. * \param[in] PbMsgID Message type.
  644. * \param[in] PpbOptMsgList List of received optional messages.
  645. *
  646. * \retval #OCP_FL_OK Requested message found in list
  647. * \retval #OCP_FL_ERROR Requested message not found in list
  648. */
  649. _STATIC_H int32_t DtlsHS_CheckOptMsg(uint8_t PbMsgID, const uint8_t* PpbOptMsgList)
  650. {
  651. int32_t i4Status = (int32_t)OCP_FL_ERROR;
  652. do
  653. {
  654. if(NULL == PpbOptMsgList)
  655. {
  656. break;
  657. }
  658. while(0xFF != *(PpbOptMsgList))
  659. {
  660. if(PbMsgID == *PpbOptMsgList)
  661. {
  662. i4Status = (int32_t)OCP_FL_OK;
  663. break;
  664. }
  665. PpbOptMsgList++;
  666. }
  667. }while(0);
  668. return i4Status;
  669. }
  670. /**
  671. * Checks if the given message can be sent to the server or not.<br>
  672. *
  673. * \param[in] PbMsgID Message type.
  674. * \param[in] PpbOptMsgList List of received optional messages.
  675. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  676. *
  677. * \retval #OCP_FL_OK Requested message can be sent
  678. * \retval #OCP_FL_ERROR Requested message cannot be sent
  679. */
  680. _STATIC_H int32_t DtlsHS_Flight5_CheckOptMsg(uint8_t PbMsgID, const uint8_t* PpbOptMsgList, const sMsgLyr_d* PpsMessageLayer)
  681. {
  682. int32_t i4Status = (int32_t)OCP_FL_OK;
  683. do
  684. {
  685. if(((uint8_t)eClientCertificate == PbMsgID) || ((uint8_t)eCertificateVerify == PbMsgID))
  686. {
  687. if(OCP_FL_OK != DtlsHS_CheckOptMsg((uint8_t)eCertificateRequest, PpbOptMsgList))
  688. {
  689. i4Status = (int32_t)OCP_FL_ERROR;
  690. break;
  691. }
  692. }
  693. if(((uint8_t)eCertificateVerify == PbMsgID) && (0x0000 == PpsMessageLayer->wOIDDevCertificate))
  694. {
  695. i4Status = (int32_t)OCP_FL_ERROR;
  696. break;
  697. }
  698. }while(0);
  699. return i4Status;
  700. }
  701. /**
  702. * Initialises message node to respective parameters.<br>
  703. * - Forms the change cipher spec message.<br>
  704. *
  705. * \param[in,out] PpsMsgNode Pointer to the message node.
  706. *
  707. * \retval #OCP_FL_OK Requested message can be sent
  708. * \retval #OCP_FL_ERROR Requested message cannot be sent
  709. */
  710. _STATIC_H int32_t DtlsHS_SInit_ChangeCipherSpec(sMsgInfo_d* PpsMsgNode)
  711. {
  712. int32_t i4Status = (int32_t)OCP_FL_OK;
  713. /// @cond hidden
  714. #define CHANGE_CIPHERSPEC_MSGSIZE 1
  715. /// @endcond
  716. do
  717. {
  718. PpsMsgNode->psMsgHolder = (uint8_t*)OCP_MALLOC(CHANGE_CIPHERSPEC_MSGSIZE);
  719. if(NULL == PpsMsgNode->psMsgHolder)
  720. {
  721. i4Status = (int32_t)OCP_FL_MALLOC_FAILURE;
  722. break;
  723. }
  724. *PpsMsgNode->psMsgHolder = (int32_t)0x01;
  725. PpsMsgNode->dwMsgLength = CHANGE_CIPHERSPEC_MSGSIZE;
  726. PpsMsgNode->psMsgMapPtr = NULL;
  727. }while(0);
  728. /// @cond hidden
  729. #undef CHANGE_CIPHERSPEC_MSGSIZE
  730. /// @endcond
  731. return i4Status;
  732. }
  733. /**
  734. * Returns the sequence number of the last message in a flight.<br>
  735. *
  736. * \param[in] PpsMessageList Pointer to list of messages.
  737. * \param[out] PwLastMsgSeqNum Pointer to last sequence number.
  738. *
  739. * \retval #OCP_FL_OK Successful Execution
  740. * \retval #OCP_FL_ERROR Failure Execution
  741. */
  742. _STATIC_H void DtlsHS_FlightGetLastMsgSeqNum(const sMsgInfo_d *PpsMessageList, uint16_t *PwLastMsgSeqNum)
  743. {
  744. do
  745. {
  746. if(NULL == PpsMessageList)
  747. {
  748. break;
  749. }
  750. while(NULL != PpsMessageList->psNext)
  751. {
  752. PpsMessageList = PpsMessageList->psNext;
  753. }
  754. *PwLastMsgSeqNum = PpsMessageList->wMsgSequence;
  755. }while(0);
  756. }
  757. /**
  758. * Initializes the message node with respective parameters and updates the bit map.<br>
  759. *
  760. * \param[in,out] PpsMsgNode Pointer to new message node.
  761. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  762. *
  763. * \retval #OCP_FL_OK Successful Execution
  764. * \retval #OCP_FL_ERROR Failure Execution
  765. * \retval #OCP_FL_MSG_INCOMPLETE Message is not completely received
  766. */
  767. _STATIC_H int32_t DtlsHS_RInit_MessageNode(sMsgInfo_d* PpsMsgNode, sMsgLyr_d* PpsMessageLayer)
  768. {
  769. int32_t i4Status = OCP_FL_OK;
  770. uint32_t dwOffset;
  771. uint32_t dwFragLen;
  772. uint32_t dwTotalLen;
  773. do
  774. {
  775. PpsMsgNode->bMsgType = HS_MESSAGE_TYPE(PpsMessageLayer->sMsg.prgbStream);
  776. dwOffset = HS_MESSAGE_FRAGOFFSET(PpsMessageLayer->sMsg.prgbStream);
  777. dwFragLen = HS_MESSAGE_FRAGLEN(PpsMessageLayer->sMsg.prgbStream);
  778. dwTotalLen = HS_MESSAGE_LENGTH(PpsMessageLayer->sMsg.prgbStream);
  779. //Length of the message payload should not exceed 1536 bytes
  780. if(MAX_MSG_PAYLOAD < dwTotalLen)
  781. {
  782. i4Status = (int32_t)OCP_HL_BUFFER_OVERFLOW;
  783. break;
  784. }
  785. memcpy((PpsMsgNode->psMsgHolder + OVERHEAD_LEN + dwOffset), PpsMessageLayer->sMsg.prgbStream + LENGTH_HS_MSG_HEADER, dwFragLen);
  786. PpsMsgNode->wMsgSequence = HS_MESSAGE_SEQNUM(PpsMessageLayer->sMsg.prgbStream);
  787. PpsMsgNode->dwMsgLength = dwTotalLen;
  788. PpsMsgNode->psMsgMapPtr = NULL;
  789. PpsMsgNode->bMsgCount = 0;
  790. //lint --e{534} suppress "Return value is not required to be checked"
  791. DtlsHS_PrepareMsgHeader((PpsMsgNode->psMsgHolder + (OVERHEAD_LEN - MSG_HEADER_LEN)), PpsMsgNode);
  792. //Initialise Bit Map for message status
  793. if(OCP_FL_OK != DtlsHS_MsgCompleteInit(PpsMsgNode->dwMsgLength, &PpsMsgNode->psMsgMapPtr))
  794. {
  795. i4Status = (int32_t)OCP_FL_ERROR;
  796. break;
  797. }
  798. //lint --e{534} suppress "Return value is not required to be checked"
  799. DtlsHS_MsgUptBitMsk(dwOffset, dwFragLen, PpsMsgNode->psMsgMapPtr, PpsMsgNode->dwMsgLength);
  800. // Check Message Completeness
  801. if(OCP_FL_OK != DtlsHS_MsgCompleteCheck(PpsMsgNode->psMsgMapPtr, PpsMsgNode->dwMsgLength))
  802. {
  803. i4Status = (int32_t)OCP_FL_MSG_INCOMPLETE;
  804. break;
  805. }
  806. DtlsHS_Update_OptList(PpsMessageLayer, PpsMsgNode->bMsgType, (uint8_t)PpsMessageLayer->eFlight);
  807. // If message is completely received, change state to Received
  808. UPDATE_MSGSTATE(PpsMsgNode->eMsgState, eComplete);
  809. PpsMsgNode->bMsgCount++;
  810. }while(0);
  811. return i4Status;
  812. }
  813. /**
  814. * Returns the total number of messages in a flight.<br>
  815. *
  816. * \param[in] PwMsgDescList Pointer to list of messages.
  817. * \param[out] PbMsgCount Pointer to message counter.
  818. *
  819. */
  820. _STATIC_H void DtlsHS_GetTotalMsgCount(const uint16_t *PwMsgDescList, uint8_t *PbMsgCount)
  821. {
  822. *PbMsgCount = 0;
  823. do
  824. {
  825. (*PbMsgCount)++;
  826. }while(0xFFFF != *(++PwMsgDescList));
  827. }
  828. /**
  829. * Returns the number of optional messages in a flight.<br>
  830. *
  831. * \param[in] PwMsgDescList Pointer to list of messages.
  832. * \param[out] PbOptMsgCount Pointer to message counter.
  833. *
  834. */
  835. _STATIC_H void DtlsHS_GetOptMsgCount(const uint16_t *PwMsgDescList, uint8_t *PbOptMsgCount)
  836. {
  837. do
  838. {
  839. if(IsOptional(*PwMsgDescList))
  840. {
  841. (*PbOptMsgCount)++;
  842. }
  843. }while(0xFFFF != *(++PwMsgDescList));
  844. }
  845. /**
  846. * Returns the message descriptor from the list of messages.<br>
  847. *
  848. * \param[in] PwMsgDescList Pointer to list of messages.
  849. * \param[in] PbMsgType Message ID of which descriptor is required.
  850. * \param[out] PwMsgDesc Pointer to message descriptor.
  851. *
  852. */
  853. _STATIC_H void DtlsHS_GetMsgDesc(const uint16_t *PwMsgDescList, uint8_t PbMsgType, uint16_t *PwMsgDesc)
  854. {
  855. do
  856. {
  857. if(MSG_ID(*PwMsgDescList) == PbMsgType)
  858. {
  859. *PwMsgDesc = *PwMsgDescList;
  860. }
  861. PwMsgDescList++;
  862. }while(0xFFFF != *PwMsgDescList);
  863. }
  864. /**
  865. * Adds a optional message to the list of optional messages.<br>
  866. *
  867. * \param[in,out] PpsMessageLayer Pointer to the structure containing message configuration information.
  868. * \param[in] PbMsgType Optional Message ID.
  869. * \param[in] PeFlightID Flight ID.
  870. *
  871. */
  872. _STATIC_H void DtlsHS_Update_OptList(sMsgLyr_d* PpsMessageLayer, uint8_t PbMsgType, uint8_t PeFlightID)
  873. {
  874. int32_t i4Status = (int32_t)OCP_FL_ERROR;
  875. uint8_t bIndex = 0;
  876. uint16_t *pwFlightMsgs;
  877. do
  878. {
  879. DtlsHS_GetFlightMsgInfo(PeFlightID, &pwFlightMsgs);
  880. //Check whether given message is optional
  881. while(0xFFFF != *pwFlightMsgs)
  882. {
  883. if(MSG_ID(*pwFlightMsgs) == PbMsgType)
  884. {
  885. if(IsOptional(*pwFlightMsgs))
  886. {
  887. i4Status = (int32_t)OCP_FL_OK;
  888. break;
  889. }
  890. }
  891. pwFlightMsgs++;
  892. }
  893. //If the Message is optional, then store the message ID in Optional Message list
  894. if((int32_t)OCP_FL_OK == i4Status)
  895. {
  896. do
  897. {
  898. if((PpsMessageLayer->rgbOptMsgList[bIndex] == PbMsgType) || (PpsMessageLayer->rgbOptMsgList[bIndex] == 0xFF))
  899. {
  900. PpsMessageLayer->rgbOptMsgList[bIndex] = PbMsgType;
  901. break;
  902. }
  903. }while(++bIndex < ((sizeof(PpsMessageLayer->rgbOptMsgList)/sizeof(PpsMessageLayer->rgbOptMsgList[0]))));
  904. }
  905. }while(0);
  906. }
  907. /**
  908. * Checks if all the messages of a flight are completely received.<br>
  909. *
  910. * \param[in] PpsMessageList Pointer to list of messages of a flight.
  911. * \param[in] PeFlightID Flight ID.
  912. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  913. *
  914. * \retval #OCP_FL_OK All messages are completely received
  915. * \retval #OCP_FL_ERROR Atleast one message is incomplete
  916. */
  917. _STATIC_H int32_t DtlsHS_CheckFlight_Compl(sMsgInfo_d *PpsMessageList, uint8_t PeFlightID, const sMsgLyr_d* PpsMessageLayer)
  918. {
  919. int32_t i4Status = (int32_t)OCP_FL_ERROR;
  920. sMsgInfo_d* psMsgListTrav = PpsMessageList;
  921. uint32_t dwLastMsgSeq, dwPrevFlightMsgSeq;
  922. uint16_t *pwFlightMsgs;
  923. uint8_t bOptMsgCount = 0;
  924. uint8_t bTotalMsgInFlight = 0;
  925. uint8_t bTotalOptMsgInFlight = 0;
  926. uint8_t bTotalMsgRxDInFlight = 0;
  927. uint16_t wMsgDesc;
  928. uint8_t bSeqNumFlag = TRUE, bLoopCount, bIndex;
  929. dwLastMsgSeq = PpsMessageLayer->dwRMsgSeqNum;
  930. dwPrevFlightMsgSeq = PpsMessageLayer->dwRMsgSeqNum;
  931. do
  932. {
  933. #ifdef ENABLE_NULL_CHECKS
  934. if(NULL == psMsgListTrav)
  935. {
  936. break;
  937. }
  938. #endif
  939. DtlsHS_GetFlightMsgInfo(PeFlightID, &pwFlightMsgs);
  940. DtlsHS_GetTotalMsgCount(pwFlightMsgs, &bTotalMsgInFlight);
  941. DtlsHS_GetOptMsgCount(pwFlightMsgs, &bTotalOptMsgInFlight);
  942. if(dwPrevFlightMsgSeq == 0xFFFFFFFF)
  943. {
  944. bLoopCount = 2;
  945. }
  946. else
  947. {
  948. bLoopCount = 1;
  949. }
  950. for(bIndex = 0; bIndex < bLoopCount; bIndex++)
  951. {
  952. psMsgListTrav = PpsMessageList;
  953. do
  954. {
  955. if(eComplete == psMsgListTrav->eMsgState)
  956. {
  957. DtlsHS_GetMsgDesc(pwFlightMsgs, psMsgListTrav->bMsgType, &wMsgDesc);
  958. if(IsOptional(wMsgDesc))
  959. {
  960. bOptMsgCount++;
  961. }
  962. if(psMsgListTrav->wMsgSequence != dwLastMsgSeq+1)
  963. {
  964. bSeqNumFlag = FALSE;
  965. }
  966. dwLastMsgSeq = psMsgListTrav->wMsgSequence;
  967. bTotalMsgRxDInFlight++;
  968. }
  969. psMsgListTrav = psMsgListTrav->psNext;
  970. }while(NULL != psMsgListTrav);
  971. if((bOptMsgCount != 0) && ((bTotalMsgInFlight == bTotalMsgRxDInFlight) && (TRUE == bSeqNumFlag)))
  972. {
  973. i4Status = (int32_t)OCP_FL_OK;
  974. break;
  975. }
  976. else if((bOptMsgCount == 0) && ((bTotalMsgRxDInFlight == bTotalMsgInFlight-bTotalOptMsgInFlight) && (TRUE == bSeqNumFlag)))
  977. {
  978. i4Status = (int32_t)OCP_FL_OK;
  979. break;
  980. }
  981. else
  982. {
  983. i4Status = (int32_t)OCP_FL_MSG_INCOMPLETE;
  984. }
  985. dwLastMsgSeq = dwPrevFlightMsgSeq+1;
  986. bOptMsgCount = 0;
  987. bTotalMsgRxDInFlight = 0;
  988. bSeqNumFlag = TRUE;
  989. }
  990. }while(0);
  991. return i4Status;
  992. }
  993. /**
  994. * Checks if a message is received more than or equal to six times.<br>
  995. *
  996. * \param[in] PpsMsgNode Pointer to message node.
  997. *
  998. * \retval #OCP_FL_OK Message is received less than six times
  999. * \retval #OCP_FL_ERROR Message is received six times
  1000. */
  1001. _STATIC_H int32_t DtlsHS_MessageLimitCheck(sMsgInfo_d *PpsMsgNode)
  1002. {
  1003. int32_t i4Status = OCP_FL_OK;
  1004. do
  1005. {
  1006. if(++(PpsMsgNode->bMsgCount) > OCP_MSGRX_MAX_COUNT)
  1007. {
  1008. PpsMsgNode->bMsgCount = OCP_MSGRX_MAX_COUNT;
  1009. i4Status = (int32_t)OCP_FL_ERROR;
  1010. }
  1011. }while(0);
  1012. return i4Status;
  1013. }
  1014. /**
  1015. * Buffers the received message/ fragment.<br>
  1016. *
  1017. * \param[in,out] PpsMessageList Pointer to list of messages in a flight.
  1018. * \param[in] PbMsgID Received message type.
  1019. * \param[in] PpsMsgIn Pointer to the received message buffer.
  1020. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  1021. * \param[in] PeFlightID Flight number.
  1022. *
  1023. * \retval #OCP_FL_OK Successful execution
  1024. * \retval #OCP_FL_ERROR Failure in execution
  1025. * \retval #OCP_FL_MSG_NODE_NOT_AVAIL Node corresponding to message is not in the list of messages
  1026. * \retval #OCP_FL_MALLOC_FAILURE Memory allocation failure
  1027. * \retval #OCP_FL_INVALID_MSG_LENGTH Message Length of new fragment does not match the buffered message length
  1028. * \retval #OCP_FL_INVALID_MSG_SEQ Sequence number of new fragment does not match the buffered message/ fragment sequence length
  1029. * \retval #OCP_FL_MSG_INCOMPLETE Message is not completely received
  1030. * \retval #OCP_FL_MSG_MAXCOUNT Message is received six times
  1031. */
  1032. _STATIC_H int32_t DtlsHS_FlightMsgChkAndBuffer(sMsgInfo_d *PpsMessageList, uint8_t PbMsgID, const sbBlob_d* PpsMsgIn, sMsgLyr_d* PpsMessageLayer, uint8_t PeFlightID)
  1033. {
  1034. int32_t i4Status = (int32_t)OCP_FL_MSG_NODE_NOT_AVAIL;
  1035. uint32_t dwOffset = 0;
  1036. uint32_t dwFragLength = 0;
  1037. sMsgInfo_d *psMsgListTrav = PpsMessageList;
  1038. do
  1039. {
  1040. if(NULL == psMsgListTrav)
  1041. {
  1042. break;
  1043. }
  1044. do
  1045. {
  1046. if(psMsgListTrav->bMsgType == PbMsgID)
  1047. {
  1048. if((ePartial == psMsgListTrav->eMsgState))
  1049. {
  1050. dwOffset = HS_MESSAGE_FRAGOFFSET(PpsMsgIn->prgbStream);
  1051. dwFragLength = HS_MESSAGE_FRAGLEN(PpsMsgIn->prgbStream);
  1052. if(NULL == psMsgListTrav->psMsgHolder)
  1053. {
  1054. psMsgListTrav->dwMsgLength = HS_MESSAGE_LENGTH(PpsMsgIn->prgbStream);
  1055. psMsgListTrav->bMsgType = *PpsMsgIn->prgbStream;
  1056. psMsgListTrav->psMsgHolder = (uint8_t*)OCP_MALLOC( psMsgListTrav->dwMsgLength + OVERHEAD_LEN);
  1057. if(NULL == psMsgListTrav->psMsgHolder)
  1058. {
  1059. i4Status = (int32_t)OCP_FL_MALLOC_FAILURE;
  1060. break;
  1061. }
  1062. //lint --e{534} suppress "Return value is not required to be checked"
  1063. DtlsHS_PrepareMsgHeader((psMsgListTrav->psMsgHolder + (OVERHEAD_LEN - MSG_HEADER_LEN)), psMsgListTrav);
  1064. }
  1065. if(NULL == psMsgListTrav->psMsgMapPtr)
  1066. {
  1067. //Initialise Bit Map for message status
  1068. if(OCP_FL_OK != DtlsHS_MsgCompleteInit(psMsgListTrav->dwMsgLength, &psMsgListTrav->psMsgMapPtr))
  1069. {
  1070. i4Status = (int32_t)OCP_FL_ERROR;
  1071. break;
  1072. }
  1073. }
  1074. if(psMsgListTrav->dwMsgLength != HS_MESSAGE_LENGTH(PpsMsgIn->prgbStream))
  1075. {
  1076. i4Status = (int32_t)OCP_FL_INVALID_MSG_LENGTH;
  1077. break;
  1078. }
  1079. if(psMsgListTrav->wMsgSequence != HS_MESSAGE_SEQNUM(PpsMsgIn->prgbStream))
  1080. {
  1081. i4Status = (int32_t)OCP_FL_INVALID_MSG_SEQ;
  1082. break;
  1083. }
  1084. //Buffer the message
  1085. memcpy(psMsgListTrav->psMsgHolder+OVERHEAD_LEN+dwOffset, PpsMsgIn->prgbStream+LENGTH_HS_MSG_HEADER, dwFragLength);
  1086. //Update message status
  1087. //lint --e{534} suppress "Return value is not required to be checked"
  1088. DtlsHS_MsgUptBitMsk(dwOffset, dwFragLength, psMsgListTrav->psMsgMapPtr, psMsgListTrav->dwMsgLength);
  1089. }
  1090. // Check Message Completeness
  1091. if(OCP_FL_OK != DtlsHS_MsgCompleteCheck(psMsgListTrav->psMsgMapPtr, psMsgListTrav->dwMsgLength))
  1092. {
  1093. i4Status = (int32_t)OCP_FL_MSG_INCOMPLETE;
  1094. break;
  1095. }
  1096. // If message is completely received, change state to Complete
  1097. UPDATE_MSGSTATE(psMsgListTrav->eMsgState, eComplete);
  1098. DtlsHS_Update_OptList(PpsMessageLayer, psMsgListTrav->bMsgType, PeFlightID);
  1099. if(OCP_FL_OK != DtlsHS_MessageLimitCheck(psMsgListTrav))
  1100. {
  1101. i4Status = (int32_t)OCP_FL_MSG_MAXCOUNT;
  1102. break;
  1103. }
  1104. i4Status = OCP_FL_OK;
  1105. }
  1106. psMsgListTrav = psMsgListTrav->psNext;
  1107. }while(NULL != psMsgListTrav);
  1108. }while(0);
  1109. return i4Status;
  1110. }
  1111. /**
  1112. * Checks if the message type of the received message/ fragment belongs to the flight.<br>
  1113. *
  1114. * \param[in] PbFlightID Flight ID.
  1115. * \param[in] PbMsgID Received Message type
  1116. *
  1117. * \retval #OCP_FL_OK Successful execution
  1118. * \retval #OCP_FL_MSG_NOT_IN_FLIGHT Received message is not in flight
  1119. */
  1120. _STATIC_H int32_t DtlsHS_Flight_LimitChk(uint8_t PbFlightID, uint8_t PbMsgID)
  1121. {
  1122. int32_t i4Status = (int32_t)OCP_FL_MSG_NOT_IN_FLIGHT;
  1123. uint16_t* pwMsgList = NULL;
  1124. do
  1125. {
  1126. DtlsHS_GetFlightMsgInfo(PbFlightID, &pwMsgList);
  1127. while(0xFFFF != *pwMsgList)
  1128. {
  1129. if(PbMsgID == FLIGHTID(*pwMsgList))
  1130. {
  1131. i4Status = OCP_FL_OK;
  1132. break;
  1133. }
  1134. pwMsgList++;
  1135. }
  1136. }while(0);
  1137. return i4Status;
  1138. }
  1139. /**
  1140. * Sends the message to be processed to Security Chip.<br>
  1141. *
  1142. * \param[in] PpsMessageList Pointer to the structure containing message linked list.
  1143. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  1144. *
  1145. * \retval #OCP_ML_OK Successful execution
  1146. * \retval #OCP_ML_ERROR Failure in execution
  1147. * \retval #CMD_DEV_ERROR Command lib error
  1148. * \retval #OCP_FL_SEND_MSG_TO_OPTIGA_ERROR Message processing failed on Security Chip
  1149. *
  1150. */
  1151. _STATIC_H int32_t DtlsHS_SendFlightToOptiga(sMsgInfo_d *PpsMessageList, const sMsgLyr_d* PpsMessageLayer)
  1152. {
  1153. int32_t i4Status = (int32_t)OCP_ML_ERROR;
  1154. sMessageLayer_d sMessageLayer;
  1155. sbBlob_d sMessage;
  1156. do
  1157. {
  1158. sMessageLayer.wOIDDevCertificate = PpsMessageLayer->wOIDDevCertificate;
  1159. sMessageLayer.pfGetUnixTIme = PpsMessageLayer->pfGetUnixTIme;
  1160. sMessageLayer.psConfigRL = PpsMessageLayer->psConfigRL;
  1161. sMessageLayer.wMaxPmtu = PpsMessageLayer->wMaxPmtu;
  1162. sMessageLayer.wSessionID = PpsMessageLayer->wSessionID;
  1163. while(NULL != PpsMessageList)
  1164. {
  1165. if(NULL != PpsMessageList->psMsgHolder)
  1166. {
  1167. sMessage.prgbStream = PpsMessageList->psMsgHolder;
  1168. sMessage.wLen = (uint16_t)PpsMessageList->dwMsgLength + OVERHEAD_LEN;
  1169. //Process the obtained message by sending the message to Security Chip
  1170. i4Status = MsgLayer_ProcessMessage((eMsgType_d)PpsMessageList->bMsgType, &sMessageLayer, &sMessage);
  1171. if((i4Status & (int32_t)DEV_ERROR_CODE_MASK) == (int32_t)CMD_DEV_ERROR)
  1172. {
  1173. break;
  1174. }
  1175. else if((int32_t)OCP_ML_OK != i4Status)
  1176. {
  1177. i4Status = (int32_t)OCP_FL_SEND_MSG_TO_OPTIGA_ERROR;
  1178. break;
  1179. }
  1180. }
  1181. UPDATE_MSGSTATE(PpsMessageList->eMsgState, eProcessed);
  1182. PpsMessageList = PpsMessageList->psNext;
  1183. }
  1184. }while(0);
  1185. return i4Status;
  1186. }
  1187. /**
  1188. * Resets the flight 2 node .<br>
  1189. *
  1190. * \param[in,out] PpsThisFlight Pointer to the structure containing flight status.
  1191. *
  1192. */
  1193. _STATIC_H void DtlsHS_ResetFlight2MsgNode(const sFlightStats_d* PpsThisFlight)
  1194. {
  1195. do
  1196. {
  1197. if(NULL != PpsThisFlight->psMessageList->psMsgHolder)
  1198. {
  1199. OCP_FREE(PpsThisFlight->psMessageList->psMsgHolder);
  1200. PpsThisFlight->psMessageList->psMsgHolder = NULL;
  1201. }
  1202. if(NULL != PpsThisFlight->psMessageList->psMsgMapPtr)
  1203. {
  1204. OCP_FREE(PpsThisFlight->psMessageList->psMsgMapPtr);
  1205. PpsThisFlight->psMessageList->psMsgMapPtr = NULL;
  1206. }
  1207. PpsThisFlight->psMessageList->eMsgState = ePartial;
  1208. }while(0);
  1209. }
  1210. /**
  1211. * Frees a node and all the pointers in it .<br>
  1212. *
  1213. * \param[in] PpsMsgNode Pointer to the message node.
  1214. *
  1215. */
  1216. _STATIC_H void DtlsHS_FreeMsgNode(sMsgInfo_d *PpsMsgNode)
  1217. {
  1218. if(NULL != PpsMsgNode->psMsgHolder)
  1219. {
  1220. OCP_FREE(PpsMsgNode->psMsgHolder);
  1221. PpsMsgNode->psMsgHolder = NULL;
  1222. }
  1223. if(NULL != PpsMsgNode->psMsgMapPtr)
  1224. {
  1225. OCP_FREE(PpsMsgNode->psMsgMapPtr);
  1226. PpsMsgNode->psMsgMapPtr = NULL;
  1227. }
  1228. OCP_FREE(PpsMsgNode);
  1229. }
  1230. /**
  1231. * Checks if message sequence number and length of received message/ fragment of flight4 is the same as the buffered one.<br>
  1232. *
  1233. * \param[in] PbRxMsgType Message type of received message/ fragment.
  1234. * \param[in] PwRxMsgSeqNum Message sequence number of received message/ fragment.
  1235. * \param[in] PdwRxMsgLen Message sequence number of received message/ fragment.
  1236. * \param[in] PpsMsgList Pointer to list of messages in the flight.
  1237. *
  1238. * \retval #OCP_FL_OK Sequence number and message length of buffered message is same as re-received message/ fragment
  1239. * \retval #OCP_FL_ERROR Received message/ fragment can be ignored
  1240. */
  1241. _STATIC_H int32_t DtlsHS_Flight4CheckBuffMsg(uint8_t PbRxMsgType, uint16_t PwRxMsgSeqNum, uint32_t PdwRxMsgLen, sMsgInfo_d *PpsMsgList)
  1242. {
  1243. int32_t i4Status = (int32_t)OCP_FL_ERROR;
  1244. sMsgInfo_d *psMsgListTrav = PpsMsgList;
  1245. do
  1246. {
  1247. if(psMsgListTrav->bMsgType == PbRxMsgType)
  1248. {
  1249. if((psMsgListTrav->wMsgSequence == PwRxMsgSeqNum) && (psMsgListTrav->dwMsgLength == PdwRxMsgLen))
  1250. {
  1251. i4Status = (int32_t)OCP_FL_OK;
  1252. break;
  1253. }
  1254. }
  1255. psMsgListTrav = psMsgListTrav->psNext;
  1256. }while(NULL != psMsgListTrav);
  1257. return i4Status;
  1258. }
  1259. /**
  1260. * Checks for flight4 completion.<br>
  1261. *
  1262. * \param[in] PpsMsgList Pointer to message list of the flight.
  1263. *
  1264. * \retval #OCP_FL_OK Flight 4 is received completely
  1265. * \retval #OCP_FL_ERROR Flight 4 is not received completely
  1266. */
  1267. _STATIC_H int32_t DtlsHS_Flight4ReRxCompl(sMsgInfo_d *PpsMsgList)
  1268. {
  1269. int32_t i4Status = (int32_t)OCP_FL_OK;
  1270. sMsgInfo_d *psMsgListTrav = PpsMsgList;
  1271. do
  1272. {
  1273. if(eProcessed != psMsgListTrav->eMsgState)
  1274. {
  1275. i4Status = (int32_t)OCP_FL_ERROR;
  1276. break;
  1277. }
  1278. psMsgListTrav = psMsgListTrav->psNext;
  1279. }while(NULL != psMsgListTrav);
  1280. return i4Status;
  1281. }
  1282. /**
  1283. * Updates bit map and sets the message state.<br>
  1284. *
  1285. * \param[in] PbMsgID Message type of received message/ fragment.
  1286. * \param[in] PpsMsgList Pointer to message list of the flight.
  1287. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  1288. *
  1289. * \retval #OCP_FL_OK Successful execution
  1290. * \retval #OCP_FL_ERROR Received message/ fragment can be ignored
  1291. */
  1292. _STATIC_H int32_t DtlsHS_Flight4UpdateMsgStat(uint8_t PbMsgID, sMsgInfo_d *PpsMsgList, const sMsgLyr_d* PpsMessageLayer)
  1293. {
  1294. int32_t i4Status = (int32_t)OCP_FL_ERROR;
  1295. sMsgInfo_d *psMsgListTrav = PpsMsgList;
  1296. uint32_t dwOffset, dwFragLen;
  1297. dwOffset = HS_MESSAGE_FRAGOFFSET(PpsMessageLayer->sMsg.prgbStream);
  1298. dwFragLen = HS_MESSAGE_FRAGLEN(PpsMessageLayer->sMsg.prgbStream);
  1299. do
  1300. {
  1301. do
  1302. {
  1303. if(NULL != psMsgListTrav)
  1304. {
  1305. if(psMsgListTrav->bMsgType == PbMsgID)
  1306. {
  1307. i4Status = (int32_t)OCP_FL_OK;
  1308. break;
  1309. }
  1310. psMsgListTrav = psMsgListTrav->psNext;
  1311. }
  1312. }while(NULL != psMsgListTrav);
  1313. //lint --e{613} suppress "If 'psMsgListTrav' parameter is null then based on return code it doesnt enter the below path"
  1314. if(OCP_FL_OK == i4Status)
  1315. {
  1316. if(OCP_FL_OK != DtlsHS_MsgUptBitMsk(dwOffset, dwFragLen, psMsgListTrav->psMsgMapPtr, psMsgListTrav->dwMsgLength))
  1317. {
  1318. i4Status = (int32_t)OCP_FL_ERROR;
  1319. break;
  1320. }
  1321. // Check Message Completeness
  1322. if(OCP_FL_OK != DtlsHS_MsgCompleteCheck(psMsgListTrav->psMsgMapPtr, psMsgListTrav->dwMsgLength))
  1323. {
  1324. i4Status = (int32_t)OCP_FL_MSG_INCOMPLETE;
  1325. break;
  1326. }
  1327. UPDATE_MSGSTATE(psMsgListTrav->eMsgState, eProcessed);
  1328. if(OCP_FL_OK != DtlsHS_MessageLimitCheck(psMsgListTrav))
  1329. {
  1330. i4Status = (int32_t)OCP_FL_MSG_MAXCOUNT;
  1331. break;
  1332. }
  1333. }
  1334. }while(0);
  1335. return i4Status;
  1336. }
  1337. /**
  1338. * Clears the messages of a flight and resets the bit map.<br>
  1339. *
  1340. * \param[in, out] PpsMsgList Pointer to list of messages.
  1341. *
  1342. */
  1343. _STATIC_H void DtlsHS_Flight4ClearMsgsInList(sMsgInfo_d *PpsMsgList)
  1344. {
  1345. sMsgInfo_d *psMsgListTrav = PpsMsgList;
  1346. do
  1347. {
  1348. if(NULL!= psMsgListTrav->psMsgHolder)
  1349. {
  1350. OCP_FREE(psMsgListTrav->psMsgHolder);
  1351. psMsgListTrav->psMsgHolder = NULL;
  1352. }
  1353. if(NULL != psMsgListTrav->psMsgMapPtr)
  1354. {
  1355. if(OCP_FL_OK == DtlsHS_MsgClearBitMap(psMsgListTrav->psMsgMapPtr, psMsgListTrav->dwMsgLength))
  1356. {
  1357. UPDATE_MSGSTATE(psMsgListTrav->eMsgState, ePartial);
  1358. }
  1359. else
  1360. {
  1361. break;
  1362. }
  1363. }
  1364. psMsgListTrav = psMsgListTrav->psNext;
  1365. }while(NULL != psMsgListTrav);
  1366. }
  1367. /**
  1368. * Checks Message Sequence number of flight six messages.<br>
  1369. *
  1370. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  1371. * \param[in] PbMsgID Message Type of the received message/ fragment.
  1372. * \param[in] PwMsgSeqNum Message sequence number of received message.
  1373. *
  1374. * \retval #OCP_FL_OK Sequence number is correct
  1375. * \retval #OCP_FL_ERROR Sequence number is incorrect
  1376. */
  1377. _STATIC_H int32_t DtlsHS_Flight6CheckMsgSeqNum(const sMsgLyr_d* PpsMessageLayer, uint8_t PbMsgID, uint16_t PwMsgSeqNum)
  1378. {
  1379. int32_t i4Status = (int32_t)OCP_FL_OK;
  1380. do
  1381. {
  1382. if((PbMsgID == (uint8_t)eServerFinished) && (PwMsgSeqNum != (uint16_t)PpsMessageLayer->dwRMsgSeqNum+1))
  1383. {
  1384. i4Status = (int32_t)OCP_FL_ERROR;
  1385. }
  1386. else if(PbMsgID == (uint8_t)eChangeCipherSpec)
  1387. {
  1388. i4Status = (int32_t)OCP_FL_OK;
  1389. }
  1390. }while(0);
  1391. return i4Status;
  1392. }
  1393. /**
  1394. * Searches the look-up table and returns the message descriptors of a flight.<br>
  1395. *
  1396. * \param[in] PeFlightID Flight number.
  1397. * \param[in,out] PpwMessageList Pointer container to message descriptor.
  1398. *
  1399. */
  1400. void DtlsHS_GetFlightMsgInfo(uint8_t PeFlightID, uint16_t** PpwMessageList)
  1401. {
  1402. int32_t i4Status = (int32_t)OCP_FL_ERROR;
  1403. uint8_t bIndex;
  1404. do
  1405. {
  1406. for(bIndex = 0; bIndex < OCP_FLIGHT_TABLE_MAX_SIZE; bIndex++)
  1407. {
  1408. if(((rgsSFlightInfo[bIndex].wFlightDesc) & 0xFF) == (uint8_t)PeFlightID)
  1409. {
  1410. *PpwMessageList = (uint16_t*)&(rgsSFlightInfo[bIndex].wMsgTypes[0]);
  1411. i4Status = (int32_t)OCP_FL_OK;
  1412. break;
  1413. }
  1414. }
  1415. if((int32_t)OCP_FL_ERROR == i4Status)
  1416. {
  1417. for(bIndex = 0; bIndex < OCP_FLIGHT_TABLE_MAX_SIZE; bIndex++)
  1418. {
  1419. if(((rgsRFlightInfo[bIndex].wFlightDesc) & 0xFF) == (uint8_t)PeFlightID)
  1420. {
  1421. *PpwMessageList = (uint16_t*)&(rgsRFlightInfo[bIndex].wMsgTypes[0]);
  1422. break;
  1423. }
  1424. }
  1425. }
  1426. }while(0);
  1427. }
  1428. /**
  1429. * Initialises flight node to default parameters.<br>
  1430. *
  1431. * \param[in,out] PpsFlightNode Pointer to flight node
  1432. * \param[in] PbLastProcFlight Last processed flight ID
  1433. *
  1434. * \retval #OCP_HL_OK Successful Execution
  1435. * \retval #OCP_HL_ERROR Failure Execution
  1436. */
  1437. int32_t DtlsHS_FlightNodeInit(sFlightDetails_d* PpsFlightNode, uint8_t PbLastProcFlight)
  1438. {
  1439. int32_t i4Status = (int32_t)OCP_FL_OK;
  1440. do
  1441. {
  1442. PpsFlightNode->psNext = NULL;
  1443. if(OCP_FL_OK != DtlsHS_GetFlightDescriptor(PbLastProcFlight+1, &PpsFlightNode->wFlightDecp))
  1444. {
  1445. i4Status = (int32_t)OCP_FL_ERROR;
  1446. break;
  1447. }
  1448. if(OCP_FL_OK != DtlsHS_GetFlightHandler(FLIGHTID(PpsFlightNode->wFlightDecp), &PpsFlightNode->pFlightHndlr))
  1449. {
  1450. i4Status = (int32_t)OCP_FL_ERROR;
  1451. break;
  1452. }
  1453. PpsFlightNode->sFlightStats.psMessageList = NULL;
  1454. UPDATE_FSTATE(PpsFlightNode->sFlightStats.bFlightState, (uint8_t)efInit);
  1455. }while(0);
  1456. return i4Status;
  1457. }
  1458. /**
  1459. * Gets the Flight type for the corresponding message type.<br>
  1460. *
  1461. * \param[in] PbMsgType Message type
  1462. * \param[in,out] PpFlightID Pointer to the flight number
  1463. *
  1464. * \retval #OCP_FL_OK Successful Execution
  1465. * \retval #OCP_FL_ERROR Failure Execution
  1466. */
  1467. int32_t DtlsHS_GetFlightID(uint8_t PbMsgType, uint8_t* PpFlightID)
  1468. {
  1469. int32_t i4Status = (int32_t)OCP_FL_ERROR;
  1470. uint8_t bFlightIndex,bMsgIndex ;
  1471. do
  1472. {
  1473. for(bFlightIndex = 0; bFlightIndex < OCP_FLIGHT_TABLE_MAX_SIZE; bFlightIndex++)
  1474. {
  1475. bMsgIndex = 0;
  1476. while(MSG_ID(rgsRFlightInfo[bFlightIndex].wMsgTypes[bMsgIndex]) != 0xFF)
  1477. {
  1478. if(MSG_ID(rgsRFlightInfo[bFlightIndex].wMsgTypes[bMsgIndex]) == PbMsgType)
  1479. {
  1480. *PpFlightID = FLIGHTID(rgsRFlightInfo[bFlightIndex].wFlightDesc);
  1481. i4Status = (int32_t)OCP_FL_OK;
  1482. break;
  1483. }
  1484. bMsgIndex++;
  1485. }
  1486. if((int32_t)OCP_FL_OK == i4Status)
  1487. {
  1488. break;
  1489. }
  1490. }
  1491. }while(0);
  1492. return i4Status;
  1493. }
  1494. /**
  1495. * Checks whether the received message belongs to the expected flight.<br>
  1496. *
  1497. * \param[in] PbLastProcFlight Last processed flight number
  1498. * \param[in,out] PpsBlobMessage Pointer to the Blob containing record
  1499. * \param[in] PpsMessageLayer Pointer to the structure containing message layer information
  1500. *
  1501. * \retval #OCP_FL_OK Successful Execution
  1502. * \retval #OCP_FL_ERROR Failure Execution
  1503. */
  1504. int32_t DtlsHS_MsgCheck(uint8_t PbLastProcFlight, const sbBlob_d* PpsBlobMessage, const sMsgLyr_d* PpsMessageLayer)
  1505. {
  1506. uint8_t bFlightA = 0;
  1507. uint8_t bFlightB = 0;
  1508. int32_t i4Status = (int32_t)OCP_FL_OK;
  1509. uint8_t bMsgType;
  1510. uint32_t dwTotalMsgLen = 0;
  1511. uint8_t bFirstMsgFlightID = 0;
  1512. uint8_t bMsgFlightID;
  1513. sbBlob_d sMsgIn;
  1514. do
  1515. {
  1516. //Change cipher spec record is received
  1517. if(PpsMessageLayer->psConfigRL->sRL.bRecvCCSRecord == CCS_RECORD_RECV)
  1518. {
  1519. if((PpsMessageLayer->eFlight != eFlight5) && (PpsMessageLayer->eFlight != eFlight6))
  1520. {
  1521. //CCS message doesn't belongs to flight 6
  1522. i4Status = (int32_t)OCP_FL_MSG_NOT_IN_FLIGHT;
  1523. PpsMessageLayer->psConfigRL->sRL.bRecvCCSRecord = CCS_RECORD_NOTRECV;
  1524. }
  1525. break;
  1526. }
  1527. //Minimum length check
  1528. if(LENGTH_HS_MSG_HEADER > PpsBlobMessage->wLen)
  1529. {
  1530. i4Status = (int32_t)OCP_HL_INVALID_LENGTH;
  1531. break;
  1532. }
  1533. sMsgIn.prgbStream = PpsBlobMessage->prgbStream;
  1534. sMsgIn.wLen = PpsBlobMessage->wLen;
  1535. //Check for default flight state
  1536. if((uint8_t)PpsMessageLayer->eFlight == PbLastProcFlight)
  1537. {
  1538. if((PbLastProcFlight == 1) || (PbLastProcFlight == 3))
  1539. {
  1540. bFlightA = (uint8_t)eFlight2;
  1541. bFlightB = (uint8_t)eFlight4;
  1542. }
  1543. else if(PbLastProcFlight == 5)
  1544. {
  1545. bFlightA = (uint8_t)eFlight4;
  1546. bFlightB = (uint8_t)eFlight6;
  1547. }
  1548. bMsgType = *(sMsgIn.prgbStream);
  1549. //Get the flight number to which the message belongs
  1550. if(OCP_FL_OK != DtlsHS_GetFlightID(bMsgType, &bFirstMsgFlightID))
  1551. {
  1552. i4Status = (int32_t)OCP_FL_MSG_NOT_LISTED;
  1553. break;
  1554. }
  1555. //Check if the flight number belongs to the expected the flight
  1556. if((bFlightA != bFirstMsgFlightID) && (bFlightB != bFirstMsgFlightID))
  1557. {
  1558. i4Status = (int32_t)OCP_FL_MSG_NOT_IN_FLIGHT;
  1559. break;
  1560. }
  1561. //Header validation
  1562. i4Status = DtlsHS_ProcHeader(sMsgIn);
  1563. if(OCP_HL_OK == i4Status)
  1564. {
  1565. i4Status = (int32_t)OCP_FL_OK;
  1566. }
  1567. else
  1568. {
  1569. break;
  1570. }
  1571. dwTotalMsgLen = Utility_GetUint24(sMsgIn.prgbStream + OFFSET_HS_MSG_FRAGLEN);
  1572. sMsgIn.prgbStream += dwTotalMsgLen + LENGTH_HS_MSG_HEADER;
  1573. sMsgIn.wLen -= (uint16_t)dwTotalMsgLen + LENGTH_HS_MSG_HEADER;
  1574. }
  1575. //For multiple messages in a record
  1576. while(sMsgIn.wLen != 0)
  1577. {
  1578. bMsgType = *(sMsgIn.prgbStream);
  1579. //Minimum length check
  1580. if(LENGTH_HS_MSG_HEADER > sMsgIn.wLen)
  1581. {
  1582. i4Status = (int32_t)OCP_HL_INVALID_LENGTH;
  1583. break;
  1584. }
  1585. //Get the flight number to which the message belongs
  1586. if(OCP_FL_OK != DtlsHS_GetFlightID(bMsgType, &bMsgFlightID))
  1587. {
  1588. i4Status = (int32_t)OCP_FL_MSG_NOT_LISTED;
  1589. break;
  1590. }
  1591. if((uint8_t)PpsMessageLayer->eFlight == PbLastProcFlight)
  1592. {
  1593. if(bFirstMsgFlightID != bMsgFlightID)
  1594. {
  1595. i4Status = (int32_t)OCP_FL_MSG_NOT_IN_FLIGHT;
  1596. break;
  1597. }
  1598. }
  1599. else
  1600. {
  1601. if(PpsMessageLayer->eFlight != (eFlight_d)bMsgFlightID)
  1602. {
  1603. i4Status = (int32_t)OCP_FL_MSG_NOT_IN_FLIGHT;
  1604. break;
  1605. }
  1606. }
  1607. i4Status = DtlsHS_ProcHeader(sMsgIn);
  1608. if(OCP_HL_OK == i4Status)
  1609. {
  1610. i4Status = (int32_t)OCP_FL_OK;
  1611. }
  1612. else
  1613. {
  1614. break;
  1615. }
  1616. dwTotalMsgLen = Utility_GetUint24(sMsgIn.prgbStream + OFFSET_HS_MSG_FRAGLEN);
  1617. sMsgIn.wLen -= (uint16_t)dwTotalMsgLen + LENGTH_HS_MSG_HEADER;
  1618. if(sMsgIn.wLen != 0)
  1619. {
  1620. sMsgIn.prgbStream += dwTotalMsgLen + LENGTH_HS_MSG_HEADER;
  1621. }
  1622. }
  1623. }while(0);
  1624. return i4Status;
  1625. }
  1626. /**
  1627. * Flight one handler to process flight 1 messages .<br>
  1628. *
  1629. * \param[in] PbLastProcFlight Last processed flight.
  1630. * \param[in,out] PpsThisFlight Pointer to structure containing flight1 status.
  1631. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  1632. *
  1633. * \retval #OCP_FL_OK Successful execution
  1634. * \retval #OCP_FL_MSG_ERROR Failure in execution
  1635. * \retval #OCP_FL_MALLOC_FAILURE Malloc failure
  1636. * \retval #OCP_FL_GET_MSG_FROM_OPTIGA_ERROR Error from Security Chip
  1637. * \retval #OCP_FL_FLIGHTSEND_ERROR Error while sending flight to Security Chip
  1638. */
  1639. //lint --e{818} suppress "This is ignored as same flight handler pointer is used for both send and receive"
  1640. int32_t DtlsHS_Flight1Handler(uint8_t PbLastProcFlight, sFlightStats_d* PpsThisFlight, sMsgLyr_d* PpsMessageLayer)
  1641. {
  1642. int32_t i4Status = (int32_t)OCP_FL_OK;
  1643. sMsgInfo_d* psMsgListTrav = NULL;
  1644. uint16_t *pwMsgIDList = NULL;
  1645. do
  1646. {
  1647. if(((uint8_t)efInit == PpsThisFlight->bFlightState) && ((uint8_t)eFlight0 == PbLastProcFlight))
  1648. {
  1649. //Generate List of Messages of this flight
  1650. //create list of messages of this flight and call Command lib to get messages from Security Chip
  1651. DtlsHS_GetFlightMsgInfo((uint8_t)eFlight1, &pwMsgIDList);
  1652. while(0xFF != MSG_ID(*pwMsgIDList))
  1653. {
  1654. psMsgListTrav = (sMsgInfo_d*)OCP_MALLOC(sizeof(sMsgInfo_d));
  1655. if(NULL == psMsgListTrav)
  1656. {
  1657. i4Status = (int32_t)OCP_FL_MALLOC_FAILURE;
  1658. break;
  1659. }
  1660. psMsgListTrav->bMsgType = MSG_ID(*pwMsgIDList);
  1661. psMsgListTrav->eMsgState = ePartial;
  1662. psMsgListTrav->psNext = NULL;
  1663. psMsgListTrav->psMsgMapPtr = NULL;
  1664. psMsgListTrav->psMsgHolder = NULL;
  1665. DtlsHS_InsertMsgNode(&PpsThisFlight->psMessageList, psMsgListTrav);
  1666. pwMsgIDList++;
  1667. }
  1668. if((int32_t)OCP_FL_OK == i4Status)
  1669. {
  1670. UPDATE_FSTATE(PpsThisFlight->bFlightState, (uint8_t)efReady);
  1671. }
  1672. }
  1673. else if(((uint8_t)eFlight1 == PbLastProcFlight+1) || ((uint8_t)eFlight1 == PbLastProcFlight))
  1674. {
  1675. if((uint8_t)efReady == PpsThisFlight->bFlightState)
  1676. {
  1677. //Get Message from Security Chip and Send to Server
  1678. psMsgListTrav = PpsThisFlight->psMessageList;
  1679. while(NULL != psMsgListTrav)
  1680. {
  1681. if(ePartial == psMsgListTrav->eMsgState)
  1682. {
  1683. i4Status = DtlsHS_SInit_MessageNode(psMsgListTrav, PpsMessageLayer);
  1684. if((i4Status & (int32_t)DEV_ERROR_CODE_MASK) == (int32_t)CMD_DEV_ERROR)
  1685. {
  1686. break;
  1687. }
  1688. else if((int32_t)OCP_FL_OK != i4Status)
  1689. {
  1690. i4Status = (int32_t)OCP_FL_GET_MSG_FROM_OPTIGA_ERROR;
  1691. break;
  1692. }
  1693. psMsgListTrav->eMsgState = eComplete;
  1694. }
  1695. if(OCP_HL_OK != DtlsHS_FSendMessage(psMsgListTrav, PpsMessageLayer))
  1696. {
  1697. i4Status = (int32_t)OCP_FL_FLIGHTSEND_ERROR;
  1698. break;
  1699. }
  1700. psMsgListTrav->eMsgState = eProcessed;
  1701. psMsgListTrav = psMsgListTrav->psNext;
  1702. }
  1703. if((int32_t)OCP_FL_OK == i4Status)
  1704. {
  1705. PpsMessageLayer->eFlight = eFlight1;
  1706. UPDATE_FSTATE(PpsThisFlight->bFlightState, (uint8_t)efTransmitted);
  1707. }
  1708. }
  1709. else if((uint8_t)efReTransmit == PpsThisFlight->bFlightState)
  1710. {
  1711. psMsgListTrav = PpsThisFlight->psMessageList;
  1712. //If already transmitted, now retransmit
  1713. while(NULL != psMsgListTrav)
  1714. {
  1715. if(OCP_HL_OK != DtlsHS_FSendMessage(psMsgListTrav, PpsMessageLayer))
  1716. {
  1717. i4Status = (int32_t)OCP_FL_FLIGHTSEND_ERROR;
  1718. break;
  1719. }
  1720. psMsgListTrav = psMsgListTrav->psNext;
  1721. }
  1722. if((int32_t)OCP_FL_OK == i4Status)
  1723. {
  1724. UPDATE_FSTATE(PpsThisFlight->bFlightState, (uint8_t)efTransmitted);
  1725. }
  1726. }
  1727. }
  1728. else
  1729. {
  1730. PpsThisFlight->bFlightState = (uint8_t)efDone;
  1731. break;
  1732. }
  1733. }while(0);
  1734. return i4Status;
  1735. }
  1736. /**
  1737. * Flight three handler to process flight 3 message.<br>
  1738. *
  1739. * \param[in] PbLastProcFlight Last processed flight.
  1740. * \param[in,out] PpsThisFlight Pointer to structure containing flight3 status.
  1741. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  1742. *
  1743. * \retval #OCP_FL_OK Successful execution
  1744. * \retval #OCP_FL_MSG_ERROR Failure in execution
  1745. * \retval #OCP_FL_MALLOC_FAILURE Malloc failure
  1746. * \retval #OCP_FL_GET_MSG_FROM_OPTIGA_ERROR Error from Security Chip
  1747. * \retval #OCP_FL_FLIGHTSEND_ERROR Error while sending flight to Security Chip
  1748. */
  1749. int32_t DtlsHS_Flight3Handler(uint8_t PbLastProcFlight, sFlightStats_d* PpsThisFlight, sMsgLyr_d* PpsMessageLayer)
  1750. {
  1751. int32_t i4Status = (int32_t)OCP_FL_OK;
  1752. sMsgInfo_d* psMsgListTrav = NULL;
  1753. uint16_t *pwMsgIDList = NULL;
  1754. uint8_t bMsgType = 0;
  1755. do
  1756. {
  1757. if(((uint8_t)efInit == PpsThisFlight->bFlightState) && ((uint8_t)eFlight2 == PbLastProcFlight))
  1758. {
  1759. //create list of messages of this flight and call Command lib to get messages from Security Chip
  1760. DtlsHS_GetFlightMsgInfo((uint8_t)eFlight3, &pwMsgIDList);
  1761. while(0xFF != MSG_ID(*pwMsgIDList))
  1762. {
  1763. psMsgListTrav = (sMsgInfo_d*)OCP_MALLOC(sizeof(sMsgInfo_d));
  1764. if(NULL == psMsgListTrav)
  1765. {
  1766. i4Status = (int32_t)OCP_FL_MALLOC_FAILURE;
  1767. break;
  1768. }
  1769. psMsgListTrav->bMsgType = MSG_ID(*pwMsgIDList);
  1770. psMsgListTrav->eMsgState = ePartial;
  1771. psMsgListTrav->psNext = NULL;
  1772. psMsgListTrav->psMsgMapPtr = NULL;
  1773. psMsgListTrav->psMsgHolder = NULL;
  1774. DtlsHS_InsertMsgNode(&PpsThisFlight->psMessageList, psMsgListTrav);
  1775. pwMsgIDList++;
  1776. }
  1777. if((int32_t)OCP_FL_OK == i4Status)
  1778. {
  1779. UPDATE_FSTATE(PpsThisFlight->bFlightState, (uint8_t)efReady);
  1780. }
  1781. }
  1782. else if((uint8_t)eFlight3 == PbLastProcFlight+1)
  1783. {
  1784. if(((uint8_t)efReady == PpsThisFlight->bFlightState) || ((uint8_t)efTransmitted == PpsThisFlight->bFlightState))
  1785. {
  1786. //Get Message from Security Chip and Send to Server
  1787. psMsgListTrav = PpsThisFlight->psMessageList;
  1788. if((uint8_t)efTransmitted == PpsThisFlight->bFlightState)
  1789. {
  1790. OCP_FREE(psMsgListTrav->psMsgHolder);
  1791. psMsgListTrav->psMsgHolder = NULL;
  1792. psMsgListTrav->eMsgState = ePartial;
  1793. }
  1794. while(NULL != psMsgListTrav)
  1795. {
  1796. if(ePartial == psMsgListTrav->eMsgState)
  1797. {
  1798. bMsgType = psMsgListTrav->bMsgType;
  1799. psMsgListTrav->bMsgType = (uint8_t)eClientHelloWithCookie;
  1800. i4Status = DtlsHS_SInit_MessageNode(psMsgListTrav, PpsMessageLayer);
  1801. psMsgListTrav->bMsgType = bMsgType;
  1802. if((i4Status & (int32_t)DEV_ERROR_CODE_MASK) == (int32_t)CMD_DEV_ERROR)
  1803. {
  1804. break;
  1805. }
  1806. else if((int32_t)OCP_FL_OK != i4Status)
  1807. {
  1808. i4Status = (int32_t)OCP_FL_GET_MSG_FROM_OPTIGA_ERROR;
  1809. break;
  1810. }
  1811. psMsgListTrav->eMsgState = eComplete;
  1812. }
  1813. if(OCP_HL_OK != DtlsHS_FSendMessage(psMsgListTrav, PpsMessageLayer))
  1814. {
  1815. i4Status = (int32_t)OCP_FL_FLIGHTSEND_ERROR;
  1816. break;
  1817. }
  1818. psMsgListTrav->eMsgState = eProcessed;
  1819. psMsgListTrav = psMsgListTrav->psNext;
  1820. }
  1821. if(OCP_FL_OK == i4Status)
  1822. {
  1823. PpsMessageLayer->eFlight = eFlight3;
  1824. UPDATE_FSTATE(PpsThisFlight->bFlightState, (uint8_t)efTransmitted);
  1825. }
  1826. }
  1827. }
  1828. else if((uint8_t)eFlight3 == PbLastProcFlight)
  1829. {
  1830. if((uint8_t)efReTransmit == PpsThisFlight->bFlightState)
  1831. {
  1832. psMsgListTrav = PpsThisFlight->psMessageList;
  1833. //If already transmitted, now retransmit
  1834. while(NULL != psMsgListTrav)
  1835. {
  1836. if(OCP_HL_OK != DtlsHS_FSendMessage(psMsgListTrav, PpsMessageLayer))
  1837. {
  1838. i4Status = (int32_t)OCP_FL_FLIGHTSEND_ERROR;
  1839. break;
  1840. }
  1841. psMsgListTrav = psMsgListTrav->psNext;
  1842. }
  1843. if(OCP_FL_OK == i4Status)
  1844. {
  1845. UPDATE_FSTATE(PpsThisFlight->bFlightState, (uint8_t)efTransmitted);
  1846. }
  1847. }
  1848. }
  1849. else
  1850. {
  1851. PpsThisFlight->bFlightState = (uint8_t)efDone;
  1852. break;
  1853. }
  1854. }while(0);
  1855. return i4Status;
  1856. }
  1857. /**
  1858. * Flight five handler to process flight 5 messages .<br>
  1859. *
  1860. * \param[in] PbLastProcFlight Last processed flight.
  1861. * \param[in,out] PpsThisFlight Pointer to structure containing flight5 status.
  1862. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  1863. *
  1864. * \retval #OCP_FL_OK Successful execution
  1865. * \retval #OCP_FL_MSG_ERROR Failure in execution
  1866. * \retval #OCP_FL_MALLOC_FAILURE Malloc failure
  1867. * \retval #OCP_FL_GET_MSG_FROM_OPTIGA_ERROR Error from Security Chip
  1868. * \retval #OCP_FL_FLIGHTSEND_ERROR Error while sending flight to Security Chip
  1869. */
  1870. //lint --e{818} suppress "This is ignored as same flight handler pointer is used for both send and receive"
  1871. int32_t DtlsHS_Flight5Handler(uint8_t PbLastProcFlight, sFlightStats_d* PpsThisFlight, sMsgLyr_d* PpsMessageLayer)
  1872. {
  1873. int32_t i4Status = (int32_t)OCP_FL_OK;
  1874. sMsgInfo_d* psMsgListTrav = NULL;
  1875. uint16_t *pwMsgIDList = NULL;
  1876. do
  1877. {
  1878. if(((uint8_t)efInit == PpsThisFlight->bFlightState) && ((uint8_t)eFlight4 == PbLastProcFlight))
  1879. {
  1880. //create list of messages of this flight and call Command lib to get messages from Security Chip
  1881. DtlsHS_GetFlightMsgInfo((uint8_t)eFlight5, &pwMsgIDList);
  1882. while(0xFF != MSG_ID(*pwMsgIDList))
  1883. {
  1884. if(OCP_FL_OK == DtlsHS_Flight5_CheckOptMsg(MSG_ID(*pwMsgIDList), &(PpsMessageLayer->rgbOptMsgList[0]), PpsMessageLayer))
  1885. {
  1886. psMsgListTrav = (sMsgInfo_d*)OCP_MALLOC(sizeof(sMsgInfo_d));
  1887. if(NULL == psMsgListTrav)
  1888. {
  1889. i4Status = (int32_t)OCP_FL_MALLOC_FAILURE;
  1890. break;
  1891. }
  1892. psMsgListTrav->bMsgType = MSG_ID(*pwMsgIDList);
  1893. psMsgListTrav->eMsgState = ePartial;
  1894. psMsgListTrav->psNext = NULL;
  1895. psMsgListTrav->psMsgMapPtr = NULL;
  1896. psMsgListTrav->psMsgHolder = NULL;
  1897. DtlsHS_AddMsgNode(&PpsThisFlight->psMessageList, psMsgListTrav);
  1898. }
  1899. pwMsgIDList++;
  1900. }
  1901. if((int32_t)OCP_FL_OK == i4Status)
  1902. {
  1903. UPDATE_FSTATE(PpsThisFlight->bFlightState, (uint8_t)efReady);
  1904. }
  1905. }
  1906. else if(((uint8_t)eFlight5 == PbLastProcFlight+1) || ((uint8_t)eFlight5 == PbLastProcFlight))
  1907. {
  1908. if((uint8_t)efReady == PpsThisFlight->bFlightState)
  1909. {
  1910. //Get Message from Security Chip and Send to Server
  1911. psMsgListTrav = PpsThisFlight->psMessageList;
  1912. while(NULL != psMsgListTrav)
  1913. {
  1914. if(ePartial == psMsgListTrav->eMsgState)
  1915. {
  1916. if((uint8_t)eChangeCipherSpec == psMsgListTrav->bMsgType)
  1917. {
  1918. i4Status = DtlsHS_SInit_ChangeCipherSpec(psMsgListTrav);
  1919. }
  1920. else
  1921. {
  1922. i4Status = DtlsHS_SInit_MessageNode(psMsgListTrav, PpsMessageLayer);
  1923. }
  1924. if((i4Status & (int32_t)DEV_ERROR_CODE_MASK) == (int32_t)CMD_DEV_ERROR)
  1925. {
  1926. break;
  1927. }
  1928. else if((int32_t)OCP_FL_OK != i4Status)
  1929. {
  1930. i4Status = (int32_t)OCP_FL_GET_MSG_FROM_OPTIGA_ERROR;
  1931. break;
  1932. }
  1933. psMsgListTrav->eMsgState = eComplete;
  1934. }
  1935. if(OCP_HL_OK != DtlsHS_FSendMessage(psMsgListTrav, PpsMessageLayer))
  1936. {
  1937. i4Status = (int32_t)OCP_FL_FLIGHTSEND_ERROR;
  1938. break;
  1939. }
  1940. psMsgListTrav->eMsgState = eProcessed;
  1941. psMsgListTrav = psMsgListTrav->psNext;
  1942. }
  1943. if((int32_t)OCP_FL_OK == i4Status)
  1944. {
  1945. PpsMessageLayer->eFlight = eFlight5;
  1946. UPDATE_FSTATE(PpsThisFlight->bFlightState, (uint8_t)efTransmitted);
  1947. }
  1948. }
  1949. else if(((uint8_t)efReTransmit == PpsThisFlight->bFlightState) || ((uint8_t)efTransmitted == PpsThisFlight->bFlightState))
  1950. {
  1951. PpsMessageLayer->psConfigRL->sRL.fRetransmit = TRUE;
  1952. psMsgListTrav = PpsThisFlight->psMessageList;
  1953. //If already transmitted, now retransmit
  1954. while(NULL != psMsgListTrav)
  1955. {
  1956. if(OCP_HL_OK != DtlsHS_FSendMessage(psMsgListTrav, PpsMessageLayer))
  1957. {
  1958. i4Status = (int32_t)OCP_FL_FLIGHTSEND_ERROR;
  1959. break;
  1960. }
  1961. psMsgListTrav = psMsgListTrav->psNext;
  1962. }
  1963. if((int32_t)OCP_FL_OK == i4Status)
  1964. {
  1965. UPDATE_FSTATE(PpsThisFlight->bFlightState, (uint8_t)efTransmitted);
  1966. }
  1967. }
  1968. }
  1969. }while(0);
  1970. return i4Status;
  1971. }
  1972. /**
  1973. * Validates the sequence number of message/ fragment received of flight 2.<br>
  1974. *
  1975. * \param[in] PbRxMsgID Message type of the received message/ fragment.
  1976. * \param[in] PwRxMsgSeqNum Message sequence number of the received message/ fragment.
  1977. * \param[in] PpsMessageList Pointer to list of messages.
  1978. *
  1979. * \retval #OCP_FL_OK Successful Execution
  1980. * \retval #OCP_FL_INVALID_MSG_SEQNUM Sequence number does not match to buffered fragment sequence number
  1981. */
  1982. int32_t DtlsHS_Flight2CheckMsgSeqNum(uint8_t PbRxMsgID, uint16_t PwRxMsgSeqNum, const sMsgInfo_d *PpsMessageList)
  1983. {
  1984. int32_t i4Status = (int32_t)OCP_FL_OK;
  1985. do
  1986. {
  1987. if(PwRxMsgSeqNum > 1)
  1988. {
  1989. i4Status = (int32_t)OCP_FL_INVALID_MSG_SEQNUM;
  1990. break;
  1991. }
  1992. while(NULL != PpsMessageList)
  1993. {
  1994. if(PpsMessageList->bMsgType == PbRxMsgID)
  1995. {
  1996. if(PpsMessageList->wMsgSequence != PwRxMsgSeqNum)
  1997. {
  1998. i4Status = (int32_t)OCP_FL_INVALID_MSG_SEQNUM;
  1999. break;
  2000. }
  2001. }
  2002. PpsMessageList = PpsMessageList->psNext;
  2003. }
  2004. }while(0);
  2005. return i4Status;
  2006. }
  2007. /**
  2008. * Flight two handler to process flight 2 messages.<br>
  2009. *
  2010. * \param[in] PbLastProcFlight Last processed flight.
  2011. * \param[in,out] PpsThisFlight Pointer to the structure containing flight2 status.
  2012. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  2013. *
  2014. * \retval #OCP_FL_OK Successful execution
  2015. * \retval #OCP_FL_MSG_ERROR Failure in execution
  2016. * \retval #OCP_FL_MALLOC_FAILURE Memory allocation failure
  2017. * \retval #OCP_FL_INVALID_MSG_SEQNUM Invalid Message sequence number
  2018. * \retval #OCP_FL_MSG_NOT_IN_FLIGHT Message doesn't belong to current flight
  2019. * \retval #OCP_FL_RXING Message is not yet completely received
  2020. */
  2021. //lint --e{818} suppress "This is ignored as same flight handler pointer is used for both send and receive"
  2022. int32_t DtlsHS_Flight2Handler(uint8_t PbLastProcFlight, sFlightStats_d* PpsThisFlight, sMsgLyr_d* PpsMessageLayer)
  2023. {
  2024. int32_t i4Status = (int32_t)OCP_FL_OK;
  2025. uint16_t bMsgSeqNum;
  2026. uint8_t bMsgID;
  2027. sMsgInfo_d* psMsgListTrav = NULL;
  2028. uint16_t wFlightLastMsgSeqNum = 0xFFFF;
  2029. do
  2030. {
  2031. //Check Message/ Fragment
  2032. //Optional Message Check
  2033. //Check Message Sequence Number
  2034. //Buffer the message/ De-Fragmentation()
  2035. //Message Completeness()
  2036. //Send to Security Chip for Processing()
  2037. if((uint8_t)efInit == PpsThisFlight->bFlightState)
  2038. {
  2039. PpsThisFlight->bFlightState = (uint8_t)efReady;
  2040. break;
  2041. }
  2042. else if((PpsThisFlight->bFlightState == (uint8_t)efReady) || (PpsThisFlight->bFlightState == (uint8_t)efProcessed) || (PpsThisFlight->bFlightState == (uint8_t)efReReceive))
  2043. {
  2044. if(PpsThisFlight->bFlightState == (uint8_t)efProcessed)
  2045. {
  2046. PpsThisFlight->bFlightState = (uint8_t)efReReceive;
  2047. }
  2048. bMsgSeqNum = HS_MESSAGE_SEQNUM(PpsMessageLayer->sMsg.prgbStream);
  2049. bMsgID = HS_MESSAGE_TYPE(PpsMessageLayer->sMsg.prgbStream);
  2050. if(OCP_FL_OK != DtlsHS_Flight_LimitChk((uint8_t)eFlight2,bMsgID))
  2051. {
  2052. i4Status = (int32_t)OCP_FL_MSG_NOT_IN_FLIGHT;
  2053. break;
  2054. }
  2055. if(OCP_FL_OK != FLIGHT_IDLIMITCHK(1, PbLastProcFlight, 3))
  2056. {
  2057. i4Status = (int32_t)OCP_FL_INVALID_PROCFLIGHT;
  2058. break;
  2059. }
  2060. if(OCP_FL_OK != DtlsHS_Flight2CheckMsgSeqNum(bMsgID, bMsgSeqNum, PpsThisFlight->psMessageList))
  2061. {
  2062. i4Status = (int32_t)OCP_FL_INVALID_MSG_SEQNUM;
  2063. break;
  2064. }
  2065. i4Status = DtlsHS_FlightMsgChkAndBuffer(PpsThisFlight->psMessageList, bMsgID, &(PpsMessageLayer->sMsg), PpsMessageLayer, (uint8_t)eFlight2);
  2066. if(((int32_t)OCP_FL_ERROR == i4Status) || ((int32_t)OCP_FL_MSG_MAXCOUNT == i4Status) || ((int32_t)OCP_FL_MALLOC_FAILURE == i4Status))
  2067. {
  2068. break;
  2069. }
  2070. PpsMessageLayer->eFlight = eFlight2;
  2071. if((int32_t)OCP_FL_MSG_NODE_NOT_AVAIL == i4Status)
  2072. {
  2073. // Buffer the message
  2074. psMsgListTrav = (sMsgInfo_d*)OCP_MALLOC(sizeof(sMsgInfo_d));
  2075. if(NULL == psMsgListTrav)
  2076. {
  2077. i4Status = (int32_t)OCP_FL_MALLOC_FAILURE;
  2078. break;
  2079. }
  2080. psMsgListTrav->eMsgState = ePartial;
  2081. psMsgListTrav->psNext = NULL;
  2082. psMsgListTrav->psMsgMapPtr = NULL;
  2083. psMsgListTrav->psMsgHolder = (uint8_t*)OCP_MALLOC(HS_MESSAGE_LENGTH(PpsMessageLayer->sMsg.prgbStream) + OVERHEAD_LEN);
  2084. if(NULL == psMsgListTrav->psMsgHolder)
  2085. {
  2086. DtlsHS_FreeMsgNode(psMsgListTrav);
  2087. i4Status = (int32_t)OCP_FL_MALLOC_FAILURE;
  2088. break;
  2089. }
  2090. i4Status = DtlsHS_RInit_MessageNode(psMsgListTrav, PpsMessageLayer);
  2091. if(((int32_t)OCP_FL_OK != i4Status) && ((int32_t)OCP_FL_MSG_INCOMPLETE != i4Status))
  2092. {
  2093. DtlsHS_FreeMsgNode(psMsgListTrav);
  2094. break;
  2095. }
  2096. DtlsHS_InsertMsgNode(&PpsThisFlight->psMessageList, psMsgListTrav);
  2097. }
  2098. //Check Flight Completeness
  2099. if(NULL != PpsThisFlight->psMessageList)
  2100. {
  2101. if((eComplete != PpsThisFlight->psMessageList->eMsgState) && (eProcessed != PpsThisFlight->psMessageList->eMsgState))
  2102. {
  2103. i4Status = (int32_t)OCP_FL_RXING;
  2104. break;
  2105. }
  2106. }
  2107. else
  2108. {
  2109. i4Status = (int32_t)OCP_FL_ERROR;
  2110. break;
  2111. }
  2112. PpsThisFlight->bFlightState = (uint8_t)efReceived;
  2113. //Send to Security Chip For Processing
  2114. i4Status = DtlsHS_SendFlightToOptiga(PpsThisFlight->psMessageList, PpsMessageLayer);
  2115. if(OCP_ML_OK != i4Status)
  2116. {
  2117. break;
  2118. }
  2119. //Update Flight Status
  2120. PpsThisFlight->bFlightState = (uint8_t)efProcessed;
  2121. DtlsHS_ResetFlight2MsgNode(PpsThisFlight);
  2122. DtlsHS_FlightGetLastMsgSeqNum(PpsThisFlight->psMessageList, &wFlightLastMsgSeqNum);
  2123. UPDATE_RX_MSGSEQNUM(PpsMessageLayer->dwRMsgSeqNum, wFlightLastMsgSeqNum);
  2124. }
  2125. i4Status = OCP_FL_OK;
  2126. }while(0);
  2127. return i4Status;
  2128. }
  2129. /**
  2130. * Checks if message sequence number of received message/ fragment of flight4 is correct.<br>
  2131. *
  2132. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  2133. * \param[in] PbRxMsgID Message type of received message/ fragment.
  2134. * \param[in] PwRxMsgSeqNum Message sequence number of received message/ fragment.
  2135. *
  2136. * \retval #OCP_FL_OK Successful execution
  2137. * \retval #OCP_FL_ERROR Message Sequence number incorrect
  2138. */
  2139. int32_t DtlsHS_Flight4CheckMsgSeqNum(const sMsgLyr_d* PpsMessageLayer, uint8_t PbRxMsgID, uint16_t PwRxMsgSeqNum)
  2140. {
  2141. int32_t i4Status = (int32_t)OCP_FL_ERROR;
  2142. uint16_t *pwFlightMsgs = NULL;
  2143. uint32_t dwPrevFlightMsgSeq, dwLastFlightSeqNum ;
  2144. uint8_t bOptMsgCount = 0;
  2145. uint8_t bLoopCount, bIndex, bMandtryMsgCount = 0;
  2146. do
  2147. {
  2148. dwLastFlightSeqNum = PpsMessageLayer->dwRMsgSeqNum;
  2149. dwPrevFlightMsgSeq = PpsMessageLayer->dwRMsgSeqNum;
  2150. if(dwPrevFlightMsgSeq == 0xFFFFFFFF)
  2151. {
  2152. bLoopCount = 2;
  2153. }
  2154. else
  2155. {
  2156. bLoopCount = 1;
  2157. }
  2158. for(bIndex = 0; bIndex < bLoopCount; bIndex++)
  2159. {
  2160. DtlsHS_GetFlightMsgInfo((uint8_t)eFlight4, &pwFlightMsgs);
  2161. do
  2162. {
  2163. if(IsMandatory(*pwFlightMsgs))
  2164. {
  2165. bMandtryMsgCount++;
  2166. }
  2167. else
  2168. {
  2169. bOptMsgCount++;
  2170. }
  2171. if(MSG_ID(*pwFlightMsgs) == PbRxMsgID)
  2172. {
  2173. if(((dwLastFlightSeqNum+bMandtryMsgCount+bOptMsgCount) == PwRxMsgSeqNum) || ((dwLastFlightSeqNum+bMandtryMsgCount) == PwRxMsgSeqNum))
  2174. {
  2175. i4Status = (int32_t)OCP_FL_OK;
  2176. break;
  2177. }
  2178. }
  2179. }while(0xFFFF != *(++pwFlightMsgs));
  2180. if((int32_t)OCP_FL_OK == i4Status)
  2181. {
  2182. break;
  2183. }
  2184. dwLastFlightSeqNum = dwPrevFlightMsgSeq + 1;
  2185. bMandtryMsgCount = 0;
  2186. bOptMsgCount = 0;
  2187. }
  2188. }while(FALSE);
  2189. return i4Status;
  2190. }
  2191. /**
  2192. * Flight four handler to process flight 4 messages.<br>
  2193. *
  2194. * \param[in] PbLastProcFlight Last processed flight.
  2195. * \param[in,out] PpsThisFlight Pointer to the structure containing flight4 status.
  2196. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  2197. *
  2198. * \retval #OCP_FL_OK Successful execution
  2199. * \retval #OCP_FL_MSG_ERROR Failure in execution
  2200. * \retval #OCP_FL_MALLOC_FAILURE Memory allocation failure
  2201. * \retval #OCP_FL_INVALID_MSG_SEQNUM Invalid Message sequence number
  2202. * \retval #OCP_FL_MSG_NOT_IN_FLIGHT Message doesn't belong to current flight
  2203. * \retval #OCP_FL_RXING Message is not yet completely received
  2204. * \retval #OCP_FL_MSG_MAXCOUNT Message is received at least 6 times
  2205. */
  2206. int32_t DtlsHS_Flight4Handler(uint8_t PbLastProcFlight, sFlightStats_d* PpsThisFlight, sMsgLyr_d* PpsMessageLayer)
  2207. {
  2208. int32_t i4Status = (int32_t)OCP_FL_OK;
  2209. sMsgInfo_d* psMsgListTrav = NULL;
  2210. uint8_t bMsgID;
  2211. uint16_t wMsgSeqNum;
  2212. uint32_t dwMsgLen;
  2213. uint16_t wFlightLastMsgSeqNum = 0xFFFF;
  2214. do
  2215. {
  2216. //Check Message/ Fragment
  2217. //Optional Message Check
  2218. //Check Message Sequence Number
  2219. //Buffer the message/ De-Fragmentation()
  2220. //Message Completeness()
  2221. //Send to Security Chip for Processing()
  2222. if((uint8_t)efInit == PpsThisFlight->bFlightState)
  2223. {
  2224. PpsThisFlight->bFlightState = (uint8_t)efReady;
  2225. break;
  2226. }
  2227. else if((uint8_t)efReady == PpsThisFlight->bFlightState)
  2228. {
  2229. bMsgID = HS_MESSAGE_TYPE(PpsMessageLayer->sMsg.prgbStream);
  2230. wMsgSeqNum = HS_MESSAGE_SEQNUM(PpsMessageLayer->sMsg.prgbStream);
  2231. dwMsgLen = HS_MESSAGE_LENGTH(PpsMessageLayer->sMsg.prgbStream);
  2232. if(OCP_FL_OK != DtlsHS_Flight_LimitChk((uint8_t)eFlight4,bMsgID))
  2233. {
  2234. i4Status = (int32_t)OCP_FL_MSG_NOT_IN_FLIGHT;
  2235. break;
  2236. }
  2237. if(0x00 != PpsMessageLayer->psConfigRL->sRL.bDecRecord)
  2238. {
  2239. i4Status = (int32_t)OCP_FL_MSG_NOT_IN_FLIGHT;
  2240. break;
  2241. }
  2242. if(OCP_FL_OK != FLIGHT_IDLIMITCHK(1, PbLastProcFlight, 5))
  2243. {
  2244. i4Status = (int32_t)OCP_FL_INVALID_PROCFLIGHT;
  2245. break;
  2246. }
  2247. if(OCP_FL_OK != DtlsHS_Flight4CheckMsgSeqNum(PpsMessageLayer, bMsgID, wMsgSeqNum))
  2248. {
  2249. i4Status = (int32_t)OCP_FL_INVALID_MSG_SEQNUM;
  2250. break;
  2251. }
  2252. i4Status = DtlsHS_FlightMsgChkAndBuffer(PpsThisFlight->psMessageList, bMsgID, &(PpsMessageLayer->sMsg), PpsMessageLayer, (uint8_t)eFlight4);
  2253. if(((int32_t)OCP_FL_ERROR == i4Status) || ((int32_t)OCP_FL_MSG_MAXCOUNT == i4Status) || ((int32_t)OCP_FL_MALLOC_FAILURE == i4Status))
  2254. {
  2255. break;
  2256. }
  2257. PpsMessageLayer->eFlight = eFlight4;
  2258. if((int32_t)OCP_FL_MSG_NODE_NOT_AVAIL == i4Status)
  2259. {
  2260. // Buffer the message
  2261. psMsgListTrav = (sMsgInfo_d*)OCP_MALLOC(sizeof(sMsgInfo_d));
  2262. if(NULL == psMsgListTrav)
  2263. {
  2264. i4Status = (int32_t)OCP_FL_MALLOC_FAILURE;
  2265. break;
  2266. }
  2267. psMsgListTrav->eMsgState = ePartial;
  2268. psMsgListTrav->psNext = NULL;
  2269. psMsgListTrav->psMsgMapPtr = NULL;
  2270. psMsgListTrav->psMsgHolder = (uint8_t*)OCP_MALLOC(HS_MESSAGE_LENGTH(PpsMessageLayer->sMsg.prgbStream) + OVERHEAD_LEN);
  2271. if(NULL == psMsgListTrav->psMsgHolder)
  2272. {
  2273. DtlsHS_FreeMsgNode(psMsgListTrav);
  2274. i4Status = (int32_t)OCP_FL_MALLOC_FAILURE;
  2275. break;
  2276. }
  2277. i4Status = DtlsHS_RInit_MessageNode(psMsgListTrav, PpsMessageLayer);
  2278. if(((int32_t)OCP_FL_OK != i4Status) && ((int32_t)OCP_FL_MSG_INCOMPLETE != i4Status))
  2279. {
  2280. DtlsHS_FreeMsgNode(psMsgListTrav);
  2281. break;
  2282. }
  2283. DtlsHS_InsertMsgNode(&PpsThisFlight->psMessageList, psMsgListTrav);
  2284. }
  2285. //Check Flight Completeness
  2286. i4Status = DtlsHS_CheckFlight_Compl(PpsThisFlight->psMessageList, (uint8_t)eFlight4, PpsMessageLayer);
  2287. if((int32_t)OCP_FL_MSG_INCOMPLETE == i4Status)
  2288. {
  2289. i4Status = (int32_t)OCP_FL_RXING;
  2290. break;
  2291. }
  2292. if(OCP_FL_OK != i4Status)
  2293. {
  2294. break;
  2295. }
  2296. PpsThisFlight->bFlightState = (uint8_t)efReceived;
  2297. //Send to Security Chip For Processing
  2298. i4Status = DtlsHS_SendFlightToOptiga(PpsThisFlight->psMessageList, PpsMessageLayer);
  2299. if(OCP_ML_OK != i4Status)
  2300. {
  2301. break;
  2302. }
  2303. //Update Flight Status
  2304. PpsThisFlight->bFlightState = (uint8_t)efProcessed;
  2305. DtlsHS_FlightGetLastMsgSeqNum(PpsThisFlight->psMessageList, &wFlightLastMsgSeqNum);
  2306. UPDATE_RX_MSGSEQNUM(PpsMessageLayer->dwRMsgSeqNum, wFlightLastMsgSeqNum);
  2307. PpsMessageLayer->eFlight = eFlight0;
  2308. DtlsHS_Flight4ClearMsgsInList(PpsThisFlight->psMessageList);
  2309. }
  2310. else if (((uint8_t)efProcessed == PpsThisFlight->bFlightState) || ((uint8_t)efReReceive == PpsThisFlight->bFlightState))
  2311. {
  2312. if((uint8_t)efProcessed == PpsThisFlight->bFlightState)
  2313. {
  2314. UPDATE_FSTATE(PpsThisFlight->bFlightState, (uint8_t)efReReceive);
  2315. }
  2316. if(PpsMessageLayer->psConfigRL->sRL.bRecvCCSRecord == CCS_RECORD_RECV)
  2317. {
  2318. i4Status = (int32_t)OCP_FL_MSG_NOT_IN_FLIGHT;
  2319. break;
  2320. }
  2321. bMsgID = HS_MESSAGE_TYPE(PpsMessageLayer->sMsg.prgbStream);
  2322. wMsgSeqNum = HS_MESSAGE_SEQNUM(PpsMessageLayer->sMsg.prgbStream);
  2323. dwMsgLen = HS_MESSAGE_LENGTH(PpsMessageLayer->sMsg.prgbStream);
  2324. psMsgListTrav = PpsThisFlight->psMessageList;
  2325. if(OCP_FL_OK != DtlsHS_Flight_LimitChk((uint8_t)eFlight4,bMsgID))
  2326. {
  2327. i4Status = (int32_t)OCP_FL_MSG_NOT_IN_FLIGHT;
  2328. break;
  2329. }
  2330. if(0x00 != PpsMessageLayer->psConfigRL->sRL.bDecRecord)
  2331. {
  2332. i4Status = (int32_t)OCP_FL_MSG_NOT_IN_FLIGHT;
  2333. break;
  2334. }
  2335. if(OCP_FL_OK != DtlsHS_Flight4CheckBuffMsg(bMsgID, wMsgSeqNum, dwMsgLen, PpsThisFlight->psMessageList))
  2336. {
  2337. i4Status = (int32_t)OCP_FL_MSG_IGNORE;
  2338. break;
  2339. }
  2340. PpsMessageLayer->eFlight = eFlight4;
  2341. i4Status = DtlsHS_Flight4UpdateMsgStat(bMsgID, PpsThisFlight->psMessageList, PpsMessageLayer);
  2342. if(((int32_t)OCP_FL_OK != i4Status) && ((int32_t)OCP_FL_MSG_INCOMPLETE != i4Status))
  2343. {
  2344. break;
  2345. }
  2346. if(OCP_FL_OK != DtlsHS_Flight4ReRxCompl(PpsThisFlight->psMessageList))
  2347. {
  2348. i4Status = (int32_t)OCP_FL_RXING;
  2349. break;
  2350. }
  2351. //Update Flight Status
  2352. UPDATE_FSTATE(PpsThisFlight->bFlightState, (uint8_t)efProcessed);
  2353. DtlsHS_Flight4ClearMsgsInList(PpsThisFlight->psMessageList);
  2354. }
  2355. i4Status = (int32_t)OCP_FL_OK;
  2356. }while(0);
  2357. return i4Status;
  2358. }
  2359. /**
  2360. * Checks whether flight six is received.<br>
  2361. *
  2362. * \param[in] PpsMessageList Pointer to the list of messages of flight6.
  2363. *
  2364. * \retval #OCP_FL_OK Flight six is received
  2365. * \retval #OCP_FL_ERROR Flight six is not received
  2366. */
  2367. _STATIC_H int32_t DtlsHS_CheckFlight6_Compl(sMsgInfo_d *PpsMessageList)
  2368. {
  2369. int32_t i4Status = (int32_t)OCP_FL_OK;
  2370. sMsgInfo_d * psMsgListTrav = PpsMessageList;
  2371. uint16_t * pwFlightMsgs;
  2372. uint8_t bTotalMsgInFlight;
  2373. uint8_t bNumMsgRxdInFlight = 0;
  2374. DtlsHS_GetFlightMsgInfo((uint8_t)eFlight6, &pwFlightMsgs);
  2375. DtlsHS_GetTotalMsgCount(pwFlightMsgs, &bTotalMsgInFlight);
  2376. do
  2377. {
  2378. while(NULL != psMsgListTrav)
  2379. {
  2380. bNumMsgRxdInFlight++;
  2381. if(eComplete != psMsgListTrav->eMsgState)
  2382. {
  2383. i4Status = (int32_t)OCP_FL_MSG_INCOMPLETE;
  2384. break;
  2385. }
  2386. psMsgListTrav = psMsgListTrav->psNext;
  2387. }
  2388. if(((int32_t)OCP_FL_OK == i4Status) && (bNumMsgRxdInFlight != bTotalMsgInFlight))
  2389. {
  2390. i4Status = (int32_t)OCP_FL_MSG_INCOMPLETE;
  2391. break;
  2392. }
  2393. }while(0);
  2394. return i4Status;
  2395. }
  2396. /**
  2397. * Flight six handler to process flight 6 messages.<br>
  2398. *
  2399. * \param[in] PbLastProcFlight Last processed flight.
  2400. * \param[in,out] PpsThisFlight Pointer to the structure containing flight6 status.
  2401. * \param[in] PpsMessageLayer Pointer to the structure containing message configuration information.
  2402. *
  2403. * \retval #OCP_FL_OK Successful execution
  2404. * \retval #OCP_FL_MSG_ERROR Failure in execution
  2405. * \retval #OCP_FL_MALLOC_FAILURE Memory allocation failure
  2406. * \retval #OCP_FL_INVALID_MSG_SEQNUM Invalid Message sequence number
  2407. * \retval #OCP_FL_MSG_NOT_IN_FLIGHT Message doesn't belong to current flight
  2408. * \retval #OCP_FL_RXING Message is not yet completely received
  2409. * \retval #OCP_FL_MSG_MAXCOUNT Message is received at least 6 times
  2410. */
  2411. int32_t DtlsHS_Flight6Handler(uint8_t PbLastProcFlight, sFlightStats_d* PpsThisFlight, sMsgLyr_d* PpsMessageLayer)
  2412. {
  2413. int32_t i4Status = (int32_t)OCP_FL_OK;
  2414. sMsgInfo_d* psMsgListTrav = NULL;
  2415. uint8_t bMsgID;
  2416. uint16_t wMsgSeqNum = 0;
  2417. sCBStateTrn_d sCBStateTrn;
  2418. uint16_t wFlightLastMsgSeqNum = 0xFFFF;
  2419. /// @cond hidden
  2420. #define SETFULL_BITMAP 0xFF
  2421. #define SIZE_OF_CCSMSG 0x01
  2422. #define CHANGE_CIPHER_SPEC_PROTOCOL 0x01
  2423. #define INTERNAL_PROC_ERROR 0x06
  2424. #define INVALID_HS_MESSAGE 0x21
  2425. /// @endcond
  2426. do
  2427. {
  2428. //Check Message/ Fragment
  2429. //Optional Message Check
  2430. //Check Message Sequence Number
  2431. //Buffer the message/ De-Fragmentation()
  2432. //Message Completeness()
  2433. //Send to Security Chip for Processing()
  2434. if((uint8_t)efInit == PpsThisFlight->bFlightState)
  2435. {
  2436. PpsThisFlight->bFlightState = (uint8_t)efReady;
  2437. break;
  2438. }
  2439. else if((uint8_t)efReady == PpsThisFlight->bFlightState)
  2440. {
  2441. if(PpsMessageLayer->psConfigRL->sRL.bRecvCCSRecord == CCS_RECORD_RECV)
  2442. {
  2443. if((*(PpsMessageLayer->sMsg.prgbStream) != (uint8_t)CHANGE_CIPHER_SPEC_PROTOCOL) || (PpsMessageLayer->sMsg.wLen != (uint16_t)SIZE_OF_CCSMSG))
  2444. {
  2445. i4Status = (int32_t)OCP_FL_MSG_NOT_IN_FLIGHT;
  2446. PpsMessageLayer->psConfigRL->sRL.bRecvCCSRecord = CCS_RECORD_NOTRECV;
  2447. break;
  2448. }
  2449. if(NULL != PpsMessageLayer->psConfigRL->sRL.fServerStateTrn)
  2450. {
  2451. sCBStateTrn.phRLHdl = PpsMessageLayer->psConfigRL->sRL.phRLHdl;
  2452. PpsMessageLayer->psConfigRL->sRL.fServerStateTrn((Void*)&sCBStateTrn);
  2453. }
  2454. else
  2455. {
  2456. i4Status = (int32_t)OCP_FL_NULL_PARAM;
  2457. break;
  2458. }
  2459. bMsgID = (uint8_t)eChangeCipherSpec;
  2460. }
  2461. else
  2462. {
  2463. bMsgID = HS_MESSAGE_TYPE(PpsMessageLayer->sMsg.prgbStream);
  2464. wMsgSeqNum = HS_MESSAGE_SEQNUM(PpsMessageLayer->sMsg.prgbStream);
  2465. }
  2466. if(OCP_FL_OK != DtlsHS_Flight_LimitChk((uint8_t)eFlight6, bMsgID))
  2467. {
  2468. i4Status = (int32_t)OCP_FL_MSG_NOT_IN_FLIGHT;
  2469. break;
  2470. }
  2471. if(OCP_FL_OK != FLIGHT_IDLIMITCHK(4, PbLastProcFlight, 6))
  2472. {
  2473. i4Status = (int32_t)OCP_FL_INVALID_PROCFLIGHT;
  2474. break;
  2475. }
  2476. if(OCP_FL_OK != DtlsHS_Flight6CheckMsgSeqNum(PpsMessageLayer, bMsgID, wMsgSeqNum))
  2477. {
  2478. i4Status = (int32_t)OCP_FL_INVALID_MSG_SEQNUM;
  2479. break;
  2480. }
  2481. i4Status = DtlsHS_FlightMsgChkAndBuffer(PpsThisFlight->psMessageList, bMsgID, &(PpsMessageLayer->sMsg), PpsMessageLayer, (uint8_t)eFlight6);
  2482. if(((int32_t)OCP_FL_ERROR == i4Status) || ((int32_t)OCP_FL_MSG_MAXCOUNT == i4Status) || ((int32_t)OCP_FL_MALLOC_FAILURE == i4Status))
  2483. {
  2484. break;
  2485. }
  2486. PpsMessageLayer->eFlight = eFlight6;
  2487. if((int32_t)OCP_FL_MSG_NODE_NOT_AVAIL == i4Status)
  2488. {
  2489. // Buffer the message
  2490. psMsgListTrav = (sMsgInfo_d*)OCP_MALLOC(sizeof(sMsgInfo_d));
  2491. if(NULL == psMsgListTrav)
  2492. {
  2493. i4Status = (int32_t)OCP_FL_MALLOC_FAILURE;
  2494. break;
  2495. }
  2496. if((uint8_t)eChangeCipherSpec == bMsgID)
  2497. {
  2498. psMsgListTrav->bMsgType = (uint8_t)eChangeCipherSpec;
  2499. psMsgListTrav->psNext = NULL;
  2500. psMsgListTrav->psMsgHolder = NULL;
  2501. psMsgListTrav->bMsgCount = 1;
  2502. psMsgListTrav->psMsgMapPtr = (uint8_t*)OCP_MALLOC(SIZE_OF_CCSMSG);
  2503. if(NULL == psMsgListTrav->psMsgMapPtr)
  2504. {
  2505. DtlsHS_FreeMsgNode(psMsgListTrav);
  2506. i4Status = (int32_t)OCP_FL_MALLOC_FAILURE;
  2507. break;
  2508. }
  2509. *psMsgListTrav->psMsgMapPtr = SETFULL_BITMAP;
  2510. psMsgListTrav->dwMsgLength = SIZE_OF_CCSMSG;
  2511. psMsgListTrav->eMsgState = eComplete;
  2512. i4Status = OCP_FL_OK;
  2513. }
  2514. else
  2515. {
  2516. psMsgListTrav->eMsgState = ePartial;
  2517. psMsgListTrav->psNext = NULL;
  2518. psMsgListTrav->psMsgMapPtr = NULL;
  2519. psMsgListTrav->psMsgHolder = (uint8_t*)OCP_MALLOC(HS_MESSAGE_LENGTH(PpsMessageLayer->sMsg.prgbStream) + OVERHEAD_LEN);
  2520. if(NULL == psMsgListTrav->psMsgHolder)
  2521. {
  2522. DtlsHS_FreeMsgNode(psMsgListTrav);
  2523. i4Status = (int32_t)OCP_FL_MALLOC_FAILURE;
  2524. break;
  2525. }
  2526. i4Status = DtlsHS_RInit_MessageNode(psMsgListTrav, PpsMessageLayer);
  2527. if(((int32_t)OCP_FL_OK != i4Status) && ((int32_t)OCP_FL_MSG_INCOMPLETE != i4Status))
  2528. {
  2529. DtlsHS_FreeMsgNode(psMsgListTrav);
  2530. break;
  2531. }
  2532. }
  2533. DtlsHS_InsertMsgNode(&PpsThisFlight->psMessageList, psMsgListTrav);
  2534. }
  2535. //Check Flight Completeness
  2536. if((int32_t)OCP_FL_MSG_INCOMPLETE == DtlsHS_CheckFlight6_Compl(PpsThisFlight->psMessageList))
  2537. {
  2538. i4Status = (int32_t)OCP_FL_RXING;
  2539. break;
  2540. }
  2541. if(OCP_FL_OK != i4Status)
  2542. {
  2543. break;
  2544. }
  2545. PpsThisFlight->bFlightState = (uint8_t)efReceived;
  2546. //Send to Security Chip For Processing
  2547. i4Status = DtlsHS_SendFlightToOptiga(PpsThisFlight->psMessageList, PpsMessageLayer);
  2548. if(OCP_ML_OK != i4Status)
  2549. {
  2550. if((i4Status & (int32_t)DEV_ERROR_CODE_MASK) == (int32_t)CMD_DEV_ERROR)
  2551. {
  2552. if((i4Status & (int32_t)DEV_ERROR_LSBCODE_MASK) == INTERNAL_PROC_ERROR)
  2553. {
  2554. i4Status = (int32_t)OCP_FL_INT_ERROR;
  2555. }
  2556. else if((i4Status & (int32_t)DEV_ERROR_LSBCODE_MASK) == INVALID_HS_MESSAGE)
  2557. {
  2558. i4Status = (int32_t)OCP_FL_HS_ERROR;
  2559. }
  2560. }
  2561. break;
  2562. }
  2563. //Update Flight Status
  2564. PpsThisFlight->bFlightState = (uint8_t)efProcessed;
  2565. DtlsHS_FlightGetLastMsgSeqNum(PpsThisFlight->psMessageList, &wFlightLastMsgSeqNum);
  2566. UPDATE_RX_MSGSEQNUM(PpsMessageLayer->dwRMsgSeqNum, wFlightLastMsgSeqNum);
  2567. }
  2568. i4Status = (int32_t)OCP_FL_OK;
  2569. }while(0);
  2570. /// @cond hidden
  2571. #undef SETFULL_BITMAP
  2572. #undef SIZE_OF_CCSMSG
  2573. #undef CHANGE_CIPHER_SPEC_PROTOCOL
  2574. #undef INTERNAL_PROC_ERROR
  2575. #undef INVALID_HS_MESSAGE
  2576. /// @endcond
  2577. return i4Status;
  2578. }
  2579. /**
  2580. * @}
  2581. */
  2582. #endif /*MODULE_ENABLE_DTLS_MUTUAL_AUTH*/