ua_securechannel.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_ChunkInfo;
  35. struct UA_SecureChannel {
  36. UA_MessageSecurityMode securityMode;
  37. UA_ChannelSecurityToken securityToken; // the channelId is contained in the securityToken
  38. UA_ChannelSecurityToken nextSecurityToken; // the channelId is contained in the securityToken
  39. UA_AsymmetricAlgorithmSecurityHeader clientAsymAlgSettings;
  40. UA_AsymmetricAlgorithmSecurityHeader serverAsymAlgSettings;
  41. UA_ByteString clientNonce;
  42. UA_ByteString serverNonce;
  43. UA_UInt32 receiveSequenceNumber;
  44. UA_UInt32 sendSequenceNumber;
  45. UA_Connection *connection;
  46. LIST_HEAD(session_pointerlist, SessionEntry) sessions;
  47. LIST_HEAD(chunk_pointerlist, ChunkEntry) chunks;
  48. };
  49. void UA_SecureChannel_init(UA_SecureChannel *channel);
  50. void UA_SecureChannel_deleteMembersCleanup(UA_SecureChannel *channel);
  51. UA_StatusCode UA_SecureChannel_generateNonce(UA_ByteString *nonce);
  52. void UA_SecureChannel_attachSession(UA_SecureChannel *channel, UA_Session *session);
  53. void UA_SecureChannel_detachSession(UA_SecureChannel *channel, UA_Session *session);
  54. UA_Session * UA_SecureChannel_getSession(UA_SecureChannel *channel, UA_NodeId *token);
  55. UA_StatusCode UA_SecureChannel_sendBinaryMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
  56. const void *content, const UA_DataType *contentType);
  57. void UA_SecureChannel_revolveTokens(UA_SecureChannel *channel);
  58. /**
  59. * Chunking
  60. * -------- */
  61. typedef void
  62. (UA_ProcessMessageCallback)(void *application, UA_SecureChannel *channel,
  63. UA_MessageType messageType, UA_UInt32 requestId,
  64. const UA_ByteString *message);
  65. UA_StatusCode
  66. UA_SecureChannel_processChunks(UA_SecureChannel *channel, const UA_ByteString *chunks,
  67. UA_ProcessMessageCallback callback, void *application);
  68. /**
  69. * Log Helper
  70. * ---------- */
  71. #define UA_LOG_TRACE_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  72. UA_LOG_TRACE(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, "Connection %i | SecureChannel %i | " MSG, \
  73. ((CHANNEL)->connection ? CHANNEL->connection->sockfd : 0), \
  74. (CHANNEL)->securityToken.channelId, ##__VA_ARGS__);
  75. #define UA_LOG_DEBUG_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  76. UA_LOG_DEBUG(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, "Connection %i | SecureChannel %i | " MSG, \
  77. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  78. (CHANNEL)->securityToken.channelId, ##__VA_ARGS__);
  79. #define UA_LOG_INFO_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  80. UA_LOG_INFO(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, "Connection %i | SecureChannel %i | " MSG, \
  81. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  82. (CHANNEL)->securityToken.channelId, ##__VA_ARGS__);
  83. #define UA_LOG_WARNING_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  84. UA_LOG_WARNING(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, "Connection %i | SecureChannel %i | " MSG, \
  85. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  86. (CHANNEL)->securityToken.channelId, ##__VA_ARGS__);
  87. #define UA_LOG_ERROR_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  88. UA_LOG_ERROR(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, "Connection %i | SecureChannel %i | " MSG, \
  89. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  90. (CHANNEL)->securityToken.channelId, ##__VA_ARGS__);
  91. #define UA_LOG_FATAL_CHANNEL(LOGGER, CHANNEL, MSG, ...) \
  92. UA_LOG_FATAL(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, "Connection %i | SecureChannel %i | " MSG, \
  93. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  94. (CHANNEL)->securityToken.channelId, ##__VA_ARGS__);
  95. #ifdef __cplusplus
  96. } // extern "C"
  97. #endif
  98. #endif /* UA_SECURECHANNEL_H_ */