Skip to content

Commit ac289aa

Browse files
authored
perf(pricing): give the pricing refresh a verify-only TLS 1.3 rustls provider (ccusage#1927)
* perf(pricing): give rustls a verify-only crypto provider ureq's `rustls` feature installs rustls' own ring provider, whose key loader keeps ring's RSA, ECDSA and Ed25519 signing code and PKCS#8 parsing in the binary. The pricing refresh only verifies servers and never presents a client certificate, so none of that is reachable in practice, but LTO cannot prove it. The binary now enables ureq's `rustls-no-provider` and `rustls-webpki-roots` features instead and hands ureq a provider built from the same ring cipher suites, key exchange groups and signature verification algorithms, with system randomness from ring and a key loader that always fails. Negotiation and certificate verification are unchanged. On aarch64-darwin the release binary's sections shrink by 35,090 bytes (3,635,724 -> 3,600,634); the file shrinks by 66,192 bytes because the __TEXT segment drops a page. * perf(pricing): offer only TLS 1.3 cipher suites raw.githubusercontent.com and models.dev both negotiate TLS 1.3 (checked with `openssl s_client -tls1_3`; both pick X25519 and a TLS 1.3 AEAD suite), so the six TLS 1.2 suites only keep their key schedule, PRF and AEAD glue in the binary. The provider now lists the three TLS 1.3 suites alone, which also means rustls cannot fall back to TLS 1.2. The one setup that could need TLS 1.2 is a TLS-intercepting middlebox, and those already fail certificate verification because the client trusts only the embedded web PKI roots. CONNECT proxies tunnel the origin's own handshake and are unaffected. ureq still enables rustls' `tls12` feature, so the TLS 1.2 handshake state machine stays compiled in for now. Release binary sections on aarch64-darwin: 3,600,634 -> 3,593,067 (-7,567 bytes). * perf(pricing): offer only X25519 for key exchange Both pricing hosts select X25519 when it is offered, so the P-256 and P-384 ECDH groups only add their key generation and agreement glue. The provider now offers X25519 alone. ring's P-256 and P-384 arithmetic and its precomputed P-256 table stay: certificate verification still needs them for the ECDSA chain models.dev serves (WE1 -> GTS Root R4). Release binary sections on aarch64-darwin: 3,593,067 -> 3,586,978 (-6,089 bytes). * perf(pricing): stop verifying Ed25519 signatures The CA/Browser Forum Baseline Requirements only permit RSA and ECDSA subscriber and CA keys, and none of the embedded web PKI roots is an Ed25519 key, so no chain the pricing client can accept and no TLS 1.3 CertificateVerify from such a chain uses Ed25519. Dropping it from the verification algorithms lets LTO discard ring's Ed25519 verifier. The X25519 base-point table stays, since key exchange still uses it. Release binary sections on aarch64-darwin: 3,586,978 -> 3,579,431 (-7,547 bytes); the file drops another 16 KiB page. * docs(pricing): correct the TLS 1.2 note for https proxies * docs(ccusage): describe the pricing HTTP client and its dependencies The ccusage README now lists http.rs and http/ and the ureq, rustls, ring and rustls-webpki dependencies it pulls in. ccusage-core's list drops ureq, which it has not depended on since the client moved into the binary, and gains miniz_oxide, which it does use.
1 parent 8fe3f47 commit ac289aa

7 files changed

Lines changed: 195 additions & 3 deletions

File tree

‎rust/Cargo.lock‎

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

‎rust/Cargo.toml‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ memchr = "2.8.3"
5858
miniz_oxide = { version = "0.9.1", default-features = false, features = ["with-alloc"] }
5959
mimalloc = "0.1.52"
6060
minreq = { version = "3.0.0", default-features = false, features = ["https-rustls"] }
61+
# The pricing client's crypto: rustls over ring, with the provider assembled in
62+
# the binary (rust/crates/ccusage/src/http/tls.rs) rather than rustls' own.
63+
ring = { version = "0.17.14", default-features = false }
6164
rustc-hash = "2.1.3"
65+
rustls = { version = "0.23.45", default-features = false, features = ["ring", "std"] }
66+
rustls-webpki = { version = "0.103.15", default-features = false, features = ["ring"] }
6267
schemars = "0.8"
6368
serde = { version = "1.0.229", features = ["derive"] }
6469
serde_json = "1.0.154"
@@ -67,7 +72,11 @@ sqlite = { version = "0.37.4", default-features = false, features = ["bundled",
6772
terminal_size = "0.4.4"
6873
toml = { version = "1.1.8", default-features = false, features = ["parse", "serde", "std"] }
6974
unicode-width = "0.2.2"
70-
ureq = { version = "3.4.2", default-features = false, features = ["rustls", "gzip"] }
75+
ureq = { version = "3.4.2", default-features = false, features = [
76+
"gzip",
77+
"rustls-no-provider",
78+
"rustls-webpki-roots",
79+
] }
7180
zstd = { version = "0.14.2", default-features = false }
7281

7382
[profile.release]

‎rust/crates/ccusage-core/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ instead, which falls back to that crate when nothing was installed.
3737
- `ccusage-terminal`
3838
- `jiff`
3939
- `memchr`
40+
- `miniz_oxide`
4041
- `rustc-hash`
4142
- `serde`
4243
- `serde_json`
4344
- `smallvec`
44-
- `ureq`
4545
- `ccusage-pricing-snapshots` — optional, behind the test-only `test-snapshots`
4646
feature
4747

‎rust/crates/ccusage/Cargo.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ ccusage-cli.workspace = true
4040
ccusage-config.workspace = true
4141
ccusage-core.workspace = true
4242
ccusage-pricing-snapshots.workspace = true
43+
ring.workspace = true
44+
rustls.workspace = true
45+
rustls-webpki.workspace = true
4346
serde.workspace = true
4447
serde_json.workspace = true
4548
ureq.workspace = true

‎rust/crates/ccusage/README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ source in its own `ccusage-adapter-*` crate.
1313
`blocks` and `statusline`.
1414
- `adapter/` — the thin aliases that map each `ccusage <agent>` subcommand to its
1515
adapter crate.
16+
- `http.rs` and `http/` — the pricing refresh's HTTP client, installed into
17+
`ccusage-core` at startup: ureq with an ETag cache and 304 revalidation, over a
18+
verify-only rustls provider (TLS 1.3, X25519, RSA and ECDSA verification).
19+
Keeping it here keeps ureq, rustls and ring out of the crates every adapter builds.
1620
- `bin/generate_config_schema.rs` — writes `apps/ccusage/config-schema.json`; the
1721
`config-schema` flake check fails when the committed file drifts.
1822

@@ -42,8 +46,12 @@ source in its own `ccusage-adapter-*` crate.
4246
- `ccusage-config`
4347
- `ccusage-core`
4448
- `ccusage-pricing-snapshots`
49+
- `ring`
50+
- `rustls`
51+
- `rustls-webpki`
4552
- `serde`
4653
- `serde_json`
54+
- `ureq`
4755

4856
## Build layer
4957

‎rust/crates/ccusage/src/http.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ use std::{
22
fs,
33
io::{self, Read},
44
path::{Path, PathBuf},
5-
sync::atomic::{AtomicU64, Ordering},
5+
sync::{
6+
Arc,
7+
atomic::{AtomicU64, Ordering},
8+
},
69
time::Duration,
710
};
811

912
use ccusage_core::pricing::PricingEndpoint;
1013

14+
mod tls;
15+
1116
const PRICING_FETCH_TIMEOUT_SECONDS: u64 = 10;
1217
const PRICING_FETCH_MAX_BYTES: u64 = 64 * 1024 * 1024;
1318
const CACHE_ETAG_MAX_BYTES: u64 = 4096;
@@ -74,6 +79,11 @@ fn fetch_json_with_cache_dir_inner(
7479

7580
let agent = ureq::Agent::config_builder()
7681
.timeout_global(Some(Duration::from_secs(PRICING_FETCH_TIMEOUT_SECONDS)))
82+
.tls_config(
83+
ureq::tls::TlsConfig::builder()
84+
.unversioned_rustls_crypto_provider(Arc::new(tls::provider()))
85+
.build(),
86+
)
7787
.build()
7888
.new_agent();
7989
let mut request = agent.get(url);
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
//! The rustls crypto provider for the pricing refresh.
2+
//!
3+
//! rustls' own `ring` provider also carries everything a TLS server or a client
4+
//! presenting a certificate needs, and its key loader keeps ring's RSA, ECDSA,
5+
//! and Ed25519 signing code in the binary. The refresh only ever verifies a
6+
//! server, so this provider is assembled from the same ring primitives without
7+
//! a way to load private keys.
8+
9+
use std::sync::Arc;
10+
11+
use rustls::{
12+
SignatureScheme,
13+
crypto::{
14+
CryptoProvider, GetRandomFailed, KeyProvider, SecureRandom, WebPkiSupportedAlgorithms, ring,
15+
},
16+
pki_types::PrivateKeyDer,
17+
sign::SigningKey,
18+
};
19+
use webpki::ring as algs;
20+
21+
/// Builds the provider the pricing client hands to rustls.
22+
///
23+
/// Only TLS 1.3 suites are offered: both pricing hosts negotiate TLS 1.3, and
24+
/// the TLS 1.2 suites would keep their key schedule and AEAD glue in the binary.
25+
/// TLS-intercepting middleboxes already failed, because the client trusts no
26+
/// root beyond the embedded web PKI ones; an `https://` proxy with a publicly
27+
/// trusted certificate now needs TLS 1.3 as well.
28+
///
29+
/// X25519 is the only key exchange group, which both hosts pick on their own;
30+
/// the NIST curves stay for certificate signatures, not for key agreement.
31+
pub(super) fn provider() -> CryptoProvider {
32+
CryptoProvider {
33+
cipher_suites: vec![
34+
ring::cipher_suite::TLS13_AES_256_GCM_SHA384,
35+
ring::cipher_suite::TLS13_AES_128_GCM_SHA256,
36+
ring::cipher_suite::TLS13_CHACHA20_POLY1305_SHA256,
37+
],
38+
kx_groups: vec![ring::kx_group::X25519],
39+
signature_verification_algorithms: SIGNATURE_ALGORITHMS,
40+
secure_random: &VerifyOnly,
41+
key_provider: &VerifyOnly,
42+
}
43+
}
44+
45+
/// The signature algorithms rustls' ring provider verifies, in its order, minus
46+
/// Ed25519: the CA/Browser Forum Baseline Requirements allow only RSA and ECDSA
47+
/// keys, so no publicly trusted chain or TLS 1.3 server signature can use it.
48+
static SIGNATURE_ALGORITHMS: WebPkiSupportedAlgorithms = WebPkiSupportedAlgorithms {
49+
all: &[
50+
algs::ECDSA_P256_SHA256,
51+
algs::ECDSA_P256_SHA384,
52+
algs::ECDSA_P384_SHA256,
53+
algs::ECDSA_P384_SHA384,
54+
algs::RSA_PSS_2048_8192_SHA256_LEGACY_KEY,
55+
algs::RSA_PSS_2048_8192_SHA384_LEGACY_KEY,
56+
algs::RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
57+
algs::RSA_PKCS1_2048_8192_SHA256,
58+
algs::RSA_PKCS1_2048_8192_SHA384,
59+
algs::RSA_PKCS1_2048_8192_SHA512,
60+
algs::RSA_PKCS1_2048_8192_SHA256_ABSENT_PARAMS,
61+
algs::RSA_PKCS1_2048_8192_SHA384_ABSENT_PARAMS,
62+
algs::RSA_PKCS1_2048_8192_SHA512_ABSENT_PARAMS,
63+
],
64+
mapping: &[
65+
// TLS 1.3 fixes the curve per scheme; TLS 1.2 does not, hence two entries.
66+
(
67+
SignatureScheme::ECDSA_NISTP384_SHA384,
68+
&[algs::ECDSA_P384_SHA384, algs::ECDSA_P256_SHA384],
69+
),
70+
(
71+
SignatureScheme::ECDSA_NISTP256_SHA256,
72+
&[algs::ECDSA_P256_SHA256, algs::ECDSA_P384_SHA256],
73+
),
74+
(
75+
SignatureScheme::RSA_PSS_SHA512,
76+
&[algs::RSA_PSS_2048_8192_SHA512_LEGACY_KEY],
77+
),
78+
(
79+
SignatureScheme::RSA_PSS_SHA384,
80+
&[algs::RSA_PSS_2048_8192_SHA384_LEGACY_KEY],
81+
),
82+
(
83+
SignatureScheme::RSA_PSS_SHA256,
84+
&[algs::RSA_PSS_2048_8192_SHA256_LEGACY_KEY],
85+
),
86+
(
87+
SignatureScheme::RSA_PKCS1_SHA512,
88+
&[algs::RSA_PKCS1_2048_8192_SHA512],
89+
),
90+
(
91+
SignatureScheme::RSA_PKCS1_SHA384,
92+
&[algs::RSA_PKCS1_2048_8192_SHA384],
93+
),
94+
(
95+
SignatureScheme::RSA_PKCS1_SHA256,
96+
&[algs::RSA_PKCS1_2048_8192_SHA256],
97+
),
98+
],
99+
};
100+
101+
/// Supplies ring's system randomness and refuses to load private keys.
102+
#[derive(Debug)]
103+
struct VerifyOnly;
104+
105+
impl SecureRandom for VerifyOnly {
106+
fn fill(&self, buf: &mut [u8]) -> Result<(), GetRandomFailed> {
107+
use ::ring::rand::SecureRandom as _;
108+
109+
::ring::rand::SystemRandom::new()
110+
.fill(buf)
111+
.map_err(|_| GetRandomFailed)
112+
}
113+
}
114+
115+
impl KeyProvider for VerifyOnly {
116+
fn load_private_key(
117+
&self,
118+
_key_der: PrivateKeyDer<'static>,
119+
) -> Result<Arc<dyn SigningKey>, rustls::Error> {
120+
Err(rustls::Error::General(
121+
"the pricing client does not present certificates".to_string(),
122+
))
123+
}
124+
}
125+
126+
#[cfg(test)]
127+
mod tests {
128+
use std::sync::Arc;
129+
130+
use rustls::{ClientConfig, pki_types::PrivateKeyDer};
131+
132+
use super::provider;
133+
134+
#[test]
135+
fn provider_builds_a_client_config_but_loads_no_keys() {
136+
let provider = provider();
137+
assert!(
138+
provider
139+
.key_provider
140+
.load_private_key(PrivateKeyDer::Pkcs8(Vec::new().into()))
141+
.is_err()
142+
);
143+
assert!(
144+
ClientConfig::builder_with_provider(Arc::new(provider))
145+
.with_safe_default_protocol_versions()
146+
.is_ok(),
147+
"rustls must accept the provider"
148+
);
149+
}
150+
151+
#[test]
152+
fn provider_cannot_negotiate_tls_1_2() {
153+
assert!(
154+
ClientConfig::builder_with_provider(Arc::new(provider()))
155+
.with_protocol_versions(&[&rustls::version::TLS12])
156+
.is_err()
157+
);
158+
}
159+
}

0 commit comments

Comments
 (0)