ua_transport_binary_secure.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #include <stdio.h>
  2. #include <memory.h> // memcpy
  3. #include "ua_transport_binary.h"
  4. #include "ua_transport_binary_secure.h"
  5. #include "ua_transport.h"
  6. #include "ua_statuscodes.h"
  7. #include "ua_services.h"
  8. #include "ua_stack_session_manager.h"
  9. #include "ua_stack_session.h"
  10. #define SIZE_SECURECHANNEL_HEADER 12
  11. #define SIZE_SEQHEADER_HEADER 8
  12. static UA_Int32 SL_Send(SL_Channel *channel,
  13. const UA_ByteString * responseMessage, UA_Int32 type)
  14. {
  15. UA_UInt32 pos = 0;
  16. UA_Int32 isAsym = (type == UA_OPENSECURECHANNELRESPONSE_NS0); // FIXME: this is a to dumb method to determine asymmetric algorithm setting
  17. UA_UInt32 channelId;
  18. UA_UInt32 sequenceNumber;
  19. UA_UInt32 requestId;
  20. UA_NodeId resp_nodeid;
  21. UA_TL_Connection *connection;
  22. UA_AsymmetricAlgorithmSecurityHeader *asymAlgSettings = UA_NULL;
  23. resp_nodeid.nodeIdType = UA_NODEIDTYPE_NUMERIC;
  24. resp_nodeid.namespace = 0;
  25. resp_nodeid.identifier.numeric = type + 2; // binary encoding
  26. const UA_ByteString *response_gather[2]; // securechannel_header, seq_header, security_encryption_header, message_length (eventually + padding + size_signature);
  27. UA_alloc((void ** )&response_gather[0], sizeof(UA_ByteString));
  28. if (isAsym) {
  29. SL_Channel_getLocalAsymAlgSettings(channel, &asymAlgSettings);
  30. UA_ByteString_newMembers((UA_ByteString *) response_gather[0],
  31. SIZE_SECURECHANNEL_HEADER + SIZE_SEQHEADER_HEADER
  32. + UA_AsymmetricAlgorithmSecurityHeader_calcSizeBinary(
  33. asymAlgSettings)
  34. + UA_NodeId_calcSizeBinary(&resp_nodeid));
  35. }
  36. else
  37. {
  38. UA_ByteString_newMembers((UA_ByteString *) response_gather[0], 8 + 16 + // normal header + 4*32bit secure channel information
  39. UA_NodeId_calcSizeBinary(&resp_nodeid));
  40. }
  41. // sizePadding = 0;
  42. // sizeSignature = 0;
  43. UA_ByteString *header = (UA_ByteString *) response_gather[0];
  44. /*---encode Secure Conversation Message Header ---*/
  45. if (isAsym)
  46. {
  47. header->data[0] = 'O';
  48. header->data[1] = 'P';
  49. header->data[2] = 'N';
  50. }
  51. else
  52. {
  53. header->data[0] = 'M';
  54. header->data[1] = 'S';
  55. header->data[2] = 'G';
  56. }
  57. pos += 3;
  58. header->data[pos] = 'F';
  59. pos += 1;
  60. UA_Int32 packetSize = response_gather[0]->length + responseMessage->length;
  61. UA_Int32_encodeBinary(&packetSize, header,&pos);
  62. //use get accessor to read the channel Id
  63. SL_Channel_getChannelId(channel, &channelId);
  64. UA_UInt32_encodeBinary(&channelId, header,&pos);
  65. /*---encode Algorithm Security Header ---*/
  66. if (isAsym)
  67. {
  68. UA_AsymmetricAlgorithmSecurityHeader_encodeBinary(asymAlgSettings,
  69. header,&pos);
  70. UA_free(asymAlgSettings);
  71. }
  72. else
  73. {
  74. UA_UInt32 tokenId = 0;
  75. SL_Channel_getTokenId(channel, &tokenId);
  76. UA_UInt32_encodeBinary(&tokenId, header,&pos);
  77. }
  78. /*---encode Sequence Header ---*/
  79. SL_Channel_getSequenceNumber(channel, &sequenceNumber);
  80. UA_UInt32_encodeBinary(&sequenceNumber, header, &pos);
  81. SL_Channel_getRequestId(channel, &requestId);
  82. UA_UInt32_encodeBinary(&requestId, header,&pos);
  83. /*---add payload type---*/
  84. UA_NodeId_encodeBinary(&resp_nodeid, header,&pos);
  85. /*---add encoded Message ---*/
  86. response_gather[1] = responseMessage; // is deleted in the calling function
  87. /* sign Data*/
  88. /* encrypt Data*/
  89. /* send Data */
  90. SL_Channel_getConnection(channel, &connection);
  91. TL_Send(connection, response_gather, 2);
  92. UA_ByteString_delete((UA_ByteString *) response_gather[0]);
  93. return UA_SUCCESS;
  94. }
  95. static void init_response_header(UA_RequestHeader const * p,
  96. UA_ResponseHeader * r)
  97. {
  98. memset((void*) r, 0, sizeof(UA_ResponseHeader));
  99. r->requestHandle = p->requestHandle;
  100. r->serviceResult = UA_STATUSCODE_GOOD;
  101. r->stringTableSize = 0;
  102. r->timestamp = UA_DateTime_now();
  103. }
  104. #define RESPONSE_PREPARE(TYPE) \
  105. UA_##TYPE##Request p; \
  106. UA_##TYPE##Response r; \
  107. UA_Session *session = UA_NULL; \
  108. UA_##TYPE##Request_decodeBinary(msg, &recvOffset, &p); \
  109. UA_##TYPE##Response_init(&r); \
  110. init_response_header(&p.requestHeader, &r.responseHeader); \
  111. UA_SessionManager_getSessionByToken(&p.requestHeader.authenticationToken, &session); \
  112. DBG_VERBOSE(printf("Invoke Service: %s\n", #TYPE)); \
  113. #define INVOKE_SERVICE(TYPE) \
  114. UA_##TYPE##Request p; \
  115. UA_##TYPE##Response r; \
  116. UA_Session session = UA_NULL; \
  117. UA_##TYPE##Request_decodeBinary(msg, &recvOffset, &p); \
  118. UA_##TYPE##Response_init(&r); \
  119. init_response_header(&p.requestHeader, &r.responseHeader); \
  120. UA_SessionManager_getSessionByToken(&p.requestHeader.authenticationToken, &session); \
  121. DBG_VERBOSE(printf("Invoke Service: %s\n", #TYPE)); \
  122. Service_##TYPE(session, &p, &r); \
  123. DBG_VERBOSE(printf("Finished Service: %s\n", #TYPE)); \
  124. sendOffset = 0; \
  125. UA_ByteString_newMembers(&response_msg, UA_##TYPE##Response_calcSizeBinary(&r)); \
  126. UA_##TYPE##Response_encodeBinary(&r, &sendOffset, &response_msg); \
  127. UA_##TYPE##Request_deleteMembers(&p); \
  128. UA_##TYPE##Response_deleteMembers(&r); \
  129. /*
  130. #define INVOKE_SERVICE(TYPE) \
  131. DBG_VERBOSE(printf("Invoke Service: %s\n", #TYPE)); \
  132. Service_##TYPE(session, &p, &r); \
  133. DBG_VERBOSE(printf("Finished Service: %s\n", #TYPE)); \
  134. */
  135. #define RESPONSE_CLEANUP(TYPE) \
  136. DBG_VERBOSE(printf("Finished Service: %s\n", #TYPE)); \
  137. sendOffset = 0; \
  138. UA_ByteString_newMembers(&response_msg, UA_##TYPE##Response_calcSizeBinary(&r)); \
  139. UA_##TYPE##Response_encodeBinary(&r, &response_msg,&sendOffset); \
  140. UA_##TYPE##Request_deleteMembers(&p); \
  141. UA_##TYPE##Response_deleteMembers(&r); \
  142. UA_Int32 SL_handleRequest(SL_Channel *channel, const UA_ByteString* msg,
  143. UA_UInt32 *pos)
  144. {
  145. UA_Int32 retval = UA_SUCCESS;
  146. UA_UInt32 recvOffset = *pos;
  147. UA_UInt32 sendOffset = 0;
  148. // Every Message starts with a NodeID which names the serviceRequestType
  149. UA_NodeId serviceRequestType;
  150. UA_NodeId_decodeBinary(msg, &recvOffset,&serviceRequestType);
  151. #ifdef DEBUG
  152. UA_NodeId_printf("SL_processMessage - serviceRequestType=",
  153. &serviceRequestType);
  154. #endif
  155. UA_ByteString response_msg;
  156. UA_Int32 serviceid = serviceRequestType.identifier.numeric - 2; // binary encoding has 2 added to the id
  157. UA_Int32 responsetype;
  158. /* stack related services which only need information about the secure Channel */
  159. if (serviceid == UA_GETENDPOINTSREQUEST_NS0)
  160. {
  161. RESPONSE_PREPARE(GetEndpoints);
  162. Service_GetEndpoints(channel,&p, &r);
  163. RESPONSE_CLEANUP(GetEndpoints);
  164. //INVOKE_SERVICE(GetEndpoints);
  165. responsetype = UA_GETENDPOINTSRESPONSE_NS0;
  166. }
  167. else if (serviceid == UA_OPENSECURECHANNELREQUEST_NS0)
  168. {
  169. RESPONSE_PREPARE(OpenSecureChannel);
  170. Service_OpenSecureChannel(channel,&p, &r);
  171. RESPONSE_CLEANUP(OpenSecureChannel);
  172. responsetype = UA_OPENSECURECHANNELRESPONSE_NS0;
  173. }
  174. else if (serviceid == UA_CLOSESECURECHANNELREQUEST_NS0)
  175. {
  176. RESPONSE_PREPARE(CloseSecureChannel);
  177. Service_CloseSecureChannel(channel,&p,&r);
  178. RESPONSE_CLEANUP(CloseSecureChannel);
  179. responsetype = UA_CLOSESECURECHANNELRESPONSE_NS0;
  180. }
  181. else if (serviceid == UA_CREATESESSIONREQUEST_NS0)
  182. {
  183. RESPONSE_PREPARE(CreateSession);
  184. Service_CreateSession(channel,&p, &r);
  185. RESPONSE_CLEANUP(CreateSession);
  186. responsetype = UA_CREATESESSIONRESPONSE_NS0;
  187. }
  188. /* services which need a session object */
  189. else if (serviceid == UA_ACTIVATESESSIONREQUEST_NS0)
  190. {
  191. RESPONSE_PREPARE(ActivateSession);
  192. UA_Session_updateLifetime(session); //renew session timeout
  193. Service_ActivateSession(channel, session,&p, &r);
  194. RESPONSE_CLEANUP(ActivateSession);
  195. responsetype = UA_ACTIVATESESSIONRESPONSE_NS0;
  196. }
  197. else if (serviceid == UA_CLOSESESSIONREQUEST_NS0)
  198. {
  199. RESPONSE_PREPARE(CloseSession);
  200. if (UA_Session_verifyChannel(session,channel)){
  201. UA_Session_updateLifetime(session); //renew session timeout
  202. Service_CloseSession(session,&p, &r);
  203. RESPONSE_CLEANUP(CloseSession);
  204. }
  205. else
  206. {
  207. DBG_VERBOSE(printf("session does not match secure channel"));
  208. }
  209. responsetype = UA_CLOSESESSIONRESPONSE_NS0;
  210. }
  211. else if (serviceid == UA_READREQUEST_NS0)
  212. {
  213. RESPONSE_PREPARE(Read);
  214. UA_Session_updateLifetime(session); //renew session timeout
  215. DBG_VERBOSE(printf("Finished Service: %s\n", Read));
  216. if (UA_Session_verifyChannel(session,channel)){
  217. UA_Session_updateLifetime(session); //renew session timeout
  218. Service_Read(session,&p, &r);
  219. }
  220. else
  221. {
  222. DBG_VERBOSE(printf("session does not match secure channel"));
  223. }
  224. DBG_VERBOSE(printf("Finished Service: %s\n", Read));
  225. RESPONSE_CLEANUP(Read);
  226. responsetype = UA_READRESPONSE_NS0;
  227. }
  228. else if (serviceid == UA_WRITEREQUEST_NS0)
  229. {
  230. RESPONSE_PREPARE(Write);
  231. DBG_VERBOSE(printf("Finished Service: %s\n", Write));
  232. if (UA_Session_verifyChannel(session,channel)){
  233. UA_Session_updateLifetime(session); //renew session timeout
  234. Service_Write(session,&p, &r);
  235. }
  236. else
  237. {
  238. DBG_VERBOSE(printf("session does not match secure channel"));
  239. }
  240. DBG_VERBOSE(printf("Finished Service: %s\n", Write));
  241. RESPONSE_CLEANUP(Write);
  242. responsetype = UA_WRITERESPONSE_NS0;
  243. }
  244. else if (serviceid == UA_BROWSEREQUEST_NS0)
  245. {
  246. RESPONSE_PREPARE(Browse);
  247. DBG_VERBOSE(printf("Finished Service: %s\n", Browse));
  248. if (UA_Session_verifyChannel(session,channel)){
  249. Service_Browse(session,&p, &r);
  250. }
  251. else
  252. {
  253. DBG_VERBOSE(printf("session does not match secure channel"));
  254. }
  255. DBG_VERBOSE(printf("Finished Service: %s\n", Browse));
  256. RESPONSE_CLEANUP(Browse);
  257. responsetype = UA_BROWSERESPONSE_NS0;
  258. }
  259. else if (serviceid == UA_CREATESUBSCRIPTIONREQUEST_NS0)
  260. {
  261. RESPONSE_PREPARE(CreateSubscription);
  262. DBG_VERBOSE(printf("Finished Service: %s\n", CreateSubscription));
  263. if (UA_Session_verifyChannel(session,channel)){
  264. Service_CreateSubscription(session, &p, &r);
  265. }
  266. else
  267. {
  268. DBG_VERBOSE(printf("session does not match secure channel"));
  269. }
  270. DBG_VERBOSE(printf("Finished Service: %s\n", CreateSubscription));
  271. RESPONSE_CLEANUP(CreateSubscription);
  272. responsetype = UA_CREATESUBSCRIPTIONRESPONSE_NS0;
  273. }
  274. else if (serviceid == UA_TRANSLATEBROWSEPATHSTONODEIDSREQUEST_NS0)
  275. {
  276. RESPONSE_PREPARE(TranslateBrowsePathsToNodeIds);
  277. DBG_VERBOSE(printf("Finished Service: %s\n", TranslateBrowsePathsToNodeIds));
  278. if (UA_Session_verifyChannel(session,channel)){
  279. Service_TranslateBrowsePathsToNodeIds(session, &p, &r);
  280. }
  281. else
  282. {
  283. DBG_VERBOSE(printf("session does not match secure channel"));
  284. }
  285. DBG_VERBOSE(printf("Finished Service: %s\n", TranslateBrowsePathsToNodeIds));
  286. RESPONSE_CLEANUP(TranslateBrowsePathsToNodeIds);
  287. responsetype = UA_TRANSLATEBROWSEPATHSTONODEIDSRESPONSE_NS0;
  288. }
  289. else if (serviceid == UA_PUBLISHREQUEST_NS0)
  290. {
  291. RESPONSE_PREPARE(Publish);
  292. DBG_VERBOSE(printf("Finished Service: %s\n", Publish));
  293. if (UA_Session_verifyChannel(session,channel)){
  294. Service_Publish(session, &p, &r);
  295. }
  296. else
  297. {
  298. DBG_VERBOSE(printf("session does not match secure channel"));
  299. }
  300. DBG_VERBOSE(printf("Finished Service: %s\n", Publish));
  301. RESPONSE_CLEANUP(Publish);
  302. responsetype = UA_PUBLISHRESPONSE_NS0;
  303. }
  304. else if (serviceid == UA_CREATEMONITOREDITEMSREQUEST_NS0)
  305. {
  306. RESPONSE_PREPARE(CreateMonitoredItems);
  307. DBG_VERBOSE(printf("Finished Service: %s\n", CreateMonitoredItems));
  308. if (UA_Session_verifyChannel(session,channel)){
  309. Service_CreateMonitoredItems(session, &p, &r);
  310. }
  311. else
  312. {
  313. DBG_VERBOSE(printf("session does not match secure channel"));
  314. }
  315. DBG_VERBOSE(printf("Finished Service: %s\n", CreateMonitoredItems));
  316. RESPONSE_CLEANUP(CreateMonitoredItems);
  317. responsetype = UA_CREATEMONITOREDITEMSRESPONSE_NS0;
  318. }
  319. else if (serviceid == UA_SETPUBLISHINGMODEREQUEST_NS0)
  320. {
  321. RESPONSE_PREPARE(SetPublishingMode);
  322. DBG_VERBOSE(printf("Finished Service: %s\n",SetPublishingMode));
  323. if (UA_Session_verifyChannel(session,channel)){
  324. Service_SetPublishingMode(session, &p, &r);
  325. }
  326. else
  327. {
  328. DBG_VERBOSE(printf("session does not match secure channel"));
  329. }
  330. DBG_VERBOSE(printf("Finished Service: %s\n", SetPublishingMode));
  331. RESPONSE_CLEANUP(SetPublishingMode);
  332. responsetype = UA_SETPUBLISHINGMODERESPONSE_NS0;
  333. }
  334. else
  335. {
  336. printf(
  337. "SL_processMessage - unknown request, namespace=%d, request=%d\n",
  338. serviceRequestType.namespace,
  339. serviceRequestType.identifier.numeric);
  340. retval = UA_ERROR;
  341. UA_RequestHeader p;
  342. UA_ResponseHeader r;
  343. UA_RequestHeader_decodeBinary(msg, &recvOffset, &p);
  344. UA_ResponseHeader_init(&r);
  345. r.requestHandle = p.requestHandle;
  346. r.serviceResult = UA_STATUSCODE_BADSERVICEUNSUPPORTED;
  347. sendOffset = 0;
  348. UA_ByteString_newMembers(&response_msg, UA_ResponseHeader_calcSizeBinary(&r));
  349. UA_ResponseHeader_encodeBinary(&r, &response_msg,&sendOffset);
  350. responsetype = UA_RESPONSEHEADER_NS0;
  351. }
  352. if(serviceid != UA_CLOSESECURECHANNELREQUEST_NS0){
  353. SL_Send(channel, &response_msg, responsetype);
  354. }
  355. UA_ByteString_deleteMembers(&response_msg);
  356. *pos = recvOffset;
  357. return retval;
  358. }
  359. UA_Int32 SL_ProcessOpenChannel(SL_Channel *channel, const UA_ByteString* msg,
  360. UA_UInt32 *pos)
  361. {
  362. UA_Int32 retval = UA_SUCCESS;
  363. UA_SequenceHeader sequenceHeader;
  364. UA_AsymmetricAlgorithmSecurityHeader asymHeader;
  365. UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(msg,pos,&asymHeader);
  366. UA_SequenceHeader_decodeBinary(msg,pos,&sequenceHeader);
  367. //init remote security settings from the security header
  368. SL_Channel_setRemoteSecuritySettings(channel,&asymHeader,&sequenceHeader);
  369. return SL_handleRequest(channel, msg, pos) | retval;
  370. }
  371. /* not used anymore */
  372. //UA_Int32 SL_ProcessCloseChannel(SL_Channel *channel, const UA_ByteString* msg,
  373. // UA_UInt32 *pos)
  374. //{
  375. // return SL_handleRequest(channel, msg, pos);
  376. //}
  377. UA_Int32 SL_Process(const UA_ByteString* msg,
  378. UA_UInt32* pos)
  379. {
  380. DBG_VERBOSE(printf("SL_process - entered \n"));
  381. UA_UInt32 secureChannelId;
  382. UA_UInt32 foundChannelId;
  383. SL_Channel *channel;
  384. UA_SequenceHeader sequenceHeader;
  385. UA_UInt32_decodeBinary(msg, pos, &secureChannelId);
  386. //FIXME: we assume SAS, need to check if AAS or SAS
  387. UA_SymmetricAlgorithmSecurityHeader symAlgSecHeader;
  388. // if (connection->securityMode == UA_MESSAGESECURITYMODE_NONE) {
  389. UA_SymmetricAlgorithmSecurityHeader_decodeBinary(msg, pos,
  390. &symAlgSecHeader);
  391. if (SL_ChannelManager_getChannel(secureChannelId,
  392. &channel) == UA_SUCCESS)
  393. {
  394. SL_Channel_getChannelId(channel, &foundChannelId);
  395. printf("SL_process - received msg, with channel id: %i \n",
  396. foundChannelId);
  397. //sequence number processing
  398. UA_SequenceHeader_decodeBinary(msg, pos,
  399. &sequenceHeader);
  400. SL_Channel_checkSequenceNumber(channel,sequenceHeader.sequenceNumber);
  401. SL_Channel_checkRequestId(channel,sequenceHeader.requestId);
  402. //request id processing
  403. SL_handleRequest(channel, msg, pos);
  404. }
  405. else
  406. {
  407. printf("SL_process - ERROR could not find channel with id: %i \n",
  408. secureChannelId);
  409. //TODO generate ERROR_Bad_SecureChannelUnkown
  410. }
  411. return UA_SUCCESS;
  412. }