message_parsers: Use local session instance

In message_parsers, it was calling `getSession()` in a few functions,
which is not necessary because the caller already has the session
instance.

Add the `session` as the argument of the functions so that it does not
need to call `getSession()` again.

This also fixes the issue that the session is removed in the session
manager and throws in `getSession()`, see details in the previous commit.

Tested: No logic changes, verify build is OK, and ipmi lanplus commands
       are working OK.

Signed-off-by: Lei YU <yulei.sh@bytedance.com>
Change-Id: Ibaf38ce86cad9f421a672aeb4304246aa9aa1e49
diff --git a/message_parsers.cpp b/message_parsers.cpp
index 4271c13..9c7218c 100644
--- a/message_parsers.cpp
+++ b/message_parsers.cpp
@@ -220,7 +220,8 @@
 
     if (message->isPacketAuthenticated)
     {
-        if (!(internal::verifyPacketIntegrity(inPacket, message, payloadLen)))
+        if (!(internal::verifyPacketIntegrity(inPacket, message, payloadLen,
+                                              session)))
         {
             throw std::runtime_error("Packet Integrity check failed");
         }
@@ -231,7 +232,7 @@
     {
         // Assign the decrypted payload to the IPMI Message
         message->payload =
-            internal::decryptPayload(inPacket, message, payloadLen);
+            internal::decryptPayload(inPacket, message, payloadLen, session);
     }
     else
     {
@@ -267,7 +268,7 @@
     if (outMessage->isPacketEncrypted)
     {
         header->payloadType |= PAYLOAD_ENCRYPT_MASK;
-        auto cipherPayload = internal::encryptPayload(outMessage);
+        auto cipherPayload = internal::encryptPayload(outMessage, session);
         payloadLen = cipherPayload.size();
         header->payloadLength = endian::to_ipmi<uint16_t>(cipherPayload.size());
 
@@ -289,7 +290,7 @@
     {
         header = reinterpret_cast<SessionHeader_t*>(packet.data());
         header->payloadType |= PAYLOAD_AUTH_MASK;
-        internal::addIntegrityData(packet, outMessage, payloadLen);
+        internal::addIntegrityData(packet, outMessage, payloadLen, session);
     }
 
     return packet;
@@ -316,7 +317,8 @@
 
 bool verifyPacketIntegrity(const std::vector<uint8_t>& packet,
                            const std::shared_ptr<Message> message,
-                           size_t payloadLen)
+                           size_t payloadLen,
+                           const std::shared_ptr<session::Session>& session)
 {
     /*
      * Padding bytes are added to cause the number of bytes in the data range
@@ -344,8 +346,6 @@
         return false;
     }
 
-    auto session = session::Manager::get().getSession(message->bmcSessionID);
-
     auto integrityAlgo = session->getIntegrityAlgo();
 
     // Check if Integrity data length is as expected, check integrity data
@@ -369,7 +369,8 @@
 }
 
 void addIntegrityData(std::vector<uint8_t>& packet,
-                      const std::shared_ptr<Message> message, size_t payloadLen)
+                      const std::shared_ptr<Message> message, size_t payloadLen,
+                      const std::shared_ptr<session::Session>& session)
 {
     // The following logic calculates the number of padding bytes to be added to
     // IPMI packet. If needed each integrity Pad byte is set to FFh.
@@ -384,28 +385,25 @@
     trailer->padLength = paddingLen;
     trailer->nextHeader = parser::RMCP_MESSAGE_CLASS_IPMI;
 
-    auto session = session::Manager::get().getSession(message->bmcSessionID);
-
     auto integrityData =
         session->getIntegrityAlgo()->generateIntegrityData(packet);
 
     packet.insert(packet.end(), integrityData.begin(), integrityData.end());
 }
 
-std::vector<uint8_t> decryptPayload(const std::vector<uint8_t>& packet,
-                                    const std::shared_ptr<Message> message,
-                                    size_t payloadLen)
+std::vector<uint8_t>
+    decryptPayload(const std::vector<uint8_t>& packet,
+                   const std::shared_ptr<Message> message, size_t payloadLen,
+                   const std::shared_ptr<session::Session>& session)
 {
-    auto session = session::Manager::get().getSession(message->bmcSessionID);
-
     return session->getCryptAlgo()->decryptPayload(
         packet, sizeof(SessionHeader_t), payloadLen);
 }
 
-std::vector<uint8_t> encryptPayload(std::shared_ptr<Message> message)
+std::vector<uint8_t>
+    encryptPayload(std::shared_ptr<Message> message,
+                   const std::shared_ptr<session::Session>& session)
 {
-    auto session = session::Manager::get().getSession(message->bmcSessionID);
-
     return session->getCryptAlgo()->encryptPayload(message->payload);
 }
 
diff --git a/message_parsers.hpp b/message_parsers.hpp
index b26f9a3..a992e0a 100644
--- a/message_parsers.hpp
+++ b/message_parsers.hpp
@@ -178,6 +178,7 @@
  *        session header
  *
  * @param[in] outMessage - IPMI message to be flattened
+ * @param[in] session - session handle
  *
  * @return IPMI packet on success
  */
@@ -203,11 +204,13 @@
  * @param[in] packet - Incoming IPMI packet
  * @param[in] message - IPMI Message populated from the incoming packet
  * @param[in] payloadLen - Length of the IPMI payload
+ * @param[in] session - session handle
  *
  */
 bool verifyPacketIntegrity(const std::vector<uint8_t>& packet,
                            const std::shared_ptr<Message> message,
-                           size_t payloadLen);
+                           size_t payloadLen,
+                           const std::shared_ptr<session::Session>& session);
 
 /**
  * @brief Add Integrity data to the outgoing IPMI packet
@@ -217,8 +220,8 @@
  * @param[in] payloadLen - Length of the IPMI payload
  */
 void addIntegrityData(std::vector<uint8_t>& packet,
-                      const std::shared_ptr<Message> message,
-                      size_t payloadLen);
+                      const std::shared_ptr<Message> message, size_t payloadLen,
+                      const std::shared_ptr<session::Session>& session);
 
 /**
  * @brief Decrypt the encrypted payload in the incoming IPMI packet
@@ -226,21 +229,26 @@
  * @param[in] packet - Incoming IPMI packet
  * @param[in] message - IPMI Message populated from the incoming packet
  * @param[in] payloadLen - Length of encrypted IPMI payload
+ * @param[in] session - session handle
  *
  * @return on successful completion, return the plain text payload
  */
-std::vector<uint8_t> decryptPayload(const std::vector<uint8_t>& packet,
-                                    const std::shared_ptr<Message> message,
-                                    size_t payloadLen);
+std::vector<uint8_t>
+    decryptPayload(const std::vector<uint8_t>& packet,
+                   const std::shared_ptr<Message> message, size_t payloadLen,
+                   const std::shared_ptr<session::Session>& session);
 
 /**
  * @brief Encrypt the plain text payload for the outgoing IPMI packet
  *
  * @param[in] message - IPMI Message populated for the outgoing packet
+ * @param[in] session - session handle
  *
  * @return on successful completion, return the encrypted payload
  */
-std::vector<uint8_t> encryptPayload(std::shared_ptr<Message> message);
+std::vector<uint8_t>
+    encryptPayload(std::shared_ptr<Message> message,
+                   const std::shared_ptr<session::Session>& session);
 
 } // namespace internal