Skip to content

Fix build with OpenSSL 4.0.0 - #3808

Open
bkuhls wants to merge 1 commit into
xmrig:masterfrom
bkuhls:openssl4
Open

bkuhls wants to merge 1 commit into
xmrig:masterfrom
bkuhls:openssl4

Conversation

@bkuhls

@bkuhls bkuhls commented May 2, 2026

Copy link
Copy Markdown
src/base/net/tls/TlsGen.cpp: In member function 'bool
 xmrig::TlsGen::generate_x509(const char*)':
src/base/net/tls/TlsGen.cpp:118:32: error: invalid conversion from
 'const X509_name_st*' to 'X509_NAME*' {aka 'X509_name_st*'}
 [-fpermissive]
  118 |     X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, reinterpret_cast<const uint8_t *>(commonName), -1, -1, 0);
      |                                ^~~~
      |                                |
      |                                const X509_name_st*

openssl/openssl#29117

src/base/net/tls/TlsGen.cpp: In member function 'bool
 xmrig::TlsGen::generate_x509(const char*)':
src/base/net/tls/TlsGen.cpp:118:32: error: invalid conversion from
 'const X509_name_st*' to 'X509_NAME*' {aka 'X509_name_st*'}
 [-fpermissive]
  118 |     X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, reinterpret_cast<const uint8_t *>(commonName), -1, -1, 0);
      |                                ^~~~
      |                                |
      |                                const X509_name_st*

openssl/openssl#29117

Signed-off-by: Bernd Kuhls <bernd@kuhls.net>
@loqs

loqs commented Jul 23, 2026

Copy link
Copy Markdown

@bkuhls instead of casting away the const have you considered using X509_NAME_new to create a X509_NAME *name without a const, use X509_set_subject_name before X509_set_issuer_name to populate it and X509_NAME_free afterwards to free the created name? This is the approach used in znc/znc@94bcf91 and avoids undefined behavior.

@SChernykh

Copy link
Copy Markdown
Contributor

The better fix (compared to what znc did) would be

auto name = X509_NAME_dup(X509_get_subject_name(cert));
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, reinterpret_cast<const uint8_t *>(commonName), -1, -1, 0);
X509_set_issuer_name(m_x509, name);
X509_NAME_free(name);

@SChernykh SChernykh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The better fix:

auto name = X509_NAME_dup(X509_get_subject_name(cert));
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, reinterpret_cast<const uint8_t *>(commonName), -1, -1, 0);
X509_set_issuer_name(m_x509, name);
X509_NAME_free(name);

@loqs

loqs commented Jul 24, 2026

Copy link
Copy Markdown

@SChernykh

--- a/src/base/net/tls/TlsGen.cpp
+++ b/src/base/net/tls/TlsGen.cpp
@@ -114,10 +114,11 @@ bool xmrig::TlsGen::generate_x509(const char *commonName)
     X509_gmtime_adj(X509_get_notBefore(m_x509), 0);
     X509_gmtime_adj(X509_get_notAfter(m_x509), 315360000L);
 
-    auto name = X509_get_subject_name(m_x509);
+    auto name = X509_NAME_dup(X509_get_subject_name(cert));
     X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, reinterpret_cast<const uint8_t *>(commonName), -1, -1, 0);
 
     X509_set_issuer_name(m_x509, name);
+    X509_NAME_free(name);
 
     return X509_sign(m_x509, m_pkey, EVP_sha256());
 }

Produces:

/build/xmrig/src/xmrig/src/base/net/tls/TlsGen.cpp: In member function ‘bool xmrig::TlsGen::generate_x509(const char*)’:
/build/xmrig/src/xmrig/src/base/net/tls/TlsGen.cpp:117:53: error: invalid use of non-static member function ‘const xmrig::String& xmrig::TlsGen::cert() const’
  117 |     auto name = X509_NAME_dup(X509_get_subject_name(cert));
      |                                                     ^~~~
In file included from /build/xmrig/src/xmrig/src/base/net/tls/TlsGen.cpp:19:
/build/xmrig/src/xmrig/src/base/net/tls/TlsGen.h:42:26: note: declared here
   42 |     inline const String &cert() const       { return m_cert; }
      |                          ^~~~
make[2]: *** [CMakeFiles/xmrig.dir/build.make:3327: CMakeFiles/xmrig.dir/src/base/net/tls/TlsGen.cpp.o] Error 1

@loqs

loqs commented Jul 24, 2026

Copy link
Copy Markdown

Did you mean:

--- a/src/base/net/tls/TlsGen.cpp
+++ b/src/base/net/tls/TlsGen.cpp
@@ -114,10 +114,11 @@ bool xmrig::TlsGen::generate_x509(const char *commonName)
     X509_gmtime_adj(X509_get_notBefore(m_x509), 0);
     X509_gmtime_adj(X509_get_notAfter(m_x509), 315360000L);
 
-    auto name = X509_get_subject_name(m_x509);
+    auto name = X509_NAME_dup(X509_get_subject_name(m_x509));
     X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, reinterpret_cast<const uint8_t *>(commonName), -1, -1, 0);
 
     X509_set_issuer_name(m_x509, name);
+    X509_NAME_free(name);
 
     return X509_sign(m_x509, m_pkey, EVP_sha256());
 }

@SChernykh

Copy link
Copy Markdown
Contributor

Yes, it's m_x509 there. In any case, X509_NAME_dup/X509_NAME_free is a proper solution here.

Comment on lines 117 to 121
auto name = X509_get_subject_name(m_x509);
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, reinterpret_cast<const uint8_t *>(commonName), -1, -1, 0);
X509_NAME_add_entry_by_txt((X509_NAME *) name, "CN", MBSTRING_ASC, reinterpret_cast<const uint8_t *>(commonName), -1, -1, 0);

X509_set_issuer_name(m_x509, name);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recommended fix is to allocate a new X509_NAME then to assign it to both the subject and issuer of the certificate (since duplication does not use the original subject name):

auto name = X509_NAME_new();
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, reinterpret_cast<const uint8_t *>(commonName), -1, -1, 0);

X509_set_subject_name(m_x509, name);
X509_set_issuer_name(m_x509, name);
X509_NAME_free(name);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants