Skip to content

Commit a02dc9e

Browse files
akramclaude
andcommitted
test(crypto): add SPIFFE JWT-SVID regression tests for multi-backend
Five tests validating EdDSA JWT-SVID operations used in the SPIFFE token exchange flow, runnable with either crypto backend: cargo test -p openshell-library-crypto-consumer --features runtime -- spiffe cargo test -p openshell-library-crypto-consumer --features openssl-runtime -- spiffe Covers: sign+verify, wrong audience, tampered signature, expired token, and cross-key rejection. Guards against regressions when rebasing Varsha's PR ai-openshell#5046 onto newer main. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 15dad6f commit a02dc9e

3 files changed

Lines changed: 294 additions & 4 deletions

File tree

‎tests/crypto-consumer/Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/crypto-consumer/Cargo.toml‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,28 @@ core-default = ["core-minimal", "openshell-core/default"]
1818
core-all = ["core-default", "openshell-core/oauth", "openshell-core/jwt", "openshell-core/trace-context", "openshell-core/driver-extraction"]
1919
sdk = ["dep:openshell-sdk"]
2020
policy = ["dep:openshell-policy"]
21-
runtime = ["core-all", "sdk", "policy", "dep:openshell-crypto", "dep:rcgen", "dep:rustls", "dep:jsonwebtoken", "dep:tokio", "dep:tokio-rustls", "dep:h2"]
21+
integration = ["core-all", "sdk", "policy", "dep:openshell-crypto", "dep:rcgen", "dep:rustls", "dep:jsonwebtoken", "dep:tokio", "dep:tokio-rustls", "dep:h2"]
22+
23+
runtime = ["integration", "rcgen/ring", "rustls/ring", "jsonwebtoken/rust_crypto", "dep:openshell-extension-core", "dep:openshell-otel", "dep:serde_json"]
24+
openssl-runtime = ["integration", "openshell-crypto/openssl", "dep:serde_json"]
25+
core-aws-lc = ["core-all", "openshell-core/aws-lc"]
26+
sdk-aws-lc = ["sdk", "openshell-sdk/aws-lc"]
2227

2328
[dependencies]
29+
openshell-extension-core = { path = "../../crates/openshell-extension-core", optional = true }
30+
openshell-otel = { path = "../../crates/openshell-otel", optional = true }
2431
openshell-core = { path = "../../crates/openshell-core", default-features = false, optional = true }
2532
openshell-sdk = { path = "../../crates/openshell-sdk", optional = true }
2633
openshell-policy = { path = "../../crates/openshell-policy", optional = true }
2734
openshell-crypto = { path = "../../crates/openshell-crypto", default-features = false, optional = true }
2835
# Consumer-owned implementations, used only by this isolated regression fixture.
29-
rcgen = { version = "0.14.10", default-features = false, features = ["ring", "pem"], optional = true }
30-
rustls = { version = "0.23", default-features = false, features = ["std", "ring", "tls12"], optional = true }
31-
jsonwebtoken = { version = "10", default-features = false, features = ["rust_crypto", "use_pem"], optional = true }
36+
rcgen = { version = "0.14.10", default-features = false, features = ["pem"], optional = true }
37+
rustls = { version = "0.23", default-features = false, features = ["std", "tls12"], optional = true }
38+
jsonwebtoken = { version = "10", default-features = false, features = ["use_pem"], optional = true }
3239
tokio = { version = "1.53.3", features = ["macros", "rt-multi-thread", "net", "time"], optional = true }
3340
tokio-rustls = { version = "0.26", default-features = false, optional = true }
3441
h2 = { version = "0.4", optional = true }
42+
serde_json = { version = "1", optional = true }
3543

3644
[[test]]
3745
name = "selected_backend"
@@ -44,3 +52,7 @@ required-features = ["runtime"]
4452
[[test]]
4553
name = "host_tls_provider"
4654
required-features = ["runtime"]
55+
56+
[[test]]
57+
name = "spiffe_jwt_svid"
58+
required-features = ["integration"]
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Validates that SPIFFE JWT-SVID signing, verification, and claim extraction
5+
//! work identically across crypto backends (aws-lc/Ring vs OpenSSL).
6+
//!
7+
//! Run with either backend:
8+
//! cargo test -p openshell-library-crypto-consumer --features runtime -- spiffe
9+
//! cargo test -p openshell-library-crypto-consumer --features openssl-runtime -- spiffe
10+
11+
use openshell_crypto::CryptoContext;
12+
use std::sync::Once;
13+
14+
static INIT: Once = Once::new();
15+
16+
fn install_backend() {
17+
INIT.call_once(|| {
18+
#[cfg(feature = "runtime")]
19+
{
20+
// Ring-backed consumer context (same as selected_backend.rs)
21+
openshell_crypto::install_default_context(CryptoContext::new(Box::new(
22+
RingConsumer,
23+
)))
24+
.unwrap();
25+
}
26+
#[cfg(feature = "openssl-runtime")]
27+
{
28+
openshell_crypto::install_default_context(CryptoContext::new(Box::new(
29+
openshell_crypto::OpenSsl,
30+
)))
31+
.unwrap();
32+
}
33+
});
34+
}
35+
36+
#[cfg(feature = "runtime")]
37+
struct RingConsumer;
38+
39+
#[cfg(feature = "runtime")]
40+
impl openshell_crypto::CryptoBackend for RingConsumer {
41+
fn capabilities(&self) -> openshell_crypto::Capabilities {
42+
openshell_crypto::Capabilities {
43+
backend: "ring-spiffe-test",
44+
provider_version: None,
45+
fips: openshell_crypto::FipsState::NotVerified,
46+
primitives: &[],
47+
key_algorithms: &[],
48+
}
49+
}
50+
fn fill_random(&self, buf: &mut [u8]) -> Result<(), openshell_crypto::CryptoError> {
51+
use std::io::Read;
52+
std::fs::File::open("/dev/urandom")
53+
.and_then(|mut f| f.read_exact(buf))
54+
.map_err(|_| openshell_crypto::CryptoError::Random)
55+
}
56+
fn sha256_digest(&self) -> Result<Box<dyn openshell_crypto::Digest>, openshell_crypto::CryptoError> {
57+
Err(openshell_crypto::CryptoError::Operation)
58+
}
59+
fn seal(&self, _: &[u8; 32], _: &[u8], _: &[u8]) -> Result<openshell_crypto::aead::Sealed, openshell_crypto::CryptoError> {
60+
Err(openshell_crypto::CryptoError::Operation)
61+
}
62+
fn open(&self, _: &[u8; 32], _: &[u8], _: &[u8; 12], _: &[u8]) -> Result<Vec<u8>, openshell_crypto::CryptoError> {
63+
Err(openshell_crypto::CryptoError::Authentication)
64+
}
65+
}
66+
67+
#[cfg(feature = "runtime")]
68+
impl openshell_crypto::ProtocolBackend for RingConsumer {
69+
fn tls_provider(&self) -> rustls::crypto::CryptoProvider {
70+
rustls::crypto::ring::default_provider()
71+
}
72+
fn jwt_provider(&self) -> &'static jsonwebtoken::crypto::CryptoProvider {
73+
&jsonwebtoken::crypto::rust_crypto::DEFAULT_PROVIDER
74+
}
75+
fn generate_keypair(
76+
&self,
77+
_: &'static rcgen::SignatureAlgorithm,
78+
) -> Result<openshell_crypto::pki::KeyPair, rcgen::Error> {
79+
Err(rcgen::Error::RemoteKeyError)
80+
}
81+
fn import_keypair_der(&self, _: &[u8]) -> Result<openshell_crypto::pki::KeyPair, rcgen::Error> {
82+
Err(rcgen::Error::RemoteKeyError)
83+
}
84+
fn import_keypair_pem(&self, _: &str) -> Result<openshell_crypto::pki::KeyPair, rcgen::Error> {
85+
Err(rcgen::Error::RemoteKeyError)
86+
}
87+
}
88+
89+
fn generate_ed25519_keypair() -> (String, String) {
90+
#[cfg(feature = "runtime")]
91+
{
92+
let key = rcgen::KeyPair::generate_for(&rcgen::PKCS_ED25519).unwrap();
93+
(key.serialize_pem(), key.public_key_pem())
94+
}
95+
#[cfg(feature = "openssl-runtime")]
96+
{
97+
let key = openshell_crypto::pki::generate_jwt_keypair().unwrap();
98+
(key.serialize_pem().unwrap(), key.public_key_pem())
99+
}
100+
}
101+
102+
/// Sign and verify an EdDSA JWT-SVID with SPIFFE standard claims.
103+
/// This is the core operation in the gateway SPIFFE token exchange flow:
104+
/// SPIRE mints a JWT-SVID, the gateway verifies it, extracts the SPIFFE ID,
105+
/// then exchanges it with Keycloak for a scoped access token.
106+
#[test]
107+
fn spiffe_eddsa_jwt_sign_and_verify() {
108+
install_backend();
109+
110+
let (private_pem, public_pem) = generate_ed25519_keypair();
111+
let signing = jsonwebtoken::EncodingKey::from_ed_pem(private_pem.as_bytes()).unwrap();
112+
let verifying = jsonwebtoken::DecodingKey::from_ed_pem(public_pem.as_bytes()).unwrap();
113+
114+
let claims = serde_json::json!({
115+
"sub": "spiffe://openshell.local/ns/openshell/sa/openshell-gateway",
116+
"iss": "https://oidc-discovery.example.com",
117+
"aud": ["model-api", "keycloak"],
118+
"exp": 4_000_000_000_u64,
119+
"iat": 1_700_000_000_u64
120+
});
121+
122+
let header = jsonwebtoken::Header::new(jsonwebtoken::Algorithm::EdDSA);
123+
let token = openshell_crypto::jwt::encode(&header, &claims, &signing).unwrap();
124+
125+
let mut validation = jsonwebtoken::Validation::new(jsonwebtoken::Algorithm::EdDSA);
126+
validation.set_issuer(&["https://oidc-discovery.example.com"]);
127+
validation.set_audience(&["model-api"]);
128+
129+
let decoded =
130+
openshell_crypto::jwt::decode::<serde_json::Value>(&token, &verifying, &validation)
131+
.unwrap();
132+
133+
assert_eq!(
134+
decoded.claims["sub"],
135+
"spiffe://openshell.local/ns/openshell/sa/openshell-gateway"
136+
);
137+
assert_eq!(
138+
decoded.claims["aud"],
139+
serde_json::json!(["model-api", "keycloak"])
140+
);
141+
}
142+
143+
/// Wrong audience must be rejected — the gateway checks `aud` before exchange.
144+
#[test]
145+
fn spiffe_jwt_wrong_audience_rejected() {
146+
install_backend();
147+
148+
let (private_pem, public_pem) = generate_ed25519_keypair();
149+
let signing = jsonwebtoken::EncodingKey::from_ed_pem(private_pem.as_bytes()).unwrap();
150+
let verifying = jsonwebtoken::DecodingKey::from_ed_pem(public_pem.as_bytes()).unwrap();
151+
152+
let claims = serde_json::json!({
153+
"sub": "spiffe://openshell.local/ns/openshell/sa/openshell-gateway",
154+
"iss": "https://oidc-discovery.example.com",
155+
"aud": "wrong-service",
156+
"exp": 4_000_000_000_u64
157+
});
158+
159+
let header = jsonwebtoken::Header::new(jsonwebtoken::Algorithm::EdDSA);
160+
let token = openshell_crypto::jwt::encode(&header, &claims, &signing).unwrap();
161+
162+
let mut validation = jsonwebtoken::Validation::new(jsonwebtoken::Algorithm::EdDSA);
163+
validation.set_issuer(&["https://oidc-discovery.example.com"]);
164+
validation.set_audience(&["model-api"]);
165+
166+
let err = openshell_crypto::jwt::decode::<serde_json::Value>(&token, &verifying, &validation)
167+
.unwrap_err();
168+
assert_eq!(
169+
err.kind(),
170+
&jsonwebtoken::errors::ErrorKind::InvalidAudience
171+
);
172+
}
173+
174+
/// Tampered signature must be rejected — protects against SVID forgery.
175+
#[test]
176+
fn spiffe_jwt_tampered_signature_rejected() {
177+
install_backend();
178+
179+
let (private_pem, public_pem) = generate_ed25519_keypair();
180+
let signing = jsonwebtoken::EncodingKey::from_ed_pem(private_pem.as_bytes()).unwrap();
181+
let verifying = jsonwebtoken::DecodingKey::from_ed_pem(public_pem.as_bytes()).unwrap();
182+
183+
let claims = serde_json::json!({
184+
"sub": "spiffe://openshell.local/ns/openshell/sa/openshell-gateway",
185+
"iss": "https://oidc-discovery.example.com",
186+
"aud": "model-api",
187+
"exp": 4_000_000_000_u64
188+
});
189+
190+
let header = jsonwebtoken::Header::new(jsonwebtoken::Algorithm::EdDSA);
191+
let token = openshell_crypto::jwt::encode(&header, &claims, &signing).unwrap();
192+
193+
// Tamper the signature
194+
let (message, sig) = token.rsplit_once('.').unwrap();
195+
let mut sig_bytes = sig.as_bytes().to_vec();
196+
sig_bytes[0] = if sig_bytes[0] == b'A' { b'B' } else { b'A' };
197+
let tampered = format!("{message}.{}", String::from_utf8(sig_bytes).unwrap());
198+
199+
let mut validation = jsonwebtoken::Validation::new(jsonwebtoken::Algorithm::EdDSA);
200+
validation.set_issuer(&["https://oidc-discovery.example.com"]);
201+
validation.set_audience(&["model-api"]);
202+
203+
let err =
204+
openshell_crypto::jwt::decode::<serde_json::Value>(&tampered, &verifying, &validation)
205+
.unwrap_err();
206+
assert_eq!(
207+
err.kind(),
208+
&jsonwebtoken::errors::ErrorKind::InvalidSignature
209+
);
210+
}
211+
212+
/// Expired JWT-SVID must be rejected — SPIRE SVIDs have short TTLs.
213+
#[test]
214+
fn spiffe_jwt_expired_rejected() {
215+
install_backend();
216+
217+
let (private_pem, public_pem) = generate_ed25519_keypair();
218+
let signing = jsonwebtoken::EncodingKey::from_ed_pem(private_pem.as_bytes()).unwrap();
219+
let verifying = jsonwebtoken::DecodingKey::from_ed_pem(public_pem.as_bytes()).unwrap();
220+
221+
let claims = serde_json::json!({
222+
"sub": "spiffe://openshell.local/ns/openshell/sa/openshell-gateway",
223+
"iss": "https://oidc-discovery.example.com",
224+
"aud": "model-api",
225+
"exp": 1 // expired long ago
226+
});
227+
228+
let header = jsonwebtoken::Header::new(jsonwebtoken::Algorithm::EdDSA);
229+
let token = openshell_crypto::jwt::encode(&header, &claims, &signing).unwrap();
230+
231+
let mut validation = jsonwebtoken::Validation::new(jsonwebtoken::Algorithm::EdDSA);
232+
validation.set_issuer(&["https://oidc-discovery.example.com"]);
233+
validation.set_audience(&["model-api"]);
234+
235+
let err = openshell_crypto::jwt::decode::<serde_json::Value>(&token, &verifying, &validation)
236+
.unwrap_err();
237+
assert_eq!(
238+
err.kind(),
239+
&jsonwebtoken::errors::ErrorKind::ExpiredSignature
240+
);
241+
}
242+
243+
/// Cross-key verification must fail — a JWT-SVID signed by one SPIRE server
244+
/// must not verify with another server's key.
245+
#[test]
246+
fn spiffe_jwt_cross_key_rejected() {
247+
install_backend();
248+
249+
let (private_pem, _) = generate_ed25519_keypair();
250+
let (_, other_public_pem) = generate_ed25519_keypair();
251+
252+
let signing = jsonwebtoken::EncodingKey::from_ed_pem(private_pem.as_bytes()).unwrap();
253+
let wrong_verifier =
254+
jsonwebtoken::DecodingKey::from_ed_pem(other_public_pem.as_bytes()).unwrap();
255+
256+
let claims = serde_json::json!({
257+
"sub": "spiffe://openshell.local/ns/openshell/sa/openshell-gateway",
258+
"iss": "https://oidc-discovery.example.com",
259+
"aud": "model-api",
260+
"exp": 4_000_000_000_u64
261+
});
262+
263+
let header = jsonwebtoken::Header::new(jsonwebtoken::Algorithm::EdDSA);
264+
let token = openshell_crypto::jwt::encode(&header, &claims, &signing).unwrap();
265+
266+
let mut validation = jsonwebtoken::Validation::new(jsonwebtoken::Algorithm::EdDSA);
267+
validation.set_issuer(&["https://oidc-discovery.example.com"]);
268+
validation.set_audience(&["model-api"]);
269+
270+
let err =
271+
openshell_crypto::jwt::decode::<serde_json::Value>(&token, &wrong_verifier, &validation)
272+
.unwrap_err();
273+
assert_eq!(
274+
err.kind(),
275+
&jsonwebtoken::errors::ErrorKind::InvalidSignature
276+
);
277+
}

0 commit comments

Comments
 (0)