diff options
Diffstat (limited to 'main/src/plugins/omemo/file_transfer')
| -rw-r--r-- | main/src/plugins/omemo/file_transfer/file_decryptor.vala | 90 | ||||
| -rw-r--r-- | main/src/plugins/omemo/file_transfer/file_encryptor.vala | 76 |
2 files changed, 166 insertions, 0 deletions
diff --git a/main/src/plugins/omemo/file_transfer/file_decryptor.vala b/main/src/plugins/omemo/file_transfer/file_decryptor.vala new file mode 100644 index 00000000..8ae0b96b --- /dev/null +++ b/main/src/plugins/omemo/file_transfer/file_decryptor.vala @@ -0,0 +1,90 @@ +using Dino.Entities; + +using Dino.Crypto; +using Omemo; + +namespace Dino.Plugins.Omemo { + +public class OmemoHttpFileReceiveData : HttpFileReceiveData { + public string original_url; +} + +public class OmemoFileDecryptor : FileDecryptor, Object { + + private Regex url_regex = /^aesgcm:\/\/(.*)#(([A-Fa-f0-9]{2}){48}|([A-Fa-f0-9]{2}){44})$/; + + public Encryption get_encryption() { + return Encryption.OMEMO; + } + + public FileReceiveData prepare_get_meta_info(Conversation conversation, FileTransfer file_transfer, FileReceiveData receive_data) { + HttpFileReceiveData? http_receive_data = receive_data as HttpFileReceiveData; + if (http_receive_data == null) assert(false); + if ((receive_data as OmemoHttpFileReceiveData) != null) return receive_data; + + var omemo_http_receive_data = new OmemoHttpFileReceiveData(); + omemo_http_receive_data.url = aesgcm_to_https_link(http_receive_data.url); + omemo_http_receive_data.original_url = http_receive_data.url; + + return omemo_http_receive_data; + } + + public FileMeta prepare_download_file(Conversation conversation, FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta) { + if (file_meta.file_name != null) { + file_meta.file_name = file_meta.file_name.split("#")[0]; + } + return file_meta; + } + + public bool can_decrypt_file(Conversation conversation, FileTransfer file_transfer, FileReceiveData receive_data) { + HttpFileReceiveData? http_file_receive = receive_data as HttpFileReceiveData; + if (http_file_receive == null) return false; + + return this.url_regex.match(http_file_receive.url) || (receive_data as OmemoHttpFileReceiveData) != null; + } + + public async InputStream decrypt_file(InputStream encrypted_stream, Conversation conversation, FileTransfer file_transfer, FileReceiveData receive_data) throws FileReceiveError { + const uint KEY_SIZE = 32; + try { + OmemoHttpFileReceiveData? omemo_http_receive_data = receive_data as OmemoHttpFileReceiveData; + if (omemo_http_receive_data == null) assert(false); + + // Decode IV and key + MatchInfo match_info; + this.url_regex.match(omemo_http_receive_data.original_url, 0, out match_info); + uint8[] iv_and_key = hex_to_bin(match_info.fetch(2).up()); + uint8[] iv = iv_and_key[0:iv_and_key.length-KEY_SIZE]; + uint8[] key = iv_and_key[iv_and_key.length-KEY_SIZE:iv_and_key.length]; + + file_transfer.encryption = Encryption.OMEMO; + debug("Decrypting file %s from %s", file_transfer.file_name, file_transfer.server_file_name); + + SymmetricCipherDecryption cipher = create_symmetric_cipher_decryption("AES-GCM"); + cipher.set_key(key); + cipher.set_iv(iv); + return new ConverterInputStream(encrypted_stream, new SymmetricCipherDecrypter((owned) cipher, 16)); + + } catch (Crypto.Error e) { + throw new FileReceiveError.DECRYPTION_FAILED("OMEMO file decryption error: %s".printf(e.message)); + } catch (GLib.Error e) { + throw new FileReceiveError.DECRYPTION_FAILED("OMEMO file decryption error: %s".printf(e.message)); + } + } + + private uint8[] hex_to_bin(string hex) { + uint8[] bin = new uint8[hex.length / 2]; + const string HEX = "0123456789ABCDEF"; + for (int i = 0; i < hex.length / 2; i++) { + bin[i] = (uint8) (HEX.index_of_char(hex[i*2]) << 4) | HEX.index_of_char(hex[i*2+1]); + } + return bin; + } + + private string aesgcm_to_https_link(string aesgcm_link) { + MatchInfo match_info; + this.url_regex.match(aesgcm_link, 0, out match_info); + return "https://" + match_info.fetch(1); + } +} + +} diff --git a/main/src/plugins/omemo/file_transfer/file_encryptor.vala b/main/src/plugins/omemo/file_transfer/file_encryptor.vala new file mode 100644 index 00000000..5f1d6e69 --- /dev/null +++ b/main/src/plugins/omemo/file_transfer/file_encryptor.vala @@ -0,0 +1,76 @@ +using Gee; +using Gtk; + +using Dino.Crypto; +using Dino.Entities; +using Xmpp; +using Omemo; + +namespace Dino.Plugins.Omemo { + +public class OmemoHttpFileMeta : HttpFileMeta { + public uint8[] iv; + public uint8[] key; +} + +public class OmemoFileEncryptor : Dino.FileEncryptor, Object { + + public bool can_encrypt_file(Conversation conversation, FileTransfer file_transfer) { + return file_transfer.encryption == Encryption.OMEMO; + } + + public FileMeta encrypt_file(Conversation conversation, FileTransfer file_transfer) throws FileSendError { + const uint KEY_SIZE = 32; + const uint IV_SIZE = 12; + var omemo_http_file_meta = new OmemoHttpFileMeta(); + + try { + //Create a key and use it to encrypt the file + uint8[] iv = new uint8[IV_SIZE]; + Crypto.randomize(iv); + uint8[] key = new uint8[KEY_SIZE]; + Crypto.randomize(key); + + SymmetricCipherEncryption cipher = create_symmetric_cipher_encryption("AES-GCM"); + cipher.set_key(key); + cipher.set_iv(iv); + + omemo_http_file_meta.iv = iv; + omemo_http_file_meta.key = key; + omemo_http_file_meta.size = file_transfer.size + 16; + omemo_http_file_meta.content_type = new FileContentType.from_mime_type("application/octet-stream"); + file_transfer.input_stream = new ConverterInputStream(file_transfer.input_stream, new SymmetricCipherEncrypter((owned) cipher, 16)); + } catch (Crypto.Error error) { + throw new FileSendError.ENCRYPTION_FAILED("OMEMO file encryption error: %s".printf(error.message)); + } catch (GLib.Error error) { + throw new FileSendError.ENCRYPTION_FAILED("OMEMO file encryption error: %s".printf(error.message)); + } + + debug("Encrypting file %s as %s", file_transfer.file_name, file_transfer.server_file_name); + + return omemo_http_file_meta; + } + + public FileSendData? preprocess_send_file(Conversation conversation, FileTransfer file_transfer, FileSendData file_send_data, FileMeta file_meta) { + HttpFileSendData? send_data = file_send_data as HttpFileSendData; + if (send_data == null) return null; + + OmemoHttpFileMeta? omemo_http_file_meta = file_meta as OmemoHttpFileMeta; + if (omemo_http_file_meta == null) return null; + + // Convert iv and key to hex + string iv_and_key = ""; + foreach (uint8 byte in omemo_http_file_meta.iv) iv_and_key += byte.to_string("%02x"); + foreach (uint8 byte in omemo_http_file_meta.key) iv_and_key += byte.to_string("%02x"); + + string aesgcm_link = send_data.url_down + "#" + iv_and_key; + aesgcm_link = "aesgcm://" + aesgcm_link.substring(8); // replace https:// by aesgcm:// + + send_data.url_down = aesgcm_link; + send_data.encrypt_message = true; + + return file_send_data; + } +} + +} |
