ua_securechannel_manager.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "ua_securechannel_manager.h"
  2. #include "ua_session.h"
  3. #include "ua_statuscodes.h"
  4. struct channel_list_entry {
  5. UA_SecureChannel channel;
  6. LIST_ENTRY(channel_list_entry) pointers;
  7. };
  8. UA_StatusCode UA_SecureChannelManager_init(UA_SecureChannelManager *cm, UA_UInt32 maxChannelCount,
  9. UA_UInt32 tokenLifetime, UA_UInt32 startChannelId,
  10. UA_UInt32 startTokenId) {
  11. LIST_INIT(&cm->channels);
  12. cm->lastChannelId = startChannelId;
  13. cm->lastTokenId = startTokenId;
  14. cm->maxChannelLifetime = tokenLifetime;
  15. cm->maxChannelCount = maxChannelCount;
  16. return UA_STATUSCODE_GOOD;
  17. }
  18. void UA_SecureChannelManager_deleteMembers(UA_SecureChannelManager *cm) {
  19. struct channel_list_entry *current;
  20. struct channel_list_entry *next = LIST_FIRST(&cm->channels);
  21. while(next) {
  22. current = next;
  23. next = LIST_NEXT(current, pointers);
  24. LIST_REMOVE(current, pointers);
  25. if(current->channel.session)
  26. current->channel.session->channel = UA_NULL;
  27. if(current->channel.connection)
  28. current->channel.connection->channel = UA_NULL;
  29. UA_SecureChannel_deleteMembers(&current->channel);
  30. UA_free(current);
  31. }
  32. }
  33. UA_StatusCode UA_SecureChannelManager_open(UA_SecureChannelManager *cm,
  34. UA_Connection *conn,
  35. const UA_OpenSecureChannelRequest *request,
  36. UA_OpenSecureChannelResponse *response) {
  37. switch(request->securityMode) {
  38. case UA_MESSAGESECURITYMODE_INVALID:
  39. response->responseHeader.serviceResult = UA_STATUSCODE_BADSECURITYMODEREJECTED;
  40. return response->responseHeader.serviceResult;
  41. // fall through and handle afterwards
  42. /* case UA_MESSAGESECURITYMODE_NONE: */
  43. /* UA_ByteString_copy(&request->clientNonce, &entry->channel.clientNonce); */
  44. /* break; */
  45. case UA_MESSAGESECURITYMODE_SIGN:
  46. case UA_MESSAGESECURITYMODE_SIGNANDENCRYPT:
  47. response->responseHeader.serviceResult = UA_STATUSCODE_BADSECURITYMODEREJECTED;
  48. return response->responseHeader.serviceResult;
  49. default:
  50. // do nothing
  51. break;
  52. }
  53. struct channel_list_entry *entry = UA_malloc(sizeof(struct channel_list_entry));
  54. if(!entry) return UA_STATUSCODE_BADOUTOFMEMORY;
  55. UA_SecureChannel_init(&entry->channel);
  56. response->responseHeader.stringTableSize = 0;
  57. response->responseHeader.timestamp = UA_DateTime_now();
  58. entry->channel.connection = conn;
  59. conn->channel = &entry->channel;
  60. entry->channel.securityToken.channelId = cm->lastChannelId++;
  61. entry->channel.securityToken.tokenId = cm->lastTokenId++;
  62. entry->channel.securityToken.createdAt = UA_DateTime_now();
  63. entry->channel.securityToken.revisedLifetime =
  64. request->requestedLifetime > cm->maxChannelLifetime ?
  65. cm->maxChannelLifetime : request->requestedLifetime;
  66. UA_ByteString_copy(&request->clientNonce, &entry->channel.clientNonce);
  67. entry->channel.serverAsymAlgSettings.securityPolicyUri =
  68. UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#None");
  69. LIST_INSERT_HEAD(&cm->channels, entry, pointers);
  70. response->serverProtocolVersion = 0;
  71. UA_SecureChannel_generateNonce(&entry->channel.serverNonce);
  72. UA_ByteString_copy(&entry->channel.serverNonce, &response->serverNonce);
  73. UA_ChannelSecurityToken_copy(&entry->channel.securityToken, &response->securityToken);
  74. conn->channel = &entry->channel;
  75. return UA_STATUSCODE_GOOD;
  76. }
  77. UA_StatusCode UA_SecureChannelManager_renew(UA_SecureChannelManager *cm,
  78. UA_Connection *conn,
  79. const UA_OpenSecureChannelRequest *request,
  80. UA_OpenSecureChannelResponse *response) {
  81. UA_SecureChannel *channel = conn->channel;
  82. if(channel == UA_NULL) return UA_STATUSCODE_BADINTERNALERROR;
  83. channel->securityToken.tokenId = cm->lastTokenId++;
  84. channel->securityToken.createdAt = UA_DateTime_now(); // todo: is wanted?
  85. channel->securityToken.revisedLifetime = request->requestedLifetime > cm->maxChannelLifetime ?
  86. cm->maxChannelLifetime : request->requestedLifetime;
  87. if(channel->serverNonce.data != UA_NULL)
  88. UA_ByteString_deleteMembers(&channel->serverNonce);
  89. UA_SecureChannel_generateNonce(&channel->serverNonce);
  90. UA_ByteString_copy(&channel->serverNonce, &response->serverNonce);
  91. UA_ChannelSecurityToken_copy(&channel->securityToken, &response->securityToken);
  92. return UA_STATUSCODE_GOOD;
  93. }
  94. UA_SecureChannel * UA_SecureChannelManager_get(UA_SecureChannelManager *cm, UA_UInt32 channelId) {
  95. struct channel_list_entry *entry;
  96. LIST_FOREACH(entry, &cm->channels, pointers) {
  97. if(entry->channel.securityToken.channelId == channelId)
  98. return &entry->channel;
  99. }
  100. return UA_NULL;
  101. }
  102. UA_StatusCode UA_SecureChannelManager_close(UA_SecureChannelManager *cm, UA_UInt32 channelId) {
  103. //TODO lock access
  104. // TODO: close the binaryconnection if it is still open. So we dö not have stray pointers..
  105. struct channel_list_entry *entry;
  106. LIST_FOREACH(entry, &cm->channels, pointers) {
  107. if(entry->channel.securityToken.channelId == channelId) {
  108. if(entry->channel.connection)
  109. entry->channel.connection->channel = UA_NULL; // remove pointer back to the channel
  110. if(entry->channel.session)
  111. entry->channel.session->channel = UA_NULL; // remove ponter back to the channel
  112. UA_SecureChannel_deleteMembers(&entry->channel);
  113. LIST_REMOVE(entry, pointers);
  114. UA_free(entry);
  115. return UA_STATUSCODE_GOOD;
  116. }
  117. }
  118. //TODO notify server application that secureChannel has been closed part 6 - §7.1.4
  119. return UA_STATUSCODE_BADINTERNALERROR;
  120. }