Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ffi: Expose CiphertextMessageType constants #17

Merged
merged 2 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions rust/bridge/ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ crate-type = ["staticlib"]
libsignal-protocol-rust = { path = "../../protocol" }
rand = "0.7.3"
libc = "0.2"
num-traits = "0.2"
num-derive = "0.3"
static_assertions = "1.1"

[build-dependencies]
cbindgen = "0.14"
3 changes: 2 additions & 1 deletion rust/bridge/ffi/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ autogen_warning = "/* This file was automatically generated by cbindgen */"
prefix_with_name = true

[export]
include = ["SignalErrorCode", "FfiDirection"]
include = ["SignalErrorCode", "FfiDirection", "FfiCiphertextMessageType"]
prefix = "Signal"
renaming_overrides_prefixing = true

Expand All @@ -21,6 +21,7 @@ renaming_overrides_prefixing = true
"FfiSignedPreKeyStoreStruct" = "SignalSignedPreKeyStore"
"FfiSenderKeyStoreStruct" = "SignalSenderKeyStore"
"FfiDirection" = "SignalDirection"
"FfiCiphertextMessageType" = "SignalCiphertextMessageType"

# Avoid double-prefixing these
"SignalFfiError" = "SignalFfiError"
Expand Down
37 changes: 31 additions & 6 deletions rust/bridge/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use libc::{c_char, c_int, c_uchar, c_uint, c_ulonglong, size_t};
use libsignal_protocol_rust::*;
use num_derive::ToPrimitive;
use static_assertions::const_assert_eq;
use std::convert::TryFrom;
use std::ffi::{c_void, CString};

Expand Down Expand Up @@ -63,7 +63,7 @@ pub unsafe extern "C" fn signal_error_get_type(err: *const SignalFfiError) -> u3
match err.as_ref() {
Some(err) => {
let code: SignalErrorCode = err.into();
num_traits::ToPrimitive::to_u32(&code).expect("Error enum can be converted to u32")
code as u32
}
None => 0,
}
Expand Down Expand Up @@ -806,7 +806,7 @@ type IsTrustedIdentity = extern "C" fn(
ctx: *mut c_void,
) -> c_int;

#[derive(Debug, ToPrimitive)]
#[derive(Debug)]
#[repr(C)]
pub enum FfiDirection {
Sending = 0,
Expand Down Expand Up @@ -909,12 +909,11 @@ impl IdentityKeyStore for FfiIdentityKeyStore {
Direction::Sending => FfiDirection::Sending,
Direction::Receiving => FfiDirection::Receiving,
};
let primitive_direction = num_traits::ToPrimitive::to_u32(&direction).unwrap();
let result = (self.store.is_trusted_identity)(
self.store.ctx,
&*address,
&*identity.public_key(),
primitive_direction,
direction as u32,
ctx,
);

Expand Down Expand Up @@ -1284,14 +1283,40 @@ pub unsafe extern "C" fn signal_encrypt_message(

ffi_fn_destroy!(signal_ciphertext_message_destroy destroys CiphertextMessage);

#[derive(Debug)]
#[repr(C)]
pub enum FfiCiphertextMessageType {
Whisper = 2,
PreKey = 3,
SenderKey = 4,
SenderKeyDistribution = 5,
}

const_assert_eq!(
FfiCiphertextMessageType::Whisper as u8,
CiphertextMessageType::Whisper as u8
);
const_assert_eq!(
FfiCiphertextMessageType::PreKey as u8,
CiphertextMessageType::PreKey as u8
);
const_assert_eq!(
FfiCiphertextMessageType::SenderKey as u8,
CiphertextMessageType::SenderKey as u8
);
const_assert_eq!(
FfiCiphertextMessageType::SenderKeyDistribution as u8,
CiphertextMessageType::SenderKeyDistribution as u8
);

#[no_mangle]
pub unsafe extern "C" fn signal_ciphertext_message_type(
typ: *mut u8,
msg: *const CiphertextMessage,
) -> *mut SignalFfiError {
run_ffi_safe(|| {
let msg = native_handle_cast::<CiphertextMessage>(msg)?;
*typ = msg.message_type().encoding();
*typ = msg.message_type() as u8;
Ok(())
})
}
Expand Down
3 changes: 1 addition & 2 deletions rust/bridge/ffi/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

use libc::{c_char, c_uchar, c_uint, c_ulonglong, size_t};
use libsignal_protocol_rust::*;
use num_derive::ToPrimitive;
use std::ffi::{CStr, CString};
use std::fmt;

Expand All @@ -22,7 +21,7 @@ pub enum SignalFfiError {
InvalidType,
}

#[derive(Debug, ToPrimitive)]
#[derive(Debug)]
#[repr(C)]
pub enum SignalErrorCode {
UnknownError = 1,
Expand Down
19 changes: 4 additions & 15 deletions rust/protocol/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,10 @@ pub enum CiphertextMessage {

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum CiphertextMessageType {
Whisper,
PreKey,
SenderKey,
SenderKeyDistribution,
}

impl CiphertextMessageType {
pub fn encoding(&self) -> u8 {
match self {
CiphertextMessageType::Whisper => 2,
CiphertextMessageType::PreKey => 3,
CiphertextMessageType::SenderKey => 4,
CiphertextMessageType::SenderKeyDistribution => 5,
}
}
Whisper = 2,
PreKey = 3,
SenderKey = 4,
SenderKeyDistribution = 5,
}

impl CiphertextMessage {
Expand Down
30 changes: 27 additions & 3 deletions swift/Sources/SignalClient/messages/CiphertextMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@ import SignalFfi
public class CiphertextMessage {
private var handle: OpaquePointer?

public struct MessageType: RawRepresentable, Hashable {
public var rawValue: UInt8
public init(rawValue: UInt8) {
self.rawValue = rawValue
}

internal init(_ knownType: SignalCiphertextMessageType) {
self.init(rawValue: UInt8(knownType.rawValue))
}

public static var whisper: Self {
return Self(SignalCiphertextMessageType_Whisper)
}
public static var preKey: Self {
return Self(SignalCiphertextMessageType_PreKey)
}
public static var senderKey: Self {
return Self(SignalCiphertextMessageType_SenderKey)
}
public static var senderKeyDistribution: Self {
return Self(SignalCiphertextMessageType_SenderKeyDistribution)
}
}

deinit {
signal_ciphertext_message_destroy(handle)
}
Expand All @@ -17,9 +41,9 @@ public class CiphertextMessage {
}
}

public func messageType() throws -> UInt8 {
return try invokeFnReturningInteger {
public func messageType() throws -> MessageType {
return MessageType(rawValue: try invokeFnReturningInteger {
signal_ciphertext_message_type($0, handle)
}
})
}
}
4 changes: 2 additions & 2 deletions swift/Tests/SignalClientTests/PublicAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class PublicAPITests: XCTestCase {
identityStore: alice_store,
context: nil)

XCTAssertEqual(try! ctext_a.messageType(), 3); // prekey
XCTAssertEqual(try! ctext_a.messageType(), .preKey)

let ctext_b = try! PreKeySignalMessage(bytes: try! ctext_a.serialize())

Expand All @@ -302,7 +302,7 @@ class PublicAPITests: XCTestCase {
identityStore: bob_store,
context: nil)

XCTAssertEqual(try! ctext2_b.messageType(), 2); // normal message
XCTAssertEqual(try! ctext2_b.messageType(), .whisper)

let ctext2_a = try! SignalMessage(bytes: try! ctext2_b.serialize())

Expand Down