Skip to content

Commit 7152a0c

Browse files
refactor(s2n-quic-core): break CryptoError up into tls::Error and packet_protection::Error (#2113)
* refactor(s2n-quic-core): break CryptoError up into tls::Error and packet_protection::Error * remove `with_reason`
1 parent eeea4dc commit 7152a0c

File tree

33 files changed

+199
-167
lines changed

33 files changed

+199
-167
lines changed

quic/s2n-quic-core/src/connection/close.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::{application, crypto, transport};
4+
use crate::{application, crypto::tls, transport};
55
pub use crate::{frame::ConnectionClose, inet::SocketAddress};
66

77
/// Provides a hook for applications to rewrite CONNECTION_CLOSE frames
@@ -116,8 +116,8 @@ impl Formatter for Production {
116116
//# includes replacing any alert with a generic alert, such as
117117
//# handshake_failure (0x0128 in QUIC). Endpoints MAY use a generic
118118
//# error code to avoid possibly exposing confidential information.
119-
if error.try_into_crypto_error().is_some() {
120-
return transport::Error::from(crypto::CryptoError::HANDSHAKE_FAILURE).into();
119+
if error.try_into_tls_error().is_some() {
120+
return transport::Error::from(tls::Error::HANDSHAKE_FAILURE).into();
121121
}
122122

123123
// only preserve the error code

quic/s2n-quic-core/src/connection/error.rs

+6-22
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::{
5-
application, connection, crypto::CryptoError, endpoint, frame::ConnectionClose, transport,
5+
application, connection, crypto::packet_protection, endpoint, frame::ConnectionClose, transport,
66
};
77
use core::{convert::TryInto, fmt, panic, time::Duration};
88

@@ -469,13 +469,6 @@ impl From<transport::Error> for Error {
469469
}
470470
}
471471

472-
impl From<CryptoError> for Error {
473-
#[track_caller]
474-
fn from(error: CryptoError) -> Self {
475-
transport::Error::from(error).into()
476-
}
477-
}
478-
479472
impl<'a> From<ConnectionClose<'a>> for Error {
480473
#[track_caller]
481474
fn from(error: ConnectionClose) -> Self {
@@ -530,7 +523,7 @@ impl From<Error> for std::io::ErrorKind {
530523
}
531524
}
532525

533-
/// Some connection methods may need to indicate both `TransportError`s and `CryptoError`s. This
526+
/// Some connection methods may need to indicate both `ConnectionError`s and `DecryptError`s. This
534527
/// enum is used to allow for either error type to be returned as appropriate.
535528
#[derive(Clone, Copy, Debug, PartialEq)]
536529
pub enum ProcessingError {
@@ -548,21 +541,12 @@ impl From<Error> for ProcessingError {
548541
impl From<crate::transport::Error> for ProcessingError {
549542
#[track_caller]
550543
fn from(inner_error: crate::transport::Error) -> Self {
551-
// Try extracting out the decrypt error from other transport errors
552-
if let Some(error) = inner_error.try_into_crypto_error() {
553-
error.into()
554-
} else {
555-
Self::ConnectionError(inner_error.into())
556-
}
544+
Self::ConnectionError(inner_error.into())
557545
}
558546
}
559547

560-
impl From<CryptoError> for ProcessingError {
561-
fn from(inner_error: CryptoError) -> Self {
562-
if inner_error.code == CryptoError::DECRYPT_ERROR.code {
563-
Self::DecryptError
564-
} else {
565-
Self::ConnectionError(inner_error.into())
566-
}
548+
impl From<packet_protection::Error> for ProcessingError {
549+
fn from(_: packet_protection::Error) -> Self {
550+
Self::DecryptError
567551
}
568552
}

quic/s2n-quic-core/src/crypto/application/keyset.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<K: OneRttKey> KeySet<K> {
203203
return Err(transport::Error::AEAD_LIMIT_REACHED.into());
204204
}
205205

206-
Err(err.into())
206+
Err(err)
207207
}
208208
}
209209
}

quic/s2n-quic-core/src/crypto/key.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::crypto::CryptoError;
4+
use crate::crypto::packet_protection;
55
use s2n_codec::encoder::scatter;
66

77
/// A trait for crypto keys
@@ -12,15 +12,15 @@ pub trait Key: Send {
1212
packet_number: u64,
1313
header: &[u8],
1414
payload: &mut [u8],
15-
) -> Result<(), CryptoError>;
15+
) -> Result<(), packet_protection::Error>;
1616

1717
/// Encrypt a payload
1818
fn encrypt(
1919
&self,
2020
packet_number: u64,
2121
header: &[u8],
2222
payload: &mut scatter::Buffer,
23-
) -> Result<(), CryptoError>;
23+
) -> Result<(), packet_protection::Error>;
2424

2525
/// Length of the appended tag
2626
fn tag_len(&self) -> usize;
@@ -37,8 +37,9 @@ pub trait Key: Send {
3737
#[cfg(any(test, feature = "testing"))]
3838
pub mod testing {
3939
use crate::crypto::{
40+
packet_protection,
4041
retry::{IntegrityTag, INTEGRITY_TAG_LEN},
41-
scatter, CryptoError, HandshakeHeaderKey, HandshakeKey, HeaderKey as CryptoHeaderKey,
42+
scatter, HandshakeHeaderKey, HandshakeKey, HeaderKey as CryptoHeaderKey,
4243
HeaderProtectionMask, InitialHeaderKey, InitialKey, OneRttHeaderKey, OneRttKey, RetryKey,
4344
ZeroRttHeaderKey, ZeroRttKey,
4445
};
@@ -77,9 +78,9 @@ pub mod testing {
7778
_packet_number: u64,
7879
_header: &[u8],
7980
_payload: &mut [u8],
80-
) -> Result<(), CryptoError> {
81+
) -> Result<(), packet_protection::Error> {
8182
if self.fail_on_decrypt {
82-
return Err(CryptoError::DECRYPT_ERROR);
83+
return Err(packet_protection::Error::DECRYPT_ERROR);
8384
}
8485

8586
Ok(())
@@ -91,7 +92,7 @@ pub mod testing {
9192
_packet_number: u64,
9293
_header: &[u8],
9394
payload: &mut scatter::Buffer,
94-
) -> Result<(), CryptoError> {
95+
) -> Result<(), packet_protection::Error> {
9596
// copy any bytes into the final slice
9697
payload.flatten();
9798
Ok(())
@@ -145,7 +146,7 @@ pub mod testing {
145146
fn generate_tag(_payload: &[u8]) -> IntegrityTag {
146147
[0u8; INTEGRITY_TAG_LEN]
147148
}
148-
fn validate(_payload: &[u8], _tag: IntegrityTag) -> Result<(), CryptoError> {
149+
fn validate(_payload: &[u8], _tag: IntegrityTag) -> Result<(), packet_protection::Error> {
149150
Ok(())
150151
}
151152
}

quic/s2n-quic-core/src/crypto/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@
132132
//!
133133
134134
pub mod application;
135-
pub mod error;
136135
pub mod handshake;
137136
pub mod header_crypto;
138137
pub mod initial;
@@ -149,7 +148,6 @@ pub mod zero_rtt;
149148
mod tests;
150149

151150
pub use application::*;
152-
pub use error::*;
153151
pub use handshake::*;
154152
pub use header_crypto::*;
155153
pub use initial::*;
@@ -213,7 +211,7 @@ pub fn encrypt<'a, K: Key>(
213211
packet_number_len: PacketNumberLen,
214212
header_len: usize,
215213
payload: scatter::Buffer<'a>,
216-
) -> Result<(EncryptedPayload<'a>, EncoderBuffer<'a>), CryptoError> {
214+
) -> Result<(EncryptedPayload<'a>, EncoderBuffer<'a>), packet_protection::Error> {
217215
let header_with_pn_len = packet_number_len.bytesize() + header_len;
218216

219217
let (mut payload, extra) = payload.into_inner();
@@ -254,7 +252,7 @@ pub fn decrypt<'a, K: Key>(
254252
key: &K,
255253
packet_number: PacketNumber,
256254
payload: EncryptedPayload<'a>,
257-
) -> Result<(DecoderBufferMut<'a>, DecoderBufferMut<'a>), CryptoError> {
255+
) -> Result<(DecoderBufferMut<'a>, DecoderBufferMut<'a>), packet_protection::Error> {
258256
let (header, payload) = payload.split_mut();
259257
key.decrypt(packet_number.as_crypto_nonce(), header, payload)?;
260258

quic/s2n-quic-core/src/crypto/packet_protection.rs

+52
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,55 @@ pub const QUIC_IV_LABEL: [u8; 7] = *b"quic iv";
1414
//= https://www.rfc-editor.org/rfc/rfc9001#section-5.1
1515
//# The header protection key uses the "quic hp" label; see Section 5.4.
1616
pub const QUIC_HP_LABEL: [u8; 7] = *b"quic hp";
17+
18+
use core::fmt;
19+
use s2n_codec::DecoderError;
20+
21+
/// Error type for errors during removal of packet protection
22+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
23+
#[cfg_attr(feature = "thiserror", derive(thiserror::Error))]
24+
pub struct Error {
25+
pub reason: &'static str,
26+
}
27+
28+
impl Error {
29+
pub const DECODE_ERROR: Self = Self {
30+
reason: "DECODE_ERROR",
31+
};
32+
pub const DECRYPT_ERROR: Self = Self {
33+
reason: "DECRYPT_ERROR",
34+
};
35+
pub const INTERNAL_ERROR: Self = Self {
36+
reason: "INTERNAL_ERROR",
37+
};
38+
}
39+
40+
impl fmt::Display for Error {
41+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42+
if !self.reason.is_empty() {
43+
self.reason.fmt(f)
44+
} else {
45+
write!(f, "packet_protection::Error")
46+
}
47+
}
48+
}
49+
50+
impl fmt::Debug for Error {
51+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52+
let mut d = f.debug_struct("packet_protection::Error");
53+
54+
if !self.reason.is_empty() {
55+
d.field("reason", &self.reason);
56+
}
57+
58+
d.finish()
59+
}
60+
}
61+
62+
impl From<DecoderError> for Error {
63+
fn from(decoder_error: DecoderError) -> Self {
64+
Self {
65+
reason: decoder_error.into(),
66+
}
67+
}
68+
}

quic/s2n-quic-core/src/crypto/retry.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::crypto::CryptoError;
4+
use crate::crypto::packet_protection;
55
use hex_literal::hex;
66

77
pub const INTEGRITY_TAG_LEN: usize = 16;
88
pub type IntegrityTag = [u8; INTEGRITY_TAG_LEN];
99

1010
pub trait RetryKey {
1111
fn generate_tag(payload: &[u8]) -> IntegrityTag;
12-
fn validate(payload: &[u8], tag: IntegrityTag) -> Result<(), CryptoError>;
12+
fn validate(payload: &[u8], tag: IntegrityTag) -> Result<(), packet_protection::Error>;
1313
}
1414

1515
//= https://www.rfc-editor.org/rfc/rfc9001#section-5.8

quic/s2n-quic-core/src/crypto/tests.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::{
5-
crypto::{scatter, CryptoError, HeaderKey, HeaderProtectionMask, Key, ProtectedPayload},
5+
crypto::{packet_protection, scatter, HeaderKey, HeaderProtectionMask, Key, ProtectedPayload},
66
packet::number::{PacketNumber, PacketNumberSpace},
77
varint::VarInt,
88
};
@@ -43,7 +43,7 @@ fn round_trip() {
4343
fn fuzz_unprotect(
4444
input: &mut [u8],
4545
largest_packet_number: PacketNumber,
46-
) -> Result<(PacketNumber, usize), CryptoError> {
46+
) -> Result<(PacketNumber, usize), packet_protection::Error> {
4747
let buffer = DecoderBufferMut::new(input);
4848
let header_len = {
4949
let peek = buffer.peek();
@@ -64,7 +64,7 @@ fn fuzz_unprotect(
6464
packet_number
6565
.truncate(largest_packet_number)
6666
.filter(|actual| truncated_packet_number.eq(actual))
67-
.ok_or(CryptoError::DECODE_ERROR)?;
67+
.ok_or(packet_protection::Error::DECODE_ERROR)?;
6868

6969
let (_header, _payload) = crate::crypto::decrypt(&FuzzCrypto, packet_number, payload)?;
7070

@@ -76,7 +76,7 @@ fn fuzz_protect(
7676
header_len: usize,
7777
largest_packet_number: PacketNumber,
7878
packet_number: PacketNumber,
79-
) -> Result<(), CryptoError> {
79+
) -> Result<(), packet_protection::Error> {
8080
let payload_len = input.len();
8181
let mut payload = EncoderBuffer::new(input);
8282
payload.set_position(payload_len);
@@ -108,7 +108,7 @@ impl Key for FuzzCrypto {
108108
packet_number: u64,
109109
_header: &[u8],
110110
payload: &mut [u8],
111-
) -> Result<(), CryptoError> {
111+
) -> Result<(), packet_protection::Error> {
112112
let mask = packet_number as u8;
113113
for byte in payload.iter_mut() {
114114
*byte ^= mask;
@@ -121,7 +121,7 @@ impl Key for FuzzCrypto {
121121
packet_number: u64,
122122
_header: &[u8],
123123
payload: &mut scatter::Buffer,
124-
) -> Result<(), CryptoError> {
124+
) -> Result<(), packet_protection::Error> {
125125
let payload = payload.flatten();
126126
let (payload, _) = payload.split_mut();
127127

quic/s2n-quic-core/src/crypto/tls.rs

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ pub use bytes::{Bytes, BytesMut};
66
use core::{convert::TryFrom, fmt::Debug};
77
use zerocopy::{AsBytes, FromBytes, FromZeroes, Unaligned};
88

9+
mod error;
10+
pub use error::Error;
11+
912
#[cfg(any(test, feature = "testing"))]
1013
pub mod testing;
1114

0 commit comments

Comments
 (0)