ua_securechannel_manager.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "ua_securechannel_manager.h"
  2. #include "ua_session.h"
  3. #include "ua_util.h"
  4. struct channel_list_entry {
  5. UA_SecureChannel channel;
  6. LIST_ENTRY(channel_list_entry) pointers;
  7. };
  8. struct UA_SecureChannelManager {
  9. UA_Int32 maxChannelCount;
  10. UA_DateTime maxChannelLifetime;
  11. LIST_HEAD(channel_list, channel_list_entry) channels;
  12. UA_MessageSecurityMode securityMode;
  13. UA_String endpointUrl;
  14. UA_DateTime channelLifeTime;
  15. UA_Int32 lastChannelId;
  16. UA_UInt32 lastTokenId;
  17. };
  18. UA_Int32 UA_SecureChannelManager_new(UA_SecureChannelManager **cm, UA_UInt32 maxChannelCount,
  19. UA_UInt32 tokenLifetime, UA_UInt32 startChannelId,
  20. UA_UInt32 startTokenId, UA_String *endpointUrl) {
  21. UA_alloc((void **)cm, sizeof(UA_SecureChannelManager));
  22. UA_SecureChannelManager *channelManager = *cm;
  23. LIST_INIT(&channelManager->channels);
  24. channelManager->lastChannelId = startChannelId;
  25. channelManager->lastTokenId = startTokenId;
  26. UA_String_copy(endpointUrl, &channelManager->endpointUrl);
  27. channelManager->maxChannelLifetime = tokenLifetime;
  28. channelManager->maxChannelCount = maxChannelCount;
  29. return UA_SUCCESS;
  30. }
  31. UA_Int32 UA_SecureChannelManager_delete(UA_SecureChannelManager *cm) {
  32. struct channel_list_entry *entry;
  33. LIST_FOREACH(entry, &cm->channels, pointers) {
  34. LIST_REMOVE(entry, pointers);
  35. if(entry->channel.session)
  36. entry->channel.session->channel = UA_NULL;
  37. if(entry->channel.connection)
  38. entry->channel.connection->channel = UA_NULL;
  39. UA_SecureChannel_deleteMembers(&entry->channel);
  40. UA_free(entry);
  41. }
  42. UA_String_deleteMembers(&cm->endpointUrl);
  43. UA_free(cm);
  44. return UA_SUCCESS;
  45. }
  46. UA_Int32 UA_SecureChannelManager_open(UA_SecureChannelManager *cm,
  47. UA_Connection *conn,
  48. const UA_OpenSecureChannelRequest *request,
  49. UA_OpenSecureChannelResponse *response) {
  50. struct channel_list_entry *entry;
  51. UA_alloc((void **)&entry, sizeof(struct channel_list_entry));
  52. UA_SecureChannel_init(&entry->channel);
  53. entry->channel.connection = conn;
  54. entry->channel.securityToken.channelId = cm->lastChannelId++;
  55. entry->channel.securityToken.tokenId = cm->lastTokenId++;
  56. entry->channel.securityToken.createdAt = UA_DateTime_now();
  57. entry->channel.securityToken.revisedLifetime =
  58. request->requestedLifetime > cm->maxChannelLifetime ?
  59. cm->maxChannelLifetime : request->requestedLifetime;
  60. switch(request->securityMode) {
  61. case UA_SECURITYMODE_INVALID:
  62. printf("UA_SecureChannel_processOpenRequest - client demands invalid \n");
  63. break;
  64. case UA_SECURITYMODE_NONE:
  65. UA_ByteString_copy(&request->clientNonce, &entry->channel.clientNonce);
  66. break;
  67. case UA_SECURITYMODE_SIGNANDENCRYPT:
  68. printf("UA_SecureChannel_processOpenRequest - client demands signed & encrypted \n");
  69. //TODO check if senderCertificate and ReceiverCertificateThumbprint are present
  70. break;
  71. }
  72. UA_String_copycstring("http://opcfoundation.org/UA/SecurityPolicy#None",
  73. (UA_String *)&entry->channel.serverAsymAlgSettings.securityPolicyUri);
  74. LIST_INSERT_HEAD(&cm->channels, entry, pointers);
  75. response->serverProtocolVersion = 0;
  76. UA_SecureChannel_generateNonce(&entry->channel.serverNonce);
  77. UA_ByteString_copy(&entry->channel.serverNonce, &response->serverNonce);
  78. UA_ChannelSecurityToken_copy(&entry->channel.securityToken, &response->securityToken);
  79. conn->channel = &entry->channel;
  80. return UA_SUCCESS;
  81. }
  82. UA_Int32 UA_SecureChannelManager_renew(UA_SecureChannelManager *cm,
  83. UA_Connection *conn,
  84. const UA_OpenSecureChannelRequest *request,
  85. UA_OpenSecureChannelResponse *response) {
  86. UA_SecureChannel *channel = conn->channel;
  87. if(channel == UA_NULL)
  88. return UA_ERROR;
  89. channel->securityToken.tokenId = cm->lastTokenId++;
  90. channel->securityToken.createdAt = UA_DateTime_now(); // todo: is wanted?
  91. channel->securityToken.revisedLifetime = request->requestedLifetime > cm->maxChannelLifetime ?
  92. cm->maxChannelLifetime : request->requestedLifetime;
  93. UA_SecureChannel_generateNonce(&channel->serverNonce);
  94. UA_ByteString_copy(&channel->serverNonce, &response->serverNonce);
  95. UA_ChannelSecurityToken_copy(&channel->securityToken, &response->securityToken);
  96. return UA_SUCCESS;
  97. }
  98. UA_Int32 UA_SecureChannelManager_get(UA_SecureChannelManager *cm, UA_UInt32 channelId,
  99. UA_SecureChannel **channel) {
  100. struct channel_list_entry *entry;
  101. LIST_FOREACH(entry, &cm->channels, pointers) {
  102. if(entry->channel.securityToken.channelId == channelId) {
  103. *channel = &entry->channel;
  104. return UA_SUCCESS;
  105. }
  106. }
  107. *channel = UA_NULL;
  108. return UA_ERROR;
  109. }
  110. UA_Int32 UA_SecureChannelManager_close(UA_SecureChannelManager *cm, UA_UInt32 channelId) {
  111. //TODO lock access
  112. // TODO: close the binaryconnection if it is still open. So we dö not have stray pointers..
  113. struct channel_list_entry *entry;
  114. LIST_FOREACH(entry, &cm->channels, pointers) {
  115. if(entry->channel.securityToken.channelId == channelId) {
  116. UA_SecureChannel_deleteMembers(&entry->channel);
  117. LIST_REMOVE(entry, pointers);
  118. UA_free(entry);
  119. return UA_SUCCESS;
  120. }
  121. }
  122. //TODO notify server application that secureChannel has been closed part 6 - §7.1.4
  123. return UA_ERROR;
  124. }