blob: b49903c358b7c4469506366e5315b20120724077 [file] [log] [blame]
Sampa Misrad823cc02020-03-24 04:53:20 -05001#include "file_io_type_cert.hpp"
2
George Liu6492f522020-06-16 10:34:05 +08003#include "libpldm/base.h"
4#include "oem/ibm/libpldm/file_io.h"
5
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05006#include "common/utils.hpp"
Sampa Misrad823cc02020-03-24 04:53:20 -05007
8#include <stdint.h>
9
10#include <iostream>
11
Sampa Misrad823cc02020-03-24 04:53:20 -050012namespace pldm
13{
14namespace responder
15{
16
17static constexpr auto csrFilePath = "/var/lib/bmcweb/CSR";
18static constexpr auto rootCertPath = "/var/lib/bmcweb/RootCert";
19static constexpr auto clientCertPath = "/var/lib/bmcweb/ClientCert";
20
21CertMap CertHandler::certMap;
22
23int CertHandler::writeFromMemory(uint32_t offset, uint32_t length,
Sampa Misra69508502020-09-08 00:08:21 -050024 uint64_t address,
25 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misrad823cc02020-03-24 04:53:20 -050026{
27 auto it = certMap.find(certType);
28 if (it == certMap.end())
29 {
30 std::cerr << "file for type " << certType << " doesn't exist\n";
31 return PLDM_ERROR;
32 }
33
34 auto fd = std::get<0>(it->second);
35 auto& remSize = std::get<1>(it->second);
36 auto rc = transferFileData(fd, false, offset, length, address);
37 if (rc == PLDM_SUCCESS)
38 {
39 remSize -= length;
40 if (!remSize)
41 {
42 close(fd);
43 certMap.erase(it);
44 }
45 }
46 return rc;
47}
48
49int CertHandler::readIntoMemory(uint32_t offset, uint32_t& length,
Sampa Misra69508502020-09-08 00:08:21 -050050 uint64_t address,
51 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misrad823cc02020-03-24 04:53:20 -050052{
53 if (certType != PLDM_FILE_TYPE_CERT_SIGNING_REQUEST)
54 {
55 return PLDM_ERROR_INVALID_DATA;
56 }
57 return transferFileData(csrFilePath, true, offset, length, address);
58}
59
Sampa Misra69508502020-09-08 00:08:21 -050060int CertHandler::read(uint32_t offset, uint32_t& length, Response& response,
61 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misrad823cc02020-03-24 04:53:20 -050062{
63 if (certType != PLDM_FILE_TYPE_CERT_SIGNING_REQUEST)
64 {
65 return PLDM_ERROR_INVALID_DATA;
66 }
67 return readFile(csrFilePath, offset, length, response);
68}
69
Sampa Misra69508502020-09-08 00:08:21 -050070int CertHandler::write(const char* buffer, uint32_t offset, uint32_t& length,
71 oem_platform::Handler* /*oemPlatformHandler*/)
Sampa Misrad823cc02020-03-24 04:53:20 -050072{
73 auto it = certMap.find(certType);
74 if (it == certMap.end())
75 {
76 std::cerr << "file for type " << certType << " doesn't exist\n";
77 return PLDM_ERROR;
78 }
79
80 auto fd = std::get<0>(it->second);
81 int rc = lseek(fd, offset, SEEK_SET);
82 if (rc == -1)
83 {
84 std::cerr << "lseek failed, ERROR=" << errno << ", OFFSET=" << offset
85 << "\n";
86 return PLDM_ERROR;
87 }
88 rc = ::write(fd, buffer, length);
89 if (rc == -1)
90 {
91 std::cerr << "file write failed, ERROR=" << errno
92 << ", LENGTH=" << length << ", OFFSET=" << offset << "\n";
93 return PLDM_ERROR;
94 }
95 length = rc;
96 auto& remSize = std::get<1>(it->second);
97 remSize -= length;
98 if (!remSize)
99 {
100 close(fd);
101 certMap.erase(it);
102 }
103 return PLDM_SUCCESS;
104}
105
106int CertHandler::newFileAvailable(uint64_t length)
107{
108 static constexpr auto vmiCertPath = "/var/lib/bmcweb";
109 fs::create_directories(vmiCertPath);
110 int fileFd = -1;
111 int flags = O_WRONLY | O_CREAT | O_TRUNC;
112
113 if (certType == PLDM_FILE_TYPE_CERT_SIGNING_REQUEST)
114 {
115 return PLDM_ERROR_INVALID_DATA;
116 }
117 if (certType == PLDM_FILE_TYPE_SIGNED_CERT)
118 {
119 fileFd = open(clientCertPath, flags);
120 }
121 else if (certType == PLDM_FILE_TYPE_ROOT_CERT)
122 {
123 fileFd = open(rootCertPath, flags);
124 }
125 if (fileFd == -1)
126 {
127 std::cerr << "failed to open file for type " << certType
128 << " ERROR=" << errno << "\n";
129 return PLDM_ERROR;
130 }
131 certMap.emplace(certType, std::tuple(fileFd, length));
132 return PLDM_SUCCESS;
133}
134
135} // namespace responder
136} // namespace pldm