123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- #include "ua_util.h"
- #include "ua_securechannel.h"
- #include "ua_session.h"
- #include "ua_types_encoding_binary.h"
- #include "ua_types_generated_encoding_binary.h"
- #include "ua_transport_generated_encoding_binary.h"
- #include "ua_types_generated_handling.h"
- #include "ua_transport_generated_handling.h"
- #define UA_SECURE_MESSAGE_HEADER_LENGTH 24
- void UA_SecureChannel_init(UA_SecureChannel *channel) {
- memset(channel, 0, sizeof(UA_SecureChannel));
-
-
-
- }
- void UA_SecureChannel_deleteMembersCleanup(UA_SecureChannel *channel) {
-
- UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&channel->serverAsymAlgSettings);
- UA_ByteString_deleteMembers(&channel->serverNonce);
- UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&channel->clientAsymAlgSettings);
- UA_ByteString_deleteMembers(&channel->clientNonce);
- UA_ChannelSecurityToken_deleteMembers(&channel->securityToken);
- UA_ChannelSecurityToken_deleteMembers(&channel->nextSecurityToken);
-
- if(channel->connection)
- UA_Connection_detachSecureChannel(channel->connection);
-
- struct SessionEntry *se, *temp;
- LIST_FOREACH_SAFE(se, &channel->sessions, pointers, temp) {
- if(se->session)
- se->session->channel = NULL;
- LIST_REMOVE(se, pointers);
- UA_free(se);
- }
-
- struct ChunkEntry *ch, *temp_ch;
- LIST_FOREACH_SAFE(ch, &channel->chunks, pointers, temp_ch) {
- UA_ByteString_deleteMembers(&ch->bytes);
- LIST_REMOVE(ch, pointers);
- UA_free(ch);
- }
- }
- UA_StatusCode UA_SecureChannel_generateNonce(UA_ByteString *nonce) {
- if(!(nonce->data = UA_malloc(1)))
- return UA_STATUSCODE_BADOUTOFMEMORY;
- nonce->length = 1;
- nonce->data[0] = 'a';
- return UA_STATUSCODE_GOOD;
- }
- #if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 6)
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wextra"
- #pragma GCC diagnostic ignored "-Wcast-qual"
- #pragma GCC diagnostic ignored "-Wunused-value"
- #endif
- void UA_SecureChannel_attachSession(UA_SecureChannel *channel, UA_Session *session) {
- struct SessionEntry *se = UA_malloc(sizeof(struct SessionEntry));
- if(!se)
- return;
- se->session = session;
- if(UA_atomic_cmpxchg((void**)&session->channel, NULL, channel) != NULL) {
- UA_free(se);
- return;
- }
- LIST_INSERT_HEAD(&channel->sessions, se, pointers);
- }
- #if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 6)
- #pragma GCC diagnostic pop
- #endif
- void UA_SecureChannel_detachSession(UA_SecureChannel *channel, UA_Session *session) {
- if(session)
- session->channel = NULL;
- struct SessionEntry *se;
- LIST_FOREACH(se, &channel->sessions, pointers) {
- if(se->session == session)
- break;
- }
- if(!se)
- return;
- LIST_REMOVE(se, pointers);
- UA_free(se);
- }
- UA_Session * UA_SecureChannel_getSession(UA_SecureChannel *channel, UA_NodeId *token) {
- struct SessionEntry *se;
- LIST_FOREACH(se, &channel->sessions, pointers) {
- if(UA_NodeId_equal(&se->session->authenticationToken, token))
- break;
- }
- if(!se)
- return NULL;
- return se->session;
- }
- void UA_SecureChannel_revolveTokens(UA_SecureChannel *channel) {
- if(channel->nextSecurityToken.tokenId == 0)
- return;
-
- memcpy(&channel->securityToken, &channel->nextSecurityToken,
- sizeof(UA_ChannelSecurityToken));
- UA_ChannelSecurityToken_init(&channel->nextSecurityToken);
- }
- static UA_StatusCode
- UA_SecureChannel_sendChunk(UA_ChunkInfo *ci, UA_ByteString *dst, size_t offset) {
- UA_SecureChannel *channel = ci->channel;
- UA_Connection *connection = channel->connection;
- if(!connection)
- return UA_STATUSCODE_BADINTERNALERROR;
-
- dst->data = &dst->data[-UA_SECURE_MESSAGE_HEADER_LENGTH];
- dst->length += UA_SECURE_MESSAGE_HEADER_LENGTH;
- offset += UA_SECURE_MESSAGE_HEADER_LENGTH;
- if(ci->messageSizeSoFar + offset > connection->remoteConf.maxMessageSize &&
- connection->remoteConf.maxMessageSize > 0)
- ci->errorCode = UA_STATUSCODE_BADRESPONSETOOLARGE;
- if(++ci->chunksSoFar > connection->remoteConf.maxChunkCount &&
- connection->remoteConf.maxChunkCount > 0)
- ci->errorCode = UA_STATUSCODE_BADRESPONSETOOLARGE;
-
- UA_SecureConversationMessageHeader respHeader;
- respHeader.secureChannelId = channel->securityToken.channelId;
- respHeader.messageHeader.messageTypeAndChunkType = ci->messageType;
- if(ci->errorCode == UA_STATUSCODE_GOOD) {
- if(ci->final)
- respHeader.messageHeader.messageTypeAndChunkType += UA_CHUNKTYPE_FINAL;
- else
- respHeader.messageHeader.messageTypeAndChunkType += UA_CHUNKTYPE_INTERMEDIATE;
- } else {
-
- ci->final = true;
- respHeader.messageHeader.messageTypeAndChunkType += UA_CHUNKTYPE_ABORT;
- UA_String errorMsg;
- UA_String_init(&errorMsg);
- offset = UA_SECURE_MESSAGE_HEADER_LENGTH;
- UA_UInt32_encodeBinary(&ci->errorCode, dst, &offset);
- UA_String_encodeBinary(&errorMsg, dst, &offset);
- }
- respHeader.messageHeader.messageSize = (UA_UInt32)offset;
- ci->messageSizeSoFar += offset;
-
- UA_SymmetricAlgorithmSecurityHeader symSecHeader;
- symSecHeader.tokenId = channel->securityToken.tokenId;
- UA_SequenceHeader seqHeader;
- seqHeader.requestId = ci->requestId;
- seqHeader.sequenceNumber = UA_atomic_add(&channel->sendSequenceNumber, 1);
- size_t offset_header = 0;
- UA_SecureConversationMessageHeader_encodeBinary(&respHeader, dst, &offset_header);
- UA_SymmetricAlgorithmSecurityHeader_encodeBinary(&symSecHeader, dst, &offset_header);
- UA_SequenceHeader_encodeBinary(&seqHeader, dst, &offset_header);
-
- dst->length = offset;
- connection->send(channel->connection, dst);
-
- if(!ci->final) {
- UA_StatusCode retval =
- connection->getSendBuffer(connection, connection->localConf.sendBufferSize, dst);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
-
- dst->data = &dst->data[UA_SECURE_MESSAGE_HEADER_LENGTH];
- dst->length = connection->localConf.sendBufferSize - UA_SECURE_MESSAGE_HEADER_LENGTH;
- }
- return ci->errorCode;
- }
- UA_StatusCode
- UA_SecureChannel_sendBinaryMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
- const void *content, const UA_DataType *contentType) {
- UA_Connection *connection = channel->connection;
- if(!connection)
- return UA_STATUSCODE_BADINTERNALERROR;
-
- UA_ByteString message;
- UA_StatusCode retval =
- connection->getSendBuffer(connection, connection->localConf.sendBufferSize, &message);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
-
- message.data = &message.data[UA_SECURE_MESSAGE_HEADER_LENGTH];
- message.length -= UA_SECURE_MESSAGE_HEADER_LENGTH;
-
- size_t messagePos = 0;
- UA_NodeId typeId = contentType->typeId;
- typeId.identifier.numeric = contentType->binaryEncodingId;
- UA_NodeId_encodeBinary(&typeId, &message, &messagePos);
-
- UA_ChunkInfo ci;
- ci.channel = channel;
- ci.requestId = requestId;
- ci.chunksSoFar = 0;
- ci.messageSizeSoFar = 0;
- ci.final = false;
- ci.messageType = UA_MESSAGETYPE_MSG;
- ci.errorCode = UA_STATUSCODE_GOOD;
- if(typeId.identifier.numeric == 446 || typeId.identifier.numeric == 449)
- ci.messageType = UA_MESSAGETYPE_OPN;
- else if(typeId.identifier.numeric == 452 || typeId.identifier.numeric == 455)
- ci.messageType = UA_MESSAGETYPE_CLO;
- retval = UA_encodeBinary(content, contentType,
- (UA_exchangeEncodeBuffer)UA_SecureChannel_sendChunk,
- &ci, &message, &messagePos);
-
- if(retval != UA_STATUSCODE_GOOD) {
- if(!ci.final) {
-
- ci.errorCode = retval;
- UA_SecureChannel_sendChunk(&ci, &message, messagePos);
- }
- return retval;
- }
-
- ci.final = UA_TRUE;
- return UA_SecureChannel_sendChunk(&ci, &message, messagePos);
- }
- static void
- UA_SecureChannel_removeChunk(UA_SecureChannel *channel, UA_UInt32 requestId) {
- struct ChunkEntry *ch;
- LIST_FOREACH(ch, &channel->chunks, pointers) {
- if(ch->requestId == requestId) {
- UA_ByteString_deleteMembers(&ch->bytes);
- LIST_REMOVE(ch, pointers);
- UA_free(ch);
- return;
- }
- }
- }
- static void
- appendChunk(struct ChunkEntry *ch, const UA_ByteString *msg,
- size_t offset, size_t chunklength) {
- UA_Byte* new_bytes = UA_realloc(ch->bytes.data, ch->bytes.length + chunklength);
- if(!new_bytes) {
- UA_ByteString_deleteMembers(&ch->bytes);
- return;
- }
- ch->bytes.data = new_bytes;
- memcpy(&ch->bytes.data[ch->bytes.length], &msg->data[offset], chunklength);
- ch->bytes.length += chunklength;
- }
- static void
- UA_SecureChannel_appendChunk(UA_SecureChannel *channel, UA_UInt32 requestId,
- const UA_ByteString *msg, size_t offset,
- size_t chunklength) {
-
- if(msg->length - offset < chunklength) {
-
- UA_SecureChannel_removeChunk(channel, requestId);
- return;
- }
-
- struct ChunkEntry *ch;
- LIST_FOREACH(ch, &channel->chunks, pointers) {
- if(ch->requestId == requestId)
- break;
- }
-
- if(!ch) {
- ch = UA_malloc(sizeof(struct ChunkEntry));
- if(!ch)
- return;
- ch->requestId = requestId;
- UA_ByteString_init(&ch->bytes);
- LIST_INSERT_HEAD(&channel->chunks, ch, pointers);
- }
- appendChunk(ch, msg, offset, chunklength);
- }
- static UA_ByteString
- UA_SecureChannel_finalizeChunk(UA_SecureChannel *channel, UA_UInt32 requestId,
- const UA_ByteString *msg, size_t offset,
- size_t chunklength, UA_Boolean *deleteChunk) {
- if(msg->length - offset < chunklength) {
-
- UA_SecureChannel_removeChunk(channel, requestId);
- return UA_BYTESTRING_NULL;
- }
- struct ChunkEntry *ch;
- LIST_FOREACH(ch, &channel->chunks, pointers) {
- if(ch->requestId == requestId)
- break;
- }
- UA_ByteString bytes;
- if(!ch) {
- *deleteChunk = false;
- bytes.length = chunklength;
- bytes.data = msg->data + offset;
- } else {
- *deleteChunk = true;
- appendChunk(ch, msg, offset, chunklength);
- bytes = ch->bytes;
- LIST_REMOVE(ch, pointers);
- UA_free(ch);
- }
- return bytes;
- }
- static UA_StatusCode
- UA_SecureChannel_processSequenceNumber(UA_SecureChannel *channel, UA_UInt32 SequenceNumber) {
-
- if(SequenceNumber != channel->receiveSequenceNumber + 1) {
- if(channel->receiveSequenceNumber + 1 > 4294966271 && SequenceNumber < 1024)
- channel->receiveSequenceNumber = SequenceNumber - 1;
- else
- return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
- }
- ++channel->receiveSequenceNumber;
- return UA_STATUSCODE_GOOD;
- }
- UA_StatusCode
- UA_SecureChannel_processChunks(UA_SecureChannel *channel, const UA_ByteString *chunks,
- UA_ProcessMessageCallback callback, void *application) {
- UA_StatusCode retval = UA_STATUSCODE_GOOD;
- size_t offset= 0;
- do {
- if (chunks->length > 3 && chunks->data[offset] == 'E' &&
- chunks->data[offset+1] == 'R' && chunks->data[offset+2] == 'R') {
- UA_TcpMessageHeader header;
- retval = UA_TcpMessageHeader_decodeBinary(chunks, &offset, &header);
- if(retval != UA_STATUSCODE_GOOD)
- break;
- UA_TcpErrorMessage errorMessage;
- retval = UA_TcpErrorMessage_decodeBinary(chunks, &offset, &errorMessage);
- if(retval != UA_STATUSCODE_GOOD)
- break;
- callback(application, channel, UA_MESSAGETYPE_ERR, 0, (void*)&errorMessage);
- continue;
- }
-
- size_t initial_offset = offset;
-
- UA_SecureConversationMessageHeader header;
- retval = UA_SecureConversationMessageHeader_decodeBinary(chunks, &offset, &header);
- if(retval != UA_STATUSCODE_GOOD)
- break;
-
- if(header.secureChannelId != channel->securityToken.channelId) {
-
-
- return UA_STATUSCODE_BADCOMMUNICATIONERROR;
- }
-
- UA_SequenceHeader sequenceHeader;
- UA_SequenceHeader_init(&sequenceHeader);
- if((header.messageHeader.messageTypeAndChunkType & 0x00ffffff) != UA_MESSAGETYPE_OPN) {
-
- UA_UInt32 tokenId = 0;
- retval |= UA_UInt32_decodeBinary(chunks, &offset, &tokenId);
- retval |= UA_SequenceHeader_decodeBinary(chunks, &offset, &sequenceHeader);
- if(retval != UA_STATUSCODE_GOOD)
- return UA_STATUSCODE_BADCOMMUNICATIONERROR;
-
- if(tokenId != channel->securityToken.tokenId) {
- if(tokenId != channel->nextSecurityToken.tokenId)
- return UA_STATUSCODE_BADCOMMUNICATIONERROR;
- UA_SecureChannel_revolveTokens(channel);
- }
-
- retval = UA_SecureChannel_processSequenceNumber(channel, sequenceHeader.sequenceNumber);
- if(retval != UA_STATUSCODE_GOOD)
- return UA_STATUSCODE_BADCOMMUNICATIONERROR;
- }
-
- size_t processed_header = offset - initial_offset;
- switch(header.messageHeader.messageTypeAndChunkType & 0xff000000) {
- case UA_CHUNKTYPE_INTERMEDIATE:
- UA_SecureChannel_appendChunk(channel, sequenceHeader.requestId, chunks, offset,
- header.messageHeader.messageSize - processed_header);
- break;
- case UA_CHUNKTYPE_FINAL: {
- UA_Boolean realloced = false;
- UA_ByteString message =
- UA_SecureChannel_finalizeChunk(channel, sequenceHeader.requestId, chunks, offset,
- header.messageHeader.messageSize - processed_header,
- &realloced);
- if(message.length > 0) {
- callback(application, channel, header.messageHeader.messageTypeAndChunkType & 0x00ffffff,
- sequenceHeader.requestId, &message);
- if(realloced)
- UA_ByteString_deleteMembers(&message);
- }
- break; }
- case UA_CHUNKTYPE_ABORT:
- UA_SecureChannel_removeChunk(channel, sequenceHeader.requestId);
- break;
- default:
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- offset += (header.messageHeader.messageSize - processed_header);
- } while(chunks->length > offset);
- return retval;
- }
|