ua_securechannel.c 3.2 KB

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