F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
SdlsFileKeyManager.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title SdlsFileKeyManager.cpp
3 // \author lestarch-autobot
4 // \brief cpp file for SdlsFileKeyManager component implementation class
5 // ======================================================================
6 
8 #include <cstring>
9 #include "Fw/Types/Assert.hpp"
10 #include "Fw/Types/StringUtils.hpp"
11 #include "Os/File.hpp"
12 
13 namespace Svc {
14 
15 namespace Ccsds {
16 
17 // ----------------------------------------------------------------------
18 // Component construction, configuration, and destruction
19 // ----------------------------------------------------------------------
20 
21 SdlsFileKeyManager ::SdlsFileKeyManager(const char* const compName)
22  : SdlsFileKeyManagerComponentBase(compName), m_path(), m_keySize(0), m_configured(false) {}
23 
25 
26 void SdlsFileKeyManager ::configure(const char* path, FwSizeType keySize) {
27  FW_ASSERT(path != nullptr);
28  FW_ASSERT(keySize > 0, static_cast<FwAssertArgType>(keySize));
29  FW_ASSERT(keySize <= SdlsCfg::MAX_SDLS_KEY_SIZE, static_cast<FwAssertArgType>(keySize));
30  // Reject paths that would be silently truncated by the string assignment
31  FW_ASSERT(Fw::StringUtils::string_length(path, this->m_path.getCapacity()) < this->m_path.getCapacity());
32  this->m_path = path;
33  this->m_keySize = keySize;
34  this->m_configured = true;
35 }
36 
37 // ----------------------------------------------------------------------
38 // Handler implementations for typed input ports
39 // ----------------------------------------------------------------------
40 
41 Svc::Ccsds::SdlsStatus SdlsFileKeyManager ::keyGet_handler(FwIndexType portNum, Svc::Ccsds::SdlsKeyBuffer& key) {
42  FW_ASSERT(this->m_configured);
43  Os::File file;
44  const Os::File::Status openStatus = file.open(this->m_path.toChar(), Os::File::OPEN_READ);
45  if (openStatus != Os::File::OP_OK) {
46  this->log_WARNING_HI_KeyReadFailed(static_cast<I32>(openStatus), 0, this->m_keySize);
47  // Zeroize any stale key bytes so they cannot leak to the caller
48  (void)::memset(key.getBuffAddr(), 0, static_cast<size_t>(key.getCapacity()));
49  const Fw::SerializeStatus resetStatus = key.setBuffLen(0);
50  FW_ASSERT(resetStatus == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(resetStatus));
51  return SdlsStatus::KEY_ERROR;
52  }
53  FwSizeType readSize = this->m_keySize;
54  const Os::File::Status readStatus = file.read(key.getBuffAddr(), readSize);
55  if ((readStatus != Os::File::OP_OK) || (readSize != this->m_keySize)) {
56  this->log_WARNING_HI_KeyReadFailed(static_cast<I32>(readStatus), readSize, this->m_keySize);
57  // Zeroize any partially-read key bytes so they cannot leak to the caller
58  (void)::memset(key.getBuffAddr(), 0, static_cast<size_t>(key.getCapacity()));
59  const Fw::SerializeStatus resetStatus = key.setBuffLen(0);
60  FW_ASSERT(resetStatus == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(resetStatus));
61  return SdlsStatus::KEY_ERROR;
62  }
63  const Fw::SerializeStatus setStatus = key.setBuffLen(this->m_keySize);
64  FW_ASSERT(setStatus == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(setStatus));
65  return SdlsStatus::SUCCESS;
66 }
67 
68 } // namespace Ccsds
69 
70 } // namespace Svc
Serialization/Deserialization operation was successful.
PlatformSizeType FwSizeType
~SdlsFileKeyManager()
Destroy SdlsFileKeyManager object.
Serializable::SizeType getCapacity() const override
Get buffer capacity.
Status of an SDLS (Space Data Link Security) encryption/decryption request.
SerializeStatus
forward declaration for string
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
Definition: File.cpp:50
U8 * getBuffAddr()
Get buffer address for data filling (non-const version)
const char * toChar() const
Convert to a C-style char*.
SerializeStatus setBuffLen(Serializable::SizeType length) override
Set buffer length manually.
SdlsFileKeyManager(const char *const compName)
Construct SdlsFileKeyManager object.
Status read(U8 *buffer, FwSizeType &size)
read data from this file into supplied buffer bounded by size
Definition: File.cpp:194
void configure(const char *path, FwSizeType keySize)
Configure the key file path and the key length to read from the file.
StringBase::SizeType getCapacity() const
Return the size of the buffer.
Operation was successful.
Definition: File.hpp:42
PlatformIndexType FwIndexType
Open file for reading.
Definition: File.hpp:33
RateGroupDivider component implementation.
Request completed successfully.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
FwSizeType string_length(const CHAR *source, FwSizeType buffer_size)
get the length of the source string
Definition: StringUtils.cpp:32