libsignal-protocol 0.1.0

An idiomatic high-level interface to the libsignal-protocol-c crate.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use std::{
    convert::TryFrom,
    fmt::{self, Debug, Formatter},
    os::raw::{c_char, c_int, c_void},
    panic::RefUnwindSafe,
    pin::Pin,
    ptr,
    rc::Rc,
    sync::Mutex,
    time::SystemTime,
};

use failure::Error;
use lock_api::RawMutex as _;
use log::Level;
use parking_lot::RawMutex;

#[cfg(feature = "crypto-native")]
use crate::crypto::DefaultCrypto;
use crate::{
    crypto::{Crypto, CryptoProvider},
    errors::{FromInternalErrorCode, InternalError},
    hkdf::HMACBasedKeyDerivationFunction,
    keys::{
        IdentityKeyPair, KeyPair, PreKeyList, PrivateKey, SessionSignedPreKey,
    },
    raw_ptr::Raw,
    session_builder::SessionBuilder,
    stores::{
        identity_key_store::{self as iks, IdentityKeyStore},
        pre_key_store::{self as pks, PreKeyStore},
        session_store::{self as sess, SessionStore},
        signed_pre_key_store::{self as spks, SignedPreKeyStore},
    },
    Address, Buffer, StoreContext,
};
// for rustdoc link resolution
#[allow(unused_imports)]
use crate::keys::{PreKey, PublicKey};

/// A helper function for generating a new [`IdentityKeyPair`].
pub fn generate_identity_key_pair(
    ctx: &Context,
) -> Result<IdentityKeyPair, Error> {
    unsafe {
        let mut key_pair = ptr::null_mut();
        sys::signal_protocol_key_helper_generate_identity_key_pair(
            &mut key_pair,
            ctx.raw(),
        )
        .into_result()?;
        Ok(IdentityKeyPair {
            raw: Raw::from_ptr(key_pair),
        })
    }
}

/// Generate a normal elliptic curve key pair.
pub fn generate_key_pair(ctx: &Context) -> Result<KeyPair, Error> {
    unsafe {
        let mut key_pair = ptr::null_mut();
        sys::curve_generate_key_pair(ctx.raw(), &mut key_pair).into_result()?;

        Ok(KeyPair {
            raw: Raw::from_ptr(key_pair),
        })
    }
}

/// Calculate the signature for a message.
///
/// # Examples
///
/// This is the counterpart to [`PublicKey::verify_signature`].
///
/// ```rust
/// # use libsignal_protocol::{keys::PublicKey, Context};
/// # use failure::Error;
/// # use cfg_if::cfg_if;
/// # fn main() -> Result<(), Error> {
/// # cfg_if::cfg_if! {
/// #  if #[cfg(feature = "crypto-native")] {
/// #      type Crypto = libsignal_protocol::crypto::DefaultCrypto;
/// #  } else if #[cfg(feature = "crypto-openssl")] {
/// #      type Crypto = libsignal_protocol::crypto::OpenSSLCrypto;
/// #  } else {
/// #      compile_error!("These tests require one of the crypto features to be enabled");
/// #  }
/// # }
/// // the `Crypto` here is a type alias to one of `OpenSSLCrypto` or `DefaultCrypto`.
/// let ctx = Context::new(Crypto::default()).unwrap();
/// let key_pair = libsignal_protocol::generate_key_pair(&ctx)?;
///
/// let msg = "Hello, World!";
/// let private_key = key_pair.private();
/// let signature = libsignal_protocol::calculate_signature(
///     &ctx,
///     &private_key,
///     msg.as_bytes(),
/// )?;
///
/// let public = key_pair.public();
/// let got = public.verify_signature(msg.as_bytes(), signature.as_slice());
/// assert!(got.is_ok());
/// # Ok(())
/// # }
/// ```
pub fn calculate_signature(
    ctx: &Context,
    private: &PrivateKey,
    message: &[u8],
) -> Result<Buffer, Error> {
    unsafe {
        let mut buffer = ptr::null_mut();
        sys::curve_calculate_signature(
            ctx.raw(),
            &mut buffer,
            private.raw.as_const_ptr(),
            message.as_ptr(),
            message.len(),
        )
        .into_result()?;

        Ok(Buffer::from_raw(buffer))
    }
}

/// Generate a new registration ID.
pub fn generate_registration_id(
    ctx: &Context,
    extended_range: i32,
) -> Result<u32, Error> {
    let mut id = 0;
    unsafe {
        sys::signal_protocol_key_helper_generate_registration_id(
            &mut id,
            extended_range,
            ctx.raw(),
        )
        .into_result()?;
    }

    Ok(id)
}

/// Generate a list of [`PreKey`]s. Clients should do this at install time, and
/// subsequently any time the list of [`PreKey`]s stored on the server runs low.
///
/// Pre key IDs are shorts, so they will eventually be repeated. Clients should
/// store pre keys in a circular buffer, so that they are repeated as
/// infrequently as possible.
pub fn generate_pre_keys(
    ctx: &Context,
    start: u32,
    count: u32,
) -> Result<PreKeyList, Error> {
    unsafe {
        let mut pre_keys_head = ptr::null_mut();
        sys::signal_protocol_key_helper_generate_pre_keys(
            &mut pre_keys_head,
            start,
            count,
            ctx.raw(),
        )
        .into_result()?;

        Ok(PreKeyList::from_raw(pre_keys_head))
    }
}

/// Generate a signed pre-key.
pub fn generate_signed_pre_key(
    ctx: &Context,
    identity_key_pair: &IdentityKeyPair,
    id: u32,
    timestamp: SystemTime,
) -> Result<SessionSignedPreKey, Error> {
    unsafe {
        let mut raw = ptr::null_mut();
        let unix_time = timestamp.duration_since(SystemTime::UNIX_EPOCH)?;

        sys::signal_protocol_key_helper_generate_signed_pre_key(
            &mut raw,
            identity_key_pair.raw.as_const_ptr(),
            id,
            unix_time.as_secs(),
            ctx.raw(),
        )
        .into_result()?;

        if raw.is_null() {
            Err(failure::err_msg("Unable to generate a signed pre key"))
        } else {
            Ok(SessionSignedPreKey {
                raw: Raw::from_ptr(raw),
            })
        }
    }
}

/// Create a container for the state used by the signal protocol.
pub fn store_context<P, K, S, I>(
    ctx: &Context,
    pre_key_store: P,
    signed_pre_key_store: K,
    session_store: S,
    identity_key_store: I,
) -> Result<StoreContext, Error>
where
    P: PreKeyStore + 'static,
    K: SignedPreKeyStore + 'static,
    S: SessionStore + 'static,
    I: IdentityKeyStore + 'static,
{
    unsafe {
        let mut store_ctx = ptr::null_mut();
        sys::signal_protocol_store_context_create(&mut store_ctx, ctx.raw())
            .into_result()?;

        let pre_key_store = pks::new_vtable(pre_key_store);
        sys::signal_protocol_store_context_set_pre_key_store(
            store_ctx,
            &pre_key_store,
        )
        .into_result()?;

        let signed_pre_key_store = spks::new_vtable(signed_pre_key_store);
        sys::signal_protocol_store_context_set_signed_pre_key_store(
            store_ctx,
            &signed_pre_key_store,
        )
        .into_result()?;

        let session_store = sess::new_vtable(session_store);
        sys::signal_protocol_store_context_set_session_store(
            store_ctx,
            &session_store,
        )
        .into_result()?;

        let identity_key_store = iks::new_vtable(identity_key_store);
        sys::signal_protocol_store_context_set_identity_key_store(
            store_ctx,
            &identity_key_store,
        )
        .into_result()?;

        Ok(StoreContext::new(store_ctx, &ctx.0))
    }
}

/// Create a new HMAC-based key derivation function.
pub fn create_hkdf(
    ctx: &Context,
    version: i32,
) -> Result<HMACBasedKeyDerivationFunction, Error> {
    HMACBasedKeyDerivationFunction::new(version, ctx)
}

/// Create a new session builder for communication with the user with the
/// specified address.
pub fn session_builder(
    ctx: &Context,
    store_context: &StoreContext,
    address: &Address,
) -> SessionBuilder {
    SessionBuilder::new(ctx, store_context, address)
}

/// Global state and callbacks used by the library.
///
/// Most functions which require access to the global context (e.g. for crypto
/// functions or locking) will accept a `&Context` as their first argument.
#[derive(Debug, Clone)]
pub struct Context(pub(crate) Rc<ContextInner>);

impl Context {
    /// Create a new [`Context`] using the provided cryptographic functions.
    pub fn new<C: Crypto + 'static>(crypto: C) -> Result<Context, Error> {
        ContextInner::new(crypto)
            .map(|c| Context(Rc::new(c)))
            .map_err(Error::from)
    }

    /// Access the original [`Crypto`] object.
    pub fn crypto(&self) -> &dyn Crypto { self.0.crypto.state() }

    pub(crate) fn raw(&self) -> *mut sys::signal_context { self.0.raw() }

    /// Se the function to use when `libsignal-protocol-c` emits a log message.
    pub fn set_log_func<F>(&self, log_func: F)
    where
        F: Fn(Level, &str) + RefUnwindSafe + 'static,
    {
        let mut lf = self.0.state.log_func.lock().unwrap();
        *lf = Box::new(log_func);
    }
}

#[cfg(feature = "crypto-native")]
impl Default for Context {
    fn default() -> Context {
        match Context::new(DefaultCrypto::default()) {
            Ok(c) => c,
            Err(e) => {
                panic!("Unable to create a context using the defaults: {}", e)
            },
        }
    }
}

/// Our Rust wrapper around the [`sys::signal_context`].
///
/// # Safety
///
/// This **must** outlive any data created by the `libsignal-protocol-c`
/// library. You'll usually do this by adding a `Rc<ContextInner>` to any
/// wrapper types.
#[allow(dead_code)]
pub(crate) struct ContextInner {
    raw: *mut sys::signal_context,
    crypto: CryptoProvider,
    // A pointer to our [`State`] has been passed to `libsignal-protocol-c`, so
    // we need to make sure it is never moved.
    state: Pin<Box<State>>,
}

impl ContextInner {
    pub(crate) fn new<C: Crypto + 'static>(
        crypto: C,
    ) -> Result<ContextInner, InternalError> {
        unsafe {
            let mut global_context: *mut sys::signal_context = ptr::null_mut();
            let crypto = CryptoProvider::new(crypto);
            let mut state = Pin::new(Box::new(State {
                mux: RawMutex::INIT,
                log_func: Mutex::new(Box::new(default_log_func)),
            }));

            let user_data =
                state.as_mut().get_mut() as *mut State as *mut c_void;
            sys::signal_context_create(&mut global_context, user_data)
                .into_result()?;
            sys::signal_context_set_crypto_provider(
                global_context,
                &crypto.vtable,
            )
            .into_result()?;
            sys::signal_context_set_locking_functions(
                global_context,
                Some(lock_function),
                Some(unlock_function),
            )
            .into_result()?;
            sys::signal_context_set_log_function(
                global_context,
                Some(log_trampoline),
            )
            .into_result()?;

            Ok(ContextInner {
                raw: global_context,
                crypto,
                state,
            })
        }
    }

    pub(crate) const fn raw(&self) -> *mut sys::signal_context { self.raw }
}

impl Drop for ContextInner {
    fn drop(&mut self) {
        unsafe {
            sys::signal_context_destroy(self.raw());
        }
    }
}

impl Debug for ContextInner {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_tuple("ContextInner").finish()
    }
}

fn default_log_func(level: Level, message: &str) {
    log::log!(level, "{}", message);

    if level == Level::Error && std::env::var("RUST_BACKTRACE").is_ok() {
        log::error!("{}", failure::Backtrace::new());
    }
}

unsafe extern "C" fn log_trampoline(
    level: c_int,
    msg: *const c_char,
    len: usize,
    user_data: *mut c_void,
) {
    signal_assert!(!msg.is_null(), ());
    signal_assert!(!user_data.is_null(), ());

    let state = &*(user_data as *const State);
    let buffer = std::slice::from_raw_parts(msg as *const u8, len);
    let level = translate_log_level(level);

    if let Ok(message) = std::str::from_utf8(buffer) {
        // we can't log the errors that occur while logging errors, so just
        // drop them on the floor...
        let _ = std::panic::catch_unwind(|| {
            let log_func = state.log_func.lock().unwrap();
            log_func(level, message);
        });
    }
}

fn translate_log_level(raw: c_int) -> Level {
    match u32::try_from(raw) {
        Ok(sys::SG_LOG_ERROR) => Level::Error,
        Ok(sys::SG_LOG_WARNING) => Level::Warn,
        Ok(sys::SG_LOG_INFO) => Level::Info,
        Ok(sys::SG_LOG_DEBUG) => Level::Debug,
        Ok(sys::SG_LOG_NOTICE) => Level::Trace,
        _ => Level::Info,
    }
}

unsafe extern "C" fn lock_function(user_data: *mut c_void) {
    let state = &*(user_data as *const State);
    state.mux.lock();
}

unsafe extern "C" fn unlock_function(user_data: *mut c_void) {
    let state = &*(user_data as *const State);
    state.mux.unlock();
}

/// The "user state" we pass to `libsignal-protocol-c` as part of the global
/// context.
///
/// # Safety
///
/// A pointer to this [`State`] will be shared throughout the
/// `libsignal-protocol-c` library, so any mutation **must** be done using the
/// appropriate synchronisation mechanisms (i.e. `RefCell` or atomics).
struct State {
    mux: RawMutex,
    log_func: Mutex<Box<dyn Fn(Level, &str) + RefUnwindSafe>>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(feature = "crypto-native")]
    #[test]
    fn library_initialization_example_from_readme_native() {
        let ctx = Context::default();

        drop(ctx);
    }

    #[cfg(feature = "crypto-openssl")]
    #[test]
    fn library_initialization_example_from_readme_openssl() {
        use crate::crypto::OpenSSLCrypto;
        let ctx = Context::new(OpenSSLCrypto::default()).unwrap();

        drop(ctx);
    }
}