ua_securechannel_manager.c 6.0 KB

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