ua_securechannel.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "ua_securechannel.h"
  2. #include "ua_util.h"
  3. #include "ua_statuscodes.h"
  4. #ifdef UA_MULTITHREADING
  5. #include <urcu/uatomic.h>
  6. #endif
  7. void UA_SecureChannel_init(UA_SecureChannel *channel) {
  8. UA_MessageSecurityMode_init(&channel->securityMode);
  9. UA_ChannelSecurityToken_init(&channel->securityToken);
  10. UA_AsymmetricAlgorithmSecurityHeader_init(&channel->clientAsymAlgSettings);
  11. UA_AsymmetricAlgorithmSecurityHeader_init(&channel->serverAsymAlgSettings);
  12. UA_ByteString_init(&channel->clientNonce);
  13. UA_ByteString_init(&channel->serverNonce);
  14. channel->requestId = 0;
  15. channel->sequenceNumber = 0;
  16. channel->connection = UA_NULL;
  17. channel->session = UA_NULL;
  18. }
  19. void UA_SecureChannel_deleteMembers(UA_SecureChannel *channel) {
  20. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&channel->serverAsymAlgSettings);
  21. UA_ByteString_deleteMembers(&channel->serverNonce);
  22. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&channel->clientAsymAlgSettings);
  23. UA_ByteString_deleteMembers(&channel->clientNonce);
  24. UA_ChannelSecurityToken_deleteMembers(&channel->securityToken);
  25. }
  26. void UA_SecureChannel_delete(UA_SecureChannel *channel) {
  27. UA_SecureChannel_deleteMembers(channel);
  28. UA_free(channel);
  29. }
  30. UA_Boolean UA_SecureChannel_compare(UA_SecureChannel *sc1, UA_SecureChannel *sc2) {
  31. return (sc1->securityToken.channelId == sc2->securityToken.channelId);
  32. }
  33. //TODO implement real nonce generator - DUMMY function
  34. UA_StatusCode UA_SecureChannel_generateNonce(UA_ByteString *nonce) {
  35. if(!(nonce->data = UA_malloc(1)))
  36. return UA_STATUSCODE_BADOUTOFMEMORY;
  37. nonce->length = 1;
  38. nonce->data[0] = 'a';
  39. return UA_STATUSCODE_GOOD;
  40. }
  41. UA_StatusCode UA_SecureChannel_updateRequestId(UA_SecureChannel *channel, UA_UInt32 requestId) {
  42. //TODO review checking of request id
  43. if(channel->requestId+1 != requestId)
  44. return UA_STATUSCODE_BADINTERNALERROR;
  45. channel->requestId++;
  46. return UA_STATUSCODE_GOOD;
  47. }
  48. UA_Int32 UA_SecureChannel_updateSequenceNumber(UA_SecureChannel *channel, UA_UInt32 sequenceNumber) {
  49. //TODO review checking of sequence
  50. if(channel->sequenceNumber+1 != sequenceNumber)
  51. return UA_STATUSCODE_BADINTERNALERROR;
  52. channel->sequenceNumber++;
  53. return UA_STATUSCODE_GOOD;
  54. }
  55. void UA_Connection_detachSecureChannel(UA_Connection *connection) {
  56. #ifdef UA_MULTITHREADING
  57. UA_SecureChannel *channel = connection->channel;
  58. if(channel)
  59. uatomic_cmpxchg(&channel->connection, connection, UA_NULL);
  60. uatomic_set(&connection->channel, UA_NULL);
  61. #else
  62. if(connection->channel)
  63. connection->channel->connection = UA_NULL;
  64. connection->channel = UA_NULL;
  65. #endif
  66. }
  67. void UA_Connection_attachSecureChannel(UA_Connection *connection, UA_SecureChannel *channel) {
  68. #ifdef UA_MULTITHREADING
  69. if(uatomic_cmpxchg(&channel->connection, UA_NULL, connection) == UA_NULL)
  70. uatomic_set(&connection->channel, channel);
  71. #else
  72. if(channel->connection != UA_NULL)
  73. return;
  74. channel->connection = connection;
  75. connection->channel = channel;
  76. #endif
  77. }