ua_securechannel.h 11 KB

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