ua_securechannel.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include "ua_util.h"
  14. struct UA_Session;
  15. typedef struct UA_Session UA_Session;
  16. struct SessionEntry {
  17. LIST_ENTRY(SessionEntry) pointers;
  18. UA_Session *session; // Just a pointer. The session is held in the session manager or the client
  19. };
  20. /* For chunked requests */
  21. struct ChunkEntry {
  22. LIST_ENTRY(ChunkEntry) pointers;
  23. UA_UInt32 requestId;
  24. UA_ByteString bytes;
  25. };
  26. /* For chunked responses */
  27. typedef struct {
  28. UA_SecureChannel *channel;
  29. UA_UInt32 requestId;
  30. UA_UInt32 messageType;
  31. UA_UInt16 chunksSoFar;
  32. size_t messageSizeSoFar;
  33. UA_Boolean final;
  34. UA_StatusCode errorCode;
  35. UA_ByteString buffer;
  36. } UA_ChunkInfo;
  37. struct UA_SecureChannel {
  38. UA_MessageSecurityMode securityMode;
  39. UA_ChannelSecurityToken securityToken; // the channelId is contained in the securityToken
  40. UA_ChannelSecurityToken nextSecurityToken; // the channelId is contained in the securityToken
  41. UA_AsymmetricAlgorithmSecurityHeader clientAsymAlgSettings;
  42. UA_AsymmetricAlgorithmSecurityHeader serverAsymAlgSettings;
  43. UA_ByteString clientNonce;
  44. UA_ByteString serverNonce;
  45. UA_UInt32 receiveSequenceNumber;
  46. UA_UInt32 sendSequenceNumber;
  47. UA_Connection *connection;
  48. LIST_HEAD(session_pointerlist, SessionEntry) sessions;
  49. LIST_HEAD(chunk_pointerlist, ChunkEntry) chunks;
  50. };
  51. void UA_SecureChannel_init(UA_SecureChannel *channel);
  52. void UA_SecureChannel_deleteMembersCleanup(UA_SecureChannel *channel);
  53. UA_StatusCode UA_SecureChannel_generateNonce(UA_ByteString *nonce);
  54. void UA_SecureChannel_attachSession(UA_SecureChannel *channel, UA_Session *session);
  55. void UA_SecureChannel_detachSession(UA_SecureChannel *channel, UA_Session *session);
  56. UA_Session * UA_SecureChannel_getSession(UA_SecureChannel *channel, UA_NodeId *token);
  57. UA_StatusCode UA_SecureChannel_sendBinaryMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
  58. const void *content, const UA_DataType *contentType);
  59. void UA_SecureChannel_revolveTokens(UA_SecureChannel *channel);
  60. /**
  61. * Chunking
  62. * -------- */
  63. typedef void
  64. (UA_ProcessMessageCallback)(void *application, UA_SecureChannel *channel,
  65. UA_MessageType messageType, UA_UInt32 requestId,
  66. const UA_ByteString *message);
  67. UA_StatusCode
  68. UA_SecureChannel_processChunks(UA_SecureChannel *channel, const UA_ByteString *chunks,
  69. UA_ProcessMessageCallback callback, void *application);
  70. /**
  71. * Log Helper
  72. * ----------
  73. * C99 requires at least one element for the variadic argument. If the log
  74. * statement has no variable arguments, supply an additional NULL. It will be
  75. * ignored by printf.
  76. *
  77. * We have to jump through some hoops to enable the use of format strings
  78. * without arguments since (pedantic) C99 does not allow variadic macros with
  79. * zero arguments. So we add a dummy argument that is not printed (%.0s is
  80. * string of length zero). */
  81. #define UA_LOG_TRACE_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  82. UA_LOG_TRACE(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  83. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  84. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  85. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  86. #define UA_LOG_TRACE_CHANNEL(LOGGER, CHANNEL, ...) \
  87. UA_MACRO_EXPAND(UA_LOG_TRACE_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  88. #define UA_LOG_DEBUG_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  89. UA_LOG_DEBUG(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  90. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  91. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  92. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  93. #define UA_LOG_DEBUG_CHANNEL(LOGGER, CHANNEL, ...) \
  94. UA_MACRO_EXPAND(UA_LOG_DEBUG_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  95. #define UA_LOG_INFO_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  96. UA_LOG_INFO(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  97. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  98. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  99. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  100. #define UA_LOG_INFO_CHANNEL(LOGGER, CHANNEL, ...) \
  101. UA_MACRO_EXPAND(UA_LOG_INFO_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  102. #define UA_LOG_WARNING_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  103. UA_LOG_WARNING(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  104. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  105. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  106. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  107. #define UA_LOG_WARNING_CHANNEL(LOGGER, CHANNEL, ...) \
  108. UA_MACRO_EXPAND(UA_LOG_WARNING_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  109. #define UA_LOG_ERROR_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  110. UA_LOG_ERROR(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  111. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  112. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  113. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  114. #define UA_LOG_ERROR_CHANNEL(LOGGER, CHANNEL, ...) \
  115. UA_MACRO_EXPAND(UA_LOG_ERROR_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  116. #define UA_LOG_FATAL_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  117. UA_LOG_FATAL(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  118. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  119. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  120. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  121. #define UA_LOG_FATAL_CHANNEL(LOGGER, CHANNEL, ...) \
  122. UA_MACRO_EXPAND(UA_LOG_FATAL_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  123. #ifdef __cplusplus
  124. } // extern "C"
  125. #endif
  126. #endif /* UA_SECURECHANNEL_H_ */