Skip to content

Commit ac40805

Browse files
authored
feat: Added support for compiling with openssl v1 (#203)
1 parent e0d52c1 commit ac40805

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

CMakeLists.txt

+15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ option(PIPY_LTO "enable LTO" OFF)
2323
option(PIPY_USE_NTLS, "Use externally compiled TongSuo Crypto library instead of OpenSSL. Used with PIPY_OPENSSL" OFF)
2424
option(PIPY_USE_SYSTEM_ZLIB "Use system installed zlib" OFF)
2525
option(PIPY_USE_SYSTEM_OPENSSL "Use system installed OpenSSL" OFF)
26+
option(PIPY_USE_OPENSSL1, "Use openssl v1 when compiling pipy" OFF)
2627

2728
set(BUILD_SHARED_LIBS OFF)
2829
set(BUILD_TESTING OFF)
@@ -131,6 +132,10 @@ else()
131132
endif(PIPY_USE_SYSTEM_ZLIB)
132133
endif()
133134

135+
if(PIPY_USE_OPENSSL1)
136+
add_definitions(-DPIPY_USE_OPENSSL1)
137+
endif()
138+
134139
if(PIPY_USE_SYSTEM_OPENSSL)
135140
find_package(OpenSSL REQUIRED)
136141
if(OPENSSL_FOUND)
@@ -148,6 +153,16 @@ elseif(PIPY_OPENSSL)
148153
set(OPENSSL_LIB_DIR ${PIPY_OPENSSL}/lib)
149154

150155
else()
156+
if (DEFINED PIPY_USE_OPENSSL1)
157+
message(FATAL_ERROR
158+
"Error: OpenSSL v1 detected (PIPY_USE_OPENSSL1 is set).\n"
159+
"To compile with OpenSSL v1, you must set one of the following options:\n"
160+
" - PIPY_OPENSSL: Provide a custom OpenSSL installation path.\n"
161+
" - PIPY_USE_SYSTEM_OPENSSL: Use the system-installed OpenSSL library.\n"
162+
"Please configure CMake with the correct option and try again."
163+
)
164+
endif()
165+
151166
set(OPENSSL_SRC_DIR ${CMAKE_SOURCE_DIR}/deps/openssl-3.2.0)
152167
set(OPENSSL_LIB_DIR ${OPENSSL_SRC_DIR}/build)
153168
set(OPENSSL_INC_DIR ${OPENSSL_SRC_DIR}/include ${OPENSSL_LIB_DIR}/include)

src/api/crypto.cpp

+19-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,16 @@ PublicKey::PublicKey(pjs::Str *data) {
168168
}
169169

170170
PublicKey::PublicKey(PrivateKey *pkey) {
171+
#ifdef PIPY_USE_OPENSSL1
172+
m_pkey = pkey->pkey();
173+
if (!m_pkey)
174+
throw_error();
175+
EVP_PKEY_up_ref(m_pkey);
176+
#else
171177
m_pkey = EVP_PKEY_dup(pkey->pkey());
172-
if (!m_pkey) throw_error();
178+
if (!m_pkey)
179+
throw_error();
180+
#endif
173181
}
174182

175183
PublicKey::~PublicKey() {
@@ -423,14 +431,23 @@ Certificate::Certificate(const Options &options) {
423431
throw std::runtime_error("missing public key");
424432
}
425433

426-
// Digest algorithm
434+
// Digest algorithm
435+
#ifdef PIPY_USE_OPENSSL1
436+
const EVP_MD *md = nullptr;
437+
if (EVP_PKEY_type(EVP_PKEY_id(options.private_key->pkey())) == EVP_PKEY_RSA ||
438+
EVP_PKEY_type(EVP_PKEY_id(options.private_key->pkey())) == EVP_PKEY_EC) {
439+
// Default to SHA256 for RSA and EC keys
440+
md = EVP_sha256();
441+
}
442+
#else
427443
char digest_name[80];
428444
if (EVP_PKEY_get_default_digest_name(options.private_key->pkey(), digest_name, sizeof(digest_name)) == 2) {
429445
if (!std::strcmp(digest_name, "UNDEF")) {
430446
digest_name[0] = '\0';
431447
}
432448
}
433449
auto md = digest_name[0] ? Hash::algorithm(digest_name) : nullptr;
450+
#endif
434451

435452
// Sign
436453
if (!X509_sign(x509, options.private_key->pkey(), md)) throw_error();

src/filters/tls.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,16 @@ auto TLSSession::hostname() -> pjs::Str* {
353353

354354
auto TLSSession::peer() -> crypto::Certificate* {
355355
if (!m_peer) {
356+
#ifndef PIPY_USE_OPENSSL1
356357
if (auto x = SSL_get0_peer_certificate(m_ssl)) {
357358
m_peer = crypto::Certificate::make(x);
358359
}
360+
#else
361+
if (auto x = SSL_get_peer_certificate(m_ssl)) {
362+
m_peer = crypto::Certificate::make(x);
363+
X509_free(x);
364+
}
365+
#endif
359366
}
360367
return m_peer;
361368
}

0 commit comments

Comments
 (0)