ua_securechannel.c 2.7 KB

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