12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214 |
- /* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/.
- *
- * Copyright 2014-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
- * Copyright 2014, 2016-2017 (c) Florian Palm
- * Copyright 2015-2016 (c) Sten Grüner
- * Copyright 2015 (c) Oleksiy Vasylyev
- * Copyright 2016 (c) TorbenD
- * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
- * Copyright 2017-2018 (c) Mark Giraud, Fraunhofer IOSB
- */
- #include "ua_util_internal.h"
- #include "ua_securechannel.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"
- #include "ua_plugin_securitypolicy.h"
- #define UA_BITMASK_MESSAGETYPE 0x00ffffff
- #define UA_BITMASK_CHUNKTYPE 0xff000000
- #define UA_ASYMMETRIC_ALG_SECURITY_HEADER_FIXED_LENGTH 12
- #define UA_SYMMETRIC_ALG_SECURITY_HEADER_LENGTH 4
- #define UA_SEQUENCE_HEADER_LENGTH 8
- #define UA_SECUREMH_AND_SYMALGH_LENGTH \
- (UA_SECURE_CONVERSATION_MESSAGE_HEADER_LENGTH + \
- UA_SYMMETRIC_ALG_SECURITY_HEADER_LENGTH)
- const UA_ByteString
- UA_SECURITY_POLICY_NONE_URI = {47, (UA_Byte *)"http://opcfoundation.org/UA/SecurityPolicy#None"};
- #ifdef UA_ENABLE_UNIT_TEST_FAILURE_HOOKS
- UA_StatusCode decrypt_verifySignatureFailure;
- UA_StatusCode sendAsym_sendFailure;
- UA_StatusCode processSym_seqNumberFailure;
- #endif
- void
- UA_SecureChannel_init(UA_SecureChannel *channel) {
- /* Linked lists are also initialized by zeroing out */
- memset(channel, 0, sizeof(UA_SecureChannel));
- channel->state = UA_SECURECHANNELSTATE_FRESH;
- TAILQ_INIT(&channel->messages);
- }
- UA_StatusCode
- UA_SecureChannel_setSecurityPolicy(UA_SecureChannel *channel,
- const UA_SecurityPolicy *securityPolicy,
- const UA_ByteString *remoteCertificate) {
- /* Is a policy already configured? */
- if(channel->securityPolicy)
- return UA_STATUSCODE_BADINTERNALERROR;
-
- channel->securityPolicy = securityPolicy;
- UA_StatusCode retval;
- if(channel->securityPolicy->certificateVerification != NULL) {
- retval = channel->securityPolicy->certificateVerification->
- verifyCertificate(channel->securityPolicy->certificateVerification->context, remoteCertificate);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- } else {
- UA_LOG_WARNING(channel->securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY, "No PKI plugin set. "
- "Accepting all certificates");
- }
- retval = securityPolicy->channelModule.
- newContext(securityPolicy, remoteCertificate, &channel->channelContext);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- retval = UA_ByteString_copy(remoteCertificate, &channel->remoteCertificate);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- UA_ByteString remoteCertificateThumbprint = {20, channel->remoteCertificateThumbprint};
- retval = securityPolicy->asymmetricModule.
- makeCertificateThumbprint(securityPolicy, &channel->remoteCertificate,
- &remoteCertificateThumbprint);
- return retval;
- }
- static void deleteMessage(UA_Message *me) {
- UA_ChunkPayload *cp;
- while((cp = SIMPLEQ_FIRST(&me->chunkPayloads))) {
- if(cp->copied)
- UA_ByteString_deleteMembers(&cp->bytes);
- SIMPLEQ_REMOVE_HEAD(&me->chunkPayloads, pointers);
- UA_free(cp);
- }
- UA_free(me);
- }
- static void
- deleteLatestMessage(UA_SecureChannel *channel, UA_UInt32 requestId) {
- UA_Message *me = TAILQ_LAST(&channel->messages, UA_MessageQueue);
- if(!me)
- return;
- if(me->requestId != requestId)
- return;
- TAILQ_REMOVE(&channel->messages, me, pointers);
- deleteMessage(me);
- }
- void
- UA_SecureChannel_deleteMessages(UA_SecureChannel *channel) {
- UA_Message *me, *me_tmp;
- TAILQ_FOREACH_SAFE(me, &channel->messages, pointers, me_tmp) {
- TAILQ_REMOVE(&channel->messages, me, pointers);
- deleteMessage(me);
- }
- }
- void
- UA_SecureChannel_deleteMembersCleanup(UA_SecureChannel *channel) {
- /* Delete members */
- UA_ByteString_deleteMembers(&channel->remoteCertificate);
- UA_ByteString_deleteMembers(&channel->localNonce);
- UA_ByteString_deleteMembers(&channel->remoteNonce);
- UA_ChannelSecurityToken_deleteMembers(&channel->securityToken);
- UA_ChannelSecurityToken_deleteMembers(&channel->nextSecurityToken);
- /* Delete the channel context for the security policy */
- if(channel->securityPolicy)
- channel->securityPolicy->channelModule.deleteContext(channel->channelContext);
- /* Detach from the connection and close the connection */
- if(channel->connection) {
- if(channel->connection->state != UA_CONNECTION_CLOSED)
- channel->connection->close(channel->connection);
- UA_Connection_detachSecureChannel(channel->connection);
- }
- /* Remove session pointers (not the sessions) and NULL the pointers back to
- * the SecureChannel in the Session */
- UA_SessionHeader *sh, *temp;
- LIST_FOREACH_SAFE(sh, &channel->sessions, pointers, temp) {
- sh->channel = NULL;
- LIST_REMOVE(sh, pointers);
- }
- /* Remove the buffered messages */
- UA_SecureChannel_deleteMessages(channel);
- }
- UA_StatusCode
- UA_SecureChannel_generateLocalNonce(UA_SecureChannel *channel) {
- if(!channel->securityPolicy)
- return UA_STATUSCODE_BADINTERNALERROR;
- /* Is the length of the previous nonce correct? */
- size_t nonceLength = channel->securityPolicy->symmetricModule.secureChannelNonceLength;
- if(channel->localNonce.length != nonceLength) {
- UA_ByteString_deleteMembers(&channel->localNonce);
- UA_StatusCode retval = UA_ByteString_allocBuffer(&channel->localNonce, nonceLength);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- }
- return channel->securityPolicy->symmetricModule.
- generateNonce(channel->securityPolicy, &channel->localNonce);
- }
- static UA_StatusCode
- UA_SecureChannel_generateLocalKeys(const UA_SecureChannel *const channel,
- const UA_SecurityPolicy *const securityPolicy) {
- const UA_SecurityPolicyChannelModule *channelModule = &securityPolicy->channelModule;
- const UA_SecurityPolicySymmetricModule *symmetricModule = &securityPolicy->symmetricModule;
- const UA_SecurityPolicyCryptoModule *const cryptoModule = &securityPolicy->symmetricModule.cryptoModule;
- /* Symmetric key length */
- size_t encryptionKeyLength =
- cryptoModule->encryptionAlgorithm.getLocalKeyLength(securityPolicy, channel->channelContext);
- size_t encryptionBlockSize =
- cryptoModule->encryptionAlgorithm.getLocalBlockSize(securityPolicy, channel->channelContext);
- size_t signingKeyLength =
- cryptoModule->signatureAlgorithm.getLocalKeyLength(securityPolicy, channel->channelContext);
- const size_t bufSize = encryptionBlockSize + signingKeyLength + encryptionKeyLength;
- UA_STACKARRAY(UA_Byte, bufBytes, bufSize);
- UA_ByteString buffer = {bufSize, bufBytes};
- /* Local keys */
- UA_StatusCode retval = symmetricModule->generateKey(securityPolicy, &channel->remoteNonce,
- &channel->localNonce, &buffer);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- const UA_ByteString localSigningKey = {signingKeyLength, buffer.data};
- const UA_ByteString localEncryptingKey = {encryptionKeyLength,
- buffer.data + signingKeyLength};
- const UA_ByteString localIv = {encryptionBlockSize,
- buffer.data + signingKeyLength +
- encryptionKeyLength};
- retval = channelModule->setLocalSymSigningKey(channel->channelContext, &localSigningKey);
- retval |= channelModule->setLocalSymEncryptingKey(channel->channelContext, &localEncryptingKey);
- retval |= channelModule->setLocalSymIv(channel->channelContext, &localIv);
- return retval;
- }
- static UA_StatusCode
- UA_SecureChannel_generateRemoteKeys(const UA_SecureChannel *const channel,
- const UA_SecurityPolicy *const securityPolicy) {
- const UA_SecurityPolicyChannelModule *channelModule = &securityPolicy->channelModule;
- const UA_SecurityPolicySymmetricModule *symmetricModule = &securityPolicy->symmetricModule;
- const UA_SecurityPolicyCryptoModule *const cryptoModule = &securityPolicy->symmetricModule.cryptoModule;
- /* Symmetric key length */
- size_t encryptionKeyLength =
- cryptoModule->encryptionAlgorithm.getRemoteKeyLength(securityPolicy, channel->channelContext);
- size_t encryptionBlockSize =
- cryptoModule->encryptionAlgorithm.getRemoteBlockSize(securityPolicy, channel->channelContext);
- size_t signingKeyLength =
- cryptoModule->signatureAlgorithm.getRemoteKeyLength(securityPolicy, channel->channelContext);
- const size_t bufSize = encryptionBlockSize + signingKeyLength + encryptionKeyLength;
- UA_STACKARRAY(UA_Byte, bufBytes, bufSize);
- UA_ByteString buffer = {bufSize, bufBytes};
- /* Remote keys */
- UA_StatusCode retval = symmetricModule->generateKey(securityPolicy, &channel->localNonce,
- &channel->remoteNonce, &buffer);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- const UA_ByteString remoteSigningKey = {signingKeyLength, buffer.data};
- const UA_ByteString remoteEncryptingKey = {encryptionKeyLength,
- buffer.data + signingKeyLength};
- const UA_ByteString remoteIv = {encryptionBlockSize,
- buffer.data + signingKeyLength +
- encryptionKeyLength};
- retval = channelModule->setRemoteSymSigningKey(channel->channelContext, &remoteSigningKey);
- retval |= channelModule->setRemoteSymEncryptingKey(channel->channelContext, &remoteEncryptingKey);
- retval |= channelModule->setRemoteSymIv(channel->channelContext, &remoteIv);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- return retval;
- }
- UA_StatusCode
- UA_SecureChannel_generateNewKeys(UA_SecureChannel *channel) {
- UA_StatusCode retval = UA_SecureChannel_generateLocalKeys(channel, channel->securityPolicy);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- retval = UA_SecureChannel_generateRemoteKeys(channel, channel->securityPolicy);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- return retval;
- }
- UA_SessionHeader *
- UA_SecureChannel_getSession(UA_SecureChannel *channel,
- const UA_NodeId *authenticationToken) {
- UA_SessionHeader *sh;
- LIST_FOREACH(sh, &channel->sessions, pointers) {
- if(UA_NodeId_equal(&sh->authenticationToken, authenticationToken))
- break;
- }
- return sh;
- }
- UA_StatusCode
- UA_SecureChannel_revolveTokens(UA_SecureChannel *channel) {
- if(channel->nextSecurityToken.tokenId == 0) // no security token issued
- return UA_STATUSCODE_BADSECURECHANNELTOKENUNKNOWN;
- //FIXME: not thread-safe
- memcpy(&channel->securityToken, &channel->nextSecurityToken,
- sizeof(UA_ChannelSecurityToken));
- UA_ChannelSecurityToken_init(&channel->nextSecurityToken);
- return UA_SecureChannel_generateNewKeys(channel);
- }
- /***************************/
- /* Send Asymmetric Message */
- /***************************/
- static UA_UInt16
- calculatePaddingAsym(const UA_SecurityPolicy *securityPolicy, const void *channelContext,
- size_t bytesToWrite, UA_Byte *paddingSize, UA_Byte *extraPaddingSize) {
- size_t plainTextBlockSize = securityPolicy->asymmetricModule.cryptoModule.encryptionAlgorithm.
- getRemotePlainTextBlockSize(securityPolicy, channelContext);
- size_t signatureSize = securityPolicy->asymmetricModule.cryptoModule.signatureAlgorithm.
- getLocalSignatureSize(securityPolicy, channelContext);
- size_t paddingBytes = 1;
- if(securityPolicy->asymmetricModule.cryptoModule.encryptionAlgorithm.
- getRemoteKeyLength(securityPolicy, channelContext) > 2048)
- ++paddingBytes;
- size_t padding = (plainTextBlockSize - ((bytesToWrite + signatureSize + paddingBytes) %
- plainTextBlockSize));
- *paddingSize = (UA_Byte)(padding & 0xff);
- *extraPaddingSize = (UA_Byte)(padding >> 8);
- return (UA_UInt16)padding;
- }
- static size_t
- calculateAsymAlgSecurityHeaderLength(const UA_SecureChannel *channel) {
- size_t asymHeaderLength = UA_ASYMMETRIC_ALG_SECURITY_HEADER_FIXED_LENGTH +
- channel->securityPolicy->policyUri.length;
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
- channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
- /* OPN is always encrypted even if mode sign only */
- asymHeaderLength += 20; /* Thumbprints are always 20 byte long */
- asymHeaderLength += channel->securityPolicy->localCertificate.length;
- }
- return asymHeaderLength;
- }
- static void
- hideBytesAsym(const UA_SecureChannel *channel, UA_Byte **buf_start, const UA_Byte **buf_end) {
- const UA_SecurityPolicy *securityPolicy = channel->securityPolicy;
- *buf_start += UA_SECURE_CONVERSATION_MESSAGE_HEADER_LENGTH + UA_SEQUENCE_HEADER_LENGTH;
- /* Add the SecurityHeaderLength */
- *buf_start += calculateAsymAlgSecurityHeaderLength(channel);
- size_t potentialEncryptionMaxSize = (size_t)(*buf_end - *buf_start) + UA_SEQUENCE_HEADER_LENGTH;
- /* Hide bytes for signature and padding */
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
- channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
- *buf_end -= securityPolicy->asymmetricModule.cryptoModule.signatureAlgorithm.
- getLocalSignatureSize(securityPolicy, channel->channelContext);
- *buf_end -= 2; /* padding byte and extraPadding byte */
- /* Add some overhead length due to RSA implementations adding a signature themselves */
- *buf_end -= UA_SecurityPolicy_getRemoteAsymEncryptionBufferLengthOverhead(securityPolicy,
- channel->channelContext,
- potentialEncryptionMaxSize);
- }
- }
- /* Sends an OPN message using asymmetric encryption if defined */
- UA_StatusCode
- UA_SecureChannel_sendAsymmetricOPNMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
- const void *content, const UA_DataType *contentType) {
- if(channel->securityMode == UA_MESSAGESECURITYMODE_INVALID)
- return UA_STATUSCODE_BADSECURITYMODEREJECTED;
- const UA_SecurityPolicy *const securityPolicy = channel->securityPolicy;
- UA_Connection *connection = channel->connection;
- if(!connection)
- return UA_STATUSCODE_BADINTERNALERROR;
- /* Allocate the message buffer */
- UA_ByteString buf = UA_BYTESTRING_NULL;
- UA_StatusCode retval =
- connection->getSendBuffer(connection, connection->localConf.sendBufferSize, &buf);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- /* Restrict buffer to the available space for the payload */
- UA_Byte *buf_pos = buf.data;
- const UA_Byte *buf_end = &buf.data[buf.length];
- hideBytesAsym(channel, &buf_pos, &buf_end);
- /* Encode the message type and content */
- UA_NodeId typeId = UA_NODEID_NUMERIC(0, contentType->binaryEncodingId);
- retval = UA_encodeBinary(&typeId, &UA_TYPES[UA_TYPES_NODEID], &buf_pos, &buf_end, NULL, NULL);
- retval |= UA_encodeBinary(content, contentType, &buf_pos, &buf_end, NULL, NULL);
- if(retval != UA_STATUSCODE_GOOD) {
- connection->releaseSendBuffer(connection, &buf);
- return retval;
- }
- /* Compute the length of the asym header */
- const size_t securityHeaderLength = calculateAsymAlgSecurityHeaderLength(channel);
- /* Pad the message. Also if securitymode is only sign, since we are using
- * asymmetric communication to exchange keys and thus need to encrypt. */
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
- channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
- const UA_Byte *buf_body_start =
- &buf.data[UA_SECURE_CONVERSATION_MESSAGE_HEADER_LENGTH +
- UA_SEQUENCE_HEADER_LENGTH + securityHeaderLength];
- const size_t bytesToWrite =
- (uintptr_t)buf_pos - (uintptr_t)buf_body_start + UA_SEQUENCE_HEADER_LENGTH;
- UA_Byte paddingSize = 0;
- UA_Byte extraPaddingSize = 0;
- UA_UInt16 totalPaddingSize =
- calculatePaddingAsym(securityPolicy, channel->channelContext,
- bytesToWrite, &paddingSize, &extraPaddingSize);
- // This is <= because the paddingSize byte also has to be written.
- for(UA_UInt16 i = 0; i <= totalPaddingSize; ++i) {
- *buf_pos = paddingSize;
- ++buf_pos;
- }
- if(securityPolicy->asymmetricModule.cryptoModule.encryptionAlgorithm.
- getRemoteKeyLength(securityPolicy, channel->channelContext) > 2048) {
- *buf_pos = extraPaddingSize;
- ++buf_pos;
- }
- }
- /* The total message length */
- size_t pre_sig_length = (uintptr_t)buf_pos - (uintptr_t)buf.data;
- size_t total_length = pre_sig_length;
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
- channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
- total_length += securityPolicy->asymmetricModule.cryptoModule.signatureAlgorithm.
- getLocalSignatureSize(securityPolicy, channel->channelContext);
- /* Encode the headers at the beginning of the message */
- UA_Byte *header_pos = buf.data;
- size_t dataToEncryptLength =
- total_length - (UA_SECURE_CONVERSATION_MESSAGE_HEADER_LENGTH + securityHeaderLength);
- UA_SecureConversationMessageHeader respHeader;
- respHeader.messageHeader.messageTypeAndChunkType = UA_MESSAGETYPE_OPN + UA_CHUNKTYPE_FINAL;
- respHeader.messageHeader.messageSize = (UA_UInt32)
- (total_length + UA_SecurityPolicy_getRemoteAsymEncryptionBufferLengthOverhead(securityPolicy,
- channel->channelContext,
- dataToEncryptLength));
- respHeader.secureChannelId = channel->securityToken.channelId;
- retval = UA_encodeBinary(&respHeader, &UA_TRANSPORT[UA_TRANSPORT_SECURECONVERSATIONMESSAGEHEADER],
- &header_pos, &buf_end, NULL, NULL);
- UA_AsymmetricAlgorithmSecurityHeader asymHeader;
- UA_AsymmetricAlgorithmSecurityHeader_init(&asymHeader);
- asymHeader.securityPolicyUri = channel->securityPolicy->policyUri;
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
- channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
- asymHeader.senderCertificate = channel->securityPolicy->localCertificate;
- asymHeader.receiverCertificateThumbprint.length = 20;
- asymHeader.receiverCertificateThumbprint.data = channel->remoteCertificateThumbprint;
- }
- retval |= UA_encodeBinary(&asymHeader, &UA_TRANSPORT[UA_TRANSPORT_ASYMMETRICALGORITHMSECURITYHEADER],
- &header_pos, &buf_end, NULL, NULL);
- UA_SequenceHeader seqHeader;
- seqHeader.requestId = requestId;
- seqHeader.sequenceNumber = UA_atomic_addUInt32(&channel->sendSequenceNumber, 1);
- retval |= UA_encodeBinary(&seqHeader, &UA_TRANSPORT[UA_TRANSPORT_SEQUENCEHEADER],
- &header_pos, &buf_end, NULL, NULL);
- /* Did encoding the header succeed? */
- if(retval != UA_STATUSCODE_GOOD) {
- connection->releaseSendBuffer(connection, &buf);
- return retval;
- }
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
- channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
- /* Sign message */
- const UA_ByteString dataToSign = {pre_sig_length, buf.data};
- size_t sigsize = securityPolicy->asymmetricModule.cryptoModule.signatureAlgorithm.
- getLocalSignatureSize(securityPolicy, channel->channelContext);
- UA_ByteString signature = {sigsize, buf.data + pre_sig_length};
- retval = securityPolicy->asymmetricModule.cryptoModule.signatureAlgorithm.
- sign(securityPolicy, channel->channelContext, &dataToSign, &signature);
- if(retval != UA_STATUSCODE_GOOD) {
- connection->releaseSendBuffer(connection, &buf);
- return retval;
- }
- /* Specification part 6, 6.7.4: The OpenSecureChannel Messages are
- * signed and encrypted if the SecurityMode is not None (even if the
- * SecurityMode is SignOnly). */
- size_t unencrypted_length =
- UA_SECURE_CONVERSATION_MESSAGE_HEADER_LENGTH + securityHeaderLength;
- UA_ByteString dataToEncrypt = {total_length - unencrypted_length,
- &buf.data[unencrypted_length]};
- retval = securityPolicy->asymmetricModule.cryptoModule.encryptionAlgorithm.
- encrypt(securityPolicy, channel->channelContext, &dataToEncrypt);
- if(retval != UA_STATUSCODE_GOOD) {
- connection->releaseSendBuffer(connection, &buf);
- return retval;
- }
- }
- /* Send the message, the buffer is freed in the network layer */
- buf.length = respHeader.messageHeader.messageSize;
- retval = connection->send(connection, &buf);
- #ifdef UA_ENABLE_UNIT_TEST_FAILURE_HOOKS
- retval |= sendAsym_sendFailure
- #endif
- return retval;
- }
- /**************************/
- /* Send Symmetric Message */
- /**************************/
- static UA_UInt16
- calculatePaddingSym(const UA_SecurityPolicy *securityPolicy, const void *channelContext,
- size_t bytesToWrite, UA_Byte *paddingSize, UA_Byte *extraPaddingSize) {
- size_t encryptionBlockSize = securityPolicy->symmetricModule.cryptoModule.encryptionAlgorithm.
- getLocalBlockSize(securityPolicy, channelContext);
- size_t signatureSize = securityPolicy->symmetricModule.cryptoModule.signatureAlgorithm.
- getLocalSignatureSize(securityPolicy, channelContext);
- UA_UInt16 padding = (UA_UInt16)(encryptionBlockSize - ((bytesToWrite + signatureSize + 1) % encryptionBlockSize));
- *paddingSize = (UA_Byte)padding;
- *extraPaddingSize = (UA_Byte)(padding >> 8);
- return padding;
- }
- static void
- setBufPos(UA_MessageContext *mc) {
- const UA_SecureChannel *channel = mc->channel;
- const UA_SecurityPolicy *securityPolicy = channel->securityPolicy;
- /* Forward the data pointer so that the payload is encoded after the
- * message header */
- mc->buf_pos = &mc->messageBuffer.data[UA_SECURE_MESSAGE_HEADER_LENGTH];
- mc->buf_end = &mc->messageBuffer.data[mc->messageBuffer.length];
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
- channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
- mc->buf_end -= securityPolicy->symmetricModule.cryptoModule.signatureAlgorithm.
- getLocalSignatureSize(securityPolicy, channel->channelContext);
- /* Hide a byte needed for padding */
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
- mc->buf_end -= 2;
- }
- static UA_StatusCode
- sendSymmetricChunk(UA_MessageContext *mc) {
- UA_StatusCode res = UA_STATUSCODE_GOOD;
- UA_SecureChannel *const channel = mc->channel;
- const UA_SecurityPolicy *securityPolicy = channel->securityPolicy;
- UA_Connection *const connection = channel->connection;
- if(!connection)
- return UA_STATUSCODE_BADINTERNALERROR;
- /* Will this chunk surpass the capacity of the SecureChannel for the message? */
- UA_Byte *buf_body_start = mc->messageBuffer.data + UA_SECURE_MESSAGE_HEADER_LENGTH;
- const UA_Byte *buf_body_end = mc->buf_pos;
- size_t bodyLength = (uintptr_t)buf_body_end - (uintptr_t)buf_body_start;
- mc->messageSizeSoFar += bodyLength;
- mc->chunksSoFar++;
- if(mc->messageSizeSoFar > connection->remoteConf.maxMessageSize &&
- connection->remoteConf.maxMessageSize != 0)
- res = UA_STATUSCODE_BADRESPONSETOOLARGE;
- if(mc->chunksSoFar > connection->remoteConf.maxChunkCount &&
- connection->remoteConf.maxChunkCount != 0)
- res = UA_STATUSCODE_BADRESPONSETOOLARGE;
- if(res != UA_STATUSCODE_GOOD) {
- connection->releaseSendBuffer(channel->connection, &mc->messageBuffer);
- return res;
- }
- /* Pad the message. The bytes for the padding and signature were removed
- * from buf_end before encoding the payload. So we don't check here. */
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
- size_t bytesToWrite = bodyLength + UA_SEQUENCE_HEADER_LENGTH;
- UA_Byte paddingSize = 0;
- UA_Byte extraPaddingSize = 0;
- UA_UInt16 totalPaddingSize =
- calculatePaddingSym(securityPolicy, channel->channelContext,
- bytesToWrite, &paddingSize, &extraPaddingSize);
- // This is <= because the paddingSize byte also has to be written.
- for(UA_UInt16 i = 0; i <= totalPaddingSize; ++i) {
- *mc->buf_pos = paddingSize;
- ++(mc->buf_pos);
- }
- if(extraPaddingSize > 0) {
- *mc->buf_pos = extraPaddingSize;
- ++(mc->buf_pos);
- }
- }
- /* The total message length */
- size_t pre_sig_length = (uintptr_t)(mc->buf_pos) - (uintptr_t)mc->messageBuffer.data;
- size_t total_length = pre_sig_length;
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
- channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
- total_length += securityPolicy->symmetricModule.cryptoModule.signatureAlgorithm.
- getLocalSignatureSize(securityPolicy, channel->channelContext);
- mc->messageBuffer.length = total_length; /* For giving the buffer to the network layer */
- /* Encode the chunk headers at the beginning of the buffer */
- UA_assert(res == UA_STATUSCODE_GOOD);
- UA_Byte *header_pos = mc->messageBuffer.data;
- UA_SecureConversationMessageHeader respHeader;
- respHeader.secureChannelId = channel->securityToken.channelId;
- respHeader.messageHeader.messageTypeAndChunkType = mc->messageType;
- respHeader.messageHeader.messageSize = (UA_UInt32)total_length;
- if(mc->final)
- respHeader.messageHeader.messageTypeAndChunkType += UA_CHUNKTYPE_FINAL;
- else
- respHeader.messageHeader.messageTypeAndChunkType += UA_CHUNKTYPE_INTERMEDIATE;
- res = UA_encodeBinary(&respHeader, &UA_TRANSPORT[UA_TRANSPORT_SECURECONVERSATIONMESSAGEHEADER],
- &header_pos, &mc->buf_end, NULL, NULL);
- UA_SymmetricAlgorithmSecurityHeader symSecHeader;
- symSecHeader.tokenId = channel->securityToken.tokenId;
- res |= UA_encodeBinary(&symSecHeader.tokenId,
- &UA_TRANSPORT[UA_TRANSPORT_SYMMETRICALGORITHMSECURITYHEADER],
- &header_pos, &mc->buf_end, NULL, NULL);
- UA_SequenceHeader seqHeader;
- seqHeader.requestId = mc->requestId;
- seqHeader.sequenceNumber = UA_atomic_addUInt32(&channel->sendSequenceNumber, 1);
- res |= UA_encodeBinary(&seqHeader, &UA_TRANSPORT[UA_TRANSPORT_SEQUENCEHEADER],
- &header_pos, &mc->buf_end, NULL, NULL);
- /* Sign message */
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
- channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
- UA_ByteString dataToSign = mc->messageBuffer;
- dataToSign.length = pre_sig_length;
- UA_ByteString signature;
- signature.length = securityPolicy->symmetricModule.cryptoModule.signatureAlgorithm.
- getLocalSignatureSize(securityPolicy, channel->channelContext);
- signature.data = mc->buf_pos;
- res |= securityPolicy->symmetricModule.cryptoModule.signatureAlgorithm.
- sign(securityPolicy, channel->channelContext, &dataToSign, &signature);
- }
- /* Encrypt message */
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
- UA_ByteString dataToEncrypt;
- dataToEncrypt.data = mc->messageBuffer.data + UA_SECUREMH_AND_SYMALGH_LENGTH;
- dataToEncrypt.length = total_length - UA_SECUREMH_AND_SYMALGH_LENGTH;
- res |= securityPolicy->symmetricModule.cryptoModule.encryptionAlgorithm.
- encrypt(securityPolicy, channel->channelContext, &dataToEncrypt);
- }
- if(res != UA_STATUSCODE_GOOD) {
- connection->releaseSendBuffer(channel->connection, &mc->messageBuffer);
- return res;
- }
- /* Send the chunk, the buffer is freed in the network layer */
- return connection->send(channel->connection, &mc->messageBuffer);
- }
- /* Callback from the encoding layer. Send the chunk and replace the buffer. */
- static UA_StatusCode
- sendSymmetricEncodingCallback(void *data, UA_Byte **buf_pos, const UA_Byte **buf_end) {
- /* Set buf values from encoding in the messagecontext */
- UA_MessageContext *mc = (UA_MessageContext *)data;
- mc->buf_pos = *buf_pos;
- mc->buf_end = *buf_end;
- /* Send out */
- UA_StatusCode retval = sendSymmetricChunk(mc);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- /* Set a new buffer for the next chunk */
- UA_Connection *connection = mc->channel->connection;
- retval = connection->getSendBuffer(connection, connection->localConf.sendBufferSize,
- &mc->messageBuffer);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- /* Hide bytes for header, padding and signature */
- setBufPos(mc);
- *buf_pos = mc->buf_pos;
- *buf_end = mc->buf_end;
- return UA_STATUSCODE_GOOD;
- }
- UA_StatusCode
- UA_MessageContext_begin(UA_MessageContext *mc, UA_SecureChannel *channel,
- UA_UInt32 requestId, UA_MessageType messageType) {
- UA_Connection *connection = channel->connection;
- if(!connection)
- return UA_STATUSCODE_BADINTERNALERROR;
- if(messageType != UA_MESSAGETYPE_MSG && messageType != UA_MESSAGETYPE_CLO)
- return UA_STATUSCODE_BADINTERNALERROR;
- /* Create the chunking info structure */
- mc->channel = channel;
- mc->requestId = requestId;
- mc->chunksSoFar = 0;
- mc->messageSizeSoFar = 0;
- mc->final = false;
- mc->messageBuffer = UA_BYTESTRING_NULL;
- mc->messageType = messageType;
- /* Minimum required size */
- if(connection->localConf.sendBufferSize <= UA_SECURE_MESSAGE_HEADER_LENGTH)
- return UA_STATUSCODE_BADRESPONSETOOLARGE;
- /* Allocate the message buffer */
- UA_StatusCode retval =
- connection->getSendBuffer(connection, connection->localConf.sendBufferSize,
- &mc->messageBuffer);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- /* Hide bytes for header, padding and signature */
- setBufPos(mc);
- return UA_STATUSCODE_GOOD;
- }
- UA_StatusCode
- UA_MessageContext_encode(UA_MessageContext *mc, const void *content,
- const UA_DataType *contentType) {
- UA_StatusCode retval = UA_encodeBinary(content, contentType, &mc->buf_pos, &mc->buf_end,
- sendSymmetricEncodingCallback, mc);
- if(retval != UA_STATUSCODE_GOOD) {
- /* TODO: Send the abort message */
- if(mc->messageBuffer.length > 0) {
- UA_Connection *connection = mc->channel->connection;
- connection->releaseSendBuffer(connection, &mc->messageBuffer);
- }
- }
- return retval;
- }
- UA_StatusCode
- UA_MessageContext_finish(UA_MessageContext *mc) {
- mc->final = true;
- return sendSymmetricChunk(mc);
- }
- void
- UA_MessageContext_abort(UA_MessageContext *mc) {
- UA_Connection *connection = mc->channel->connection;
- connection->releaseSendBuffer(connection, &mc->messageBuffer);
- }
- UA_StatusCode
- UA_SecureChannel_sendSymmetricMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
- UA_MessageType messageType, void *payload,
- const UA_DataType *payloadType) {
- if(!channel || !payload || !payloadType)
- return UA_STATUSCODE_BADINTERNALERROR;
- if(channel->connection) {
- if(channel->connection->state == UA_CONNECTION_CLOSED)
- return UA_STATUSCODE_BADCONNECTIONCLOSED;
- }
- UA_MessageContext mc;
- UA_StatusCode retval = UA_MessageContext_begin(&mc, channel, requestId, messageType);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- /* Assert's required for clang-analyzer */
- UA_assert(mc.buf_pos == &mc.messageBuffer.data[UA_SECURE_MESSAGE_HEADER_LENGTH]);
- UA_assert(mc.buf_end <= &mc.messageBuffer.data[mc.messageBuffer.length]);
- UA_NodeId typeId = UA_NODEID_NUMERIC(0, payloadType->binaryEncodingId);
- retval = UA_MessageContext_encode(&mc, &typeId, &UA_TYPES[UA_TYPES_NODEID]);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- retval = UA_MessageContext_encode(&mc, payload, payloadType);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- return UA_MessageContext_finish(&mc);
- }
- /*****************************/
- /* Assemble Complete Message */
- /*****************************/
- static UA_StatusCode
- addChunkPayload(UA_SecureChannel *channel, UA_UInt32 requestId,
- UA_MessageType messageType, UA_ByteString *chunkPayload,
- UA_Boolean final) {
- UA_Message *latest = TAILQ_LAST(&channel->messages, UA_MessageQueue);
- if(latest) {
- if(latest->requestId != requestId) {
- /* Start of a new message */
- if(!latest->final)
- return UA_STATUSCODE_BADTCPMESSAGETYPEINVALID;
- latest = NULL;
- } else {
- if(latest->messageType != messageType) /* MessageType mismatch */
- return UA_STATUSCODE_BADTCPMESSAGETYPEINVALID;
- if(latest->final) /* Correct message, but already finalized */
- return UA_STATUSCODE_BADTCPMESSAGETYPEINVALID;
- }
- }
- /* Create a new message entry */
- if(!latest) {
- latest = (UA_Message*)UA_malloc(sizeof(UA_Message));
- if(!latest)
- return UA_STATUSCODE_BADOUTOFMEMORY;
- memset(latest, 0, sizeof(UA_Message));
- latest->requestId = requestId;
- latest->messageType = messageType;
- SIMPLEQ_INIT(&latest->chunkPayloads);
- TAILQ_INSERT_TAIL(&channel->messages, latest, pointers);
- }
- /* Test against the connection settings */
- const UA_ConnectionConfig *config = &channel->connection->localConf;
- if(config->maxChunkCount > 0 &&
- config->maxChunkCount <= latest->chunkPayloadsSize)
- return UA_STATUSCODE_BADRESPONSETOOLARGE;
- if(config->maxMessageSize > 0 &&
- config->maxMessageSize < latest->messageSize + chunkPayload->length)
- return UA_STATUSCODE_BADRESPONSETOOLARGE;
- /* Create a new chunk entry */
- UA_ChunkPayload* cp = (UA_ChunkPayload*)UA_malloc(sizeof(UA_ChunkPayload));
- if(!cp)
- return UA_STATUSCODE_BADOUTOFMEMORY;
- cp->bytes = *chunkPayload;
- cp->copied = false;
- /* Add the chunk */
- SIMPLEQ_INSERT_TAIL(&latest->chunkPayloads, cp, pointers);
- latest->chunkPayloadsSize += 1;
- latest->messageSize += chunkPayload->length;
- latest->final = final;
- return UA_STATUSCODE_GOOD;
- }
- static UA_StatusCode
- processMessage(UA_SecureChannel *channel, const UA_Message *message,
- void *application, UA_ProcessMessageCallback callback) {
- if(message->chunkPayloadsSize == 1) {
- /* No need to combine chunks */
- UA_ChunkPayload *cp = SIMPLEQ_FIRST(&message->chunkPayloads);
- callback(application, channel, message->messageType, message->requestId, &cp->bytes);
- } else {
- /* Allocate memory */
- UA_ByteString bytes;
- bytes.data = (UA_Byte*) UA_malloc(message->messageSize);
- if(!bytes.data) {
- UA_LOG_WARNING(channel->securityPolicy->logger, UA_LOGCATEGORY_SECURECHANNEL,
- "Could not allocate the memory to assemble the message");
- return UA_STATUSCODE_BADOUTOFMEMORY;
- }
- bytes.length = message->messageSize;
- /* Assemble the full message */
- size_t curPos = 0;
- UA_ChunkPayload *cp;
- SIMPLEQ_FOREACH(cp, &message->chunkPayloads, pointers) {
- memcpy(&bytes.data[curPos], cp->bytes.data, cp->bytes.length);
- curPos += cp->bytes.length;
- }
- /* Process the message */
- callback(application, channel, message->messageType, message->requestId, &bytes);
- UA_ByteString_deleteMembers(&bytes);
- }
- return UA_STATUSCODE_GOOD;
- }
- UA_StatusCode
- UA_SecureChannel_processCompleteMessages(UA_SecureChannel *channel, void *application,
- UA_ProcessMessageCallback callback) {
- UA_Message *me, *me_tmp;
- UA_StatusCode retval = UA_STATUSCODE_GOOD;
- TAILQ_FOREACH_SAFE(me, &channel->messages, pointers, me_tmp) {
- /* Stop at the first incomplete message */
- if(!me->final)
- break;
- /* Remove the current message before processing */
- TAILQ_REMOVE(&channel->messages, me, pointers);
- /* Process */
- retval = processMessage(channel, me, application, callback);
- if(retval != UA_STATUSCODE_GOOD)
- break;
- /* Clean up the message */
- UA_ChunkPayload *cp;
- while((cp = SIMPLEQ_FIRST(&me->chunkPayloads))) {
- if(cp->copied)
- UA_ByteString_deleteMembers(&cp->bytes);
- SIMPLEQ_REMOVE_HEAD(&me->chunkPayloads, pointers);
- UA_free(cp);
- }
- UA_free(me);
- }
- return retval;
- }
- /****************************/
- /* Process a received Chunk */
- /****************************/
- /* Sets the payload to a pointer inside the chunk buffer. Returns the requestId
- * and the sequenceNumber */
- static UA_StatusCode
- decryptChunk(const UA_SecureChannel *channel, const UA_SecurityPolicyCryptoModule *cryptoModule,
- UA_MessageType messageType, const UA_ByteString *chunk, size_t offset,
- UA_UInt32 *requestId, UA_UInt32 *sequenceNumber, UA_ByteString *payload) {
- UA_StatusCode retval = UA_STATUSCODE_GOOD;
- const UA_SecurityPolicy *securityPolicy = channel->securityPolicy;
- size_t chunkSizeAfterDecryption = chunk->length;
- /* Decrypt the chunk. Always decrypt opn messages if mode not none */
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT ||
- messageType == UA_MESSAGETYPE_OPN) {
- UA_ByteString cipherText = {chunk->length - offset, chunk->data + offset};
- size_t sizeBeforeDecryption = cipherText.length;
- retval = cryptoModule->encryptionAlgorithm.decrypt(securityPolicy, channel->channelContext, &cipherText);
- chunkSizeAfterDecryption -= (sizeBeforeDecryption - cipherText.length);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- }
- /* Verify the chunk signature */
- size_t sigsize = 0;
- size_t paddingSize = 0;
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
- channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT ||
- messageType == UA_MESSAGETYPE_OPN) {
- /* Compute the padding size */
- sigsize = cryptoModule->signatureAlgorithm.getRemoteSignatureSize(securityPolicy, channel->channelContext);
- if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT ||
- (messageType == UA_MESSAGETYPE_OPN &&
- channel->securityMode > UA_MESSAGESECURITYMODE_NONE)) {
- paddingSize = (size_t)chunk->data[chunkSizeAfterDecryption - sigsize - 1];
- size_t keyLength =
- cryptoModule->encryptionAlgorithm.getRemoteKeyLength(securityPolicy, channel->channelContext);
- if(keyLength > 2048) {
- paddingSize <<= 8; /* Extra padding size */
- paddingSize += chunk->data[chunkSizeAfterDecryption - sigsize - 2];
- // see comment below but for extraPaddingSize
- paddingSize += 1;
- }
- // we need to add one to the padding size since the paddingSize byte itself need to be removed as well.
- // TODO: write unit test for correct padding calculation
- paddingSize += 1;
- }
- if(offset + paddingSize + sigsize >= chunkSizeAfterDecryption)
- return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
- /* Verify the signature */
- const UA_ByteString chunkDataToVerify = {chunkSizeAfterDecryption - sigsize, chunk->data};
- const UA_ByteString signature = {sigsize, chunk->data + chunkSizeAfterDecryption - sigsize};
- retval = cryptoModule->signatureAlgorithm.verify(securityPolicy, channel->channelContext,
- &chunkDataToVerify, &signature);
- #ifdef UA_ENABLE_UNIT_TEST_FAILURE_HOOKS
- retval |= decrypt_verifySignatureFailure;
- #endif
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- }
- /* Decode the sequence header */
- UA_SequenceHeader sequenceHeader;
- retval = UA_SequenceHeader_decodeBinary(chunk, &offset, &sequenceHeader);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- if(offset + paddingSize + sigsize >= chunk->length)
- return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
- *requestId = sequenceHeader.requestId;
- *sequenceNumber = sequenceHeader.sequenceNumber;
- payload->data = chunk->data + offset;
- payload->length = chunkSizeAfterDecryption - offset - sigsize - paddingSize;
- return UA_STATUSCODE_GOOD;
- }
- typedef UA_StatusCode(*UA_SequenceNumberCallback)(UA_SecureChannel *channel,
- UA_UInt32 sequenceNumber);
- static UA_StatusCode
- processSequenceNumberAsym(UA_SecureChannel *const channel, UA_UInt32 sequenceNumber) {
- channel->receiveSequenceNumber = sequenceNumber;
- return UA_STATUSCODE_GOOD;
- }
- static UA_StatusCode
- processSequenceNumberSym(UA_SecureChannel *const channel, UA_UInt32 sequenceNumber) {
- /* Failure mode hook for unit tests */
- #ifdef UA_ENABLE_UNIT_TEST_FAILURE_HOOKS
- if(processSym_seqNumberFailure != UA_STATUSCODE_GOOD)
- return processSym_seqNumberFailure;
- #endif
- /* Does the sequence number match? */
- if(sequenceNumber != channel->receiveSequenceNumber + 1) {
- /* FIXME: Remove magic numbers :( */
- if(channel->receiveSequenceNumber + 1 > 4294966271 && sequenceNumber < 1024)
- channel->receiveSequenceNumber = sequenceNumber - 1; /* Roll over */
- else
- return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
- }
- ++channel->receiveSequenceNumber;
- return UA_STATUSCODE_GOOD;
- }
- static UA_StatusCode
- checkAsymHeader(UA_SecureChannel *const channel,
- UA_AsymmetricAlgorithmSecurityHeader *const asymHeader) {
- UA_StatusCode retval = UA_STATUSCODE_GOOD;
- const UA_SecurityPolicy *const securityPolicy = channel->securityPolicy;
- if(!UA_ByteString_equal(&securityPolicy->policyUri, &asymHeader->securityPolicyUri)) {
- return UA_STATUSCODE_BADSECURITYPOLICYREJECTED;
- }
- // TODO: Verify certificate using certificate plugin. This will come with a new PR
- /* Something like this
- retval = certificateManager->verify(certificateStore??, &asymHeader->senderCertificate);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- */
- retval = securityPolicy->asymmetricModule.
- compareCertificateThumbprint(securityPolicy, &asymHeader->receiverCertificateThumbprint);
- if(retval != UA_STATUSCODE_GOOD) {
- return retval;
- }
- return UA_STATUSCODE_GOOD;
- }
- static UA_StatusCode
- checkSymHeader(UA_SecureChannel *const channel,
- const UA_UInt32 tokenId) {
- if(tokenId != channel->securityToken.tokenId) {
- if(tokenId != channel->nextSecurityToken.tokenId)
- return UA_STATUSCODE_BADSECURECHANNELTOKENUNKNOWN;
- return UA_SecureChannel_revolveTokens(channel);
- }
- return UA_STATUSCODE_GOOD;
- }
- /* The chunk body begins after the SecureConversationMessageHeader */
- static UA_StatusCode
- decryptAddChunk(UA_SecureChannel *channel, const UA_ByteString *chunk) {
- /* Decode the MessageHeader */
- size_t offset = 0;
- UA_SecureConversationMessageHeader messageHeader;
- UA_StatusCode retval =
- UA_SecureConversationMessageHeader_decodeBinary(chunk, &offset, &messageHeader);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- #if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
- /* The wrong ChannelId. Non-opened channels have the id zero. */
- if(messageHeader.secureChannelId != channel->securityToken.channelId &&
- channel->state != UA_SECURECHANNELSTATE_FRESH)
- return UA_STATUSCODE_BADSECURECHANNELIDINVALID;
- #endif
- UA_MessageType messageType = (UA_MessageType)
- (messageHeader.messageHeader.messageTypeAndChunkType & UA_BITMASK_MESSAGETYPE);
- UA_ChunkType chunkType = (UA_ChunkType)
- (messageHeader.messageHeader.messageTypeAndChunkType & UA_BITMASK_CHUNKTYPE);
- UA_UInt32 requestId = 0;
- UA_UInt32 sequenceNumber = 0;
- UA_ByteString chunkPayload;
- const UA_SecurityPolicyCryptoModule *cryptoModule = NULL;
- UA_SequenceNumberCallback sequenceNumberCallback = NULL;
- switch(messageType) {
- /* ERR message (not encrypted) */
- case UA_MESSAGETYPE_ERR:
- if(chunkType != UA_CHUNKTYPE_FINAL)
- return UA_STATUSCODE_BADTCPMESSAGETYPEINVALID;
- chunkPayload.length = chunk->length - offset;
- chunkPayload.data = chunk->data + offset;
- goto addPayload;
- /* MSG and CLO: Symmetric encryption */
- case UA_MESSAGETYPE_MSG:
- case UA_MESSAGETYPE_CLO: {
- /* Decode and check the symmetric security header (tokenId) */
- UA_SymmetricAlgorithmSecurityHeader symmetricSecurityHeader;
- UA_SymmetricAlgorithmSecurityHeader_init(&symmetricSecurityHeader);
- retval = UA_SymmetricAlgorithmSecurityHeader_decodeBinary(chunk, &offset,
- &symmetricSecurityHeader);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
- /* Help fuzzing by always setting the correct tokenId */
- symmetricSecurityHeader.tokenId = channel->securityToken.tokenId;
- #endif
- retval = checkSymHeader(channel, symmetricSecurityHeader.tokenId);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- cryptoModule = &channel->securityPolicy->symmetricModule.cryptoModule;
- sequenceNumberCallback = processSequenceNumberSym;
- break;
- }
- /* OPN: Asymmetric encryption */
- case UA_MESSAGETYPE_OPN: {
- /* Chunking not allowed for OPN */
- if(chunkType != UA_CHUNKTYPE_FINAL)
- return UA_STATUSCODE_BADTCPMESSAGETYPEINVALID;
- /* Decode the asymmetric algorithm security header and call the callback
- * to perform checks. */
- UA_AsymmetricAlgorithmSecurityHeader asymHeader;
- UA_AsymmetricAlgorithmSecurityHeader_init(&asymHeader);
- offset = UA_SECURE_CONVERSATION_MESSAGE_HEADER_LENGTH;
- retval = UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(chunk, &offset, &asymHeader);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- retval = checkAsymHeader(channel, &asymHeader);
- UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- cryptoModule = &channel->securityPolicy->asymmetricModule.cryptoModule;
- sequenceNumberCallback = processSequenceNumberAsym;
- break;
- }
- /* Invalid message type */
- default:
- return UA_STATUSCODE_BADTCPMESSAGETYPEINVALID;
- }
- /* Decrypt message */
- UA_assert(cryptoModule != NULL);
- retval = decryptChunk(channel, cryptoModule, messageType, chunk, offset,
- &requestId, &sequenceNumber, &chunkPayload);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- #if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
- /* Check the sequence number. Skip sequence number checking for fuzzer to
- * improve coverage */
- if(sequenceNumberCallback == NULL)
- return UA_STATUSCODE_BADINTERNALERROR;
- retval = sequenceNumberCallback(channel, sequenceNumber);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- #endif
- /* Add the payload to the (new) message */
- addPayload:
- switch(chunkType) {
- case UA_CHUNKTYPE_INTERMEDIATE:
- case UA_CHUNKTYPE_FINAL:
- retval = addChunkPayload(channel, requestId, messageType,
- &chunkPayload, chunkType == UA_CHUNKTYPE_FINAL);
- break;
- case UA_CHUNKTYPE_ABORT:
- deleteLatestMessage(channel, requestId);
- break;
- default:
- retval = UA_STATUSCODE_BADTCPMESSAGETYPEINVALID;
- }
- return retval;
- }
- UA_StatusCode
- UA_SecureChannel_decryptAddChunk(UA_SecureChannel *channel, const UA_ByteString *chunk) {
- /* Has the SecureChannel timed out? */
- if(channel->state == UA_SECURECHANNELSTATE_CLOSED)
- return UA_STATUSCODE_BADSECURECHANNELCLOSED;
- if(channel->state == UA_SECURECHANNELSTATE_OPEN &&
- (channel->securityToken.createdAt +
- (channel->securityToken.revisedLifetime * UA_DATETIME_MSEC))
- < UA_DateTime_nowMonotonic()) {
- UA_SecureChannel_deleteMembersCleanup(channel);
- return UA_STATUSCODE_BADSECURECHANNELCLOSED;
- }
- UA_StatusCode retval = decryptAddChunk(channel, chunk);
- if(retval != UA_STATUSCODE_GOOD)
- UA_SecureChannel_deleteMembersCleanup(channel);
- return retval;
- }
- UA_StatusCode
- UA_SecureChannel_persistIncompleteMessages(UA_SecureChannel *channel) {
- UA_Message *me;
- TAILQ_FOREACH(me, &channel->messages, pointers) {
- UA_ChunkPayload *cp;
- SIMPLEQ_FOREACH(cp, &me->chunkPayloads, pointers) {
- if(cp->copied)
- continue;
- UA_ByteString copy;
- UA_StatusCode retval = UA_ByteString_copy(&cp->bytes, ©);
- if(retval != UA_STATUSCODE_GOOD) {
- UA_SecureChannel_deleteMembersCleanup(channel);
- return retval;
- }
- cp->bytes = copy;
- cp->copied = true;
- }
- }
- return UA_STATUSCODE_GOOD;
- }
- /* Functionality used by both the SecureChannel and the SecurityPolicy */
- size_t
- UA_SecurityPolicy_getRemoteAsymEncryptionBufferLengthOverhead(const UA_SecurityPolicy *securityPolicy,
- const void *channelContext,
- size_t maxEncryptionLength) {
- if(maxEncryptionLength == 0)
- return 0;
- size_t plainTextBlockSize = securityPolicy->asymmetricModule.cryptoModule.encryptionAlgorithm.
- getRemotePlainTextBlockSize(securityPolicy, channelContext);
- size_t encryptedBlockSize = securityPolicy->asymmetricModule.cryptoModule.encryptionAlgorithm.
- getRemoteBlockSize(securityPolicy, channelContext);
- if(plainTextBlockSize == 0)
- return 0;
- size_t maxNumberOfBlocks = maxEncryptionLength / plainTextBlockSize;
- return maxNumberOfBlocks * (encryptedBlockSize - plainTextBlockSize);
- }
|