ua_securechannel.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.*/
  4. #ifndef UA_SECURECHANNEL_H_
  5. #define UA_SECURECHANNEL_H_
  6. #include "queue.h"
  7. #include "ua_types.h"
  8. #include "ua_transport_generated.h"
  9. #include "ua_connection_internal.h"
  10. struct UA_Session;
  11. typedef struct UA_Session UA_Session;
  12. struct SessionEntry {
  13. LIST_ENTRY(SessionEntry) pointers;
  14. UA_Session *session; // Just a pointer. The session is held in the session manager or the client
  15. };
  16. /* For chunked requests */
  17. struct ChunkEntry {
  18. LIST_ENTRY(ChunkEntry) pointers;
  19. UA_UInt32 requestId;
  20. UA_ByteString bytes;
  21. };
  22. /* For chunked responses */
  23. typedef struct {
  24. UA_SecureChannel *channel;
  25. UA_UInt32 requestId;
  26. UA_UInt32 messageType;
  27. UA_UInt16 chunksSoFar;
  28. size_t messageSizeSoFar;
  29. UA_Boolean final;
  30. UA_StatusCode errorCode;
  31. } UA_ChunkInfo;
  32. struct UA_SecureChannel {
  33. UA_MessageSecurityMode securityMode;
  34. UA_ChannelSecurityToken securityToken; // the channelId is contained in the securityToken
  35. UA_ChannelSecurityToken nextSecurityToken; // the channelId is contained in the securityToken
  36. UA_AsymmetricAlgorithmSecurityHeader clientAsymAlgSettings;
  37. UA_AsymmetricAlgorithmSecurityHeader serverAsymAlgSettings;
  38. UA_ByteString clientNonce;
  39. UA_ByteString serverNonce;
  40. UA_UInt32 receiveSequenceNumber;
  41. UA_UInt32 sendSequenceNumber;
  42. UA_Connection *connection;
  43. LIST_HEAD(session_pointerlist, SessionEntry) sessions;
  44. LIST_HEAD(chunk_pointerlist, ChunkEntry) chunks;
  45. };
  46. void UA_SecureChannel_init(UA_SecureChannel *channel);
  47. void UA_SecureChannel_deleteMembersCleanup(UA_SecureChannel *channel);
  48. UA_StatusCode UA_SecureChannel_generateNonce(UA_ByteString *nonce);
  49. void UA_SecureChannel_attachSession(UA_SecureChannel *channel, UA_Session *session);
  50. void UA_SecureChannel_detachSession(UA_SecureChannel *channel, UA_Session *session);
  51. UA_Session * UA_SecureChannel_getSession(UA_SecureChannel *channel, UA_NodeId *token);
  52. UA_StatusCode UA_SecureChannel_sendBinaryMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
  53. const void *content, const UA_DataType *contentType);
  54. void UA_SecureChannel_revolveTokens(UA_SecureChannel *channel);
  55. /**
  56. * Chunking
  57. * -------- */
  58. typedef void
  59. (UA_ProcessMessageCallback)(void *application, UA_SecureChannel *channel,
  60. UA_MessageType messageType, UA_UInt32 requestId,
  61. const UA_ByteString *message);
  62. UA_StatusCode
  63. UA_SecureChannel_processChunks(UA_SecureChannel *channel, const UA_ByteString *chunks,
  64. UA_ProcessMessageCallback callback, void *application);
  65. /**
  66. * Log Helper
  67. * ---------- */
  68. #define UA_LOG_TRACE_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  69. UA_LOG_TRACE(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, "Connection %i | SecureChannel %i | " MSG, \
  70. ((CHANNEL)->connection ? CHANNEL->connection->sockfd : 0), \
  71. (CHANNEL)->securityToken.channelId, ##__VA_ARGS__);
  72. #define UA_LOG_DEBUG_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  73. UA_LOG_DEBUG(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, "Connection %i | SecureChannel %i | " MSG, \
  74. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  75. (CHANNEL)->securityToken.channelId, ##__VA_ARGS__);
  76. #define UA_LOG_INFO_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  77. UA_LOG_INFO(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, "Connection %i | SecureChannel %i | " MSG, \
  78. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  79. (CHANNEL)->securityToken.channelId, ##__VA_ARGS__);
  80. #define UA_LOG_WARNING_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  81. UA_LOG_WARNING(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, "Connection %i | SecureChannel %i | " MSG, \
  82. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  83. (CHANNEL)->securityToken.channelId, ##__VA_ARGS__);
  84. #define UA_LOG_ERROR_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  85. UA_LOG_ERROR(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, "Connection %i | SecureChannel %i | " MSG, \
  86. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  87. (CHANNEL)->securityToken.channelId, ##__VA_ARGS__);
  88. #define UA_LOG_FATAL_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  89. UA_LOG_FATAL(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, "Connection %i | SecureChannel %i | " MSG, \
  90. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  91. (CHANNEL)->securityToken.channelId, ##__VA_ARGS__);
  92. #endif /* UA_SECURECHANNEL_H_ */