Skip to content

Commit 9e8d367

Browse files
committed
wip: super clumsy fixes for no-std build
Slapping `cfg` gates all over the place as a stopgap fix. Fixing properly will require some reworking that will be done along other required changes.
1 parent c0bd6e6 commit 9e8d367

File tree

11 files changed

+106
-24
lines changed

11 files changed

+106
-24
lines changed

rustls/src/client/builder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ impl ConfigBuilder<ClientConfig, WantsClientCert> {
164164
#[cfg(feature = "tls12")]
165165
require_ems: cfg!(feature = "fips"),
166166
time_provider: self.state.time_provider,
167+
#[cfg(feature = "std")]
167168
grease_ech_hpke_provider: None,
169+
#[cfg(feature = "std")]
168170
ech_config: None,
169171
}
170172
}

rustls/src/client/client_conn.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use alloc::sync::Arc;
2+
#[cfg(feature = "std")]
23
use alloc::vec;
34
use alloc::vec::Vec;
45
use core::marker::PhantomData;
@@ -10,18 +11,25 @@ use pki_types::{ServerName, UnixTime};
1011
use super::handy::NoClientSessionStorage;
1112
use super::hs;
1213
use crate::builder::ConfigBuilder;
14+
#[cfg(feature = "std")]
1315
use crate::client::ech::EchState;
1416
use crate::common_state::{CommonState, Protocol, Side};
1517
use crate::conn::{ConnectionCore, UnbufferedConnectionCommon};
18+
#[cfg(feature = "std")]
1619
use crate::crypto::hpke::{HpkeProvider, HpkeSuite};
1720
use crate::crypto::{CryptoProvider, SupportedKxGroup};
1821
use crate::enums::{CipherSuite, ProtocolVersion, SignatureScheme};
1922
use crate::error::Error;
2023
#[cfg(feature = "logging")]
2124
use crate::log::trace;
25+
#[cfg(feature = "std")]
2226
use crate::msgs::codec::{Codec, Reader};
23-
use crate::msgs::enums::{EchVersion, NamedGroup};
24-
use crate::msgs::handshake::{ClientExtension, EchConfig as EchConfigMsg};
27+
#[cfg(feature = "std")]
28+
use crate::msgs::enums::EchVersion;
29+
use crate::msgs::enums::NamedGroup;
30+
use crate::msgs::handshake::ClientExtension;
31+
#[cfg(feature = "std")]
32+
use crate::msgs::handshake::EchConfig as EchConfigMsg;
2533
use crate::msgs::persist;
2634
use crate::suites::SupportedCipherSuite;
2735
#[cfg(feature = "std")]
@@ -209,8 +217,10 @@ pub struct ClientConfig {
209217

210218
/// Provides the current system time
211219
pub time_provider: Arc<dyn TimeProvider>,
220+
212221
/// When an HPKE provider is configured and no `ech_config` is provided, a GREASE
213222
/// ECH extension will be offered when negotiating TLS 1.3.
223+
#[cfg(feature = "std")]
214224
pub grease_ech_hpke_provider: Option<&'static dyn HpkeProvider>,
215225

216226
/// Source of randomness and other crypto.
@@ -224,6 +234,7 @@ pub struct ClientConfig {
224234
pub(super) verifier: Arc<dyn verify::ServerCertVerifier>,
225235

226236
/// How to offer Encrypted Client Hello (ECH). The default is to not offer ECH.
237+
#[cfg(feature = "std")]
227238
pub(super) ech_config: Option<EchConfig>,
228239
}
229240

@@ -344,6 +355,7 @@ impl ClientConfig {
344355
///
345356
/// If the client configuration has enabled TLS 1.2, this function will return an error. ECH
346357
/// may only be used with TLS 1.3+.
358+
#[cfg(feature = "std")]
347359
pub fn enable_ech(&mut self, config: EchConfig) -> Result<(), Error> {
348360
if self
349361
.versions
@@ -418,7 +430,9 @@ impl Clone for ClientConfig {
418430
#[cfg(feature = "tls12")]
419431
require_ems: self.require_ems,
420432
time_provider: Arc::clone(&self.time_provider),
433+
#[cfg(feature = "std")]
421434
grease_ech_hpke_provider: self.grease_ech_hpke_provider,
435+
#[cfg(feature = "std")]
422436
ech_config: self.ech_config.clone(),
423437
}
424438
}
@@ -510,6 +524,7 @@ pub enum Tls12Resumption {
510524
///
511525
/// Note: differs from the protocol-encoded EchConfig (`EchConfigMsg`).
512526
#[derive(Clone, Debug)]
527+
#[cfg(feature = "std")]
513528
pub struct EchConfig {
514529
/// The provider to use for HPKE operations.
515530
pub(crate) hpke_provider: &'static dyn HpkeProvider,
@@ -521,6 +536,7 @@ pub struct EchConfig {
521536
pub(crate) suite: HpkeSuite,
522537
}
523538

539+
#[cfg(feature = "std")]
524540
impl EchConfig {
525541
/// Construct an EchConfig by selecting a ECH config from the provided bytes that is compatible
526542
/// with the given HPKE provider.

rustls/src/client/ech.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use alloc::vec::Vec;
55
use pki_types::{DnsName, ServerName};
66
use subtle::ConstantTimeEq;
77

8+
#[cfg(feature = "std")]
89
use crate::client::EchConfig;
910
use crate::crypto::hash::Hash;
1011
use crate::crypto::hpke::{EncapsulatedSecret, HpkeProvider, HpkePublicKey, HpkeSealer, HpkeSuite};

rustls/src/client/hs.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::bs_debug;
1515
use crate::check::inappropriate_handshake_message;
1616
use crate::client::client_conn::ClientConnectionData;
1717
use crate::client::common::ClientHelloDetails;
18+
#[cfg(feature = "std")]
1819
use crate::client::ech::EchState;
1920
use crate::client::{tls13, ClientConfig};
2021
use crate::common_state::{CommonState, State};
@@ -146,6 +147,7 @@ pub(super) fn start_handshake(
146147
let random = Random::new(config.provider.secure_random)?;
147148
let extension_order_seed = crate::rand::random_u16(config.provider.secure_random)?;
148149

150+
#[cfg(feature = "std")]
149151
let ech_context = match config.ech_config.as_ref() {
150152
Some(ech_config) => Some(EchState::new(
151153
ech_config,
@@ -176,6 +178,7 @@ pub(super) fn start_handshake(
176178
server_name,
177179
},
178180
cx,
181+
#[cfg(feature = "std")]
179182
ech_context,
180183
)
181184
}
@@ -186,6 +189,7 @@ struct ExpectServerHello {
186189
early_key_schedule: Option<KeyScheduleEarly>,
187190
offered_key_share: Option<Box<dyn ActiveKeyExchange>>,
188191
suite: Option<SupportedCipherSuite>,
192+
#[cfg(feature = "std")]
189193
ech_context: Option<EchState>,
190194
}
191195

@@ -214,7 +218,7 @@ fn emit_client_hello_for_retry(
214218
suite: Option<SupportedCipherSuite>,
215219
mut input: ClientHelloInput,
216220
cx: &mut ClientContext<'_>,
217-
mut ech_context: Option<EchState>,
221+
#[cfg(feature = "std")] mut ech_context: Option<EchState>,
218222
) -> NextStateOrError<'static> {
219223
let config = &input.config;
220224
let support_tls12 = config.supports_version(ProtocolVersion::TLSv1_2) && !cx.common.is_quic();
@@ -266,12 +270,19 @@ fn emit_client_hello_for_retry(
266270
// We only want to send the SNI extension if the server name contains a DNS name and SNI is
267271
// enabled.
268272
if let (ServerName::DnsName(dns), true) = (&input.server_name, config.enable_sni) {
269-
// If we have an ECH context, then we need to use the ECH config's public name for the
270-
// outer hello SNI, otherwise we use the server name from the client config.
271-
exts.push(ClientExtension::make_sni(match ech_context.as_ref() {
272-
Some(ech_context) => &ech_context.outer_name,
273-
None => dns,
274-
}));
273+
#[cfg(feature = "std")]
274+
{
275+
// If we have an ECH context, then we need to use the ECH config's public name for the
276+
// outer hello SNI, otherwise we use the server name from the client config.
277+
exts.push(ClientExtension::make_sni(match ech_context.as_ref() {
278+
Some(ech_context) => &ech_context.outer_name,
279+
None => dns,
280+
}));
281+
}
282+
#[cfg(not(feature = "std"))]
283+
{
284+
exts.push(ClientExtension::make_sni(dns));
285+
}
275286
}
276287

277288
if let Some(key_share) = &key_share {
@@ -335,6 +346,7 @@ fn emit_client_hello_for_retry(
335346
// We don't do renegotiation at all, in fact.
336347
cipher_suites.push(CipherSuite::TLS_EMPTY_RENEGOTIATION_INFO_SCSV);
337348

349+
#[cfg_attr(not(feature = "std"), allow(unused_mut))]
338350
let mut chp_payload = ClientHelloPayload {
339351
client_version: ProtocolVersion::TLSv1_2,
340352
random: input.random,
@@ -344,6 +356,7 @@ fn emit_client_hello_for_retry(
344356
extensions: exts,
345357
};
346358

359+
#[cfg(feature = "std")]
347360
match (ech_context.as_mut(), config.grease_ech_hpke_provider) {
348361
// ECH config, GREASE is irrelevant.
349362
(Some(ech_context), _) => {
@@ -434,6 +447,7 @@ fn emit_client_hello_for_retry(
434447
early_key_schedule,
435448
offered_key_share: key_share,
436449
suite,
450+
#[cfg(feature = "std")]
437451
ech_context,
438452
};
439453

@@ -730,7 +744,9 @@ impl State<ClientConnectionData> for ExpectServerHello {
730744
// We always send a key share when TLS 1.3 is enabled.
731745
self.offered_key_share.unwrap(),
732746
self.input.sent_tls13_fake_ccs,
747+
#[cfg(feature = "std")]
733748
self.ech_context,
749+
#[cfg(feature = "std")]
734750
&m,
735751
)
736752
}
@@ -768,7 +784,7 @@ impl ExpectServerHelloOrHelloRetryRequest {
768784
}
769785

770786
fn handle_hello_retry_request(
771-
mut self,
787+
#[cfg_attr(not(feature = "std"), allow(unused_mut))] mut self,
772788
cx: &mut ClientContext<'_>,
773789
m: Message,
774790
) -> NextStateOrError<'static> {
@@ -893,6 +909,7 @@ impl ExpectServerHelloOrHelloRetryRequest {
893909
cx.common.suite = Some(cs);
894910

895911
// If we offered ECH, we need to confirm that the server accepted it.
912+
#[cfg(feature = "std")]
896913
match (self.next.ech_context.as_ref(), cs.tls13()) {
897914
(Some(_), None) => {
898915
unreachable!("ECH context should only be set when TLS 1.3 was negotiated")
@@ -913,6 +930,7 @@ impl ExpectServerHelloOrHelloRetryRequest {
913930

914931
// If we offered ECH, we also need to update the separate transcript with the
915932
// hello retry request message.
933+
#[cfg(feature = "std")]
916934
if let Some(ech_context) = self.next.ech_context.as_mut() {
917935
ech_context.transcript_hrr_update(cs.hash_provider(), &m);
918936
}
@@ -947,6 +965,7 @@ impl ExpectServerHelloOrHelloRetryRequest {
947965
Some(cs),
948966
self.next.input,
949967
cx,
968+
#[cfg(feature = "std")]
950969
self.next.ech_context,
951970
)
952971
}

rustls/src/client/tls13.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use super::client_conn::ClientConnectionData;
1010
use super::hs::ClientContext;
1111
use crate::check::inappropriate_handshake_message;
1212
use crate::client::common::{ClientAuthDetails, ClientHelloDetails, ServerCertDetails};
13-
use crate::client::ech::EchState;
14-
use crate::client::{ech, hs, ClientConfig, ClientSessionStore};
13+
#[cfg(feature = "std")]
14+
use crate::client::ech::{self, EchState};
15+
use crate::client::{hs, ClientConfig, ClientSessionStore};
1516
use crate::common_state::{CommonState, Protocol, Side, State};
1617
use crate::conn::ConnectionRandoms;
1718
use crate::crypto::ActiveKeyExchange;
@@ -65,15 +66,15 @@ pub(super) fn handle_server_hello(
6566
server_hello: &ServerHelloPayload,
6667
mut resuming_session: Option<persist::Tls13ClientSessionValue>,
6768
server_name: ServerName<'static>,
68-
mut randoms: ConnectionRandoms,
69+
#[cfg_attr(not(feature = "std"), allow(unused_mut))] mut randoms: ConnectionRandoms,
6970
suite: &'static Tls13CipherSuite,
70-
mut transcript: HandshakeHash,
71+
#[cfg_attr(not(feature = "std"), allow(unused_mut))] mut transcript: HandshakeHash,
7172
early_key_schedule: Option<KeyScheduleEarly>,
7273
hello: ClientHelloDetails,
7374
our_key_share: Box<dyn ActiveKeyExchange>,
7475
mut sent_tls13_fake_ccs: bool,
75-
ech_context: Option<EchState>,
76-
server_hello_msg: &Message,
76+
#[cfg(feature = "std")] ech_context: Option<EchState>,
77+
#[cfg(feature = "std")] server_hello_msg: &Message,
7778
) -> hs::NextStateOrError<'static> {
7879
validate_server_hello(cx.common, server_hello)?;
7980

@@ -148,6 +149,7 @@ pub(super) fn handle_server_hello(
148149

149150
let shared_secret = our_key_share.complete(&their_key_share.payload.0)?;
150151

152+
#[cfg_attr(not(feature = "std"), allow(unused_mut))]
151153
let mut key_schedule = key_schedule_pre_handshake.into_handshake(shared_secret);
152154

153155
// Remember what KX group the server liked for next time.
@@ -160,6 +162,7 @@ pub(super) fn handle_server_hello(
160162
// the two halves will have different record layer protections. Disallow this.
161163
cx.common.check_aligned_handshake()?;
162164

165+
#[cfg(feature = "std")]
163166
let ech_status = match ech_context {
164167
// ECH wasn't offered.
165168
None => ech::Status::NotOffered,
@@ -205,6 +208,7 @@ pub(super) fn handle_server_hello(
205208
transcript,
206209
key_schedule,
207210
hello,
211+
#[cfg(feature = "std")]
208212
ech_status,
209213
}))
210214
}
@@ -397,6 +401,7 @@ struct ExpectEncryptedExtensions {
397401
transcript: HandshakeHash,
398402
key_schedule: KeyScheduleHandshake,
399403
hello: ClientHelloDetails,
404+
#[cfg(feature = "std")]
400405
ech_status: ech::Status,
401406
}
402407

@@ -425,6 +430,7 @@ impl State<ClientConnectionData> for ExpectEncryptedExtensions {
425430
// this stage rather than in the ExpectServerHello state because we want to be able to
426431
// include retry configs that may be present in the server's encrypted extensions. This
427432
// also allows us to send the alert in encrypted form.
433+
#[cfg(feature = "std")]
428434
if matches!(self.ech_status, ech::Status::Rejected) {
429435
return Err(ech::fatal_alert_required(
430436
exts.server_ech_extension()

rustls/src/error.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use core::fmt;
66
use std::time::SystemTimeError;
77

88
use crate::enums::{AlertDescription, ContentType, HandshakeType};
9-
use crate::msgs::handshake::{EchConfig, KeyExchangeAlgorithm};
9+
#[cfg(feature = "std")]
10+
use crate::msgs::handshake::EchConfig;
11+
use crate::msgs::handshake::KeyExchangeAlgorithm;
1012
use crate::rand;
1113

1214
/// rustls reports protocol errors using this type.
@@ -275,6 +277,7 @@ pub enum PeerIncompatible {
275277
Tls12NotOfferedOrEnabled,
276278
Tls13RequiredForQuic,
277279
UncompressedEcPointsRequired,
280+
#[cfg(feature = "std")]
278281
ServerRejectedEncryptedClientHello(Option<Vec<EchConfig>>),
279282
}
280283

rustls/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,11 @@ pub mod internal {
441441
pub mod handshake {
442442
pub use crate::msgs::handshake::{
443443
CertificateChain, ClientExtension, ClientHelloPayload, DistinguishedName,
444-
EchConfig, EchConfigContents, HandshakeMessagePayload, HandshakePayload,
445-
HpkeKeyConfig, HpkeSymmetricCipherSuite, KeyShareEntry, Random, SessionId,
444+
HandshakeMessagePayload, HandshakePayload, HpkeKeyConfig, HpkeSymmetricCipherSuite,
445+
KeyShareEntry, Random, SessionId,
446446
};
447+
#[cfg(feature = "std")]
448+
pub use crate::msgs::handshake::{EchConfig, EchConfigContents};
447449
}
448450
pub mod message {
449451
pub use crate::msgs::message::{
@@ -534,6 +536,7 @@ pub mod client {
534536
pub(super) mod builder;
535537
mod client_conn;
536538
mod common;
539+
#[cfg(feature = "std")]
537540
mod ech;
538541
pub(super) mod handy;
539542
mod hs;
@@ -543,11 +546,11 @@ pub mod client {
543546

544547
pub use builder::WantsClientCert;
545548
pub use client_conn::{
546-
ClientConfig, ClientConnectionData, ClientSessionStore, EarlyDataError, EchConfig,
547-
ResolvesClientCert, Resumption, Tls12Resumption, UnbufferedClientConnection,
549+
ClientConfig, ClientConnectionData, ClientSessionStore, EarlyDataError, ResolvesClientCert,
550+
Resumption, Tls12Resumption, UnbufferedClientConnection,
548551
};
549552
#[cfg(feature = "std")]
550-
pub use client_conn::{ClientConnection, WriteEarlyData};
553+
pub use client_conn::{ClientConnection, EchConfig, WriteEarlyData};
551554
#[cfg(feature = "std")]
552555
pub use handy::ClientSessionMemoryCache;
553556

rustls/src/msgs/base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ impl fmt::Debug for PayloadU16 {
152152
pub struct PayloadU8(pub(crate) Vec<u8>);
153153

154154
impl PayloadU8 {
155+
#[cfg(feature = "std")]
155156
pub(crate) fn encode_slice(slice: &[u8], bytes: &mut Vec<u8>) {
156157
(slice.len() as u8).encode(bytes);
157158
bytes.extend_from_slice(slice);

0 commit comments

Comments
 (0)