ua_securechannel.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 <open62541/plugin/log.h>
  13. #include <open62541/plugin/securitypolicy.h>
  14. #include <open62541/transport_generated.h>
  15. #include <open62541/types.h>
  16. #include "open62541_queue.h"
  17. #include "ua_connection_internal.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. void UA_SecureChannel_close(UA_SecureChannel *channel);
  92. UA_StatusCode
  93. UA_SecureChannel_setSecurityPolicy(UA_SecureChannel *channel,
  94. const UA_SecurityPolicy *securityPolicy,
  95. const UA_ByteString *remoteCertificate);
  96. /* Remove (partially) received unprocessed messages */
  97. void UA_SecureChannel_deleteMessages(UA_SecureChannel *channel);
  98. void UA_SecureChannel_deleteMembers(UA_SecureChannel *channel);
  99. /* Generates new keys and sets them in the channel context */
  100. UA_StatusCode
  101. UA_SecureChannel_generateNewKeys(UA_SecureChannel* channel);
  102. /* Wrapper function for generating a local nonce for the supplied channel. Uses
  103. * the random generator of the channels security policy to allocate and generate
  104. * a nonce with the specified length. */
  105. UA_StatusCode
  106. UA_SecureChannel_generateLocalNonce(UA_SecureChannel *channel);
  107. UA_SessionHeader *
  108. UA_SecureChannel_getSession(UA_SecureChannel *channel,
  109. const UA_NodeId *authenticationToken);
  110. UA_StatusCode
  111. UA_SecureChannel_revolveTokens(UA_SecureChannel *channel);
  112. /**
  113. * Sending Messages
  114. * ---------------- */
  115. UA_StatusCode
  116. UA_SecureChannel_sendAsymmetricOPNMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
  117. const void *content, const UA_DataType *contentType);
  118. UA_StatusCode
  119. UA_SecureChannel_sendSymmetricMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
  120. UA_MessageType messageType, void *payload,
  121. const UA_DataType *payloadType);
  122. /* The MessageContext is forwarded into the encoding layer so that we can send
  123. * chunks before continuing to encode. This lets us reuse a fixed chunk-sized
  124. * messages buffer. */
  125. typedef struct {
  126. UA_SecureChannel *channel;
  127. UA_UInt32 requestId;
  128. UA_UInt32 messageType;
  129. UA_UInt16 chunksSoFar;
  130. size_t messageSizeSoFar;
  131. UA_ByteString messageBuffer;
  132. UA_Byte *buf_pos;
  133. const UA_Byte *buf_end;
  134. UA_Boolean final;
  135. } UA_MessageContext;
  136. /* Start the context of a new symmetric message. */
  137. UA_StatusCode
  138. UA_MessageContext_begin(UA_MessageContext *mc, UA_SecureChannel *channel,
  139. UA_UInt32 requestId, UA_MessageType messageType);
  140. /* Encode the content and send out full chunks. If the return code is good, then
  141. * the ChunkInfo contains encoded content that has not been sent. If the return
  142. * code is bad, then the ChunkInfo has been cleaned up internally. */
  143. UA_StatusCode
  144. UA_MessageContext_encode(UA_MessageContext *mc, const void *content,
  145. const UA_DataType *contentType);
  146. /* Sends a symmetric message already encoded in the context. The context is
  147. * cleaned up, also in case of errors. */
  148. UA_StatusCode
  149. UA_MessageContext_finish(UA_MessageContext *mc);
  150. /* To be used when a failure occures when a MessageContext is open. Note that
  151. * the _encode and _finish methods will clean up internally. _abort can be run
  152. * on a MessageContext that has already been cleaned up before. */
  153. void
  154. UA_MessageContext_abort(UA_MessageContext *mc);
  155. /**
  156. * Receive Message
  157. * --------------- */
  158. /* Decrypt a chunk and add it to the message. Create a new message if necessary. */
  159. UA_StatusCode
  160. UA_SecureChannel_decryptAddChunk(UA_SecureChannel *channel, const UA_ByteString *chunk,
  161. UA_Boolean allowPreviousToken);
  162. /* The network buffer is about to be cleared. Copy all chunks that point into
  163. * the network buffer into dedicated memory. */
  164. UA_StatusCode
  165. UA_SecureChannel_persistIncompleteMessages(UA_SecureChannel *channel);
  166. typedef void
  167. (UA_ProcessMessageCallback)(void *application, UA_SecureChannel *channel,
  168. UA_MessageType messageType, UA_UInt32 requestId,
  169. const UA_ByteString *message);
  170. /* Process received complete messages in-order. The callback function is called
  171. * with the complete message body if the message is complete. The message is
  172. * removed afterwards.
  173. *
  174. * Symmetric callback is ERR, MSG, CLO only
  175. * Asymmetric callback is OPN only
  176. *
  177. * @param channel the channel the chunks were received on.
  178. * @param application data pointer to application specific data that gets passed
  179. * on to the callback function.
  180. * @param callback the callback function that gets called with the complete
  181. * message body, once a final chunk is processed.
  182. * @return Returns if an irrecoverable error occured. Maybe close the channel. */
  183. UA_StatusCode
  184. UA_SecureChannel_processCompleteMessages(UA_SecureChannel *channel, void *application,
  185. UA_ProcessMessageCallback callback);
  186. /**
  187. * Log Helper
  188. * ----------
  189. * C99 requires at least one element for the variadic argument. If the log
  190. * statement has no variable arguments, supply an additional NULL. It will be
  191. * ignored by printf.
  192. *
  193. * We have to jump through some hoops to enable the use of format strings
  194. * without arguments since (pedantic) C99 does not allow variadic macros with
  195. * zero arguments. So we add a dummy argument that is not printed (%.0s is
  196. * string of length zero). */
  197. #define UA_LOG_TRACE_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  198. UA_LOG_TRACE(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  199. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  200. ((CHANNEL)->connection ? (int)((CHANNEL)->connection->sockfd) : 0), \
  201. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  202. #define UA_LOG_TRACE_CHANNEL(LOGGER, CHANNEL, ...) \
  203. UA_MACRO_EXPAND(UA_LOG_TRACE_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  204. #define UA_LOG_DEBUG_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  205. UA_LOG_DEBUG(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  206. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  207. ((CHANNEL)->connection ? (int)((CHANNEL)->connection->sockfd) : 0), \
  208. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  209. #define UA_LOG_DEBUG_CHANNEL(LOGGER, CHANNEL, ...) \
  210. UA_MACRO_EXPAND(UA_LOG_DEBUG_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  211. #define UA_LOG_INFO_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  212. UA_LOG_INFO(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  213. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  214. ((CHANNEL)->connection ? (int)((CHANNEL)->connection->sockfd) : 0), \
  215. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  216. #define UA_LOG_INFO_CHANNEL(LOGGER, CHANNEL, ...) \
  217. UA_MACRO_EXPAND(UA_LOG_INFO_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  218. #define UA_LOG_WARNING_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  219. UA_LOG_WARNING(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  220. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  221. ((CHANNEL)->connection ? (int)((CHANNEL)->connection->sockfd) : 0), \
  222. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  223. #define UA_LOG_WARNING_CHANNEL(LOGGER, CHANNEL, ...) \
  224. UA_MACRO_EXPAND(UA_LOG_WARNING_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  225. #define UA_LOG_ERROR_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  226. UA_LOG_ERROR(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  227. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  228. ((CHANNEL)->connection ? (int)((CHANNEL)->connection->sockfd) : 0), \
  229. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  230. #define UA_LOG_ERROR_CHANNEL(LOGGER, CHANNEL, ...) \
  231. UA_MACRO_EXPAND(UA_LOG_ERROR_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  232. #define UA_LOG_FATAL_CHANNEL_INTERNAL(LOGGER, CHANNEL, MSG, ...) \
  233. UA_LOG_FATAL(LOGGER, UA_LOGCATEGORY_SECURECHANNEL, \
  234. "Connection %i | SecureChannel %i | " MSG "%.0s", \
  235. ((CHANNEL)->connection ? (CHANNEL)->connection->sockfd : 0), \
  236. (CHANNEL)->securityToken.channelId, __VA_ARGS__)
  237. #define UA_LOG_FATAL_CHANNEL(LOGGER, CHANNEL, ...) \
  238. UA_MACRO_EXPAND(UA_LOG_FATAL_CHANNEL_INTERNAL(LOGGER, CHANNEL, __VA_ARGS__, ""))
  239. _UA_END_DECLS
  240. #endif /* UA_SECURECHANNEL_H_ */