mirror of
https://github.com/chatmail/core.git
synced 2026-05-09 01:46:30 +03:00
Return Option<String> from dc_decrypt_setup_file
This commit is contained in:
@@ -272,16 +272,18 @@ pub unsafe fn dc_continue_key_transfer(context: &Context, msg_id: u32, setup_cod
|
|||||||
if let Some(filename) = msg.get_file(context) {
|
if let Some(filename) = msg.get_file(context) {
|
||||||
if let Ok(buf) = dc_read_file(context, filename) {
|
if let Ok(buf) = dc_read_file(context, filename) {
|
||||||
let norm_sc = CString::yolo(dc_normalize_setup_code(setup_code));
|
let norm_sc = CString::yolo(dc_normalize_setup_code(setup_code));
|
||||||
let armored_key = dc_decrypt_setup_file(context, norm_sc.as_ptr(), buf.as_ptr().cast());
|
if let Some(armored_key) =
|
||||||
if armored_key.is_null() {
|
dc_decrypt_setup_file(context, norm_sc.as_ptr(), buf.as_ptr().cast())
|
||||||
warn!(context, "Cannot decrypt Autocrypt Setup Message.",);
|
{
|
||||||
false
|
if set_self_key(context, &armored_key, 1) {
|
||||||
} else if set_self_key(context, armored_key, 1) {
|
/*set default*/
|
||||||
/*set default*/
|
/* error already logged */
|
||||||
/* error already logged */
|
true
|
||||||
free(armored_key as *mut libc::c_void);
|
} else {
|
||||||
true
|
false
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
warn!(context, "Cannot decrypt Autocrypt Setup Message.",);
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -294,14 +296,7 @@ pub unsafe fn dc_continue_key_transfer(context: &Context, msg_id: u32, setup_cod
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_self_key(
|
fn set_self_key(context: &Context, armored: &str, set_default: libc::c_int) -> bool {
|
||||||
context: &Context,
|
|
||||||
armored_c: *const libc::c_char,
|
|
||||||
set_default: libc::c_int,
|
|
||||||
) -> bool {
|
|
||||||
assert!(!armored_c.is_null(), "invalid buffer");
|
|
||||||
let armored = as_str(armored_c);
|
|
||||||
|
|
||||||
let keys = Key::from_armored_string(armored, KeyType::Private)
|
let keys = Key::from_armored_string(armored, KeyType::Private)
|
||||||
.and_then(|(k, h)| if k.verify() { Some((k, h)) } else { None })
|
.and_then(|(k, h)| if k.verify() { Some((k, h)) } else { None })
|
||||||
.and_then(|(k, h)| k.split_key().map(|pub_key| (k, pub_key, h)));
|
.and_then(|(k, h)| k.split_key().map(|pub_key| (k, pub_key, h)));
|
||||||
@@ -377,7 +372,7 @@ pub unsafe fn dc_decrypt_setup_file(
|
|||||||
context: &Context,
|
context: &Context,
|
||||||
passphrase: *const libc::c_char,
|
passphrase: *const libc::c_char,
|
||||||
filecontent: *const libc::c_char,
|
filecontent: *const libc::c_char,
|
||||||
) -> *mut libc::c_char {
|
) -> Option<String> {
|
||||||
let fc_buf: *mut libc::c_char;
|
let fc_buf: *mut libc::c_char;
|
||||||
let mut fc_headerline: *const libc::c_char = ptr::null();
|
let mut fc_headerline: *const libc::c_char = ptr::null();
|
||||||
let mut fc_base64: *const libc::c_char = ptr::null();
|
let mut fc_base64: *const libc::c_char = ptr::null();
|
||||||
@@ -385,7 +380,7 @@ pub unsafe fn dc_decrypt_setup_file(
|
|||||||
let mut binary_bytes: libc::size_t = 0;
|
let mut binary_bytes: libc::size_t = 0;
|
||||||
let mut indx: libc::size_t = 0;
|
let mut indx: libc::size_t = 0;
|
||||||
|
|
||||||
let mut payload: *mut libc::c_char = ptr::null_mut();
|
let mut payload: Option<String> = None;
|
||||||
fc_buf = dc_strdup(filecontent);
|
fc_buf = dc_strdup(filecontent);
|
||||||
if dc_split_armored_data(
|
if dc_split_armored_data(
|
||||||
fc_buf,
|
fc_buf,
|
||||||
@@ -417,10 +412,7 @@ pub unsafe fn dc_decrypt_setup_file(
|
|||||||
as_str(passphrase),
|
as_str(passphrase),
|
||||||
std::slice::from_raw_parts(binary as *const u8, binary_bytes),
|
std::slice::from_raw_parts(binary as *const u8, binary_bytes),
|
||||||
) {
|
) {
|
||||||
Ok(plain) => {
|
Ok(plain) => payload = Some(String::from_utf8(plain).unwrap()),
|
||||||
let payload_c = CString::new(plain).unwrap();
|
|
||||||
payload = strdup(payload_c.as_ptr());
|
|
||||||
}
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!(context, "Failed to decrypt message: {:?}", err);
|
error!(context, "Failed to decrypt message: {:?}", err);
|
||||||
}
|
}
|
||||||
@@ -886,7 +878,7 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
|
|||||||
);
|
);
|
||||||
set_default = 0i32
|
set_default = 0i32
|
||||||
}
|
}
|
||||||
if !set_self_key(context, private_key, set_default) {
|
if !set_self_key(context, as_str(private_key), set_default) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
imported_cnt += 1
|
imported_cnt += 1
|
||||||
|
|||||||
@@ -251,8 +251,9 @@ unsafe fn stress_functions(context: &Context) {
|
|||||||
|
|
||||||
assert!(preferencrypt_0.is_null());
|
assert!(preferencrypt_0.is_null());
|
||||||
free(buf_1 as *mut libc::c_void);
|
free(buf_1 as *mut libc::c_void);
|
||||||
buf_1 = dc_decrypt_setup_file(context, S_EM_SETUPCODE, S_EM_SETUPFILE);
|
|
||||||
assert!(!buf_1.is_null());
|
let decrypted = dc_decrypt_setup_file(context, S_EM_SETUPCODE, S_EM_SETUPFILE).unwrap();
|
||||||
|
buf_1 = decrypted.strdup();
|
||||||
assert!(dc_split_armored_data(
|
assert!(dc_split_armored_data(
|
||||||
buf_1,
|
buf_1,
|
||||||
&mut headerline_0,
|
&mut headerline_0,
|
||||||
|
|||||||
Reference in New Issue
Block a user