Skip to content

Commit 99ae5ec

Browse files
committed
Add OpenSSL as a bazel dependency for bssl-compat
Signed-off-by: Ted Poole <[email protected]>
1 parent e22d3f9 commit 99ae5ec

File tree

13 files changed

+227
-64
lines changed

13 files changed

+227
-64
lines changed

bazel/external/openssl.BUILD

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
load("@rules_foreign_cc//foreign_cc:configure.bzl", "configure_make")
2+
load("@rules_cc//cc:defs.bzl", "cc_library")
3+
4+
licenses(["notice"]) # Apache 2
5+
6+
filegroup(
7+
name = "all",
8+
srcs = glob(["**"]),
9+
visibility = ["//visibility:public"],
10+
)
11+
12+
configure_make(
13+
name = "openssl",
14+
lib_source = ":all",
15+
configure_in_place = True,
16+
configure_command = "Configure",
17+
targets = ["build_sw", "install_sw"],
18+
args = ["-j"],
19+
out_lib_dir = "lib64",
20+
out_shared_libs = ["libssl.so.3", "libcrypto.so.3"],
21+
visibility = ["//visibility:public"],
22+
)

bazel/repositories.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ def envoy_dependencies(skip_targets = []):
135135
# Setup external Bazel rules
136136
_foreign_cc_dependencies()
137137

138+
_openssl()
139+
138140
# Binding to an alias pointing to the bssl-compat layer
139141
native.bind(
140142
name = "ssl",
@@ -300,6 +302,12 @@ def _aws_lc():
300302
build_file = "@envoy//bazel/external:aws_lc.BUILD",
301303
)
302304

305+
def _openssl():
306+
external_http_archive(
307+
name = "openssl",
308+
build_file = "@envoy//bazel/external:openssl.BUILD",
309+
)
310+
303311
def _com_github_openhistogram_libcircllhist():
304312
external_http_archive(
305313
name = "com_github_openhistogram_libcircllhist",

bazel/repository_locations.bzl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ REPOSITORY_LOCATIONS_SPEC = dict(
172172
release_date = "2025-06-27",
173173
cpe = "cpe:2.3:a:google:boringssl:*",
174174
),
175+
openssl = dict(
176+
project_name = "OpenSSL",
177+
project_desc = "TLS/SSL and crypto library",
178+
project_url = "https://github.com/openssl/openssl",
179+
version = "3.0.16",
180+
sha256 = "57e03c50feab5d31b152af2b764f10379aecd8ee92f16c985983ce4a99f7ef86",
181+
strip_prefix = "openssl-{version}",
182+
urls = ["https://github.com/openssl/openssl/releases/download/openssl-{version}/openssl-{version}.tar.gz"],
183+
use_category = ["controlplane", "dataplane_core"],
184+
release_date = "2025-02-11",
185+
cpe = "cpe:2.3:a:openssl:openssl:*",
186+
license = "Apache-2.0",
187+
license_url = "https://github.com/openssl/openssl/blob/openssl-{version}/LICENSE.txt",
188+
),
175189
aspect_bazel_lib = dict(
176190
project_name = "Aspect Bazel helpers",
177191
project_desc = "Base Starlark libraries and basic Bazel rules which are useful for constructing rulesets and BUILD files",

bssl-compat/BUILD

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,47 @@ filegroup(
77

88
licenses(["notice"]) # Apache 2
99

10+
filegroup(
11+
name = "openssl-libssl",
12+
srcs = ["@openssl//:openssl"],
13+
output_group = "libssl.so.3",
14+
visibility = ["//visibility:private"],
15+
)
16+
17+
filegroup(
18+
name = "openssl-libcrypto",
19+
srcs = ["@openssl//:openssl"],
20+
output_group = "libcrypto.so.3",
21+
visibility = ["//visibility:private"],
22+
)
23+
24+
filegroup(
25+
name = "openssl-libs",
26+
srcs = [
27+
":openssl-libssl",
28+
":openssl-libcrypto",
29+
],
30+
visibility = ["//visibility:private"],
31+
)
32+
33+
cc_library(
34+
name = "openssl-headers",
35+
hdrs = ["@openssl//:openssl"],
36+
include_prefix = "",
37+
strip_include_prefix = "",
38+
visibility = ["//visibility:private"],
39+
)
40+
1041
cmake(
1142
name = "bssl-compat",
1243
lib_source = ":srcs",
1344
out_shared_libs = [],
1445
out_static_libs = ["libbssl-compat.a"],
1546
visibility = ["//visibility:public"],
1647
generate_crosstool_file = False,
17-
out_binaries = ["utests-bssl-compat"],
18-
build_args = [ "-j" ]
19-
)
20-
21-
filegroup(
22-
name = "bssl-compat-utests-bssl-compat",
23-
srcs = [":bssl-compat"],
24-
output_group = "utests-bssl-compat",
25-
)
26-
27-
sh_test(
28-
name = "utests-bssl-compat",
29-
size = "small",
30-
srcs = [":bssl-compat-utests-bssl-compat"],
48+
build_args = [ "-j" ],
49+
deps = [":openssl-headers"],
50+
data = [":openssl-libs"],
3151
)
3252

3353
alias(

bssl-compat/CMakeLists.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ if(POLICY CMP0135)
77
cmake_policy(SET CMP0135 NEW)
88
endif()
99

10-
set(OPENSSL_URL https://github.com/openssl/openssl/archive/refs/tags/openssl-3.0.13.tar.gz)
11-
set(OPENSSL_URL_HASH e74504ed7035295ec7062b1da16c15b57ff2a03cd2064a28d8c39458cacc45fc)
12-
1310
set(CMAKE_C_STANDARD 11)
1411
set(CMAKE_CXX_STANDARD 17)
1512

@@ -38,6 +35,8 @@ add_dependencies(bssl-gen ossl-gen) # generate ossl headers before bssl headers
3835

3936
# custom mappings
4037
add_library(bssl-compat STATIC
38+
source/ossl_dlfcn.c
39+
source/ossl_init_debug.cc
4140
source/ASN1_TIME_free.cc
4241
source/BASIC_CONSTRAINTS_free.cc
4342
source/BASIC_CONSTRAINTS_new.cc
@@ -702,6 +701,7 @@ target_add_bssl_source(bssl-compat
702701
)
703702
target_compile_definitions(bssl-compat PUBLIC ossl_OPENSSL_SUPPRESS_DEPRECATED)
704703
target_include_directories(bssl-compat PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
704+
target_include_directories(bssl-compat PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/source)
705705
target_include_directories(bssl-compat PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/include)
706706
target_include_directories(bssl-compat PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/source)
707707
target_link_libraries(bssl-compat INTERFACE ${CMAKE_DL_LIBS})
@@ -776,15 +776,15 @@ set(utests-source-list
776776
################################################################################
777777
# Unit Tests built on bssl-compat
778778
################################################################################
779-
add_executable(utests-bssl-compat ${utests-source-list} source/test/main.cc)
780-
target_add_bssl_source(utests-bssl-compat ${utests-bssl-source-list})
781-
set_source_files_properties(source/extra/err_extra.c PROPERTIES COMPILE_FLAGS -Wno-deprecated-declarations)
782-
# Add env variable SANITIZE_OPTIONS for optional sanitizers
783-
target_link_options(utests-bssl-compat PRIVATE "$ENV{SANITIZE_OPTIONS}" )
784-
target_link_libraries(utests-bssl-compat PRIVATE stdc++ GTest::gtest bssl-compat)
785-
set_target_properties(utests-bssl-compat PROPERTIES BUILD_RPATH "${OPENSSL_LIBRARY_DIR}")
786-
gtest_discover_tests(utests-bssl-compat)
787-
install(TARGETS utests-bssl-compat DESTINATION bin)
779+
# add_executable(utests-bssl-compat ${utests-source-list} source/test/main.cc)
780+
# target_add_bssl_source(utests-bssl-compat ${utests-bssl-source-list})
781+
# set_source_files_properties(source/extra/err_extra.c PROPERTIES COMPILE_FLAGS -Wno-deprecated-declarations)
782+
# # Add env variable SANITIZE_OPTIONS for optional sanitizers
783+
# target_link_options(utests-bssl-compat PRIVATE "$ENV{SANITIZE_OPTIONS}" )
784+
# target_link_libraries(utests-bssl-compat PRIVATE stdc++ GTest::gtest bssl-compat)
785+
# set_target_properties(utests-bssl-compat PROPERTIES BUILD_RPATH "${OPENSSL_LIBRARY_DIR}")
786+
# gtest_discover_tests(utests-bssl-compat)
787+
# install(TARGETS utests-bssl-compat DESTINATION bin)
788788

789789
################################################################################
790790
# Unit Tests built on BoringSSL

bssl-compat/cmake/openssl.cmake

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,5 @@ if(OpenSSL_FOUND)
55
get_filename_component(OPENSSL_LIBRARY_DIR ${OPENSSL_CRYPTO_LIBRARY} DIRECTORY)
66
message(STATUS "Found OpenSSL ${OPENSSL_VERSION} (${OPENSSL_LIBRARY_DIR})")
77
else()
8-
message(STATUS "Building OpenSSL (${OPENSSL_URL})")
9-
include(ExternalProject)
10-
set(OPENSSL_SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/openssl/source)
11-
set(OPENSSL_CONFIG_CMD ${OPENSSL_SOURCE_DIR}/config)
12-
set(OPENSSL_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/openssl/install)
13-
set(OPENSSL_INCLUDE_DIR ${OPENSSL_INSTALL_DIR}/include)
14-
set(OPENSSL_LIBRARY_DIR ${OPENSSL_INSTALL_DIR}/lib)
15-
ExternalProject_Add(OpenSSL
16-
URL ${OPENSSL_URL}
17-
URL_HASH SHA256=${OPENSSL_URL_HASH}
18-
SOURCE_DIR ${OPENSSL_SOURCE_DIR}
19-
CONFIGURE_COMMAND ${OPENSSL_CONFIG_CMD} --prefix=${OPENSSL_INSTALL_DIR} --libdir=lib
20-
TEST_COMMAND ""
21-
INSTALL_COMMAND make install_sw
22-
)
8+
message(FATAL_ERROR "OpenSSL 3.0 not found. Aborting.")
239
endif()

bssl-compat/prefixer/prefixer.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,9 @@ class MyFrontendAction: public clang::ASTFrontendAction {
265265
if (prefixable(token.getLocation())) {
266266
std::string name = pp.getSpelling(token);
267267
m_identifiers.insert(name);
268-
if (name == "SHLIB_VERSION_NUMBER") {
268+
if (name == "OPENSSL_VERSION_MAJOR") {
269269
const auto &token = directive->getMacroInfo()->getReplacementToken(0);
270270
m_shlibversion = std::string(token.getLiteralData(), token.getLength());
271-
// Remove the quotation marks.
272-
m_shlibversion = m_shlibversion.substr(1, m_shlibversion.size() - 1);
273-
m_shlibversion.pop_back();
274271
}
275272
}
276273
}
@@ -440,9 +437,10 @@ void MyFrontendAction::EndSourceFileAction() {
440437
std::map<std::string,std::vector<Function>> funcmap;
441438

442439
for(const auto &f : m_functions) {
443-
std::string header = f.getHeader(srcmgr);
440+
std::filesystem::path header = f.getHeader(srcmgr);
441+
header = header.lexically_relative(opt::incdir());
444442
if(funcmap.find(header) == funcmap.end()) {
445-
hstr << "#include \"" << header <<"\"" << std::endl;
443+
hstr << "#include \"" << header.string() <<"\"" << std::endl;
446444
}
447445
funcmap[header].push_back(f);
448446
}
@@ -474,9 +472,11 @@ void MyFrontendAction::EndSourceFileAction() {
474472
std::ofstream cstr (opt::cfile());
475473

476474
cstr << "//" << std::endl << "// THIS FILE IS GENERATED BY THE PREFIXER TOOL DO NOT EDIT" << std::endl << "//" << std::endl
475+
<< "#include <link.h>" << std::endl
477476
<< "#include <dlfcn.h>" << std::endl
478477
<< "#include <errno.h>" << std::endl
479478
<< "#include <assert.h>" << std::endl
479+
<< "#include \"ossl_dlfcn.h\"" << std::endl
480480
<< "#include \"" << opt::prefix << ".h\"" << std::endl
481481
<< std::endl
482482
<< "#define LIBCRYPTO_SO \"libcrypto.so" << (m_shlibversion.size() ? "." + m_shlibversion : "") << "\"" << std::endl
@@ -495,19 +495,20 @@ void MyFrontendAction::EndSourceFileAction() {
495495
<< " const char *s = symbol + " << opt::prefix.size() + 1 << ";" << std::endl
496496
<< " if ((result = dlsym(libcrypto, s)) != NULL) return result;" << std::endl
497497
<< " if((result = dlsym(libssl, s)) != NULL) return result;" << std::endl
498-
<< "//fprintf(stderr, \"dlsym(%s) : %s\\n\", s, dlerror());" << std::endl
499-
<< "//exit(ELIBACC);" << std::endl
500498
<< " return NULL;" << std::endl
501499
<< "}" << std::endl
502500
<< std::endl
501+
<< "void " << opt::prefix << "_init_debug(void);" << std::endl
502+
<< std::endl
503503
<< "static void " << opt::prefix << "_init(void) {" << std::endl
504-
<< " if((libcrypto = dlopen(LIBCRYPTO_SO, RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND)) == NULL) {" << std::endl
505-
<< " fprintf(stderr, \"dlopen(%s) : %s\\n\", LIBCRYPTO_SO, dlerror());" << std::endl
504+
<< " " << opt::prefix << "_init_debug();" << std::endl
505+
<< " if((libcrypto = ossl_dlopen(LIBCRYPTO_SO)) == NULL) {" << std::endl
506+
<< " fprintf(stderr, \"dlopen(%s) failed after searching multiple paths: %s\\n\", LIBCRYPTO_SO, dlerror());" << std::endl
506507
<< " exit(ELIBACC);" << std::endl
507508
<< " }" << std::endl
508509
<< std::endl
509-
<< " if((libssl = dlopen(LIBSSL_SO, RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND)) == NULL) {" << std::endl
510-
<< " fprintf(stderr, \"dlopen(%s) : %s\\n\", LIBSSL_SO, dlerror());" << std::endl
510+
<< " if((libssl = ossl_dlopen(LIBSSL_SO)) == NULL) {" << std::endl
511+
<< " fprintf(stderr, \"dlopen(%s) failed after searching multiple paths: %s\\n\", LIBSSL_SO, dlerror());" << std::endl
511512
<< " exit(ELIBACC);" << std::endl
512513
<< " }" << std::endl
513514
<< std::endl
@@ -713,7 +714,7 @@ int main(int argc, const char **argv) {
713714
globflags |= GLOB_APPEND;
714715
}
715716
for (auto i = 0; i < globbuf.gl_pathc; i++) {
716-
auto p = std::filesystem::proximate(globbuf.gl_pathv[i], srcpath);
717+
auto p = std::filesystem::path(globbuf.gl_pathv[i]).lexically_relative(srcpath);
717718
opt::headers[p] = true;
718719
}
719720
globfree (&globbuf);
@@ -727,7 +728,7 @@ int main(int argc, const char **argv) {
727728
globflags |= GLOB_APPEND;
728729
}
729730
for (auto i = 0; i < globbuf.gl_pathc; i++) {
730-
auto p = std::filesystem::proximate(globbuf.gl_pathv[i], srcpath);
731+
auto p = std::filesystem::path(globbuf.gl_pathv[i]).lexically_relative(srcpath);
731732
opt::headers[p] = false;
732733
}
733734
globfree (&globbuf);
@@ -758,6 +759,8 @@ int main(int argc, const char **argv) {
758759
std::filesystem::remove(dsthdr);
759760
}
760761
std::filesystem::copy_file(srcpath / hdr, dsthdr);
762+
std::filesystem::permissions(dsthdr, std::filesystem::perms::owner_write |
763+
std::filesystem::perms::owner_read);
761764
}
762765
}
763766

bssl-compat/source/ossl_dlfcn.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#define _GNU_SOURCE
2+
#include <dlfcn.h>
3+
#include <limits.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include "ossl_dlfcn.h"
7+
8+
9+
void* ossl_dlopen(const char* name) {
10+
void* handle = NULL;
11+
const char* runfiles_dir = getenv("RUNFILES_DIR");
12+
13+
if (runfiles_dir) {
14+
char fullpath[PATH_MAX];
15+
snprintf(fullpath, sizeof(fullpath), "%s/%s/%s", runfiles_dir,
16+
"envoy/external/openssl/openssl/lib64", name);
17+
handle = dlopen(fullpath, RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND);
18+
}
19+
else {
20+
handle = dlopen(name, RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND);
21+
}
22+
23+
if(handle) {
24+
char origin[PATH_MAX];
25+
if (dlinfo(handle, RTLD_DI_ORIGIN, origin) == 0) {
26+
fprintf(stderr, "bssl-compat: Loaded %s from %s\n", name, origin);
27+
}
28+
}
29+
30+
return handle;
31+
}

bssl-compat/source/ossl_dlfcn.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef OSSL_DLFCN_H
2+
#define OSSL_DLFCN_H
3+
4+
void* ossl_dlopen(const char* name);
5+
6+
#endif // OSSL_DLFCN_H
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
#include <filesystem>
3+
#include <cstdlib>
4+
#include <dlfcn.h>
5+
6+
extern "C" void ossl_init_debug(void) {
7+
std::cerr << std::endl;
8+
std::cerr << std::endl;
9+
std::cerr << "================================================================================" << std::endl;
10+
std::cerr << "pwd = " << std::filesystem::current_path() << std::endl;
11+
std::cerr << "RUNFILES = " << (getenv("RUNFILES") ? getenv("RUNFILES") : "<not set>") << std::endl;
12+
std::cerr << "RUNFILES_DIR = " << (getenv("RUNFILES_DIR") ? getenv("RUNFILES_DIR") : "<not set>") << std::endl;
13+
std::cerr << "LD_LIBRARY_PATH = " << (getenv("LD_LIBRARY_PATH") ? getenv("LD_LIBRARY_PATH") : "<not set>") << std::endl;
14+
std::cerr << "==================================== set =======================================" << std::endl;
15+
system("set");
16+
std::cerr << "================================================================================" << std::endl;
17+
std::cerr << "libcrypto.so.3 : "; system("find . -name 'libcrypto.so.3' -ls");
18+
std::cerr << "================================================================================" << std::endl;
19+
std::cerr << "libssl.so.3 : "; system("find . -name 'libssl.so.3' -ls");
20+
std::cerr << "================================================================================" << std::endl;
21+
std::cerr << std::endl;
22+
std::cerr << std::endl;
23+
24+
// Check if libcrypto.so is already linked in
25+
if (dlsym(RTLD_DEFAULT, "OPENSSL_version_major") != NULL) {
26+
std::cerr << std::endl;
27+
std::cerr << std::endl;
28+
std::cerr << "===============================================" << std::endl;
29+
std::cerr << "======= libcrypto.so already linked in! =======" << std::endl;
30+
std::cerr << "===============================================" << std::endl;
31+
std::cerr << std::endl;
32+
std::cerr << std::endl;
33+
// exit(1);
34+
}
35+
}

0 commit comments

Comments
 (0)