diff --git a/build.rs b/build.rs index b8192108d..a7e6abdda 100644 --- a/build.rs +++ b/build.rs @@ -15,9 +15,7 @@ fn main() { println!("cargo:rustc-link-lib=dylib=sasl2"); println!("cargo:rustc-link-lib=dylib=z"); - println!("cargo:rustc-link-lib=dylib=pthread"); - println!("cargo:rustc-link-lib=dylib=crypto"); println!("cargo:rustc-link-lib=dylib=tools"); if std::env::var("TARGET").unwrap().contains("-apple") { diff --git a/src/dc_context.rs b/src/dc_context.rs index a3479758f..ecbd94b0f 100644 --- a/src/dc_context.rs +++ b/src/dc_context.rs @@ -13,7 +13,7 @@ use crate::dc_loginparam::*; use crate::dc_lot::dc_lot_t; use crate::dc_move::*; use crate::dc_msg::*; -use crate::dc_openssl::*; +// use crate::dc_openssl::*; use crate::dc_pgp::*; use crate::dc_receive_imf::*; use crate::dc_smtp::*; @@ -119,7 +119,7 @@ pub unsafe extern "C" fn dc_context_new( (*context).cb = if cb.is_some() { cb } else { Some(cb_dummy) }; (*context).os_name = dc_strdup_keep_null(os_name); (*context).shall_stop_ongoing = 1i32; - dc_openssl_init(); + // dc_openssl_init(); dc_pgp_init(); (*context).sql = dc_sqlite3_new(context); (*context).inbox = dc_imap_new( @@ -288,7 +288,7 @@ pub unsafe extern "C" fn dc_context_unref(mut context: *mut dc_context_t) { dc_imap_unref((*context).mvbox_thread.imap); dc_smtp_unref((*context).smtp); dc_sqlite3_unref((*context).sql); - dc_openssl_exit(); + pthread_mutex_destroy(&mut (*context).smear_critical); pthread_mutex_destroy(&mut (*context).bobs_qr_critical); pthread_mutex_destroy(&mut (*context).inboxidle_condmutex); diff --git a/src/dc_imex.rs b/src/dc_imex.rs index c746a8be2..7342d18a5 100644 --- a/src/dc_imex.rs +++ b/src/dc_imex.rs @@ -1,5 +1,6 @@ use c2rust_bitfields::BitfieldStruct; use libc; +use rand::{thread_rng, Rng}; use crate::dc_chat::*; use crate::dc_configure::*; @@ -360,23 +361,10 @@ pub unsafe extern "C" fn dc_create_setup_code(mut context: *mut dc_context_t) -> }; dc_strbuilder_init(&mut ret, 0i32); i = 0i32; + let mut rng = thread_rng(); while i < 9i32 { loop { - if 0 == RAND_bytes( - &mut random_val as *mut uint16_t as *mut libc::c_uchar, - ::std::mem::size_of::() as libc::c_ulong as libc::c_int, - ) { - dc_log_warning( - context, - 0i32, - b"Falling back to pseudo-number generation for the setup code.\x00" as *const u8 - as *const libc::c_char, - ); - RAND_pseudo_bytes( - &mut random_val as *mut uint16_t as *mut libc::c_uchar, - ::std::mem::size_of::() as libc::c_ulong as libc::c_int, - ); - } + random_val = rng.gen(); if !(random_val as libc::c_int > 60000i32) { break; } diff --git a/src/dc_openssl.rs b/src/dc_openssl.rs deleted file mode 100644 index eeb3a52c6..000000000 --- a/src/dc_openssl.rs +++ /dev/null @@ -1,172 +0,0 @@ -use libc; - -use crate::types::*; -use crate::x::*; - -static mut s_init_lock: pthread_mutex_t = _opaque_pthread_mutex_t { - __sig: 0x32aaaba7i32 as libc::c_long, - __opaque: [ - 0i32 as libc::c_char, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - ], -}; -static mut s_init_not_required: libc::c_int = 0i32; -static mut s_init_counter: libc::c_int = 0i32; -static mut s_mutex_buf: *mut pthread_mutex_t = 0 as *const pthread_mutex_t as *mut pthread_mutex_t; -unsafe extern "C" fn id_function() -> libc::c_ulong { - return pthread_self() as libc::c_ulong; -} -unsafe extern "C" fn locking_function( - mut mode: libc::c_int, - mut n: libc::c_int, - mut file: *const libc::c_char, - mut line: libc::c_int, -) { - if 0 != mode & 1i32 { - pthread_mutex_lock(&mut *s_mutex_buf.offset(n as isize)); - } else { - pthread_mutex_unlock(&mut *s_mutex_buf.offset(n as isize)); - }; -} -unsafe extern "C" fn dyn_create_function( - mut file: *const libc::c_char, - mut line: libc::c_int, -) -> *mut CRYPTO_dynlock_value { - let mut value: *mut CRYPTO_dynlock_value = - malloc(::std::mem::size_of::() as libc::c_ulong) - as *mut CRYPTO_dynlock_value; - if value.is_null() { - return 0 as *mut CRYPTO_dynlock_value; - } - pthread_mutex_init(&mut (*value).mutex, 0 as *const pthread_mutexattr_t); - return value; -} -unsafe extern "C" fn dyn_lock_function( - mut mode: libc::c_int, - mut l: *mut CRYPTO_dynlock_value, - mut file: *const libc::c_char, - mut line: libc::c_int, -) { - if 0 != mode & 1i32 { - pthread_mutex_lock(&mut (*l).mutex); - } else { - pthread_mutex_unlock(&mut (*l).mutex); - }; -} -unsafe extern "C" fn dyn_destroy_function( - mut l: *mut CRYPTO_dynlock_value, - mut file: *const libc::c_char, - mut line: libc::c_int, -) { - pthread_mutex_destroy(&mut (*l).mutex); - free(l as *mut libc::c_void); -} -#[no_mangle] -pub unsafe extern "C" fn dc_openssl_init() { - pthread_mutex_lock(&mut s_init_lock); - s_init_counter += 1; - if s_init_counter == 1i32 { - if 0 == s_init_not_required { - s_mutex_buf = malloc( - (CRYPTO_num_locks() as libc::c_ulong) - .wrapping_mul(::std::mem::size_of::() as libc::c_ulong), - ) as *mut pthread_mutex_t; - if s_mutex_buf.is_null() { - exit(53i32); - } - let mut i: libc::c_int = 0i32; - while i < CRYPTO_num_locks() { - pthread_mutex_init( - &mut *s_mutex_buf.offset(i as isize), - 0 as *const pthread_mutexattr_t, - ); - i += 1 - } - CRYPTO_set_id_callback(Some(id_function)); - CRYPTO_set_locking_callback(Some(locking_function)); - CRYPTO_set_dynlock_create_callback(Some(dyn_create_function)); - CRYPTO_set_dynlock_lock_callback(Some(dyn_lock_function)); - CRYPTO_set_dynlock_destroy_callback(Some(dyn_destroy_function)); - OPENSSL_init(); - OPENSSL_add_all_algorithms_noconf(); - } - mailstream_openssl_init_not_required(); - } - pthread_mutex_unlock(&mut s_init_lock); -} -#[no_mangle] -pub unsafe extern "C" fn dc_openssl_exit() { - pthread_mutex_lock(&mut s_init_lock); - if s_init_counter > 0i32 { - s_init_counter -= 1; - if s_init_counter == 0i32 && 0 == s_init_not_required { - CRYPTO_set_id_callback(None); - CRYPTO_set_locking_callback(None); - CRYPTO_set_dynlock_create_callback(None); - CRYPTO_set_dynlock_lock_callback(None); - CRYPTO_set_dynlock_destroy_callback(None); - let mut i: libc::c_int = 0i32; - while i < CRYPTO_num_locks() { - pthread_mutex_destroy(&mut *s_mutex_buf.offset(i as isize)); - i += 1 - } - free(s_mutex_buf as *mut libc::c_void); - s_mutex_buf = 0 as *mut pthread_mutex_t - } - } - pthread_mutex_unlock(&mut s_init_lock); -} diff --git a/src/dc_tools.rs b/src/dc_tools.rs index a7bb27a0a..0fb52fb69 100644 --- a/src/dc_tools.rs +++ b/src/dc_tools.rs @@ -1,5 +1,6 @@ use c2rust_bitfields::BitfieldStruct; use libc; +use rand::{thread_rng, Rng}; use crate::dc_array::*; use crate::dc_context::dc_context_t; @@ -989,18 +990,9 @@ pub unsafe extern "C" fn dc_create_id() -> *mut libc::c_char { - for OUTGOING messages this ID is written to the header as `Chat-Group-ID:` and is added to the message ID as Gr..@ - for INCOMING messages, the ID is taken from the Chat-Group-ID-header or from the Message-ID in the In-Reply-To: or References:-Header - the group-id should be a string with the characters [a-zA-Z0-9\-_] */ - let mut buf: [uint32_t; 3] = [0; 3]; - if 0 == RAND_bytes( - &mut buf as *mut [uint32_t; 3] as *mut libc::c_uchar, - (::std::mem::size_of::() as libc::c_ulong).wrapping_mul(3i32 as libc::c_ulong) - as libc::c_int, - ) { - RAND_pseudo_bytes( - &mut buf as *mut [uint32_t; 3] as *mut libc::c_uchar, - (::std::mem::size_of::() as libc::c_ulong).wrapping_mul(3i32 as libc::c_ulong) - as libc::c_int, - ); - } + + let mut rng = thread_rng(); + let mut buf: [uint32_t; 3] = [rng.gen(), rng.gen(), rng.gen()]; return encode_66bits_as_base64(buf[0usize], buf[1usize], buf[2usize]); } /* ****************************************************************************** diff --git a/src/lib.rs b/src/lib.rs index 8aa48347e..b660f21c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,7 +54,6 @@ pub mod dc_mimeparser; pub mod dc_move; pub mod dc_msg; pub mod dc_oauth2; -pub mod dc_openssl; pub mod dc_param; pub mod dc_pgp; pub mod dc_qr; diff --git a/src/x.rs b/src/x.rs index 501224b5a..74e5a9a2f 100644 --- a/src/x.rs +++ b/src/x.rs @@ -66,8 +66,6 @@ extern "C" { pub fn opendir(_: *const libc::c_char) -> *mut DIR; pub fn readdir(_: *mut DIR) -> *mut dirent; pub fn sleep(_: libc::c_uint) -> libc::c_uint; - pub fn RAND_bytes(buf: *mut libc::c_uchar, num: libc::c_int) -> libc::c_int; - pub fn RAND_pseudo_bytes(buf: *mut libc::c_uchar, num: libc::c_int) -> libc::c_int; pub fn mmap_string_unref(str: *mut libc::c_char) -> libc::c_int; pub fn strncmp(_: *const libc::c_char, _: *const libc::c_char, _: libc::c_ulong) -> libc::c_int; @@ -603,52 +601,6 @@ extern "C" { progr_fun: Option ()>, context: *mut libc::c_void, ); - pub fn mailstream_openssl_init_not_required(); - - // -- OpenSSL - - /* return CRYPTO_NUM_LOCKS (shared libs!) */ - pub fn CRYPTO_num_locks() -> libc::c_int; - pub fn CRYPTO_set_locking_callback( - func: Option< - unsafe extern "C" fn( - _: libc::c_int, - _: libc::c_int, - _: *const libc::c_char, - _: libc::c_int, - ) -> (), - >, - ); - pub fn CRYPTO_set_id_callback(func: Option libc::c_ulong>); - pub fn CRYPTO_set_dynlock_create_callback( - dyn_create_function_0: Option< - unsafe extern "C" fn( - _: *const libc::c_char, - _: libc::c_int, - ) -> *mut CRYPTO_dynlock_value, - >, - ); - pub fn CRYPTO_set_dynlock_lock_callback( - dyn_lock_function_0: Option< - unsafe extern "C" fn( - _: libc::c_int, - _: *mut CRYPTO_dynlock_value, - _: *const libc::c_char, - _: libc::c_int, - ) -> (), - >, - ); - pub fn CRYPTO_set_dynlock_destroy_callback( - dyn_destroy_function_0: Option< - unsafe extern "C" fn( - _: *mut CRYPTO_dynlock_value, - _: *const libc::c_char, - _: libc::c_int, - ) -> (), - >, - ); - pub fn OPENSSL_init(); - pub fn OPENSSL_add_all_algorithms_noconf(); // -- DC Methods