Skip to content

Commit f2e119b

Browse files
authored
refactor: move and rewrite configuration (#1034)
* refactor: move and rewrite configuration * fix wasm
1 parent 271ac37 commit f2e119b

File tree

41 files changed

+1411
-980
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1411
-980
lines changed

Cargo.lock

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

crates/attestation/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fixtures = ["tlsn-core/fixtures", "dep:tlsn-data-fixtures"]
99

1010
[dependencies]
1111
tlsn-tls-core = { workspace = true }
12-
tlsn-core = { workspace = true }
12+
tlsn-core = { workspace = true, features = ["mozilla-certs"] }
1313
tlsn-data-fixtures = { workspace = true, optional = true }
1414

1515
bcs = { workspace = true }

crates/core/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ workspace = true
1313

1414
[features]
1515
default = []
16+
mozilla-certs = ["dep:webpki-root-certs", "dep:webpki-roots"]
1617
fixtures = [
1718
"dep:hex",
1819
"dep:tlsn-data-fixtures",
@@ -44,7 +45,8 @@ sha2 = { workspace = true }
4445
thiserror = { workspace = true }
4546
tiny-keccak = { workspace = true, features = ["keccak"] }
4647
web-time = { workspace = true }
47-
webpki-roots = { workspace = true }
48+
webpki-roots = { workspace = true, optional = true }
49+
webpki-root-certs = { workspace = true, optional = true }
4850
rustls-webpki = { workspace = true, features = ["ring"] }
4951
rustls-pki-types = { workspace = true }
5052
itybity = { workspace = true }

crates/core/src/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! Configuration types.
2+
3+
pub mod prove;
4+
pub mod prover;
5+
pub mod tls;
6+
pub mod tls_commit;
7+
pub mod verifier;

crates/core/src/config/prove.rs

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
//! Proving configuration.
2+
3+
use rangeset::{RangeSet, ToRangeSet, UnionMut};
4+
use serde::{Deserialize, Serialize};
5+
6+
use crate::transcript::{Direction, Transcript, TranscriptCommitConfig, TranscriptCommitRequest};
7+
8+
/// Configuration to prove information to the verifier.
9+
#[derive(Debug, Clone, Serialize, Deserialize)]
10+
pub struct ProveConfig {
11+
server_identity: bool,
12+
reveal: Option<(RangeSet<usize>, RangeSet<usize>)>,
13+
transcript_commit: Option<TranscriptCommitConfig>,
14+
}
15+
16+
impl ProveConfig {
17+
/// Creates a new builder.
18+
pub fn builder(transcript: &Transcript) -> ProveConfigBuilder<'_> {
19+
ProveConfigBuilder::new(transcript)
20+
}
21+
22+
/// Returns `true` if the server identity is to be proven.
23+
pub fn server_identity(&self) -> bool {
24+
self.server_identity
25+
}
26+
27+
/// Returns the sent and received ranges of the transcript to be revealed,
28+
/// respectively.
29+
pub fn reveal(&self) -> Option<&(RangeSet<usize>, RangeSet<usize>)> {
30+
self.reveal.as_ref()
31+
}
32+
33+
/// Returns the transcript commitment configuration.
34+
pub fn transcript_commit(&self) -> Option<&TranscriptCommitConfig> {
35+
self.transcript_commit.as_ref()
36+
}
37+
38+
/// Returns a request.
39+
pub fn to_request(&self) -> ProveRequest {
40+
ProveRequest {
41+
server_identity: self.server_identity,
42+
reveal: self.reveal.clone(),
43+
transcript_commit: self
44+
.transcript_commit
45+
.clone()
46+
.map(|config| config.to_request()),
47+
}
48+
}
49+
}
50+
51+
/// Builder for [`ProveConfig`].
52+
#[derive(Debug)]
53+
pub struct ProveConfigBuilder<'a> {
54+
transcript: &'a Transcript,
55+
server_identity: bool,
56+
reveal: Option<(RangeSet<usize>, RangeSet<usize>)>,
57+
transcript_commit: Option<TranscriptCommitConfig>,
58+
}
59+
60+
impl<'a> ProveConfigBuilder<'a> {
61+
/// Creates a new builder.
62+
pub fn new(transcript: &'a Transcript) -> Self {
63+
Self {
64+
transcript,
65+
server_identity: false,
66+
reveal: None,
67+
transcript_commit: None,
68+
}
69+
}
70+
71+
/// Proves the server identity.
72+
pub fn server_identity(&mut self) -> &mut Self {
73+
self.server_identity = true;
74+
self
75+
}
76+
77+
/// Configures transcript commitments.
78+
pub fn transcript_commit(&mut self, transcript_commit: TranscriptCommitConfig) -> &mut Self {
79+
self.transcript_commit = Some(transcript_commit);
80+
self
81+
}
82+
83+
/// Reveals the given ranges of the transcript.
84+
pub fn reveal(
85+
&mut self,
86+
direction: Direction,
87+
ranges: &dyn ToRangeSet<usize>,
88+
) -> Result<&mut Self, ProveConfigError> {
89+
let idx = ranges.to_range_set();
90+
91+
if idx.end().unwrap_or(0) > self.transcript.len_of_direction(direction) {
92+
return Err(ProveConfigError(ErrorRepr::IndexOutOfBounds {
93+
direction,
94+
actual: idx.end().unwrap_or(0),
95+
len: self.transcript.len_of_direction(direction),
96+
}));
97+
}
98+
99+
let (sent, recv) = self.reveal.get_or_insert_default();
100+
match direction {
101+
Direction::Sent => sent.union_mut(&idx),
102+
Direction::Received => recv.union_mut(&idx),
103+
}
104+
105+
Ok(self)
106+
}
107+
108+
/// Reveals the given ranges of the sent data transcript.
109+
pub fn reveal_sent(
110+
&mut self,
111+
ranges: &dyn ToRangeSet<usize>,
112+
) -> Result<&mut Self, ProveConfigError> {
113+
self.reveal(Direction::Sent, ranges)
114+
}
115+
116+
/// Reveals all of the sent data transcript.
117+
pub fn reveal_sent_all(&mut self) -> Result<&mut Self, ProveConfigError> {
118+
let len = self.transcript.len_of_direction(Direction::Sent);
119+
let (sent, _) = self.reveal.get_or_insert_default();
120+
sent.union_mut(&(0..len));
121+
Ok(self)
122+
}
123+
124+
/// Reveals the given ranges of the received data transcript.
125+
pub fn reveal_recv(
126+
&mut self,
127+
ranges: &dyn ToRangeSet<usize>,
128+
) -> Result<&mut Self, ProveConfigError> {
129+
self.reveal(Direction::Received, ranges)
130+
}
131+
132+
/// Reveals all of the received data transcript.
133+
pub fn reveal_recv_all(&mut self) -> Result<&mut Self, ProveConfigError> {
134+
let len = self.transcript.len_of_direction(Direction::Received);
135+
let (_, recv) = self.reveal.get_or_insert_default();
136+
recv.union_mut(&(0..len));
137+
Ok(self)
138+
}
139+
140+
/// Builds the configuration.
141+
pub fn build(self) -> Result<ProveConfig, ProveConfigError> {
142+
Ok(ProveConfig {
143+
server_identity: self.server_identity,
144+
reveal: self.reveal,
145+
transcript_commit: self.transcript_commit,
146+
})
147+
}
148+
}
149+
150+
/// Request to prove statements about the connection.
151+
#[derive(Debug, Clone, Serialize, Deserialize)]
152+
pub struct ProveRequest {
153+
server_identity: bool,
154+
reveal: Option<(RangeSet<usize>, RangeSet<usize>)>,
155+
transcript_commit: Option<TranscriptCommitRequest>,
156+
}
157+
158+
impl ProveRequest {
159+
/// Returns `true` if the server identity is to be proven.
160+
pub fn server_identity(&self) -> bool {
161+
self.server_identity
162+
}
163+
164+
/// Returns the sent and received ranges of the transcript to be revealed,
165+
/// respectively.
166+
pub fn reveal(&self) -> Option<&(RangeSet<usize>, RangeSet<usize>)> {
167+
self.reveal.as_ref()
168+
}
169+
170+
/// Returns the transcript commitment configuration.
171+
pub fn transcript_commit(&self) -> Option<&TranscriptCommitRequest> {
172+
self.transcript_commit.as_ref()
173+
}
174+
}
175+
176+
/// Error for [`ProveConfig`].
177+
#[derive(Debug, thiserror::Error)]
178+
#[error(transparent)]
179+
pub struct ProveConfigError(#[from] ErrorRepr);
180+
181+
#[derive(Debug, thiserror::Error)]
182+
enum ErrorRepr {
183+
#[error("range is out of bounds of the transcript ({direction}): {actual} > {len}")]
184+
IndexOutOfBounds {
185+
direction: Direction,
186+
actual: usize,
187+
len: usize,
188+
},
189+
}

crates/core/src/config/prover.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//! Prover configuration.
2+
3+
use serde::{Deserialize, Serialize};
4+
5+
/// Prover configuration.
6+
#[derive(Debug, Clone, Serialize, Deserialize)]
7+
pub struct ProverConfig {}
8+
9+
impl ProverConfig {
10+
/// Creates a new builder.
11+
pub fn builder() -> ProverConfigBuilder {
12+
ProverConfigBuilder::default()
13+
}
14+
}
15+
16+
/// Builder for [`ProverConfig`].
17+
#[derive(Debug, Default)]
18+
pub struct ProverConfigBuilder {}
19+
20+
impl ProverConfigBuilder {
21+
/// Builds the configuration.
22+
pub fn build(self) -> Result<ProverConfig, ProverConfigError> {
23+
Ok(ProverConfig {})
24+
}
25+
}
26+
27+
/// Error for [`ProverConfig`].
28+
#[derive(Debug, thiserror::Error)]
29+
#[error(transparent)]
30+
pub struct ProverConfigError(#[from] ErrorRepr);
31+
32+
#[derive(Debug, thiserror::Error)]
33+
enum ErrorRepr {}

crates/core/src/config/tls.rs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//! TLS client configuration.
2+
3+
use serde::{Deserialize, Serialize};
4+
5+
use crate::{
6+
connection::ServerName,
7+
webpki::{CertificateDer, PrivateKeyDer, RootCertStore},
8+
};
9+
10+
/// TLS client configuration.
11+
#[derive(Debug, Clone, Serialize, Deserialize)]
12+
pub struct TlsClientConfig {
13+
server_name: ServerName,
14+
/// Root certificates.
15+
root_store: RootCertStore,
16+
/// Certificate chain and a matching private key for client
17+
/// authentication.
18+
client_auth: Option<(Vec<CertificateDer>, PrivateKeyDer)>,
19+
}
20+
21+
impl TlsClientConfig {
22+
/// Creates a new builder.
23+
pub fn builder() -> TlsConfigBuilder {
24+
TlsConfigBuilder::default()
25+
}
26+
27+
/// Returns the server name.
28+
pub fn server_name(&self) -> &ServerName {
29+
&self.server_name
30+
}
31+
32+
/// Returns the root certificates.
33+
pub fn root_store(&self) -> &RootCertStore {
34+
&self.root_store
35+
}
36+
37+
/// Returns a certificate chain and a matching private key for client
38+
/// authentication.
39+
pub fn client_auth(&self) -> Option<&(Vec<CertificateDer>, PrivateKeyDer)> {
40+
self.client_auth.as_ref()
41+
}
42+
}
43+
44+
/// Builder for [`TlsClientConfig`].
45+
#[derive(Debug, Default)]
46+
pub struct TlsConfigBuilder {
47+
server_name: Option<ServerName>,
48+
root_store: Option<RootCertStore>,
49+
client_auth: Option<(Vec<CertificateDer>, PrivateKeyDer)>,
50+
}
51+
52+
impl TlsConfigBuilder {
53+
/// Sets the server name.
54+
pub fn server_name(mut self, server_name: ServerName) -> Self {
55+
self.server_name = Some(server_name);
56+
self
57+
}
58+
59+
/// Sets the root certificates to use for verifying the server's
60+
/// certificate.
61+
pub fn root_store(mut self, store: RootCertStore) -> Self {
62+
self.root_store = Some(store);
63+
self
64+
}
65+
66+
/// Sets a DER-encoded certificate chain and a matching private key for
67+
/// client authentication.
68+
///
69+
/// Often the chain will consist of a single end-entity certificate.
70+
///
71+
/// # Arguments
72+
///
73+
/// * `cert_key` - A tuple containing the certificate chain and the private
74+
/// key.
75+
///
76+
/// - Each certificate in the chain must be in the X.509 format.
77+
/// - The key must be in the ASN.1 format (either PKCS#8 or PKCS#1).
78+
pub fn client_auth(mut self, cert_key: (Vec<CertificateDer>, PrivateKeyDer)) -> Self {
79+
self.client_auth = Some(cert_key);
80+
self
81+
}
82+
83+
/// Builds the TLS configuration.
84+
pub fn build(self) -> Result<TlsClientConfig, TlsConfigError> {
85+
let server_name = self.server_name.ok_or(ErrorRepr::MissingField {
86+
field: "server_name",
87+
})?;
88+
89+
let root_store = self.root_store.ok_or(ErrorRepr::MissingField {
90+
field: "root_store",
91+
})?;
92+
93+
Ok(TlsClientConfig {
94+
server_name,
95+
root_store,
96+
client_auth: self.client_auth,
97+
})
98+
}
99+
}
100+
101+
/// TLS configuration error.
102+
#[derive(Debug, thiserror::Error)]
103+
#[error(transparent)]
104+
pub struct TlsConfigError(#[from] ErrorRepr);
105+
106+
#[derive(Debug, thiserror::Error)]
107+
#[error("tls config error")]
108+
enum ErrorRepr {
109+
#[error("missing required field: {field}")]
110+
MissingField { field: &'static str },
111+
}

0 commit comments

Comments
 (0)