opcua_secureChannelLayer.c 21 KB

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