opcua_secureChannelLayer.c 21 KB

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