Skip to content

Commit

Permalink
Expose PresharedKey peer parameter from the new_tunnel library functi…
Browse files Browse the repository at this point in the history
…on. (cloudflare#198)

Co-authored-by: Vadim Smirnov <[email protected]>
  • Loading branch information
wiresock and wiresock committed Dec 21, 2021
1 parent 8618234 commit a133f1d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/// C bindings for the BoringTun library
pub mod benchmark;
use self::benchmark::do_benchmark;
use super::noise::{Tunn, TunnResult};
use super::noise::{make_array, Tunn, TunnResult};
use crate::crypto::{X25519PublicKey, X25519SecretKey};
use base64::{decode, encode};
use hex::encode as encode_hex;
Expand Down Expand Up @@ -159,6 +159,7 @@ pub unsafe extern "C" fn check_base64_encoded_x25519_key(key: *const c_char) ->
pub unsafe extern "C" fn new_tunnel(
static_private: *const c_char,
server_static_public: *const c_char,
preshared_key: *const c_char,
keep_alive: u16,
index: u32,
) -> *mut Tunn {
Expand All @@ -174,6 +175,15 @@ pub unsafe extern "C" fn new_tunnel(
Ok(string) => string,
};

let c_str = CStr::from_ptr(preshared_key);
let preshared_key = match c_str.to_str() {
Err(_) => None,
Ok(string) => match string.parse::<X25519PublicKey>() {
Ok(key) => Some(make_array(key.as_bytes())),
Err(_) => None,
},
};

let private_key = match static_private.parse() {
Err(_) => return ptr::null_mut(),
Ok(key) => key,
Expand All @@ -193,7 +203,7 @@ pub unsafe extern "C" fn new_tunnel(
let tunnel = match Tunn::new(
Arc::new(private_key),
Arc::new(public_key),
None,
preshared_key,
keep_alive,
index,
None,
Expand Down
1 change: 1 addition & 0 deletions src/wireguard_ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ int check_base64_encoded_x25519_key(const char *key);
// Allocate a new tunnel
struct wireguard_tunnel *new_tunnel(const char *static_private,
const char *server_static_public,
const char *preshared_key,
uint16_t keep_alive, // Keep alive interval in seconds
uint32_t index, // The 24bit index prefix to be used for session indexes
void (*log_printer)(const char *),
Expand Down

0 comments on commit a133f1d

Please sign in to comment.