Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,10 @@ common --lockfile_mode=error
# LLVM source-based coverage (@score_coverage): --config=llvm_cov
import %workspace%/tools/coverage/coverage.bazelrc

# Dynamic linking ergonomics
build:pkcs11-dynamic --//score/crypto/src/backend:pkcs11_link_type=dynamic
build:pkcs11-dynamic --@score_openssl//:use_dynamic_openssl=true
build:openssl-dynamic --@score_openssl//:use_dynamic_openssl=true

# Import optional user-specific configurations after repository defaults.
try-import %workspace%/user.bazelrc
37 changes: 14 additions & 23 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ python.toolchain(
)

# Foreign CC rules for building projects with external build systems (make, cmake, etc.)
bazel_dep(name = "rules_foreign_cc", version = "0.15.1")
bazel_dep(name = "rules_foreign_cc", version = "0.16.0")

# rules_foreign_cc's cmake rule does not know the QNX target OS (CMAKE_SYSTEM_NAME "QNX")
single_version_override(
module_name = "rules_foreign_cc",
patch_strip = 1,
patches = ["//third_party/soft_hsm:rules_foreign_cc_qnx.patch"],
)

# Use the host's pkg-config and m4 instead of building them from source. The bundled glib of the
# source-built pkgconfig uses the GNU 'asm' keyword and fails with a strict -std=c11 host compiler.
Expand Down Expand Up @@ -156,6 +163,11 @@ use_repo(llvm, "llvm_toolchain", "llvm_toolchain_llvm")
bazel_dep(name = "score_toolchains_rust", version = "0.10.1", dev_dependency = True)

bazel_dep(name = "score_crates", version = "0.0.15")
git_override(
module_name = "score_crates",
commit = "871234431b05bc99b249fb5db775bbe850ed7f0d",
remote = "https://github.com/eclipse-score/score-crates.git",
)

crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate", isolate = True)
crate.from_cargo(
Expand All @@ -169,13 +181,6 @@ crate.from_cargo(
"x86_64-pc-nto-qnx710",
],
)
crate.annotation(
crate = "openssl-sys",
patch_args = ["-p1"],
patches = ["@@//third_party/patches:openssl_sys_build_rs.patch"],
rustc_flags = ["--cap-lints=allow"],
deps = ["@@//third_party/openssl:openssl_shared"],
)
use_repo(crate, "crates")

# *******************************************************************************
Expand Down Expand Up @@ -214,21 +219,7 @@ use_repo(
# OpenSSL
# *******************************************************************************

http_archive(
name = "openssl_source",
build_file_content = """
filegroup(
name = "all",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)
""",
patch_args = ["-p1"],
patches = ["//third_party/openssl:openssl_qnx_target.patch"],
sha256 = "b1bfedcd5b289ff22aee87c9d600f515767ebf45f77168cb6d64f231f518a82e",
strip_prefix = "openssl-3.6.1",
urls = ["https://www.openssl.org/source/openssl-3.6.1.tar.gz"],
)
bazel_dep(name = "score_openssl", version = "0.2.1")

# *******************************************************************************
# SoftHSMv2
Expand Down
459 changes: 106 additions & 353 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

96 changes: 93 additions & 3 deletions score/crypto/src/backend/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
# SPDX-License-Identifier: Apache-2.0
# *******************************************************************************

load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")
load("@bazel_skylib//lib:selects.bzl", "selects")
load("@rules_cc//cc:defs.bzl", "cc_library")

# ============================================================================
Expand Down Expand Up @@ -125,10 +126,84 @@ cc_library(
# --//score/crypto/src/backend:pkcs11_backend=//external/vendor_hsm:backend
label_flag(
name = "pkcs11_backend",
build_setting_default = "//third_party/soft_hsm:soft_hsm",
visibility = ["//visibility:public"],
)

# Label flag allowing command-line override of the shared PKCS#11 implementation
# when dynamic linking is enabled (--//score/crypto/src/backend:pkcs11_link_type=dynamic).
# Default is SoftHSM's shared library target.
label_flag(
name = "pkcs11_shared_backend",
build_setting_default = "//third_party/soft_hsm:softhsm_shared",
visibility = ["//visibility:public"],
)

# Command line flag allowing users to select PKCS#11 link type independently.
# Usage:
# bazel build //score/crypto/src/daemon:crypto_daemon \
# --//score/crypto/src/backend:pkcs11_link_type=dynamic
string_flag(
name = "pkcs11_link_type",
build_setting_default = "static",
values = [
"static",
"dynamic",
],
visibility = ["//visibility:public"],
)

config_setting(
name = "pkcs11_link_dynamic",
flag_values = {":pkcs11_link_type": "dynamic"},
visibility = ["//visibility:public"],
)

# Dynamic PKCS#11 is added to the daemon if:
# 1. PKCS#11 link type is dynamic, AND
# 2. PKCS#11 backend is enabled.
selects.config_setting_group(
name = "dynamic_pkcs11_in_daemon",
match_all = [
":pkcs11_link_dynamic",
":pkcs11_backend_enabled",
],
visibility = ["//visibility:public"],
)

# Dynamic OpenSSL Case A: User explicitly passed --@score_openssl//:use_dynamic_openssl=true
# AND OpenSSL backend is active in the daemon.
selects.config_setting_group(
name = "dynamic_openssl_via_flag",
match_all = [
"@score_openssl//:dynamic_openssl_enabled",
":openssl_backend_active",
],
visibility = ["//visibility:public"],
)

# Dynamic OpenSSL Case B (Guardrail):
# Dynamic PKCS#11 is active AND OpenSSL backend is active in the daemon.
# (Prevents two static OpenSSL instances in memory).
selects.config_setting_group(
name = "dynamic_openssl_forced_by_pkcs11",
match_all = [
":pkcs11_link_dynamic",
":openssl_backend_active",
],
visibility = ["//visibility:public"],
)

# Master switch: OpenSSL shared is linked into the daemon if EITHER Case A OR Case B is true.
selects.config_setting_group(
name = "dynamic_openssl_in_daemon",
match_any = [
":dynamic_openssl_via_flag",
":dynamic_openssl_forced_by_pkcs11",
],
visibility = ["//visibility:public"],
)

# Lightweight: single define exposing whether PKCS#11 is compiled in.
# Used as implementation_deps in targets that need the define for compilation
# but must not propagate it to their consumers.
Expand Down Expand Up @@ -156,13 +231,28 @@ cc_library(
)

# ============================================================================
# Valeo Custom Compile Toggles
# Cryptoki Provider Selection
# ============================================================================

config_setting(
name = "rust_cryptoki_selected",
name = "rust_cryptoki_static_selected",
flag_values = {
":pkcs11_backend": "//score/cryptoki:cryptoki_static_wrapped",
},
)

config_setting(
name = "rust_cryptoki_dynamic_selected",
flag_values = {
":pkcs11_backend": "//score/cryptoki:cryptoki_cdylib_wrapped",
},
)

selects.config_setting_group(
name = "rust_cryptoki_selected",
match_any = [
":rust_cryptoki_static_selected",
":rust_cryptoki_dynamic_selected",
],
visibility = ["//visibility:public"],
)
55 changes: 54 additions & 1 deletion score/crypto/src/backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ via a `label_flag`:
# backend/BUILD
label_flag(
name = "pkcs11_backend",
build_setting_default = "//third_party/soft_hsm:softhsm_shared",
build_setting_default = "//third_party/soft_hsm:soft_hsm",
)
```

Expand All @@ -102,6 +102,59 @@ bazel build //score/crypto/src/daemon:crypto_daemon \
Config parsing (`Pkcs11Config::ParseConfig`) lives in `backend/pkcs11/` and is
independent of the selected library.

## Static vs. Dynamic Linking Behavior

By default (per S-CORE architectural mandate **DR-002**), all cryptographic backends and dependencies are linked **statically** into the binaries:
- OpenSSL static archives (`libcrypto.a`, `libssl.a`) are linked directly into `crypto_daemon`.
- SoftHSM compiles as a static archive (`libsofthsm2-static.a`) and links statically into `crypto_daemon`.
- Rust Cryptoki provides a C-compatible static archive (`cryptoki_static` / `cryptoki_static_wrapped`) via `rust_static_library` for pure static linking.

### Independent Link Selection Flags

Linking for OpenSSL and PKCS#11 is fully decoupled into orthogonal Bazel flags:

| Dimension | Flag | Default | Dynamic Override |
| :--- | :--- | :--- | :--- |
| **OpenSSL Linking** | `@score_openssl//:use_dynamic_openssl` | `False` (Static) | `--@score_openssl//:use_dynamic_openssl=true` |
| **PKCS#11 Linking** | `//score/crypto/src/backend:pkcs11_link_type` | `"static"` | `--//score/crypto/src/backend:pkcs11_link_type=dynamic` |

### Command-Line Shorthand Configurations (`.bazelrc`)

To simplify local development and CI execution, `.bazelrc` provides convenient presets:

```bash
# 1. Pure Static Build (Default per DR-002)
bazel build //score/crypto/src/daemon:crypto_daemon

# 2. Dynamic OpenSSL Only (PKCS#11 remains static)
bazel build //score/crypto/src/daemon:crypto_daemon --config=openssl-dynamic

# 3. Dynamic PKCS#11 (Automatically applies OpenSSL guardrail)
bazel build //score/crypto/src/daemon:crypto_daemon --config=pkcs11-dynamic
```

### Transitive Invariants & Safety Guardrails

The linking architecture enforces three mathematical rules:

```text
┌────────────────────────────────────────────────────────┐
│ 1. Dynamic OpenSSL does NOT force PKCS#11 dynamic. │
│ (OpenSSL can be dynamic while PKCS#11 is static). │
├────────────────────────────────────────────────────────┤
│ 2. Disabled backends are excluded from dynamic_deps. │
│ (If OpenSSL is disabled, openssl_shared is omitted).│
├────────────────────────────────────────────────────────┤
│ 3. Dynamic PKCS#11 forces dynamic OpenSSL ONLY when │
│ OpenSSL backend is active in the daemon. │
└────────────────────────────────────────────────────────┘
```

* **Rationale for Rule 3 (Preventing ODR Violation):**
If the daemon enables the OpenSSL provider and dynamically links PKCS#11 (`libsofthsm2.so` or `libcryptoki.so`), both components require OpenSSL. If OpenSSL were static, the daemon would embed `libcrypto.a` while the PKCS#11 shared library embeds another `libcrypto.a`, causing two static OpenSSL instances to conflict in process memory. Bazel automatically promotes OpenSSL to dynamic (`openssl_shared`) so both share a single `libcrypto.so` instance mapped once by the OS dynamic linker (`ld.so`).
* **Tool Isolation:**
CLI tools that only interact with PKCS#11 (such as `init_pkcs11_token`) link exclusively to `pkcs11_shared_backend` and never directly link `openssl_shared`. OpenSSL is loaded transitively by the PKCS#11 driver via standard ELF `DT_NEEDED`.

## Requirements for the PKCS#11 Library

The selected PKCS#11 library must successfully initialize when `C_Initialize`
Expand Down
19 changes: 12 additions & 7 deletions score/crypto/src/backend/score_provider/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
"//score/crypto/src/daemon/provider/score_provider:score_backend_adapter",
"//score/crypto/src/backend/score_provider/openssl:openssl_backend_define",
# To add a new score backend: add its *_backend_define dep here (step 3).
],
] + select({
"//score/crypto/src/backend:openssl_backend_active": [
"//score/crypto/src/backend/score_provider/openssl:openssl_backend_define",
],
"//conditions:default": [],
}),
)

# Heavy: aggregates all active sub-backend adapter targets.
Expand All @@ -69,8 +72,10 @@ cc_library(
"//conditions:default": ["@platforms//:incompatible"],
}),
visibility = ["//score/crypto/src/backend:__pkg__"],
deps = [
"//score/crypto/src/backend/score_provider/openssl:openssl_backend",
# To add a new score backend: add its *_backend dep here (step 4).
],
deps = select({
"//score/crypto/src/backend:openssl_backend_active": [
"//score/crypto/src/backend/score_provider/openssl:openssl_backend",
],
"//conditions:default": [],
}),
)
11 changes: 11 additions & 0 deletions score/crypto/src/daemon/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ cc_binary(
"-O1",
],
}),
dynamic_deps = select({
"//score/crypto/src/backend:dynamic_openssl_in_daemon": [
"@score_openssl//:openssl_shared",
],
"//conditions:default": [],
}) + select({
"//score/crypto/src/backend:dynamic_pkcs11_in_daemon": [
"//score/crypto/src/backend:pkcs11_shared_backend",
],
"//conditions:default": [],
}),
includes = ["IPC"],
linkopts = select({
"//conditions:default": [],
Expand Down
12 changes: 6 additions & 6 deletions score/crypto/src/daemon/provider/score_provider/openssl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ cc_library(
visibility = ["//score/crypto/src/daemon/provider:__subpackages__"],
deps = [
"//score/crypto/src/api/types:certificate_types",
"//third_party/openssl:openssl_shared",
"@score_openssl//:openssl",
],
)

Expand All @@ -90,7 +90,7 @@ cc_library(
srcs = ["cert_management/openssl_cert_parser.cpp"],
implementation_deps = [
":openssl_cert_utils",
"//third_party/openssl:openssl_shared",
"@score_openssl//:openssl",
],
target_compatible_with = select({
"//score/crypto/src/backend:openssl_backend_active": [],
Expand All @@ -117,7 +117,7 @@ cc_test(
":openssl_cert_management_headers",
":openssl_cert_parser_library",
"//score/crypto/src/daemon/cert_management:cert_management_headers",
"//third_party/openssl:openssl_shared",
"@score_openssl//:openssl",
"@googletest//:gtest_main",
],
)
Expand All @@ -126,7 +126,7 @@ cc_test(
cc_library(
name = "openssl_detail_headers",
hdrs = ["detail/openssl_algorithm_info.hpp"],
implementation_deps = ["//third_party/openssl:openssl_shared"],
implementation_deps = ["@score_openssl//:openssl"],
target_compatible_with = select({
"//score/crypto/src/backend:openssl_backend_active": [],
"//conditions:default": ["@platforms//:incompatible"],
Expand Down Expand Up @@ -205,7 +205,7 @@ cc_library(
"provider_openssl.cpp",
],
implementation_deps = [
"//third_party/openssl:openssl_shared",
"@score_openssl//:openssl",
],
includes = ["."],
linkstatic = True,
Expand Down Expand Up @@ -245,7 +245,7 @@ cc_test(
":provider_openssl_library",
"//score/crypto/src/daemon/common:algorithm_catalog",
"//score/crypto/src/daemon/common:algorithm_info",
"//third_party/openssl:openssl_shared",
"@score_openssl//:openssl",
"@googletest//:gtest_main",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cc_test(
"//score/crypto/src/daemon/provider/handler:hash_handler_operations",
"//score/crypto/src/daemon/provider/score_provider/openssl:provider_openssl_library",
"//score/tests/utility",
"//third_party/openssl:openssl_shared",
"@score_openssl//:openssl",
"@googletest//:gtest",
],
)
Expand Down
Loading
Loading