@@ -478,7 +478,7 @@ std::shared_ptr<X509> GetX509Certificate(const String& pemfile)
478
478
return std::shared_ptr<X509>(cert, X509_free);
479
479
}
480
480
481
- int MakeX509CSR (const String& cn, const String& keyfile, const String& csrfile, const String& certfile, bool ca)
481
+ int MakeX509CSR (const String& cn, const String& keyfile, const String& csrfile, const String& certfile, int validFor, bool ca)
482
482
{
483
483
char errbuf[256 ];
484
484
@@ -547,7 +547,7 @@ int MakeX509CSR(const String& cn, const String& keyfile, const String& csrfile,
547
547
X509_NAME *subject = X509_NAME_new ();
548
548
X509_NAME_add_entry_by_txt (subject, " CN" , MBSTRING_ASC, (unsigned char *)cn.CStr (), -1 , -1 , 0 );
549
549
550
- std::shared_ptr<X509> cert = CreateCert (key, subject, subject, key, ca);
550
+ std::shared_ptr<X509> cert = CreateCert (key, subject, subject, key, validFor, ca);
551
551
552
552
X509_NAME_free (subject);
553
553
@@ -640,12 +640,16 @@ int MakeX509CSR(const String& cn, const String& keyfile, const String& csrfile,
640
640
return 1 ;
641
641
}
642
642
643
- std::shared_ptr<X509> CreateCert (EVP_PKEY *pubkey, X509_NAME *subject, X509_NAME *issuer, EVP_PKEY *cakey, bool ca)
643
+ std::shared_ptr<X509> CreateCert (EVP_PKEY *pubkey, X509_NAME *subject, X509_NAME *issuer, EVP_PKEY *cakey, int validFor, bool ca)
644
644
{
645
645
X509 *cert = X509_new ();
646
646
X509_set_version (cert, 2 );
647
- X509_gmtime_adj (X509_get_notBefore (cert), 0 );
648
- X509_gmtime_adj (X509_get_notAfter (cert), ca ? ROOT_VALID_FOR : LEAF_VALID_FOR);
647
+ auto notBefore = 0 ;
648
+ if (validFor < 0 ) {
649
+ notBefore = validFor - validFor / 4 ; // Add 25% of the validity period to the past
650
+ }
651
+ X509_gmtime_adj (X509_get_notBefore (cert), notBefore);
652
+ X509_gmtime_adj (X509_get_notAfter (cert), validFor);
649
653
X509_set_pubkey (cert, pubkey);
650
654
651
655
X509_set_subject_name (cert, subject);
@@ -728,7 +732,7 @@ String GetIcingaCADir()
728
732
return Configuration::DataDir + " /ca" ;
729
733
}
730
734
731
- std::shared_ptr<X509> CreateCertIcingaCA (EVP_PKEY *pubkey, X509_NAME *subject, bool ca)
735
+ std::shared_ptr<X509> CreateCertIcingaCA (EVP_PKEY *pubkey, X509_NAME *subject, int validFor, bool ca)
732
736
{
733
737
char errbuf[256 ];
734
738
@@ -765,13 +769,20 @@ std::shared_ptr<X509> CreateCertIcingaCA(EVP_PKEY *pubkey, X509_NAME *subject, b
765
769
EVP_PKEY *privkey = EVP_PKEY_new ();
766
770
EVP_PKEY_assign_RSA (privkey, rsa);
767
771
768
- return CreateCert (pubkey, subject, X509_get_subject_name (cacert.get ()), privkey, ca);
772
+ return CreateCert (pubkey, subject, X509_get_subject_name (cacert.get ()), privkey, validFor, ca);
769
773
}
770
774
771
- std::shared_ptr<X509> CreateCertIcingaCA (const std::shared_ptr<X509>& cert)
775
+ /* *
776
+ * Creates a new X509 certificate signed by the Icinga CA.
777
+ *
778
+ * @param cert The certificate containing the public key and subject name.
779
+ * @param validFor The validity period in seconds. Defaults to LEAF_VALID_FOR.
780
+ * @returns The new X509 certificate or an empty shared_ptr on error.
781
+ */
782
+ std::shared_ptr<X509> CreateCertIcingaCA (const std::shared_ptr<X509>& cert, int validFor)
772
783
{
773
784
std::shared_ptr<EVP_PKEY> pkey = std::shared_ptr<EVP_PKEY>(X509_get_pubkey (cert.get ()), EVP_PKEY_free);
774
- return CreateCertIcingaCA (pkey.get (), X509_get_subject_name (cert.get ()));
785
+ return CreateCertIcingaCA (pkey.get (), X509_get_subject_name (cert.get ()), validFor );
775
786
}
776
787
777
788
static inline
0 commit comments