ua_securechannel.c 2.9 KB

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