remove asymetric ecryption
This commit is contained in:
parent
13223b5417
commit
fb0a1644b7
234
Cargo.lock
generated
234
Cargo.lock
generated
@ -177,18 +177,6 @@ version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
||||
|
||||
[[package]]
|
||||
name = "base64ct"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "55248b47b0caf0546f7988906588779981c43bb1bc9d0c44087278f80cdb44ba"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.9.1"
|
||||
@ -208,12 +196,6 @@ dependencies = [
|
||||
"piper",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.1"
|
||||
@ -275,49 +257,12 @@ dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "const-oid"
|
||||
version = "0.9.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
||||
|
||||
[[package]]
|
||||
name = "crypto-common"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "der"
|
||||
version = "0.7.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb"
|
||||
dependencies = [
|
||||
"const-oid",
|
||||
"pem-rfc7468",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
version = "0.10.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
|
||||
dependencies = [
|
||||
"const-oid",
|
||||
"crypto-common",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.13"
|
||||
@ -380,16 +325,6 @@ dependencies = [
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
|
||||
dependencies = [
|
||||
"typenum",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.16"
|
||||
@ -419,80 +354,18 @@ version = "1.70.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
dependencies = [
|
||||
"spin",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.174"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
|
||||
|
||||
[[package]]
|
||||
name = "libm"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de"
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12"
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint-dig"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc84195820f291c7697304f3cbdadd1cb7199c0efc917ff5eafd71225c136151"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"lazy_static",
|
||||
"libm",
|
||||
"num-integer",
|
||||
"num-iter",
|
||||
"num-traits",
|
||||
"rand",
|
||||
"smallvec",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.46"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-iter"
|
||||
version = "0.1.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"libm",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.21.3"
|
||||
@ -517,20 +390,10 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"rand",
|
||||
"rsa",
|
||||
"smol",
|
||||
"tappers",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pem-rfc7468"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "88b39c9bfcfc231068454382784bb460aae594343fb030d46e9f50a645418412"
|
||||
dependencies = [
|
||||
"base64ct",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.16"
|
||||
@ -548,27 +411,6 @@ dependencies = [
|
||||
"futures-io",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pkcs1"
|
||||
version = "0.7.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c8ffb9f10fa047879315e6625af03c164b16962a5368d724ed16323b68ace47f"
|
||||
dependencies = [
|
||||
"der",
|
||||
"pkcs8",
|
||||
"spki",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pkcs8"
|
||||
version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7"
|
||||
dependencies = [
|
||||
"der",
|
||||
"spki",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "polling"
|
||||
version = "3.9.0"
|
||||
@ -640,26 +482,6 @@ dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rsa"
|
||||
version = "0.9.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78928ac1ed176a5ca1d17e578a1825f3d81ca54cf41053a592584b020cfd691b"
|
||||
dependencies = [
|
||||
"const-oid",
|
||||
"digest",
|
||||
"num-bigint-dig",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
"pkcs1",
|
||||
"pkcs8",
|
||||
"rand_core",
|
||||
"signature",
|
||||
"spki",
|
||||
"subtle",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "1.0.8"
|
||||
@ -682,28 +504,12 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signature"
|
||||
version = "2.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de"
|
||||
dependencies = [
|
||||
"digest",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "slab"
|
||||
version = "0.4.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04dc19736151f35336d325007ac991178d504a119863a2fcb3758cdb5e52c50d"
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.15.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
|
||||
|
||||
[[package]]
|
||||
name = "smol"
|
||||
version = "2.0.2"
|
||||
@ -721,34 +527,12 @@ dependencies = [
|
||||
"futures-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.9.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
|
||||
|
||||
[[package]]
|
||||
name = "spki"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d"
|
||||
dependencies = [
|
||||
"base64ct",
|
||||
"der",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "subtle"
|
||||
version = "2.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.104"
|
||||
@ -771,12 +555,6 @@ dependencies = [
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.18.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.18"
|
||||
@ -789,12 +567,6 @@ version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.1+wasi-snapshot-preview1"
|
||||
@ -966,9 +738,3 @@ dependencies = [
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zeroize"
|
||||
version = "1.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde"
|
||||
|
@ -15,6 +15,5 @@ path = "src/client/main.rs"
|
||||
[dependencies]
|
||||
clap = { version = "4.5.41", features = ["derive"] }
|
||||
rand = "0.8.5"
|
||||
rsa = "0.9.8"
|
||||
smol = "2.0.2"
|
||||
tappers = "0.4.2"
|
||||
|
@ -1,8 +1,42 @@
|
||||
use std::net::UdpSocket;
|
||||
use pea_2_pea::SERVER_PORT;
|
||||
|
||||
use std::{
|
||||
io::{Read, Write},
|
||||
net::UdpSocket,
|
||||
process::exit,
|
||||
};
|
||||
|
||||
#[derive(clap::Parser)]
|
||||
#[command(name = "pea_2_pea")]
|
||||
#[command(about = "A CLI tool for pea_2_pea P2P vpn client")]
|
||||
struct Cli {
|
||||
#[arg(short = 'r', long = "registrar")]
|
||||
#[arg(help = "registrar ip address or hostname")]
|
||||
registrar: String,
|
||||
|
||||
#[arg(short = 'v', long = "verbose")]
|
||||
verbose: bool,
|
||||
|
||||
#[arg(short = 'V', long = "version")]
|
||||
version: bool,
|
||||
|
||||
#[arg(short = 'p', long = "registrar-port")]
|
||||
#[arg(help = format!("Port number for the registrar service (1-65535) Default: {}", SERVER_PORT))]
|
||||
registrar_port: Option<u16>,
|
||||
|
||||
#[arg(short = 'P', long = "bind-port")]
|
||||
bind_port: Option<u16>,
|
||||
}
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
let cli = <Cli as clap::Parser>::parse();
|
||||
{
|
||||
let socket: UdpSocket = (|| -> std::io::Result<UdpSocket> {
|
||||
let mut port: u16 = 59999;
|
||||
let mut port: u16;
|
||||
match cli.bind_port {
|
||||
Some(port_proveded) => port = port_proveded,
|
||||
None => port = 59999, // Magic number
|
||||
}
|
||||
loop {
|
||||
port += 1;
|
||||
match UdpSocket::bind(format!("0.0.0.0:{}", port)) {
|
||||
@ -12,6 +46,32 @@ fn main() -> std::io::Result<()> {
|
||||
}
|
||||
})()
|
||||
.expect("Failed to bind to any available port");
|
||||
|
||||
// send query request to get server public key
|
||||
let server_port: u16 = (|| -> u16 {
|
||||
match cli.bind_port {
|
||||
Some(port_proveded) => return port_proveded,
|
||||
None => return pea_2_pea::SERVER_PORT,
|
||||
}
|
||||
})();
|
||||
|
||||
let server_SocketAddr: core::net::SocketAddr = format!("{}:{}", cli.registrar, server_port)
|
||||
.parse()
|
||||
.unwrap();
|
||||
|
||||
let mut buf: [u8; pea_2_pea::BUFFER_SIZE] = [0; pea_2_pea::BUFFER_SIZE];
|
||||
loop {
|
||||
match socket.recv_from(&mut buf) {
|
||||
Ok((data_length, src)) => {}
|
||||
Err(e) => {
|
||||
eprintln!("Error receiving data: {}", e);
|
||||
std::process::exit(-4);
|
||||
}
|
||||
}
|
||||
let mut out = std::io::stdout();
|
||||
out.write_all(&buf)?;
|
||||
out.flush()?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -6,10 +6,9 @@ pub const RSA_SIZE: usize = 2048;
|
||||
|
||||
#[repr(u8)]
|
||||
pub enum ServerMethods {
|
||||
QUERY = 0,
|
||||
REGISTER = 1,
|
||||
GET = 2,
|
||||
HEARTBEAT = 3,
|
||||
REGISTER = 0,
|
||||
GET = 1,
|
||||
HEARTBEAT = 2,
|
||||
}
|
||||
|
||||
pub mod shared;
|
||||
|
@ -1,12 +1,11 @@
|
||||
mod net;
|
||||
use std::{net::UdpSocket, process::exit, sync::Arc};
|
||||
|
||||
use rsa::pkcs8::der::zeroize::Zeroize;
|
||||
fn main() -> std::io::Result<()> {
|
||||
{
|
||||
let socket: Arc<UdpSocket> = Arc::new(
|
||||
(|| -> std::io::Result<UdpSocket> {
|
||||
let listen_port: u16 = 60000;
|
||||
let listen_port: u16 = pea_2_pea::SERVER_PORT;
|
||||
match UdpSocket::bind(format!("0.0.0.0:{}", listen_port)) {
|
||||
Ok(socket) => return Ok(socket),
|
||||
Err(e) => return Err(e),
|
||||
@ -15,22 +14,12 @@ fn main() -> std::io::Result<()> {
|
||||
.expect("Failed to bind to any available port"),
|
||||
);
|
||||
|
||||
let server_key_pear: pea_2_pea::shared::crypto::KeyPair =
|
||||
pea_2_pea::shared::crypto::generate_rsa_key_pair();
|
||||
|
||||
let mut buf: [u8; pea_2_pea::BUFFER_SIZE] = [0; pea_2_pea::BUFFER_SIZE];
|
||||
smol::block_on(async {
|
||||
loop {
|
||||
buf.zeroize();
|
||||
match socket.recv_from(&mut buf) {
|
||||
Ok((data_length, src)) => {
|
||||
smol::spawn(net::handle_request(
|
||||
buf,
|
||||
socket.clone(),
|
||||
src,
|
||||
data_length,
|
||||
server_key_pear.clone(),
|
||||
))
|
||||
smol::spawn(net::handle_request(buf, socket.clone(), src, data_length))
|
||||
.detach();
|
||||
}
|
||||
Err(e) => {
|
||||
|
@ -1,47 +1,12 @@
|
||||
use rsa::{Pkcs1v15Encrypt, pkcs1::EncodeRsaPublicKey, pkcs8::DecodePublicKey};
|
||||
pub async fn handle_request(
|
||||
buf: [u8; pea_2_pea::BUFFER_SIZE],
|
||||
socket: std::sync::Arc<std::net::UdpSocket>,
|
||||
src: core::net::SocketAddr,
|
||||
data_len: usize,
|
||||
server_key_pair: pea_2_pea::shared::crypto::KeyPair,
|
||||
) {
|
||||
let mut rng: rand::prelude::ThreadRng = rand::thread_rng();
|
||||
|
||||
match buf[0] {
|
||||
x if x == pea_2_pea::ServerMethods::QUERY as u8 => {
|
||||
#[cfg(debug_assertions)]
|
||||
eprintln!("QUERY method");
|
||||
|
||||
let client_public_key: rsa::RsaPublicKey =
|
||||
match rsa::RsaPublicKey::from_public_key_der(&buf[1..]) {
|
||||
Ok(key) => key,
|
||||
Err(e) => {
|
||||
eprintln!("Error serializing client key: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let pkcs_encoded_public_key: rsa::pkcs8::Document =
|
||||
server_key_pair.public_key.to_pkcs1_der().unwrap();
|
||||
let mut send_vec: Vec<u8> = pkcs_encoded_public_key.into_vec();
|
||||
send_vec.insert(0, pea_2_pea::ServerMethods::QUERY as u8);
|
||||
|
||||
let send_vec_encrypted: Vec<u8> = client_public_key
|
||||
.encrypt(&mut rng, Pkcs1v15Encrypt, &send_vec)
|
||||
.unwrap();
|
||||
|
||||
match socket.send_to(&send_vec_encrypted, &src) {
|
||||
Ok(s) => {
|
||||
#[cfg(debug_assertions)]
|
||||
eprintln!("send {} bytes", s);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error snding data: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
x if x == pea_2_pea::ServerMethods::GET as u8 => {
|
||||
#[cfg(debug_assertions)]
|
||||
println!("GET method");
|
||||
|
@ -1,21 +1 @@
|
||||
#[derive(Clone)]
|
||||
pub struct KeyPair {
|
||||
pub private_key: rsa::RsaPrivateKey,
|
||||
pub public_key: rsa::RsaPublicKey,
|
||||
}
|
||||
pub fn generate_rsa_private_key() -> Result<rsa::RsaPrivateKey, rsa::Error> {
|
||||
let mut rng: rand::prelude::ThreadRng = rand::thread_rng();
|
||||
let bits: usize = 2048;
|
||||
|
||||
return rsa::RsaPrivateKey::new(&mut rng, bits);
|
||||
}
|
||||
|
||||
pub fn generate_rsa_key_pair() -> KeyPair {
|
||||
let private_key: rsa::RsaPrivateKey = generate_rsa_private_key().unwrap();
|
||||
let public_key: rsa::RsaPublicKey = rsa::RsaPublicKey::from(&private_key);
|
||||
|
||||
KeyPair {
|
||||
private_key,
|
||||
public_key,
|
||||
}
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
pub mod crypto;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user