ua_transport_binary_secure.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include <stdio.h>
  2. #include <memory.h> // memcpy
  3. #include "opcua.h"
  4. #include "ua_transport_binary.h"
  5. #include "ua_transport_binary_secure.h"
  6. #include "ua_transport.h"
  7. #include "ua_statuscodes.h"
  8. #include "ua_services.h"
  9. #define SIZE_SECURECHANNEL_HEADER 12
  10. #define SIZE_SEQHEADER_HEADER 8
  11. SL_Channel slc;
  12. static UA_Int32 SL_Send(SL_Channel* channel, const UA_ByteString * responseMessage, UA_Int32 type) {
  13. UA_Int32 pos = 0;
  14. UA_Int32 isAsym = (type == UA_OPENSECURECHANNELRESPONSE_NS0); // FIXME: this is a to dumb method to determine asymmetric algorithm setting
  15. UA_NodeId resp_nodeid;
  16. resp_nodeid.encodingByte = UA_NODEIDTYPE_FOURBYTE;
  17. resp_nodeid.namespace = 0;
  18. resp_nodeid.identifier.numeric = type+2; // binary encoding
  19. const UA_ByteString *response_gather[2]; // securechannel_header, seq_header, security_encryption_header, message_length (eventually + padding + size_signature);
  20. UA_alloc((void **)&response_gather[0], sizeof(UA_ByteString));
  21. if(isAsym) {
  22. UA_ByteString_newMembers((UA_ByteString *)response_gather[0], SIZE_SECURECHANNEL_HEADER + SIZE_SEQHEADER_HEADER +
  23. UA_AsymmetricAlgorithmSecurityHeader_calcSize(&channel->localAsymAlgSettings) +
  24. UA_NodeId_calcSize(&resp_nodeid));
  25. }
  26. else {
  27. UA_ByteString_newMembers((UA_ByteString *)response_gather[0], 8 + 16 + // normal header + 4*32bit secure channel information
  28. UA_NodeId_calcSize(&resp_nodeid));
  29. }
  30. // sizePadding = 0;
  31. // sizeSignature = 0;
  32. UA_ByteString *header = (UA_ByteString *)response_gather[0];
  33. /*---encode Secure Conversation Message Header ---*/
  34. if (isAsym) {
  35. header->data[0] = 'O';
  36. header->data[1] = 'P';
  37. header->data[2] = 'N';
  38. } else {
  39. header->data[0] = 'M';
  40. header->data[1] = 'S';
  41. header->data[2] = 'G';
  42. }
  43. pos += 3;
  44. header->data[pos] = 'F';
  45. pos += 1;
  46. UA_Int32 packetSize = response_gather[0]->length + responseMessage->length;
  47. UA_Int32_encodeBinary(&packetSize, &pos, header);
  48. UA_UInt32_encodeBinary(&channel->securityToken.secureChannelId, &pos, header);
  49. /*---encode Algorithm Security Header ---*/
  50. if (isAsym) {
  51. UA_AsymmetricAlgorithmSecurityHeader_encodeBinary(&channel->localAsymAlgSettings, &pos, header);
  52. } else {
  53. UA_SymmetricAlgorithmSecurityHeader_encodeBinary(&channel->securityToken.tokenId, &pos, header);
  54. }
  55. /*---encode Sequence Header ---*/
  56. UA_UInt32_encodeBinary(&channel->sequenceHeader.sequenceNumber, &pos, header);
  57. UA_UInt32_encodeBinary(&channel->sequenceHeader.requestId, &pos, header);
  58. /*---add payload type---*/
  59. UA_NodeId_encodeBinary(&resp_nodeid, &pos, header);
  60. /*---add encoded Message ---*/
  61. response_gather[1] = responseMessage; // is deleted in the calling function
  62. /* sign Data*/
  63. /* encrypt Data*/
  64. /* send Data */
  65. TL_Send(channel->tlConnection, response_gather, 2);
  66. UA_ByteString_delete((UA_ByteString *)response_gather[0]);
  67. return UA_SUCCESS;
  68. }
  69. static void init_response_header(UA_RequestHeader const * p, UA_ResponseHeader * r) {
  70. memset((void*) r, 0, sizeof(UA_ResponseHeader));
  71. r->requestHandle = p->requestHandle;
  72. r->serviceResult = UA_STATUSCODE_GOOD;
  73. r->stringTableSize = 0;
  74. r->timestamp = UA_DateTime_now();
  75. }
  76. #define INVOKE_SERVICE(TYPE) \
  77. UA_##TYPE##Request p; \
  78. UA_##TYPE##Response r; \
  79. UA_##TYPE##Request_decodeBinary(msg, pos, &p); \
  80. UA_##TYPE##Response_init(&r); \
  81. init_response_header(&p.requestHeader, &r.responseHeader); \
  82. DBG_VERBOSE(printf("Invoke Service: %s\n", #TYPE)); \
  83. Service_##TYPE(channel, &p, &r); \
  84. DBG_VERBOSE(printf("Finished Service: %s\n", #TYPE)); \
  85. *pos = 0; \
  86. UA_ByteString_newMembers(&response_msg, UA_##TYPE##Response_calcSize(&r)); \
  87. UA_##TYPE##Response_encodeBinary(&r, pos, &response_msg); \
  88. UA_##TYPE##Request_deleteMembers(&p); \
  89. UA_##TYPE##Response_deleteMembers(&r); \
  90. /** this function manages all the generic stuff for the request-response game */
  91. UA_Int32 SL_handleRequest(SL_Channel *channel, const UA_ByteString* msg, UA_Int32 *pos) {
  92. UA_Int32 retval = UA_SUCCESS;
  93. // Every Message starts with a NodeID which names the serviceRequestType
  94. UA_NodeId serviceRequestType;
  95. UA_NodeId_decodeBinary(msg, pos, &serviceRequestType);
  96. UA_NodeId_printf("SL_processMessage - serviceRequestType=", &serviceRequestType);
  97. UA_ByteString response_msg;
  98. int serviceid = serviceRequestType.identifier.numeric-2; // binary encoding has 2 added to the id
  99. UA_Int32 responsetype;
  100. if(serviceid == UA_GETENDPOINTSREQUEST_NS0) {
  101. INVOKE_SERVICE(GetEndpoints);
  102. responsetype = UA_GETENDPOINTSRESPONSE_NS0;
  103. }
  104. else if(serviceid == UA_OPENSECURECHANNELREQUEST_NS0) {
  105. INVOKE_SERVICE(OpenSecureChannel);
  106. responsetype = UA_OPENSECURECHANNELRESPONSE_NS0;
  107. }
  108. else if(serviceid == UA_CLOSESECURECHANNELREQUEST_NS0) {
  109. INVOKE_SERVICE(CloseSecureChannel);
  110. responsetype = UA_CLOSESECURECHANNELRESPONSE_NS0;
  111. }
  112. else if(serviceid == UA_CREATESESSIONREQUEST_NS0) {
  113. INVOKE_SERVICE(CreateSession);
  114. responsetype = UA_CREATESESSIONRESPONSE_NS0;
  115. }
  116. else if(serviceid == UA_ACTIVATESESSIONREQUEST_NS0) {
  117. INVOKE_SERVICE(ActivateSession);
  118. responsetype = UA_ACTIVATESESSIONRESPONSE_NS0;
  119. }
  120. else if(serviceid == UA_CLOSESESSIONREQUEST_NS0) {
  121. INVOKE_SERVICE(CloseSession);
  122. responsetype = UA_CLOSESESSIONRESPONSE_NS0;
  123. }
  124. else if(serviceid == UA_READREQUEST_NS0) {
  125. INVOKE_SERVICE(Read);
  126. responsetype = UA_READRESPONSE_NS0;
  127. }
  128. else {
  129. printf("SL_processMessage - unknown request, namespace=%d, request=%d\n", serviceRequestType.namespace,serviceRequestType.identifier.numeric);
  130. retval = UA_ERROR;
  131. UA_RequestHeader p;
  132. UA_ResponseHeader r;
  133. UA_RequestHeader_decodeBinary(msg,pos,&p);
  134. UA_ResponseHeader_init(&r);
  135. r.requestHandle = p.requestHandle;
  136. r.serviceResult = UA_STATUSCODE_BADSERVICEUNSUPPORTED;
  137. *pos = 0;
  138. UA_ByteString_newMembers(&response_msg, UA_ResponseHeader_calcSize(&r));
  139. UA_ResponseHeader_encodeBinary(&r, pos, &response_msg);
  140. responsetype = UA_RESPONSEHEADER_NS0;
  141. }
  142. SL_Send(channel, &response_msg, responsetype);
  143. UA_ByteString_deleteMembers(&response_msg);
  144. return retval;
  145. }
  146. /**
  147. *
  148. * @param connection
  149. * @param msg
  150. * @param pos
  151. * @return
  152. */
  153. UA_Int32 SL_Channel_new(TL_Connection *connection, const UA_ByteString* msg, UA_Int32* pos) {
  154. DBG_VERBOSE(printf("SL_Channel_new - entered\n"));
  155. UA_Int32 retval = UA_SUCCESS;
  156. /* Create New Channel*/
  157. SL_Channel *channel = &slc; // FIXME: generate new secure channel
  158. UA_AsymmetricAlgorithmSecurityHeader_init(&(channel->localAsymAlgSettings));
  159. UA_ByteString_copy(&UA_ByteString_securityPoliceNone, &(channel->localAsymAlgSettings.securityPolicyUri));
  160. UA_alloc((void**)&(channel->localNonce.data), sizeof(UA_Byte));
  161. channel->localNonce.length = 1;
  162. channel->connectionState = CONNECTIONSTATE_CLOSED; // the state of the channel will be opened in the service
  163. channel->sequenceHeader.requestId = 0;
  164. channel->sequenceHeader.sequenceNumber = 1;
  165. UA_String_init(&(channel->secureChannelId));
  166. channel->securityMode = UA_SECURITYMODE_INVALID;
  167. channel->securityToken.secureChannelId = 25; //TODO set a valid start secureChannelId number
  168. channel->securityToken.tokenId.tokenId = 1; //TODO set a valid start TokenId
  169. connection->secureChannel = channel;
  170. connection->secureChannel->tlConnection = connection;
  171. /* Read the OPN message headers */
  172. *pos += 4; // skip the securechannelid
  173. UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(msg, pos, &connection->secureChannel->remoteAsymAlgSettings);
  174. UA_SequenceHeader_decodeBinary(msg, pos, &connection->secureChannel->sequenceHeader);
  175. //TODO check that the sequence number is smaller than MaxUInt32 - 1024
  176. //TODO check if a OpenSecureChannelRequest follows
  177. retval |= SL_handleRequest(channel, msg, pos);
  178. return retval;
  179. // FIXME: reject
  180. }
  181. /**
  182. * process the rest of the header. TL already processed MessageType
  183. * (OPN,MSG,...), isFinal and MessageSize. SL_process cares for
  184. * secureChannelId, XASHeader and sequenceHeader
  185. * */
  186. UA_Int32 SL_Process(SL_Channel* connection, const UA_ByteString* msg, UA_Int32* pos) {
  187. DBG_VERBOSE(printf("SL_process - entered \n"));
  188. UA_UInt32 secureChannelId;
  189. if (connection->connectionState == CONNECTIONSTATE_ESTABLISHED) {
  190. UA_UInt32_decodeBinary(msg,pos,&secureChannelId);
  191. //FIXME: we assume SAS, need to check if AAS or SAS
  192. UA_SymmetricAlgorithmSecurityHeader symAlgSecHeader;
  193. // if (connection->securityMode == UA_MESSAGESECURITYMODE_NONE) {
  194. UA_SymmetricAlgorithmSecurityHeader_decodeBinary(msg, pos, &symAlgSecHeader);
  195. printf("SL_process - securityToken received=%d, expected=%d\n",secureChannelId,connection->securityToken.secureChannelId);
  196. if (secureChannelId == connection->securityToken.secureChannelId) {
  197. UA_SequenceHeader_decodeBinary(msg, pos, &(connection->sequenceHeader));
  198. SL_handleRequest(&slc, msg, pos);
  199. } else {
  200. //TODO generate ERROR_Bad_SecureChannelUnkown
  201. }
  202. }
  203. return UA_SUCCESS;
  204. }