opcua_secureChannelLayer.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. * opcua_secureChannelLayer.c
  3. *
  4. * Created on: Jan 13, 2014
  5. * Author: opcua
  6. */
  7. #include <stdio.h>
  8. #include <memory.h> // memcpy
  9. #include "opcua_secureChannelLayer.h"
  10. #define SIZE_SECURECHANNEL_HEADER 12
  11. #define SIZE_SEQHEADER_HEADER 8
  12. /*
  13. * inits a connection object for secure channel layer
  14. */
  15. UA_Int32 SL_initConnectionObject(UA_connection *connection)
  16. {
  17. //TODO: fill with valid information
  18. connection->secureLayer.localAsymAlgSettings.ReceiverCertificateThumbprint.data = NULL;
  19. connection->secureLayer.localAsymAlgSettings.ReceiverCertificateThumbprint.length = 0;
  20. connection->secureLayer.localAsymAlgSettings.SecurityPolicyUri.data = "http://opcfoundation.org/UA/SecurityPolicy#None";
  21. connection->secureLayer.localAsymAlgSettings.SecurityPolicyUri.length = 47;
  22. connection->secureLayer.localAsymAlgSettings.SenderCertificate.data = NULL;
  23. connection->secureLayer.localAsymAlgSettings.SenderCertificate.length = 0;
  24. connection->secureLayer.remoteNonce.data = NULL;
  25. connection->secureLayer.remoteNonce.length = 0;
  26. UA_alloc((void**)&(connection->secureLayer.localNonce.data),sizeof(UA_Byte));
  27. connection->secureLayer.localNonce.length = 1;
  28. connection->secureLayer.connectionState = connectionState_CLOSED;
  29. connection->secureLayer.requestId = 0;
  30. connection->secureLayer.requestType = 0;
  31. connection->secureLayer.secureChannelId.data = NULL;
  32. connection->secureLayer.secureChannelId.length = 0;
  33. connection->secureLayer.securityMode = UA_SecurityMode_INVALID;
  34. //TODO set a valid start secureChannelId number
  35. connection->secureLayer.securityToken.secureChannelId = 25;
  36. //TODO set a valid start TokenId
  37. connection->secureLayer.securityToken.tokenId = 1;
  38. connection->secureLayer.sequenceNumber = 1;
  39. return UA_NO_ERROR;
  40. }
  41. UA_Int32 SL_send(UA_connection *connection, UA_ByteString responseMessage, UA_Int32 type)
  42. {
  43. UA_UInt32 sequenceNumber;
  44. UA_UInt32 requestId;
  45. UA_Int32 pos;
  46. UA_Int32 sizeAsymAlgHeader;
  47. UA_ByteString responsePacket;
  48. UA_Int32 packetSize;
  49. UA_Int32 sizePadding;
  50. UA_Int32 sizeSignature;
  51. sizeAsymAlgHeader = 3 * sizeof(UA_UInt32) +
  52. connection->secureLayer.localAsymAlgSettings.SecurityPolicyUri.length +
  53. connection->secureLayer.localAsymAlgSettings.SenderCertificate.length +
  54. connection->secureLayer.localAsymAlgSettings.ReceiverCertificateThumbprint.length;
  55. pos = 0;
  56. //sequence header
  57. sequenceNumber = connection->secureLayer.sequenceNumber;
  58. requestId = connection->secureLayer.requestId;
  59. if(type == 449) //openSecureChannelResponse -> asymmetric algorithm
  60. {
  61. //TODO fill with valid sizes
  62. sizePadding = 0;
  63. sizeSignature = 0;
  64. //TODO: size calculation need
  65. packetSize = SIZE_SECURECHANNEL_HEADER +
  66. SIZE_SEQHEADER_HEADER +
  67. sizeAsymAlgHeader +
  68. responseMessage.length +
  69. sizePadding +
  70. sizeSignature;
  71. //get memory for response
  72. UA_alloc((void**)&(responsePacket.data),packetSize);
  73. responsePacket.length = packetSize;
  74. /*---encode Secure Conversation Message Header ---*/
  75. //encode MessageType - OPN message
  76. responsePacket.data[0] = 'O';
  77. responsePacket.data[1] = 'P';
  78. responsePacket.data[2] = 'N';
  79. pos += 3;
  80. //encode Chunk Type - set to final
  81. responsePacket.data[3] = 'F';
  82. pos += 1;
  83. UA_Int32_encode(&packetSize,&pos,responsePacket.data);
  84. UA_Int32_encode(&(connection->secureLayer.securityToken.secureChannelId),&pos,responsePacket.data);
  85. /*---encode Asymmetric Algorithm Header ---*/
  86. UA_ByteString_encode(&(connection->secureLayer.localAsymAlgSettings.SecurityPolicyUri),
  87. &pos,responsePacket.data);
  88. UA_ByteString_encode(&(connection->secureLayer.localAsymAlgSettings.SenderCertificate),
  89. &pos,responsePacket.data );
  90. UA_ByteString_encode(&(connection->secureLayer.localAsymAlgSettings.ReceiverCertificateThumbprint),
  91. &pos,responsePacket.data );
  92. }
  93. /*---encode Sequence Header ---*/
  94. UA_UInt32_encode(&sequenceNumber,&pos,responsePacket.data);
  95. UA_UInt32_encode(&requestId,&pos,responsePacket.data);
  96. /*---add encoded Message ---*/
  97. memcpy(&(responsePacket.data[pos]), responseMessage.data, responseMessage.length);
  98. /* sign Data*/
  99. /* encrypt Data */
  100. /* send Data */
  101. TL_send(connection,&responsePacket);
  102. return UA_NO_ERROR;
  103. }
  104. /*
  105. * opens a secure channel
  106. */
  107. UA_Int32 SL_openSecureChannel(UA_connection *connection,
  108. UA_RequestHeader *requestHeader,
  109. UA_StatusCode serviceResult)
  110. {
  111. UA_ResponseHeader responseHeader;
  112. UA_ExtensionObject additionalHeader;
  113. SL_ChannelSecurityToken securityToken;
  114. UA_ByteString serverNonce;
  115. UA_NodeId responseType;
  116. //sizes for memory allocation
  117. UA_Int32 sizeResponse;
  118. UA_Int32 sizeRespHeader;
  119. UA_Int32 sizeResponseType;
  120. UA_Int32 sizeRespMessage;
  121. UA_Int32 sizeSecurityToken;
  122. UA_ByteString response;
  123. UA_UInt32 serverProtocolVersion;
  124. UA_Int32 pos;
  125. UA_DiagnosticInfo serviceDiagnostics;
  126. if(requestHeader->returnDiagnostics != 0)
  127. {
  128. printf("SL_openSecureChannel - diagnostics demanded by the client\n");
  129. printf("SL_openSecureChannel - retrieving diagnostics not implemented!\n");
  130. //TODO fill with demanded information part 4, 7.8 - Table 123
  131. serviceDiagnostics.encodingMask = 0;
  132. }
  133. else
  134. {
  135. serviceDiagnostics.encodingMask = 0;
  136. }
  137. /*--------------type ----------------------*/
  138. //Four Bytes Encoding
  139. responseType.encodingByte = UA_NodeIdType_FourByte;
  140. //openSecureChannelResponse = 449
  141. responseType.identifier.numeric = 449;
  142. responseType.namespace = 0;
  143. /*--------------responseHeader-------------*/
  144. /* Res-1) ResponseHeader responseHeader
  145. * timestamp UtcTime
  146. * requestHandle IntegerId
  147. * serviceResult StatusCode
  148. * serviceDiagnostics DiagnosticInfo
  149. * stringTable[] String
  150. * addtionalHeader Extensible Parameter
  151. */
  152. //current time
  153. responseHeader.timestamp = UA_DateTime_now();
  154. //request Handle which client sent
  155. responseHeader.requestHandle = requestHeader->requestHandle;
  156. // StatusCode which informs client about quality of response
  157. responseHeader.serviceResult = serviceResult;
  158. //retrieve diagnosticInfo if client demands
  159. responseHeader.serviceDiagnostics = &serviceDiagnostics;
  160. //text of fields defined in the serviceDiagnostics
  161. responseHeader.stringTableSize = 0;
  162. responseHeader.stringTable = NULL;
  163. // no additional header
  164. additionalHeader.encoding = 0;
  165. additionalHeader.body.data = NULL;
  166. additionalHeader.body.length = 0;
  167. additionalHeader.typeId.encodingByte = 0;
  168. additionalHeader.typeId.namespace = 0;
  169. additionalHeader.typeId.identifier.numeric = 0;
  170. responseHeader.additionalHeader = &additionalHeader;
  171. printf("SL_openSecureChannel - built response header\n");
  172. //calculate the size
  173. sizeRespHeader = UA_ResponseHeader_calcSize(&responseHeader);
  174. printf("SL_openSecureChannel - size response header =%d\n",sizeRespHeader);
  175. /*--------------responseMessage-------------*/
  176. /* Res-2) UInt32 ServerProtocolVersion
  177. * Res-3) SecurityToken channelSecurityToken
  178. * Res-5) ByteString ServerNonce
  179. */
  180. // secureChannelId + TokenId + CreatedAt + RevisedLifetime
  181. sizeSecurityToken = sizeof(UA_UInt32) + sizeof(UA_UInt32) + sizeof(UA_DateTime) + sizeof(UA_Int32);
  182. //ignore server nonce
  183. serverNonce.length = -1;
  184. serverNonce.data = NULL;
  185. serverNonce.length = connection->secureLayer.localNonce.length;
  186. serverNonce.data = connection->secureLayer.localNonce.data;
  187. //fill token structure with default server information
  188. securityToken.secureChannelId = connection->secureLayer.securityToken.secureChannelId;
  189. securityToken.tokenId = connection->secureLayer.securityToken.tokenId;
  190. securityToken.createdAt = UA_DateTime_now();
  191. securityToken.revisedLifetime = connection->secureLayer.securityToken.revisedLifetime;
  192. serverProtocolVersion = connection->transportLayer.localConf.protocolVersion;
  193. // ProtocolVersion + SecurityToken + Nonce
  194. sizeRespMessage = sizeof(UA_UInt32) + serverNonce.length + sizeof(UA_Int32) + sizeSecurityToken;
  195. printf("SL_openSecureChannel - size of response message=%d\n",sizeRespMessage);
  196. //get memory for response
  197. sizeResponseType = UA_NodeId_calcSize(&responseType);
  198. response.length = sizeResponseType + sizeRespHeader + sizeRespMessage;
  199. //get memory for response
  200. UA_alloc(&(response.data), UA_NodeId_calcSize(&responseType) + sizeRespHeader + sizeRespMessage);
  201. pos = 0;
  202. //encode responseType (NodeId)
  203. UA_NodeId_printf("SL_openSecureChannel - TypeId =",&responseType);
  204. UA_NodeId_encode(&responseType, &pos, response.data);
  205. //encode header
  206. printf("SL_openSecureChannel - encoding response header \n");
  207. UA_ResponseHeader_encode(&responseHeader, &pos, &response);
  208. printf("SL_openSecureChannel - response header encoded \n");
  209. //encode message
  210. printf("SL_openSecureChannel - serverProtocolVersion = %d \n",serverProtocolVersion);
  211. UA_UInt32_encode(&serverProtocolVersion, &pos,response.data);
  212. printf("SL_openSecureChannel - secureChannelId = %d \n",securityToken.secureChannelId);
  213. UA_UInt32_encode(&(securityToken.secureChannelId), &pos,response.data);
  214. printf("SL_openSecureChannel - tokenId = %d \n",securityToken.tokenId);
  215. UA_Int32_encode(&(securityToken.tokenId), &pos,response.data);
  216. UA_DateTime_encode(&(securityToken.createdAt), &pos,response.data);
  217. printf("SL_openSecureChannel - revisedLifetime = %d \n",securityToken.revisedLifetime);
  218. UA_Int32_encode(&(securityToken.revisedLifetime), &pos,response.data);
  219. UA_ByteString_encode(&serverNonce, &pos,response.data);
  220. printf("SL_openSecureChannel - response.length = %d \n",response.length);
  221. //449 = openSecureChannelResponse
  222. SL_send(connection, response, 449);
  223. return UA_SUCCESS;
  224. }
  225. /*
  226. Int32 SL_openSecureChannel_responseMessage_calcSize(SL_Response *response,
  227. Int32* sizeInOut) {
  228. Int32 length = 0;
  229. length += sizeof(response->SecurityToken);
  230. length += UAString_calcSize(response->ServerNonce);
  231. length += sizeof(response->ServerProtocolVersion);
  232. return length;
  233. }
  234. */
  235. /*
  236. * closes a secureChannel (server side)
  237. */
  238. void SL_secureChannel_close(UA_connection *connection) {
  239. }
  240. UA_Int32 SL_check(UA_connection *connection, UA_ByteString secureChannelPacket) {
  241. return UA_NO_ERROR;
  242. }
  243. UA_Int32 SL_createSecurityToken(UA_connection *connection, UA_Int32 lifeTime) {
  244. return UA_NO_ERROR;
  245. }
  246. UA_Int32 SL_processMessage(UA_connection *connection, UA_ByteString message) {
  247. UA_DiagnosticInfo serviceDiagnostics;
  248. UA_Int32 pos = 0;
  249. UA_RequestHeader requestHeader;
  250. UA_UInt32 clientProtocolVersion;
  251. UA_NodeId serviceRequestType;
  252. UA_Int32 requestType;
  253. UA_Int32 securityMode;
  254. UA_Int32 requestedLifetime;
  255. UA_ByteString clientNonce;
  256. UA_StatusCode serviceResult;
  257. // Every Message starts with a NodeID which names the serviceRequestType
  258. UA_NodeId_decode(message.data, &pos,
  259. &serviceRequestType);
  260. UA_NodeId_printf("SL_processMessage - serviceRequestType=",
  261. &serviceRequestType);
  262. if (serviceRequestType.encodingByte == UA_NodeIdType_FourByte
  263. && serviceRequestType.identifier.numeric == 446) {
  264. /* OpenSecureChannelService, defined in 62541-6 §6.4.4, Table 34.
  265. * Note that part 6 adds ClientProtocolVersion and ServerProtocolVersion
  266. * to the definition in part 4 */
  267. // Req-1) RequestHeader requestHeader
  268. UA_RequestHeader requestHeader;
  269. // Req-2) UInt32 ClientProtocolVersion
  270. UA_UInt32 clientProtocolVersion;
  271. // Req-3) Enum SecurityTokenRequestType requestType
  272. UA_Int32 requestType;
  273. // Req-4) Enum MessageSecurityMode SecurityMode
  274. UA_Int32 securityMode;
  275. // Req-5) ByteString ClientNonce
  276. UA_ByteString clientNonce;
  277. // Req-6) Int32 RequestLifetime
  278. UA_Int32 requestedLifetime;
  279. UA_ByteString_printx("SL_processMessage - message=", &message);
  280. // Req-1) RequestHeader requestHeader
  281. UA_RequestHeader_decode(message.data, &pos, &requestHeader);
  282. UA_String_printf("SL_processMessage - requestHeader.auditEntryId=",
  283. &requestHeader.auditEntryId);
  284. UA_NodeId_printf(
  285. "SL_processMessage - requestHeader.authenticationToken=",
  286. &requestHeader.authenticationToken);
  287. // Req-2) UInt32 ClientProtocolVersion
  288. UA_UInt32_decode(message.data, &pos,
  289. &clientProtocolVersion);
  290. printf("SL_processMessage - clientProtocolVersion=%d\n",
  291. clientProtocolVersion);
  292. if (clientProtocolVersion
  293. != connection->transportLayer.remoteConf.protocolVersion) {
  294. printf("SL_processMessage - error protocol version \n");
  295. //TODO error protocol version
  296. //TODO ERROR_Bad_ProtocolVersionUnsupported
  297. }
  298. // Req-3) SecurityTokenRequestType requestType
  299. UA_Int32_decode(message.data, &pos, &requestType);
  300. printf("SL_processMessage - requestType=%d\n", requestType);
  301. switch (requestType) {
  302. case securityToken_ISSUE:
  303. if (connection->secureLayer.connectionState
  304. == connectionState_ESTABLISHED) {
  305. printf("SL_processMessage - multiply security token request");
  306. //TODO return ERROR
  307. return UA_ERROR;
  308. }
  309. printf(
  310. "SL_processMessage - TODO: create new token for a new SecureChannel\n");
  311. // SL_createNewToken(connection);
  312. break;
  313. case securityToken_RENEW:
  314. if (connection->secureLayer.connectionState
  315. == connectionState_CLOSED) {
  316. printf(
  317. "SL_processMessage - renew token request received, but no secureChannel was established before");
  318. //TODO return ERROR
  319. return UA_ERROR;
  320. }
  321. printf("TODO: create new token for an existing SecureChannel\n");
  322. break;
  323. }
  324. // Req-4) MessageSecurityMode SecurityMode
  325. UA_UInt32_decode(message.data, &pos, &securityMode);
  326. printf("SL_processMessage - securityMode=%d\n", securityMode);
  327. switch (securityMode) {
  328. case UA_SecurityMode_INVALID:
  329. connection->secureLayer.remoteNonce.data = NULL;
  330. connection->secureLayer.remoteNonce.length = 0;
  331. printf("SL_processMessage - client demands no security \n");
  332. break;
  333. case UA_SecurityMode_SIGN:
  334. printf("SL_processMessage - client demands signed \n");
  335. //TODO check if senderCertificate and ReceiverCertificateThumbprint are present
  336. break;
  337. case UA_SecurityMode_SIGNANDENCRYPT:
  338. printf("SL_processMessage - client demands signed & encrypted \n");
  339. //TODO check if senderCertificate and ReceiverCertificateThumbprint are present
  340. break;
  341. }
  342. // Req-5) ByteString ClientNonce
  343. UA_ByteString_decode(message.data, &pos,
  344. &clientNonce);
  345. UA_ByteString_printf("SL_processMessage - clientNonce=", &clientNonce);
  346. // Req-6) Int32 RequestLifetime
  347. UA_Int32_decode(message.data, &pos,
  348. &requestedLifetime);
  349. printf("SL_processMessage - requestedLifeTime=%d\n", requestedLifetime);
  350. //TODO process requestedLifetime
  351. // 62541-4 §7.27 "The requestHandle given by the Client to the request."
  352. return SL_openSecureChannel(connection, &requestHeader, SC_Good);
  353. } else {
  354. printf("SL_processMessage - unknown service request");
  355. //TODO change error code
  356. return UA_ERROR;
  357. }
  358. return UA_SUCCESS;
  359. }
  360. /*
  361. * receive and process data from underlying layer
  362. */
  363. void SL_receive(UA_connection *connection, UA_ByteString *serviceMessage) {
  364. UA_ByteString secureChannelPacket;
  365. UA_ByteString message;
  366. SL_SecureConversationMessageHeader SCM_Header;
  367. SL_AsymmetricAlgorithmSecurityHeader AAS_Header;
  368. SL_SequenceHeader SequenceHeader;
  369. UA_Int32 packetType = 0;
  370. UA_Int32 pos = 0;
  371. UA_Int32 iTmp;
  372. //TODO Error Handling, length checking
  373. //get data from transport layer
  374. printf("SL_receive - entered \n");
  375. TL_receive(connection, &secureChannelPacket);
  376. if (secureChannelPacket.length > 0 && secureChannelPacket.data != NULL) {
  377. printf("SL_receive - data received \n");
  378. packetType = TL_getPacketType(&secureChannelPacket, &pos);
  379. decodeSCMHeader(&secureChannelPacket, &pos, &SCM_Header);
  380. switch (SCM_Header.MessageType) {
  381. case packetType_OPN: /* openSecureChannel Message received */
  382. decodeAASHeader(&secureChannelPacket, &pos, &AAS_Header);
  383. UA_String_printf("SL_receive - AAS_Header.ReceiverThumbprint=",
  384. &(AAS_Header.ReceiverThumbprint));
  385. UA_String_printf("SL_receive - AAS_Header.SecurityPolicyUri=",
  386. &(AAS_Header.SecurityPolicyUri));
  387. UA_ByteString_printf("SL_receive - AAS_Header.SenderCertificate=",
  388. &(AAS_Header.SenderCertificate));
  389. if (SCM_Header.SecureChannelId != 0) {
  390. iTmp = UA_ByteString_compare(
  391. &(connection->secureLayer.remoteAsymAlgSettings.SenderCertificate),
  392. &(AAS_Header.SenderCertificate));
  393. if (iTmp != UA_EQUAL) {
  394. printf("SL_receive - UA_ERROR_BadSecureChannelUnknown \n");
  395. //TODO return UA_ERROR_BadSecureChannelUnknown
  396. }
  397. }
  398. else
  399. {
  400. //TODO invalid securechannelId
  401. }
  402. decodeSequenceHeader(&secureChannelPacket, &pos, &SequenceHeader);
  403. printf("SL_receive - SequenceHeader.RequestId=%d\n",
  404. SequenceHeader.RequestId);
  405. printf("SL_receive - SequenceHeader.SequenceNr=%d\n",
  406. SequenceHeader.SequenceNumber);
  407. //save request id to return it to client
  408. connection->secureLayer.requestId = SequenceHeader.RequestId;
  409. //TODO check that the sequence number is smaller than MaxUInt32 - 1024
  410. connection->secureLayer.sequenceNumber =
  411. SequenceHeader.SequenceNumber;
  412. //SL_decrypt(&secureChannelPacket);
  413. message.data = &secureChannelPacket.data[pos];
  414. message.length = secureChannelPacket.length - pos;
  415. SL_processMessage(connection, message);
  416. break;
  417. case packetType_MSG: /* secure Channel Message received */
  418. if (connection->secureLayer.connectionState
  419. == connectionState_ESTABLISHED) {
  420. if (SCM_Header.SecureChannelId
  421. == connection->secureLayer.securityToken.secureChannelId) {
  422. } else {
  423. //TODO generate ERROR_Bad_SecureChannelUnkown
  424. }
  425. }
  426. break;
  427. case packetType_CLO: /* closeSecureChannel Message received */
  428. if (SL_check(connection, secureChannelPacket) == UA_NO_ERROR) {
  429. }
  430. break;
  431. }
  432. } else {
  433. printf("SL_receive - no data received \n");
  434. }
  435. /*
  436. Int32 readPosition = 0;
  437. //get the Secure Channel Message Header
  438. decodeSCMHeader(secureChannelPacket,
  439. &readPosition, &SCM_Header);
  440. //get the Secure Channel Asymmetric Algorithm Security Header
  441. decodeAASHeader(secureChannelPacket,
  442. &readPosition, &AAS_Header);
  443. //get the Sequence Header
  444. decodeSequenceHeader(secureChannelPacket,
  445. &readPosition, &SequenceHeader);
  446. //get Secure Channel Message
  447. //SL_secureChannel_Message_get(connection, secureChannelPacket,
  448. // &readPosition,serviceMessage);
  449. if (secureChannelPacket->length > 0)
  450. {
  451. switch (SCM_Header.MessageType)
  452. {
  453. case packetType_MSG:
  454. if (connection->secureLayer.connectionState
  455. == connectionState_ESTABLISHED)
  456. {
  457. }
  458. else //receiving message, without secure channel
  459. {
  460. //TODO send back Error Message
  461. }
  462. break;
  463. case packetType_OPN:
  464. //Server Handling
  465. // if (openSecureChannelHeader_check(connection, secureChannelPacket))
  466. // {
  467. //check if the request is valid
  468. // SL_openSecureChannelRequest_check(connection, secureChannelPacket);
  469. // }
  470. // else
  471. // {
  472. // //TODO send back Error Message
  473. // }
  474. //Client Handling
  475. //TODO free memory for secureChannelPacket
  476. break;
  477. case packetType_CLO:
  478. //TODO free memory for secureChannelPacket
  479. break;
  480. }
  481. }
  482. */
  483. }
  484. /*
  485. * get the secure channel message header
  486. */
  487. UA_Int32 decodeSCMHeader(UA_ByteString *rawMessage, UA_Int32 *pos,
  488. SL_SecureConversationMessageHeader* SC_Header) {
  489. UA_UInt32 err;
  490. printf("decodeSCMHeader - entered \n");
  491. // LU: wild guess - reset pos, we want to reread the message type again
  492. *pos = 0;
  493. SC_Header->MessageType = TL_getPacketType(rawMessage, pos);
  494. SC_Header->IsFinal = rawMessage->data[*pos];
  495. *pos += 1;
  496. UA_UInt32_decode(rawMessage->data, pos, &(SC_Header->MessageSize));
  497. UA_UInt32_decode(rawMessage->data, pos, &(SC_Header->SecureChannelId));
  498. return UA_SUCCESS;
  499. }
  500. /*
  501. Int32 encodeSCMHeader(SL_SecureConversationMessageHeader *SC_Header, Int32 *pos,
  502. AD_RawMessage *rawMessage) {
  503. const char *type = "ERR";
  504. switch (SC_Header->MessageType) {
  505. case packetType_ACK:
  506. type = "ACK";
  507. break;
  508. case packetType_CLO:
  509. type = "CLO";
  510. break;
  511. case packetType_ERR:
  512. type = "ERR";
  513. break;
  514. case packetType_HEL:
  515. type = "HEL";
  516. break;
  517. case packetType_MSG:
  518. type = "MSG";
  519. break;
  520. case packetType_OPN:
  521. type = "OPN";
  522. break;
  523. default:
  524. return UA_ERROR;
  525. }
  526. memcpy(&(rawMessage->message[*pos]), &type, 3);
  527. return UA_NO_ERROR;
  528. }
  529. */
  530. UA_Int32 decodeSequenceHeader(UA_ByteString *rawMessage, UA_Int32 *pos,
  531. SL_SequenceHeader *SequenceHeader) {
  532. UA_UInt32_decode(rawMessage->data, pos, &(SequenceHeader->SequenceNumber));
  533. UA_UInt32_decode(rawMessage->data, pos, &(SequenceHeader->RequestId));
  534. return UA_SUCCESS;
  535. }
  536. /*
  537. * get the asymmetric algorithm security header
  538. */
  539. UA_Int32 decodeAASHeader(UA_ByteString *rawMessage, UA_Int32 *pos,
  540. SL_AsymmetricAlgorithmSecurityHeader* AAS_Header) {
  541. UA_Int32 err = 0;
  542. err |= UA_String_decode(rawMessage->data, pos,
  543. &(AAS_Header->SecurityPolicyUri));
  544. err |= UA_ByteString_decode(rawMessage->data, pos,
  545. &(AAS_Header->SenderCertificate));
  546. err |= UA_String_decode(rawMessage->data, pos,
  547. &(AAS_Header->ReceiverThumbprint));
  548. return err;
  549. }
  550. UA_Int32 encodeAASHeader(SL_AsymmetricAlgorithmSecurityHeader *AAS_Header,
  551. UA_Int32 *pos, UA_ByteString* dstRawMessage) {
  552. UA_String_encode(&(AAS_Header->SecurityPolicyUri), pos,
  553. dstRawMessage->data);
  554. UA_ByteString_encode(&(AAS_Header->SenderCertificate), pos,
  555. dstRawMessage->data);
  556. UA_String_encode(&(AAS_Header->ReceiverThumbprint), pos,
  557. dstRawMessage->data);
  558. return UA_SUCCESS;
  559. }