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" |
| 5 | |
| 6 | #include <algorithm> |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 7 | #include <filesystem> |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 8 | #include <fstream> |
| 9 | #include <iterator> |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 10 | #include <sdeventplus/event.hpp> |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 11 | #include <string> |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Certs/error.hpp> |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 13 | #include <xyz/openbmc_project/Common/error.hpp> |
| 14 | |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 15 | #include <gtest/gtest.h> |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 16 | namespace fs = std::filesystem; |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 17 | using InternalFailure = |
| 18 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 19 | using InvalidCertificate = |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame] | 20 | sdbusplus::xyz::openbmc_project::Certs::Error::InvalidCertificate; |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 21 | using namespace phosphor::certs; |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 22 | |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 23 | /** |
| 24 | * Class to generate certificate file and test verification of certificate file |
| 25 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 26 | class TestCertificates : public ::testing::Test |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 27 | { |
| 28 | public: |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 29 | TestCertificates() : bus(sdbusplus::bus::new_default()) |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 30 | { |
| 31 | } |
| 32 | void SetUp() override |
| 33 | { |
| 34 | char dirTemplate[] = "/tmp/FakeCerts.XXXXXX"; |
| 35 | auto dirPtr = mkdtemp(dirTemplate); |
| 36 | if (dirPtr == NULL) |
| 37 | { |
| 38 | throw std::bad_alloc(); |
| 39 | } |
| 40 | certDir = dirPtr; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame^] | 41 | |
| 42 | createNewCertificate(); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 43 | } |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame^] | 44 | |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 45 | void TearDown() override |
| 46 | { |
| 47 | fs::remove_all(certDir); |
| 48 | fs::remove(certificateFile); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 49 | fs::remove(CSRFile); |
| 50 | fs::remove(privateKeyFile); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 51 | } |
| 52 | |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame^] | 53 | void createNewCertificate(bool setNewCertId = false) |
| 54 | { |
| 55 | certificateFile = "cert.pem"; |
| 56 | CSRFile = "domain.csr"; |
| 57 | privateKeyFile = "privkey.pem"; |
| 58 | rsaPrivateKeyFilePath = certDir + "/.rsaprivkey.pem"; |
| 59 | std::string cmd = "openssl req -x509 -sha256 -newkey rsa:2048 "; |
| 60 | cmd += "-keyout cert.pem -out cert.pem -days 3650 -nodes"; |
| 61 | cmd += " -subj /O=openbmc-project.xyz/CN=localhost"; |
| 62 | |
| 63 | if (setNewCertId) |
| 64 | { |
| 65 | cmd += std::to_string(certId++); |
| 66 | } |
| 67 | |
| 68 | auto val = std::system(cmd.c_str()); |
| 69 | if (val) |
| 70 | { |
| 71 | std::cout << "COMMAND Error: " << val << std::endl; |
| 72 | } |
| 73 | } |
| 74 | |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 75 | bool compareFiles(const std::string& file1, const std::string& file2) |
| 76 | { |
| 77 | std::ifstream f1(file1, std::ifstream::binary | std::ifstream::ate); |
| 78 | std::ifstream f2(file2, std::ifstream::binary | std::ifstream::ate); |
| 79 | |
| 80 | if (f1.fail() || f2.fail()) |
| 81 | { |
| 82 | return false; // file problem |
| 83 | } |
| 84 | |
| 85 | if (f1.tellg() != f2.tellg()) |
| 86 | { |
| 87 | return false; // size mismatch |
| 88 | } |
| 89 | |
| 90 | // seek back to beginning and use std::equal to compare contents |
| 91 | f1.seekg(0, std::ifstream::beg); |
| 92 | f2.seekg(0, std::ifstream::beg); |
| 93 | return std::equal(std::istreambuf_iterator<char>(f1.rdbuf()), |
| 94 | std::istreambuf_iterator<char>(), |
| 95 | std::istreambuf_iterator<char>(f2.rdbuf())); |
| 96 | } |
| 97 | |
| 98 | protected: |
| 99 | sdbusplus::bus::bus bus; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 100 | std::string certificateFile, CSRFile, privateKeyFile, rsaPrivateKeyFilePath; |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 101 | |
| 102 | std::string certDir; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame^] | 103 | uint64_t certId; |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | class MainApp |
| 107 | { |
| 108 | public: |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 109 | MainApp(phosphor::certs::Manager* manager, |
| 110 | phosphor::certs::CSR* csr = nullptr) : |
| 111 | manager(manager), |
| 112 | csr(csr) |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 113 | { |
| 114 | } |
| 115 | void install(std::string& path) |
| 116 | { |
| 117 | manager->install(path); |
| 118 | } |
Marri Devender Rao | 9abfae8 | 2018-10-03 08:10:35 -0500 | [diff] [blame] | 119 | void delete_() |
| 120 | { |
| 121 | manager->delete_(); |
| 122 | } |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 123 | |
| 124 | std::string generateCSR(std::vector<std::string> alternativeNames, |
| 125 | std::string challengePassword, std::string city, |
| 126 | std::string commonName, std::string contactPerson, |
| 127 | std::string country, std::string email, |
| 128 | std::string givenName, std::string initials, |
| 129 | int64_t keyBitLength, std::string keyCurveId, |
| 130 | std::string keyPairAlgorithm, |
| 131 | std::vector<std::string> keyUsage, |
| 132 | std::string organization, |
| 133 | std::string organizationalUnit, std::string state, |
| 134 | std::string surname, std::string unstructuredName) |
| 135 | { |
| 136 | return (manager->generateCSR( |
| 137 | alternativeNames, challengePassword, city, commonName, |
| 138 | contactPerson, country, email, givenName, initials, keyBitLength, |
| 139 | keyCurveId, keyPairAlgorithm, keyUsage, organization, |
| 140 | organizationalUnit, state, surname, unstructuredName)); |
| 141 | } |
| 142 | std::string cSR() |
| 143 | { |
| 144 | return (csr->cSR()); |
| 145 | } |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 146 | phosphor::certs::Manager* manager; |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 147 | phosphor::certs::CSR* csr; |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 148 | }; |
| 149 | |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 150 | /** @brief Check if server install routine is invoked for server setup |
| 151 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 152 | TEST_F(TestCertificates, InvokeServerInstall) |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 153 | { |
| 154 | std::string endpoint("https"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 155 | std::string unit(""); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 156 | std::string type("server"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 157 | std::string installPath(certDir + "/" + certificateFile); |
| 158 | std::string verifyPath(installPath); |
| 159 | UnitsToRestart verifyUnit(unit); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 160 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 161 | auto event = sdeventplus::Event::get_default(); |
| 162 | // Attach the bus to sd_event to service user requests |
| 163 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 164 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 165 | std::move(installPath)); |
| 166 | MainApp mainApp(&manager); |
| 167 | mainApp.install(certificateFile); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 168 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 169 | } |
| 170 | |
| 171 | /** @brief Check if client install routine is invoked for client setup |
| 172 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 173 | TEST_F(TestCertificates, InvokeClientInstall) |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 174 | { |
| 175 | std::string endpoint("ldap"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 176 | std::string unit(""); |
| 177 | std::string type("server"); |
| 178 | std::string installPath(certDir + "/" + certificateFile); |
| 179 | std::string verifyPath(installPath); |
| 180 | UnitsToRestart verifyUnit(unit); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 181 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 182 | auto event = sdeventplus::Event::get_default(); |
| 183 | // Attach the bus to sd_event to service user requests |
| 184 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 185 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 186 | std::move(installPath)); |
| 187 | MainApp mainApp(&manager); |
| 188 | mainApp.install(certificateFile); |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 189 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 190 | } |
| 191 | |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame^] | 192 | /** @brief Check if storage install routine is invoked for storage setup |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 193 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 194 | TEST_F(TestCertificates, InvokeAuthorityInstall) |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 195 | { |
| 196 | std::string endpoint("ldap"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 197 | std::string unit(""); |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 198 | std::string type("authority"); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame^] | 199 | std::string verifyDir(certDir); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 200 | UnitsToRestart verifyUnit(unit); |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 201 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 202 | auto event = sdeventplus::Event::get_default(); |
| 203 | // Attach the bus to sd_event to service user requests |
| 204 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 205 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame^] | 206 | std::move(certDir)); |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 207 | MainApp mainApp(&manager); |
| 208 | mainApp.install(certificateFile); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame^] | 209 | |
| 210 | std::vector<std::unique_ptr<Certificate>>& certs = |
| 211 | manager.getCertificates(); |
| 212 | |
| 213 | EXPECT_FALSE(certs.empty()); |
| 214 | |
| 215 | std::string verifyPath = verifyDir + "/" + certs[0]->getHash() + ".0"; |
| 216 | |
| 217 | // Check that certificate has been created at installation directory |
| 218 | EXPECT_FALSE(fs::is_empty(verifyDir)); |
| 219 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 220 | |
| 221 | // Check that installed cert is identical to input one |
| 222 | EXPECT_TRUE(compareFiles(certificateFile, verifyPath)); |
| 223 | } |
| 224 | |
| 225 | /** @brief Check if in athority mode user can install a certificate with certain |
| 226 | * subject hash once, but cannot install another one with the same hash |
| 227 | */ |
| 228 | TEST_F(TestCertificates, InvokeInstallSameSubjectTwice) |
| 229 | { |
| 230 | using NotAllowed = |
| 231 | sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
| 232 | |
| 233 | std::string endpoint("ldap"); |
| 234 | std::string unit(""); |
| 235 | std::string type("authority"); |
| 236 | std::string verifyDir(certDir); |
| 237 | UnitsToRestart verifyUnit(unit); |
| 238 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 239 | auto event = sdeventplus::Event::get_default(); |
| 240 | // Attach the bus to sd_event to service user requests |
| 241 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 242 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 243 | std::move(certDir)); |
| 244 | MainApp mainApp(&manager); |
| 245 | mainApp.install(certificateFile); |
| 246 | |
| 247 | std::vector<std::unique_ptr<Certificate>>& certs = |
| 248 | manager.getCertificates(); |
| 249 | |
| 250 | EXPECT_FALSE(certs.empty()); |
| 251 | |
| 252 | std::string verifyPath = verifyDir + "/" + certs[0]->getHash() + ".0"; |
| 253 | |
| 254 | // Check that certificate has been created at installation directory |
| 255 | EXPECT_FALSE(fs::is_empty(verifyDir)); |
| 256 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 257 | |
| 258 | // Check that installed cert is identical to input one |
| 259 | EXPECT_TRUE(compareFiles(certificateFile, verifyPath)); |
| 260 | |
| 261 | // Try to install another one with the same subject |
| 262 | createNewCertificate(false); |
| 263 | |
| 264 | EXPECT_THROW( |
| 265 | { |
| 266 | try |
| 267 | { |
| 268 | // install second certificate |
| 269 | mainApp.install(certificateFile); |
| 270 | } |
| 271 | catch (const NotAllowed& e) |
| 272 | { |
| 273 | throw; |
| 274 | } |
| 275 | }, |
| 276 | NotAllowed); |
| 277 | |
| 278 | // Check that the original certificate has been not removed |
| 279 | EXPECT_FALSE(fs::is_empty(verifyDir)); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 280 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 281 | } |
| 282 | |
| 283 | /** @brief Compare the installed certificate with the copied certificate |
| 284 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 285 | TEST_F(TestCertificates, CompareInstalledCertificate) |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 286 | { |
| 287 | std::string endpoint("ldap"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 288 | std::string unit(""); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 289 | std::string type("client"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 290 | std::string installPath(certDir + "/" + certificateFile); |
| 291 | std::string verifyPath(installPath); |
| 292 | UnitsToRestart verifyUnit(unit); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 293 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 294 | auto event = sdeventplus::Event::get_default(); |
| 295 | // Attach the bus to sd_event to service user requests |
| 296 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 297 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 298 | std::move(installPath)); |
| 299 | MainApp mainApp(&manager); |
| 300 | mainApp.install(certificateFile); |
Marri Devender Rao | 947258d | 2018-09-25 10:52:24 -0500 | [diff] [blame] | 301 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 302 | EXPECT_TRUE(compareFiles(verifyPath, certificateFile)); |
| 303 | } |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 304 | |
| 305 | /** @brief Check if install fails if certificate file is not found |
| 306 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 307 | TEST_F(TestCertificates, TestNoCertificateFile) |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 308 | { |
| 309 | std::string endpoint("ldap"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 310 | std::string unit(""); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 311 | std::string type("client"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 312 | std::string installPath(certDir + "/" + certificateFile); |
| 313 | std::string verifyPath(installPath); |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 314 | std::string verifyUnit(unit); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 315 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 316 | std::string uploadFile = "nofile.pem"; |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 317 | EXPECT_THROW( |
| 318 | { |
| 319 | try |
| 320 | { |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 321 | auto event = sdeventplus::Event::get_default(); |
| 322 | // Attach the bus to sd_event to service user requests |
| 323 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 324 | Manager manager(bus, event, objPath.c_str(), type, |
| 325 | std::move(unit), std::move(installPath)); |
| 326 | MainApp mainApp(&manager); |
| 327 | mainApp.install(uploadFile); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 328 | } |
| 329 | catch (const InternalFailure& e) |
| 330 | { |
| 331 | throw; |
| 332 | } |
| 333 | }, |
| 334 | InternalFailure); |
| 335 | EXPECT_FALSE(fs::exists(verifyPath)); |
| 336 | } |
| 337 | |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 338 | /** @brief Test replacing existing certificate |
| 339 | */ |
| 340 | TEST_F(TestCertificates, TestReplaceCertificate) |
| 341 | { |
| 342 | std::string endpoint("ldap"); |
| 343 | std::string unit(""); |
| 344 | std::string type("server"); |
| 345 | std::string installPath(certDir + "/" + certificateFile); |
| 346 | std::string verifyPath(installPath); |
| 347 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 348 | auto event = sdeventplus::Event::get_default(); |
| 349 | // Attach the bus to sd_event to service user requests |
| 350 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 351 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 352 | std::move(installPath)); |
| 353 | MainApp mainApp(&manager); |
| 354 | mainApp.install(certificateFile); |
| 355 | EXPECT_TRUE(fs::exists(verifyPath)); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame^] | 356 | std::vector<std::unique_ptr<Certificate>>& certs = |
| 357 | manager.getCertificates(); |
| 358 | EXPECT_FALSE(certs.empty()); |
| 359 | EXPECT_NE(certs[0], nullptr); |
| 360 | certs[0]->replace(certificateFile); |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 361 | EXPECT_TRUE(fs::exists(verifyPath)); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame^] | 362 | } |
| 363 | |
| 364 | /** @brief Test replacing existing certificate |
| 365 | */ |
| 366 | TEST_F(TestCertificates, TestAuthorityReplaceCertificate) |
| 367 | { |
| 368 | std::string endpoint("ldap"); |
| 369 | std::string unit(""); |
| 370 | std::string type("authority"); |
| 371 | std::string verifyDir(certDir); |
| 372 | UnitsToRestart verifyUnit(unit); |
| 373 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 374 | auto event = sdeventplus::Event::get_default(); |
| 375 | // Attach the bus to sd_event to service user requests |
| 376 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 377 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 378 | std::move(certDir)); |
| 379 | MainApp mainApp(&manager); |
| 380 | mainApp.install(certificateFile); |
| 381 | |
| 382 | std::vector<std::unique_ptr<Certificate>>& certs = |
| 383 | manager.getCertificates(); |
| 384 | constexpr const unsigned int REPLACE_ITERATIONS = 10; |
| 385 | |
| 386 | for (unsigned int i = 0; i < REPLACE_ITERATIONS; i++) |
| 387 | { |
| 388 | // Certificate successfully installed |
| 389 | EXPECT_FALSE(certs.empty()); |
| 390 | |
| 391 | std::string verifyPath = verifyDir + "/" + certs[0]->getHash() + ".0"; |
| 392 | |
| 393 | // Check that certificate has been created at installation directory |
| 394 | EXPECT_FALSE(fs::is_empty(verifyDir)); |
| 395 | EXPECT_TRUE(fs::exists(verifyPath)); |
| 396 | |
| 397 | // Check that installed cert is identical to input one |
| 398 | EXPECT_TRUE(compareFiles(certificateFile, verifyPath)); |
| 399 | |
| 400 | // Create new certificate |
| 401 | createNewCertificate(true); |
| 402 | |
| 403 | certs[0]->replace(certificateFile); |
| 404 | |
| 405 | // Verify that old certificate has been removed |
| 406 | EXPECT_FALSE(fs::exists(verifyPath)); |
| 407 | } |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 408 | } |
| 409 | |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 410 | /** @brief Check if install fails if certificate file is empty |
| 411 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 412 | TEST_F(TestCertificates, TestEmptyCertificateFile) |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 413 | { |
| 414 | std::string endpoint("ldap"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 415 | std::string unit(""); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 416 | std::string type("client"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 417 | std::string installPath(certDir + "/" + certificateFile); |
| 418 | std::string verifyPath(installPath); |
| 419 | std::string verifyUnit(unit); |
| 420 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 421 | std::string emptyFile("emptycert.pem"); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 422 | std::ofstream ofs; |
| 423 | ofs.open(emptyFile, std::ofstream::out); |
| 424 | ofs.close(); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 425 | EXPECT_THROW( |
| 426 | { |
| 427 | try |
| 428 | { |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 429 | auto event = sdeventplus::Event::get_default(); |
| 430 | // Attach the bus to sd_event to service user requests |
| 431 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 432 | Manager manager(bus, event, objPath.c_str(), type, |
| 433 | std::move(unit), std::move(installPath)); |
| 434 | MainApp mainApp(&manager); |
| 435 | mainApp.install(emptyFile); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 436 | } |
| 437 | catch (const InvalidCertificate& e) |
| 438 | { |
| 439 | throw; |
| 440 | } |
| 441 | }, |
| 442 | InvalidCertificate); |
| 443 | EXPECT_FALSE(fs::exists(verifyPath)); |
| 444 | fs::remove(emptyFile); |
| 445 | } |
| 446 | |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 447 | /** @brief Check if install fails if certificate file is corrupted |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 448 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 449 | TEST_F(TestCertificates, TestInvalidCertificateFile) |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 450 | { |
| 451 | std::string endpoint("ldap"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 452 | std::string unit(""); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 453 | std::string type("client"); |
| 454 | |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 455 | std::ofstream ofs; |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 456 | ofs.open(certificateFile, std::ofstream::out); |
| 457 | ofs << "-----BEGIN CERTIFICATE-----"; |
| 458 | ofs << "ADD_SOME_INVALID_DATA_INTO_FILE"; |
| 459 | ofs << "-----END CERTIFICATE-----"; |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 460 | ofs.close(); |
| 461 | |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 462 | std::string installPath(certDir + "/" + certificateFile); |
| 463 | std::string verifyPath(installPath); |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 464 | std::string verifyUnit(unit); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 465 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 466 | EXPECT_THROW( |
| 467 | { |
| 468 | try |
| 469 | { |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 470 | auto event = sdeventplus::Event::get_default(); |
| 471 | // Attach the bus to sd_event to service user requests |
| 472 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 473 | Manager manager(bus, event, objPath.c_str(), type, |
| 474 | std::move(unit), std::move(installPath)); |
| 475 | MainApp mainApp(&manager); |
| 476 | mainApp.install(certificateFile); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 477 | } |
| 478 | catch (const InvalidCertificate& e) |
| 479 | { |
| 480 | throw; |
| 481 | } |
| 482 | }, |
| 483 | InvalidCertificate); |
| 484 | EXPECT_FALSE(fs::exists(verifyPath)); |
Marri Devender Rao | e6597c5 | 2018-10-01 06:36:55 -0500 | [diff] [blame] | 485 | } |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 486 | |
| 487 | /** |
| 488 | * Class to generate private and certificate only file and test verification |
| 489 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 490 | class TestInvalidCertificate : public ::testing::Test |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 491 | { |
| 492 | public: |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 493 | TestInvalidCertificate() : bus(sdbusplus::bus::new_default()) |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 494 | { |
| 495 | } |
| 496 | void SetUp() override |
| 497 | { |
| 498 | char dirTemplate[] = "/tmp/FakeCerts.XXXXXX"; |
| 499 | auto dirPtr = mkdtemp(dirTemplate); |
| 500 | if (dirPtr == NULL) |
| 501 | { |
| 502 | throw std::bad_alloc(); |
| 503 | } |
| 504 | certDir = dirPtr; |
| 505 | certificateFile = "cert.pem"; |
| 506 | keyFile = "key.pem"; |
| 507 | std::string cmd = "openssl req -x509 -sha256 -newkey rsa:2048 "; |
| 508 | cmd += "-keyout key.pem -out cert.pem -days 3650 "; |
| 509 | cmd += "-subj " |
| 510 | "/O=openbmc-project.xyz/CN=localhost" |
| 511 | " -nodes"; |
| 512 | |
| 513 | auto val = std::system(cmd.c_str()); |
| 514 | if (val) |
| 515 | { |
| 516 | std::cout << "command Error: " << val << std::endl; |
| 517 | } |
| 518 | } |
| 519 | void TearDown() override |
| 520 | { |
| 521 | fs::remove_all(certDir); |
| 522 | fs::remove(certificateFile); |
| 523 | fs::remove(keyFile); |
| 524 | } |
| 525 | |
| 526 | protected: |
| 527 | sdbusplus::bus::bus bus; |
| 528 | std::string certificateFile; |
| 529 | std::string keyFile; |
| 530 | std::string certDir; |
| 531 | }; |
| 532 | |
| 533 | /** @brief Check install fails if private key is missing in certificate file |
| 534 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 535 | TEST_F(TestInvalidCertificate, TestMissingPrivateKey) |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 536 | { |
| 537 | std::string endpoint("ldap"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 538 | std::string unit(""); |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 539 | std::string type("client"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 540 | std::string installPath(certDir + "/" + certificateFile); |
| 541 | std::string verifyPath(installPath); |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 542 | std::string verifyUnit(unit); |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 543 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 544 | EXPECT_THROW( |
| 545 | { |
| 546 | try |
| 547 | { |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 548 | auto event = sdeventplus::Event::get_default(); |
| 549 | // Attach the bus to sd_event to service user requests |
| 550 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 551 | Manager manager(bus, event, objPath.c_str(), type, |
| 552 | std::move(unit), std::move(installPath)); |
| 553 | MainApp mainApp(&manager); |
| 554 | mainApp.install(certificateFile); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 555 | } |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 556 | catch (const InternalFailure& e) |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 557 | { |
| 558 | throw; |
| 559 | } |
| 560 | }, |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 561 | InternalFailure); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 562 | EXPECT_FALSE(fs::exists(verifyPath)); |
| 563 | } |
| 564 | |
| 565 | /** @brief Check install fails if ceritificate is missing in certificate file |
| 566 | */ |
| 567 | TEST_F(TestInvalidCertificate, TestMissingCeritificate) |
| 568 | { |
| 569 | std::string endpoint("ldap"); |
| 570 | std::string unit(""); |
| 571 | std::string type("client"); |
| 572 | std::string installPath(certDir + "/" + keyFile); |
| 573 | std::string verifyPath(installPath); |
| 574 | std::string verifyUnit(unit); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 575 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 576 | EXPECT_THROW( |
| 577 | { |
| 578 | try |
| 579 | { |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 580 | auto event = sdeventplus::Event::get_default(); |
| 581 | // Attach the bus to sd_event to service user requests |
| 582 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 583 | Manager manager(bus, event, objPath.c_str(), type, |
| 584 | std::move(unit), std::move(installPath)); |
| 585 | MainApp mainApp(&manager); |
| 586 | mainApp.install(keyFile); |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 587 | } |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 588 | catch (const InternalFailure& e) |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 589 | { |
| 590 | throw; |
| 591 | } |
| 592 | }, |
| 593 | InvalidCertificate); |
| 594 | EXPECT_FALSE(fs::exists(verifyPath)); |
| 595 | } |
| 596 | |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 597 | /** @brief Check if error is thrown when multiple certificates are installed |
| 598 | * At present only one certificate per service is allowed |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 599 | */ |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 600 | TEST_F(TestCertificates, TestCertInstallNotAllowed) |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 601 | { |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 602 | using NotAllowed = |
| 603 | sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 604 | std::string endpoint("ldap"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 605 | std::string unit(""); |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 606 | std::string type("client"); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 607 | std::string installPath(certDir + "/" + certificateFile); |
| 608 | std::string verifyPath(installPath); |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 609 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 610 | auto event = sdeventplus::Event::get_default(); |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 611 | // Attach the bus to sd_event to service user requests |
| 612 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 613 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 614 | std::move(installPath)); |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 615 | MainApp mainApp(&manager); |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 616 | mainApp.install(certificateFile); |
| 617 | EXPECT_TRUE(fs::exists(verifyPath)); |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 618 | EXPECT_THROW( |
| 619 | { |
| 620 | try |
| 621 | { |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 622 | // install second certificate |
| 623 | mainApp.install(certificateFile); |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 624 | } |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 625 | catch (const NotAllowed& e) |
Marri Devender Rao | ddf6486 | 2018-10-03 07:11:02 -0500 | [diff] [blame] | 626 | { |
| 627 | throw; |
| 628 | } |
| 629 | }, |
Marri Devender Rao | 8841dbd | 2019-03-04 05:43:55 -0600 | [diff] [blame] | 630 | NotAllowed); |
Marri Devender Rao | 9abfae8 | 2018-10-03 08:10:35 -0500 | [diff] [blame] | 631 | } |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 632 | |
| 633 | TEST_F(TestCertificates, TestGenerateCSR) |
| 634 | { |
| 635 | std::string endpoint("https"); |
| 636 | std::string unit(""); |
| 637 | std::string type("Server"); |
| 638 | std::string installPath(certDir + "/" + certificateFile); |
| 639 | std::string verifyPath(installPath); |
| 640 | std::string CSRPath(certDir + "/" + CSRFile); |
| 641 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 642 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 643 | std::string challengePassword("Password"); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 644 | std::string city("HYB"); |
| 645 | std::string commonName("abc.com"); |
| 646 | std::string contactPerson("Admin"); |
| 647 | std::string country("IN"); |
| 648 | std::string email("admin@in.ibm.com"); |
| 649 | std::string givenName("givenName"); |
| 650 | std::string initials("G"); |
| 651 | int64_t keyBitLength(2048); |
| 652 | std::string keyCurveId("0"); |
| 653 | std::string keyPairAlgorithm("RSA"); |
| 654 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 655 | std::string organization("IBM"); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 656 | std::string organizationalUnit("orgUnit"); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 657 | std::string state("TS"); |
| 658 | std::string surname("surname"); |
| 659 | std::string unstructuredName("unstructuredName"); |
| 660 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 661 | auto event = sdeventplus::Event::get_default(); |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 662 | // Attach the bus to sd_event to service user requests |
| 663 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 664 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 665 | std::move(installPath)); |
| 666 | Status status; |
| 667 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 668 | MainApp mainApp(&manager, &csr); |
| 669 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 670 | contactPerson, country, email, givenName, initials, |
| 671 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 672 | organization, organizationalUnit, state, surname, |
| 673 | unstructuredName); |
| 674 | std::string csrData(""); |
| 675 | // generateCSR takes considerable time to create CSR and privateKey Files |
| 676 | EXPECT_FALSE(fs::exists(CSRPath)); |
| 677 | EXPECT_FALSE(fs::exists(privateKeyPath)); |
| 678 | EXPECT_THROW( |
| 679 | { |
| 680 | try |
| 681 | { |
| 682 | csrData = csr.cSR(); |
| 683 | } |
| 684 | catch (const InternalFailure& e) |
| 685 | { |
| 686 | throw; |
| 687 | } |
| 688 | }, |
| 689 | InternalFailure); |
| 690 | // wait for 10 sec to get CSR and privateKey Files generated |
| 691 | sleep(10); |
| 692 | EXPECT_TRUE(fs::exists(CSRPath)); |
| 693 | EXPECT_TRUE(fs::exists(privateKeyPath)); |
| 694 | csrData = csr.cSR(); |
| 695 | ASSERT_NE("", csrData.c_str()); |
| 696 | } |
| 697 | |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 698 | /** @brief Check if ECC key pair is generated when user is not given algorithm |
| 699 | * type. At present RSA and EC key pair algorithm are supported |
| 700 | */ |
| 701 | TEST_F(TestCertificates, TestGenerateCSRwithEmptyKeyPairAlgorithm) |
| 702 | { |
| 703 | std::string endpoint("https"); |
| 704 | std::string unit(""); |
| 705 | std::string type("Server"); |
| 706 | std::string installPath(certDir + "/" + certificateFile); |
| 707 | std::string verifyPath(installPath); |
| 708 | std::string CSRPath(certDir + "/" + CSRFile); |
| 709 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 710 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 711 | std::string challengePassword("Password"); |
| 712 | std::string city("HYB"); |
| 713 | std::string commonName("abc.com"); |
| 714 | std::string contactPerson("Admin"); |
| 715 | std::string country("IN"); |
| 716 | std::string email("admin@in.ibm.com"); |
| 717 | std::string givenName("givenName"); |
| 718 | std::string initials("G"); |
| 719 | int64_t keyBitLength(2048); |
| 720 | std::string keyCurveId(""); |
| 721 | std::string keyPairAlgorithm(""); |
| 722 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 723 | std::string organization("IBM"); |
| 724 | std::string organizationalUnit("orgUnit"); |
| 725 | std::string state("TS"); |
| 726 | std::string surname("surname"); |
| 727 | std::string unstructuredName("unstructuredName"); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 728 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 729 | auto event = sdeventplus::Event::get_default(); |
| 730 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 731 | std::move(installPath)); |
| 732 | Status status; |
| 733 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 734 | MainApp mainApp(&manager, &csr); |
| 735 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 736 | contactPerson, country, email, givenName, initials, |
| 737 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 738 | organization, organizationalUnit, state, surname, |
| 739 | unstructuredName); |
| 740 | sleep(10); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 741 | EXPECT_TRUE(fs::exists(CSRPath)); |
| 742 | EXPECT_TRUE(fs::exists(privateKeyPath)); |
| 743 | } |
| 744 | |
| 745 | /** @brief Check if error is thrown when giving un supported key pair |
| 746 | * algorithm. At present RSA and EC key pair algorithm are supported |
| 747 | */ |
| 748 | TEST_F(TestCertificates, TestGenerateCSRwithUnsupportedKeyPairAlgorithm) |
| 749 | { |
| 750 | std::string endpoint("https"); |
| 751 | std::string unit(""); |
| 752 | std::string type("Server"); |
| 753 | std::string installPath(certDir + "/" + certificateFile); |
| 754 | std::string verifyPath(installPath); |
| 755 | std::string CSRPath(certDir + "/" + CSRFile); |
| 756 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 757 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 758 | std::string challengePassword("Password"); |
| 759 | std::string city("HYB"); |
| 760 | std::string commonName("abc.com"); |
| 761 | std::string contactPerson("Admin"); |
| 762 | std::string country("IN"); |
| 763 | std::string email("admin@in.ibm.com"); |
| 764 | std::string givenName("givenName"); |
| 765 | std::string initials("G"); |
| 766 | int64_t keyBitLength(2048); |
| 767 | std::string keyCurveId("secp521r1"); |
| 768 | std::string keyPairAlgorithm("UnSupportedAlgorithm"); |
| 769 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 770 | std::string organization("IBM"); |
| 771 | std::string organizationalUnit("orgUnit"); |
| 772 | std::string state("TS"); |
| 773 | std::string surname("surname"); |
| 774 | std::string unstructuredName("unstructuredName"); |
| 775 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 776 | auto event = sdeventplus::Event::get_default(); |
| 777 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 778 | std::move(installPath)); |
| 779 | Status status; |
| 780 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 781 | MainApp mainApp(&manager, &csr); |
| 782 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 783 | contactPerson, country, email, givenName, initials, |
| 784 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 785 | organization, organizationalUnit, state, surname, |
| 786 | unstructuredName); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 787 | EXPECT_FALSE(fs::exists(CSRPath)); |
| 788 | EXPECT_FALSE(fs::exists(privateKeyPath)); |
| 789 | } |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 790 | |
| 791 | /** @brief Check if error is thrown when NID_undef is returned for given key |
| 792 | * curve id |
| 793 | */ |
| 794 | TEST_F(TestCertificates, TestECKeyGenerationwithNIDundefCase) |
| 795 | { |
| 796 | std::string endpoint("https"); |
| 797 | std::string unit(""); |
| 798 | std::string type("Server"); |
| 799 | std::string installPath(certDir + "/" + certificateFile); |
| 800 | std::string verifyPath(installPath); |
| 801 | std::string CSRPath(certDir + "/" + CSRFile); |
| 802 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 803 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 804 | std::string challengePassword("Password"); |
| 805 | std::string city("BLR"); |
| 806 | std::string commonName("abc.com"); |
| 807 | std::string contactPerson("Admin"); |
| 808 | std::string country("IN"); |
| 809 | std::string email("admin@in.ibm.com"); |
| 810 | std::string givenName("givenName"); |
| 811 | std::string initials("G"); |
| 812 | int64_t keyBitLength(2048); |
| 813 | std::string keyCurveId("DummyCurveName"); |
| 814 | std::string keyPairAlgorithm("EC"); |
| 815 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 816 | std::string organization("IBM"); |
| 817 | std::string organizationalUnit("orgUnit"); |
| 818 | std::string state("TS"); |
| 819 | std::string surname("surname"); |
| 820 | std::string unstructuredName("unstructuredName"); |
| 821 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 822 | auto event = sdeventplus::Event::get_default(); |
| 823 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 824 | std::move(installPath)); |
| 825 | Status status; |
| 826 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 827 | MainApp mainApp(&manager, &csr); |
| 828 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 829 | contactPerson, country, email, givenName, initials, |
| 830 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 831 | organization, organizationalUnit, state, surname, |
| 832 | unstructuredName); |
| 833 | EXPECT_FALSE(fs::exists(CSRPath)); |
| 834 | EXPECT_FALSE(fs::exists(privateKeyPath)); |
| 835 | } |
| 836 | |
| 837 | /** @brief Check default Key Curve Id is used if given curve id is empty |
| 838 | */ |
| 839 | TEST_F(TestCertificates, TestECKeyGenerationwithDefaultKeyCurveId) |
| 840 | { |
| 841 | std::string endpoint("https"); |
| 842 | std::string unit(""); |
| 843 | std::string type("Server"); |
| 844 | std::string installPath(certDir + "/" + certificateFile); |
| 845 | std::string verifyPath(installPath); |
| 846 | std::string CSRPath(certDir + "/" + CSRFile); |
| 847 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 848 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 849 | std::string challengePassword("Password"); |
| 850 | std::string city("BLR"); |
| 851 | std::string commonName("abc.com"); |
| 852 | std::string contactPerson("Admin"); |
| 853 | std::string country("IN"); |
| 854 | std::string email("admin@in.ibm.com"); |
| 855 | std::string givenName("givenName"); |
| 856 | std::string initials("G"); |
| 857 | int64_t keyBitLength(2048); |
| 858 | std::string keyCurveId(""); |
| 859 | std::string keyPairAlgorithm("EC"); |
| 860 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 861 | std::string organization("IBM"); |
| 862 | std::string organizationalUnit("orgUnit"); |
| 863 | std::string state("TS"); |
| 864 | std::string surname("surname"); |
| 865 | std::string unstructuredName("unstructuredName"); |
| 866 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 867 | auto event = sdeventplus::Event::get_default(); |
| 868 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 869 | std::move(installPath)); |
| 870 | Status status; |
| 871 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 872 | MainApp mainApp(&manager, &csr); |
| 873 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 874 | contactPerson, country, email, givenName, initials, |
| 875 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 876 | organization, organizationalUnit, state, surname, |
| 877 | unstructuredName); |
| 878 | sleep(10); |
| 879 | EXPECT_TRUE(fs::exists(CSRPath)); |
| 880 | EXPECT_TRUE(fs::exists(privateKeyPath)); |
| 881 | } |
| 882 | |
| 883 | /** @brief Check if error is not thrown to generate EC key pair |
| 884 | */ |
| 885 | TEST_F(TestCertificates, TestECKeyGeneration) |
| 886 | { |
| 887 | std::string endpoint("https"); |
| 888 | std::string unit(""); |
| 889 | std::string type("Server"); |
| 890 | std::string installPath(certDir + "/" + certificateFile); |
| 891 | std::string verifyPath(installPath); |
| 892 | std::string CSRPath(certDir + "/" + CSRFile); |
| 893 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 894 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 895 | std::string challengePassword("Password"); |
| 896 | std::string city("BLR"); |
| 897 | std::string commonName("abc.com"); |
| 898 | std::string contactPerson("Admin"); |
| 899 | std::string country("IN"); |
| 900 | std::string email("admin@in.ibm.com"); |
| 901 | std::string givenName("givenName"); |
| 902 | std::string initials("G"); |
| 903 | int64_t keyBitLength(2048); |
| 904 | std::string keyCurveId("secp521r1"); |
| 905 | std::string keyPairAlgorithm("EC"); |
| 906 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 907 | std::string organization("IBM"); |
| 908 | std::string organizationalUnit("orgUnit"); |
| 909 | std::string state("TS"); |
| 910 | std::string surname("surname"); |
| 911 | std::string unstructuredName("unstructuredName"); |
| 912 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 913 | auto event = sdeventplus::Event::get_default(); |
| 914 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 915 | std::move(installPath)); |
| 916 | Status status; |
| 917 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 918 | MainApp mainApp(&manager, &csr); |
| 919 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 920 | contactPerson, country, email, givenName, initials, |
| 921 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 922 | organization, organizationalUnit, state, surname, |
| 923 | unstructuredName); |
| 924 | std::cout << "CSRPath: " << CSRPath << std::endl |
| 925 | << "privateKeyPath: " << privateKeyPath << std::endl; |
| 926 | sleep(10); |
| 927 | EXPECT_TRUE(fs::exists(CSRPath)); |
| 928 | EXPECT_TRUE(fs::exists(privateKeyPath)); |
| 929 | } |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 930 | |
| 931 | /** @brief Check error is thrown if giving unsupported ket bit length to |
| 932 | * generate rsa key |
| 933 | */ |
| 934 | TEST_F(TestCertificates, TestRSAKeyWithUnsupportedKeyBitLength) |
| 935 | { |
| 936 | std::string endpoint("https"); |
| 937 | std::string unit(""); |
| 938 | std::string type("Server"); |
| 939 | std::string installPath(certDir + "/" + certificateFile); |
| 940 | std::string verifyPath(installPath); |
| 941 | std::string CSRPath(certDir + "/" + CSRFile); |
| 942 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 943 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 944 | std::string challengePassword("Password"); |
| 945 | std::string city("BLR"); |
| 946 | std::string commonName("abc.com"); |
| 947 | std::string contactPerson("Admin"); |
| 948 | std::string country("IN"); |
| 949 | std::string email("admin@in.ibm.com"); |
| 950 | std::string givenName("givenName"); |
| 951 | std::string initials("G"); |
| 952 | int64_t keyBitLength(4096); |
| 953 | std::string keyCurveId("secp521r1"); |
| 954 | std::string keyPairAlgorithm("RSA"); |
| 955 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 956 | std::string organization("IBM"); |
| 957 | std::string organizationalUnit("orgUnit"); |
| 958 | std::string state("TS"); |
| 959 | std::string surname("surname"); |
| 960 | std::string unstructuredName("unstructuredName"); |
| 961 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 962 | auto event = sdeventplus::Event::get_default(); |
| 963 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 964 | std::move(installPath)); |
| 965 | Status status; |
| 966 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 967 | MainApp mainApp(&manager, &csr); |
| 968 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 969 | contactPerson, country, email, givenName, initials, |
| 970 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 971 | organization, organizationalUnit, state, surname, |
| 972 | unstructuredName); |
| 973 | EXPECT_FALSE(fs::exists(CSRPath)); |
| 974 | EXPECT_FALSE(fs::exists(privateKeyPath)); |
| 975 | } |
| 976 | |
| 977 | /** @brief Check error is thrown if generated rsa key file is not present |
| 978 | */ |
| 979 | TEST_F(TestCertificates, TestRSAKeyFileNotPresentCase) |
| 980 | { |
| 981 | std::string endpoint("https"); |
| 982 | std::string unit(""); |
| 983 | std::string type("Server"); |
| 984 | std::string installPath(certDir + "/" + certificateFile); |
| 985 | std::string verifyPath(installPath); |
| 986 | std::string CSRPath(certDir + "/" + CSRFile); |
| 987 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 988 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 989 | std::string challengePassword("Password"); |
| 990 | std::string city("BLR"); |
| 991 | std::string commonName("abc.com"); |
| 992 | std::string contactPerson("Admin"); |
| 993 | std::string country("IN"); |
| 994 | std::string email("admin@in.ibm.com"); |
| 995 | std::string givenName("givenName"); |
| 996 | std::string initials("G"); |
| 997 | int64_t keyBitLength(2048); |
| 998 | std::string keyCurveId("secp521r1"); |
| 999 | std::string keyPairAlgorithm("RSA"); |
| 1000 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 1001 | std::string organization("IBM"); |
| 1002 | std::string organizationalUnit("orgUnit"); |
| 1003 | std::string state("TS"); |
| 1004 | std::string surname("surname"); |
| 1005 | std::string unstructuredName("unstructuredName"); |
| 1006 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 1007 | auto event = sdeventplus::Event::get_default(); |
| 1008 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 1009 | std::move(installPath)); |
| 1010 | |
| 1011 | // Removing generated RSA key file |
| 1012 | fs::remove(rsaPrivateKeyFilePath); |
| 1013 | |
| 1014 | Status status; |
| 1015 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 1016 | MainApp mainApp(&manager, &csr); |
| 1017 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 1018 | contactPerson, country, email, givenName, initials, |
| 1019 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 1020 | organization, organizationalUnit, state, surname, |
| 1021 | unstructuredName); |
| 1022 | EXPECT_FALSE(fs::exists(CSRPath)); |
| 1023 | EXPECT_FALSE(fs::exists(privateKeyPath)); |
| 1024 | } |
| 1025 | |
| 1026 | /** @brief Check private key file is created from generated rsa key file is |
| 1027 | * `present |
| 1028 | */ |
| 1029 | TEST_F(TestCertificates, TestRSAKeyFromRSAKeyFileIsWrittenIntoPrivateKeyFile) |
| 1030 | { |
| 1031 | std::string endpoint("https"); |
| 1032 | std::string unit(""); |
| 1033 | std::string type("Server"); |
| 1034 | std::string installPath(certDir + "/" + certificateFile); |
| 1035 | std::string verifyPath(installPath); |
| 1036 | std::string CSRPath(certDir + "/" + CSRFile); |
| 1037 | std::string privateKeyPath(certDir + "/" + privateKeyFile); |
| 1038 | std::vector<std::string> alternativeNames{"localhost1", "localhost2"}; |
| 1039 | std::string challengePassword("Password"); |
| 1040 | std::string city("BLR"); |
| 1041 | std::string commonName("abc.com"); |
| 1042 | std::string contactPerson("Admin"); |
| 1043 | std::string country("IN"); |
| 1044 | std::string email("admin@in.ibm.com"); |
| 1045 | std::string givenName("givenName"); |
| 1046 | std::string initials("G"); |
| 1047 | int64_t keyBitLength(2048); |
| 1048 | std::string keyCurveId("secp521r1"); |
| 1049 | std::string keyPairAlgorithm("RSA"); |
| 1050 | std::vector<std::string> keyUsage{"serverAuth", "clientAuth"}; |
| 1051 | std::string organization("IBM"); |
| 1052 | std::string organizationalUnit("orgUnit"); |
| 1053 | std::string state("TS"); |
| 1054 | std::string surname("surname"); |
| 1055 | std::string unstructuredName("unstructuredName"); |
| 1056 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 1057 | auto event = sdeventplus::Event::get_default(); |
| 1058 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 1059 | std::move(installPath)); |
| 1060 | Status status; |
| 1061 | CSR csr(bus, objPath.c_str(), CSRPath.c_str(), status); |
| 1062 | MainApp mainApp(&manager, &csr); |
| 1063 | mainApp.generateCSR(alternativeNames, challengePassword, city, commonName, |
| 1064 | contactPerson, country, email, givenName, initials, |
| 1065 | keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage, |
| 1066 | organization, organizationalUnit, state, surname, |
| 1067 | unstructuredName); |
| 1068 | sleep(10); |
| 1069 | EXPECT_TRUE(fs::exists(CSRPath)); |
| 1070 | EXPECT_TRUE(fs::exists(privateKeyPath)); |
| 1071 | } |
| 1072 | |
| 1073 | /** @brief Check RSA key is generted during application startup*/ |
| 1074 | TEST_F(TestCertificates, TestGenerateRSAPrivateKeyFile) |
| 1075 | { |
| 1076 | std::string endpoint("https"); |
| 1077 | std::string unit(""); |
| 1078 | std::string type("Server"); |
| 1079 | std::string installPath(certDir + "/" + certificateFile); |
| 1080 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 1081 | auto event = sdeventplus::Event::get_default(); |
| 1082 | |
| 1083 | EXPECT_FALSE(fs::exists(rsaPrivateKeyFilePath)); |
| 1084 | Manager manager(bus, event, objPath.c_str(), type, std::move(unit), |
| 1085 | std::move(installPath)); |
| 1086 | EXPECT_TRUE(fs::exists(rsaPrivateKeyFilePath)); |
| 1087 | } |