ua_securechannel.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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_plugin_securitypolicy.h"
  14. #include "ua_plugin_log.h"
  15. #include "ua_util.h"
  16. #define UA_SECURE_CONVERSATION_MESSAGE_HEADER_LENGTH 12
  17. #ifdef UA_ENABLE_UNIT_TEST_FAILURE_HOOKS
  18. extern UA_THREAD_LOCAL UA_StatusCode decrypt_verifySignatureFailure;
  19. extern UA_THREAD_LOCAL UA_StatusCode sendAsym_sendFailure;
  20. extern UA_THREAD_LOCAL UA_StatusCode processSym_seqNumberFailure;
  21. #endif
  22. struct UA_Session;
  23. typedef struct UA_Session UA_Session;
  24. struct SessionEntry {
  25. LIST_ENTRY(SessionEntry) pointers;
  26. UA_Session *session; // Just a pointer. The session is held in the session manager or the client
  27. };
  28. /* For chunked requests */
  29. struct ChunkEntry {
  30. LIST_ENTRY(ChunkEntry) pointers;
  31. UA_UInt32 requestId;
  32. UA_ByteString bytes;
  33. };
  34. typedef enum {
  35. UA_SECURECHANNELSTATE_FRESH,
  36. UA_SECURECHANNELSTATE_OPEN,
  37. UA_SECURECHANNELSTATE_CLOSED
  38. } UA_SecureChannelState;
  39. struct UA_SecureChannel {
  40. UA_SecureChannelState state;
  41. UA_MessageSecurityMode securityMode;
  42. UA_ChannelSecurityToken securityToken; /* the channelId is contained in the securityToken */
  43. UA_ChannelSecurityToken nextSecurityToken;
  44. /* The endpoint and context of the channel */
  45. const UA_SecurityPolicy *securityPolicy;
  46. void *channelContext; /* For interaction with the security policy */
  47. UA_Connection *connection;
  48. /* Asymmetric encryption info */
  49. UA_ByteString remoteCertificate;
  50. UA_Byte remoteCertificateThumbprint[20]; /* The thumprint of the remote certificate */
  51. /* Symmetric encryption info */
  52. UA_ByteString remoteNonce;
  53. UA_ByteString localNonce;
  54. UA_UInt32 receiveSequenceNumber;
  55. UA_UInt32 sendSequenceNumber;
  56. LIST_HEAD(session_pointerlist, SessionEntry) sessions;
  57. LIST_HEAD(chunk_pointerlist, ChunkEntry) chunks;
  58. };
  59. UA_StatusCode
  60. UA_SecureChannel_init(UA_SecureChannel *channel,
  61. const UA_SecurityPolicy *securityPolicy,
  62. const UA_ByteString *remoteCertificate);
  63. void UA_SecureChannel_deleteMembersCleanup(UA_SecureChannel *channel);
  64. /* Generates new keys and sets them in the channel context */
  65. UA_StatusCode UA_SecureChannel_generateNewKeys(UA_SecureChannel* const channel);
  66. /* Wrapper function for generating nonces for the supplied channel.
  67. *
  68. * Uses the random generator of the channels security policy to allocate
  69. * and generate a nonce with the specified length.
  70. *
  71. * \param channel the channel to use.
  72. * \param nonceLength the length of the nonce to be generated.
  73. * \param nonce will contain the nonce after being successfully called.
  74. */
  75. UA_StatusCode UA_SecureChannel_generateNonce(const UA_SecureChannel *const channel,
  76. const size_t nonceLength,
  77. UA_ByteString *const nonce);
  78. void UA_SecureChannel_attachSession(UA_SecureChannel *channel, UA_Session *session);
  79. void UA_SecureChannel_detachSession(UA_SecureChannel *channel, UA_Session *session);
  80. UA_Session * UA_SecureChannel_getSession(UA_SecureChannel *channel, UA_NodeId *token);
  81. UA_StatusCode UA_SecureChannel_revolveTokens(UA_SecureChannel *channel);
  82. UA_StatusCode
  83. UA_SecureChannel_sendSymmetricMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
  84. UA_MessageType messageType, const void *content,
  85. const UA_DataType *contentType);
  86. UA_StatusCode
  87. UA_SecureChannel_sendAsymmetricOPNMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
  88. const void *content, const UA_DataType *contentType);
  89. typedef UA_StatusCode
  90. (UA_ProcessMessageCallback)(void *application, UA_SecureChannel *channel,
  91. UA_MessageType messageType, UA_UInt32 requestId,
  92. const UA_ByteString *message);
  93. /* Process a single chunk. This also decrypts the chunk if required. The
  94. * callback function is called with the complete message body if the message is
  95. * complete.
  96. *
  97. * Symmetric calback is ERR, MSG, CLO only
  98. * Asymmetric callback is OPN only
  99. *
  100. * @param channel the channel the chunks were recieved on.
  101. * @param chunks the memory region where the chunks are stored.
  102. * @param callback the callback function that gets called with the complete
  103. * message body, once a final chunk is processed.
  104. * @param application data pointer to application specific data that gets passed
  105. * on to the callback function. */
  106. UA_StatusCode
  107. UA_SecureChannel_processChunk(UA_SecureChannel *channel, UA_ByteString *chunk,
  108. UA_ProcessMessageCallback callback,
  109. void *application);
  110. /**
  111. * Log Helper
  112. * ----------
  113. * C99 requires at least one element for the variadic argument. If the log
  114. * statement has no variable arguments, supply an additional NULL. It will be
  115. * ignored by printf.
  116. *
  117. * We have to jump through some hoops to enable the use of format strings
  118. * without arguments since (pedantic) C99 does not allow variadic macros with
  119. * zero arguments. So we add a dummy argument that is not printed (%.0s is
  120. * string of length zero). */
  121. #define UA_LOG_TRACE_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  122. UA_LOG_TRACE(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  123. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  124. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  125. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  126. #define UA_LOG_TRACE_CHANNEL(LOGGER, CHANNEL, ...) \
  127. UA_MACRO_EXPAND(UA_LOG_TRACE_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  128. #define UA_LOG_DEBUG_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  129. UA_LOG_DEBUG(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  130. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  131. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  132. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  133. #define UA_LOG_DEBUG_CHANNEL(LOGGER, CHANNEL, ...) \
  134. UA_MACRO_EXPAND(UA_LOG_DEBUG_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  135. #define UA_LOG_INFO_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  136. UA_LOG_INFO(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  137. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  138. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  139. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  140. #define UA_LOG_INFO_CHANNEL(LOGGER, CHANNEL, ...) \
  141. UA_MACRO_EXPAND(UA_LOG_INFO_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  142. #define UA_LOG_WARNING_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  143. UA_LOG_WARNING(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  144. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  145. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  146. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  147. #define UA_LOG_WARNING_CHANNEL(LOGGER, CHANNEL, ...) \
  148. UA_MACRO_EXPAND(UA_LOG_WARNING_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  149. #define UA_LOG_ERROR_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  150. UA_LOG_ERROR(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  151. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  152. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  153. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  154. #define UA_LOG_ERROR_CHANNEL(LOGGER, CHANNEL, ...) \
  155. UA_MACRO_EXPAND(UA_LOG_ERROR_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  156. #define UA_LOG_FATAL_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  157. UA_LOG_FATAL(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  158. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  159. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  160. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  161. #define UA_LOG_FATAL_CHANNEL(LOGGER, CHANNEL, ...) \
  162. UA_MACRO_EXPAND(UA_LOG_FATAL_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  163. #ifdef __cplusplus
  164. } // extern "C"
  165. #endif
  166. #endif /* UA_SECURECHANNEL_H_ */