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
use std::net::SocketAddr;
use tor_llcrypto::pk;
pub trait ChanTarget {
fn addrs(&self) -> &[SocketAddr];
fn ed_identity(&self) -> &pk::ed25519::Ed25519Identity;
fn ed_identity_key(&self) -> Option<pk::ed25519::PublicKey> {
self.ed_identity().try_into().ok()
}
fn rsa_identity(&self) -> &pk::rsa::RsaIdentity;
}
pub trait CircTarget: ChanTarget {
fn linkspecs(&self) -> Vec<crate::LinkSpec> {
let mut result = vec![(*self.ed_identity()).into(), (*self.rsa_identity()).into()];
for addr in self.addrs().iter() {
result.push(addr.into());
}
result
}
fn ntor_onion_key(&self) -> &pk::curve25519::PublicKey;
fn protovers(&self) -> &tor_protover::Protocols;
}
#[cfg(test)]
mod test {
#![allow(clippy::unwrap_used)]
use super::*;
use hex_literal::hex;
use std::net::IpAddr;
use tor_llcrypto::pk;
struct Example {
addrs: Vec<SocketAddr>,
ed_id: pk::ed25519::Ed25519Identity,
rsa_id: pk::rsa::RsaIdentity,
ntor: pk::curve25519::PublicKey,
pv: tor_protover::Protocols,
}
impl ChanTarget for Example {
fn addrs(&self) -> &[SocketAddr] {
&self.addrs[..]
}
fn ed_identity(&self) -> &pk::ed25519::Ed25519Identity {
&self.ed_id
}
fn rsa_identity(&self) -> &pk::rsa::RsaIdentity {
&self.rsa_id
}
}
impl CircTarget for Example {
fn ntor_onion_key(&self) -> &pk::curve25519::PublicKey {
&self.ntor
}
fn protovers(&self) -> &tor_protover::Protocols {
&self.pv
}
}
fn example() -> Example {
Example {
addrs: vec![
"127.0.0.1:99".parse::<SocketAddr>().unwrap(),
"[::1]:909".parse::<SocketAddr>().unwrap(),
],
ed_id: pk::ed25519::PublicKey::from_bytes(&hex!(
"fc51cd8e6218a1a38da47ed00230f058
0816ed13ba3303ac5deb911548908025"
))
.unwrap()
.into(),
rsa_id: pk::rsa::RsaIdentity::from_bytes(&hex!(
"1234567890abcdef12341234567890abcdef1234"
))
.unwrap(),
ntor: pk::curve25519::PublicKey::from(hex!(
"e6db6867583030db3594c1a424b15f7c
726624ec26b3353b10a903a6d0ab1c4c"
)),
pv: tor_protover::Protocols::default(),
}
}
#[test]
fn test_linkspecs() {
let ex = example();
let specs = ex.linkspecs();
assert_eq!(4, specs.len());
use crate::ls::LinkSpec;
assert_eq!(
specs[0],
LinkSpec::Ed25519Id(
pk::ed25519::PublicKey::from_bytes(&hex!(
"fc51cd8e6218a1a38da47ed00230f058
0816ed13ba3303ac5deb911548908025"
))
.unwrap()
.into()
)
);
assert_eq!(
specs[1],
LinkSpec::RsaId(
pk::rsa::RsaIdentity::from_bytes(&hex!("1234567890abcdef12341234567890abcdef1234"))
.unwrap()
)
);
assert_eq!(
specs[2],
LinkSpec::OrPort("127.0.0.1".parse::<IpAddr>().unwrap(), 99)
);
assert_eq!(
specs[3],
LinkSpec::OrPort("::1".parse::<IpAddr>().unwrap(), 909)
);
}
#[test]
fn key_accessor() {
let ex = example();
let key = ex.ed_identity_key().unwrap();
assert_eq!(&pk::ed25519::Ed25519Identity::from(key), ex.ed_identity());
let a = hex!("6d616e79737472696e677361726565646b6579736e6f74746869736f6e654091");
let ex = Example {
ed_id: pk::ed25519::Ed25519Identity::from_bytes(&a).unwrap(),
..ex
};
let key = ex.ed_identity_key();
assert!(key.is_none());
}
}