ua_transport_binary_secure.c 14 KB

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