Sampa Misra | d823cc0 | 2020-03-24 04:53:20 -0500 | [diff] [blame] | 1 | #include "file_io_type_cert.hpp" |
| 2 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 3 | #include "libpldm/base.h" |
| 4 | #include "oem/ibm/libpldm/file_io.h" |
| 5 | |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 6 | #include "common/utils.hpp" |
Sampa Misra | d823cc0 | 2020-03-24 04:53:20 -0500 | [diff] [blame] | 7 | |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <iostream> |
| 11 | |
Sampa Misra | d823cc0 | 2020-03-24 04:53:20 -0500 | [diff] [blame] | 12 | namespace pldm |
| 13 | { |
| 14 | namespace responder |
| 15 | { |
| 16 | |
| 17 | static constexpr auto csrFilePath = "/var/lib/bmcweb/CSR"; |
| 18 | static constexpr auto rootCertPath = "/var/lib/bmcweb/RootCert"; |
| 19 | static constexpr auto clientCertPath = "/var/lib/bmcweb/ClientCert"; |
| 20 | |
| 21 | CertMap CertHandler::certMap; |
| 22 | |
| 23 | int CertHandler::writeFromMemory(uint32_t offset, uint32_t length, |
Sampa Misra | 6950850 | 2020-09-08 00:08:21 -0500 | [diff] [blame] | 24 | uint64_t address, |
| 25 | oem_platform::Handler* /*oemPlatformHandler*/) |
Sampa Misra | d823cc0 | 2020-03-24 04:53:20 -0500 | [diff] [blame] | 26 | { |
| 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 | |
| 49 | int CertHandler::readIntoMemory(uint32_t offset, uint32_t& length, |
Sampa Misra | 6950850 | 2020-09-08 00:08:21 -0500 | [diff] [blame] | 50 | uint64_t address, |
| 51 | oem_platform::Handler* /*oemPlatformHandler*/) |
Sampa Misra | d823cc0 | 2020-03-24 04:53:20 -0500 | [diff] [blame] | 52 | { |
| 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 Misra | 6950850 | 2020-09-08 00:08:21 -0500 | [diff] [blame] | 60 | int CertHandler::read(uint32_t offset, uint32_t& length, Response& response, |
| 61 | oem_platform::Handler* /*oemPlatformHandler*/) |
Sampa Misra | d823cc0 | 2020-03-24 04:53:20 -0500 | [diff] [blame] | 62 | { |
| 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 Misra | 6950850 | 2020-09-08 00:08:21 -0500 | [diff] [blame] | 70 | int CertHandler::write(const char* buffer, uint32_t offset, uint32_t& length, |
| 71 | oem_platform::Handler* /*oemPlatformHandler*/) |
Sampa Misra | d823cc0 | 2020-03-24 04:53:20 -0500 | [diff] [blame] | 72 | { |
| 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 | |
| 106 | int 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 | { |
Ed Tanous | 788d96e | 2021-01-25 13:12:07 -0800 | [diff] [blame] | 119 | fileFd = open(clientCertPath, flags, S_IRUSR | S_IWUSR); |
Sampa Misra | d823cc0 | 2020-03-24 04:53:20 -0500 | [diff] [blame] | 120 | } |
| 121 | else if (certType == PLDM_FILE_TYPE_ROOT_CERT) |
| 122 | { |
Ed Tanous | 788d96e | 2021-01-25 13:12:07 -0800 | [diff] [blame] | 123 | fileFd = open(rootCertPath, flags, S_IRUSR | S_IWUSR); |
Sampa Misra | d823cc0 | 2020-03-24 04:53:20 -0500 | [diff] [blame] | 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 |