ua_securechannel.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "ua_util.h"
  5. #include "ua_securechannel.h"
  6. #include "ua_session.h"
  7. #include "ua_types_encoding_binary.h"
  8. #include "ua_types_generated_encoding_binary.h"
  9. #include "ua_transport_generated_encoding_binary.h"
  10. #include "ua_types_generated_handling.h"
  11. #include "ua_transport_generated_handling.h"
  12. #include "ua_plugin_securitypolicy.h"
  13. #define UA_BITMASK_MESSAGETYPE 0x00ffffff
  14. #define UA_BITMASK_CHUNKTYPE 0xff000000
  15. #define UA_SECURE_MESSAGE_HEADER_LENGTH 24
  16. #define UA_ASYMMETRIC_ALG_SECURITY_HEADER_FIXED_LENGTH 12
  17. #define UA_SYMMETRIC_ALG_SECURITY_HEADER_LENGTH 4
  18. #define UA_SEQUENCE_HEADER_LENGTH 8
  19. #define UA_SECUREMH_AND_SYMALGH_LENGTH \
  20. (UA_SECURE_CONVERSATION_MESSAGE_HEADER_LENGTH + \
  21. UA_SYMMETRIC_ALG_SECURITY_HEADER_LENGTH)
  22. const UA_ByteString
  23. UA_SECURITY_POLICY_NONE_URI = { 47, (UA_Byte*)"http://opcfoundation.org/UA/SecurityPolicy#None" };
  24. UA_StatusCode
  25. UA_SecureChannel_init(UA_SecureChannel *channel,
  26. const UA_SecurityPolicy *securityPolicy,
  27. const UA_ByteString *remoteCertificate) {
  28. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  29. memset(channel, 0, sizeof(UA_SecureChannel));
  30. channel->state = UA_SECURECHANNELSTATE_FRESH;
  31. channel->securityPolicy = securityPolicy;
  32. retval = securityPolicy->channelModule.newContext(securityPolicy, remoteCertificate,
  33. &channel->channelContext);
  34. if(retval != UA_STATUSCODE_GOOD)
  35. return retval;
  36. retval = UA_ByteString_copy(remoteCertificate, &channel->remoteCertificate);
  37. if(retval != UA_STATUSCODE_GOOD)
  38. return retval;
  39. UA_ByteString remoteCertificateThumbprint = { 20, channel->remoteCertificateThumbprint };
  40. retval = securityPolicy->asymmetricModule.
  41. makeCertificateThumbprint(securityPolicy, &channel->remoteCertificate,
  42. &remoteCertificateThumbprint);
  43. return retval;
  44. /* Linked lists are also initialized by zeroing out */
  45. /* LIST_INIT(&channel->sessions); */
  46. /* LIST_INIT(&channel->chunks); */
  47. }
  48. void UA_SecureChannel_deleteMembersCleanup(UA_SecureChannel* channel) {
  49. /* Delete members */
  50. UA_ByteString_deleteMembers(&channel->remoteCertificate);
  51. UA_ByteString_deleteMembers(&channel->localNonce);
  52. UA_ByteString_deleteMembers(&channel->remoteNonce);
  53. UA_ChannelSecurityToken_deleteMembers(&channel->securityToken);
  54. UA_ChannelSecurityToken_deleteMembers(&channel->nextSecurityToken);
  55. /* Delete the channel context for the security policy */
  56. if(channel->securityPolicy)
  57. channel->securityPolicy->channelModule.deleteContext(channel->channelContext);
  58. /* Detach from the connection */
  59. if(channel->connection)
  60. UA_Connection_detachSecureChannel(channel->connection);
  61. /* Remove session pointers (not the sessions) */
  62. struct SessionEntry *se, *temp;
  63. LIST_FOREACH_SAFE(se, &channel->sessions, pointers, temp) {
  64. if(se->session)
  65. se->session->channel = NULL;
  66. LIST_REMOVE(se, pointers);
  67. UA_free(se);
  68. }
  69. /* Remove the buffered chunks */
  70. struct ChunkEntry *ch, *temp_ch;
  71. LIST_FOREACH_SAFE(ch, &channel->chunks, pointers, temp_ch) {
  72. UA_ByteString_deleteMembers(&ch->bytes);
  73. LIST_REMOVE(ch, pointers);
  74. UA_free(ch);
  75. }
  76. }
  77. UA_StatusCode UA_SecureChannel_generateNonce(const UA_SecureChannel *const channel,
  78. const size_t nonceLength,
  79. UA_ByteString *const nonce) {
  80. UA_ByteString_allocBuffer(nonce, nonceLength);
  81. if(!nonce->data)
  82. return UA_STATUSCODE_BADOUTOFMEMORY;
  83. return channel->securityPolicy->symmetricModule.generateNonce(channel->securityPolicy,
  84. nonce);
  85. }
  86. UA_StatusCode
  87. UA_SecureChannel_generateNewKeys(UA_SecureChannel *const channel) {
  88. const UA_SecurityPolicy *const securityPolicy = channel->securityPolicy;
  89. const UA_SecurityPolicyChannelModule *channelModule =
  90. &securityPolicy->channelModule;
  91. const UA_SecurityPolicySymmetricModule *symmetricModule =
  92. &securityPolicy->symmetricModule;
  93. /* Symmetric key length */
  94. size_t encryptionKeyLength = symmetricModule->cryptoModule.
  95. getLocalEncryptionKeyLength(securityPolicy, channel->channelContext);
  96. const size_t buffSize = symmetricModule->encryptionBlockSize +
  97. symmetricModule->signingKeyLength + encryptionKeyLength;
  98. UA_ByteString buffer = { buffSize, (UA_Byte*)UA_alloca(buffSize) };
  99. /* Remote keys */
  100. UA_StatusCode retval = symmetricModule->generateKey(securityPolicy, &channel->localNonce,
  101. &channel->remoteNonce, &buffer);
  102. if(retval != UA_STATUSCODE_GOOD)
  103. return retval;
  104. const UA_ByteString remoteSigningKey = { symmetricModule->signingKeyLength, buffer.data };
  105. const UA_ByteString remoteEncryptingKey =
  106. { encryptionKeyLength, buffer.data + symmetricModule->signingKeyLength };
  107. const UA_ByteString remoteIv = { symmetricModule->encryptionBlockSize,
  108. buffer.data + symmetricModule->signingKeyLength +
  109. encryptionKeyLength };
  110. retval = channelModule->setRemoteSymSigningKey(channel->channelContext, &remoteSigningKey);
  111. retval |= channelModule->setRemoteSymEncryptingKey(channel->channelContext, &remoteEncryptingKey);
  112. retval |= channelModule->setRemoteSymIv(channel->channelContext, &remoteIv);
  113. if(retval != UA_STATUSCODE_GOOD)
  114. return retval;
  115. /* Local keys */
  116. symmetricModule->generateKey(securityPolicy, &channel->remoteNonce, &channel->localNonce, &buffer);
  117. if(retval != UA_STATUSCODE_GOOD)
  118. return retval;
  119. const UA_ByteString localSigningKey = { symmetricModule->signingKeyLength, buffer.data };
  120. const UA_ByteString localEncryptingKey =
  121. { encryptionKeyLength, buffer.data + symmetricModule->signingKeyLength };
  122. const UA_ByteString localIv = { symmetricModule->encryptionBlockSize,
  123. buffer.data + symmetricModule->signingKeyLength +
  124. encryptionKeyLength };
  125. retval = channelModule->setLocalSymSigningKey(channel->channelContext, &localSigningKey);
  126. retval |= channelModule->setLocalSymEncryptingKey(channel->channelContext, &localEncryptingKey);
  127. retval |= channelModule->setLocalSymIv(channel->channelContext, &localIv);
  128. return retval;
  129. }
  130. void UA_SecureChannel_attachSession(UA_SecureChannel* channel, UA_Session* session) {
  131. struct SessionEntry* se = (struct SessionEntry *)UA_malloc(sizeof(struct SessionEntry));
  132. if(!se)
  133. return;
  134. se->session = session;
  135. if(UA_atomic_cmpxchg((void**)&session->channel, NULL, channel) != NULL) {
  136. UA_free(se);
  137. return;
  138. }
  139. LIST_INSERT_HEAD(&channel->sessions, se, pointers);
  140. }
  141. void UA_SecureChannel_detachSession(UA_SecureChannel* channel, UA_Session* session) {
  142. if(session)
  143. session->channel = NULL;
  144. struct SessionEntry* se;
  145. LIST_FOREACH(se, &channel->sessions, pointers) {
  146. if(se->session == session)
  147. break;
  148. }
  149. if(!se)
  150. return;
  151. LIST_REMOVE(se, pointers);
  152. UA_free(se);
  153. }
  154. UA_Session *
  155. UA_SecureChannel_getSession(UA_SecureChannel* channel, UA_NodeId* token) {
  156. struct SessionEntry* se;
  157. LIST_FOREACH(se, &channel->sessions, pointers) {
  158. if(UA_NodeId_equal(&se->session->authenticationToken, token))
  159. break;
  160. }
  161. if(!se)
  162. return NULL;
  163. return se->session;
  164. }
  165. UA_StatusCode UA_SecureChannel_revolveTokens(UA_SecureChannel* channel) {
  166. if(channel->nextSecurityToken.tokenId == 0) // no security token issued
  167. return UA_STATUSCODE_BADSECURECHANNELTOKENUNKNOWN;
  168. //FIXME: not thread-safe
  169. memcpy(&channel->securityToken, &channel->nextSecurityToken,
  170. sizeof(UA_ChannelSecurityToken));
  171. UA_ChannelSecurityToken_init(&channel->nextSecurityToken);
  172. return UA_SecureChannel_generateNewKeys(channel);
  173. }
  174. /***************************/
  175. /* Send Asymmetric Message */
  176. /***************************/
  177. static UA_UInt16
  178. calculatePaddingAsym(const UA_SecurityPolicy *securityPolicy, const void *channelContext,
  179. size_t bytesToWrite, UA_Byte *paddingSize, UA_Byte *extraPaddingSize) {
  180. size_t plainTextBlockSize = securityPolicy->channelModule.
  181. getRemoteAsymPlainTextBlockSize(channelContext);
  182. size_t signatureSize = securityPolicy->asymmetricModule.cryptoModule.
  183. getLocalSignatureSize(securityPolicy, channelContext);
  184. size_t padding = (plainTextBlockSize - ((bytesToWrite + signatureSize + 1) % plainTextBlockSize));
  185. *paddingSize = (UA_Byte)padding;
  186. *extraPaddingSize = (UA_Byte)(padding >> 8);
  187. return (UA_UInt16)padding;
  188. }
  189. static size_t
  190. calculateAsymAlgSecurityHeaderLength(const UA_SecureChannel *channel) {
  191. size_t asymHeaderLength = UA_ASYMMETRIC_ALG_SECURITY_HEADER_FIXED_LENGTH +
  192. channel->securityPolicy->policyUri.length;
  193. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
  194. channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  195. asymHeaderLength += channel->securityPolicy->localCertificate.length;
  196. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  197. asymHeaderLength += 20; /* Thumbprints are always 20 byte long */
  198. return asymHeaderLength;
  199. }
  200. static void
  201. hideBytesAsym(UA_SecureChannel *const channel, UA_Byte **const buf_start,
  202. const UA_Byte **const buf_end) {
  203. const UA_SecurityPolicy *const securityPolicy = channel->securityPolicy;
  204. *buf_start += UA_SECURE_CONVERSATION_MESSAGE_HEADER_LENGTH + UA_SEQUENCE_HEADER_LENGTH;
  205. /* Add the SecurityHeaderLength */
  206. *buf_start += calculateAsymAlgSecurityHeaderLength(channel);
  207. size_t potentialEncryptionMaxSize = (size_t)(*buf_end - *buf_start) + UA_SEQUENCE_HEADER_LENGTH;
  208. /* Hide bytes for signature and padding */
  209. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
  210. channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
  211. *buf_end -= securityPolicy->asymmetricModule.cryptoModule.
  212. getLocalSignatureSize(securityPolicy, channel->channelContext);
  213. *buf_end -= 2; // padding byte and extraPadding byte
  214. /* Add some overhead length due to RSA implementations adding a signature themselves */
  215. *buf_end -= securityPolicy->channelModule
  216. .getRemoteAsymEncryptionBufferLengthOverhead(channel->channelContext,
  217. potentialEncryptionMaxSize);
  218. }
  219. }
  220. /* Sends an OPN message using asymmetric encryption if defined */
  221. UA_StatusCode
  222. UA_SecureChannel_sendAsymmetricOPNMessage(UA_SecureChannel *channel, UA_UInt32 requestId,
  223. const void *content, const UA_DataType *contentType) {
  224. const UA_SecurityPolicy *const securityPolicy = channel->securityPolicy;
  225. UA_Connection* connection = channel->connection;
  226. if(!connection)
  227. return UA_STATUSCODE_BADINTERNALERROR;
  228. /* Allocate the message buffer */
  229. UA_ByteString buf = UA_BYTESTRING_NULL;
  230. UA_StatusCode retval =
  231. connection->getSendBuffer(connection, connection->localConf.sendBufferSize, &buf);
  232. if(retval != UA_STATUSCODE_GOOD)
  233. return retval;
  234. /* Restrict buffer to the available space for the payload */
  235. UA_Byte *buf_pos = buf.data;
  236. const UA_Byte *buf_end = &buf.data[buf.length];
  237. hideBytesAsym(channel, &buf_pos, &buf_end);
  238. /* Encode the message type and content */
  239. UA_NodeId typeId = UA_NODEID_NUMERIC(0, contentType->binaryEncodingId);
  240. retval = UA_encodeBinary(&typeId, &UA_TYPES[UA_TYPES_NODEID],
  241. &buf_pos, &buf_end, NULL, NULL);
  242. retval |= UA_encodeBinary(content, contentType, &buf_pos, &buf_end, NULL, NULL);
  243. if(retval != UA_STATUSCODE_GOOD) {
  244. connection->releaseSendBuffer(connection, &buf);
  245. return retval;
  246. }
  247. /* Compute the length of the asym header */
  248. const size_t securityHeaderLength = calculateAsymAlgSecurityHeaderLength(channel);
  249. /* Pad the message. Also if securitymode is only sign, since we are using
  250. * asymmetric communication to exchange keys and thus need to encrypt. */
  251. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
  252. channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
  253. const UA_Byte *buf_body_start =
  254. &buf.data[UA_SECURE_CONVERSATION_MESSAGE_HEADER_LENGTH +
  255. UA_SEQUENCE_HEADER_LENGTH + securityHeaderLength];
  256. const size_t bytesToWrite =
  257. (uintptr_t)buf_pos - (uintptr_t)buf_body_start + UA_SEQUENCE_HEADER_LENGTH;
  258. UA_Byte paddingSize = 0;
  259. UA_Byte extraPaddingSize = 0;
  260. UA_UInt16 totalPaddingSize =
  261. calculatePaddingAsym(securityPolicy, channel->channelContext,
  262. bytesToWrite, &paddingSize, &extraPaddingSize);
  263. for(UA_UInt16 i = 0; i < totalPaddingSize; ++i) {
  264. *buf_pos = paddingSize;
  265. ++buf_pos;
  266. }
  267. if(extraPaddingSize > 0) {
  268. *buf_pos = extraPaddingSize;
  269. ++buf_pos;
  270. }
  271. }
  272. /* The total message length */
  273. size_t pre_sig_length = (uintptr_t)buf_pos - (uintptr_t)buf.data;
  274. size_t total_length = pre_sig_length;
  275. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
  276. channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  277. total_length += securityPolicy->asymmetricModule.cryptoModule.
  278. getLocalSignatureSize(securityPolicy, channel->channelContext);
  279. /* Encode the headers at the beginning of the message */
  280. UA_Byte *header_pos = buf.data;
  281. size_t dataToEncryptLength = total_length -
  282. (UA_SECURE_CONVERSATION_MESSAGE_HEADER_LENGTH + securityHeaderLength);
  283. UA_SecureConversationMessageHeader respHeader;
  284. respHeader.messageHeader.messageTypeAndChunkType = UA_MESSAGETYPE_OPN + UA_CHUNKTYPE_FINAL;
  285. respHeader.messageHeader.messageSize = (UA_UInt32)
  286. (total_length + securityPolicy->channelModule.
  287. getRemoteAsymEncryptionBufferLengthOverhead(channel->channelContext, dataToEncryptLength));
  288. respHeader.secureChannelId = channel->securityToken.channelId;
  289. retval = UA_encodeBinary(&respHeader, &UA_TRANSPORT[UA_TRANSPORT_SECURECONVERSATIONMESSAGEHEADER],
  290. &header_pos, &buf_end, NULL, NULL);
  291. UA_AsymmetricAlgorithmSecurityHeader asymHeader;
  292. UA_AsymmetricAlgorithmSecurityHeader_init(&asymHeader);
  293. asymHeader.securityPolicyUri = channel->securityPolicy->policyUri;
  294. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
  295. channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  296. asymHeader.senderCertificate = channel->securityPolicy->localCertificate;
  297. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
  298. asymHeader.receiverCertificateThumbprint.length = 20;
  299. asymHeader.receiverCertificateThumbprint.data = channel->remoteCertificateThumbprint;
  300. }
  301. retval |= UA_encodeBinary(&asymHeader, &UA_TRANSPORT[UA_TRANSPORT_ASYMMETRICALGORITHMSECURITYHEADER],
  302. &header_pos, &buf_end, NULL, NULL);
  303. UA_SequenceHeader seqHeader;
  304. seqHeader.requestId = requestId;
  305. seqHeader.sequenceNumber = UA_atomic_add(&channel->sendSequenceNumber, 1);
  306. retval |= UA_encodeBinary(&seqHeader, &UA_TRANSPORT[UA_TRANSPORT_SEQUENCEHEADER],
  307. &header_pos, &buf_end, NULL, NULL);
  308. /* Did encoding the header succeed? */
  309. if(retval != UA_STATUSCODE_GOOD) {
  310. connection->releaseSendBuffer(connection, &buf);
  311. return retval;
  312. }
  313. /* Sign message */
  314. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
  315. channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
  316. const UA_ByteString dataToSign = { pre_sig_length, buf.data };
  317. size_t sigsize = securityPolicy->asymmetricModule.cryptoModule.
  318. getLocalSignatureSize(securityPolicy, channel->channelContext);
  319. UA_ByteString signature = { sigsize, buf.data + pre_sig_length };
  320. securityPolicy->asymmetricModule.cryptoModule.
  321. sign(securityPolicy, channel->channelContext, &dataToSign, &signature);
  322. }
  323. /* Encrypt message if mode not none */
  324. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
  325. channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
  326. size_t unencrypted_length =
  327. UA_SECURE_CONVERSATION_MESSAGE_HEADER_LENGTH + securityHeaderLength;
  328. UA_ByteString dataToEncrypt = { total_length - unencrypted_length,
  329. &buf.data[unencrypted_length] };
  330. securityPolicy->asymmetricModule.cryptoModule.
  331. encrypt(securityPolicy, channel->channelContext, &dataToEncrypt);
  332. }
  333. /* Send the message, the buffer is freed in the network layer */
  334. buf.length = respHeader.messageHeader.messageSize;
  335. return connection->send(connection, &buf);
  336. }
  337. /**************************/
  338. /* Send Symmetric Message */
  339. /**************************/
  340. static UA_UInt16
  341. calculatePaddingSym(const UA_SecurityPolicy *securityPolicy, const void *channelContext,
  342. size_t bytesToWrite, UA_Byte *paddingSize, UA_Byte *extraPaddingSize) {
  343. UA_UInt16 padding = (UA_UInt16)(securityPolicy->symmetricModule.encryptionBlockSize -
  344. ((bytesToWrite + securityPolicy->symmetricModule.cryptoModule.
  345. getLocalSignatureSize(securityPolicy, channelContext) + 1) %
  346. securityPolicy->symmetricModule.encryptionBlockSize));
  347. *paddingSize = (UA_Byte)padding;
  348. *extraPaddingSize = (UA_Byte)(padding >> 8);
  349. return padding;
  350. }
  351. /* Sends a message using symmetric encryption if defined
  352. *
  353. * @param ci the chunk information that is used to send the chunk.
  354. * @param buf_pos the position in the send buffer after the body was encoded.
  355. * Should be less than or equal to buf_end.
  356. * @param buf_end the maximum position of the body. */
  357. static UA_StatusCode
  358. sendChunkSymmetric(UA_ChunkInfo* ci, UA_Byte **buf_pos, const UA_Byte **buf_end) {
  359. UA_SecureChannel* const channel = ci->channel;
  360. const UA_SecurityPolicy *securityPolicy = channel->securityPolicy;
  361. UA_Connection* const connection = channel->connection;
  362. if(!connection)
  363. return UA_STATUSCODE_BADINTERNALERROR;
  364. /* Will this chunk surpass the capacity of the SecureChannel for the message? */
  365. UA_Byte *buf_body_start = ci->messageBuffer.data + UA_SECURE_MESSAGE_HEADER_LENGTH;
  366. UA_Byte *buf_body_end = *buf_pos;
  367. size_t bodyLength = (uintptr_t)buf_body_end - (uintptr_t)buf_body_start;
  368. ci->messageSizeSoFar += bodyLength;
  369. ci->chunksSoFar++;
  370. if(ci->messageSizeSoFar > connection->remoteConf.maxMessageSize &&
  371. connection->remoteConf.maxMessageSize != 0)
  372. ci->errorCode = UA_STATUSCODE_BADRESPONSETOOLARGE;
  373. if(ci->chunksSoFar > connection->remoteConf.maxChunkCount &&
  374. connection->remoteConf.maxChunkCount != 0)
  375. ci->errorCode = UA_STATUSCODE_BADRESPONSETOOLARGE;
  376. if(ci->errorCode != UA_STATUSCODE_GOOD)
  377. return ci->errorCode;
  378. /* Pad the message. The bytes for the padding and signature were removed
  379. * from buf_end before encoding the payload. So we don't check here. */
  380. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
  381. size_t bytesToWrite = bodyLength + UA_SEQUENCE_HEADER_LENGTH;
  382. UA_Byte paddingSize = 0;
  383. UA_Byte extraPaddingSize = 0;
  384. UA_UInt16 totalPaddingSize =
  385. calculatePaddingSym(securityPolicy, channel->channelContext,
  386. bytesToWrite, &paddingSize, &extraPaddingSize);
  387. for(UA_UInt16 i = 0; i < totalPaddingSize; ++i) {
  388. **buf_pos = paddingSize;
  389. ++(*buf_pos);
  390. }
  391. if(extraPaddingSize > 0) {
  392. **buf_pos = extraPaddingSize;
  393. ++(*buf_pos);
  394. }
  395. }
  396. /* The total message length */
  397. size_t pre_sig_length = (uintptr_t)(*buf_pos) - (uintptr_t)ci->messageBuffer.data;
  398. size_t total_length = pre_sig_length;
  399. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
  400. channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  401. total_length += securityPolicy->symmetricModule.cryptoModule.
  402. getLocalSignatureSize(securityPolicy, channel->channelContext);
  403. /* Encode the chunk headers at the beginning of the buffer */
  404. UA_Byte *header_pos = ci->messageBuffer.data;
  405. UA_SecureConversationMessageHeader respHeader;
  406. respHeader.secureChannelId = channel->securityToken.channelId;
  407. respHeader.messageHeader.messageTypeAndChunkType = ci->messageType;
  408. respHeader.messageHeader.messageSize = (UA_UInt32)total_length;
  409. if(ci->errorCode == UA_STATUSCODE_GOOD) {
  410. if(ci->final)
  411. respHeader.messageHeader.messageTypeAndChunkType += UA_CHUNKTYPE_FINAL;
  412. else
  413. respHeader.messageHeader.messageTypeAndChunkType += UA_CHUNKTYPE_INTERMEDIATE;
  414. } else {
  415. respHeader.messageHeader.messageTypeAndChunkType += UA_CHUNKTYPE_ABORT;
  416. }
  417. ci->errorCode |= UA_encodeBinary(&respHeader,
  418. &UA_TRANSPORT[UA_TRANSPORT_SECURECONVERSATIONMESSAGEHEADER],
  419. &header_pos, buf_end, NULL, NULL);
  420. UA_SymmetricAlgorithmSecurityHeader symSecHeader;
  421. symSecHeader.tokenId = channel->securityToken.tokenId;
  422. ci->errorCode |= UA_encodeBinary(&symSecHeader.tokenId,
  423. &UA_TRANSPORT[UA_TRANSPORT_SYMMETRICALGORITHMSECURITYHEADER],
  424. &header_pos, buf_end, NULL, NULL);
  425. UA_SequenceHeader seqHeader;
  426. seqHeader.requestId = ci->requestId;
  427. seqHeader.sequenceNumber = UA_atomic_add(&channel->sendSequenceNumber, 1);
  428. ci->errorCode |= UA_encodeBinary(&seqHeader, &UA_TRANSPORT[UA_TRANSPORT_SEQUENCEHEADER],
  429. &header_pos, buf_end, NULL, NULL);
  430. /* Sign message */
  431. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
  432. channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
  433. UA_ByteString dataToSign = ci->messageBuffer;
  434. dataToSign.length = pre_sig_length;
  435. UA_ByteString signature;
  436. signature.length = securityPolicy->symmetricModule.cryptoModule.
  437. getLocalSignatureSize(securityPolicy, channel->channelContext);
  438. signature.data = *buf_pos;
  439. securityPolicy->symmetricModule.cryptoModule.
  440. sign(securityPolicy, channel->channelContext, &dataToSign, &signature);
  441. }
  442. /* Encrypt message */
  443. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
  444. UA_ByteString dataToEncrypt;
  445. dataToEncrypt.data = ci->messageBuffer.data + UA_SECUREMH_AND_SYMALGH_LENGTH;
  446. dataToEncrypt.length = total_length - UA_SECUREMH_AND_SYMALGH_LENGTH;
  447. securityPolicy->symmetricModule.cryptoModule.
  448. encrypt(securityPolicy, channel->channelContext, &dataToEncrypt);
  449. }
  450. /* Send the chunk, the buffer is freed in the network layer */
  451. ci->messageBuffer.length = respHeader.messageHeader.messageSize;
  452. connection->send(channel->connection, &ci->messageBuffer);
  453. /* Replace with the buffer for the next chunk */
  454. if(!ci->final && ci->errorCode == UA_STATUSCODE_GOOD) {
  455. UA_StatusCode retval =
  456. connection->getSendBuffer(connection, connection->localConf.sendBufferSize,
  457. &ci->messageBuffer);
  458. if(retval != UA_STATUSCODE_GOOD)
  459. return retval;
  460. /* Forward the data pointer so that the payload is encoded after the
  461. * message header */
  462. *buf_pos = &ci->messageBuffer.data[UA_SECURE_MESSAGE_HEADER_LENGTH];
  463. *buf_end = &ci->messageBuffer.data[ci->messageBuffer.length];
  464. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
  465. channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  466. *buf_end -= securityPolicy->symmetricModule.cryptoModule.
  467. getLocalSignatureSize(securityPolicy, channel->channelContext);
  468. /* Hide a byte needed for padding */
  469. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  470. *buf_end -= 2;
  471. }
  472. return ci->errorCode;
  473. }
  474. UA_StatusCode
  475. UA_SecureChannel_sendSymmetricMessage(UA_SecureChannel* channel, UA_UInt32 requestId,
  476. UA_MessageType messageType, const void *content,
  477. const UA_DataType *contentType) {
  478. const UA_SecurityPolicy *const securityPolicy = channel->securityPolicy;
  479. UA_Connection* connection = channel->connection;
  480. if(!connection)
  481. return UA_STATUSCODE_BADINTERNALERROR;
  482. /* Minimum required size */
  483. if(connection->localConf.sendBufferSize <= UA_SECURE_MESSAGE_HEADER_LENGTH)
  484. return UA_STATUSCODE_BADRESPONSETOOLARGE;
  485. /* Create the chunking info structure */
  486. UA_ChunkInfo ci;
  487. ci.channel = channel;
  488. ci.requestId = requestId;
  489. ci.chunksSoFar = 0;
  490. ci.messageSizeSoFar = 0;
  491. ci.final = false;
  492. ci.errorCode = UA_STATUSCODE_GOOD;
  493. ci.messageBuffer = UA_BYTESTRING_NULL;
  494. ci.messageType = messageType;
  495. /* Allocate the message buffer */
  496. UA_StatusCode retval =
  497. connection->getSendBuffer(connection, connection->localConf.sendBufferSize,
  498. &ci.messageBuffer);
  499. if(retval != UA_STATUSCODE_GOOD)
  500. return retval;
  501. /* Hide the message beginning where the header will be encoded */
  502. UA_Byte *buf_start = &ci.messageBuffer.data[UA_SECURE_MESSAGE_HEADER_LENGTH];
  503. const UA_Byte *buf_end = &ci.messageBuffer.data[ci.messageBuffer.length];
  504. /* Hide bytes for signature */
  505. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
  506. channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  507. buf_end -= securityPolicy->symmetricModule.cryptoModule.
  508. getLocalSignatureSize(securityPolicy, channel->channelContext);
  509. /* Hide one byte for padding */
  510. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  511. buf_end -= 2;
  512. /* Encode the message type */
  513. UA_NodeId typeId = UA_NODEID_NUMERIC(0, contentType->binaryEncodingId);
  514. retval = UA_encodeBinary(&typeId, &UA_TYPES[UA_TYPES_NODEID],
  515. &buf_start, &buf_end, NULL, NULL);
  516. /* Encode with the chunking callback */
  517. retval |= UA_encodeBinary(content, contentType, &buf_start, &buf_end,
  518. (UA_exchangeEncodeBuffer)sendChunkSymmetric, &ci);
  519. /* Encoding failed, release the message */
  520. if(retval != UA_STATUSCODE_GOOD) {
  521. if(!ci.final) {
  522. /* the abort message was not sent */
  523. ci.errorCode = retval;
  524. sendChunkSymmetric(&ci, &buf_start, &buf_end);
  525. }
  526. return retval;
  527. }
  528. /* Encoding finished, send the final chunk */
  529. ci.final = UA_TRUE;
  530. return sendChunkSymmetric(&ci, &buf_start, &buf_end);
  531. }
  532. /*****************************/
  533. /* Assemble Complete Message */
  534. /*****************************/
  535. static void
  536. UA_SecureChannel_removeChunks(UA_SecureChannel *channel, UA_UInt32 requestId) {
  537. struct ChunkEntry *ch;
  538. LIST_FOREACH(ch, &channel->chunks, pointers) {
  539. if(ch->requestId == requestId) {
  540. UA_ByteString_deleteMembers(&ch->bytes);
  541. LIST_REMOVE(ch, pointers);
  542. UA_free(ch);
  543. return;
  544. }
  545. }
  546. }
  547. static UA_StatusCode
  548. appendChunk(struct ChunkEntry* const chunkEntry, const UA_ByteString* const chunkBody) {
  549. UA_Byte* new_bytes = (UA_Byte*)
  550. UA_realloc(chunkEntry->bytes.data, chunkEntry->bytes.length + chunkBody->length);
  551. if(!new_bytes) {
  552. UA_ByteString_deleteMembers(&chunkEntry->bytes);
  553. return UA_STATUSCODE_BADOUTOFMEMORY;
  554. }
  555. chunkEntry->bytes.data = new_bytes;
  556. memcpy(&chunkEntry->bytes.data[chunkEntry->bytes.length], chunkBody->data, chunkBody->length);
  557. chunkEntry->bytes.length += chunkBody->length;
  558. return UA_STATUSCODE_GOOD;
  559. }
  560. static UA_StatusCode
  561. UA_SecureChannel_appendChunk(UA_SecureChannel* channel, UA_UInt32 requestId,
  562. const UA_ByteString* chunkBody) {
  563. struct ChunkEntry* ch;
  564. LIST_FOREACH(ch, &channel->chunks, pointers) {
  565. if(ch->requestId == requestId)
  566. break;
  567. }
  568. /* No chunkentry on the channel, create one */
  569. if(!ch) {
  570. ch = (struct ChunkEntry *)UA_malloc(sizeof(struct ChunkEntry));
  571. if(!ch)
  572. return UA_STATUSCODE_BADOUTOFMEMORY;
  573. ch->requestId = requestId;
  574. UA_ByteString_init(&ch->bytes);
  575. LIST_INSERT_HEAD(&channel->chunks, ch, pointers);
  576. }
  577. return appendChunk(ch, chunkBody);
  578. }
  579. static UA_StatusCode
  580. UA_SecureChannel_finalizeChunk(UA_SecureChannel *channel, UA_UInt32 requestId,
  581. const UA_ByteString *const chunkBody, UA_MessageType messageType,
  582. UA_ProcessMessageCallback callback, void *application) {
  583. struct ChunkEntry* chunkEntry;
  584. LIST_FOREACH(chunkEntry, &channel->chunks, pointers) {
  585. if(chunkEntry->requestId == requestId)
  586. break;
  587. }
  588. UA_ByteString bytes;
  589. if(!chunkEntry) {
  590. bytes = *chunkBody;
  591. } else {
  592. UA_StatusCode retval = appendChunk(chunkEntry, chunkBody);
  593. if(retval != UA_STATUSCODE_GOOD)
  594. return retval;
  595. bytes = chunkEntry->bytes;
  596. LIST_REMOVE(chunkEntry, pointers);
  597. UA_free(chunkEntry);
  598. }
  599. UA_StatusCode retval = callback(application, channel, messageType, requestId, &bytes);
  600. if(chunkEntry)
  601. UA_ByteString_deleteMembers(&bytes);
  602. return retval;
  603. }
  604. /****************************/
  605. /* Process a received Chunk */
  606. /****************************/
  607. static UA_StatusCode
  608. decryptChunk(UA_SecureChannel *channel, const UA_SecurityPolicyCryptoModule *cryptoModule,
  609. UA_ByteString *chunk, size_t offset, UA_UInt32 *requestId,
  610. UA_UInt32 *sequenceNumber, UA_ByteString *payload,
  611. UA_MessageType messageType) {
  612. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  613. const UA_SecurityPolicy *securityPolicy = channel->securityPolicy;
  614. if(cryptoModule == NULL)
  615. return UA_STATUSCODE_BADINTERNALERROR;
  616. /* Decrypt the chunk. Always decrypt opn messages if mode not none */
  617. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT ||
  618. messageType == UA_MESSAGETYPE_OPN) {
  619. UA_ByteString cipherText = { chunk->length - offset, chunk->data + offset };
  620. retval = cryptoModule->decrypt(securityPolicy, channel->channelContext, &cipherText);
  621. if(retval != UA_STATUSCODE_GOOD)
  622. return retval;
  623. }
  624. /* Verify the chunk signature */
  625. size_t sigsize = 0;
  626. size_t paddingSize = 0;
  627. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
  628. channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT ||
  629. messageType == UA_MESSAGETYPE_OPN) {
  630. /* Compute the padding size */
  631. sigsize = cryptoModule->getRemoteSignatureSize(securityPolicy, channel->channelContext);
  632. if(channel->securityMode != UA_MESSAGESECURITYMODE_NONE)
  633. paddingSize = chunk->data[chunk->length - sigsize - 1];
  634. size_t keyLength =
  635. cryptoModule->getRemoteEncryptionKeyLength(securityPolicy, channel->channelContext);
  636. if(keyLength > 2048) {
  637. paddingSize <<= 8; /* Extra padding size */
  638. paddingSize += chunk->data[chunk->length - sigsize - 2];
  639. }
  640. if(offset + paddingSize + sigsize >= chunk->length)
  641. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  642. /* Verify the signature */
  643. const UA_ByteString chunkDataToVerify = { chunk->length - sigsize, chunk->data };
  644. const UA_ByteString signature = { sigsize, chunk->data + chunk->length - sigsize };
  645. retval = securityPolicy->asymmetricModule.cryptoModule.
  646. verify(securityPolicy, channel->channelContext, &chunkDataToVerify, &signature);
  647. if(retval != UA_STATUSCODE_GOOD)
  648. return retval;
  649. }
  650. /* Decode the sequence header */
  651. UA_SequenceHeader sequenceHeader;
  652. retval = UA_SequenceHeader_decodeBinary(chunk, &offset, &sequenceHeader);
  653. if(retval != UA_STATUSCODE_GOOD)
  654. return retval;
  655. *requestId = sequenceHeader.requestId;
  656. *sequenceNumber = sequenceHeader.sequenceNumber;
  657. payload->data = chunk->data + offset;
  658. payload->length = chunk->length - offset - sigsize - paddingSize;
  659. return UA_STATUSCODE_GOOD;
  660. }
  661. typedef UA_StatusCode(*UA_SequenceNumberCallback)(UA_SecureChannel *channel,
  662. UA_UInt32 sequenceNumber);
  663. static UA_StatusCode
  664. processSequenceNumberAsym(UA_SecureChannel *const channel, UA_UInt32 sequenceNumber) {
  665. channel->receiveSequenceNumber = sequenceNumber;
  666. return UA_STATUSCODE_GOOD;
  667. }
  668. // TODO: We somehow need to make sure that a sequence number is never reused for the same tokenId
  669. static UA_StatusCode
  670. processSequenceNumberSym(UA_SecureChannel *const channel, UA_UInt32 sequenceNumber) {
  671. /* Does the sequence number match? */
  672. if(sequenceNumber != channel->receiveSequenceNumber + 1) {
  673. if(channel->receiveSequenceNumber + 1 > 4294966271 && sequenceNumber < 1024) // FIXME: Remove magic numbers :(
  674. channel->receiveSequenceNumber = sequenceNumber - 1; /* Roll over */
  675. else
  676. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  677. }
  678. ++channel->receiveSequenceNumber;
  679. return UA_STATUSCODE_GOOD;
  680. }
  681. static UA_StatusCode
  682. checkAsymHeader(UA_SecureChannel *const channel,
  683. UA_AsymmetricAlgorithmSecurityHeader *const asymHeader) {
  684. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  685. const UA_SecurityPolicy *const securityPolicy = channel->securityPolicy;
  686. if(!UA_ByteString_equal(&securityPolicy->policyUri, &asymHeader->securityPolicyUri)) {
  687. return UA_STATUSCODE_BADSECURITYPOLICYREJECTED;
  688. }
  689. // TODO: Verify certificate using certificate plugin. This will come with a new PR
  690. /* Something like this
  691. retval = certificateManager->verify(certificateStore??, &asymHeader->senderCertificate);
  692. if(retval != UA_STATUSCODE_GOOD)
  693. return retval;
  694. */
  695. retval = securityPolicy->asymmetricModule.
  696. compareCertificateThumbprint(securityPolicy, &asymHeader->receiverCertificateThumbprint);
  697. if(retval != UA_STATUSCODE_GOOD) {
  698. return retval;
  699. }
  700. return UA_STATUSCODE_GOOD;
  701. }
  702. static UA_StatusCode
  703. checkSymHeader(UA_SecureChannel *const channel,
  704. const UA_UInt32 tokenId) {
  705. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  706. if(tokenId != channel->securityToken.tokenId) {
  707. if(tokenId != channel->nextSecurityToken.tokenId)
  708. return UA_STATUSCODE_BADSECURECHANNELTOKENUNKNOWN;
  709. return UA_SecureChannel_revolveTokens(channel);
  710. }
  711. #endif
  712. return UA_STATUSCODE_GOOD;
  713. }
  714. UA_StatusCode
  715. UA_SecureChannel_processChunk(UA_SecureChannel *channel, UA_ByteString *chunk,
  716. UA_ProcessMessageCallback callback,
  717. void *application) {
  718. /* Decode message header */
  719. size_t offset = 0;
  720. UA_SecureConversationMessageHeader messageHeader;
  721. UA_StatusCode retval =
  722. UA_SecureConversationMessageHeader_decodeBinary(chunk, &offset, &messageHeader);
  723. if(retval != UA_STATUSCODE_GOOD)
  724. return retval;
  725. #if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
  726. /* The wrong ChannelId. Non-opened channels have the id zero. */
  727. if(messageHeader.secureChannelId != channel->securityToken.channelId &&
  728. channel->state != UA_SECURECHANNELSTATE_FRESH)
  729. return UA_STATUSCODE_BADSECURECHANNELIDINVALID;
  730. #endif
  731. UA_MessageType messageType = (UA_MessageType)
  732. (messageHeader.messageHeader.messageTypeAndChunkType & UA_BITMASK_MESSAGETYPE);
  733. UA_ChunkType chunkType = (UA_ChunkType)
  734. (messageHeader.messageHeader.messageTypeAndChunkType & UA_BITMASK_CHUNKTYPE);
  735. /* ERR message (not encrypted) */
  736. UA_UInt32 requestId = 0;
  737. UA_UInt32 sequenceNumber = 0;
  738. UA_ByteString chunkPayload;
  739. const UA_SecurityPolicyCryptoModule *cryptoModule = NULL;
  740. UA_SequenceNumberCallback sequenceNumberCallback = NULL;
  741. switch(messageType) {
  742. case UA_MESSAGETYPE_ERR:
  743. {
  744. if(chunkType != UA_CHUNKTYPE_FINAL)
  745. return UA_STATUSCODE_BADTCPMESSAGETYPEINVALID;
  746. chunkPayload.length = chunk->length - offset;
  747. chunkPayload.data = chunk->data + offset;
  748. return callback(application, channel, messageType, requestId, &chunkPayload);
  749. }
  750. case UA_MESSAGETYPE_MSG:
  751. case UA_MESSAGETYPE_CLO:
  752. {
  753. /* Decode and check the symmetric security header (tokenId) */
  754. UA_SymmetricAlgorithmSecurityHeader symmetricSecurityHeader;
  755. UA_SymmetricAlgorithmSecurityHeader_init(&symmetricSecurityHeader);
  756. retval = UA_SymmetricAlgorithmSecurityHeader_decodeBinary(chunk, &offset,
  757. &symmetricSecurityHeader);
  758. if(retval != UA_STATUSCODE_GOOD)
  759. return retval;
  760. retval = checkSymHeader(channel, symmetricSecurityHeader.tokenId);
  761. if(retval != UA_STATUSCODE_GOOD)
  762. return retval;
  763. cryptoModule = &channel->securityPolicy->symmetricModule.cryptoModule;
  764. sequenceNumberCallback = processSequenceNumberSym;
  765. break;
  766. }
  767. case UA_MESSAGETYPE_OPN:
  768. {
  769. /* Chunking not allowed for OPN */
  770. if(chunkType != UA_CHUNKTYPE_FINAL)
  771. return UA_STATUSCODE_BADTCPMESSAGETYPEINVALID;
  772. // Decode the asymmetric algorithm security header and
  773. // call the callback to perform checks.
  774. UA_AsymmetricAlgorithmSecurityHeader asymHeader;
  775. UA_AsymmetricAlgorithmSecurityHeader_init(&asymHeader);
  776. offset = UA_SECURE_CONVERSATION_MESSAGE_HEADER_LENGTH;
  777. retval = UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(chunk,
  778. &offset,
  779. &asymHeader);
  780. if(retval != UA_STATUSCODE_GOOD)
  781. break;
  782. retval = checkAsymHeader(channel, &asymHeader);
  783. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
  784. if(retval != UA_STATUSCODE_GOOD)
  785. break;
  786. cryptoModule = &channel->securityPolicy->asymmetricModule.cryptoModule;
  787. sequenceNumberCallback = processSequenceNumberAsym;
  788. break;
  789. }
  790. default:
  791. return UA_STATUSCODE_BADTCPMESSAGETYPEINVALID;
  792. }
  793. /* Decrypt message */
  794. retval = decryptChunk(channel, cryptoModule, chunk, offset, &requestId,
  795. &sequenceNumber, &chunkPayload, messageType);
  796. if(retval != UA_STATUSCODE_GOOD)
  797. return retval;
  798. /* Check the sequence number */
  799. if(sequenceNumberCallback == NULL)
  800. return UA_STATUSCODE_BADINTERNALERROR;
  801. retval = sequenceNumberCallback(channel, sequenceNumber);
  802. if(retval != UA_STATUSCODE_GOOD)
  803. return retval;
  804. /* Process the payload */
  805. if(chunkType == UA_CHUNKTYPE_FINAL) {
  806. retval = UA_SecureChannel_finalizeChunk(channel, requestId, &chunkPayload,
  807. messageType, callback, application);
  808. } else if(chunkType == UA_CHUNKTYPE_INTERMEDIATE) {
  809. retval = UA_SecureChannel_appendChunk(channel, requestId, &chunkPayload);
  810. } else if(chunkType == UA_CHUNKTYPE_ABORT) {
  811. UA_SecureChannel_removeChunks(channel, requestId);
  812. } else {
  813. retval = UA_STATUSCODE_BADTCPMESSAGETYPEINVALID;
  814. }
  815. return retval;
  816. }