ua_securechannel.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #define UA_SECURE_MESSAGE_HEADER_LENGTH 24
  18. #ifdef UA_ENABLE_UNIT_TEST_FAILURE_HOOKS
  19. extern UA_THREAD_LOCAL UA_StatusCode decrypt_verifySignatureFailure;
  20. extern UA_THREAD_LOCAL UA_StatusCode sendAsym_sendFailure;
  21. extern UA_THREAD_LOCAL UA_StatusCode processSym_seqNumberFailure;
  22. #endif
  23. struct UA_Session;
  24. typedef struct UA_Session UA_Session;
  25. struct SessionEntry {
  26. LIST_ENTRY(SessionEntry) pointers;
  27. UA_Session *session; // Just a pointer. The session is held in the session manager or the client
  28. };
  29. /* For chunked requests */
  30. struct ChunkEntry {
  31. LIST_ENTRY(ChunkEntry) pointers;
  32. UA_UInt32 requestId;
  33. UA_ByteString bytes;
  34. };
  35. typedef enum {
  36. UA_SECURECHANNELSTATE_FRESH,
  37. UA_SECURECHANNELSTATE_OPEN,
  38. UA_SECURECHANNELSTATE_CLOSED
  39. } UA_SecureChannelState;
  40. struct UA_SecureChannel {
  41. UA_SecureChannelState state;
  42. UA_MessageSecurityMode securityMode;
  43. UA_ChannelSecurityToken securityToken; /* the channelId is contained in the securityToken */
  44. UA_ChannelSecurityToken nextSecurityToken;
  45. /* The endpoint and context of the channel */
  46. const UA_SecurityPolicy *securityPolicy;
  47. void *channelContext; /* For interaction with the security policy */
  48. UA_Connection *connection;
  49. /* Asymmetric encryption info */
  50. UA_ByteString remoteCertificate;
  51. UA_Byte remoteCertificateThumbprint[20]; /* The thumbprint of the remote certificate */
  52. /* Symmetric encryption info */
  53. UA_ByteString remoteNonce;
  54. UA_ByteString localNonce;
  55. UA_UInt32 receiveSequenceNumber;
  56. UA_UInt32 sendSequenceNumber;
  57. LIST_HEAD(session_pointerlist, SessionEntry) sessions;
  58. LIST_HEAD(chunk_pointerlist, ChunkEntry) chunks;
  59. };
  60. UA_StatusCode
  61. UA_SecureChannel_init(UA_SecureChannel *channel,
  62. const UA_SecurityPolicy *securityPolicy,
  63. const UA_ByteString *remoteCertificate);
  64. void UA_SecureChannel_deleteMembersCleanup(UA_SecureChannel *channel);
  65. /* Generates new keys and sets them in the channel context */
  66. UA_StatusCode UA_SecureChannel_generateNewKeys(UA_SecureChannel* const channel);
  67. /* Wrapper function for generating nonces for the supplied channel.
  68. *
  69. * Uses the random generator of the channels security policy to allocate
  70. * and generate a nonce with the specified length.
  71. *
  72. * \param channel the channel to use.
  73. * \param nonceLength the length of the nonce to be generated.
  74. * \param nonce will contain the nonce after being successfully called.
  75. */
  76. UA_StatusCode UA_SecureChannel_generateNonce(const UA_SecureChannel *const channel,
  77. const size_t nonceLength,
  78. UA_ByteString *const nonce);
  79. void UA_SecureChannel_attachSession(UA_SecureChannel *channel, UA_Session *session);
  80. void UA_SecureChannel_detachSession(UA_SecureChannel *channel, UA_Session *session);
  81. UA_Session * UA_SecureChannel_getSession(UA_SecureChannel *channel, UA_NodeId *token);
  82. UA_StatusCode UA_SecureChannel_revolveTokens(UA_SecureChannel *channel);
  83. /**
  84. * Sending Messages
  85. * ---------------- */
  86. UA_StatusCode
  87. UA_SecureChannel_sendSymmetricMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
  88. UA_MessageType messageType, void *payload,
  89. const UA_DataType *payloadType);
  90. typedef struct {
  91. UA_SecureChannel *channel;
  92. UA_UInt32 requestId;
  93. UA_UInt32 messageType;
  94. UA_UInt16 chunksSoFar;
  95. size_t messageSizeSoFar;
  96. UA_ByteString messageBuffer;
  97. UA_Byte *buf_pos;
  98. const UA_Byte *buf_end;
  99. UA_Boolean final;
  100. } UA_MessageContext;
  101. /* Start the context of a new symmetric message. */
  102. UA_StatusCode
  103. UA_MessageContext_begin(UA_MessageContext *mc, UA_SecureChannel *channel,
  104. UA_UInt32 requestId, UA_MessageType messageType);
  105. /* Encode the content and send out full chunks. If the return code is good, then
  106. * the ChunkInfo contains encoded content that has not been sent. If the return
  107. * code is bad, then the ChunkInfo has been cleaned up internally. */
  108. UA_StatusCode
  109. UA_MessageContext_encode(UA_MessageContext *mc, const void *content,
  110. const UA_DataType *contentType);
  111. /* Sends a symmetric message already encoded in the context. The context is
  112. * cleaned up, also in case of errors. */
  113. UA_StatusCode
  114. UA_MessageContext_finish(UA_MessageContext *mc);
  115. /* To be used when a failure occures when a MessageContext is open. Note that
  116. * the _encode and _finish methods will clean up internally. _abort can be run
  117. * on a MessageContext that has already been cleaned up before. */
  118. void
  119. UA_MessageContext_abort(UA_MessageContext *mc);
  120. UA_StatusCode
  121. UA_SecureChannel_sendAsymmetricOPNMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
  122. const void *content, const UA_DataType *contentType);
  123. /**
  124. * Process Received Chunks
  125. * ----------------------- */
  126. typedef UA_StatusCode
  127. (UA_ProcessMessageCallback)(void *application, UA_SecureChannel *channel,
  128. UA_MessageType messageType, UA_UInt32 requestId,
  129. const UA_ByteString *message);
  130. /* Process a single chunk. This also decrypts the chunk if required. The
  131. * callback function is called with the complete message body if the message is
  132. * complete.
  133. *
  134. * Symmetric callback is ERR, MSG, CLO only
  135. * Asymmetric callback is OPN only
  136. *
  137. * @param channel the channel the chunks were received on.
  138. * @param chunks the memory region where the chunks are stored.
  139. * @param callback the callback function that gets called with the complete
  140. * message body, once a final chunk is processed.
  141. * @param application data pointer to application specific data that gets passed
  142. * on to the callback function. */
  143. UA_StatusCode
  144. UA_SecureChannel_processChunk(UA_SecureChannel *channel, UA_ByteString *chunk,
  145. UA_ProcessMessageCallback callback,
  146. void *application);
  147. /**
  148. * Log Helper
  149. * ----------
  150. * C99 requires at least one element for the variadic argument. If the log
  151. * statement has no variable arguments, supply an additional NULL. It will be
  152. * ignored by printf.
  153. *
  154. * We have to jump through some hoops to enable the use of format strings
  155. * without arguments since (pedantic) C99 does not allow variadic macros with
  156. * zero arguments. So we add a dummy argument that is not printed (%.0s is
  157. * string of length zero). */
  158. #define UA_LOG_TRACE_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  159. UA_LOG_TRACE(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  160. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  161. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  162. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  163. #define UA_LOG_TRACE_CHANNEL(LOGGER, CHANNEL, ...) \
  164. UA_MACRO_EXPAND(UA_LOG_TRACE_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  165. #define UA_LOG_DEBUG_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  166. UA_LOG_DEBUG(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  167. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  168. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  169. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  170. #define UA_LOG_DEBUG_CHANNEL(LOGGER, CHANNEL, ...) \
  171. UA_MACRO_EXPAND(UA_LOG_DEBUG_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  172. #define UA_LOG_INFO_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  173. UA_LOG_INFO(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  174. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  175. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  176. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  177. #define UA_LOG_INFO_CHANNEL(LOGGER, CHANNEL, ...) \
  178. UA_MACRO_EXPAND(UA_LOG_INFO_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  179. #define UA_LOG_WARNING_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  180. UA_LOG_WARNING(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  181. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  182. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  183. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  184. #define UA_LOG_WARNING_CHANNEL(LOGGER, CHANNEL, ...) \
  185. UA_MACRO_EXPAND(UA_LOG_WARNING_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  186. #define UA_LOG_ERROR_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  187. UA_LOG_ERROR(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  188. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  189. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  190. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  191. #define UA_LOG_ERROR_CHANNEL(LOGGER, CHANNEL, ...) \
  192. UA_MACRO_EXPAND(UA_LOG_ERROR_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  193. #define UA_LOG_FATAL_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  194. UA_LOG_FATAL(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  195. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  196. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  197. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  198. #define UA_LOG_FATAL_CHANNEL(LOGGER, CHANNEL, ...) \
  199. UA_MACRO_EXPAND(UA_LOG_FATAL_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  200. #ifdef __cplusplus
  201. } // extern "C"
  202. #endif
  203. #endif /* UA_SECURECHANNEL_H_ */