Skip to content

Commit

Permalink
Move key paths to separate structure
Browse files Browse the repository at this point in the history
  • Loading branch information
neacsu committed May 17, 2024
1 parent 0000baa commit 870570d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 43 deletions.
23 changes: 7 additions & 16 deletions nym-node/src/config/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ pub struct KeysPaths {

/// Path to file containing x25519 noise public key.
pub public_x25519_noise_key_file: PathBuf,

/// Path to file containing x25519 wireguard private key.
pub private_x25519_wireguard_key_file: PathBuf,

/// Path to file containing x25519 wireguard public key.
pub public_x25519_wireguard_key_file: PathBuf,
}

impl KeysPaths {
Expand All @@ -109,9 +103,6 @@ impl KeysPaths {
public_x25519_sphinx_key_file: data_dir.join(DEFAULT_X25519_PUBLIC_SPHINX_KEY_FILENAME),
private_x25519_noise_key_file: data_dir.join(DEFAULT_X25519_PRIVATE_NOISE_KEY_FILENAME),
public_x25519_noise_key_file: data_dir.join(DEFAULT_X25519_PUBLIC_NOISE_KEY_FILENAME),
private_x25519_wireguard_key_file: data_dir.join(DEFAULT_X25519_WG_DH_KEY_FILENAME),
public_x25519_wireguard_key_file: data_dir
.join(DEFAULT_X25519_WG_PUBLIC_DH_KEY_FILENAME),
}
}

Expand All @@ -135,13 +126,6 @@ impl KeysPaths {
&self.public_x25519_noise_key_file,
)
}

pub fn x25519_wireguard_storage_paths(&self) -> nym_pemstore::KeyPairPath {
nym_pemstore::KeyPairPath::new(
&self.private_x25519_wireguard_key_file,
&self.public_x25519_wireguard_key_file,
)
}
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Serialize)]
Expand Down Expand Up @@ -398,4 +382,11 @@ impl WireguardPaths {
public_diffie_hellman_key_file: data_dir.join(DEFAULT_X25519_WG_PUBLIC_DH_KEY_FILENAME),
}
}

pub fn x25519_wireguard_storage_paths(&self) -> nym_pemstore::KeyPairPath {
nym_pemstore::KeyPairPath::new(
&self.private_diffie_hellman_key_file,
&self.public_diffie_hellman_key_file,
)
}
}
4 changes: 2 additions & 2 deletions nym-node/src/config/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ private_network_prefix = {{ wireguard.private_network_prefix }}

[wireguard.storage_paths]
# Path to file containing wireguard x25519 diffie hellman private key.
private_x25519_wireguard_key_file = '{{ wireguard.storage_paths.private_diffie_hellman_key_file }}'
private_diffie_hellman_key_file = '{{ wireguard.storage_paths.private_diffie_hellman_key_file }}'

# Path to file containing wireguard x25519 diffie hellman public key.
public_x25519_wireguard_key_file = '{{ wireguard.storage_paths.public_diffie_hellman_key_file }}'
public_diffie_hellman_key_file = '{{ wireguard.storage_paths.public_diffie_hellman_key_file }}'


##### mixnode mode nym-node config options #####
Expand Down
7 changes: 0 additions & 7 deletions nym-node/src/node/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,3 @@ pub(crate) fn store_x25519_noise_keypair(
) -> Result<(), NymNodeError> {
Ok(store_keypair(keys, paths, "x25519-noise")?)
}

pub(crate) fn store_x25519_wireguard_keypair(
keys: &x25519::KeyPair,
paths: KeyPairPath,
) -> Result<(), NymNodeError> {
Ok(store_keypair(keys, paths, "x25519-wireguard")?)
}
59 changes: 41 additions & 18 deletions nym-node/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::node::description::{load_node_description, save_node_description};
use crate::node::helpers::{
load_ed25519_identity_keypair, load_key, load_x25519_noise_keypair, load_x25519_sphinx_keypair,
store_ed25519_identity_keypair, store_key, store_keypair, store_x25519_noise_keypair,
store_x25519_sphinx_keypair, store_x25519_wireguard_keypair, DisplayDetails,
store_x25519_sphinx_keypair, DisplayDetails,
};
use crate::node::http::{sign_host_details, system_info::get_system_info};
use ipnetwork::IpNetwork;
Expand All @@ -20,7 +20,9 @@ use nym_network_requester::{
use nym_node::config::entry_gateway::ephemeral_entry_gateway_config;
use nym_node::config::exit_gateway::ephemeral_exit_gateway_config;
use nym_node::config::mixnode::ephemeral_mixnode_config;
use nym_node::config::{Config, EntryGatewayConfig, ExitGatewayConfig, MixnodeConfig, NodeMode};
use nym_node::config::{
Config, EntryGatewayConfig, ExitGatewayConfig, MixnodeConfig, NodeMode, Wireguard,
};
use nym_node::error::{EntryGatewayError, ExitGatewayError, MixnodeError, NymNodeError};
use nym_node_http_api::api::api_requests;
use nym_node_http_api::api::api_requests::v1::node::models::NodeDescription;
Expand Down Expand Up @@ -246,6 +248,33 @@ impl ExitGatewayData {
}
}

pub struct WireguardData {
x25519_wireguard_keys: Arc<x25519::KeyPair>,
}

impl WireguardData {
pub(crate) fn new(config: &Wireguard) -> Result<Self, NymNodeError> {
Ok(WireguardData {
x25519_wireguard_keys: Arc::new(load_x25519_wireguard_keypair(
config.storage_paths.x25519_wireguard_storage_paths(),
)?),
})
}

pub(crate) fn initialise(config: &Wireguard) -> Result<(), ExitGatewayError> {
let mut rng = OsRng;
let x25519_keys = x25519::KeyPair::new(&mut rng);

store_keypair(
&x25519_keys,
config.storage_paths.x25519_wireguard_storage_paths(),
"wg-x25519-dh",
)?;

Ok(())
}
}

pub(crate) struct NymNode {
config: Config,
description: NodeDescription,
Expand All @@ -261,11 +290,11 @@ pub(crate) struct NymNode {
#[allow(dead_code)]
exit_gateway: ExitGatewayData,

wireguard: WireguardData,

ed25519_identity_keys: Arc<ed25519::KeyPair>,
x25519_sphinx_keys: Arc<x25519::KeyPair>,

x25519_wireguard_keys: Arc<x25519::KeyPair>,

// to be used when noise is integrated
#[allow(dead_code)]
x25519_noise_keys: Arc<x25519::KeyPair>,
Expand All @@ -283,7 +312,6 @@ impl NymNode {
let ed25519_identity_keys = ed25519::KeyPair::new(&mut rng);
let x25519_sphinx_keys = x25519::KeyPair::new(&mut rng);
let x25519_noise_keys = x25519::KeyPair::new(&mut rng);
let x25519_wireguard_keys = x25519::KeyPair::new(&mut rng);

trace!("attempting to store ed25519 identity keypair");
store_ed25519_identity_keypair(
Expand All @@ -303,12 +331,6 @@ impl NymNode {
config.storage_paths.keys.x25519_noise_storage_paths(),
)?;

trace!("attempting to store x25519 wireguard keypair");
store_x25519_wireguard_keypair(
&x25519_wireguard_keys,
config.storage_paths.keys.x25519_wireguard_storage_paths(),
)?;

trace!("creating description file");
save_node_description(
&config.storage_paths.description,
Expand All @@ -325,6 +347,9 @@ impl NymNode {
ExitGatewayData::initialise(&config.exit_gateway, *ed25519_identity_keys.public_key())
.await?;

// wireguard initialisation
WireguardData::initialise(&config.wireguard)?;

config.save()
}

Expand All @@ -339,14 +364,12 @@ impl NymNode {
x25519_noise_keys: Arc::new(load_x25519_noise_keypair(
config.storage_paths.keys.x25519_noise_storage_paths(),
)?),
x25519_wireguard_keys: Arc::new(load_x25519_wireguard_keypair(
config.storage_paths.keys.x25519_wireguard_storage_paths(),
)?),
description: load_node_description(&config.storage_paths.description)?,
verloc_stats: Default::default(),
mixnode: MixnodeData::new(&config.mixnode)?,
entry_gateway: EntryGatewayData::new(&config.entry_gateway).await?,
exit_gateway: ExitGatewayData::new(&config.exit_gateway)?,
wireguard: WireguardData::new(&config.wireguard)?,
config,
})
}
Expand All @@ -367,6 +390,10 @@ impl NymNode {
)
}

fn x25519_wireguard_key(&self) -> &x25519::PublicKey {
self.wireguard.x25519_wireguard_keys.public_key()
}

pub(crate) fn display_details(&self) -> DisplayDetails {
DisplayDetails {
current_mode: self.config.mode,
Expand Down Expand Up @@ -396,10 +423,6 @@ impl NymNode {
self.x25519_noise_keys.public_key()
}

pub(crate) fn x25519_wireguard_key(&self) -> &x25519::PublicKey {
self.x25519_wireguard_keys.public_key()
}

fn start_mixnode(&self, task_client: TaskClient) -> Result<(), NymNodeError> {
info!("going to start the nym-node in MIXNODE mode");

Expand Down

0 comments on commit 870570d

Please sign in to comment.