forked from ibm-openbmc/phosphor-certificate-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certs_manager.cpp
1129 lines (1029 loc) · 36.3 KB
/
certs_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "config.h"
#include "certs_manager.hpp"
#include "x509_utils.hpp"
#include <openssl/asn1.h>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/evp.h>
#include <openssl/obj_mac.h>
#include <openssl/objects.h>
#include <openssl/opensslv.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <unistd.h>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/exception.hpp>
#include <sdbusplus/message.hpp>
#include <sdeventplus/source/base.hpp>
#include <sdeventplus/source/child.hpp>
#include <xyz/openbmc_project/Certs/error.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <algorithm>
#include <array>
#include <cerrno>
#include <chrono>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <fstream>
#include <utility>
namespace phosphor::certs
{
namespace
{
namespace fs = std::filesystem;
using ::phosphor::logging::commit;
using ::phosphor::logging::elog;
using ::phosphor::logging::report;
using ::sdbusplus::xyz::openbmc_project::Certs::Error::InvalidCertificate;
using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
using ::sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
using NotAllowedReason =
::phosphor::logging::xyz::openbmc_project::Common::NotAllowed::REASON;
using InvalidCertificateReason = ::phosphor::logging::xyz::openbmc_project::
Certs::InvalidCertificate::REASON;
using ::sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
using Argument =
::phosphor::logging::xyz::openbmc_project::Common::InvalidArgument;
// RAII support for openSSL functions.
using X509ReqPtr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>;
using EVPPkeyPtr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>;
using BignumPtr = std::unique_ptr<BIGNUM, decltype(&::BN_free)>;
using X509StorePtr = std::unique_ptr<X509_STORE, decltype(&::X509_STORE_free)>;
constexpr int supportedKeyBitLength = 2048;
constexpr int defaultKeyBitLength = 2048;
// secp224r1 is equal to RSA 2048 KeyBitLength. Refer RFC 5349
constexpr auto defaultKeyCurveID = "secp224r1";
// PEM certificate block markers, defined in go/rfc/7468.
constexpr std::string_view beginCertificate = "-----BEGIN CERTIFICATE-----";
constexpr std::string_view endCertificate = "-----END CERTIFICATE-----";
/**
* @brief Splits the given authorities list file and returns an array of
* individual PEM encoded x509 certificate.
*
* @param[in] sourceFilePath - Path to the authorities list file.
*
* @return An array of individual PEM encoded x509 certificate
*/
std::vector<std::string> splitCertificates(const std::string& sourceFilePath)
{
std::ifstream inputCertFileStream;
inputCertFileStream.exceptions(
std::ifstream::failbit | std::ifstream::badbit | std::ifstream::eofbit);
std::stringstream pemStream;
std::vector<std::string> certificatesList;
try
{
inputCertFileStream.open(sourceFilePath);
pemStream << inputCertFileStream.rdbuf();
inputCertFileStream.close();
}
catch (const std::exception& e)
{
lg2::error("Failed to read certificates list, ERR:{ERR}, SRC:{SRC}",
"ERR", e, "SRC", sourceFilePath);
elog<InternalFailure>();
}
std::string pem = pemStream.str();
size_t begin = 0;
// |begin| points to the current start position for searching the next
// |beginCertificate| block. When we find the beginning of the certificate,
// we extract the content between the beginning and the end of the current
// certificate. And finally we move |begin| to the end of the current
// certificate to start searching the next potential certificate.
for (begin = pem.find(beginCertificate, begin); begin != std::string::npos;
begin = pem.find(beginCertificate, begin))
{
size_t end = pem.find(endCertificate, begin);
if (end == std::string::npos)
{
lg2::error(
"invalid PEM contains a BEGIN identifier without an END");
elog<InvalidCertificate>(InvalidCertificateReason(
"invalid PEM contains a BEGIN identifier without an END"));
}
end += endCertificate.size();
certificatesList.emplace_back(pem.substr(begin, end - begin));
begin = end;
}
return certificatesList;
}
} // namespace
Manager::Manager(sdbusplus::bus_t& bus, sdeventplus::Event& event,
const char* path, CertificateType type,
const std::string& unit, const std::string& installPath) :
internal::ManagerInterface(bus, path),
bus(bus), event(event), objectPath(path), certType(type),
unitToRestart(std::move(unit)), certInstallPath(std::move(installPath)),
certParentInstallPath(fs::path(certInstallPath).parent_path())
{
try
{
// Create certificate directory if not existing.
// Set correct certificate directory permissions.
fs::path certDirectory;
try
{
if (certType == CertificateType::authority)
{
certDirectory = certInstallPath;
}
else
{
certDirectory = certParentInstallPath;
}
if (!fs::exists(certDirectory))
{
fs::create_directories(certDirectory);
}
auto permission = fs::perms::owner_read | fs::perms::owner_write |
fs::perms::owner_exec;
fs::permissions(certDirectory, permission,
fs::perm_options::replace);
storageUpdate();
}
catch (const fs::filesystem_error& e)
{
lg2::error(
"Failed to create directory, ERR:{ERR}, DIRECTORY:{DIRECTORY}",
"ERR", e, "DIRECTORY", certParentInstallPath);
report<InternalFailure>();
}
// Generating RSA private key file if certificate type is server/client
if (certType != CertificateType::authority)
{
createRSAPrivateKeyFile();
}
// restore any existing certificates
createCertificates();
// watch is not required for authority certificates
if (certType != CertificateType::authority)
{
// watch for certificate file create/replace
certWatchPtr = std::make_unique<Watch>(event, certInstallPath,
[this]() {
try
{
// if certificate file existing update it
if (!installedCerts.empty())
{
lg2::info("Inotify callback to update "
"certificate properties");
installedCerts[0]->populateProperties();
}
else
{
lg2::info(
"Inotify callback to create certificate object");
createCertificates();
}
}
catch (const InternalFailure& e)
{
commit<InternalFailure>();
}
catch (const InvalidCertificate& e)
{
commit<InvalidCertificate>();
}
});
}
else
{
try
{
const std::string singleCertPath = "/etc/ssl/certs/Root-CA.pem";
if (fs::exists(singleCertPath) && !fs::is_empty(singleCertPath))
{
lg2::notice(
"Legacy certificate detected, will be installed from,"
"SINGLE_CERTPATH:{SINGLE_CERTPATH}",
"SINGLE_CERTPATH", singleCertPath);
install(singleCertPath);
if (!fs::remove(singleCertPath))
{
lg2::error("Unable to remove old certificate from,"
"SINGLE_CERTPATH:{SINGLE_CERTPATH}",
"SINGLE_CERTPATH", singleCertPath);
elog<InternalFailure>();
}
}
}
catch (const std::exception& ex)
{
lg2::error(
"Error in restoring legacy certificate, ERROR_STR:{ERROR_STR}",
"ERROR_STR", ex);
}
}
}
catch (const std::exception& ex)
{
lg2::error(
"Error in certificate manager constructor, ERROR_STR:{ERROR_STR}",
"ERROR_STR", ex);
}
}
std::string Manager::install(const std::string filePath)
{
if (certType != CertificateType::authority && !installedCerts.empty())
{
elog<NotAllowed>(NotAllowedReason("Certificate already exist"));
}
else if (certType == CertificateType::authority &&
installedCerts.size() >= maxNumAuthorityCertificates)
{
elog<NotAllowed>(NotAllowedReason("Certificates limit reached"));
}
std::string certObjectPath;
if (isCertificateUnique(filePath))
{
certObjectPath = objectPath + '/' + std::to_string(certIdCounter);
installedCerts.emplace_back(std::make_unique<Certificate>(
bus, certObjectPath, certType, certInstallPath, filePath,
certWatchPtr.get(), *this, /*restore=*/false));
reloadOrReset(unitToRestart);
certIdCounter++;
}
else
{
elog<NotAllowed>(NotAllowedReason("Certificate already exist"));
}
return certObjectPath;
}
std::vector<sdbusplus::message::object_path>
Manager::installAll(const std::string filePath)
{
if (certType != CertificateType::authority)
{
elog<NotAllowed>(
NotAllowedReason("The InstallAll interface is only allowed for "
"Authority certificates"));
}
if (!installedCerts.empty())
{
elog<NotAllowed>(NotAllowedReason(
"There are already root certificates; Call DeleteAll then "
"InstallAll, or use ReplaceAll"));
}
fs::path sourceFile(filePath);
if (!fs::exists(sourceFile))
{
lg2::error("File is Missing, FILE:{FILE}", "FILE", filePath);
elog<InternalFailure>();
}
std::vector<std::string> authorities = splitCertificates(sourceFile);
if (authorities.size() > maxNumAuthorityCertificates)
{
elog<NotAllowed>(NotAllowedReason("Certificates limit reached"));
}
lg2::info("Starts authority list install");
fs::path authorityStore(certInstallPath);
fs::path authoritiesListFile = authorityStore /
defaultAuthoritiesListFileName;
// Atomically install all the certificates
fs::path tempPath = Certificate::generateUniqueFilePath(authorityStore);
fs::create_directory(tempPath);
// Copies the authorities list
Certificate::copyCertificate(sourceFile,
tempPath / defaultAuthoritiesListFileName);
std::vector<std::unique_ptr<Certificate>> tempCertificates;
uint64_t tempCertIdCounter = certIdCounter;
X509StorePtr x509Store = getX509Store(sourceFile);
for (const auto& authority : authorities)
{
std::string certObjectPath = objectPath + '/' +
std::to_string(tempCertIdCounter);
tempCertificates.emplace_back(std::make_unique<Certificate>(
bus, certObjectPath, certType, tempPath, *x509Store, authority,
certWatchPtr.get(), *this, /*restore=*/false));
tempCertIdCounter++;
}
// We are good now, issue swap
installedCerts = std::move(tempCertificates);
certIdCounter = tempCertIdCounter;
// Rename all the certificates including the authorities list
for (const fs::path& f : fs::directory_iterator(tempPath))
{
if (fs::is_symlink(f))
{
continue;
}
fs::rename(/*from=*/f, /*to=*/certInstallPath / f.filename());
}
// Update file locations and create symbol links
for (const auto& cert : installedCerts)
{
cert->setCertInstallPath(certInstallPath);
cert->setCertFilePath(certInstallPath /
fs::path(cert->getCertFilePath()).filename());
cert->storageUpdate();
}
// Remove the temporary folder
fs::remove_all(tempPath);
std::vector<sdbusplus::message::object_path> objects;
for (const auto& certificate : installedCerts)
{
objects.emplace_back(certificate->getObjectPath());
}
lg2::info("Finishes authority list install; reload units starts");
reloadOrReset(unitToRestart);
return objects;
}
std::vector<sdbusplus::message::object_path>
Manager::replaceAll(std::string filePath)
{
installedCerts.clear();
certIdCounter = 1;
storageUpdate();
return installAll(std::move(filePath));
}
void Manager::deleteAll()
{
// TODO: #Issue 4 when a certificate is deleted system auto generates
// certificate file. At present we are not supporting creation of
// certificate object for the auto-generated certificate file as
// deletion if only applicable for REST server and Bmcweb does not allow
// deletion of certificates
installedCerts.clear();
// If the authorities list exists, delete it as well
if (certType == CertificateType::authority)
{
if (fs::path authoritiesList = fs::path(certInstallPath) /
defaultAuthoritiesListFileName;
fs::exists(authoritiesList))
{
fs::remove(authoritiesList);
}
}
certIdCounter = 1;
storageUpdate();
reloadOrReset(unitToRestart);
}
void Manager::deleteCertificate(const Certificate* const certificate)
{
const std::vector<std::unique_ptr<Certificate>>::iterator& certIt =
std::find_if(installedCerts.begin(), installedCerts.end(),
[certificate](const std::unique_ptr<Certificate>& cert) {
return (cert.get() == certificate);
});
if (certIt != installedCerts.end())
{
installedCerts.erase(certIt);
storageUpdate();
reloadOrReset(unitToRestart);
}
else
{
lg2::error("Certificate does not exist, ID:{ID}", "ID",
certificate->getCertId());
elog<InternalFailure>();
}
}
void Manager::replaceCertificate(Certificate* const certificate,
const std::string& filePath)
{
if (isCertificateUnique(filePath, certificate))
{
certificate->install(filePath, false);
storageUpdate();
reloadOrReset(unitToRestart);
}
else
{
elog<NotAllowed>(NotAllowedReason("Certificate already exist"));
}
}
std::string Manager::generateCSR(
std::vector<std::string> alternativeNames, std::string challengePassword,
std::string city, std::string commonName, std::string contactPerson,
std::string country, std::string email, std::string givenName,
std::string initials, int64_t keyBitLength, std::string keyCurveId,
std::string keyPairAlgorithm, std::vector<std::string> keyUsage,
std::string organization, std::string organizationalUnit, std::string state,
std::string surname, std::string unstructuredName)
{
// We support only one CSR.
csrPtr.reset(nullptr);
auto pid = fork();
if (pid == -1)
{
lg2::error("Error occurred during forking process");
report<InternalFailure>();
}
else if (pid == 0)
{
try
{
generateCSRHelper(alternativeNames, challengePassword, city,
commonName, contactPerson, country, email,
givenName, initials, keyBitLength, keyCurveId,
keyPairAlgorithm, keyUsage, organization,
organizationalUnit, state, surname,
unstructuredName);
exit(EXIT_SUCCESS);
}
catch (const InternalFailure& e)
{
// commit the error reported in child process and exit
// Callback method from SDEvent Loop looks for exit status
exit(EXIT_FAILURE);
commit<InternalFailure>();
}
catch (const InvalidArgument& e)
{
// commit the error reported in child process and exit
// Callback method from SDEvent Loop looks for exit status
exit(EXIT_FAILURE);
commit<InvalidArgument>();
}
}
else
{
using namespace sdeventplus::source;
Child::Callback callback = [this](Child& eventSource,
const siginfo_t* si) {
eventSource.set_enabled(Enabled::On);
if (si->si_status != 0)
{
this->createCSRObject(Status::failure);
}
else
{
this->createCSRObject(Status::success);
}
};
try
{
sigset_t ss;
if (sigemptyset(&ss) < 0)
{
lg2::error("Unable to initialize signal set");
elog<InternalFailure>();
}
if (sigaddset(&ss, SIGCHLD) < 0)
{
lg2::error("Unable to add signal to signal set");
elog<InternalFailure>();
}
// Block SIGCHLD first, so that the event loop can handle it
if (sigprocmask(SIG_BLOCK, &ss, nullptr) < 0)
{
lg2::error("Unable to block signal");
elog<InternalFailure>();
}
if (childPtr)
{
childPtr.reset();
}
childPtr = std::make_unique<Child>(event, pid, WEXITED | WSTOPPED,
std::move(callback));
}
catch (const InternalFailure& e)
{
commit<InternalFailure>();
}
}
auto csrObjectPath = objectPath + '/' + "csr";
return csrObjectPath;
}
std::vector<std::unique_ptr<Certificate>>& Manager::getCertificates()
{
return installedCerts;
}
void Manager::generateCSRHelper(
std::vector<std::string> alternativeNames, std::string challengePassword,
std::string city, std::string commonName, std::string contactPerson,
std::string country, std::string email, std::string givenName,
std::string initials, int64_t keyBitLength, std::string keyCurveId,
std::string keyPairAlgorithm, std::vector<std::string> keyUsage,
std::string organization, std::string organizationalUnit, std::string state,
std::string surname, std::string unstructuredName)
{
int ret = 0;
// set version of x509 req
int nVersion = 1;
X509ReqPtr x509Req(X509_REQ_new(), ::X509_REQ_free);
ret = X509_REQ_set_version(x509Req.get(), nVersion);
if (ret == 0)
{
lg2::error("Error occurred during X509_REQ_set_version call");
elog<InternalFailure>();
}
// set subject of x509 req
X509_NAME* x509Name = X509_REQ_get_subject_name(x509Req.get());
if (!alternativeNames.empty())
{
for (auto& name : alternativeNames)
{
addEntry(x509Name, "subjectAltName", name);
}
}
addEntry(x509Name, "challengePassword", challengePassword);
addEntry(x509Name, "L", city);
addEntry(x509Name, "CN", commonName);
addEntry(x509Name, "name", contactPerson);
addEntry(x509Name, "C", country);
addEntry(x509Name, "emailAddress", email);
addEntry(x509Name, "GN", givenName);
addEntry(x509Name, "initials", initials);
addEntry(x509Name, "algorithm", keyPairAlgorithm);
if (!keyUsage.empty())
{
for (auto& usage : keyUsage)
{
if (isExtendedKeyUsage(usage))
{
addEntry(x509Name, "extendedKeyUsage", usage);
}
else
{
addEntry(x509Name, "keyUsage", usage);
}
}
}
addEntry(x509Name, "O", organization);
addEntry(x509Name, "OU", organizationalUnit);
addEntry(x509Name, "ST", state);
addEntry(x509Name, "SN", surname);
addEntry(x509Name, "unstructuredName", unstructuredName);
EVPPkeyPtr pKey(nullptr, ::EVP_PKEY_free);
lg2::info("Given Key pair algorithm, KEYPAIRALGORITHM:{KEYPAIRALGORITHM}",
"KEYPAIRALGORITHM", keyPairAlgorithm);
// Used EC algorithm as default if user did not give algorithm type.
if (keyPairAlgorithm == "RSA")
pKey = getRSAKeyPair(keyBitLength);
else if ((keyPairAlgorithm == "EC") || (keyPairAlgorithm.empty()))
pKey = generateECKeyPair(keyCurveId);
else
{
lg2::error("Given Key pair algorithm is not supported. Supporting "
"RSA and EC only");
elog<InvalidArgument>(
Argument::ARGUMENT_NAME("KEYPAIRALGORITHM"),
Argument::ARGUMENT_VALUE(keyPairAlgorithm.c_str()));
}
ret = X509_REQ_set_pubkey(x509Req.get(), pKey.get());
if (ret == 0)
{
lg2::error("Error occurred while setting Public key");
elog<InternalFailure>();
}
// Write private key to file
writePrivateKey(pKey, defaultPrivateKeyFileName);
// set sign key of x509 req
ret = X509_REQ_sign(x509Req.get(), pKey.get(), EVP_sha256());
if (ret == 0)
{
lg2::error("Error occurred while signing key of x509");
elog<InternalFailure>();
}
lg2::info("Writing CSR to file");
fs::path csrFilePath = certParentInstallPath / defaultCSRFileName;
writeCSR(csrFilePath.string(), x509Req);
}
bool Manager::isExtendedKeyUsage(const std::string& usage)
{
const static std::array<const char*, 6> usageList = {
"ServerAuthentication", "ClientAuthentication", "OCSPSigning",
"Timestamping", "CodeSigning", "EmailProtection"};
auto it = std::find_if(
usageList.begin(), usageList.end(),
[&usage](const char* s) { return (strcmp(s, usage.c_str()) == 0); });
return it != usageList.end();
}
EVPPkeyPtr Manager::generateRSAKeyPair(const int64_t keyBitLength)
{
int64_t keyBitLen = keyBitLength;
// set keybit length to default value if not set
if (keyBitLen <= 0)
{
lg2::info("KeyBitLength is not given.Hence, using default KeyBitLength:"
"{DEFAULTKEYBITLENGTH}",
"DEFAULTKEYBITLENGTH", defaultKeyBitLength);
keyBitLen = defaultKeyBitLength;
}
#if (OPENSSL_VERSION_NUMBER < 0x30000000L)
// generate rsa key
BignumPtr bne(BN_new(), ::BN_free);
auto ret = BN_set_word(bne.get(), RSA_F4);
if (ret == 0)
{
lg2::error("Error occurred during BN_set_word call");
elog<InternalFailure>();
}
using RSAPtr = std::unique_ptr<RSA, decltype(&::RSA_free)>;
RSAPtr rsa(RSA_new(), ::RSA_free);
ret = RSA_generate_key_ex(rsa.get(), keyBitLen, bne.get(), nullptr);
if (ret != 1)
{
lg2::error(
"Error occurred during RSA_generate_key_ex call: {KEYBITLENGTH}",
"KEYBITLENGTH", keyBitLen);
elog<InternalFailure>();
}
// set public key of x509 req
EVPPkeyPtr pKey(EVP_PKEY_new(), ::EVP_PKEY_free);
ret = EVP_PKEY_assign_RSA(pKey.get(), rsa.get());
if (ret == 0)
{
lg2::error("Error occurred during assign rsa key into EVP");
elog<InternalFailure>();
}
// Now |rsa| is managed by |pKey|
rsa.release();
return pKey;
#else
auto ctx = std::unique_ptr<EVP_PKEY_CTX, decltype(&::EVP_PKEY_CTX_free)>(
EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr), &::EVP_PKEY_CTX_free);
if (!ctx)
{
lg2::error("Error occurred creating EVP_PKEY_CTX from algorithm");
elog<InternalFailure>();
}
if ((EVP_PKEY_keygen_init(ctx.get()) <= 0) ||
(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx.get(), keyBitLen) <= 0))
{
lg2::error("Error occurred initializing keygen context");
elog<InternalFailure>();
}
EVP_PKEY* pKey = nullptr;
if (EVP_PKEY_keygen(ctx.get(), &pKey) <= 0)
{
lg2::error("Error occurred during generate EC key");
elog<InternalFailure>();
}
return {pKey, &::EVP_PKEY_free};
#endif
}
EVPPkeyPtr Manager::generateECKeyPair(const std::string& curveId)
{
std::string curId(curveId);
if (curId.empty())
{
lg2::info("KeyCurveId is not given. Hence using default curve id,"
"DEFAULTKEYCURVEID:{DEFAULTKEYCURVEID}",
"DEFAULTKEYCURVEID", defaultKeyCurveID);
curId = defaultKeyCurveID;
}
int ecGrp = OBJ_txt2nid(curId.c_str());
if (ecGrp == NID_undef)
{
lg2::error(
"Error occurred during convert the curve id string format into NID,"
"KEYCURVEID:{KEYCURVEID}",
"KEYCURVEID", curId);
elog<InternalFailure>();
}
#if (OPENSSL_VERSION_NUMBER < 0x30000000L)
EC_KEY* ecKey = EC_KEY_new_by_curve_name(ecGrp);
if (ecKey == nullptr)
{
lg2::error(
"Error occurred during create the EC_Key object from NID, ECGROUP:{ECGROUP}",
"ECGROUP", ecGrp);
elog<InternalFailure>();
}
// If you want to save a key and later load it with
// SSL_CTX_use_PrivateKey_file, then you must set the OPENSSL_EC_NAMED_CURVE
// flag on the key.
EC_KEY_set_asn1_flag(ecKey, OPENSSL_EC_NAMED_CURVE);
int ret = EC_KEY_generate_key(ecKey);
if (ret == 0)
{
EC_KEY_free(ecKey);
lg2::error("Error occurred during generate EC key");
elog<InternalFailure>();
}
EVPPkeyPtr pKey(EVP_PKEY_new(), ::EVP_PKEY_free);
ret = EVP_PKEY_assign_EC_KEY(pKey.get(), ecKey);
if (ret == 0)
{
EC_KEY_free(ecKey);
lg2::error("Error occurred during assign EC Key into EVP");
elog<InternalFailure>();
}
return pKey;
#else
auto holderOfKey = [](EVP_PKEY* key) {
return std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>{
key, &::EVP_PKEY_free};
};
// Create context to set up curve parameters.
auto ctx = std::unique_ptr<EVP_PKEY_CTX, decltype(&::EVP_PKEY_CTX_free)>(
EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr), &::EVP_PKEY_CTX_free);
if (!ctx)
{
lg2::error("Error occurred creating EVP_PKEY_CTX for params");
elog<InternalFailure>();
}
// Set up curve parameters.
EVP_PKEY* params = nullptr;
if ((EVP_PKEY_paramgen_init(ctx.get()) <= 0) ||
(EVP_PKEY_CTX_set_ec_param_enc(ctx.get(), OPENSSL_EC_NAMED_CURVE) <=
0) ||
(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx.get(), ecGrp) <= 0) ||
(EVP_PKEY_paramgen(ctx.get(), ¶ms) <= 0))
{
lg2::error("Error occurred setting curve parameters");
elog<InternalFailure>();
}
// Move parameters to RAII holder.
auto pparms = holderOfKey(params);
// Create new context for key.
ctx.reset(EVP_PKEY_CTX_new_from_pkey(nullptr, params, nullptr));
if (!ctx || (EVP_PKEY_keygen_init(ctx.get()) <= 0))
{
lg2::error("Error occurred initializing keygen context");
elog<InternalFailure>();
}
EVP_PKEY* pKey = nullptr;
if (EVP_PKEY_keygen(ctx.get(), &pKey) <= 0)
{
lg2::error("Error occurred during generate EC key");
elog<InternalFailure>();
}
return holderOfKey(pKey);
#endif
}
void Manager::writePrivateKey(const EVPPkeyPtr& pKey,
const std::string& privKeyFileName)
{
lg2::info("Writing private key to file");
// write private key to file
fs::path privKeyPath = certParentInstallPath / privKeyFileName;
FILE* fp = std::fopen(privKeyPath.c_str(), "w");
if (fp == nullptr)
{
lg2::error("Error occurred creating private key file");
elog<InternalFailure>();
}
int ret = PEM_write_PrivateKey(fp, pKey.get(), nullptr, nullptr, 0, 0,
nullptr);
std::fclose(fp);
if (ret == 0)
{
lg2::error("Error occurred while writing private key to file");
elog<InternalFailure>();
}
}
void Manager::addEntry(X509_NAME* x509Name, const char* field,
const std::string& bytes)
{
if (bytes.empty())
{
return;
}
int ret = X509_NAME_add_entry_by_txt(
x509Name, field, MBSTRING_ASC,
reinterpret_cast<const unsigned char*>(bytes.c_str()), -1, -1, 0);
if (ret != 1)
{
lg2::error("Unable to set entry, FIELD:{FIELD}, VALUE:{VALUE}", "FIELD",
field, "VALUE", bytes);
elog<InternalFailure>();
}
}
void Manager::createCSRObject(const Status& status)
{
if (csrPtr)
{
csrPtr.reset(nullptr);
}
auto csrObjectPath = objectPath + '/' + "csr";
csrPtr = std::make_unique<CSR>(bus, csrObjectPath.c_str(),
certInstallPath.c_str(), status);
}
void Manager::writeCSR(const std::string& filePath, const X509ReqPtr& x509Req)
{
if (fs::exists(filePath))
{
lg2::info("Removing the existing file, FILENAME:{FILENAME}", "FILENAME",
filePath);
if (!fs::remove(filePath.c_str()))
{
lg2::error("Unable to remove the file, FILENAME:{FILENAME}",
"FILENAME", filePath);
elog<InternalFailure>();
}
}
FILE* fp = nullptr;
if ((fp = std::fopen(filePath.c_str(), "w")) == nullptr)
{
lg2::error(
"Error opening the file to write the CSR, FILENAME:{FILENAME}",
"FILENAME", filePath);
elog<InternalFailure>();
}
int rc = PEM_write_X509_REQ(fp, x509Req.get());
if (!rc)
{
lg2::error("PEM write routine failed, FILENAME:{FILENAME}", "FILENAME",
filePath);
std::fclose(fp);
elog<InternalFailure>();
}
std::fclose(fp);
}
void Manager::createCertificates()
{
auto certObjectPath = objectPath + '/';
if (certType == CertificateType::authority)
{
// Check whether install path is a directory.
if (!fs::is_directory(certInstallPath))
{
lg2::error("Certificate installation path exists and it is "
"not a directory");
elog<InternalFailure>();
}
// If the authorities list exists, recover from it and return
if (fs::path authoritiesListFilePath = fs::path(certInstallPath) /
defaultAuthoritiesListFileName;
fs::exists(authoritiesListFilePath))
{
// remove all other files and directories
for (auto& path : fs::directory_iterator(certInstallPath))
{
if (path.path() != authoritiesListFilePath)
{
fs::remove_all(path);
}
}
installAll(authoritiesListFilePath);
return;
}
for (auto& path : fs::directory_iterator(certInstallPath))
{
try
{
// Assume here any regular file located in certificate directory
// contains certificates body. Do not want to use soft links
// would add value.
if (fs::is_regular_file(path))
{
installedCerts.emplace_back(std::make_unique<Certificate>(
bus, certObjectPath + std::to_string(certIdCounter++),
certType, certInstallPath, path.path(),
certWatchPtr.get(), *this, /*restore=*/true));
}
}
catch (const InternalFailure& e)
{
report<InternalFailure>();
}
catch (const InvalidCertificate& e)
{
report<InvalidCertificate>(InvalidCertificateReason(
"Existing certificate file is corrupted"));
}
}
}
else if (fs::exists(certInstallPath))
{
try
{
installedCerts.emplace_back(std::make_unique<Certificate>(
bus, certObjectPath + '1', certType, certInstallPath,
certInstallPath, certWatchPtr.get(), *this, /*restore=*/false));
}
catch (const InternalFailure& e)
{
report<InternalFailure>();
}
catch (const InvalidCertificate& e)
{
report<InvalidCertificate>(InvalidCertificateReason(
"Existing certificate file is corrupted"));
}
}
}
void Manager::createRSAPrivateKeyFile()
{
fs::path rsaPrivateKeyFileName = certParentInstallPath /
defaultRSAPrivateKeyFileName;