Skip to content

Commit 35381cc

Browse files
committed
ts_keys: rework to use generic types with marker traits
Signed-off-by: David Anderson <danderson@tailscale.com> Change-Id: If90c2f9fc4061dfd9cfe0a9e2c97e2596a6a6964
1 parent 7d301a5 commit 35381cc

46 files changed

Lines changed: 949 additions & 734 deletions

File tree

Some content is hidden

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

Cargo.lock

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

src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,8 @@ pub mod netstack {
336336
pub mod keys {
337337
#[doc(inline)]
338338
pub use ts_keys::{
339-
DiscoKeyPair, DiscoPrivateKey, DiscoPublicKey, MachineKeyPair, MachinePrivateKey,
340-
MachinePublicKey, NetworkLockKeyPair, NetworkLockPrivateKey, NetworkLockPublicKey,
341-
NodeKeyPair, NodePrivateKey, NodePublicKey, NodeState, PersistState,
339+
DiscoKey, MachineKey, NetworkLockKey, NodeKey, NodeState, Pair, PersistState, Private,
340+
Public,
342341
};
343342
}
344343

ts_control/src/client/connect.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bytes::Bytes;
55
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite};
66
use ts_capabilityversion::CapabilityVersion;
77
use ts_http_util::{BytesBody, ClientExt, EmptyBody, HeaderName, HeaderValue, Http2, ResponseExt};
8-
use ts_keys::{MachineKeyPair, MachinePublicKey};
8+
use ts_keys::{MachineKey, Pair, Public};
99
use url::Url;
1010
use zerocopy::network_endian::U32;
1111

@@ -140,8 +140,8 @@ impl From<InternalErrorKind> for crate::InternalErrorKind {
140140
#[derive(serde::Deserialize, serde::Serialize)]
141141
#[serde(rename_all = "camelCase")]
142142
struct ControlPublicKeys {
143-
legacy_public_key: MachinePublicKey,
144-
public_key: MachinePublicKey,
143+
legacy_public_key: Public<MachineKey>,
144+
public_key: Public<MachineKey>,
145145
}
146146

147147
impl fmt::Display for ControlPublicKeys {
@@ -153,7 +153,7 @@ impl fmt::Display for ControlPublicKeys {
153153
#[tracing::instrument(skip_all, fields(%control_url), err)]
154154
pub async fn connect(
155155
control_url: &Url,
156-
machine_keys: &MachineKeyPair,
156+
machine_keys: &Pair<MachineKey>,
157157
) -> Result<Http2<BytesBody>, ConnectionError> {
158158
let h1_client = connect_h1(control_url).await?;
159159

@@ -189,7 +189,7 @@ async fn connect_h1(url: &Url) -> Result<ts_http_util::Http1<EmptyBody>, Connect
189189
}
190190

191191
#[tracing::instrument(skip_all, fields(%control_url), ret, err, level = "trace")]
192-
pub async fn fetch_control_key(control_url: &Url) -> Result<MachinePublicKey, ConnectionError> {
192+
pub async fn fetch_control_key(control_url: &Url) -> Result<Public<MachineKey>, ConnectionError> {
193193
let mut key_url = control_url.join("/key")?;
194194

195195
#[cfg(not(feature = "insecure-keyfetch"))]
@@ -226,7 +226,7 @@ pub async fn upgrade_ts2021(
226226
control_url: &Url,
227227
init_msg: &str,
228228
handshake: ts_control_noise::Handshake,
229-
machine_key: &MachineKeyPair,
229+
machine_key: &Pair<MachineKey>,
230230
h1_client: impl ts_http_util::Client<EmptyBody>,
231231
) -> Result<impl AsyncRead + AsyncWrite + Unpin + 'static, ConnectionError> {
232232
let ts2021_url = control_url.join("/ts2021")?;

ts_control/src/control_dialer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use tokio_util::future::FutureExt;
99
use ts_bitset::BitsetDyn;
1010
use ts_capabilityversion::CapabilityVersion;
1111
use ts_http_util::{BytesBody, Http2};
12+
use ts_keys::{MachineKey, Pair};
1213
use url::Url;
1314

1415
use crate::{DialCandidate, DialMode, DialPlan, Error, InternalErrorKind, Operation};
@@ -188,7 +189,7 @@ impl ControlDialer {
188189
pub async fn full_connect_next(
189190
&mut self,
190191
url: &Url,
191-
machine_keys: &ts_keys::MachineKeyPair,
192+
machine_keys: &Pair<MachineKey>,
192193
) -> Result<Http2<BytesBody>, Error> {
193194
let next = self.next_dialer();
194195
tracing::trace!(selected_control_dialer = ?next);
@@ -221,7 +222,7 @@ impl ControlDialer {
221222
/// inner http2 connection.
222223
pub async fn complete_connection<Io>(
223224
url: &Url,
224-
machine_keys: &ts_keys::MachineKeyPair,
225+
machine_keys: &Pair<MachineKey>,
225226
stream: Io,
226227
) -> Result<Http2<BytesBody>, Error>
227228
where

ts_control/src/node.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::{
66

77
use chrono::{DateTime, Utc};
88
use ts_capabilityversion::CapabilityVersion;
9-
use ts_keys::{DiscoPublicKey, MachinePublicKey, NodePublicKey};
9+
use ts_keys::{DiscoKey, MachineKey, NodeKey, Public};
1010

1111
const LAST_SEEN_FORMAT: &str = "%F %T %Z";
1212

@@ -170,15 +170,15 @@ pub struct Node {
170170
/// The address of the node in the tailnet.
171171
pub tailnet_address: TailnetAddress,
172172

173-
/// The node's [`NodePublicKey`].
174-
pub node_key: NodePublicKey,
173+
/// The node's node public key.
174+
pub node_key: Public<NodeKey>,
175175
/// The node key's expiration.
176176
pub node_key_expiry: Option<DateTime<Utc>>,
177177

178-
/// The node's [`MachinePublicKey`], if known.
179-
pub machine_key: Option<MachinePublicKey>,
180-
/// The node's [`DiscoPublicKey`], if known.
181-
pub disco_key: Option<DiscoPublicKey>,
178+
/// The node's machine public key, if known.
179+
pub machine_key: Option<Public<MachineKey>>,
180+
/// The node's disco public key, if known.
181+
pub disco_key: Option<Public<DiscoKey>>,
182182
/// The signature of the node's public key with the Tailnet Lock signing key, if Tailnet Lock
183183
/// is enabled and the signature is known.
184184
pub tailnet_lock_key_signature: Option<Vec<u8>>,
@@ -400,13 +400,13 @@ pub struct NodeUpdate {
400400
/// The node's capabilities (node caps, not peer caps). If `None`, has not changed.
401401
pub cap_map: Option<BTreeMap<String, Vec<String>>>,
402402

403-
/// The node's [`NodePublicKey`]. If `None`, has not changed.
404-
pub node_key: Option<NodePublicKey>,
403+
/// The node's node public key. If `None`, has not changed.
404+
pub node_key: Option<Public<NodeKey>>,
405405
/// The node key's expiration. If `None`, has not changed.
406406
pub node_key_expiry: Option<DateTime<Utc>>,
407407

408-
/// The node's [`DiscoPublicKey`]. If `None`, has not changed.
409-
pub disco_key: Option<DiscoPublicKey>,
408+
/// The node's disco public key. If `None`, has not changed.
409+
pub disco_key: Option<Public<DiscoKey>>,
410410

411411
/// The node's key signature for Tailnet Lock. If `None`, has not changed.
412412
pub tailnet_lock_key_signature: Option<Vec<u8>>,

ts_control_noise/src/handshake.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bytes::BytesMut;
33
use tokio::io::{AsyncRead, AsyncReadExt};
44
use tokio_util::codec::Framed;
55
use ts_hexdump::{AsHexExt, Case};
6-
use ts_keys::{MachineKeyPair, MachinePublicKey};
6+
use ts_keys::{MachineKey, Pair, Public};
77
use ts_noise::ik::SentHandshake;
88
use zerocopy::{IntoBytes, TryFromBytes};
99

@@ -31,14 +31,14 @@ impl Handshake {
3131
/// to the control server in order to start the handshake.
3232
pub fn initialize(
3333
prologue: &str,
34-
node_machine_key: &MachineKeyPair,
35-
control_public_key: &MachinePublicKey,
34+
node_machine_key: &Pair<MachineKey>,
35+
control_public_key: &Public<MachineKey>,
3636
capability_version: ts_capabilityversion::CapabilityVersion,
3737
) -> (Self, String) {
3838
let mut ciphertext = [0; SentHandshake::INIT_SIZE];
3939
let state = SentHandshake::new(
4040
node_machine_key.into(),
41-
control_public_key.into(),
41+
&control_public_key.into(),
4242
prologue.as_bytes(),
4343
&mut ciphertext,
4444
);
@@ -55,7 +55,7 @@ impl Handshake {
5555
pub async fn complete<T: AsyncRead + Unpin>(
5656
mut self,
5757
mut conn: T,
58-
node_machine_key: &MachineKeyPair,
58+
node_machine_key: &Pair<MachineKey>,
5959
) -> Result<WrappedIo<T>, Error> {
6060
let mut hdr_bytes = [0u8; 3];
6161
conn.read_exact(&mut hdr_bytes[..]).await?;
@@ -78,7 +78,7 @@ impl Handshake {
7878
return Err(Error::BadFormat);
7979
}
8080

81-
let session = match self.state.try_finish(&mut packet, node_machine_key.into()) {
81+
let session = match self.state.try_finish(&mut packet, &node_machine_key.into()) {
8282
Ok(session) => session,
8383
Err(state) => {
8484
self.state = state;

ts_control_serde/src/netmap.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::net::SocketAddr;
44
use chrono::{DateTime, Utc};
55
use serde::Deserialize;
66
use ts_capabilityversion::CapabilityVersion;
7-
use ts_keys::{DiscoPublicKey, NodePublicKey};
7+
use ts_keys::{DiscoKey, Public};
88

99
use crate::{
1010
DerpRegionId, DnsConfig, MarshaledSignature,
@@ -50,10 +50,10 @@ pub struct MapRequest<'a> {
5050
pub keep_alive: bool,
5151

5252
/// The public key of this Tailscale node.
53-
pub node_key: NodePublicKey,
53+
pub node_key: Public<ts_keys::NodeKey>,
5454
/// The public key this Tailscale node will use with the Disco protocol to establish direct
5555
/// connections with peer nodes in the Tailnet.
56-
pub disco_key: DiscoPublicKey,
56+
pub disco_key: Public<DiscoKey>,
5757

5858
/// If populated, the public key of the node's hardware-backed identity attestation key.
5959
pub hardware_attestation_key: Option<Vec<u8>>,
@@ -487,14 +487,14 @@ pub struct PeerChange<'a> {
487487
pub endpoints: Option<Vec<SocketAddr>>,
488488

489489
/// If present, the node's wireguard public key has changed.
490-
pub key: Option<NodePublicKey>,
490+
pub key: Option<Public<ts_keys::NodeKey>>,
491491

492492
/// If present, the signature of the node's wireguard public key has changed.
493493
#[serde(borrow)]
494494
pub key_signature: Option<MarshaledSignature<'a>>,
495495

496496
/// If present, the node's disco key has changed.
497-
pub disco_key: Option<DiscoPublicKey>,
497+
pub disco_key: Option<Public<DiscoKey>>,
498498
/// If present, the node's online status changed.
499499
pub online: Option<bool>,
500500
/// If present, the node's last seen time changed.

ts_control_serde/src/node.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::net::{IpAddr, SocketAddr};
44
use chrono::{DateTime, Utc};
55
use serde::{Deserialize, Serialize};
66
use ts_capabilityversion::CapabilityVersion;
7-
use ts_keys::{DiscoPublicKey, MachinePublicKey, NodePublicKey};
7+
use ts_keys::{DiscoKey, MachineKey, NodeKey, Public};
88

99
use crate::{DnsResolver, derp_map::RegionId, host_info::HostInfo, user::UserId};
1010

@@ -63,18 +63,18 @@ pub struct Node<'a> {
6363
/// Unique ID of the user who shared this node, if non-zero and different from [`Node::user`].
6464
pub sharer: UserId,
6565

66-
/// If populated, the public key of the Tailscale node's [`NodeKeyPair`][ts_keys::NodeKeyPair].
67-
pub key: NodePublicKey,
68-
/// The date and time that the Tailscale node's [`NodeKeyPair`][ts_keys::NodeKeyPair] will expire.
66+
/// If populated, the public key of the Tailscale node's [node keypair][ts_keys::Pair<ts_keys::NodeKey>].
67+
pub key: Public<NodeKey>,
68+
/// The date and time that the Tailscale node's node keypair will expire.
6969
pub key_expiry: Option<DateTime<Utc>>,
7070
/// If populated, a signature of the Tailnet Key Authority (TKA) key authorizing the Tailscale
7171
/// node to join the Tailnet.
7272
#[serde(borrow)]
7373
pub key_signature: Option<MarshaledSignature<'a>>,
74-
/// If populated, the public key of the Tailscale node's [`MachineKeyPair`][ts_keys::MachineKeyPair].
75-
pub machine: Option<MachinePublicKey>,
76-
/// If populated, the public key of the Tailscale node's [`DiscoKeyPair`][ts_keys::DiscoKeyPair].
77-
pub disco_key: Option<DiscoPublicKey>,
74+
/// If populated, the public key of the Tailscale node's [machine keypair][ts_keys::Pair<ts_keys::MachineKey>].
75+
pub machine: Option<Public<MachineKey>>,
76+
/// If populated, the public key of the Tailscale node's [disco keypair][ts_keys::Pair<DiscoKey>].
77+
pub disco_key: Option<Public<DiscoKey>>,
7878

7979
/// The IP addresses of the Tailscale node in the Tailnet. There are exactly 2 addresses, and
8080
/// they are always in the same order: the first is the IPv4 address, the second is the IPv6

ts_control_serde/src/register.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::fmt::Debug;
22

33
use chrono::{DateTime, Utc};
44
use serde::{Deserialize, Serialize};
5-
use ts_keys::{NetworkLockPublicKey, NodePublicKey};
5+
use ts_keys::{NetworkLockKey, Public};
66
use url::Url;
77

88
use crate::{
@@ -77,14 +77,14 @@ pub struct RegisterRequest<'a> {
7777
/// The current public key of this Tailscale node. In the case of node key rotation, this is
7878
/// the "new" node public key, and [`RegisterRequest::old_node_key`] contains the expired
7979
/// public node key.
80-
pub node_key: NodePublicKey,
80+
pub node_key: Public<ts_keys::NodeKey>,
8181
/// The expired public key of this Tailscale node. Only populated when the node key has expired
8282
/// and needs to be rotated.
83-
pub old_node_key: Option<NodePublicKey>,
83+
pub old_node_key: Option<Public<ts_keys::NodeKey>>,
8484
/// The new Tailnet Lock public key for this Tailscale node. Only populated when the key has
8585
/// been changed, or has never been set for this node.
8686
#[serde(rename = "NLKey")]
87-
pub nl_key: Option<NetworkLockPublicKey>,
87+
pub nl_key: Option<Public<NetworkLockKey>>,
8888
/// Authentication information that allows this Tailscale node to register with the control
8989
/// plane and join a specific Tailnet.
9090
#[serde(borrow)]

ts_dataplane/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ts_underlay_router.workspace = true
2525
ts_tunnel.workspace = true
2626
ts_bart.workspace = true
2727
ts_disco_protocol.workspace = true
28+
ts_keys.workspace = true
2829

2930
# Unconditionally required dependencies.
3031
bytes.workspace = true

0 commit comments

Comments
 (0)