ua_securechannel.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. *
  5. * Copyright 2014-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2017 (c) Florian Palm
  7. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  8. * Copyright 2017 (c) Mark Giraud, Fraunhofer IOSB
  9. */
  10. #ifndef UA_SECURECHANNEL_H_
  11. #define UA_SECURECHANNEL_H_
  12. #include "ua_types.h"
  13. #include "ua_transport_generated.h"
  14. #include "ua_connection_internal.h"
  15. #include "ua_plugin_securitypolicy.h"
  16. #include "ua_plugin_log.h"
  17. #include "../deps/queue.h"
  18. _UA_BEGIN_DECLS
  19. #define UA_SECURE_CONVERSATION_MESSAGE_HEADER_LENGTH 12
  20. #define UA_SECURE_MESSAGE_HEADER_LENGTH 24
  21. /* Thread-local variables to force failure modes during testing */
  22. #ifdef UA_ENABLE_UNIT_TEST_FAILURE_HOOKS
  23. extern UA_StatusCode decrypt_verifySignatureFailure;
  24. extern UA_StatusCode sendAsym_sendFailure;
  25. extern UA_StatusCode processSym_seqNumberFailure;
  26. #endif
  27. /* The Session implementation differs between client and server. Still, it is
  28. * expected that the Session structure begins with the SessionHeader. This is
  29. * the interface that will be used by the SecureChannel. The lifecycle of
  30. * Sessions is independent of the underlying SecureChannel. But every Session
  31. * can be attached to only one SecureChannel. */
  32. typedef struct UA_SessionHeader {
  33. LIST_ENTRY(UA_SessionHeader) pointers;
  34. UA_NodeId authenticationToken;
  35. UA_SecureChannel *channel; /* The pointer back to the SecureChannel in the session. */
  36. } UA_SessionHeader;
  37. /* For chunked requests */
  38. typedef struct UA_ChunkPayload {
  39. SIMPLEQ_ENTRY(UA_ChunkPayload) pointers;
  40. UA_ByteString bytes;
  41. UA_Boolean copied; /* Do the bytes point to a buffer from the network or was
  42. memory allocated for the chunk separately */
  43. } UA_ChunkPayload;
  44. /* Receieved messages. Process them only in order. The Chunk payload has all
  45. * headers and the padding stripped out. The payload begins at the
  46. * ExtensionObject prefix.*/
  47. typedef struct UA_Message {
  48. TAILQ_ENTRY(UA_Message) pointers;
  49. UA_UInt32 requestId;
  50. UA_MessageType messageType;
  51. SIMPLEQ_HEAD(pp, UA_ChunkPayload) chunkPayloads;
  52. size_t chunkPayloadsSize; /* No of chunks received so far */
  53. size_t messageSize; /* Total length of the chunks received so far */
  54. UA_Boolean final; /* All chunks for the message have been received */
  55. } UA_Message;
  56. typedef enum {
  57. UA_SECURECHANNELSTATE_FRESH,
  58. UA_SECURECHANNELSTATE_OPEN,
  59. UA_SECURECHANNELSTATE_CLOSED
  60. } UA_SecureChannelState;
  61. typedef TAILQ_HEAD(UA_MessageQueue, UA_Message) UA_MessageQueue;
  62. struct UA_SecureChannel {
  63. UA_SecureChannelState state;
  64. UA_MessageSecurityMode securityMode;
  65. /* We use three tokens because when switching tokens the client is allowed to accept
  66. * messages with the old token for up to 25% of the lifetime after the token would have timed out.
  67. * For messages that are sent, the new token is already used, which is contained in the securityToken
  68. * variable. The nextSecurityToken variable holds a newly issued token, that will be automatically
  69. * revolved into the securityToken variable. This could be done with two variables, but would require
  70. * greater changes to the current code. This could be done in the future after the client and networking
  71. * structure has been reworked, which would make this easier to implement. */
  72. UA_ChannelSecurityToken securityToken; /* the channelId is contained in the securityToken */
  73. UA_ChannelSecurityToken nextSecurityToken;
  74. UA_ChannelSecurityToken previousSecurityToken;
  75. /* The endpoint and context of the channel */
  76. const UA_SecurityPolicy *securityPolicy;
  77. void *channelContext; /* For interaction with the security policy */
  78. UA_Connection *connection;
  79. /* Asymmetric encryption info */
  80. UA_ByteString remoteCertificate;
  81. UA_Byte remoteCertificateThumbprint[20]; /* The thumbprint of the remote certificate */
  82. /* Symmetric encryption info */
  83. UA_ByteString remoteNonce;
  84. UA_ByteString localNonce;
  85. UA_UInt32 receiveSequenceNumber;
  86. UA_UInt32 sendSequenceNumber;
  87. LIST_HEAD(, UA_SessionHeader) sessions;
  88. UA_MessageQueue messages;
  89. };
  90. void UA_SecureChannel_init(UA_SecureChannel *channel);
  91. UA_StatusCode
  92. UA_SecureChannel_setSecurityPolicy(UA_SecureChannel *channel,
  93. const UA_SecurityPolicy *securityPolicy,
  94. const UA_ByteString *remoteCertificate);
  95. /* Remove (partially) received unprocessed messages */
  96. void UA_SecureChannel_deleteMessages(UA_SecureChannel *channel);
  97. void UA_SecureChannel_deleteMembersCleanup(UA_SecureChannel *channel);
  98. /* Generates new keys and sets them in the channel context */
  99. UA_StatusCode
  100. UA_SecureChannel_generateNewKeys(UA_SecureChannel* channel);
  101. /* Wrapper function for generating a local nonce for the supplied channel. Uses
  102. * the random generator of the channels security policy to allocate and generate
  103. * a nonce with the specified length. */
  104. UA_StatusCode
  105. UA_SecureChannel_generateLocalNonce(UA_SecureChannel *channel);
  106. UA_SessionHeader *
  107. UA_SecureChannel_getSession(UA_SecureChannel *channel,
  108. const UA_NodeId *authenticationToken);
  109. UA_StatusCode
  110. UA_SecureChannel_revolveTokens(UA_SecureChannel *channel);
  111. /**
  112. * Sending Messages
  113. * ---------------- */
  114. UA_StatusCode
  115. UA_SecureChannel_sendAsymmetricOPNMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
  116. const void *content, const UA_DataType *contentType);
  117. UA_StatusCode
  118. UA_SecureChannel_sendSymmetricMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
  119. UA_MessageType messageType, void *payload,
  120. const UA_DataType *payloadType);
  121. /* The MessageContext is forwarded into the encoding layer so that we can send
  122. * chunks before continuing to encode. This lets us reuse a fixed chunk-sized
  123. * messages buffer. */
  124. typedef struct {
  125. UA_SecureChannel *channel;
  126. UA_UInt32 requestId;
  127. UA_UInt32 messageType;
  128. UA_UInt16 chunksSoFar;
  129. size_t messageSizeSoFar;
  130. UA_ByteString messageBuffer;
  131. UA_Byte *buf_pos;
  132. const UA_Byte *buf_end;
  133. UA_Boolean final;
  134. } UA_MessageContext;
  135. /* Start the context of a new symmetric message. */
  136. UA_StatusCode
  137. UA_MessageContext_begin(UA_MessageContext *mc, UA_SecureChannel *channel,
  138. UA_UInt32 requestId, UA_MessageType messageType);
  139. /* Encode the content and send out full chunks. If the return code is good, then
  140. * the ChunkInfo contains encoded content that has not been sent. If the return
  141. * code is bad, then the ChunkInfo has been cleaned up internally. */
  142. UA_StatusCode
  143. UA_MessageContext_encode(UA_MessageContext *mc, const void *content,
  144. const UA_DataType *contentType);
  145. /* Sends a symmetric message already encoded in the context. The context is
  146. * cleaned up, also in case of errors. */
  147. UA_StatusCode
  148. UA_MessageContext_finish(UA_MessageContext *mc);
  149. /* To be used when a failure occures when a MessageContext is open. Note that
  150. * the _encode and _finish methods will clean up internally. _abort can be run
  151. * on a MessageContext that has already been cleaned up before. */
  152. void
  153. UA_MessageContext_abort(UA_MessageContext *mc);
  154. /**
  155. * Receive Message
  156. * --------------- */
  157. /* Decrypt a chunk and add it to the message. Create a new message if necessary. */
  158. UA_StatusCode
  159. UA_SecureChannel_decryptAddChunk(UA_SecureChannel *channel, const UA_ByteString *chunk,
  160. UA_Boolean allowPreviousToken);
  161. /* The network buffer is about to be cleared. Copy all chunks that point into
  162. * the network buffer into dedicated memory. */
  163. UA_StatusCode
  164. UA_SecureChannel_persistIncompleteMessages(UA_SecureChannel *channel);
  165. typedef void
  166. (UA_ProcessMessageCallback)(void *application, UA_SecureChannel *channel,
  167. UA_MessageType messageType, UA_UInt32 requestId,
  168. const UA_ByteString *message);
  169. /* Process received complete messages in-order. The callback function is called
  170. * with the complete message body if the message is complete. The message is
  171. * removed afterwards.
  172. *
  173. * Symmetric callback is ERR, MSG, CLO only
  174. * Asymmetric callback is OPN only
  175. *
  176. * @param channel the channel the chunks were received on.
  177. * @param application data pointer to application specific data that gets passed
  178. * on to the callback function.
  179. * @param callback the callback function that gets called with the complete
  180. * message body, once a final chunk is processed.
  181. * @return Returns if an irrecoverable error occured. Maybe close the channel. */
  182. UA_StatusCode
  183. UA_SecureChannel_processCompleteMessages(UA_SecureChannel *channel, void *application,
  184. UA_ProcessMessageCallback callback);
  185. /**
  186. * Log Helper
  187. * ----------
  188. * C99 requires at least one element for the variadic argument. If the log
  189. * statement has no variable arguments, supply an additional NULL. It will be
  190. * ignored by printf.
  191. *
  192. * We have to jump through some hoops to enable the use of format strings
  193. * without arguments since (pedantic) C99 does not allow variadic macros with
  194. * zero arguments. So we add a dummy argument that is not printed (%.0s is
  195. * string of length zero). */
  196. #define UA_LOG_TRACE_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  197. UA_LOG_TRACE(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  198. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  199. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  200. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  201. #define UA_LOG_TRACE_CHANNEL(LOGGER, CHANNEL, ...) \
  202. UA_MACRO_EXPAND(UA_LOG_TRACE_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  203. #define UA_LOG_DEBUG_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  204. UA_LOG_DEBUG(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  205. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  206. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  207. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  208. #define UA_LOG_DEBUG_CHANNEL(LOGGER, CHANNEL, ...) \
  209. UA_MACRO_EXPAND(UA_LOG_DEBUG_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  210. #define UA_LOG_INFO_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  211. UA_LOG_INFO(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  212. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  213. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  214. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  215. #define UA_LOG_INFO_CHANNEL(LOGGER, CHANNEL, ...) \
  216. UA_MACRO_EXPAND(UA_LOG_INFO_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  217. #define UA_LOG_WARNING_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  218. UA_LOG_WARNING(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  219. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  220. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  221. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  222. #define UA_LOG_WARNING_CHANNEL(LOGGER, CHANNEL, ...) \
  223. UA_MACRO_EXPAND(UA_LOG_WARNING_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  224. #define UA_LOG_ERROR_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  225. UA_LOG_ERROR(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  226. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  227. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  228. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  229. #define UA_LOG_ERROR_CHANNEL(LOGGER, CHANNEL, ...) \
  230. UA_MACRO_EXPAND(UA_LOG_ERROR_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  231. #define UA_LOG_FATAL_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  232. UA_LOG_FATAL(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  233. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  234. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  235. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  236. #define UA_LOG_FATAL_CHANNEL(LOGGER, CHANNEL, ...) \
  237. UA_MACRO_EXPAND(UA_LOG_FATAL_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  238. _UA_END_DECLS
  239. #endif /* UA_SECURECHANNEL_H_ */