summary history branches tags files
commit:ff034bf66c0afe8dc4eda96f08e201237142dd0e
author:Trevor Bentley
committer:Trevor Bentley
date:Sun May 19 22:23:28 2019 +0200
parents:4577ac87b11fe20fc8b932836d23c8fe9b807e16
expose generate_auth_keypair in FFI
diff --git a/ffi/ossuary.h b/ffi/ossuary.h
line changes: +2/-0
index f1ee180..04d3b13
--- a/ffi/ossuary.h
+++ b/ffi/ossuary.h
@@ -23,6 +23,8 @@ int32_t ossuary_add_authorized_key(OssuaryConnection *conn, const uint8_t key[32
 int32_t ossuary_add_authorized_keys(OssuaryConnection *conn, uint8_t *key[], uint8_t count);
 int32_t ossuary_remote_public_key(OssuaryConnection *conn,
                                   uint8_t *key_buf, uint16_t key_buf_len);
+int32_t ossuary_generate_auth_keypair(uint8_t *secret_buf, uint16_t secret_buf_len,
+                                      uint8_t *public_buf, uint16_t public_buf_len);
 int32_t ossuary_recv_handshake(OssuaryConnection *conn,
                                uint8_t *in_buf, uint16_t *in_buf_len);
 int32_t ossuary_send_handshake(OssuaryConnection *conn,

diff --git a/src/clib.rs b/src/clib.rs
line changes: +35/-9
index 787e76f..7418285
--- a/src/clib.rs
+++ b/src/clib.rs
@@ -1,4 +1,4 @@
-use crate::{OssuaryConnection, ConnectionType, OssuaryError, KEY_LEN};
+use crate::{OssuaryConnection, ConnectionType, OssuaryError, KEY_LEN, generate_auth_keypair};
 
 pub const OSSUARY_ERR_WOULD_BLOCK: i32 = -64;
 pub const OSSUARY_ERR_UNTRUSTED_SERVER: i32 = -65;
@@ -45,12 +45,12 @@ pub extern "C" fn ossuary_add_authorized_key(conn: *mut OssuaryConnection,
     let r_key_buf: &[u8] = unsafe {
         std::slice::from_raw_parts(key_buf, KEY_LEN)
     };
-    let added = match conn.add_authorized_key(r_key_buf) {
-        Ok(_) => true,
-        Err(_) => false,
+    let res = match conn.add_authorized_key(r_key_buf) {
+        Ok(_) => 0i32,
+        Err(_) => -1i32,
     };
     ::std::mem::forget(conn);
-    added as i32
+    res
 }
 
 #[no_mangle]
@@ -253,13 +253,39 @@ pub extern "C" fn ossuary_remote_public_key(conn: *mut OssuaryConnection,
     let r_key_buf: &mut [u8] = unsafe {
         std::slice::from_raw_parts_mut(key_buf, KEY_LEN)
     };
-    let found = match conn.remote_public_key() {
+    let res = match conn.remote_public_key() {
         Ok(key) => {
             r_key_buf.copy_from_slice(key);
-            true
+            0i32
         },
-        Err(_) => false,
+        Err(_) => -1i32,
     };
     ::std::mem::forget(conn);
-    found as i32
+    res
+}
+
+#[no_mangle]
+pub extern "C" fn ossuary_generate_auth_keypair(secret_buf: *mut u8, secret_buf_len: u16,
+                                                public_buf: *mut u8, public_buf_len: u16) -> i32 {
+    if secret_buf.is_null() || public_buf.is_null() {
+        return -1i32;
+    }
+    if secret_buf_len < KEY_LEN as u16 || public_buf_len < KEY_LEN as u16 {
+        return -1i32;
+    }
+    let r_secret: &mut [u8] = unsafe {
+        std::slice::from_raw_parts_mut(secret_buf, KEY_LEN)
+    };
+    let r_public: &mut [u8] = unsafe {
+        std::slice::from_raw_parts_mut(public_buf, KEY_LEN)
+    };
+    let res = match generate_auth_keypair() {
+        Ok((s,p)) => {
+            r_secret.copy_from_slice(&s);
+            r_public.copy_from_slice(&p);
+            0i32
+        }
+        _ => -1i32,
+    };
+    res
 }

diff --git a/src/lib.rs b/src/lib.rs
line changes: +0/-1
index c7d5bf4..26e37fe
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -129,7 +129,6 @@
 //  - limit connection retries
 //  - tests should check their received strings
 //  - rustdoc everything
-//  - expose generate_auth_keypair in FFI
 //
 
 pub mod clib;