DtlsRecordLayer.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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 DtlsRecordLayer.c
  26. *
  27. * \brief This file provides APIs for the record layer functionalities.
  28. *
  29. * \addtogroup grOCP
  30. * @{
  31. *
  32. */
  33. #include "optiga/common/Util.h"
  34. #include "optiga/dtls/DtlsRecordLayer.h"
  35. #ifdef MODULE_ENABLE_DTLS_MUTUAL_AUTH
  36. ///Default size of the window
  37. #define DEFAULT_WINDOW_SIZE 32
  38. /// @cond hidden
  39. //Protocol version for DTLS 1.2
  40. #define PROTOCOL_VERSION_1_2 0xFEFD
  41. #define UDP_OVERHEAD 28 //20(IP Header) + 8(UDP Header)
  42. /**
  43. * \brief Structure to provide input to DtlsRL_CallBack_ValidateRec.
  44. */
  45. typedef struct sCBValidateRec_d
  46. {
  47. ///Record Layer
  48. sRecordLayer_d* psRecordLayer;
  49. ///Input record
  50. sbBlob_d* psbBlob;
  51. ///Record data
  52. sRecordData_d* psRecordData;
  53. }sCBValidateRec_d;
  54. /// @endcond
  55. /**
  56. * \brief Validates the record header and decrypts the fragments if PpsRecData.bEncDecFlag is set
  57. */
  58. _STATIC_H int32_t DtlsRL_Record_ProcessRecord(const sRecordLayer_d* PpsRecordLayer,const sbBlob_d* PpsBlobRecord,sRecordData_d* PpsRecData);
  59. /**
  60. * \brief Prepares a record and encrypts the fragment if PpsRecData.bEncDecFlag is set.<br>
  61. */
  62. _STATIC_H int32_t DtlsRL_Record_PrepareRecord(sRecordLayer_d* PpsRecordLayer,const sRecordData_d* PpsRecData,sbBlob_d* PpsBlobRecord);
  63. /**
  64. * \brief Callback to validate the record.
  65. */
  66. _STATIC_H int32_t DtlsRL_CallBack_ValidateRec(const Void* PpvParams);
  67. /**
  68. * \brief Gets the count of the total number of record in the buffer
  69. */
  70. _STATIC_H int32_t DtlsRL_GetRecordCount(uint8_t* PpbBuffer,uint16_t PwLen,uint8_t* PpbRecCount);
  71. /**
  72. *
  73. * Validates the record header and decrypts the fragments if PpsRecData.bEncDecFlag is set<br>
  74. * Updates sRecordData_d.dwRecordInfo with information, whether the record is from<br>
  75. * next epoch or the current epoch.<br>
  76. * \param[in] PpsRecordLayer Pointer to #sRecordLayer_d structure
  77. * \param[in] PpsBlobRecord Pointer to a blob containing record
  78. * \param[out] PpsRecData Pointer to a blob containing processed record
  79. *
  80. * \retval #OCP_RL_OK Successful execution
  81. * \retval #OCP_RL_ERROR Failure in execution
  82. *
  83. */
  84. _STATIC_H int32_t DtlsRL_Record_ProcessRecord(const sRecordLayer_d* PpsRecordLayer,const sbBlob_d* PpsBlobRecord,sRecordData_d* PpsRecData)
  85. {
  86. int32_t i4Status = OCP_RL_ERROR;
  87. uint16_t wRecvFragLen;
  88. uint16_t wServerEpoch;
  89. sbBlob_d sBlobCipherMsg;
  90. sbBlob_d sBlobPlainMsg;
  91. uint8_t bContentType;
  92. uint8_t* pbDecData = NULL;
  93. uint16_t wInputProtVersion;
  94. do
  95. {
  96. /// @cond hidden
  97. #define MIN_DEC_PAYLOAD 17 //(1(Min 1 byte cipher text)+ 8(MAC) + 8 (Explicit nonce)))
  98. /// @endcond
  99. //Check for zero len blobs
  100. if((0 == PpsBlobRecord->wLen)||(0 == PpsRecData->psBlobInOutMsg->wLen))
  101. {
  102. //Any error code
  103. i4Status = (int32_t)OCP_RL_INVALID_RECORD_LENGTH;
  104. break;
  105. }
  106. bContentType = PpsBlobRecord->prgbStream[OFFSET_RL_CONTENTTYPE];
  107. wServerEpoch = Utility_GetUint16(PpsBlobRecord->prgbStream + OFFSET_RL_EPOCH);
  108. wInputProtVersion = Utility_GetUint16(PpsBlobRecord->prgbStream + OFFSET_RL_PROT_VERSION);
  109. //Validate protocol version
  110. #ifdef SUPPORT_OPENSSL
  111. //Protocol version for DTLS 1.0
  112. #define PROTOCOL_VERSION_1_0 0xFEFF
  113. //for Hello verify request, allow DTLS 1.2 ,1.0 as protocol version
  114. if(*(PpsBlobRecord->prgbStream+LENGTH_RL_HEADER) == 0x03)
  115. {
  116. if((PROTOCOL_VERSION_1_2 != wInputProtVersion)&&(PROTOCOL_VERSION_1_0 != wInputProtVersion))
  117. {
  118. i4Status = (int32_t)OCP_RL_INVALID_PROTOCOL_VERSION;
  119. break;
  120. }
  121. }
  122. else
  123. {
  124. if(PpsRecordLayer->wTlsVersionInfo != wInputProtVersion)
  125. {
  126. i4Status = (int32_t)OCP_RL_INVALID_PROTOCOL_VERSION;
  127. break;
  128. }
  129. }
  130. #undef PROTOCOL_VERSION_1_0
  131. #else
  132. if(PpsRecordLayer->wTlsVersionInfo != wInputProtVersion)
  133. {
  134. i4Status = (int32_t)OCP_RL_INVALID_PROTOCOL_VERSION;
  135. break;
  136. }
  137. #endif /*SUPPORT_OPENSSL*/
  138. wRecvFragLen = Utility_GetUint16(PpsBlobRecord->prgbStream + OFFSET_RL_FRAG_LENGTH);
  139. //Reset the flag for each record being being processed which indicates
  140. *PpsRecordLayer->pbDec = 0x00;
  141. if(CONTENTTYPE_CIPHER_SPEC == bContentType)
  142. {
  143. if((wRecvFragLen != 0x01) || ((PpsRecordLayer->wClientNextEpoch != (PpsRecordLayer->wServerEpoch+1))) || (wServerEpoch != 0x00)
  144. || (CONTENTTYPE_HANDSHAKE != PpsRecData->bContentType))
  145. {
  146. //Error since state has not changed on client side
  147. i4Status = (int32_t)OCP_RL_BAD_RECORD;
  148. }
  149. else
  150. {
  151. PpsRecData->bContentType = bContentType;
  152. *PpsRecordLayer->pbRecvCCSRecord = CCS_RECORD_RECV;
  153. Utility_Memmove(PpsRecData->psBlobInOutMsg->prgbStream, PpsBlobRecord->prgbStream + OFFSET_RL_FRAGMENT, \
  154. wRecvFragLen);
  155. PpsRecData->psBlobInOutMsg->wLen = wRecvFragLen;
  156. i4Status = OCP_RL_OK;
  157. }
  158. break;
  159. }
  160. else if((CONTENTTYPE_HANDSHAKE == PpsRecData->bContentType) && (CONTENTTYPE_APP_DATA == bContentType))
  161. {
  162. //Error, since content type is Application but no decryption
  163. i4Status = (int32_t)OCP_RL_BAD_RECORD;
  164. break;
  165. }
  166. //Is Decryption required?
  167. else if(((ENC_DEC_ENABLED & PpsRecordLayer->bEncDecFlag)== ENC_DEC_ENABLED) &&
  168. (wServerEpoch == PpsRecordLayer->wClientNextEpoch))
  169. {
  170. if(wRecvFragLen < MIN_DEC_PAYLOAD)
  171. {
  172. i4Status = (int32_t)OCP_RL_INVALID_DEC_PAYLOAD_LEN;
  173. break;
  174. }
  175. pbDecData = (uint8_t*)OCP_MALLOC(PpsBlobRecord->wLen + OVERHEAD_UPDOWNLINK);
  176. if(NULL == pbDecData)
  177. {
  178. i4Status = (int32_t)OCP_RL_MALLOC_FAILURE;
  179. break;
  180. }
  181. //Copy the data to be decrypted to a offset by OVERHEAD_UPDOWNLINK bytes
  182. Utility_Memmove((pbDecData + OVERHEAD_UPDOWNLINK), PpsBlobRecord->prgbStream, PpsBlobRecord->wLen);
  183. //Decrypt data
  184. sBlobCipherMsg.prgbStream = pbDecData;
  185. sBlobCipherMsg.wLen = PpsBlobRecord->wLen + OVERHEAD_UPDOWNLINK;
  186. sBlobPlainMsg.prgbStream = pbDecData;
  187. sBlobPlainMsg.wLen = sBlobCipherMsg.wLen;
  188. //Decrypt call back function
  189. i4Status = PpsRecordLayer->fEncDecRecord(PpsRecordLayer->pEncDecArgs, &sBlobCipherMsg, &sBlobPlainMsg, PpsBlobRecord->wLen);
  190. if(OCP_CL_OK == i4Status)
  191. {
  192. i4Status = OCP_RL_OK;
  193. PpsRecData->bContentType = bContentType;
  194. *PpsRecordLayer->pbDec = 0x01;
  195. //Remove the record header
  196. PpsRecData->psBlobInOutMsg->wLen = sBlobPlainMsg.wLen - LENGTH_RL_HEADER;
  197. Utility_Memmove(PpsRecData->psBlobInOutMsg->prgbStream, sBlobPlainMsg.prgbStream + LENGTH_RL_HEADER, \
  198. PpsRecData->psBlobInOutMsg->wLen);
  199. }
  200. OCP_FREE(pbDecData);
  201. break;
  202. }
  203. else
  204. {
  205. PpsRecData->bContentType = bContentType;
  206. //No Decryption, just copy the data
  207. Utility_Memmove(PpsRecData->psBlobInOutMsg->prgbStream, \
  208. PpsBlobRecord->prgbStream + OFFSET_RL_FRAGMENT, \
  209. wRecvFragLen);
  210. PpsRecData->psBlobInOutMsg->wLen = wRecvFragLen;
  211. i4Status = OCP_RL_OK;
  212. }
  213. }while(0);
  214. return i4Status;
  215. }
  216. /**
  217. *
  218. * Prepares a record and encrypts the fragment if PpsRecData.bEncDecFlag is set.<br>
  219. *
  220. * \param[in] PpsRecordLayer Pointer to #sRecordLayer_d structure
  221. * \param[in] PpsRecData Pointer to a blob containing processed record
  222. * \param[in,out] PpsBlobRecord Pointer to a blob containing record
  223. *
  224. * \retval #OCP_RL_OK Successful execution
  225. * \retval #OCP_RL_ERROR Failure in execution
  226. *
  227. */
  228. _STATIC_H int32_t DtlsRL_Record_PrepareRecord(sRecordLayer_d* PpsRecordLayer,const sRecordData_d* PpsRecData,sbBlob_d* PpsBlobRecord)
  229. {
  230. int32_t i4Status = OCP_RL_ERROR;
  231. sbBlob_d sBlobPlainMsg;
  232. sbBlob_d sBlobCipherMsg;
  233. sUint64 *psClientSeqNumber;
  234. uint16_t wClientEpoch = 0;
  235. do
  236. {
  237. //Check for zero len blobs
  238. if((0 == PpsBlobRecord->wLen)||(0 == PpsRecData->psBlobInOutMsg->wLen))
  239. {
  240. //Any error code
  241. i4Status = (int32_t)OCP_RL_INVALID_RECORD_LENGTH;
  242. break;
  243. }
  244. //Client as moved to new state(Change cipher spec is sent) and next state sequence number will be used
  245. if(PpsRecordLayer->wClientNextEpoch > PpsRecordLayer->wClientEpoch)
  246. {
  247. psClientSeqNumber = &PpsRecordLayer->sClientNextSeqNumber;
  248. wClientEpoch = PpsRecordLayer->wClientNextEpoch;
  249. }
  250. else if(PpsRecordLayer->wClientNextEpoch == PpsRecordLayer->wClientEpoch)
  251. {
  252. psClientSeqNumber = &PpsRecordLayer->sClientSeqNumber;
  253. wClientEpoch = PpsRecordLayer->wClientEpoch;
  254. }
  255. //Client Next epoch must not be less than current client epoch
  256. else
  257. {
  258. break;
  259. }
  260. //Add Content
  261. PpsBlobRecord->prgbStream[0] = PpsRecData->bContentType;
  262. //Add Protocol Version
  263. PpsBlobRecord->prgbStream[OFFSET_RL_PROT_VERSION] = (uint8_t)(PpsRecordLayer->wTlsVersionInfo >> 8);
  264. PpsBlobRecord->prgbStream[OFFSET_RL_PROT_VERSION+1] = (uint8_t)PpsRecordLayer->wTlsVersionInfo;
  265. //Add Epoch
  266. PpsBlobRecord->prgbStream[OFFSET_RL_EPOCH] = (uint8_t)(wClientEpoch >> 8);
  267. PpsBlobRecord->prgbStream[OFFSET_RL_EPOCH + 1] = (uint8_t)wClientEpoch;
  268. //Add Sequence Number.
  269. if((OFFSET_RL_SEQUENCE + LENGTH_RL_SEQUENCE)>PpsBlobRecord->wLen)
  270. {
  271. break;
  272. }
  273. /// @cond hidden
  274. #define MAX_HIGH_SEQ_NUM 0x00010000
  275. /// @endcond
  276. //Check for window overflow if equal max value
  277. if((MAX_HIGH_SEQ_NUM == psClientSeqNumber->dwHigherByte) && (0x00 == psClientSeqNumber->dwLowerByte))
  278. {
  279. i4Status = (int32_t) OCP_RL_SEQUENCE_OVERFLOW;
  280. break;
  281. }
  282. //GetUint64(&PpsRecordLayer->sClientSeqNumber,PpsBlobRecord->prgbStream+OFFSET_RL_SEQUENCE,LENGTH_RL_SEQUENCE);
  283. Utility_SetUint16(PpsBlobRecord->prgbStream+OFFSET_RL_SEQUENCE,(uint16_t)psClientSeqNumber->dwHigherByte);
  284. Utility_SetUint32(PpsBlobRecord->prgbStream+OFFSET_RL_SEQUENCE+2,psClientSeqNumber->dwLowerByte);
  285. //Add Final Length
  286. PpsBlobRecord->prgbStream[OFFSET_RL_FRAG_LENGTH] = (uint8_t)(PpsRecData->psBlobInOutMsg->wLen >> 8);
  287. PpsBlobRecord->prgbStream[OFFSET_RL_FRAG_LENGTH+1] = (uint8_t)PpsRecData->psBlobInOutMsg->wLen;
  288. //Is Encryption required?
  289. if((ENC_DEC_ENABLED&PpsRecordLayer->bEncDecFlag)== ENC_DEC_ENABLED)
  290. {
  291. //Move the formed Record header by command lib over head(20) number of bytes
  292. Utility_Memmove(PpsBlobRecord->prgbStream + OVERHEAD_UPDOWNLINK, PpsBlobRecord->prgbStream, LENGTH_RL_HEADER);
  293. //Copy the data to be encrypted
  294. Utility_Memmove(PpsBlobRecord->prgbStream + LENGTH_RL_HEADER + OVERHEAD_UPDOWNLINK,
  295. PpsRecData->psBlobInOutMsg->prgbStream, PpsRecData->psBlobInOutMsg->wLen);
  296. sBlobPlainMsg.prgbStream = PpsBlobRecord->prgbStream;
  297. sBlobPlainMsg.wLen = PpsRecData->psBlobInOutMsg->wLen + LENGTH_RL_HEADER + OVERHEAD_UPDOWNLINK;
  298. sBlobCipherMsg.prgbStream = PpsBlobRecord->prgbStream;
  299. sBlobCipherMsg.wLen = PpsBlobRecord->wLen;
  300. //Encrypt data
  301. i4Status = PpsRecordLayer->fEncDecRecord(PpsRecordLayer->pEncDecArgs, &sBlobPlainMsg, &sBlobCipherMsg,
  302. (PpsRecData->psBlobInOutMsg->wLen + LENGTH_RL_HEADER));
  303. if(OCP_CL_OK == i4Status)
  304. {
  305. i4Status = OCP_RL_OK;
  306. PpsBlobRecord->wLen = sBlobCipherMsg.wLen;
  307. }
  308. }
  309. else
  310. {
  311. if(CONTENTTYPE_CIPHER_SPEC == PpsRecData->bContentType)
  312. {
  313. PpsRecordLayer->wClientNextEpoch++ ;
  314. PpsRecordLayer->bEncDecFlag = 0xB5;
  315. }
  316. if(FALSE == PpsRecData->bMemoryAllocated)
  317. {
  318. //No encryption, just copy the data
  319. Utility_Memmove(PpsBlobRecord->prgbStream + OFFSET_RL_FRAGMENT,
  320. PpsRecData->psBlobInOutMsg->prgbStream,
  321. PpsRecData->psBlobInOutMsg->wLen);
  322. }
  323. PpsBlobRecord->wLen = PpsRecData->psBlobInOutMsg->wLen + LENGTH_RL_HEADER;
  324. i4Status = OCP_RL_OK;
  325. }
  326. //Update final length on success
  327. if(OCP_RL_OK != i4Status)
  328. {
  329. PpsBlobRecord->wLen = 0;
  330. break;
  331. }
  332. i4Status = IncrementUint64(psClientSeqNumber);
  333. if(UTIL_SUCCESS == i4Status)
  334. {
  335. i4Status = OCP_RL_OK;
  336. }
  337. }while(0);
  338. /// @cond hidden
  339. #undef RECORDSEQ_HIGH
  340. /// @endcond
  341. return i4Status;
  342. }
  343. /**
  344. * \brief Callback to validate the record.
  345. */
  346. _STATIC_H int32_t DtlsRL_CallBack_ValidateRec(const Void* PpvParams)
  347. {
  348. /// @cond hidden
  349. #define CB_PARAMS ((sCBValidateRec_d*)PpvParams)
  350. /// @endcond
  351. return DtlsRL_Record_ProcessRecord(CB_PARAMS->psRecordLayer,CB_PARAMS->psbBlob,CB_PARAMS->psRecordData);
  352. /// @cond hidden
  353. #undef CB_PARAMS
  354. /// @endcond
  355. }
  356. /**
  357. * \brief Callback to Change the server epoch state and reset the change cipher spec receive flag.
  358. */
  359. _STATIC_H Void DtlsRL_CB_ChangeServerState(const Void* PpCBParams)
  360. {
  361. /// @cond hidden
  362. #define S_RECORDLAYER ((sRecordLayer_d*)(((sCBStateTrn_d*)PpCBParams)->phRLHdl))
  363. /// @endcond
  364. S_RECORDLAYER->wServerEpoch++;
  365. *S_RECORDLAYER->pbRecvCCSRecord = CCS_RECORD_NOTRECV;
  366. /// @cond hidden
  367. #undef S_RECORDLAYER
  368. /// @endcond
  369. }
  370. /**
  371. * Gets the count of the total number of record in the buffer.<br>
  372. *
  373. * \param[in,out] PpbBuffer Pointer to buffer containing the records.
  374. * \param[in] PwLen Length of the buffer.
  375. * \param[in,out] PpbRecCount Pointer to record count value.
  376. *
  377. * \retval #OCP_RL_OK Successful execution
  378. * \retval #OCP_RL_ERROR Failure in execution
  379. *
  380. */
  381. _STATIC_H int32_t DtlsRL_GetRecordCount(uint8_t* PpbBuffer,uint16_t PwLen,uint8_t* PpbRecCount)
  382. {
  383. int32_t i4Status = OCP_RL_ERROR;
  384. uint8_t* pbRecPtr = PpbBuffer;
  385. uint16_t wRemainingLen = PwLen;
  386. uint16_t wRecLen = 0;
  387. *PpbRecCount = 0;
  388. do
  389. {
  390. //Check for remaining length
  391. if(wRemainingLen > LENGTH_RL_HEADER)
  392. {
  393. //Content type check
  394. if((*pbRecPtr != CONTENTTYPE_CIPHER_SPEC) &&
  395. (*pbRecPtr != CONTENTTYPE_ALERT) &&
  396. (*pbRecPtr != CONTENTTYPE_HANDSHAKE) &&
  397. (*pbRecPtr != CONTENTTYPE_APP_DATA))
  398. {
  399. break;
  400. }
  401. //Get the record length
  402. wRecLen = (uint32_t)Utility_GetUint16(pbRecPtr+OFFSET_RL_FRAG_LENGTH);
  403. if((wRecLen+LENGTH_RL_HEADER) > wRemainingLen)
  404. {
  405. break;
  406. }
  407. (*PpbRecCount)++;
  408. wRemainingLen -= (wRecLen + LENGTH_RL_HEADER);
  409. pbRecPtr += (wRecLen + LENGTH_RL_HEADER);
  410. i4Status = OCP_RL_OK;
  411. }
  412. else
  413. {
  414. i4Status = (int32_t)OCP_RL_OK;
  415. break;
  416. }
  417. }while(TRUE);
  418. if(0 == (*PpbRecCount))
  419. {
  420. i4Status = OCP_RL_ERROR;
  421. }
  422. return i4Status;
  423. }
  424. /**
  425. * Adds record header and sends the record over the transport layer.<br>
  426. * Based on the input provided in PpsRecordLayer->bMemoryAllocated,the function decides whether to allocate
  427. * memory for the record or not.
  428. * For internal handshake implementation, memory is already allocated by Handshake layer.
  429. * In case of Application layer, memory should be allocated here.
  430. *
  431. * \param[in] PpsRecordLayer Pointer to #sRecordLayer_d structure.
  432. * \param[in] PpbData Pointer to a Data to be sent.
  433. * \param[in] PwDataLen Pointer to length of data to be sent.
  434. *
  435. * \retval #OCP_RL_OK Successful execution
  436. * \retval #OCP_RL_ERROR Failure in execution
  437. *
  438. */
  439. int32_t DtlsRL_Send(sRL_d* PpsRecordLayer,uint8_t* PpbData,uint16_t PwDataLen)
  440. {
  441. int32_t i4Status = OCP_RL_ERROR;
  442. sRecordData_d sRecordData;
  443. uint8_t* pbTotalFragMem = NULL;
  444. uint8_t* pbEncData = NULL;
  445. sbBlob_d sBlobData;
  446. sbBlob_d sRecordBlobData;
  447. /// @cond hidden
  448. #define S_RECORDLAYER ((sRecordLayer_d*)(PpsRecordLayer->phRLHdl))
  449. /// @endcond
  450. do
  451. {
  452. //If Flight 5 is retransmitted.Reset the value to previous state
  453. if(PpsRecordLayer->fRetransmit == TRUE)
  454. {
  455. S_RECORDLAYER->wClientNextEpoch--;
  456. S_RECORDLAYER->bEncDecFlag = 0x00;
  457. PpsRecordLayer->fRetransmit = FALSE;
  458. }
  459. if(TRUE == PpsRecordLayer->bMemoryAllocated)
  460. {
  461. //In case of Handshake
  462. //Form struture, point to message Data
  463. sRecordData.bContentType = PpsRecordLayer->bContentType;
  464. sRecordData.psBlobInOutMsg = &sRecordBlobData;
  465. sRecordData.psBlobInOutMsg->prgbStream = PpbData+LENGTH_RL_HEADER;
  466. sRecordData.psBlobInOutMsg->wLen = PwDataLen-LENGTH_RL_HEADER;
  467. //Client and server are in the same state.Encryption is disabled
  468. if(S_RECORDLAYER->bEncDecFlag != ENC_DEC_ENABLED)
  469. {
  470. sBlobData.prgbStream = PpbData;
  471. sBlobData.wLen = PwDataLen;
  472. }
  473. }
  474. else
  475. {
  476. sRecordData.bContentType = PpsRecordLayer->bContentType;
  477. sRecordData.psBlobInOutMsg = &sRecordBlobData;
  478. sRecordData.psBlobInOutMsg->prgbStream = PpbData;
  479. sRecordData.psBlobInOutMsg->wLen = PwDataLen;
  480. if(S_RECORDLAYER->bEncDecFlag != ENC_DEC_ENABLED)
  481. {
  482. //Assign Buffer
  483. pbTotalFragMem = (uint8_t*)OCP_MALLOC(PwDataLen + LENGTH_RL_HEADER);
  484. if(NULL == pbTotalFragMem)
  485. {
  486. i4Status = (int32_t)OCP_RL_MALLOC_FAILURE;
  487. break;
  488. }
  489. sBlobData.prgbStream = pbTotalFragMem;
  490. sBlobData.wLen = PwDataLen + LENGTH_RL_HEADER;
  491. }
  492. }
  493. //Client as moved to new state and encryption is enabled.Sufficient memory is allocated to store the encrypted data
  494. if(S_RECORDLAYER->bEncDecFlag == ENC_DEC_ENABLED)
  495. {
  496. pbEncData = (uint8_t*)OCP_MALLOC(PwDataLen + LENGTH_RL_HEADER + MAC_LENGTH + EXPLICIT_NOUNCE_LENGTH + OVERHEAD_UPDOWNLINK);
  497. if(NULL == pbEncData)
  498. {
  499. i4Status = (int32_t)OCP_RL_MALLOC_FAILURE;
  500. break;
  501. }
  502. sBlobData.prgbStream = pbEncData;
  503. sBlobData.wLen = PwDataLen + LENGTH_RL_HEADER + MAC_LENGTH + EXPLICIT_NOUNCE_LENGTH + OVERHEAD_UPDOWNLINK;
  504. }
  505. //Assign function pointer for encryption
  506. S_RECORDLAYER->fEncDecRecord = PpsRecordLayer->psConfigCL->pfEncrypt;
  507. S_RECORDLAYER->pEncDecArgs = &(PpsRecordLayer->psConfigCL->sCL);
  508. //Add Record
  509. sRecordData.bMemoryAllocated = PpsRecordLayer->bMemoryAllocated;
  510. i4Status = DtlsRL_Record_PrepareRecord(S_RECORDLAYER,&sRecordData,&sBlobData);
  511. if(OCP_RL_OK != i4Status)
  512. {
  513. break;
  514. }
  515. //Send the data over transport layer
  516. i4Status = PpsRecordLayer->psConfigTL->pfSend(&(PpsRecordLayer->psConfigTL->sTL),
  517. sBlobData.prgbStream,sBlobData.wLen);
  518. if(OCP_TL_OK != i4Status)
  519. {
  520. break;
  521. }
  522. i4Status = (int32_t)OCP_RL_OK;
  523. }while(FALSE);
  524. if(FALSE == PpsRecordLayer->bMemoryAllocated)
  525. {
  526. OCP_FREE(pbTotalFragMem);
  527. }
  528. if(S_RECORDLAYER->bEncDecFlag == ENC_DEC_ENABLED)
  529. {
  530. OCP_FREE(pbEncData);
  531. }
  532. /// @cond hidden
  533. #undef S_RECORDLAYER
  534. /// @endcond
  535. return i4Status;
  536. }
  537. /**
  538. * To Slide the window to highest set sequence number.
  539. * If Higher bound reaches a value greater than maximum possible sequence number all the bits greater than
  540. * maximum sequence number is set to 1 in the window.
  541. *
  542. * \param[in,out] PpsRecordLayer Pointer to #sRL_d structure.
  543. * \param[in] PeAuthState Indicates the state of Mutual Authentication Public Key Scheme (DTLS)
  544. *
  545. * \retval #OCP_RL_OK Successful execution
  546. * \retval #OCP_RL_ERROR Failure in execution
  547. *
  548. */
  549. void Dtls_SlideWindow(const sRL_d* PpsRecordLayer, eAuthState_d PeAuthState)
  550. {
  551. int32_t i4Status = (int32_t)OCP_RL_OK;
  552. uint8_t bSlideCount = 0;
  553. uint8_t count = 0;
  554. uint8_t bLoopCount = 1;
  555. uint16_t wWordValue;
  556. uint32_t dwValue;
  557. sUint64 sBoundValue;
  558. sWindow_d *psWindow;
  559. /// @cond hidden
  560. #define PS_WINDOW ((sRecordLayer_d*)(PpsRecordLayer->phRLHdl))->psWindow
  561. #define PS_NEXTWINDOW ((sRecordLayer_d*)(PpsRecordLayer->phRLHdl))->psNextWindow
  562. /// @endcond
  563. do
  564. {
  565. /// @cond hidden
  566. #define MAX_LOW_SEQ_NUM 0xFFFFFFFF
  567. #define RECORDSEQ_HIGH 0xFFFF
  568. /// @endcond
  569. if(eAuthCompleted == PeAuthState)
  570. {
  571. bLoopCount = 2;
  572. }
  573. while(bLoopCount > 0)
  574. {
  575. if(bLoopCount > 1)
  576. {
  577. psWindow = PS_NEXTWINDOW;
  578. }
  579. else
  580. {
  581. psWindow = PS_WINDOW;
  582. }
  583. //If window size is 32 slide the higher word of the window
  584. if(32 == psWindow->bWindowSize)
  585. {
  586. dwValue = psWindow->sWindowFrame.dwHigherByte;
  587. //Check for highest bit which is set
  588. while(count < 32)
  589. {
  590. if(LEAST_SIGNIFICANT_BIT_HIGH == (LEAST_SIGNIFICANT_BIT_HIGH & dwValue))
  591. {
  592. break;
  593. }
  594. dwValue = dwValue >> 1 ;
  595. count++;
  596. }
  597. bSlideCount = psWindow->bWindowSize - count;
  598. }
  599. //If window size is greater then 32
  600. if(32 < psWindow->bWindowSize)
  601. {
  602. //If the value of lower word is greater than zero,than highest bit is set in lower double word
  603. if(psWindow->sWindowFrame.dwLowerByte > 0)
  604. {
  605. //If the value of lower double word is greater then 0xFFFF,then the highest bit is set in MSB 16 bits
  606. if(psWindow->sWindowFrame.dwLowerByte > 0xFFFF)
  607. {
  608. wWordValue = psWindow->sWindowFrame.dwLowerByte >> 16;
  609. }
  610. //If the value of lower double word is lesser then 0xFFFF,then the highest bit is set in LSB 16 bits
  611. else
  612. {
  613. bSlideCount+= 16;
  614. wWordValue = (uint16_t)psWindow->sWindowFrame.dwLowerByte;
  615. }
  616. while(count < 16)
  617. {
  618. if(0x0001 == (wWordValue & 0x0001))
  619. {
  620. break;
  621. }
  622. wWordValue = wWordValue >> 1;
  623. count++;
  624. }
  625. count = 16 - count;
  626. bSlideCount += count ;
  627. bSlideCount += (psWindow->bWindowSize - 32);
  628. }
  629. //If the value of lower word is lesser than or equal to zero,than highest bit is set in higher double word
  630. else
  631. {
  632. dwValue = psWindow->sWindowFrame.dwHigherByte;
  633. //Check for highest bit which is set
  634. while(count < 32)
  635. {
  636. if(LEAST_SIGNIFICANT_BIT_HIGH == (LEAST_SIGNIFICANT_BIT_HIGH & dwValue))
  637. {
  638. break;
  639. }
  640. dwValue = dwValue >> 1 ;
  641. count++;
  642. }
  643. bSlideCount = ((psWindow->bWindowSize - 32) - count);
  644. }
  645. }
  646. sBoundValue.dwHigherByte = 0x00;
  647. sBoundValue.dwLowerByte = bSlideCount;
  648. //Set the window lower bound to the new value
  649. i4Status = AddUint64(&psWindow->sLowerBound, &sBoundValue,&psWindow->sLowerBound);
  650. if(UTIL_SUCCESS != i4Status)
  651. {
  652. break;
  653. }
  654. //Set the window higher bound to the new value
  655. i4Status = AddUint64(&psWindow->sHigherBound, &sBoundValue,&psWindow->sHigherBound);
  656. if(UTIL_SUCCESS != i4Status)
  657. {
  658. break;
  659. }
  660. //Update the window
  661. psWindow->sWindowFrame.dwLowerByte = 0x00;
  662. psWindow->sWindowFrame.dwHigherByte = 0x00;
  663. //If the Higher bound is greater than maximum value of the sequence number
  664. if(psWindow->sHigherBound.dwHigherByte > (uint32_t)RECORDSEQ_HIGH)
  665. {
  666. sBoundValue.dwHigherByte = RECORDSEQ_HIGH;
  667. sBoundValue.dwLowerByte = MAX_LOW_SEQ_NUM;
  668. //Difference of Higher bound and max sequene number
  669. i4Status = SubtractUint64(&psWindow->sHigherBound, &sBoundValue, &sBoundValue);
  670. if(UTIL_SUCCESS != i4Status)
  671. {
  672. break;
  673. }
  674. //If 32 bit window
  675. if(32 == psWindow->bWindowSize)
  676. {
  677. psWindow->sWindowFrame.dwHigherByte = MAX_LOW_SEQ_NUM;
  678. dwValue = 32 - sBoundValue.dwLowerByte;
  679. while(dwValue--)
  680. {
  681. psWindow->sWindowFrame.dwHigherByte = psWindow->sWindowFrame.dwHigherByte >> 1;
  682. }
  683. }
  684. //If window is greater than 32
  685. else
  686. {
  687. psWindow->sWindowFrame.dwLowerByte = MAX_LOW_SEQ_NUM;
  688. if(sBoundValue.dwLowerByte > 32)
  689. {
  690. psWindow->sWindowFrame.dwHigherByte = MAX_LOW_SEQ_NUM;
  691. dwValue = psWindow->bWindowSize - sBoundValue.dwLowerByte;
  692. while(dwValue--)
  693. {
  694. psWindow->sWindowFrame.dwHigherByte = psWindow->sWindowFrame.dwHigherByte >> 1;
  695. }
  696. }
  697. else
  698. {
  699. dwValue = 32 - sBoundValue.dwLowerByte;
  700. while(dwValue--)
  701. {
  702. psWindow->sWindowFrame.dwLowerByte = psWindow->sWindowFrame.dwLowerByte >> 1;
  703. }
  704. }
  705. }
  706. }
  707. //Reset the count values
  708. count = 0;
  709. bSlideCount = 0;
  710. dwValue = 0;
  711. wWordValue = 0;
  712. bLoopCount--;
  713. }
  714. }while(FALSE);
  715. /// @cond hidden
  716. #undef PS_WINDOW
  717. #undef PS_NEXTWINDOW
  718. #undef MAX_SEQ_NUM
  719. #undef RECORDSEQ_HIGH
  720. /// @endcond
  721. }
  722. /**
  723. * Receives a record over transport layer, performs window check and remove the record header
  724. * before passing back the data.
  725. * The function also handles multiple record received in a single datagram by maintaining a count of
  726. * number of records received.
  727. * If the count is non-zero then a record is chosen from the given array, otherwise new record is received
  728. * from the transport layer.
  729. *
  730. * \param[in] PpsRecordLayer Pointer to #sRecordLayer_d structure.
  731. * \param[in,out] PpbBuffer Pointer to buffer to receive data.
  732. * \param[in,out] PpwLen Pointer to variable containing buffer size and later length of received data.
  733. *
  734. * \retval #OCP_RL_OK Successful execution
  735. * \retval #OCP_RL_ERROR Failure in execution
  736. *
  737. */
  738. int32_t DtlsRL_Recv(sRL_d* PpsRecordLayer,uint8_t* PpbBuffer,uint16_t* PpwLen)
  739. {
  740. int32_t i4Status = OCP_RL_ERROR;
  741. sbBlob_d sbBlobCBData;
  742. sCBValidateRec_d sCBValidateRec;
  743. sRecordData_d sRecData;
  744. sbBlob_d sInBlobData;
  745. sWindow_d *psWindow;
  746. uint16_t wServerEpoch;
  747. /// @cond hidden
  748. #define S_RECORDLAYER ((sRecordLayer_d*)(PpsRecordLayer->phRLHdl))
  749. /// @endcond
  750. do
  751. {
  752. //If all record not processed, do not call receive
  753. if(0 == PpsRecordLayer->bMultipleRecord)
  754. {
  755. //Receive Data over Transport
  756. i4Status = PpsRecordLayer->psConfigTL->pfRecv(&(PpsRecordLayer->psConfigTL->sTL),
  757. PpbBuffer,PpwLen);
  758. if((int32_t)OCP_TL_NO_DATA == i4Status)
  759. {
  760. i4Status = (int32_t)OCP_RL_NO_DATA;
  761. break;
  762. }
  763. if(OCP_TL_OK != i4Status)
  764. {
  765. break;
  766. }
  767. if(*PpwLen > (MAX_PMTU - UDP_OVERHEAD))
  768. {
  769. i4Status = (int32_t)OCP_RL_INVALID_RECORD_LENGTH;
  770. break;
  771. }
  772. //Check how many record are available
  773. i4Status = DtlsRL_GetRecordCount(PpbBuffer,*PpwLen,&(PpsRecordLayer->bMultipleRecord));
  774. if(OCP_RL_OK != i4Status)
  775. {
  776. break;
  777. }
  778. //Copy the received first record to call back input sBlob
  779. sbBlobCBData.prgbStream = PpbBuffer;
  780. sbBlobCBData.wLen = LENGTH_RL_HEADER;
  781. sbBlobCBData.wLen += Utility_GetUint16(sbBlobCBData.prgbStream + OFFSET_RL_FRAG_LENGTH);
  782. //Copy the location of the next record
  783. PpsRecordLayer->pNextRecord = (sbBlobCBData.prgbStream + sbBlobCBData.wLen);
  784. //Decrement the record count after a record is copied
  785. PpsRecordLayer->bMultipleRecord--;
  786. }
  787. else
  788. {
  789. //For multiple record
  790. ////Copy the received record to call back input sBlob
  791. sbBlobCBData.prgbStream = PpsRecordLayer->pNextRecord;
  792. sbBlobCBData.wLen = LENGTH_RL_HEADER;
  793. sbBlobCBData.wLen += Utility_GetUint16(sbBlobCBData.prgbStream + OFFSET_RL_FRAG_LENGTH);
  794. //Copy the location of the next successive record
  795. PpsRecordLayer->pNextRecord = (sbBlobCBData.prgbStream + sbBlobCBData.wLen);
  796. //Decrement the record count after a record is copied
  797. PpsRecordLayer->bMultipleRecord--;
  798. }
  799. //Assign function pointer for Decryption
  800. S_RECORDLAYER->fEncDecRecord = PpsRecordLayer->psConfigCL->pfDecrypt;
  801. S_RECORDLAYER->pEncDecArgs = &(PpsRecordLayer->psConfigCL->sCL);
  802. sCBValidateRec.psRecordLayer = S_RECORDLAYER;
  803. sCBValidateRec.psRecordData = &sRecData;
  804. //Get message from received record
  805. sCBValidateRec.psRecordData->bContentType = PpsRecordLayer->bContentType;
  806. sCBValidateRec.psRecordData->psBlobInOutMsg = &sInBlobData;
  807. sCBValidateRec.psRecordData->psBlobInOutMsg->prgbStream = PpbBuffer;
  808. sCBValidateRec.psRecordData->psBlobInOutMsg->wLen = *PpwLen;
  809. S_RECORDLAYER->sServerSeqNumber.dwHigherByte = (uint32_t)Utility_GetUint16 (sbBlobCBData.prgbStream + OFFSET_RL_SEQUENCE);
  810. S_RECORDLAYER->sServerSeqNumber.dwLowerByte = Utility_GetUint32 (sbBlobCBData.prgbStream + OFFSET_RL_SEQUENCE + 2);
  811. //Pass received Record
  812. sCBValidateRec.psbBlob = &sbBlobCBData;
  813. //Validate epoch
  814. //Ignore record,If received epoch less than current epoch or
  815. //received epoch is one greater than current epoch.
  816. wServerEpoch = Utility_GetUint16 (sbBlobCBData.prgbStream + OFFSET_RL_EPOCH);
  817. if((S_RECORDLAYER->wServerEpoch > wServerEpoch)||
  818. (1 + S_RECORDLAYER->wServerEpoch) < wServerEpoch)
  819. {
  820. i4Status = (int32_t)OCP_RL_INCORRECT_EPOCH;
  821. break;
  822. }
  823. //If epoch is one greater than current epoch and Server has not moved to new epoch
  824. if((1 == (wServerEpoch-S_RECORDLAYER->wServerEpoch)) && (S_RECORDLAYER->wClientNextEpoch != wServerEpoch))
  825. {
  826. i4Status = (int32_t)OCP_RL_INCORRECT_EPOCH;
  827. break;
  828. }
  829. //If the Client and server are in the same state use the current window for record sequence filtering
  830. if(wServerEpoch == S_RECORDLAYER->wClientEpoch)
  831. {
  832. psWindow = S_RECORDLAYER->psWindow;
  833. }
  834. //If Server as moved to the new state (Change cipher spec is already sent from server) use the new window for record sequence filtering
  835. else if(wServerEpoch == (S_RECORDLAYER->wClientEpoch + 1))
  836. {
  837. psWindow = S_RECORDLAYER->psNextWindow;
  838. }
  839. //Error if server epoch is less then client epoch
  840. else
  841. {
  842. i4Status = (int32_t)OCP_RL_INCORRECT_EPOCH;
  843. break;
  844. }
  845. psWindow->fValidateRecord = DtlsRL_CallBack_ValidateRec;
  846. psWindow->pValidateArgs = (Void*)&sCBValidateRec;
  847. psWindow->sRecvSeqNumber.dwHigherByte = S_RECORDLAYER->sServerSeqNumber.dwHigherByte;
  848. psWindow->sRecvSeqNumber.dwLowerByte = S_RECORDLAYER->sServerSeqNumber.dwLowerByte;
  849. i4Status = DtlsCheckReplay(psWindow);
  850. if(((int32_t)CMD_LIB_DECRYPT_FAILURE == i4Status) || ((int32_t)OCP_RL_MALLOC_FAILURE == i4Status))
  851. {
  852. break;
  853. }
  854. else if((int32_t)OCP_RL_WINDOW_IGNORE != i4Status)
  855. {
  856. *PpwLen = sCBValidateRec.psRecordData->psBlobInOutMsg->wLen;
  857. if( CONTENTTYPE_ALERT == sCBValidateRec.psRecordData->bContentType)
  858. {
  859. i4Status = (int32_t)OCP_RL_ALERT_RECEIVED;
  860. }
  861. else if(CONTENTTYPE_APP_DATA == sCBValidateRec.psRecordData->bContentType)
  862. {
  863. i4Status = (int32_t)OCP_RL_APPDATA_RECEIVED;
  864. }
  865. else
  866. {
  867. i4Status = (int32_t)OCP_RL_OK;
  868. }
  869. }
  870. else
  871. {
  872. i4Status = (int32_t)OCP_RL_ERROR;
  873. }
  874. //if window slide refresh buffer to removed old sequence number
  875. }while(FALSE);
  876. /// @cond hidden
  877. #undef S_RECORDLAYER
  878. /// @endcond
  879. return i4Status;
  880. }
  881. /**
  882. * Initialise the DTLS Record Layer
  883. * \param[in,out] PpsRL Pointer to #sRecordLayer_d structure.
  884. *
  885. * \retval #OCP_RL_OK Successful execution
  886. * \retval #OCP_RL_ERROR Failure in execution
  887. *
  888. */
  889. int32_t DtlsRL_Init(sRL_d* PpsRL)
  890. {
  891. int32_t i4Status = OCP_RL_ERROR;
  892. do
  893. {
  894. PpsRL->phRLHdl = (sRecordLayer_d*)OCP_MALLOC(sizeof(sRecordLayer_d));
  895. if(NULL == PpsRL->phRLHdl)
  896. {
  897. i4Status = (int32_t)OCP_RL_MALLOC_FAILURE;
  898. break;
  899. }
  900. memset(PpsRL->phRLHdl, 0x00, sizeof(sRecordLayer_d));
  901. /// @cond hidden
  902. #define S_RECORDLAYER ((sRecordLayer_d*)(PpsRL->phRLHdl))
  903. #define PS_WINDOW (S_RECORDLAYER->psWindow)
  904. #define PS_NEXTWINDOW (S_RECORDLAYER->psNextWindow)
  905. /// @endcond
  906. S_RECORDLAYER->psWindow = NULL;
  907. S_RECORDLAYER->psNextWindow = NULL;
  908. S_RECORDLAYER->bEncDecFlag = 0;
  909. S_RECORDLAYER->wClientEpoch = 0;
  910. S_RECORDLAYER->wClientNextEpoch = 0;
  911. PpsRL->bDecRecord = 0;
  912. PpsRL->bRecvCCSRecord = CCS_RECORD_NOTRECV;
  913. S_RECORDLAYER->pbDec = &PpsRL->bDecRecord;
  914. S_RECORDLAYER->pbRecvCCSRecord = &PpsRL->bRecvCCSRecord;
  915. PpsRL->fServerStateTrn = DtlsRL_CB_ChangeServerState;
  916. S_RECORDLAYER->wTlsVersionInfo = PROTOCOL_VERSION_1_2;//0xFE,0xFD
  917. PpsRL->fRetransmit = FALSE;
  918. PpsRL->bMultipleRecord = 0x00;
  919. S_RECORDLAYER->psWindow = (sWindow_d*)OCP_MALLOC(sizeof(sWindow_d));
  920. if(NULL == S_RECORDLAYER->psWindow)
  921. {
  922. i4Status = (int32_t)OCP_RL_MALLOC_FAILURE;
  923. break;
  924. }
  925. memset(S_RECORDLAYER->psWindow, 0x00, sizeof(sWindow_d));
  926. PS_WINDOW->bWindowSize = DEFAULT_WINDOW_SIZE;
  927. PS_WINDOW->sHigherBound.dwLowerByte = PS_WINDOW->bWindowSize - 1;
  928. PS_WINDOW->sHigherBound.dwHigherByte = 0x00;
  929. S_RECORDLAYER->psNextWindow = (sWindow_d*)OCP_MALLOC(sizeof(sWindow_d));
  930. if(NULL == S_RECORDLAYER->psNextWindow)
  931. {
  932. i4Status = (int32_t)OCP_RL_MALLOC_FAILURE;
  933. break;
  934. }
  935. memset(S_RECORDLAYER->psNextWindow, 0x00, sizeof(sWindow_d));
  936. PS_NEXTWINDOW->bWindowSize = DEFAULT_WINDOW_SIZE;
  937. PS_NEXTWINDOW->sHigherBound.dwLowerByte = PS_WINDOW->bWindowSize - 1;
  938. PS_NEXTWINDOW->sHigherBound.dwHigherByte = 0x00;
  939. PS_WINDOW->fValidateRecord = NULL;
  940. PS_WINDOW->pValidateArgs = NULL;
  941. i4Status = OCP_RL_OK;
  942. }while(FALSE);
  943. /// @cond hidden
  944. #undef S_RECORDLAYER
  945. #undef PS_WINDOW
  946. #undef PS_NEXTWINDOW
  947. /// @endcond
  948. return i4Status;
  949. }
  950. /**
  951. * This function frees memory held by dtls record layer
  952. *
  953. * \param[in] PpsRL Pointer to the record layer structure
  954. *
  955. * \return None
  956. */
  957. void DtlsRL_Close(sRL_d* PpsRL)
  958. {
  959. /// @cond hidden
  960. #define PS_WINDOW (((sRecordLayer_d*)PpsRL->phRLHdl)->psWindow)
  961. #define PS_NEXTWINDOW (((sRecordLayer_d*)PpsRL->phRLHdl)->psNextWindow)
  962. /// @endcond
  963. //NULL check
  964. if(NULL != PpsRL)
  965. {
  966. if(NULL != PpsRL->phRLHdl)
  967. {
  968. if(NULL != PS_WINDOW)
  969. {
  970. //Free the allocated memory for sWindow_d structure
  971. OCP_FREE(PS_WINDOW);
  972. if(NULL != PS_NEXTWINDOW)
  973. {
  974. OCP_FREE(PS_NEXTWINDOW);
  975. PS_NEXTWINDOW = NULL;
  976. }
  977. PS_WINDOW = NULL;
  978. }
  979. //Free the allocated memory record handle
  980. OCP_FREE(PpsRL->phRLHdl);
  981. PpsRL->phRLHdl = NULL;
  982. }
  983. }
  984. /// @cond hidden
  985. #undef PS_WINDOW
  986. #undef PS_NEXTWINDOW
  987. /// @endcond
  988. }
  989. /**
  990. * @}
  991. */
  992. #endif /*MODULE_ENABLE_DTLS_MUTUAL_AUTH */