ant_quic/crypto.rs
1//! Traits and implementations for the QUIC cryptography protocol
2#![allow(rustdoc::bare_urls)]
3//!
4//! The protocol logic in Quinn is contained in types that abstract over the actual
5//! cryptographic protocol used. This module contains the traits used for this
6//! abstraction layer as well as a single implementation of these traits that uses
7//! *ring* and rustls to implement the TLS protocol support.
8//!
9//! Note that usage of any protocol (version) other than TLS 1.3 does not conform to any
10//! published versions of the specification, and will not be supported in QUIC v1.
11
12use std::{any::Any, str, sync::Arc};
13
14use bytes::BytesMut;
15
16use crate::{
17 ConnectError, Side, TransportError, shared::ConnectionId,
18 transport_parameters::TransportParameters,
19};
20
21/// Cryptography interface based on *ring*
22#[cfg(any(feature = "aws-lc-rs", feature = "ring"))]
23pub(crate) mod ring_like;
24/// TLS interface based on rustls
25#[cfg(any(feature = "rustls-aws-lc-rs", feature = "rustls-ring"))]
26pub mod rustls;
27
28/// Certificate management
29#[cfg(any(feature = "rustls-aws-lc-rs", feature = "rustls-ring"))]
30pub mod certificate_manager;
31
32/// RFC 7250 Raw Public Keys support
33#[cfg(any(feature = "rustls-aws-lc-rs", feature = "rustls-ring"))]
34pub mod raw_public_keys;
35
36/// Ed25519 key pair implementation
37pub mod raw_keys;
38
39/// Post-Quantum Cryptography support
40#[cfg(feature = "pqc")]
41pub mod pqc;
42
43// NOTE: The following modules were removed because they were written as external
44// integrations with Quinn, but ant-quic IS a fork of Quinn, not something that
45// integrates with it. These need to be rewritten as part of the Quinn implementation
46// if their functionality is needed.
47
48// Removed modules:
49// - rpk_integration (tried to integrate RPK with Quinn from outside)
50// - quinn_integration (tried to wrap Quinn endpoints)
51// - bootstrap_support (tried to add bootstrap support on top of Quinn)
52// - peer_discovery (distributed discovery layered on Quinn)
53// - enterprise_cert_mgmt (enterprise features added on top)
54// - performance_monitoring (monitoring Quinn from outside)
55// - performance_optimization (optimizing Quinn externally)
56// - zero_rtt_rpk (0-RTT features added on top)
57// - nat_rpk_integration (NAT traversal integration)
58
59/// TLS Extensions for RFC 7250 certificate type negotiation
60pub mod tls_extensions;
61
62/// TLS Extension Simulation for RFC 7250 Raw Public Keys
63pub mod tls_extension_simulation;
64
65/// rustls Extension Handlers for certificate type negotiation
66pub mod extension_handlers;
67
68/// Certificate Type Negotiation Protocol Implementation
69pub mod certificate_negotiation;
70
71/// Test module for TLS extension simulation
72#[cfg(test)]
73mod test_tls_simulation;
74
75/// A cryptographic session (commonly TLS)
76pub trait Session: Send + Sync + 'static {
77 /// Create the initial set of keys given the client's initial destination ConnectionId
78 fn initial_keys(&self, dst_cid: &ConnectionId, side: Side) -> Keys;
79
80 /// Get data negotiated during the handshake, if available
81 ///
82 /// Returns `None` until the connection emits `HandshakeDataReady`.
83 fn handshake_data(&self) -> Option<Box<dyn Any>>;
84
85 /// Get the peer's identity, if available
86 fn peer_identity(&self) -> Option<Box<dyn Any>>;
87
88 /// Get the 0-RTT keys if available (clients only)
89 ///
90 /// On the client side, this method can be used to see if 0-RTT key material is available
91 /// to start sending data before the protocol handshake has completed.
92 ///
93 /// Returns `None` if the key material is not available. This might happen if you have
94 /// not connected to this server before.
95 fn early_crypto(&self) -> Option<(Box<dyn HeaderKey>, Box<dyn PacketKey>)>;
96
97 /// If the 0-RTT-encrypted data has been accepted by the peer
98 fn early_data_accepted(&self) -> Option<bool>;
99
100 /// Returns `true` until the connection is fully established.
101 fn is_handshaking(&self) -> bool;
102
103 /// Read bytes of handshake data
104 ///
105 /// This should be called with the contents of `CRYPTO` frames. If it returns `Ok`, the
106 /// caller should call `write_handshake()` to check if the crypto protocol has anything
107 /// to send to the peer. This method will only return `true` the first time that
108 /// handshake data is available. Future calls will always return false.
109 ///
110 /// On success, returns `true` iff `self.handshake_data()` has been populated.
111 fn read_handshake(&mut self, buf: &[u8]) -> Result<bool, TransportError>;
112
113 /// The peer's QUIC transport parameters
114 ///
115 /// These are only available after the first flight from the peer has been received.
116 fn transport_parameters(&self) -> Result<Option<TransportParameters>, TransportError>;
117
118 /// Writes handshake bytes into the given buffer and optionally returns the negotiated keys
119 ///
120 /// When the handshake proceeds to the next phase, this method will return a new set of
121 /// keys to encrypt data with.
122 fn write_handshake(&mut self, buf: &mut Vec<u8>) -> Option<Keys>;
123
124 /// Compute keys for the next key update
125 fn next_1rtt_keys(&mut self) -> Option<KeyPair<Box<dyn PacketKey>>>;
126
127 /// Verify the integrity of a retry packet
128 fn is_valid_retry(&self, orig_dst_cid: &ConnectionId, header: &[u8], payload: &[u8]) -> bool;
129
130 /// Fill `output` with `output.len()` bytes of keying material derived
131 /// from the [Session]'s secrets, using `label` and `context` for domain
132 /// separation.
133 ///
134 /// This function will fail, returning [ExportKeyingMaterialError],
135 /// if the requested output length is too large.
136 fn export_keying_material(
137 &self,
138 output: &mut [u8],
139 label: &[u8],
140 context: &[u8],
141 ) -> Result<(), ExportKeyingMaterialError>;
142}
143
144/// A pair of keys for bidirectional communication
145pub struct KeyPair<T> {
146 /// Key for encrypting data
147 pub local: T,
148 /// Key for decrypting data
149 pub remote: T,
150}
151
152/// A complete set of keys for a certain packet space
153pub struct Keys {
154 /// Header protection keys
155 pub header: KeyPair<Box<dyn HeaderKey>>,
156 /// Packet protection keys
157 pub packet: KeyPair<Box<dyn PacketKey>>,
158}
159
160/// Client-side configuration for the crypto protocol
161pub trait ClientConfig: Send + Sync {
162 /// Start a client session with this configuration
163 fn start_session(
164 self: Arc<Self>,
165 version: u32,
166 server_name: &str,
167 params: &TransportParameters,
168 ) -> Result<Box<dyn Session>, ConnectError>;
169}
170
171/// Server-side configuration for the crypto protocol
172pub trait ServerConfig: Send + Sync {
173 /// Create the initial set of keys given the client's initial destination ConnectionId
174 fn initial_keys(
175 &self,
176 version: u32,
177 dst_cid: &ConnectionId,
178 ) -> Result<Keys, UnsupportedVersion>;
179
180 /// Generate the integrity tag for a retry packet
181 ///
182 /// Never called if `initial_keys` rejected `version`.
183 fn retry_tag(&self, version: u32, orig_dst_cid: &ConnectionId, packet: &[u8]) -> [u8; 16];
184
185 /// Start a server session with this configuration
186 ///
187 /// Never called if `initial_keys` rejected `version`.
188 fn start_session(
189 self: Arc<Self>,
190 version: u32,
191 params: &TransportParameters,
192 ) -> Box<dyn Session>;
193}
194
195/// Keys used to protect packet payloads
196pub trait PacketKey: Send + Sync {
197 /// Encrypt the packet payload with the given packet number
198 fn encrypt(&self, packet: u64, buf: &mut [u8], header_len: usize);
199 /// Decrypt the packet payload with the given packet number
200 fn decrypt(
201 &self,
202 packet: u64,
203 header: &[u8],
204 payload: &mut BytesMut,
205 ) -> Result<(), CryptoError>;
206 /// The length of the AEAD tag appended to packets on encryption
207 fn tag_len(&self) -> usize;
208 /// Maximum number of packets that may be sent using a single key
209 fn confidentiality_limit(&self) -> u64;
210 /// Maximum number of incoming packets that may fail decryption before the connection must be
211 /// abandoned
212 fn integrity_limit(&self) -> u64;
213}
214
215/// Keys used to protect packet headers
216pub trait HeaderKey: Send + Sync {
217 /// Decrypt the given packet's header
218 fn decrypt(&self, pn_offset: usize, packet: &mut [u8]);
219 /// Encrypt the given packet's header
220 fn encrypt(&self, pn_offset: usize, packet: &mut [u8]);
221 /// The sample size used for this key's algorithm
222 fn sample_size(&self) -> usize;
223}
224
225/// A key for signing with HMAC-based algorithms
226pub trait HmacKey: Send + Sync {
227 /// Method for signing a message
228 fn sign(&self, data: &[u8], signature_out: &mut [u8]);
229 /// Length of `sign`'s output
230 fn signature_len(&self) -> usize;
231 /// Method for verifying a message
232 fn verify(&self, data: &[u8], signature: &[u8]) -> Result<(), CryptoError>;
233}
234
235/// Error returned by [Session::export_keying_material].
236///
237/// This error occurs if the requested output length is too large.
238#[derive(Debug, PartialEq, Eq)]
239pub struct ExportKeyingMaterialError;
240
241/// A pseudo random key for HKDF
242pub trait HandshakeTokenKey: Send + Sync {
243 /// Derive AEAD using hkdf
244 fn aead_from_hkdf(&self, random_bytes: &[u8]) -> Box<dyn AeadKey>;
245}
246
247/// A key for sealing data with AEAD-based algorithms
248pub trait AeadKey {
249 /// Method for sealing message `data`
250 fn seal(&self, data: &mut Vec<u8>, additional_data: &[u8]) -> Result<(), CryptoError>;
251 /// Method for opening a sealed message `data`
252 fn open<'a>(
253 &self,
254 data: &'a mut [u8],
255 additional_data: &[u8],
256 ) -> Result<&'a mut [u8], CryptoError>;
257}
258
259/// Generic crypto errors
260#[derive(Debug)]
261pub struct CryptoError;
262
263/// Error indicating that the specified QUIC version is not supported
264#[derive(Debug)]
265pub struct UnsupportedVersion;
266
267impl From<UnsupportedVersion> for ConnectError {
268 fn from(_: UnsupportedVersion) -> Self {
269 Self::UnsupportedVersion
270 }
271}