ua_securechannel_manager.c 5.8 KB

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