1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};
use std::net::SocketAddr;
use tor_llcrypto::pk;
use crate::{ChanTarget, CircTarget, HasAddrs, HasRelayIds, RelayIdRef, RelayIdType};
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct RelayIds {
#[serde(rename = "ed25519")]
ed_identity: Option<pk::ed25519::Ed25519Identity>,
#[serde(rename = "rsa")]
rsa_identity: Option<pk::rsa::RsaIdentity>,
}
impl HasRelayIds for RelayIds {
fn identity(&self, key_type: RelayIdType) -> Option<crate::RelayIdRef<'_>> {
match key_type {
RelayIdType::Ed25519 => self.ed_identity.as_ref().map(RelayIdRef::from),
RelayIdType::Rsa => self.rsa_identity.as_ref().map(RelayIdRef::from),
}
}
}
impl RelayIds {
pub fn new(
ed_identity: pk::ed25519::Ed25519Identity,
rsa_identity: pk::rsa::RsaIdentity,
) -> Self {
Self {
ed_identity: Some(ed_identity),
rsa_identity: Some(rsa_identity),
}
}
pub fn from_relay_ids<T: HasRelayIds + ?Sized>(other: &T) -> Self {
Self {
ed_identity: other
.identity(RelayIdType::Ed25519)
.map(|r| *r.unwrap_ed25519()),
rsa_identity: other.identity(RelayIdType::Rsa).map(|r| *r.unwrap_rsa()),
}
}
}
#[derive(Debug, Clone)]
pub struct OwnedChanTarget {
addrs: Vec<SocketAddr>,
ids: RelayIds,
}
impl HasAddrs for OwnedChanTarget {
fn addrs(&self) -> &[SocketAddr] {
&self.addrs[..]
}
}
impl HasRelayIds for OwnedChanTarget {
fn identity(&self, key_type: RelayIdType) -> Option<RelayIdRef<'_>> {
self.ids.identity(key_type)
}
}
impl ChanTarget for OwnedChanTarget {}
impl OwnedChanTarget {
pub fn new(
addrs: Vec<SocketAddr>,
ed_identity: pk::ed25519::Ed25519Identity,
rsa_identity: pk::rsa::RsaIdentity,
) -> Self {
Self {
addrs,
ids: RelayIds::new(ed_identity, rsa_identity),
}
}
pub fn from_chan_target<C>(target: &C) -> Self
where
C: ChanTarget + ?Sized,
{
OwnedChanTarget {
addrs: target.addrs().to_vec(),
ids: RelayIds::from_relay_ids(target),
}
}
pub fn restrict_addr(&self, addr: &SocketAddr) -> Result<Self, Self> {
if self.addrs.contains(addr) {
Ok(OwnedChanTarget {
addrs: vec![*addr],
ids: self.ids.clone(),
})
} else {
Err(self.clone())
}
}
}
impl Display for OwnedChanTarget {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[")?;
match &*self.addrs {
[] => write!(f, "?")?,
[a] => write!(f, "{}", a)?,
[a, ..] => write!(f, "{}+", a)?,
};
for ident in self.identities() {
write!(f, " {}", ident)?;
}
write!(f, "]")?;
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct OwnedCircTarget {
chan_target: OwnedChanTarget,
ntor_onion_key: pk::curve25519::PublicKey,
protovers: tor_protover::Protocols,
}
impl OwnedCircTarget {
pub fn new(
chan_target: OwnedChanTarget,
ntor_onion_key: pk::curve25519::PublicKey,
protovers: tor_protover::Protocols,
) -> OwnedCircTarget {
OwnedCircTarget {
chan_target,
ntor_onion_key,
protovers,
}
}
pub fn from_circ_target<C>(target: &C) -> Self
where
C: CircTarget + ?Sized,
{
OwnedCircTarget {
chan_target: OwnedChanTarget::from_chan_target(target),
ntor_onion_key: *target.ntor_onion_key(),
protovers: target.protovers().clone(),
}
}
}
impl Display for OwnedCircTarget {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.chan_target, f)
}
}
impl HasAddrs for OwnedCircTarget {
fn addrs(&self) -> &[SocketAddr] {
self.chan_target.addrs()
}
}
impl HasRelayIds for OwnedCircTarget {
fn identity(&self, key_type: RelayIdType) -> Option<RelayIdRef<'_>> {
self.chan_target.identity(key_type)
}
}
impl ChanTarget for OwnedCircTarget {}
impl CircTarget for OwnedCircTarget {
fn ntor_onion_key(&self) -> &pk::curve25519::PublicKey {
&self.ntor_onion_key
}
fn protovers(&self) -> &tor_protover::Protocols {
&self.protovers
}
}
#[cfg(test)]
mod test {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
#[allow(clippy::redundant_clone)]
fn chan_target() {
let ti = OwnedChanTarget::new(
vec!["127.0.0.1:11".parse().unwrap()],
[42; 32].into(),
[45; 20].into(),
);
let ti2 = OwnedChanTarget::from_chan_target(&ti);
assert_eq!(ti.addrs(), ti2.addrs());
assert!(ti.same_relay_ids(&ti2));
assert_eq!(format!("{:?}", ti), format!("{:?}", ti2));
assert_eq!(format!("{:?}", ti), format!("{:?}", ti.clone()));
}
#[test]
#[allow(clippy::redundant_clone)]
fn circ_target() {
let ch = OwnedChanTarget::new(
vec!["127.0.0.1:11".parse().unwrap()],
[42; 32].into(),
[45; 20].into(),
);
let ct = OwnedCircTarget::new(ch.clone(), [99; 32].into(), "FlowCtrl=7".parse().unwrap());
assert_eq!(ct.addrs(), ch.addrs());
assert!(ct.same_relay_ids(&ch));
assert_eq!(ct.ntor_onion_key().as_bytes(), &[99; 32]);
assert_eq!(&ct.protovers().to_string(), "FlowCtrl=7");
let ct2 = OwnedCircTarget::from_circ_target(&ct);
assert_eq!(format!("{:?}", ct), format!("{:?}", ct2));
assert_eq!(format!("{:?}", ct), format!("{:?}", ct.clone()));
}
}