Line data Source code
1 : /* 2 : * 3 : * Copyright (c) 2021 Project CHIP Authors 4 : * 5 : * Licensed under the Apache License, Version 2.0 (the "License"); 6 : * you may not use this file except in compliance with the License. 7 : * You may obtain a copy of the License at 8 : * 9 : * http://www.apache.org/licenses/LICENSE-2.0 10 : * 11 : * Unless required by applicable law or agreed to in writing, software 12 : * distributed under the License is distributed on an "AS IS" BASIS, 13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 : * See the License for the specific language governing permissions and 15 : * limitations under the License. 16 : */ 17 : /** 18 : * @file 19 : * This file defines types and objects for CHIP message counter messages in secure channel protocol. 20 : * 21 : */ 22 : 23 : #pragma once 24 : 25 : #include <messaging/ExchangeDelegate.h> 26 : #include <messaging/ExchangeMgr.h> 27 : #include <protocols/Protocols.h> 28 : #include <system/SystemPacketBuffer.h> 29 : #include <transport/MessageCounterManagerInterface.h> 30 : 31 : namespace chip { 32 : namespace secure_channel { 33 : 34 : class MessageCounterManager : public Messaging::UnsolicitedMessageHandler, 35 : public Messaging::ExchangeDelegate, 36 : public Transport::MessageCounterManagerInterface 37 : { 38 : public: 39 : static constexpr uint16_t kChallengeSize = Transport::PeerMessageCounter::kChallengeSize; 40 : static constexpr uint16_t kCounterSize = 4; 41 : static constexpr uint16_t kSyncRespMsgSize = kChallengeSize + kCounterSize; 42 : static constexpr System::Clock::Timeout kSyncTimeout = System::Clock::Milliseconds32(500); 43 : 44 0 : MessageCounterManager() : mExchangeMgr(nullptr) {} 45 0 : ~MessageCounterManager() override {} 46 : 47 : CHIP_ERROR Init(Messaging::ExchangeManager * exchangeMgr); 48 : void Shutdown(); 49 : 50 : // Implement MessageCounterManagerInterface 51 : CHIP_ERROR StartSync(const SessionHandle & session, Transport::SecureSession * state) override; 52 : CHIP_ERROR QueueReceivedMessageAndStartSync(const PacketHeader & packetHeader, const SessionHandle & session, 53 : Transport::SecureSession * state, const Transport::PeerAddress & peerAddress, 54 : System::PacketBufferHandle && msgBuf) override; 55 : 56 : /** 57 : * Send peer message counter synchronization request. 58 : * This function is called while processing a message encrypted with an application key from a peer whose message counter is not 59 : * synchronized. This message is sent on a newly created exchange, which is closed immediately after. 60 : * 61 : * @param[in] session The secure session handle of the received message. 62 : * 63 : * @retval #CHIP_ERROR_NO_MEMORY If memory could not be allocated for the new 64 : * exchange context or new message buffer. 65 : * @retval #CHIP_NO_ERROR On success. 66 : * 67 : */ 68 : CHIP_ERROR SendMsgCounterSyncReq(const SessionHandle & session, Transport::SecureSession * state); 69 : 70 : /** 71 : * Add a CHIP message into the cache table to queue the incoming messages that trigger message counter synchronization 72 : * protocol for re-processing. 73 : * 74 : * @param[in] msgBuf A handle to the packet buffer holding the received message. 75 : * 76 : * @retval #CHIP_ERROR_NO_MEMORY If there is no empty slot left in the table for addition. 77 : * @retval #CHIP_NO_ERROR On success. 78 : */ 79 : CHIP_ERROR AddToReceiveTable(const PacketHeader & packetHeader, const Transport::PeerAddress & peerAddress, 80 : System::PacketBufferHandle && msgBuf); 81 : 82 : private: 83 : /** 84 : * @class ReceiveTableEntry 85 : * 86 : * @brief 87 : * This class is part of the CHIP Message Counter Synchronization Protocol and is used 88 : * to keep track of a CHIP messages to be reprocessed whose source's 89 : * message counter is unknown. The message is reprocessed after message 90 : * counter synchronization is completed. 91 : * 92 : */ 93 : struct ReceiveTableEntry 94 : { 95 : Transport::PeerAddress peerAddress; /**< The peer address for the message*/ 96 : System::PacketBufferHandle msgBuf; /**< A handle to the PacketBuffer object holding the message data. */ 97 : }; 98 : 99 : Messaging::ExchangeManager * mExchangeMgr; // [READ ONLY] Associated Exchange Manager object. 100 : 101 : // MessageCounterManager cache table to queue the incoming messages that trigger message counter synchronization protocol. 102 : ReceiveTableEntry mReceiveTable[CHIP_CONFIG_MCSP_RECEIVE_TABLE_SIZE]; 103 : 104 : void ProcessPendingMessages(NodeId peerNodeId); 105 : 106 : CHIP_ERROR SendMsgCounterSyncResp(Messaging::ExchangeContext * exchangeContext, FixedByteSpan<kChallengeSize> challenge); 107 : CHIP_ERROR HandleMsgCounterSyncReq(Messaging::ExchangeContext * exchangeContext, System::PacketBufferHandle && msgBuf); 108 : CHIP_ERROR HandleMsgCounterSyncResp(Messaging::ExchangeContext * exchangeContext, System::PacketBufferHandle && msgBuf); 109 : 110 : CHIP_ERROR OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) override; 111 : CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * exchangeContext, const PayloadHeader & payloadHeader, 112 : System::PacketBufferHandle && payload) override; 113 : 114 : void OnResponseTimeout(Messaging::ExchangeContext * exchangeContext) override; 115 : }; 116 : 117 : } // namespace secure_channel 118 : } // namespace chip