ua_securechannel.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include "queue.h"
  10. #include "ua_types.h"
  11. #include "ua_transport_generated.h"
  12. #include "ua_connection_internal.h"
  13. struct UA_Session;
  14. typedef struct UA_Session UA_Session;
  15. struct SessionEntry {
  16. LIST_ENTRY(SessionEntry) pointers;
  17. UA_Session *session; // Just a pointer. The session is held in the session manager or the client
  18. };
  19. /* For chunked requests */
  20. struct ChunkEntry {
  21. LIST_ENTRY(ChunkEntry) pointers;
  22. UA_UInt32 requestId;
  23. UA_ByteString bytes;
  24. };
  25. /* For chunked responses */
  26. typedef struct {
  27. UA_SecureChannel *channel;
  28. UA_UInt32 requestId;
  29. UA_UInt32 messageType;
  30. UA_UInt16 chunksSoFar;
  31. size_t messageSizeSoFar;
  32. UA_Boolean final;
  33. UA_StatusCode errorCode;
  34. UA_ByteString buffer;
  35. } UA_ChunkInfo;
  36. struct UA_SecureChannel {
  37. UA_MessageSecurityMode securityMode;
  38. UA_ChannelSecurityToken securityToken; // the channelId is contained in the securityToken
  39. UA_ChannelSecurityToken nextSecurityToken; // the channelId is contained in the securityToken
  40. UA_AsymmetricAlgorithmSecurityHeader clientAsymAlgSettings;
  41. UA_AsymmetricAlgorithmSecurityHeader serverAsymAlgSettings;
  42. UA_ByteString clientNonce;
  43. UA_ByteString serverNonce;
  44. UA_UInt32 receiveSequenceNumber;
  45. UA_UInt32 sendSequenceNumber;
  46. UA_Connection *connection;
  47. LIST_HEAD(session_pointerlist, SessionEntry) sessions;
  48. LIST_HEAD(chunk_pointerlist, ChunkEntry) chunks;
  49. };
  50. void UA_SecureChannel_init(UA_SecureChannel *channel);
  51. void UA_SecureChannel_deleteMembersCleanup(UA_SecureChannel *channel);
  52. UA_StatusCode UA_SecureChannel_generateNonce(UA_ByteString *nonce);
  53. void UA_SecureChannel_attachSession(UA_SecureChannel *channel, UA_Session *session);
  54. void UA_SecureChannel_detachSession(UA_SecureChannel *channel, UA_Session *session);
  55. UA_Session * UA_SecureChannel_getSession(UA_SecureChannel *channel, UA_NodeId *token);
  56. UA_StatusCode UA_SecureChannel_sendBinaryMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
  57. const void *content, const UA_DataType *contentType);
  58. void UA_SecureChannel_revolveTokens(UA_SecureChannel *channel);
  59. /**
  60. * Chunking
  61. * -------- */
  62. typedef void
  63. (UA_ProcessMessageCallback)(void *application, UA_SecureChannel *channel,
  64. UA_MessageType messageType, UA_UInt32 requestId,
  65. const UA_ByteString *message);
  66. UA_StatusCode
  67. UA_SecureChannel_processChunks(UA_SecureChannel *channel, const UA_ByteString *chunks,
  68. UA_ProcessMessageCallback callback, void *application);
  69. /**
  70. * Log Helper
  71. * ---------- */
  72. #define UA_LOG_TRACE_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  73. UA_LOG_TRACE(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_DEBUG_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  77. UA_LOG_DEBUG(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_INFO_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  81. UA_LOG_INFO(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_WARNING_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  85. UA_LOG_WARNING(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_ERROR_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  89. UA_LOG_ERROR(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, "Connection %i | SecureChannel %i | " MSG, \
  90. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  91. (CHANNEL)->securityToken.channelId, ##__VA_ARGS__);
  92. #define UA_LOG_FATAL_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  93. UA_LOG_FATAL(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, "Connection %i | SecureChannel %i | " MSG, \
  94. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  95. (CHANNEL)->securityToken.channelId, ##__VA_ARGS__);
  96. #ifdef __cplusplus
  97. } // extern "C"
  98. #endif
  99. #endif /* UA_SECURECHANNEL_H_ */