mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 17:36:29 +03:00
another rustification of encrypt()
This commit is contained in:
73
src/e2ee.rs
73
src/e2ee.rs
@@ -61,11 +61,7 @@ impl E2eeHelper {
|
|||||||
do_gossip: bool,
|
do_gossip: bool,
|
||||||
mut in_out_message: *mut mailmime,
|
mut in_out_message: *mut mailmime,
|
||||||
) -> Result<bool> {
|
) -> Result<bool> {
|
||||||
let mut col: libc::c_int = 0i32;
|
/* libEtPan's pgp_encrypt_mime() takes the parent as the new root. We just expect the root as being given to this function. */
|
||||||
let mut do_encrypt = false;
|
|
||||||
let mut keyring = Keyring::default();
|
|
||||||
let mut peerstates: Vec<Peerstate> = Vec::new();
|
|
||||||
|
|
||||||
if in_out_message.is_null() || !(*in_out_message).mm_parent.is_null() {
|
if in_out_message.is_null() || !(*in_out_message).mm_parent.is_null() {
|
||||||
bail!("invalid inputs");
|
bail!("invalid inputs");
|
||||||
}
|
}
|
||||||
@@ -81,33 +77,30 @@ impl E2eeHelper {
|
|||||||
Err(err) => {
|
Err(err) => {
|
||||||
bail!("Failed to load own public key: {}", err);
|
bail!("Failed to load own public key: {}", err);
|
||||||
}
|
}
|
||||||
Ok(public_key) => public_key
|
Ok(public_key) => public_key,
|
||||||
};
|
};
|
||||||
|
|
||||||
/* libEtPan's pgp_encrypt_mime() takes the parent as the new root. We just expect the root as being given to this function. */
|
let e2ee = context.sql.get_config_int(&context, "e2ee_enabled");
|
||||||
let prefer_encrypt = if 0
|
|
||||||
!= context
|
let prefer_encrypt = if 0 != e2ee.unwrap_or_default() {
|
||||||
.sql
|
|
||||||
.get_config_int(context, "e2ee_enabled")
|
|
||||||
.unwrap_or_default()
|
|
||||||
{
|
|
||||||
EncryptPreference::Mutual
|
EncryptPreference::Mutual
|
||||||
} else {
|
} else {
|
||||||
EncryptPreference::NoPreference
|
EncryptPreference::NoPreference
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut encryption_successfull = false;
|
||||||
|
let mut do_encrypt = false;
|
||||||
|
let mut keyring = Keyring::default();
|
||||||
|
let mut peerstates: Vec<Peerstate> = Vec::new();
|
||||||
|
|
||||||
let plain: *mut MMAPString = mmap_string_new(b"\x00" as *const u8 as *const libc::c_char);
|
|
||||||
/*only for random-seed*/
|
/*only for random-seed*/
|
||||||
if prefer_encrypt == EncryptPreference::Mutual || e2ee_guaranteed {
|
if prefer_encrypt == EncryptPreference::Mutual || e2ee_guaranteed {
|
||||||
do_encrypt = true;
|
do_encrypt = true;
|
||||||
for recipient_addr in recipients_addr.iter() {
|
for recipient_addr in recipients_addr.iter() {
|
||||||
if *recipient_addr != addr {
|
if *recipient_addr != addr {
|
||||||
let peerstate =
|
let peerstate = Peerstate::from_addr(context, &context.sql, &recipient_addr);
|
||||||
Peerstate::from_addr(context, &context.sql, &recipient_addr);
|
|
||||||
if peerstate.is_some()
|
if peerstate.is_some()
|
||||||
&& (peerstate.as_ref().unwrap().prefer_encrypt
|
&& (peerstate.as_ref().unwrap().prefer_encrypt == EncryptPreference::Mutual
|
||||||
== EncryptPreference::Mutual
|
|
||||||
|| e2ee_guaranteed)
|
|| e2ee_guaranteed)
|
||||||
{
|
{
|
||||||
let peerstate = peerstate.unwrap();
|
let peerstate = peerstate.unwrap();
|
||||||
@@ -146,7 +139,9 @@ impl E2eeHelper {
|
|||||||
}
|
}
|
||||||
/*just a pointer into mailmime structure, must not be freed*/
|
/*just a pointer into mailmime structure, must not be freed*/
|
||||||
let imffields_unprotected = mailmime_find_mailimf_fields(in_out_message);
|
let imffields_unprotected = mailmime_find_mailimf_fields(in_out_message);
|
||||||
if !imffields_unprotected.is_null() {
|
if imffields_unprotected.is_null() {
|
||||||
|
bail!("could not find mime fields");
|
||||||
|
}
|
||||||
/* encrypt message, if possible */
|
/* encrypt message, if possible */
|
||||||
if do_encrypt {
|
if do_encrypt {
|
||||||
mailprivacy_prepare_mime(in_out_message);
|
mailprivacy_prepare_mime(in_out_message);
|
||||||
@@ -170,11 +165,13 @@ impl E2eeHelper {
|
|||||||
);
|
);
|
||||||
if do_gossip {
|
if do_gossip {
|
||||||
for peerstate in peerstates {
|
for peerstate in peerstates {
|
||||||
peerstate.render_gossip_header(min_verified as usize).map(|header| {
|
peerstate
|
||||||
|
.render_gossip_header(min_verified as usize)
|
||||||
|
.map(|header| {
|
||||||
wrapmime::new_custom_field(
|
wrapmime::new_custom_field(
|
||||||
imffields_encrypted,
|
imffields_encrypted,
|
||||||
"Autocrypt-Gossip",
|
"Autocrypt-Gossip",
|
||||||
&header
|
&header,
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -189,14 +186,11 @@ impl E2eeHelper {
|
|||||||
if !field.is_null() {
|
if !field.is_null() {
|
||||||
if (*field).fld_type == MAILIMF_FIELD_SUBJECT as libc::c_int {
|
if (*field).fld_type == MAILIMF_FIELD_SUBJECT as libc::c_int {
|
||||||
move_to_encrypted = true;
|
move_to_encrypted = true;
|
||||||
} else if (*field).fld_type
|
} else if (*field).fld_type == MAILIMF_FIELD_OPTIONAL_FIELD as libc::c_int {
|
||||||
== MAILIMF_FIELD_OPTIONAL_FIELD as libc::c_int
|
|
||||||
{
|
|
||||||
let opt_field = (*field).fld_data.fld_optional_field;
|
let opt_field = (*field).fld_data.fld_optional_field;
|
||||||
if !opt_field.is_null() && !(*opt_field).fld_name.is_null() {
|
if !opt_field.is_null() && !(*opt_field).fld_name.is_null() {
|
||||||
let fld_name = to_string_lossy((*opt_field).fld_name);
|
let fld_name = to_string_lossy((*opt_field).fld_name);
|
||||||
if fld_name.starts_with("Secure-Join")
|
if fld_name.starts_with("Secure-Join") || fld_name.starts_with("Chat-")
|
||||||
|| fld_name.starts_with("Chat-")
|
|
||||||
{
|
{
|
||||||
move_to_encrypted = true;
|
move_to_encrypted = true;
|
||||||
}
|
}
|
||||||
@@ -244,15 +238,21 @@ impl E2eeHelper {
|
|||||||
"protected-headers",
|
"protected-headers",
|
||||||
"v1",
|
"v1",
|
||||||
)?;
|
)?;
|
||||||
|
let plain: *mut MMAPString =
|
||||||
|
mmap_string_new(b"\x00" as *const u8 as *const libc::c_char);
|
||||||
|
let mut col: libc::c_int = 0i32;
|
||||||
mailmime_write_mem(plain, &mut col, message_to_encrypt);
|
mailmime_write_mem(plain, &mut col, message_to_encrypt);
|
||||||
if (*plain).str_0.is_null() || (*plain).len <= 0 {
|
if (*plain).str_0.is_null() || (*plain).len <= 0 {
|
||||||
bail!("could not write/allocate");
|
bail!("could not write/allocate");
|
||||||
}
|
}
|
||||||
if let Ok(ctext_v) = dc_pgp_pk_encrypt(
|
let ctext = dc_pgp_pk_encrypt(
|
||||||
std::slice::from_raw_parts((*plain).str_0 as *const u8, (*plain).len),
|
std::slice::from_raw_parts((*plain).str_0 as *const u8, (*plain).len),
|
||||||
&keyring,
|
&keyring,
|
||||||
sign_key.as_ref(),
|
sign_key.as_ref(),
|
||||||
) {
|
);
|
||||||
|
mmap_string_free(plain);
|
||||||
|
|
||||||
|
if let Ok(ctext_v) = ctext {
|
||||||
let ctext_bytes = ctext_v.len();
|
let ctext_bytes = ctext_v.len();
|
||||||
let ctext = ctext_v.strdup();
|
let ctext = ctext_v.strdup();
|
||||||
self.cdata_to_free = Some(Box::new(ctext));
|
self.cdata_to_free = Some(Box::new(ctext));
|
||||||
@@ -265,11 +265,7 @@ impl E2eeHelper {
|
|||||||
-1,
|
-1,
|
||||||
);
|
);
|
||||||
let content: *mut mailmime_content = (*encrypted_part).mm_content_type;
|
let content: *mut mailmime_content = (*encrypted_part).mm_content_type;
|
||||||
wrapmime::append_ct_param(
|
wrapmime::append_ct_param(content, "protocol", "application/pgp-encrypted")?;
|
||||||
content,
|
|
||||||
"protocol",
|
|
||||||
"application/pgp-encrypted",
|
|
||||||
)?;
|
|
||||||
static mut VERSION_CONTENT: [libc::c_char; 13] =
|
static mut VERSION_CONTENT: [libc::c_char; 13] =
|
||||||
[86, 101, 114, 115, 105, 111, 110, 58, 32, 49, 13, 10, 0];
|
[86, 101, 114, 115, 105, 111, 110, 58, 32, 49, 13, 10, 0];
|
||||||
let version_mime: *mut mailmime = new_data_part(
|
let version_mime: *mut mailmime = new_data_part(
|
||||||
@@ -289,19 +285,12 @@ impl E2eeHelper {
|
|||||||
(*in_out_message).mm_data.mm_message.mm_msg_mime = encrypted_part;
|
(*in_out_message).mm_data.mm_message.mm_msg_mime = encrypted_part;
|
||||||
(*encrypted_part).mm_parent = in_out_message;
|
(*encrypted_part).mm_parent = in_out_message;
|
||||||
mailmime_free(message_to_encrypt);
|
mailmime_free(message_to_encrypt);
|
||||||
if !plain.is_null() {
|
encryption_successfull = true;
|
||||||
mmap_string_free(plain);
|
|
||||||
}
|
|
||||||
return Ok(true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let aheader = Aheader::new(addr, public_key, prefer_encrypt).to_string();
|
let aheader = Aheader::new(addr, public_key, prefer_encrypt).to_string();
|
||||||
new_custom_field(imffields_unprotected, "Autocrypt", &aheader);
|
new_custom_field(imffields_unprotected, "Autocrypt", &aheader);
|
||||||
}
|
Ok(encryption_successfull)
|
||||||
if !plain.is_null() {
|
|
||||||
mmap_string_free(plain);
|
|
||||||
}
|
|
||||||
Ok(false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn decrypt(&mut self, context: &Context, in_out_message: *mut mailmime) {
|
pub unsafe fn decrypt(&mut self, context: &Context, in_out_message: *mut mailmime) {
|
||||||
|
|||||||
@@ -56,7 +56,6 @@ pub fn new_custom_field(fields: *mut mailimf_fields, name: &str, value: &str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn build_body_text(text: &str) -> Result<*mut mailmime, Error> {
|
pub fn build_body_text(text: &str) -> Result<*mut mailmime, Error> {
|
||||||
let mime_fields: *mut mailmime_fields;
|
let mime_fields: *mut mailmime_fields;
|
||||||
let message_part: *mut mailmime;
|
let message_part: *mut mailmime;
|
||||||
|
|||||||
Reference in New Issue
Block a user