Skip to content

Commit f72edc9

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

File tree

14 files changed

+229
-65
lines changed

14 files changed

+229
-65
lines changed

.github/workflows/envoy-openssl.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
uses: envoyproxy/toolshed/gh-actions/[email protected]
2727
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2828
- run: |
29-
./ci/run_envoy_docker.sh './ci/do_ci.sh gcc //test/...'
29+
./ci/run_envoy_docker.sh './ci/do_ci.sh gcc //test/... --//test/common/router:route_fuzz_test'
3030
env:
3131
BAZEL_BUILD_EXTRA_OPTIONS: >-
3232
--config=remote-envoy-engflow

bazel/external/openssl.BUILD

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
# Build OpenSSL - headers only, no linkable libraries produced
13+
configure_make(
14+
name = "headers",
15+
lib_source = ":all",
16+
configure_in_place = True,
17+
configure_command = "Configure",
18+
targets = ["build_sw", "install_sw"],
19+
args = ["-j"],
20+
out_headers_only = True,
21+
out_include_dir = "include",
22+
visibility = ["//visibility:public"],
23+
)
24+
25+
# Build OpenSSL again but this time output the shared libraries
26+
# We use a separate build to avoid the libraries being linkable
27+
configure_make(
28+
name = "openssl-libs-build",
29+
lib_source = ":all",
30+
configure_in_place = True,
31+
configure_command = "Configure",
32+
targets = ["build_sw", "install_sw"],
33+
args = ["-j"],
34+
out_lib_dir = "lib64",
35+
out_shared_libs = ["libssl.so.3", "libcrypto.so.3"],
36+
visibility = ["//visibility:private"],
37+
)
38+
39+
# Runtime libraries - extracted from the libs-only build
40+
filegroup(
41+
name = "libssl",
42+
srcs = [":openssl-libs-build"],
43+
output_group = "libssl.so.3",
44+
visibility = ["//visibility:private"],
45+
)
46+
47+
filegroup(
48+
name = "libcrypto",
49+
srcs = [":openssl-libs-build"],
50+
output_group = "libcrypto.so.3",
51+
visibility = ["//visibility:private"],
52+
)
53+
54+
# Combine both libraries into a single target
55+
filegroup(
56+
name = "libs",
57+
srcs = [":libssl", ":libcrypto"],
58+
visibility = ["//visibility:public"],
59+
)

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: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,9 @@ cmake(
1414
out_static_libs = ["libbssl-compat.a"],
1515
visibility = ["//visibility:public"],
1616
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"],
17+
build_args = [ "-j" ],
18+
deps = ["@openssl//:headers"], # Build-time only: headers for compilation
19+
data = ["@openssl//:libs"], # Runtime only: shared libraries in runfiles
3120
)
3221

3322
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-libs-build/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 && getenv("BSSL_COMPAT_DEBUG_DLOPEN")) {
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

0 commit comments

Comments
 (0)