ua_securechannel_manager.c 5.8 KB

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