ua_securechannel_manager.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include "ua_securechannel_manager.h"
  5. #include "ua_session.h"
  6. #include "ua_server_internal.h"
  7. #define STARTCHANNELID 1
  8. #define STARTTOKENID 1
  9. UA_StatusCode
  10. UA_SecureChannelManager_init(UA_SecureChannelManager *cm, UA_Server *server) {
  11. LIST_INIT(&cm->channels);
  12. cm->lastChannelId = STARTCHANNELID;
  13. cm->lastTokenId = STARTTOKENID;
  14. cm->currentChannelCount = 0;
  15. cm->server = server;
  16. return UA_STATUSCODE_GOOD;
  17. }
  18. void UA_SecureChannelManager_deleteMembers(UA_SecureChannelManager *cm) {
  19. channel_list_entry *entry, *temp;
  20. LIST_FOREACH_SAFE(entry, &cm->channels, pointers, temp) {
  21. LIST_REMOVE(entry, pointers);
  22. UA_SecureChannel_deleteMembersCleanup(&entry->channel);
  23. UA_free(entry);
  24. }
  25. }
  26. static void removeSecureChannel(UA_SecureChannelManager *cm, channel_list_entry *entry){
  27. LIST_REMOVE(entry, pointers);
  28. UA_atomic_add(&cm->currentChannelCount, (UA_UInt32)-1);
  29. UA_SecureChannel_deleteMembersCleanup(&entry->channel);
  30. #ifndef UA_ENABLE_MULTITHREADING
  31. UA_free(entry);
  32. #else
  33. UA_Server_delayedFree(cm->server, entry);
  34. #endif
  35. }
  36. /* remove channels that were not renewed or who have no connection attached */
  37. void UA_SecureChannelManager_cleanupTimedOut(UA_SecureChannelManager *cm, UA_DateTime nowMonotonic) {
  38. channel_list_entry *entry, *temp;
  39. LIST_FOREACH_SAFE(entry, &cm->channels, pointers, temp) {
  40. UA_DateTime timeout = entry->channel.securityToken.createdAt +
  41. (UA_DateTime)(entry->channel.securityToken.revisedLifetime * UA_MSEC_TO_DATETIME);
  42. if(timeout < nowMonotonic || !entry->channel.connection) {
  43. UA_LOG_DEBUG_CHANNEL(cm->server->config.logger, &entry->channel, "SecureChannel has timed out");
  44. removeSecureChannel(cm, entry);
  45. } else if(entry->channel.nextSecurityToken.tokenId > 0) {
  46. UA_SecureChannel_revolveTokens(&entry->channel);
  47. }
  48. }
  49. }
  50. /* remove the first channel that has no session attached */
  51. static UA_Boolean purgeFirstChannelWithoutSession(UA_SecureChannelManager *cm) {
  52. channel_list_entry *entry;
  53. LIST_FOREACH(entry, &cm->channels, pointers) {
  54. if(LIST_EMPTY(&(entry->channel.sessions))){
  55. UA_LOG_DEBUG_CHANNEL(cm->server->config.logger, &entry->channel,
  56. "Channel was purged since maxSecureChannels was reached and channel had no session attached");
  57. removeSecureChannel(cm, entry);
  58. UA_assert(entry != LIST_FIRST(&cm->channels));
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. UA_StatusCode
  65. UA_SecureChannelManager_open(UA_SecureChannelManager *cm, UA_Connection *conn,
  66. const UA_OpenSecureChannelRequest *request,
  67. UA_OpenSecureChannelResponse *response) {
  68. if(request->securityMode != UA_MESSAGESECURITYMODE_NONE)
  69. return UA_STATUSCODE_BADSECURITYMODEREJECTED;
  70. //check if there exists a free SC, otherwise try to purge one SC without a session
  71. //the purge has been introduced to pass CTT, it is not clear what strategy is expected here
  72. if(cm->currentChannelCount >= cm->server->config.maxSecureChannels && !purgeFirstChannelWithoutSession(cm)){
  73. return UA_STATUSCODE_BADOUTOFMEMORY;
  74. }
  75. /* Set up the channel */
  76. channel_list_entry *entry = (channel_list_entry*)UA_malloc(sizeof(channel_list_entry));
  77. if(!entry)
  78. return UA_STATUSCODE_BADOUTOFMEMORY;
  79. UA_SecureChannel_init(&entry->channel);
  80. entry->channel.securityToken.channelId = cm->lastChannelId++;
  81. entry->channel.securityToken.tokenId = cm->lastTokenId++;
  82. entry->channel.securityToken.createdAt = UA_DateTime_now();
  83. entry->channel.securityToken.revisedLifetime =
  84. (request->requestedLifetime > cm->server->config.maxSecurityTokenLifetime) ?
  85. cm->server->config.maxSecurityTokenLifetime : request->requestedLifetime;
  86. if(entry->channel.securityToken.revisedLifetime == 0) /* lifetime 0 -> set the maximum possible */
  87. entry->channel.securityToken.revisedLifetime = cm->server->config.maxSecurityTokenLifetime;
  88. UA_ByteString_copy(&request->clientNonce, &entry->channel.clientNonce);
  89. entry->channel.serverAsymAlgSettings.securityPolicyUri =
  90. UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#None");
  91. UA_SecureChannel_generateNonce(&entry->channel.serverNonce);
  92. /* Set the response */
  93. UA_ByteString_copy(&entry->channel.serverNonce, &response->serverNonce);
  94. UA_ChannelSecurityToken_copy(&entry->channel.securityToken, &response->securityToken);
  95. response->responseHeader.timestamp = UA_DateTime_now();
  96. /* Now overwrite the creation date with the internal monotonic clock */
  97. entry->channel.securityToken.createdAt = UA_DateTime_nowMonotonic();
  98. /* Set all the pointers internally */
  99. UA_Connection_attachSecureChannel(conn, &entry->channel);
  100. LIST_INSERT_HEAD(&cm->channels, entry, pointers);
  101. UA_atomic_add(&cm->currentChannelCount, 1);
  102. return UA_STATUSCODE_GOOD;
  103. }
  104. UA_StatusCode
  105. UA_SecureChannelManager_renew(UA_SecureChannelManager *cm, UA_Connection *conn,
  106. const UA_OpenSecureChannelRequest *request,
  107. UA_OpenSecureChannelResponse *response) {
  108. UA_SecureChannel *channel = conn->channel;
  109. if(!channel)
  110. return UA_STATUSCODE_BADINTERNALERROR;
  111. /* if no security token is already issued */
  112. if(channel->nextSecurityToken.tokenId == 0) {
  113. channel->nextSecurityToken.channelId = channel->securityToken.channelId;
  114. channel->nextSecurityToken.tokenId = cm->lastTokenId++;
  115. channel->nextSecurityToken.createdAt = UA_DateTime_now();
  116. channel->nextSecurityToken.revisedLifetime =
  117. (request->requestedLifetime > cm->server->config.maxSecurityTokenLifetime) ?
  118. cm->server->config.maxSecurityTokenLifetime : request->requestedLifetime;
  119. if(channel->nextSecurityToken.revisedLifetime == 0) /* lifetime 0 -> return the max lifetime */
  120. channel->nextSecurityToken.revisedLifetime = cm->server->config.maxSecurityTokenLifetime;
  121. }
  122. /* invalidate the old nonce */
  123. if(channel->clientNonce.data)
  124. UA_ByteString_deleteMembers(&channel->clientNonce);
  125. /* set the response */
  126. UA_ByteString_copy(&request->clientNonce, &channel->clientNonce);
  127. UA_ByteString_copy(&channel->serverNonce, &response->serverNonce);
  128. UA_ChannelSecurityToken_copy(&channel->nextSecurityToken, &response->securityToken);
  129. /* reset the creation date to the monotonic clock */
  130. channel->nextSecurityToken.createdAt = UA_DateTime_nowMonotonic();
  131. return UA_STATUSCODE_GOOD;
  132. }
  133. UA_SecureChannel *
  134. UA_SecureChannelManager_get(UA_SecureChannelManager *cm, UA_UInt32 channelId) {
  135. channel_list_entry *entry;
  136. LIST_FOREACH(entry, &cm->channels, pointers) {
  137. if(entry->channel.securityToken.channelId == channelId)
  138. return &entry->channel;
  139. }
  140. return NULL;
  141. }
  142. UA_StatusCode
  143. UA_SecureChannelManager_close(UA_SecureChannelManager *cm, UA_UInt32 channelId) {
  144. channel_list_entry *entry;
  145. LIST_FOREACH(entry, &cm->channels, pointers) {
  146. if(entry->channel.securityToken.channelId == channelId)
  147. break;
  148. }
  149. if(!entry)
  150. return UA_STATUSCODE_BADINTERNALERROR;
  151. removeSecureChannel(cm, entry);
  152. return UA_STATUSCODE_GOOD;
  153. }