Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 3 | #include "certificate.hpp" |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 4 | #include "certs_manager.hpp" |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 5 | #include "csr.hpp" |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 6 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 7 | #include <openssl/bio.h> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 8 | #include <openssl/ossl_typ.h> |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 9 | #include <openssl/pem.h> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 10 | #include <openssl/x509.h> |
| 11 | #include <systemd/sd-event.h> |
| 12 | #include <unistd.h> |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 13 | |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 14 | #include <cstdint> |
| 15 | #include <cstdio> |
| 16 | #include <cstdlib> |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 17 | #include <filesystem> |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 18 | #include <fstream> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 19 | #include <iostream> |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 20 | #include <iterator> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <new> |
| 23 | #include <sdbusplus/bus.hpp> |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 24 | #include <sdeventplus/event.hpp> |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 25 | #include <string> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 26 | #include <utility> |
| 27 | #include <vector> |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame] | 28 | #include <xyz/openbmc_project/Certs/error.hpp> |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 29 | #include <xyz/openbmc_project/Common/error.hpp> |
| 30 | |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 31 | #include <gtest/gtest.h> |
Nan Zhou | e1289ad | 2021-12-28 11:02:56 -0800 | [diff] [blame] | 32 | |
| 33 | namespace phosphor::certs |
| 34 | { |
| 35 | namespace |
| 36 | { |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 37 | namespace fs = std::filesystem; |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 38 | using ::sdbusplus::xyz::openbmc_project::Certs::Error::InvalidCertificate; |
| 39 | using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 40 | |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 41 | /** |
| 42 | * Class to generate certificate file and test verification of certificate file |
| 43 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 44 | class TestCertificates : public ::testing::Test |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 45 | { |
| 46 | public: |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 47 | TestCertificates() : bus(sdbusplus::bus::new_default()) |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | void SetUp() override |
| 51 | { |
| 52 | char dirTemplate[] = "/tmp/FakeCerts.XXXXXX"; |
| 53 | auto dirPtr = mkdtemp(dirTemplate); |
Nan Zhou | cfb5802 | 2021-12-28 11:02:26 -0800 | [diff] [blame] | 54 | if (dirPtr == nullptr) |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 55 | { |
| 56 | throw std::bad_alloc(); |
| 57 | } |
Zbigniew Lukwinski | fe590c4 | 2019-12-10 12:33:50 +0100 | [diff] [blame] | 58 | certDir = std::string(dirPtr) + "/certs"; |
| 59 | fs::create_directories(certDir); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 60 | |
| 61 | createNewCertificate(); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 62 | } |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 63 | |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 64 | void TearDown() override |
| 65 | { |
| 66 | fs::remove_all(certDir); |
| 67 | fs::remove(certificateFile); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 68 | fs::remove(CSRFile); |
| 69 | fs::remove(privateKeyFile); |
Nan Zhou | cf811c4 | 2021-12-02 14:56:17 -0800 | [diff] [blame] | 70 | fs::remove_all("demoCA"); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 71 | } |
| 72 | |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 73 | void createNewCertificate(bool setNewCertId = false) |
| 74 | { |
| 75 | certificateFile = "cert.pem"; |
| 76 | CSRFile = "domain.csr"; |
| 77 | privateKeyFile = "privkey.pem"; |
| 78 | rsaPrivateKeyFilePath = certDir + "/.rsaprivkey.pem"; |
| 79 | std::string cmd = "openssl req -x509 -sha256 -newkey rsa:2048 "; |
Nan Zhou | cf811c4 | 2021-12-02 14:56:17 -0800 | [diff] [blame] | 80 | cmd += "-keyout cert.pem -out cert.pem -days 365000 -nodes"; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 81 | cmd += " -subj /O=openbmc-project.xyz/CN=localhost"; |
| 82 | |
| 83 | if (setNewCertId) |
| 84 | { |
| 85 | cmd += std::to_string(certId++); |
| 86 | } |
| 87 | |
| 88 | auto val = std::system(cmd.c_str()); |
| 89 | if (val) |
| 90 | { |
| 91 | std::cout << "COMMAND Error: " << val << std::endl; |
| 92 | } |
| 93 | } |
| 94 | |
Nan Zhou | cf811c4 | 2021-12-02 14:56:17 -0800 | [diff] [blame] | 95 | void createNeverExpiredRootCertificate() |
| 96 | { |
| 97 | // remove the old cert |
| 98 | fs::remove(certificateFile); |
| 99 | |
| 100 | // The following routines create a cert that has NotBefore |
| 101 | // set to 1970/01/01 and NotAfter set to 9999/12/31 via the |
| 102 | // OpenSSL CA application. |
| 103 | certificateFile = "cert.pem"; |
| 104 | ASSERT_EQ(std::system("mkdir -p demoCA"), 0); |
| 105 | ASSERT_EQ(std::system("mkdir -p demoCA/private/"), 0); |
| 106 | ASSERT_EQ(std::system("mkdir -p demoCA/newcerts/"), 0); |
| 107 | ASSERT_EQ(std::system("touch demoCA/index.txt"), 0); |
| 108 | ASSERT_EQ(std::system("echo 1000 > demoCA/serial"), 0); |
| 109 | ASSERT_EQ( |
| 110 | std::system( |
| 111 | "openssl req -x509 -sha256 -newkey rsa:2048 -keyout " |
| 112 | "demoCA/private/cakey.pem -out demoCA/cacert.pem -nodes " |
| 113 | "-subj /O=openbmc-project.xyz/C=US/ST=CA/CN=localhost-ca"), |
| 114 | 0); |
| 115 | ASSERT_EQ(std::system( |
| 116 | "openssl req -new -newkey rsa:2048 -nodes -keyout " |
| 117 | "demoCA/server.key -out demoCA/server.csr -subj " |
| 118 | "/O=openbmc-project.xyz/C=US/ST=CA/CN=localhost-server"), |
| 119 | 0); |
| 120 | ASSERT_EQ( |
| 121 | std::system( |
| 122 | "openssl ca -batch -startdate 19700101000000Z -enddate " |
| 123 | "99991231235959Z -out cert.pem -infiles demoCA/server.csr"), |
| 124 | 0); |
| 125 | } |
| 126 | |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 127 | bool compareFiles(const std::string& file1, const std::string& file2) |
| 128 | { |
| 129 | std::ifstream f1(file1, std::ifstream::binary | std::ifstream::ate); |
| 130 | std::ifstream f2(file2, std::ifstream::binary | std::ifstream::ate); |
| 131 | |
| 132 | if (f1.fail() || f2.fail()) |
| 133 | { |
| 134 | return false; // file problem |
| 135 | } |
| 136 | |
| 137 | if (f1.tellg() != f2.tellg()) |
| 138 | { |
| 139 | return false; // size mismatch |
| 140 | } |
| 141 | |
| 142 | // seek back to beginning and use std::equal to compare contents |
| 143 | f1.seekg(0, std::ifstream::beg); |
| 144 | f2.seekg(0, std::ifstream::beg); |
| 145 | return std::equal(std::istreambuf_iterator<char>(f1.rdbuf()), |
| 146 | std::istreambuf_iterator<char>(), |
| 147 | std::istreambuf_iterator<char>(f2.rdbuf())); |
| 148 | } |
| 149 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 150 | std::string getCertSubjectNameHash(const std::string& certFilePath) |
| 151 | { |
| 152 | std::unique_ptr<X509, decltype(&::X509_free)> cert(X509_new(), |
| 153 | ::X509_free); |
| 154 | if (!cert) |
| 155 | { |
| 156 | std::string(); |
| 157 | } |
| 158 | |
| 159 | std::unique_ptr<BIO, decltype(&::BIO_free)> bioCert( |
| 160 | BIO_new_file(certFilePath.c_str(), "rb"), ::BIO_free); |
| 161 | if (!bioCert) |
| 162 | { |
| 163 | std::string(); |
| 164 | } |
| 165 | |
| 166 | X509* x509 = cert.get(); |
| 167 | if (!PEM_read_bio_X509(bioCert.get(), &x509, nullptr, nullptr)) |
| 168 | { |
| 169 | std::string(); |
| 170 | } |
| 171 | |
| 172 | unsigned long hash = X509_subject_name_hash(cert.get()); |
| 173 | static constexpr auto AUTH_CERT_HASH_LENGTH = 9; |
| 174 | char hashBuf[AUTH_CERT_HASH_LENGTH]; |
| 175 | sprintf(hashBuf, "%08lx", hash); |
| 176 | return std::string(hashBuf); |
| 177 | } |
| 178 | |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 179 | protected: |
| 180 | sdbusplus::bus::bus bus; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 181 | std::string certificateFile, CSRFile, privateKeyFile, rsaPrivateKeyFilePath; |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 182 | |
| 183 | std::string certDir; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 184 | uint64_t certId; |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | class MainApp |
| 188 | { |
| 189 | public: |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 190 | MainApp(phosphor::certs::Manager* manager, |
| 191 | phosphor::certs::CSR* csr = nullptr) : |
| 192 | manager(manager), |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 193 | csr_(csr) |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 194 | { |
| 195 | } |
| 196 | void install(std::string& path) |
| 197 | { |
| 198 | manager->install(path); |
| 199 | } |
Marri Devender Rao | 9abfae8 | 2018-10-03 08:10:35 -0500 | [diff] [blame] | 200 | void delete_() |
| 201 | { |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 202 | manager->deleteAll(); |
Marri Devender Rao | 9abfae8 | 2018-10-03 08:10:35 -0500 | [diff] [blame] | 203 | } |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 204 | |
| 205 | std::string generateCSR(std::vector<std::string> alternativeNames, |
| 206 | std::string challengePassword, std::string city, |
| 207 | std::string commonName, std::string contactPerson, |
| 208 | std::string country, std::string email, |
| 209 | std::string givenName, std::string initials, |
| 210 | int64_t keyBitLength, std::string keyCurveId, |
| 211 | std::string keyPairAlgorithm, |
| 212 | std::vector<std::string> keyUsage, |
| 213 | std::string organization, |
| 214 | std::string organizationalUnit, std::string state, |
| 215 | std::string surname, std::string unstructuredName) |
| 216 | { |
| 217 | return (manager->generateCSR( |
| 218 | alternativeNames, challengePassword, city, commonName, |
| 219 | contactPerson, country, email, givenName, initials, keyBitLength, |
| 220 | keyCurveId, keyPairAlgorithm, keyUsage, organization, |
| 221 | organizationalUnit, state, surname, unstructuredName)); |
| 222 | } |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 223 | std::string csr() |
| 224 | { |
| 225 | return (csr_->csr()); |
| 226 | } |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 227 | phosphor::certs::Manager* manager; |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 228 | phosphor::certs::CSR* csr_; |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 229 | }; |
| 230 | |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 231 | /** @brief Check if server install routine is invoked for server setup |
| 232 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 233 | TEST_F(TestCertificates, InvokeServerInstall) |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 234 | { |
| 235 | std::string endpoint("https"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 236 | std::string unit; |
| 237 | CertificateType type = CertificateType::Server; |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 238 | std::string installPath(certDir + "/" + certificateFile); |
| 239 | std::string verifyPath(installPath); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 240 | std::string verifyUnit(unit); |
| 241 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 242 | certificateTypeToString(type) + '/' + endpoint; |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 243 | auto event = sdeventplus::Event::get_default(); |
| 244 | // Attach the bus to sd_event to service user requests |
| 245 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 246 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 247 | std::move(installPath)); |
| 248 | MainApp mainApp(&manager); |
| 249 | mainApp.install(certificateFile); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 250 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 251 | } |
| 252 | |
| 253 | /** @brief Check if client install routine is invoked for client setup |
| 254 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 255 | TEST_F(TestCertificates, InvokeClientInstall) |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 256 | { |
| 257 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 258 | std::string unit; |
| 259 | CertificateType type = CertificateType::Server; |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 260 | std::string installPath(certDir + "/" + certificateFile); |
| 261 | std::string verifyPath(installPath); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 262 | std::string verifyUnit(unit); |
| 263 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 264 | certificateTypeToString(type) + '/' + endpoint; |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 265 | auto event = sdeventplus::Event::get_default(); |
| 266 | // Attach the bus to sd_event to service user requests |
| 267 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 268 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 269 | std::move(installPath)); |
| 270 | MainApp mainApp(&manager); |
| 271 | mainApp.install(certificateFile); |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 272 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 273 | } |
| 274 | |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 275 | /** @brief Check if storage install routine is invoked for storage setup |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 276 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 277 | TEST_F(TestCertificates, InvokeAuthorityInstall) |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 278 | { |
| 279 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 280 | std::string unit; |
| 281 | CertificateType type = CertificateType::Authority; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 282 | std::string verifyDir(certDir); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 283 | std::string verifyUnit(unit); |
| 284 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 285 | certificateTypeToString(type) + '/' + endpoint; |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 286 | auto event = sdeventplus::Event::get_default(); |
| 287 | // Attach the bus to sd_event to service user requests |
| 288 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 289 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 290 | std::move(certDir)); |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 291 | MainApp mainApp(&manager); |
Nan Zhou | cf811c4 | 2021-12-02 14:56:17 -0800 | [diff] [blame] | 292 | // install the default certificate that's valid from today to 100 years |
| 293 | // later |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 294 | mainApp.install(certificateFile); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 295 | |
| 296 | std::vector<std::unique_ptr<Certificate>>& certs = |
| 297 | manager.getCertificates(); |
| 298 | |
Nan Zhou | cf811c4 | 2021-12-02 14:56:17 -0800 | [diff] [blame] | 299 | ASSERT_EQ(certs.size(), 1); |
| 300 | // check some attributes as well |
| 301 | EXPECT_EQ(certs.front()->validNotAfter() - certs.front()->validNotBefore(), |
| 302 | 365000ULL * 24 * 3600); |
| 303 | EXPECT_EQ(certs.front()->subject(), "O=openbmc-project.xyz,CN=localhost"); |
| 304 | EXPECT_EQ(certs.front()->issuer(), "O=openbmc-project.xyz,CN=localhost"); |
| 305 | |
| 306 | std::string verifyPath = |
| 307 | verifyDir + "/" + getCertSubjectNameHash(certificateFile) + ".0"; |
| 308 | |
| 309 | // Check that certificate has been created at installation directory |
| 310 | EXPECT_FALSE(fs::is_empty(verifyDir)); |
| 311 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 312 | |
| 313 | // Check that installed cert is identical to input one |
| 314 | EXPECT_TRUE(compareFiles(certificateFile, verifyPath)); |
| 315 | } |
| 316 | |
| 317 | /** @brief Check if storage install routine is invoked for storage setup |
| 318 | */ |
| 319 | TEST_F(TestCertificates, InvokeAuthorityInstallNeverExpiredRootCert) |
| 320 | { |
| 321 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 322 | std::string unit; |
| 323 | CertificateType type = CertificateType::Authority; |
Nan Zhou | cf811c4 | 2021-12-02 14:56:17 -0800 | [diff] [blame] | 324 | std::string verifyDir(certDir); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 325 | std::string verifyUnit(unit); |
| 326 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 327 | certificateTypeToString(type) + '/' + endpoint; |
Nan Zhou | cf811c4 | 2021-12-02 14:56:17 -0800 | [diff] [blame] | 328 | auto event = sdeventplus::Event::get_default(); |
| 329 | // Attach the bus to sd_event to service user requests |
| 330 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 331 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 332 | std::move(certDir)); |
| 333 | MainApp mainApp(&manager); |
| 334 | |
| 335 | // install the certificate that's valid from the Unix Epoch to Dec 31, 9999 |
| 336 | createNeverExpiredRootCertificate(); |
| 337 | mainApp.install(certificateFile); |
| 338 | |
| 339 | std::vector<std::unique_ptr<Certificate>>& certs = |
| 340 | manager.getCertificates(); |
| 341 | |
| 342 | EXPECT_EQ(certs.front()->validNotBefore(), 0); |
| 343 | EXPECT_EQ(certs.front()->validNotAfter(), 253402300799ULL); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 344 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 345 | std::string verifyPath = |
| 346 | verifyDir + "/" + getCertSubjectNameHash(certificateFile) + ".0"; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 347 | |
| 348 | // Check that certificate has been created at installation directory |
| 349 | EXPECT_FALSE(fs::is_empty(verifyDir)); |
| 350 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 351 | |
| 352 | // Check that installed cert is identical to input one |
| 353 | EXPECT_TRUE(compareFiles(certificateFile, verifyPath)); |
| 354 | } |
| 355 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 356 | /** @brief Check if in authority mode user can't install the same |
| 357 | * certificate twice. |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 358 | */ |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 359 | TEST_F(TestCertificates, InvokeInstallSameCertTwice) |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 360 | { |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 361 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 362 | std::string unit; |
| 363 | CertificateType type = CertificateType::Authority; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 364 | std::string verifyDir(certDir); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 365 | std::string verifyUnit(unit); |
| 366 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 367 | certificateTypeToString(type) + '/' + endpoint; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 368 | auto event = sdeventplus::Event::get_default(); |
| 369 | // Attach the bus to sd_event to service user requests |
| 370 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 371 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 372 | std::move(certDir)); |
| 373 | MainApp mainApp(&manager); |
| 374 | mainApp.install(certificateFile); |
| 375 | |
| 376 | std::vector<std::unique_ptr<Certificate>>& certs = |
| 377 | manager.getCertificates(); |
| 378 | |
| 379 | EXPECT_FALSE(certs.empty()); |
| 380 | |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 381 | // Check that certificate has been created at installation directory |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 382 | std::string verifyPath = |
| 383 | verifyDir + "/" + getCertSubjectNameHash(certificateFile) + ".0"; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 384 | EXPECT_FALSE(fs::is_empty(verifyDir)); |
| 385 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 386 | |
| 387 | // Check that installed cert is identical to input one |
| 388 | EXPECT_TRUE(compareFiles(certificateFile, verifyPath)); |
| 389 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 390 | using NotAllowed = |
| 391 | sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 392 | EXPECT_THROW( |
| 393 | { |
| 394 | try |
| 395 | { |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 396 | // Try to install the same certificate second time |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 397 | mainApp.install(certificateFile); |
| 398 | } |
| 399 | catch (const NotAllowed& e) |
| 400 | { |
| 401 | throw; |
| 402 | } |
| 403 | }, |
| 404 | NotAllowed); |
| 405 | |
| 406 | // Check that the original certificate has been not removed |
| 407 | EXPECT_FALSE(fs::is_empty(verifyDir)); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 408 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 409 | } |
| 410 | |
Zbigniew Lukwinski | 73d1fbf | 2020-01-15 15:31:12 +0100 | [diff] [blame] | 411 | /** @brief Check if in authority mode user can install a certificate with |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 412 | * certain subject hash twice. |
| 413 | */ |
| 414 | TEST_F(TestCertificates, InvokeInstallSameSubjectTwice) |
| 415 | { |
| 416 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 417 | std::string unit; |
| 418 | CertificateType type = CertificateType::Authority; |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 419 | std::string verifyDir(certDir); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 420 | std::string verifyUnit(unit); |
| 421 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 422 | certificateTypeToString(type) + '/' + endpoint; |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 423 | auto event = sdeventplus::Event::get_default(); |
| 424 | // Attach the bus to sd_event to service user requests |
| 425 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 426 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 427 | std::move(certDir)); |
| 428 | MainApp mainApp(&manager); |
| 429 | mainApp.install(certificateFile); |
| 430 | |
| 431 | std::vector<std::unique_ptr<Certificate>>& certs = |
| 432 | manager.getCertificates(); |
| 433 | |
| 434 | EXPECT_FALSE(certs.empty()); |
| 435 | |
| 436 | // Check that certificate has been created at installation directory |
| 437 | std::string verifyPath0 = |
| 438 | verifyDir + "/" + getCertSubjectNameHash(certificateFile) + ".0"; |
| 439 | EXPECT_FALSE(fs::is_empty(verifyDir)); |
| 440 | EXPECT_TRUE(fs::exists(verifyPath0)); |
| 441 | |
| 442 | // Check that installed cert is identical to input one |
| 443 | EXPECT_TRUE(compareFiles(certificateFile, verifyPath0)); |
| 444 | |
| 445 | // Prepare second certificate with the same subject |
| 446 | createNewCertificate(); |
| 447 | |
Zbigniew Lukwinski | 73d1fbf | 2020-01-15 15:31:12 +0100 | [diff] [blame] | 448 | // Install second certificate |
| 449 | mainApp.install(certificateFile); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 450 | |
| 451 | // Expect there are exactly two certificates in the collection |
Zbigniew Lukwinski | 73d1fbf | 2020-01-15 15:31:12 +0100 | [diff] [blame] | 452 | EXPECT_EQ(certs.size(), 2); |
| 453 | |
| 454 | // Check that certificate has been created at installation directory |
| 455 | std::string verifyPath1 = |
| 456 | verifyDir + "/" + getCertSubjectNameHash(certificateFile) + ".1"; |
| 457 | EXPECT_TRUE(fs::exists(verifyPath1)); |
| 458 | |
| 459 | // Check that installed cert is identical to input one |
| 460 | EXPECT_TRUE(compareFiles(certificateFile, verifyPath1)); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 461 | |
| 462 | // Check that the original/first certificate has been not removed |
| 463 | EXPECT_FALSE(fs::is_empty(verifyDir)); |
| 464 | EXPECT_TRUE(fs::exists(verifyPath0)); |
| 465 | } |
| 466 | |
| 467 | /** @brief Check if in authority mode user can't install more than |
Nan Zhou | 718eef3 | 2021-12-28 11:03:30 -0800 | [diff] [blame] | 468 | * maxNumAuthorityCertificates certificates. |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 469 | */ |
| 470 | TEST_F(TestCertificates, InvokeInstallAuthCertLimit) |
| 471 | { |
| 472 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 473 | std::string unit; |
| 474 | CertificateType type = CertificateType::Authority; |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 475 | std::string verifyDir(certDir); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 476 | std::string verifyUnit(unit); |
| 477 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 478 | certificateTypeToString(type) + '/' + endpoint; |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 479 | auto event = sdeventplus::Event::get_default(); |
| 480 | // Attach the bus to sd_event to service user requests |
| 481 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 482 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 483 | std::move(certDir)); |
| 484 | MainApp mainApp(&manager); |
| 485 | |
| 486 | std::vector<std::unique_ptr<Certificate>>& certs = |
| 487 | manager.getCertificates(); |
| 488 | |
| 489 | std::vector<std::string> verifyPaths; |
| 490 | |
| 491 | // Prepare maximum number of ceritificates |
Nan Zhou | 718eef3 | 2021-12-28 11:03:30 -0800 | [diff] [blame] | 492 | for (std::size_t i = 0; i < maxNumAuthorityCertificates; ++i) |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 493 | { |
| 494 | // Prepare new certificatate |
| 495 | createNewCertificate(true); |
| 496 | |
| 497 | // Install ceritificate |
| 498 | mainApp.install(certificateFile); |
| 499 | |
| 500 | // Check number of certificates in the collection |
| 501 | EXPECT_EQ(certs.size(), i + 1); |
| 502 | |
| 503 | // Check that certificate has been created at installation directory |
| 504 | std::string verifyPath = |
| 505 | verifyDir + "/" + getCertSubjectNameHash(certificateFile) + ".0"; |
| 506 | EXPECT_FALSE(fs::is_empty(verifyDir)); |
| 507 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 508 | |
| 509 | // Check that installed cert is identical to input one |
| 510 | EXPECT_TRUE(compareFiles(certificateFile, verifyPath)); |
| 511 | |
| 512 | // Save current certificate file for later check |
| 513 | verifyPaths.push_back(verifyPath); |
| 514 | } |
| 515 | |
| 516 | // Prepare new certificatate |
| 517 | createNewCertificate(true); |
| 518 | |
| 519 | using NotAllowed = |
| 520 | sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
| 521 | EXPECT_THROW( |
| 522 | { |
| 523 | try |
| 524 | { |
| 525 | // Try to install one more certificate |
| 526 | mainApp.install(certificateFile); |
| 527 | } |
| 528 | catch (const NotAllowed& e) |
| 529 | { |
| 530 | throw; |
| 531 | } |
| 532 | }, |
| 533 | NotAllowed); |
| 534 | |
| 535 | // Check that the original certificate has been not removed |
| 536 | EXPECT_FALSE(fs::is_empty(verifyDir)); |
Nan Zhou | 718eef3 | 2021-12-28 11:03:30 -0800 | [diff] [blame] | 537 | for (size_t i = 0; i < maxNumAuthorityCertificates; ++i) |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 538 | { |
| 539 | EXPECT_TRUE(fs::exists(verifyPaths[i])); |
| 540 | } |
| 541 | } |
| 542 | |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 543 | /** @brief Compare the installed certificate with the copied certificate |
| 544 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 545 | TEST_F(TestCertificates, CompareInstalledCertificate) |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 546 | { |
| 547 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 548 | std::string unit; |
| 549 | CertificateType type = CertificateType::Client; |
| 550 | ; |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 551 | std::string installPath(certDir + "/" + certificateFile); |
| 552 | std::string verifyPath(installPath); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 553 | std::string verifyUnit(unit); |
| 554 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 555 | certificateTypeToString(type) + '/' + endpoint; |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 556 | auto event = sdeventplus::Event::get_default(); |
| 557 | // Attach the bus to sd_event to service user requests |
| 558 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 559 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 560 | std::move(installPath)); |
| 561 | MainApp mainApp(&manager); |
| 562 | mainApp.install(certificateFile); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 563 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 564 | EXPECT_TRUE(compareFiles(verifyPath, certificateFile)); |
| 565 | } |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 566 | |
| 567 | /** @brief Check if install fails if certificate file is not found |
| 568 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 569 | TEST_F(TestCertificates, TestNoCertificateFile) |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 570 | { |
| 571 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 572 | std::string unit; |
| 573 | CertificateType type = CertificateType::Client; |
| 574 | ; |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 575 | std::string installPath(certDir + "/" + certificateFile); |
| 576 | std::string verifyPath(installPath); |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 577 | std::string verifyUnit(unit); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 578 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 579 | certificateTypeToString(type) + '/' + endpoint; |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 580 | std::string uploadFile = "nofile.pem"; |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 581 | EXPECT_THROW( |
| 582 | { |
| 583 | try |
| 584 | { |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 585 | auto event = sdeventplus::Event::get_default(); |
| 586 | // Attach the bus to sd_event to service user requests |
| 587 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 588 | Manager manager(bus, event, objPath.c_str(), type, |
| 589 | std::move(unit), std::move(installPath)); |
| 590 | MainApp mainApp(&manager); |
| 591 | mainApp.install(uploadFile); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 592 | } |
| 593 | catch (const InternalFailure& e) |
| 594 | { |
| 595 | throw; |
| 596 | } |
| 597 | }, |
| 598 | InternalFailure); |
| 599 | EXPECT_FALSE(fs::exists(verifyPath)); |
| 600 | } |
| 601 | |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 602 | /** @brief Test replacing existing certificate |
| 603 | */ |
| 604 | TEST_F(TestCertificates, TestReplaceCertificate) |
| 605 | { |
| 606 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 607 | std::string unit; |
| 608 | CertificateType type = CertificateType::Server; |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 609 | std::string installPath(certDir + "/" + certificateFile); |
| 610 | std::string verifyPath(installPath); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 611 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 612 | certificateTypeToString(type) + '/' + endpoint; |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 613 | auto event = sdeventplus::Event::get_default(); |
| 614 | // Attach the bus to sd_event to service user requests |
| 615 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 616 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 617 | std::move(installPath)); |
| 618 | MainApp mainApp(&manager); |
| 619 | mainApp.install(certificateFile); |
| 620 | EXPECT_TRUE(fs::exists(verifyPath)); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 621 | std::vector<std::unique_ptr<Certificate>>& certs = |
| 622 | manager.getCertificates(); |
| 623 | EXPECT_FALSE(certs.empty()); |
| 624 | EXPECT_NE(certs[0], nullptr); |
| 625 | certs[0]->replace(certificateFile); |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 626 | EXPECT_TRUE(fs::exists(verifyPath)); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /** @brief Test replacing existing certificate |
| 630 | */ |
| 631 | TEST_F(TestCertificates, TestAuthorityReplaceCertificate) |
| 632 | { |
| 633 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 634 | std::string unit; |
| 635 | CertificateType type = CertificateType::Authority; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 636 | std::string verifyDir(certDir); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 637 | std::string verifyUnit(unit); |
| 638 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 639 | certificateTypeToString(type) + '/' + endpoint; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 640 | auto event = sdeventplus::Event::get_default(); |
| 641 | // Attach the bus to sd_event to service user requests |
| 642 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 643 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 644 | std::move(certDir)); |
| 645 | MainApp mainApp(&manager); |
| 646 | mainApp.install(certificateFile); |
| 647 | |
| 648 | std::vector<std::unique_ptr<Certificate>>& certs = |
| 649 | manager.getCertificates(); |
| 650 | constexpr const unsigned int REPLACE_ITERATIONS = 10; |
| 651 | |
| 652 | for (unsigned int i = 0; i < REPLACE_ITERATIONS; i++) |
| 653 | { |
| 654 | // Certificate successfully installed |
| 655 | EXPECT_FALSE(certs.empty()); |
| 656 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 657 | std::string verifyPath = |
| 658 | verifyDir + "/" + getCertSubjectNameHash(certificateFile) + ".0"; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 659 | |
| 660 | // Check that certificate has been created at installation directory |
| 661 | EXPECT_FALSE(fs::is_empty(verifyDir)); |
| 662 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 663 | |
| 664 | // Check that installed cert is identical to input one |
| 665 | EXPECT_TRUE(compareFiles(certificateFile, verifyPath)); |
| 666 | |
| 667 | // Create new certificate |
| 668 | createNewCertificate(true); |
| 669 | |
| 670 | certs[0]->replace(certificateFile); |
| 671 | |
| 672 | // Verify that old certificate has been removed |
| 673 | EXPECT_FALSE(fs::exists(verifyPath)); |
| 674 | } |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 675 | } |
| 676 | |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 677 | /** @brief Test verifiing if delete function works. |
| 678 | */ |
| 679 | TEST_F(TestCertificates, TestStorageDeleteCertificate) |
| 680 | { |
| 681 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 682 | std::string unit; |
| 683 | CertificateType type = CertificateType::Authority; |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 684 | std::string verifyDir(certDir); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 685 | std::string verifyUnit(unit); |
| 686 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 687 | certificateTypeToString(type) + '/' + endpoint; |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 688 | auto event = sdeventplus::Event::get_default(); |
| 689 | // Attach the bus to sd_event to service user requests |
| 690 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 691 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 692 | std::move(certDir)); |
| 693 | MainApp mainApp(&manager); |
| 694 | |
| 695 | // Check if certificate placeholder dir is empty |
| 696 | EXPECT_TRUE(fs::is_empty(verifyDir)); |
| 697 | mainApp.install(certificateFile); |
| 698 | |
| 699 | // Create new certificate |
| 700 | createNewCertificate(true); |
| 701 | mainApp.install(certificateFile); |
| 702 | |
| 703 | createNewCertificate(true); |
| 704 | mainApp.install(certificateFile); |
| 705 | |
| 706 | std::vector<std::unique_ptr<Certificate>>& certs = |
| 707 | manager.getCertificates(); |
| 708 | |
| 709 | // All 3 certificates successfully installed and added to manager |
| 710 | EXPECT_EQ(certs.size(), 3); |
| 711 | |
| 712 | // Check if certificate placeholder is not empty, there should be 3 |
| 713 | // certificates |
| 714 | EXPECT_FALSE(fs::is_empty(verifyDir)); |
| 715 | |
| 716 | certs[0]->delete_(); |
| 717 | EXPECT_EQ(certs.size(), 2); |
| 718 | |
| 719 | certs[0]->delete_(); |
| 720 | EXPECT_EQ(certs.size(), 1); |
| 721 | |
| 722 | certs[0]->delete_(); |
| 723 | EXPECT_EQ(certs.size(), 0); |
| 724 | |
| 725 | // Check if certificate placeholder is empty. |
| 726 | EXPECT_TRUE(fs::is_empty(verifyDir)); |
| 727 | } |
| 728 | |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 729 | /** @brief Check if install fails if certificate file is empty |
| 730 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 731 | TEST_F(TestCertificates, TestEmptyCertificateFile) |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 732 | { |
| 733 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 734 | std::string unit; |
| 735 | CertificateType type = CertificateType::Client; |
| 736 | ; |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 737 | std::string installPath(certDir + "/" + certificateFile); |
| 738 | std::string verifyPath(installPath); |
| 739 | std::string verifyUnit(unit); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 740 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 741 | certificateTypeToString(type) + '/' + endpoint; |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 742 | std::string emptyFile("emptycert.pem"); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 743 | std::ofstream ofs; |
| 744 | ofs.open(emptyFile, std::ofstream::out); |
| 745 | ofs.close(); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 746 | EXPECT_THROW( |
| 747 | { |
| 748 | try |
| 749 | { |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 750 | auto event = sdeventplus::Event::get_default(); |
| 751 | // Attach the bus to sd_event to service user requests |
| 752 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 753 | Manager manager(bus, event, objPath.c_str(), type, |
| 754 | std::move(unit), std::move(installPath)); |
| 755 | MainApp mainApp(&manager); |
| 756 | mainApp.install(emptyFile); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 757 | } |
| 758 | catch (const InvalidCertificate& e) |
| 759 | { |
| 760 | throw; |
| 761 | } |
| 762 | }, |
| 763 | InvalidCertificate); |
| 764 | EXPECT_FALSE(fs::exists(verifyPath)); |
| 765 | fs::remove(emptyFile); |
| 766 | } |
| 767 | |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 768 | /** @brief Check if install fails if certificate file is corrupted |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 769 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 770 | TEST_F(TestCertificates, TestInvalidCertificateFile) |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 771 | { |
| 772 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 773 | std::string unit; |
| 774 | CertificateType type = CertificateType::Client; |
| 775 | ; |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 776 | |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 777 | std::ofstream ofs; |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 778 | ofs.open(certificateFile, std::ofstream::out); |
| 779 | ofs << "-----BEGIN CERTIFICATE-----"; |
| 780 | ofs << "ADD_SOME_INVALID_DATA_INTO_FILE"; |
| 781 | ofs << "-----END CERTIFICATE-----"; |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 782 | ofs.close(); |
| 783 | |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 784 | std::string installPath(certDir + "/" + certificateFile); |
| 785 | std::string verifyPath(installPath); |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 786 | std::string verifyUnit(unit); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 787 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 788 | certificateTypeToString(type) + '/' + endpoint; |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 789 | EXPECT_THROW( |
| 790 | { |
| 791 | try |
| 792 | { |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 793 | auto event = sdeventplus::Event::get_default(); |
| 794 | // Attach the bus to sd_event to service user requests |
| 795 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 796 | Manager manager(bus, event, objPath.c_str(), type, |
| 797 | std::move(unit), std::move(installPath)); |
| 798 | MainApp mainApp(&manager); |
| 799 | mainApp.install(certificateFile); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 800 | } |
| 801 | catch (const InvalidCertificate& e) |
| 802 | { |
| 803 | throw; |
| 804 | } |
| 805 | }, |
| 806 | InvalidCertificate); |
| 807 | EXPECT_FALSE(fs::exists(verifyPath)); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 808 | } |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 809 | |
| 810 | /** |
| 811 | * Class to generate private and certificate only file and test verification |
| 812 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 813 | class TestInvalidCertificate : public ::testing::Test |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 814 | { |
| 815 | public: |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 816 | TestInvalidCertificate() : bus(sdbusplus::bus::new_default()) |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 817 | { |
| 818 | } |
| 819 | void SetUp() override |
| 820 | { |
| 821 | char dirTemplate[] = "/tmp/FakeCerts.XXXXXX"; |
| 822 | auto dirPtr = mkdtemp(dirTemplate); |
Nan Zhou | cfb5802 | 2021-12-28 11:02:26 -0800 | [diff] [blame] | 823 | if (dirPtr == nullptr) |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 824 | { |
| 825 | throw std::bad_alloc(); |
| 826 | } |
Zbigniew Lukwinski | fe590c4 | 2019-12-10 12:33:50 +0100 | [diff] [blame] | 827 | certDir = std::string(dirPtr) + "/certs"; |
| 828 | fs::create_directories(certDir); |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 829 | certificateFile = "cert.pem"; |
| 830 | keyFile = "key.pem"; |
| 831 | std::string cmd = "openssl req -x509 -sha256 -newkey rsa:2048 "; |
| 832 | cmd += "-keyout key.pem -out cert.pem -days 3650 "; |
| 833 | cmd += "-subj " |
| 834 | "/O=openbmc-project.xyz/CN=localhost" |
| 835 | " -nodes"; |
| 836 | |
| 837 | auto val = std::system(cmd.c_str()); |
| 838 | if (val) |
| 839 | { |
| 840 | std::cout << "command Error: " << val << std::endl; |
| 841 | } |
| 842 | } |
| 843 | void TearDown() override |
| 844 | { |
| 845 | fs::remove_all(certDir); |
| 846 | fs::remove(certificateFile); |
| 847 | fs::remove(keyFile); |
| 848 | } |
| 849 | |
| 850 | protected: |
| 851 | sdbusplus::bus::bus bus; |
| 852 | std::string certificateFile; |
| 853 | std::string keyFile; |
| 854 | std::string certDir; |
| 855 | }; |
| 856 | |
| 857 | /** @brief Check install fails if private key is missing in certificate file |
| 858 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 859 | TEST_F(TestInvalidCertificate, TestMissingPrivateKey) |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 860 | { |
| 861 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 862 | std::string unit; |
| 863 | CertificateType type = CertificateType::Client; |
| 864 | ; |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 865 | std::string installPath(certDir + "/" + certificateFile); |
| 866 | std::string verifyPath(installPath); |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 867 | std::string verifyUnit(unit); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 868 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 869 | certificateTypeToString(type) + '/' + endpoint; |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 870 | EXPECT_THROW( |
| 871 | { |
| 872 | try |
| 873 | { |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 874 | auto event = sdeventplus::Event::get_default(); |
| 875 | // Attach the bus to sd_event to service user requests |
| 876 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 877 | Manager manager(bus, event, objPath.c_str(), type, |
| 878 | std::move(unit), std::move(installPath)); |
| 879 | MainApp mainApp(&manager); |
| 880 | mainApp.install(certificateFile); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 881 | } |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 882 | catch (const InternalFailure& e) |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 883 | { |
| 884 | throw; |
| 885 | } |
| 886 | }, |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 887 | InternalFailure); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 888 | EXPECT_FALSE(fs::exists(verifyPath)); |
| 889 | } |
| 890 | |
| 891 | /** @brief Check install fails if ceritificate is missing in certificate file |
| 892 | */ |
| 893 | TEST_F(TestInvalidCertificate, TestMissingCeritificate) |
| 894 | { |
| 895 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 896 | std::string unit; |
| 897 | CertificateType type = CertificateType::Client; |
| 898 | ; |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 899 | std::string installPath(certDir + "/" + keyFile); |
| 900 | std::string verifyPath(installPath); |
| 901 | std::string verifyUnit(unit); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 902 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 903 | certificateTypeToString(type) + '/' + endpoint; |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 904 | EXPECT_THROW( |
| 905 | { |
| 906 | try |
| 907 | { |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 908 | auto event = sdeventplus::Event::get_default(); |
| 909 | // Attach the bus to sd_event to service user requests |
| 910 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 911 | Manager manager(bus, event, objPath.c_str(), type, |
| 912 | std::move(unit), std::move(installPath)); |
| 913 | MainApp mainApp(&manager); |
| 914 | mainApp.install(keyFile); |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 915 | } |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 916 | catch (const InternalFailure& e) |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 917 | { |
| 918 | throw; |
| 919 | } |
| 920 | }, |
| 921 | InvalidCertificate); |
| 922 | EXPECT_FALSE(fs::exists(verifyPath)); |
| 923 | } |
| 924 | |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 925 | /** @brief Check if error is thrown when multiple certificates are installed |
| 926 | * At present only one certificate per service is allowed |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 927 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 928 | TEST_F(TestCertificates, TestCertInstallNotAllowed) |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 929 | { |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 930 | using NotAllowed = |
| 931 | sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 932 | std::string endpoint("ldap"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 933 | std::string unit; |
| 934 | CertificateType type = CertificateType::Client; |
| 935 | ; |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 936 | std::string installPath(certDir + "/" + certificateFile); |
| 937 | std::string verifyPath(installPath); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 938 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 939 | certificateTypeToString(type) + '/' + endpoint; |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 940 | auto event = sdeventplus::Event::get_default(); |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 941 | // Attach the bus to sd_event to service user requests |
| 942 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 943 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 944 | std::move(installPath)); |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 945 | MainApp mainApp(&manager); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 946 | mainApp.install(certificateFile); |
| 947 | EXPECT_TRUE(fs::exists(verifyPath)); |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 948 | EXPECT_THROW( |
| 949 | { |
| 950 | try |
| 951 | { |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 952 | // install second certificate |
| 953 | mainApp.install(certificateFile); |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 954 | } |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 955 | catch (const NotAllowed& e) |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 956 | { |
| 957 | throw; |
| 958 | } |
| 959 | }, |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 960 | NotAllowed); |
Marri Devender Rao | 9abfae8 | 2018-10-03 08:10:35 -0500 | [diff] [blame] | 961 | } |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 962 | |
| 963 | TEST_F(TestCertificates, TestGenerateCSR) |
| 964 | { |
| 965 | std::string endpoint("https"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 966 | std::string unit; |
| 967 | CertificateType type = CertificateType::Server; |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 968 | std::string installPath(certDir + "/" + certificateFile); |
| 969 | std::string verifyPath(installPath); |
| 970 | std::string CSRPath(certDir + "/" + CSRFile); |
| 971 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 972 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 973 | std::string challengePassword("Password"); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 974 | std::string city("HYB"); |
| 975 | std::string commonName("abc.com"); |
| 976 | std::string contactPerson("Admin"); |
| 977 | std::string country("IN"); |
| 978 | std::string email("admin@in.ibm.com"); |
| 979 | std::string givenName("givenName"); |
| 980 | std::string initials("G"); |
| 981 | int64_t keyBitLength(2048); |
| 982 | std::string keyCurveId("0"); |
| 983 | std::string keyPairAlgorithm("RSA"); |
| 984 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 985 | std::string organization("IBM"); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 986 | std::string organizationalUnit("orgUnit"); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 987 | std::string state("TS"); |
| 988 | std::string surname("surname"); |
| 989 | std::string unstructuredName("unstructuredName"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 990 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 991 | certificateTypeToString(type) + '/' + endpoint; |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 992 | auto event = sdeventplus::Event::get_default(); |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 993 | // Attach the bus to sd_event to service user requests |
| 994 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 995 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 996 | std::move(installPath)); |
| 997 | Status status; |
| 998 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 999 | MainApp mainApp(&manager, &csr); |
| 1000 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 1001 | contactPerson, country, email, givenName, initials, |
| 1002 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 1003 | organization, organizationalUnit, state, surname, |
| 1004 | unstructuredName); |
| 1005 | std::string csrData(""); |
| 1006 | // generateCSR takes considerable time to create CSR and privateKey Files |
| 1007 | EXPECT_FALSE(fs::exists(CSRPath)); |
| 1008 | EXPECT_FALSE(fs::exists(privateKeyPath)); |
| 1009 | EXPECT_THROW( |
| 1010 | { |
| 1011 | try |
| 1012 | { |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 1013 | csrData = csr.csr(); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 1014 | } |
| 1015 | catch (const InternalFailure& e) |
| 1016 | { |
| 1017 | throw; |
| 1018 | } |
| 1019 | }, |
| 1020 | InternalFailure); |
| 1021 | // wait for 10 sec to get CSR and privateKey Files generated |
| 1022 | sleep(10); |
| 1023 | EXPECT_TRUE(fs::exists(CSRPath)); |
| 1024 | EXPECT_TRUE(fs::exists(privateKeyPath)); |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 1025 | csrData = csr.csr(); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 1026 | ASSERT_NE("", csrData.c_str()); |
| 1027 | } |
| 1028 | |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 1029 | /** @brief Check if ECC key pair is generated when user is not given algorithm |
| 1030 | * type. At present RSA and EC key pair algorithm are supported |
| 1031 | */ |
| 1032 | TEST_F(TestCertificates, TestGenerateCSRwithEmptyKeyPairAlgorithm) |
| 1033 | { |
| 1034 | std::string endpoint("https"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1035 | std::string unit; |
| 1036 | CertificateType type = CertificateType::Server; |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 1037 | std::string installPath(certDir + "/" + certificateFile); |
| 1038 | std::string verifyPath(installPath); |
| 1039 | std::string CSRPath(certDir + "/" + CSRFile); |
| 1040 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 1041 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 1042 | std::string challengePassword("Password"); |
| 1043 | std::string city("HYB"); |
| 1044 | std::string commonName("abc.com"); |
| 1045 | std::string contactPerson("Admin"); |
| 1046 | std::string country("IN"); |
| 1047 | std::string email("admin@in.ibm.com"); |
| 1048 | std::string givenName("givenName"); |
| 1049 | std::string initials("G"); |
| 1050 | int64_t keyBitLength(2048); |
| 1051 | std::string keyCurveId(""); |
| 1052 | std::string keyPairAlgorithm(""); |
| 1053 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 1054 | std::string organization("IBM"); |
| 1055 | std::string organizationalUnit("orgUnit"); |
| 1056 | std::string state("TS"); |
| 1057 | std::string surname("surname"); |
| 1058 | std::string unstructuredName("unstructuredName"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1059 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 1060 | certificateTypeToString(type) + '/' + endpoint; |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 1061 | auto event = sdeventplus::Event::get_default(); |
| 1062 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 1063 | std::move(installPath)); |
| 1064 | Status status; |
| 1065 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 1066 | MainApp mainApp(&manager, &csr); |
| 1067 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 1068 | contactPerson, country, email, givenName, initials, |
| 1069 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 1070 | organization, organizationalUnit, state, surname, |
| 1071 | unstructuredName); |
| 1072 | sleep(10); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 1073 | EXPECT_TRUE(fs::exists(CSRPath)); |
| 1074 | EXPECT_TRUE(fs::exists(privateKeyPath)); |
| 1075 | } |
| 1076 | |
| 1077 | /** @brief Check if error is thrown when giving un supported key pair |
| 1078 | * algorithm. At present RSA and EC key pair algorithm are supported |
| 1079 | */ |
| 1080 | TEST_F(TestCertificates, TestGenerateCSRwithUnsupportedKeyPairAlgorithm) |
| 1081 | { |
| 1082 | std::string endpoint("https"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1083 | std::string unit; |
| 1084 | CertificateType type = CertificateType::Server; |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 1085 | std::string installPath(certDir + "/" + certificateFile); |
| 1086 | std::string verifyPath(installPath); |
| 1087 | std::string CSRPath(certDir + "/" + CSRFile); |
| 1088 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 1089 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 1090 | std::string challengePassword("Password"); |
| 1091 | std::string city("HYB"); |
| 1092 | std::string commonName("abc.com"); |
| 1093 | std::string contactPerson("Admin"); |
| 1094 | std::string country("IN"); |
| 1095 | std::string email("admin@in.ibm.com"); |
| 1096 | std::string givenName("givenName"); |
| 1097 | std::string initials("G"); |
| 1098 | int64_t keyBitLength(2048); |
| 1099 | std::string keyCurveId("secp521r1"); |
| 1100 | std::string keyPairAlgorithm("UnSupportedAlgorithm"); |
| 1101 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 1102 | std::string organization("IBM"); |
| 1103 | std::string organizationalUnit("orgUnit"); |
| 1104 | std::string state("TS"); |
| 1105 | std::string surname("surname"); |
| 1106 | std::string unstructuredName("unstructuredName"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1107 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 1108 | certificateTypeToString(type) + '/' + endpoint; |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 1109 | auto event = sdeventplus::Event::get_default(); |
| 1110 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 1111 | std::move(installPath)); |
| 1112 | Status status; |
| 1113 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 1114 | MainApp mainApp(&manager, &csr); |
| 1115 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 1116 | contactPerson, country, email, givenName, initials, |
| 1117 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 1118 | organization, organizationalUnit, state, surname, |
| 1119 | unstructuredName); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 1120 | EXPECT_FALSE(fs::exists(CSRPath)); |
| 1121 | EXPECT_FALSE(fs::exists(privateKeyPath)); |
| 1122 | } |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 1123 | |
| 1124 | /** @brief Check if error is thrown when NID_undef is returned for given key |
| 1125 | * curve id |
| 1126 | */ |
| 1127 | TEST_F(TestCertificates, TestECKeyGenerationwithNIDundefCase) |
| 1128 | { |
| 1129 | std::string endpoint("https"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1130 | std::string unit; |
| 1131 | CertificateType type = CertificateType::Server; |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 1132 | std::string installPath(certDir + "/" + certificateFile); |
| 1133 | std::string verifyPath(installPath); |
| 1134 | std::string CSRPath(certDir + "/" + CSRFile); |
| 1135 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 1136 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 1137 | std::string challengePassword("Password"); |
| 1138 | std::string city("BLR"); |
| 1139 | std::string commonName("abc.com"); |
| 1140 | std::string contactPerson("Admin"); |
| 1141 | std::string country("IN"); |
| 1142 | std::string email("admin@in.ibm.com"); |
| 1143 | std::string givenName("givenName"); |
| 1144 | std::string initials("G"); |
| 1145 | int64_t keyBitLength(2048); |
| 1146 | std::string keyCurveId("DummyCurveName"); |
| 1147 | std::string keyPairAlgorithm("EC"); |
| 1148 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 1149 | std::string organization("IBM"); |
| 1150 | std::string organizationalUnit("orgUnit"); |
| 1151 | std::string state("TS"); |
| 1152 | std::string surname("surname"); |
| 1153 | std::string unstructuredName("unstructuredName"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1154 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 1155 | certificateTypeToString(type) + '/' + endpoint; |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 1156 | auto event = sdeventplus::Event::get_default(); |
| 1157 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 1158 | std::move(installPath)); |
| 1159 | Status status; |
| 1160 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 1161 | MainApp mainApp(&manager, &csr); |
| 1162 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 1163 | contactPerson, country, email, givenName, initials, |
| 1164 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 1165 | organization, organizationalUnit, state, surname, |
| 1166 | unstructuredName); |
| 1167 | EXPECT_FALSE(fs::exists(CSRPath)); |
| 1168 | EXPECT_FALSE(fs::exists(privateKeyPath)); |
| 1169 | } |
| 1170 | |
| 1171 | /** @brief Check default Key Curve Id is used if given curve id is empty |
| 1172 | */ |
| 1173 | TEST_F(TestCertificates, TestECKeyGenerationwithDefaultKeyCurveId) |
| 1174 | { |
| 1175 | std::string endpoint("https"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1176 | std::string unit; |
| 1177 | CertificateType type = CertificateType::Server; |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 1178 | std::string installPath(certDir + "/" + certificateFile); |
| 1179 | std::string verifyPath(installPath); |
| 1180 | std::string CSRPath(certDir + "/" + CSRFile); |
| 1181 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 1182 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 1183 | std::string challengePassword("Password"); |
| 1184 | std::string city("BLR"); |
| 1185 | std::string commonName("abc.com"); |
| 1186 | std::string contactPerson("Admin"); |
| 1187 | std::string country("IN"); |
| 1188 | std::string email("admin@in.ibm.com"); |
| 1189 | std::string givenName("givenName"); |
| 1190 | std::string initials("G"); |
| 1191 | int64_t keyBitLength(2048); |
| 1192 | std::string keyCurveId(""); |
| 1193 | std::string keyPairAlgorithm("EC"); |
| 1194 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 1195 | std::string organization("IBM"); |
| 1196 | std::string organizationalUnit("orgUnit"); |
| 1197 | std::string state("TS"); |
| 1198 | std::string surname("surname"); |
| 1199 | std::string unstructuredName("unstructuredName"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1200 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 1201 | certificateTypeToString(type) + '/' + endpoint; |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 1202 | auto event = sdeventplus::Event::get_default(); |
| 1203 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 1204 | std::move(installPath)); |
| 1205 | Status status; |
| 1206 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 1207 | MainApp mainApp(&manager, &csr); |
| 1208 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 1209 | contactPerson, country, email, givenName, initials, |
| 1210 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 1211 | organization, organizationalUnit, state, surname, |
| 1212 | unstructuredName); |
| 1213 | sleep(10); |
| 1214 | EXPECT_TRUE(fs::exists(CSRPath)); |
| 1215 | EXPECT_TRUE(fs::exists(privateKeyPath)); |
| 1216 | } |
| 1217 | |
| 1218 | /** @brief Check if error is not thrown to generate EC key pair |
| 1219 | */ |
| 1220 | TEST_F(TestCertificates, TestECKeyGeneration) |
| 1221 | { |
| 1222 | std::string endpoint("https"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1223 | std::string unit; |
| 1224 | CertificateType type = CertificateType::Server; |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 1225 | std::string installPath(certDir + "/" + certificateFile); |
| 1226 | std::string verifyPath(installPath); |
| 1227 | std::string CSRPath(certDir + "/" + CSRFile); |
| 1228 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 1229 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 1230 | std::string challengePassword("Password"); |
| 1231 | std::string city("BLR"); |
| 1232 | std::string commonName("abc.com"); |
| 1233 | std::string contactPerson("Admin"); |
| 1234 | std::string country("IN"); |
| 1235 | std::string email("admin@in.ibm.com"); |
| 1236 | std::string givenName("givenName"); |
| 1237 | std::string initials("G"); |
| 1238 | int64_t keyBitLength(2048); |
| 1239 | std::string keyCurveId("secp521r1"); |
| 1240 | std::string keyPairAlgorithm("EC"); |
| 1241 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 1242 | std::string organization("IBM"); |
| 1243 | std::string organizationalUnit("orgUnit"); |
| 1244 | std::string state("TS"); |
| 1245 | std::string surname("surname"); |
| 1246 | std::string unstructuredName("unstructuredName"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1247 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 1248 | certificateTypeToString(type) + '/' + endpoint; |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 1249 | auto event = sdeventplus::Event::get_default(); |
| 1250 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 1251 | std::move(installPath)); |
| 1252 | Status status; |
| 1253 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 1254 | MainApp mainApp(&manager, &csr); |
| 1255 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 1256 | contactPerson, country, email, givenName, initials, |
| 1257 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 1258 | organization, organizationalUnit, state, surname, |
| 1259 | unstructuredName); |
| 1260 | std::cout << "CSRPath: " << CSRPath << std::endl |
| 1261 | << "privateKeyPath: " << privateKeyPath << std::endl; |
| 1262 | sleep(10); |
| 1263 | EXPECT_TRUE(fs::exists(CSRPath)); |
| 1264 | EXPECT_TRUE(fs::exists(privateKeyPath)); |
| 1265 | } |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1266 | |
Nan Zhou | bf3cf75 | 2021-12-28 11:02:07 -0800 | [diff] [blame] | 1267 | /** @brief Check error is thrown if giving unsupported key bit length to |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1268 | * generate rsa key |
| 1269 | */ |
| 1270 | TEST_F(TestCertificates, TestRSAKeyWithUnsupportedKeyBitLength) |
| 1271 | { |
| 1272 | std::string endpoint("https"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1273 | std::string unit; |
| 1274 | CertificateType type = CertificateType::Server; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1275 | std::string installPath(certDir + "/" + certificateFile); |
| 1276 | std::string verifyPath(installPath); |
| 1277 | std::string CSRPath(certDir + "/" + CSRFile); |
| 1278 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 1279 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 1280 | std::string challengePassword("Password"); |
| 1281 | std::string city("BLR"); |
| 1282 | std::string commonName("abc.com"); |
| 1283 | std::string contactPerson("Admin"); |
| 1284 | std::string country("IN"); |
| 1285 | std::string email("admin@in.ibm.com"); |
| 1286 | std::string givenName("givenName"); |
| 1287 | std::string initials("G"); |
| 1288 | int64_t keyBitLength(4096); |
| 1289 | std::string keyCurveId("secp521r1"); |
| 1290 | std::string keyPairAlgorithm("RSA"); |
| 1291 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 1292 | std::string organization("IBM"); |
| 1293 | std::string organizationalUnit("orgUnit"); |
| 1294 | std::string state("TS"); |
| 1295 | std::string surname("surname"); |
| 1296 | std::string unstructuredName("unstructuredName"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1297 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 1298 | certificateTypeToString(type) + '/' + endpoint; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1299 | auto event = sdeventplus::Event::get_default(); |
| 1300 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 1301 | std::move(installPath)); |
| 1302 | Status status; |
| 1303 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 1304 | MainApp mainApp(&manager, &csr); |
| 1305 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 1306 | contactPerson, country, email, givenName, initials, |
| 1307 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 1308 | organization, organizationalUnit, state, surname, |
| 1309 | unstructuredName); |
| 1310 | EXPECT_FALSE(fs::exists(CSRPath)); |
| 1311 | EXPECT_FALSE(fs::exists(privateKeyPath)); |
| 1312 | } |
| 1313 | |
| 1314 | /** @brief Check error is thrown if generated rsa key file is not present |
| 1315 | */ |
| 1316 | TEST_F(TestCertificates, TestRSAKeyFileNotPresentCase) |
| 1317 | { |
| 1318 | std::string endpoint("https"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1319 | std::string unit; |
| 1320 | CertificateType type = CertificateType::Server; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1321 | std::string installPath(certDir + "/" + certificateFile); |
| 1322 | std::string verifyPath(installPath); |
| 1323 | std::string CSRPath(certDir + "/" + CSRFile); |
| 1324 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 1325 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 1326 | std::string challengePassword("Password"); |
| 1327 | std::string city("BLR"); |
| 1328 | std::string commonName("abc.com"); |
| 1329 | std::string contactPerson("Admin"); |
| 1330 | std::string country("IN"); |
| 1331 | std::string email("admin@in.ibm.com"); |
| 1332 | std::string givenName("givenName"); |
| 1333 | std::string initials("G"); |
| 1334 | int64_t keyBitLength(2048); |
| 1335 | std::string keyCurveId("secp521r1"); |
| 1336 | std::string keyPairAlgorithm("RSA"); |
| 1337 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 1338 | std::string organization("IBM"); |
| 1339 | std::string organizationalUnit("orgUnit"); |
| 1340 | std::string state("TS"); |
| 1341 | std::string surname("surname"); |
| 1342 | std::string unstructuredName("unstructuredName"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1343 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 1344 | certificateTypeToString(type) + '/' + endpoint; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1345 | auto event = sdeventplus::Event::get_default(); |
| 1346 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 1347 | std::move(installPath)); |
| 1348 | |
| 1349 | // Removing generated RSA key file |
| 1350 | fs::remove(rsaPrivateKeyFilePath); |
| 1351 | |
| 1352 | Status status; |
| 1353 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 1354 | MainApp mainApp(&manager, &csr); |
| 1355 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 1356 | contactPerson, country, email, givenName, initials, |
| 1357 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 1358 | organization, organizationalUnit, state, surname, |
| 1359 | unstructuredName); |
| 1360 | EXPECT_FALSE(fs::exists(CSRPath)); |
| 1361 | EXPECT_FALSE(fs::exists(privateKeyPath)); |
| 1362 | } |
| 1363 | |
| 1364 | /** @brief Check private key file is created from generated rsa key file is |
| 1365 | * `present |
| 1366 | */ |
| 1367 | TEST_F(TestCertificates, TestRSAKeyFromRSAKeyFileIsWrittenIntoPrivateKeyFile) |
| 1368 | { |
| 1369 | std::string endpoint("https"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1370 | std::string unit; |
| 1371 | CertificateType type = CertificateType::Server; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1372 | std::string installPath(certDir + "/" + certificateFile); |
| 1373 | std::string verifyPath(installPath); |
| 1374 | std::string CSRPath(certDir + "/" + CSRFile); |
| 1375 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 1376 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 1377 | std::string challengePassword("Password"); |
| 1378 | std::string city("BLR"); |
| 1379 | std::string commonName("abc.com"); |
| 1380 | std::string contactPerson("Admin"); |
| 1381 | std::string country("IN"); |
| 1382 | std::string email("admin@in.ibm.com"); |
| 1383 | std::string givenName("givenName"); |
| 1384 | std::string initials("G"); |
| 1385 | int64_t keyBitLength(2048); |
| 1386 | std::string keyCurveId("secp521r1"); |
| 1387 | std::string keyPairAlgorithm("RSA"); |
| 1388 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 1389 | std::string organization("IBM"); |
| 1390 | std::string organizationalUnit("orgUnit"); |
| 1391 | std::string state("TS"); |
| 1392 | std::string surname("surname"); |
| 1393 | std::string unstructuredName("unstructuredName"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1394 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 1395 | certificateTypeToString(type) + '/' + endpoint; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1396 | auto event = sdeventplus::Event::get_default(); |
| 1397 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 1398 | std::move(installPath)); |
| 1399 | Status status; |
| 1400 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 1401 | MainApp mainApp(&manager, &csr); |
| 1402 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 1403 | contactPerson, country, email, givenName, initials, |
| 1404 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 1405 | organization, organizationalUnit, state, surname, |
| 1406 | unstructuredName); |
| 1407 | sleep(10); |
| 1408 | EXPECT_TRUE(fs::exists(CSRPath)); |
| 1409 | EXPECT_TRUE(fs::exists(privateKeyPath)); |
| 1410 | } |
| 1411 | |
Nan Zhou | bf3cf75 | 2021-12-28 11:02:07 -0800 | [diff] [blame] | 1412 | /** @brief Check RSA key is generated during application startup*/ |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1413 | TEST_F(TestCertificates, TestGenerateRSAPrivateKeyFile) |
| 1414 | { |
| 1415 | std::string endpoint("https"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1416 | std::string unit; |
| 1417 | CertificateType type = CertificateType::Server; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1418 | std::string installPath(certDir + "/" + certificateFile); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1419 | auto objPath = std::string(objectNamePrefix) + '/' + |
| 1420 | certificateTypeToString(type) + '/' + endpoint; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1421 | auto event = sdeventplus::Event::get_default(); |
| 1422 | |
| 1423 | EXPECT_FALSE(fs::exists(rsaPrivateKeyFilePath)); |
| 1424 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 1425 | std::move(installPath)); |
| 1426 | EXPECT_TRUE(fs::exists(rsaPrivateKeyFilePath)); |
| 1427 | } |
Nan Zhou | e1289ad | 2021-12-28 11:02:56 -0800 | [diff] [blame] | 1428 | } // namespace |
| 1429 | } // namespace phosphor::certs |