Skip to content

Commit

Permalink
Add CMake buildsystem and develop bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasFella committed Aug 14, 2024
1 parent c952244 commit ef326e2
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 16 deletions.
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.24)

project(Vodozemac-cpp)

find_package(Corrosion REQUIRED)

corrosion_import_crate(
MANIFEST_PATH Cargo.toml
)

add_custom_target(copy_header ALL DEPENDS cargo-build_vodozemac BYPRODUCTS ${CMAKE_BINARY_DIR}/vodozemac.h VERBATIM COMMAND
cp -L ${CMAKE_BINARY_DIR}/cargo/build/${Rust_CARGO_TARGET}/cxxbridge/vodozemac/src/lib.rs.h ${CMAKE_BINARY_DIR}/vodozemac.h
)

corrosion_install(TARGETS vodozemac)
install(FILES "${CMAKE_BINARY_DIR}/vodozemac.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/vodozemac)
9 changes: 9 additions & 0 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ impl OlmMessage {
message_type,
}
}

pub fn message_type(&self) -> u8 {
self.0.message_type() as u8
}
}

pub fn olm_message_from_parts(parts: &OlmMessageParts) -> Result<Box<OlmMessage>, anyhow::Error> {
Expand All @@ -40,6 +44,11 @@ pub fn account_from_pickle(
Ok(Account(vodozemac::olm::Account::from_pickle(pickle)).into())
}

pub fn account_from_olm_pickle(pickle: &str, pickle_key: &[u8; 32]) -> Result<Box<Account>, anyhow::Error> {
Ok(Box::new(Account(vodozemac::olm::Account::from_libolm_pickle(pickle, pickle_key)?)))
}


impl From<vodozemac::olm::InboundCreationResult> for InboundCreationResult {
fn from(v: vodozemac::olm::InboundCreationResult) -> Self {
Self {
Expand Down
16 changes: 16 additions & 0 deletions src/group_sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ pub fn group_session_from_pickle(pickle: &str, pickle_key: &[u8; 32]) -> Result<
Ok(GroupSession(vodozemac::megolm::GroupSession::from_pickle(pickle)).into())
}

pub fn group_session_from_olm_pickle(
pickle: &str,
pickle_key: &[u8; 32],
) -> Result<Box<GroupSession>> {
Ok(Box::new(GroupSession(vodozemac::megolm::GroupSession::from_libolm_pickle(pickle, pickle_key)?)))
}


pub struct InboundGroupSession(vodozemac::megolm::InboundGroupSession);

pub fn new_inbound_group_session(session_key: &SessionKey) -> Box<InboundGroupSession> {
Expand All @@ -93,6 +101,14 @@ pub fn inbound_group_session_from_pickle(
Ok(InboundGroupSession(vodozemac::megolm::InboundGroupSession::from_pickle(pickle)).into())
}

pub fn inbound_group_session_from_olm_pickle(
pickle: &str,
pickle_key: &[u8; 32],
) -> Result<Box<InboundGroupSession>> {
Ok(Box::new(InboundGroupSession(vodozemac::megolm::InboundGroupSession::from_libolm_pickle(pickle, pickle_key)?)))
}


impl InboundGroupSession {
fn new(session_key: &SessionKey) -> Self {
Self(vodozemac::megolm::InboundGroupSession::new(&session_key.0, SessionConfig::version_1()))
Expand Down
44 changes: 36 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ mod sas;
mod session;
mod types;

use account::{account_from_pickle, new_account, olm_message_from_parts, Account, OlmMessage};
use account::{account_from_pickle, account_from_olm_pickle, new_account, olm_message_from_parts, Account, OlmMessage};
use group_sessions::{
exported_session_key_from_base64, group_session_from_pickle, import_inbound_group_session,
inbound_group_session_from_pickle, megolm_message_from_base64, new_group_session,
new_inbound_group_session, session_key_from_base64, ExportedSessionKey, GroupSession,
InboundGroupSession, MegolmMessage, SessionKey,
InboundGroupSession, MegolmMessage, SessionKey, group_session_from_olm_pickle, inbound_group_session_from_olm_pickle,
};
use sas::{mac_from_base64, new_sas, EstablishedSas, Mac, Sas, SasBytes};
use session::{session_from_pickle, Session};
use session::{session_from_pickle, Session, SessionDecryptResult, session_decrypt_result_value, session_from_olm_pickle};
use types::{
curve_key_from_base64, ed25519_key_from_base64, Curve25519PublicKey, Ed25519PublicKey,
Ed25519Signature,
curve25519_public_key_from_base64, ed25519_public_key_from_base64, ed25519_signature_from_base64, ed25519_secret_key_from_base64, Curve25519PublicKey, Ed25519PublicKey,
Ed25519Signature, Ed25519SecretKey, Ed25519SignatureFromBase64Result, Ed25519SecretKeyFromBase64Result, Curve25519PublicKeyFromBase64Result, Ed25519PublicKeyFromBase64Result, curve25519_public_key_from_base64_result_value, ed25519_secret_key_from_base64_result_value, ed25519_public_key_from_base64_result_value, ed25519_signature_from_base64_result_value
};

#[cxx::bridge]
Expand Down Expand Up @@ -49,12 +49,32 @@ mod ffi {
#[namespace = "types"]
extern "Rust" {
type Curve25519PublicKey;
fn curve_key_from_base64(key: &str) -> Result<Box<Curve25519PublicKey>>;
type Curve25519PublicKeyFromBase64Result;
fn curve25519_public_key_from_base64(key: &str) -> Box<Curve25519PublicKeyFromBase64Result>;
fn to_base64(self: &Curve25519PublicKey) -> String;
fn has_error(self: &Curve25519PublicKeyFromBase64Result) -> bool;
fn curve25519_public_key_from_base64_result_value(result: Box<Curve25519PublicKeyFromBase64Result>) -> Box<Curve25519PublicKey>;

type Ed25519PublicKey;
fn ed25519_key_from_base64(key: &str) -> Result<Box<Ed25519PublicKey>>;
type Ed25519PublicKeyFromBase64Result;
fn ed25519_public_key_from_base64(key: &str) -> Box<Ed25519PublicKeyFromBase64Result>;
fn to_base64(self: &Ed25519PublicKey) -> String;
fn verify(self: &Ed25519PublicKey, message: &[u8], signature: &Ed25519Signature) -> bool;
fn ed25519_public_key_from_base64_result_value(result: Box<Ed25519PublicKeyFromBase64Result>) -> Box<Ed25519PublicKey>;

type Ed25519Signature;
type Ed25519SignatureFromBase64Result;
fn to_base64(self: &Ed25519Signature) -> String;
fn ed25519_signature_from_base64(key: &str) -> Box<Ed25519SignatureFromBase64Result>;
fn ed25519_signature_from_base64_result_value(result: Box<Ed25519SignatureFromBase64Result>) -> Box<Ed25519Signature>;

type Ed25519SecretKey;
type Ed25519SecretKeyFromBase64Result;
fn ed25519_secret_key_from_base64(key: &str) -> Box<Ed25519SecretKeyFromBase64Result>;
fn sign(self: &Ed25519SecretKey, message: &[u8]) -> Box<Ed25519Signature>;
fn ed25519_secret_key_from_base64_result_value(result: Box<Ed25519SecretKeyFromBase64Result>) -> Box<Ed25519SecretKey>;


}

#[namespace = "olm"]
Expand All @@ -71,6 +91,7 @@ mod ffi {
fn mark_keys_as_published(self: &mut Account);
fn max_number_of_one_time_keys(self: &Account) -> usize;
fn account_from_pickle(pickle: &str, pickle_key: &[u8; 32]) -> Result<Box<Account>>;
fn account_from_olm_pickle(pickle: &str, pickle_key: &[u8; 32]) -> Result<Box<Account>>;
fn pickle(self: &Account, pickle_key: &[u8; 32]) -> String;
fn create_outbound_session(
self: &Account,
Expand All @@ -84,17 +105,22 @@ mod ffi {
) -> Result<InboundCreationResult>;

type Session;
type SessionDecryptResult;
fn session_id(self: &Session) -> String;
fn session_keys(self: &Session) -> SessionKeys;
fn session_matches(self: &Session, message: &OlmMessage) -> bool;
fn encrypt(self: &mut Session, plaintext: &str) -> Box<OlmMessage>;
fn decrypt(self: &mut Session, message: &OlmMessage) -> Result<String>;
fn decrypt(self: &mut Session, message: &OlmMessage) -> Box<SessionDecryptResult>;
fn session_from_pickle(pickle: &str, pickle_key: &[u8; 32]) -> Result<Box<Session>>;
fn session_from_olm_pickle(pickle: &str, pickle_key: &[u8; 32]) -> Result<Box<Session>>;
fn pickle(self: &Session, pickle_key: &[u8; 32]) -> String;
fn session_decrypt_result_value(result: Box<SessionDecryptResult>) -> String;
fn has_error(self: &SessionDecryptResult) -> bool;

type OlmMessage;
fn to_parts(self: &OlmMessage) -> OlmMessageParts;
fn olm_message_from_parts(parts: &OlmMessageParts) -> Result<Box<OlmMessage>>;
fn message_type(self: &OlmMessage) -> u8;
}

#[namespace = "megolm"]
Expand Down Expand Up @@ -128,6 +154,7 @@ mod ffi {
pickle: &str,
pickle_key: &[u8; 32],
) -> Result<Box<GroupSession>>;
fn group_session_from_olm_pickle(pickle: &str, pickle_key: &[u8; 32]) -> Result<Box<GroupSession>>;

type InboundGroupSession;
fn new_inbound_group_session(session_key: &SessionKey) -> Box<InboundGroupSession>;
Expand All @@ -149,6 +176,7 @@ mod ffi {
pickle: &str,
pickle_key: &[u8; 32],
) -> Result<Box<InboundGroupSession>>;
fn inbound_group_session_from_olm_pickle(pickle: &str, pickle_key: &[u8; 32]) -> Result<Box<InboundGroupSession>>;
}

#[namespace = "sas"]
Expand Down
37 changes: 35 additions & 2 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ impl Session {
OlmMessage(self.0.encrypt(plaintext)).into()
}

pub fn decrypt(&mut self, message: &OlmMessage) -> Result<String, anyhow::Error> {
Ok(std::str::from_utf8(&self.0.decrypt(&message.0)?)?.to_string())
pub fn decrypt(&mut self, message: &OlmMessage) -> Box<SessionDecryptResult> {
Box::new(SessionDecryptResult(self.0.decrypt(&message.0).map(|it| String::from_utf8_lossy(&it).to_string())))
}

pub fn session_keys(&self) -> SessionKeys {
Expand All @@ -38,10 +38,43 @@ impl Session {
}
}

pub struct SessionDecryptResult(pub(crate) Result<String, vodozemac::olm::DecryptionError>);

impl SessionDecryptResult {
pub fn has_error(&self) -> bool {
self.0.is_err()
}

pub fn error_code(&self) -> u8 {
match &self.0 {
Ok(_) => 0,
Err(error) => match error {
vodozemac::olm::DecryptionError::InvalidMAC(_) => 1,
vodozemac::olm::DecryptionError::InvalidMACLength(_, _) => 2,
vodozemac::olm::DecryptionError::InvalidPadding(_) => 3,
vodozemac::olm::DecryptionError::MissingMessageKey(_) => 4,
vodozemac::olm::DecryptionError::TooBigMessageGap(_, _) => 5,
}
}
}

pub fn value(self) -> String {
self.0.unwrap()
}
}

pub fn session_decrypt_result_value(result: Box<SessionDecryptResult>) -> String {
result.value()
}

pub fn session_from_pickle(
pickle: &str,
pickle_key: &[u8; 32],
) -> Result<Box<Session>, anyhow::Error> {
let pickle = vodozemac::olm::SessionPickle::from_encrypted(pickle, pickle_key)?;
Ok(Session(vodozemac::olm::Session::from_pickle(pickle)).into())
}

pub fn session_from_olm_pickle(pickle: &str, pickle_key: &[u8; 32]) -> Result<Box<Session>, anyhow::Error> {
Ok(Box::new(Session(vodozemac::olm::Session::from_libolm_pickle(pickle, pickle_key)?)))
}
Loading

0 comments on commit ef326e2

Please sign in to comment.