mirror of
https://github.com/chatmail/core.git
synced 2026-04-29 11:26:29 +03:00
Pass buffers to pgp.rs as slices
This commit is contained in:
committed by
holger krekel
parent
814281ed7d
commit
28cae607a4
@@ -211,12 +211,8 @@ pub fn dc_render_setup_file(context: &Context, passphrase: &str) -> Result<Strin
|
||||
let encr = {
|
||||
let private_key_asc_c = CString::yolo(private_key_asc);
|
||||
let passphrase_c = CString::yolo(passphrase);
|
||||
dc_pgp_symm_encrypt(
|
||||
passphrase_c.as_ptr(),
|
||||
private_key_asc_c.as_ptr() as *const libc::c_void,
|
||||
private_key_asc_c.as_bytes().len(),
|
||||
)
|
||||
.ok_or(format_err!("Failed to encrypt private key."))?
|
||||
dc_pgp_symm_encrypt(passphrase_c.as_ptr(), private_key_asc_c.as_bytes())
|
||||
.ok_or(format_err!("Failed to encrypt private key."))?
|
||||
};
|
||||
let replacement = format!(
|
||||
concat!(
|
||||
@@ -449,9 +445,10 @@ pub unsafe fn dc_decrypt_setup_file(
|
||||
|| binary_bytes == 0)
|
||||
{
|
||||
/* decrypt symmetrically */
|
||||
if let Some(plain) =
|
||||
dc_pgp_symm_decrypt(passphrase, binary as *const libc::c_void, binary_bytes)
|
||||
{
|
||||
if let Some(plain) = dc_pgp_symm_decrypt(
|
||||
passphrase,
|
||||
std::slice::from_raw_parts(binary as *const u8, binary_bytes),
|
||||
) {
|
||||
let payload_c = CString::new(plain).unwrap();
|
||||
payload = strdup(payload_c.as_ptr());
|
||||
}
|
||||
|
||||
@@ -299,8 +299,10 @@ impl E2eeHelper {
|
||||
ok_to_continue = false;
|
||||
} else {
|
||||
if let Some(ctext_v) = dc_pgp_pk_encrypt(
|
||||
(*plain).str_0 as *const libc::c_void,
|
||||
(*plain).len,
|
||||
std::slice::from_raw_parts(
|
||||
(*plain).str_0 as *const u8,
|
||||
(*plain).len,
|
||||
),
|
||||
&keyring,
|
||||
sign_key.as_ref(),
|
||||
) {
|
||||
@@ -907,8 +909,7 @@ unsafe fn decrypt_part(
|
||||
|
||||
/*if we already have fingerprints, do not add more; this ensures, only the fingerprints from the outer-most part are collected */
|
||||
if let Some(plain) = dc_pgp_pk_decrypt(
|
||||
decoded_data as *const libc::c_void,
|
||||
decoded_data_bytes,
|
||||
std::slice::from_raw_parts(decoded_data as *const u8, decoded_data_bytes),
|
||||
&private_keyring,
|
||||
&public_keyring_for_validate,
|
||||
add_signatures,
|
||||
|
||||
35
src/pgp.rs
35
src/pgp.rs
@@ -188,15 +188,11 @@ pub fn dc_pgp_create_keypair(addr: impl AsRef<str>) -> Option<(Key, Key)> {
|
||||
}
|
||||
|
||||
pub fn dc_pgp_pk_encrypt(
|
||||
plain_text: *const libc::c_void,
|
||||
plain_bytes: size_t,
|
||||
plain: &[u8],
|
||||
public_keys_for_encryption: &Keyring,
|
||||
private_key_for_signing: Option<&Key>,
|
||||
) -> Option<String> {
|
||||
assert!(!plain_text.is_null() && !plain_bytes > 0, "invalid input");
|
||||
|
||||
let bytes = unsafe { std::slice::from_raw_parts(plain_text as *const u8, plain_bytes) };
|
||||
let lit_msg = Message::new_literal_bytes("", bytes);
|
||||
let lit_msg = Message::new_literal_bytes("", plain);
|
||||
let pkeys: Vec<&SignedPublicKey> = public_keys_for_encryption
|
||||
.keys()
|
||||
.into_iter()
|
||||
@@ -227,16 +223,11 @@ pub fn dc_pgp_pk_encrypt(
|
||||
}
|
||||
|
||||
pub fn dc_pgp_pk_decrypt(
|
||||
ctext: *const libc::c_void,
|
||||
ctext_bytes: size_t,
|
||||
ctext: &[u8],
|
||||
private_keys_for_decryption: &Keyring,
|
||||
public_keys_for_validation: &Keyring,
|
||||
ret_signature_fingerprints: Option<&mut HashSet<String>>,
|
||||
) -> Option<Vec<u8>> {
|
||||
assert!(!ctext.is_null() && ctext_bytes > 0, "invalid input");
|
||||
|
||||
let ctext = unsafe { std::slice::from_raw_parts(ctext as *const u8, ctext_bytes) };
|
||||
|
||||
// TODO: proper error handling
|
||||
if let Ok((msg, _)) = Message::from_armor_single(Cursor::new(ctext)) {
|
||||
let skeys: Vec<&SignedSecretKey> = private_keys_for_decryption
|
||||
@@ -283,19 +274,13 @@ pub fn dc_pgp_pk_decrypt(
|
||||
}
|
||||
|
||||
/// Symmetric encryption.
|
||||
pub fn dc_pgp_symm_encrypt(
|
||||
passphrase: *const libc::c_char,
|
||||
plain: *const libc::c_void,
|
||||
plain_bytes: size_t,
|
||||
) -> Option<String> {
|
||||
pub fn dc_pgp_symm_encrypt(passphrase: *const libc::c_char, plain: &[u8]) -> Option<String> {
|
||||
assert!(!passphrase.is_null(), "invalid passphrase");
|
||||
assert!(!plain.is_null() && !plain_bytes > 0, "invalid input");
|
||||
|
||||
let pw = unsafe { CStr::from_ptr(passphrase).to_str().unwrap() };
|
||||
let bytes = unsafe { std::slice::from_raw_parts(plain as *const u8, plain_bytes) };
|
||||
|
||||
let mut rng = thread_rng();
|
||||
let lit_msg = Message::new_literal_bytes("", bytes);
|
||||
let lit_msg = Message::new_literal_bytes("", plain);
|
||||
|
||||
let s2k = StringToKey::new_default(&mut rng);
|
||||
let msg = lit_msg.encrypt_with_password(&mut rng, s2k, Default::default(), || pw.into());
|
||||
@@ -304,18 +289,12 @@ pub fn dc_pgp_symm_encrypt(
|
||||
}
|
||||
|
||||
/// Symmetric decryption.
|
||||
pub fn dc_pgp_symm_decrypt(
|
||||
passphrase: *const libc::c_char,
|
||||
ctext: *const libc::c_void,
|
||||
ctext_bytes: size_t,
|
||||
) -> Option<Vec<u8>> {
|
||||
pub fn dc_pgp_symm_decrypt(passphrase: *const libc::c_char, ctext: &[u8]) -> Option<Vec<u8>> {
|
||||
assert!(!passphrase.is_null(), "invalid passphrase");
|
||||
assert!(!ctext.is_null() && !ctext_bytes > 0, "invalid input");
|
||||
|
||||
let pw = unsafe { CStr::from_ptr(passphrase).to_str().unwrap() };
|
||||
let bytes = unsafe { std::slice::from_raw_parts(ctext as *const u8, ctext_bytes) };
|
||||
|
||||
let enc_msg = Message::from_bytes(Cursor::new(bytes));
|
||||
let enc_msg = Message::from_bytes(Cursor::new(ctext));
|
||||
|
||||
enc_msg
|
||||
.and_then(|msg| {
|
||||
|
||||
Reference in New Issue
Block a user