fishlim: Implement correct handling of long and UTF-8 messages
This commit is contained in:
parent
bd3f3fa5f7
commit
078af20e8b
@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
|
|
||||||
Copyright (c) 2010 Samuel Lidén Borell <samuel@kodafritt.se>
|
Copyright (c) 2010 Samuel Lidén Borell <samuel@kodafritt.se>
|
||||||
Copyright (c) 2019 <bakasura@protonmail.ch>
|
Copyright (c) 2019-2020 <bakasura@protonmail.ch>
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -37,8 +37,11 @@
|
|||||||
|
|
||||||
#include "keystore.h"
|
#include "keystore.h"
|
||||||
#include "fish.h"
|
#include "fish.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#define IB 64
|
#define IB 64
|
||||||
|
/* rfc 2812; 512 - CR-LF = 510 */
|
||||||
|
static const int MAX_COMMAND_LENGTH = 510;
|
||||||
static const char fish_base64[] = "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
static const char fish_base64[] = "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
static const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
static const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
||||||
static const signed char fish_unbase64[256] = {
|
static const signed char fish_unbase64[256] = {
|
||||||
@ -401,38 +404,81 @@ char *fish_decrypt_str(const char *key, size_t keylen, const char *data, enum fi
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypts a message (see fish_decrypt). The key is searched for in the
|
* Determine if a nick have a key
|
||||||
* key store.
|
*
|
||||||
|
* @param [in] nick Nickname
|
||||||
|
* @return TRUE if have a key or FALSE if not
|
||||||
*/
|
*/
|
||||||
char *fish_encrypt_for_nick(const char *nick, const char *data, enum fish_mode *omode) {
|
gboolean fish_nick_has_key(const char *nick) {
|
||||||
|
gboolean has_key = FALSE;
|
||||||
char *key;
|
char *key;
|
||||||
char *encrypted, *encrypted_cbc = NULL;
|
|
||||||
enum fish_mode mode;
|
enum fish_mode mode;
|
||||||
|
|
||||||
|
key = keystore_get_key(nick, &mode);
|
||||||
|
if (key) {
|
||||||
|
has_key = TRUE;
|
||||||
|
g_free(key);
|
||||||
|
};
|
||||||
|
|
||||||
|
return has_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypts a message (see fish_encrypt). The key is searched for in the key store
|
||||||
|
*
|
||||||
|
* @param [in] nick Nickname
|
||||||
|
* @param [in] data Plaintext to encrypt
|
||||||
|
* @param [out] omode Mode of encryption
|
||||||
|
* @param [in] command_len Length of command to use without the message part
|
||||||
|
* @return A list of encoded strings with the message encrypted or NULL if any error occurred
|
||||||
|
*/
|
||||||
|
GSList *fish_encrypt_for_nick(const char *nick, const char *data, enum fish_mode *omode, size_t command_len) {
|
||||||
|
char *key;
|
||||||
|
GSList *encrypted_list = NULL;
|
||||||
|
char *encrypted = NULL;
|
||||||
|
enum fish_mode mode;
|
||||||
|
int max_len, max_chunks_len, chunks_len;
|
||||||
|
|
||||||
/* Look for key */
|
/* Look for key */
|
||||||
key = keystore_get_key(nick, &mode);
|
key = keystore_get_key(nick, &mode);
|
||||||
if (!key) return NULL;
|
if (!key) return NULL;
|
||||||
|
|
||||||
*omode = mode;
|
*omode = mode;
|
||||||
|
|
||||||
/* Encrypt */
|
/* Calculate max length of each line */
|
||||||
encrypted = fish_encrypt(key, strlen(key), data, strlen(data), mode);
|
max_len = MAX_COMMAND_LENGTH - command_len;
|
||||||
|
/* Add '*' */
|
||||||
|
if (mode == FISH_CBC_MODE) max_len--;
|
||||||
|
|
||||||
g_free(key);
|
max_chunks_len = max_text_command_len(max_len, mode);
|
||||||
|
|
||||||
if (encrypted == NULL || mode == FISH_ECB_MODE)
|
const char *data_chunk = data;
|
||||||
return encrypted;
|
|
||||||
|
|
||||||
/* Add '*' for CBC */
|
while(foreach_utf8_data_chunks(data_chunk, max_chunks_len, &chunks_len)) {
|
||||||
encrypted_cbc = g_strdup_printf("*%s",encrypted);
|
encrypted = fish_encrypt(key, strlen(key), data_chunk, chunks_len, mode);
|
||||||
g_free(encrypted);
|
|
||||||
|
|
||||||
return encrypted_cbc;
|
if (mode == FISH_CBC_MODE) {
|
||||||
|
/* Add '*' for CBC */
|
||||||
|
encrypted_list = g_slist_append(encrypted_list, g_strdup_printf("*%s", encrypted));
|
||||||
|
g_free(encrypted);
|
||||||
|
} else {
|
||||||
|
encrypted_list = g_slist_append(encrypted_list, encrypted);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Next chunk */
|
||||||
|
data_chunk += chunks_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
return encrypted_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypts a message (see fish_decrypt). The key is searched for in the
|
* Decrypts a message (see fish_decrypt). The key is searched for in the key store
|
||||||
* key store.
|
*
|
||||||
|
* @param [in] nick Nickname
|
||||||
|
* @param [in] data Plaintext to encrypt
|
||||||
|
* @param [out] omode Mode of encryption
|
||||||
|
* @return Plaintext message or NULL if any error occurred
|
||||||
*/
|
*/
|
||||||
char *fish_decrypt_from_nick(const char *nick, const char *data, enum fish_mode *omode) {
|
char *fish_decrypt_from_nick(const char *nick, const char *data, enum fish_mode *omode) {
|
||||||
char *key;
|
char *key;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
|
|
||||||
Copyright (c) 2010 Samuel Lidén Borell <samuel@kodafritt.se>
|
Copyright (c) 2010 Samuel Lidén Borell <samuel@kodafritt.se>
|
||||||
Copyright (c) 2019 <bakasura@protonmail.ch>
|
Copyright (c) 2019-2020 <bakasura@protonmail.ch>
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -40,7 +40,8 @@ char *fish_base64_decode(const char *message, size_t *final_len);
|
|||||||
char *fish_encrypt(const char *key, size_t keylen, const char *message, size_t message_len, enum fish_mode mode);
|
char *fish_encrypt(const char *key, size_t keylen, const char *message, size_t message_len, enum fish_mode mode);
|
||||||
char *fish_decrypt(const char *key, size_t keylen, const char *data, enum fish_mode mode, size_t *final_len);
|
char *fish_decrypt(const char *key, size_t keylen, const char *data, enum fish_mode mode, size_t *final_len);
|
||||||
char *fish_decrypt_str(const char *key, size_t keylen, const char *data, enum fish_mode mode);
|
char *fish_decrypt_str(const char *key, size_t keylen, const char *data, enum fish_mode mode);
|
||||||
char *fish_encrypt_for_nick(const char *nick, const char *data, enum fish_mode *omode);
|
gboolean fish_nick_has_key(const char *nick);
|
||||||
|
GSList *fish_encrypt_for_nick(const char *nick, const char *data, enum fish_mode *omode, size_t command_len);
|
||||||
char *fish_decrypt_from_nick(const char *nick, const char *data, enum fish_mode *omode);
|
char *fish_decrypt_from_nick(const char *nick, const char *data, enum fish_mode *omode);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -55,6 +55,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="dh1080.h" />
|
<ClInclude Include="dh1080.h" />
|
||||||
<ClInclude Include="fish.h" />
|
<ClInclude Include="fish.h" />
|
||||||
|
<ClInclude Include="utils.h" />
|
||||||
<ClInclude Include="irc.h" />
|
<ClInclude Include="irc.h" />
|
||||||
<ClInclude Include="keystore.h" />
|
<ClInclude Include="keystore.h" />
|
||||||
<ClInclude Include="plugin_hexchat.h" />
|
<ClInclude Include="plugin_hexchat.h" />
|
||||||
@ -62,6 +63,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="dh1080.c" />
|
<ClCompile Include="dh1080.c" />
|
||||||
<ClCompile Include="fish.c" />
|
<ClCompile Include="fish.c" />
|
||||||
|
<ClCompile Include="utils.c" />
|
||||||
<ClCompile Include="irc.c" />
|
<ClCompile Include="irc.c" />
|
||||||
<ClCompile Include="keystore.c" />
|
<ClCompile Include="keystore.c" />
|
||||||
<ClCompile Include="plugin_hexchat.c" />
|
<ClCompile Include="plugin_hexchat.c" />
|
||||||
|
@ -29,6 +29,9 @@
|
|||||||
<ClInclude Include="fish.h">
|
<ClInclude Include="fish.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="utils.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
<ClInclude Include="irc.h">
|
<ClInclude Include="irc.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
@ -46,6 +49,9 @@
|
|||||||
<ClCompile Include="fish.c">
|
<ClCompile Include="fish.c">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="utils.c">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
<ClCompile Include="irc.c">
|
<ClCompile Include="irc.c">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
@ -8,6 +8,7 @@ subdir('tests')
|
|||||||
fishlim_sources = [
|
fishlim_sources = [
|
||||||
'dh1080.c',
|
'dh1080.c',
|
||||||
'fish.c',
|
'fish.c',
|
||||||
|
'utils.c',
|
||||||
'irc.c',
|
'irc.c',
|
||||||
'keystore.c',
|
'keystore.c',
|
||||||
'plugin_hexchat.c'
|
'plugin_hexchat.c'
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Copyright (c) 2010-2011 Samuel Lidén Borell <samuel@kodafritt.se>
|
Copyright (c) 2010-2011 Samuel Lidén Borell <samuel@kodafritt.se>
|
||||||
Copyright (c) 2015 <the.cypher@gmail.com>
|
Copyright (c) 2015 <the.cypher@gmail.com>
|
||||||
Copyright (c) 2019 <bakasura@protonmail.ch>
|
Copyright (c) 2019-2020 <bakasura@protonmail.ch>
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -29,7 +29,6 @@
|
|||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "hexchat-plugin.h"
|
#include "hexchat-plugin.h"
|
||||||
|
|
||||||
#include "fish.h"
|
#include "fish.h"
|
||||||
@ -108,13 +107,14 @@ static hexchat_context *find_context_on_network (const char *name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrive the prefix character for own nick in current context
|
* Retrive the field for own user in current context
|
||||||
* @return @ or + or NULL
|
* @return the field value
|
||||||
*/
|
*/
|
||||||
char *get_my_own_prefix(void) {
|
char *get_my_info(const char *field, gboolean find_in_other_context) {
|
||||||
char *result = NULL;
|
char *result = NULL;
|
||||||
const char *own_nick;
|
const char *own_nick;
|
||||||
hexchat_list *list;
|
hexchat_list *list;
|
||||||
|
hexchat_context *ctx_current, *ctx_channel;
|
||||||
|
|
||||||
/* Display message */
|
/* Display message */
|
||||||
own_nick = hexchat_get_info(ph, "nick");
|
own_nick = hexchat_get_info(ph, "nick");
|
||||||
@ -122,12 +122,38 @@ char *get_my_own_prefix(void) {
|
|||||||
if (!own_nick)
|
if (!own_nick)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
/* Get prefix for own nick if any */
|
/* Get field for own nick if any */
|
||||||
list = hexchat_list_get (ph, "users");
|
list = hexchat_list_get(ph, "users");
|
||||||
if (list) {
|
if (list) {
|
||||||
while (hexchat_list_next(ph, list)) {
|
while (hexchat_list_next(ph, list)) {
|
||||||
if (irc_nick_cmp(own_nick, hexchat_list_str(ph, list, "nick")) == 0)
|
if (irc_nick_cmp(own_nick, hexchat_list_str(ph, list, "nick")) == 0)
|
||||||
result = g_strdup(hexchat_list_str(ph, list, "prefix"));
|
result = g_strdup(hexchat_list_str(ph, list, field));
|
||||||
|
}
|
||||||
|
hexchat_list_free(ph, list);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Try to get from a channel (we are outside a channel) */
|
||||||
|
if (!find_in_other_context) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
list = hexchat_list_get(ph, "channels");
|
||||||
|
if (list) {
|
||||||
|
ctx_current = hexchat_get_context(ph);
|
||||||
|
while (hexchat_list_next(ph, list)) {
|
||||||
|
ctx_channel = (hexchat_context *) hexchat_list_str(ph, list, "context");
|
||||||
|
|
||||||
|
hexchat_set_context(ph, ctx_channel);
|
||||||
|
result = get_my_info(field, FALSE);
|
||||||
|
hexchat_set_context(ph, ctx_current);
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
hexchat_list_free(ph, list);
|
hexchat_list_free(ph, list);
|
||||||
}
|
}
|
||||||
@ -135,6 +161,45 @@ char *get_my_own_prefix(void) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrive the prefix character for own nick in current context
|
||||||
|
* @return @ or + or NULL
|
||||||
|
*/
|
||||||
|
char *get_my_own_prefix(void) {
|
||||||
|
return get_my_info("prefix", FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrive the mask for own nick in current context
|
||||||
|
* @return Host name in the form: user@host (or NULL if not known)
|
||||||
|
*/
|
||||||
|
char *get_my_own_host(void) {
|
||||||
|
return get_my_info("host", TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the length of prefix for current user in current context
|
||||||
|
*
|
||||||
|
* @return Length of prefix
|
||||||
|
*/
|
||||||
|
int get_prefix_length(void) {
|
||||||
|
char *own_host;
|
||||||
|
int prefix_len = 0;
|
||||||
|
|
||||||
|
/* ':! ' + 'nick' + 'ident@host', e.g. ':user!~name@mynet.com ' */
|
||||||
|
prefix_len = 3 + strlen(hexchat_get_info(ph, "nick"));
|
||||||
|
own_host = get_my_own_host();
|
||||||
|
if (own_host) {
|
||||||
|
prefix_len += strlen(own_host);
|
||||||
|
} else {
|
||||||
|
/* https://stackoverflow.com/questions/8724954/what-is-the-maximum-number-of-characters-for-a-host-name-in-unix */
|
||||||
|
prefix_len += 64;
|
||||||
|
}
|
||||||
|
g_free(own_host);
|
||||||
|
|
||||||
|
return prefix_len;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Try to decrypt the first occurrence of fish message
|
* Try to decrypt the first occurrence of fish message
|
||||||
*
|
*
|
||||||
@ -228,10 +293,22 @@ static int handle_outgoing(char *word[], char *word_eol[], void *userdata) {
|
|||||||
char *prefix;
|
char *prefix;
|
||||||
enum fish_mode mode;
|
enum fish_mode mode;
|
||||||
char *message;
|
char *message;
|
||||||
/* Encrypt the message if possible */
|
GString *command;
|
||||||
|
GSList *encrypted_list, *encrypted_item;
|
||||||
|
|
||||||
const char *channel = hexchat_get_info(ph, "channel");
|
const char *channel = hexchat_get_info(ph, "channel");
|
||||||
char *encrypted = fish_encrypt_for_nick(channel, word_eol[1], &mode);
|
|
||||||
if (!encrypted) return HEXCHAT_EAT_NONE;
|
/* Check if we can encrypt */
|
||||||
|
if (!fish_nick_has_key(channel)) return HEXCHAT_EAT_NONE;
|
||||||
|
|
||||||
|
command = g_string_new("");
|
||||||
|
g_string_printf(command, "PRIVMSG %s :+OK ", channel);
|
||||||
|
|
||||||
|
encrypted_list = fish_encrypt_for_nick(channel, word_eol[1], &mode, get_prefix_length() + command->len);
|
||||||
|
if (!encrypted_list) {
|
||||||
|
g_string_free(command, TRUE);
|
||||||
|
return HEXCHAT_EAT_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
/* Get prefix for own nick if any */
|
/* Get prefix for own nick if any */
|
||||||
prefix = get_my_own_prefix();
|
prefix = get_my_own_prefix();
|
||||||
@ -241,13 +318,21 @@ static int handle_outgoing(char *word[], char *word_eol[], void *userdata) {
|
|||||||
|
|
||||||
/* Display message */
|
/* Display message */
|
||||||
hexchat_emit_print(ph, "Your Message", hexchat_get_info(ph, "nick"), message, prefix, NULL);
|
hexchat_emit_print(ph, "Your Message", hexchat_get_info(ph, "nick"), message, prefix, NULL);
|
||||||
g_free(prefix);
|
|
||||||
g_free(message);
|
g_free(message);
|
||||||
|
|
||||||
/* Send message */
|
/* Send encrypted messages */
|
||||||
hexchat_commandf(ph, "PRIVMSG %s :+OK %s", channel, encrypted);
|
encrypted_item = encrypted_list;
|
||||||
|
while (encrypted_item)
|
||||||
|
{
|
||||||
|
hexchat_commandf(ph, "%s%s", command->str, (char *)encrypted_item->data);
|
||||||
|
|
||||||
|
encrypted_item = encrypted_item->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_free(prefix);
|
||||||
|
g_string_free(command, TRUE);
|
||||||
|
g_slist_free_full(encrypted_list, g_free);
|
||||||
|
|
||||||
g_free(encrypted);
|
|
||||||
return HEXCHAT_EAT_HEXCHAT;
|
return HEXCHAT_EAT_HEXCHAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -498,8 +583,9 @@ static int handle_keyx(char *word[], char *word_eol[], void *userdata) {
|
|||||||
static int handle_crypt_topic(char *word[], char *word_eol[], void *userdata) {
|
static int handle_crypt_topic(char *word[], char *word_eol[], void *userdata) {
|
||||||
const char *target;
|
const char *target;
|
||||||
const char *topic = word_eol[2];
|
const char *topic = word_eol[2];
|
||||||
char *buf;
|
|
||||||
enum fish_mode mode;
|
enum fish_mode mode;
|
||||||
|
GString *command;
|
||||||
|
GSList *encrypted_list;
|
||||||
|
|
||||||
if (!*topic) {
|
if (!*topic) {
|
||||||
hexchat_print(ph, usage_topic);
|
hexchat_print(ph, usage_topic);
|
||||||
@ -512,44 +598,77 @@ static int handle_crypt_topic(char *word[], char *word_eol[], void *userdata) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
target = hexchat_get_info(ph, "channel");
|
target = hexchat_get_info(ph, "channel");
|
||||||
buf = fish_encrypt_for_nick(target, topic, &mode);
|
|
||||||
if (buf == NULL) {
|
/* Check if we can encrypt */
|
||||||
|
if (!fish_nick_has_key(target)) {
|
||||||
hexchat_printf(ph, "/topic+ error, no key found for %s", target);
|
hexchat_printf(ph, "/topic+ error, no key found for %s", target);
|
||||||
return HEXCHAT_EAT_ALL;
|
return HEXCHAT_EAT_ALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
hexchat_commandf(ph, "TOPIC %s +OK %s", target, buf);
|
command = g_string_new("");
|
||||||
g_free(buf);
|
g_string_printf(command, "TOPIC %s +OK ", target);
|
||||||
return HEXCHAT_EAT_ALL;
|
|
||||||
|
encrypted_list = fish_encrypt_for_nick(target, topic, &mode, get_prefix_length() + command->len);
|
||||||
|
if (!encrypted_list) {
|
||||||
|
g_string_free(command, TRUE);
|
||||||
|
hexchat_printf(ph, "/topic+ error, can't encrypt %s", target);
|
||||||
|
return HEXCHAT_EAT_ALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hexchat_commandf(ph, "%s%s", command->str, (char *) encrypted_list->data);
|
||||||
|
|
||||||
|
g_string_free(command, TRUE);
|
||||||
|
g_slist_free_full(encrypted_list, g_free);
|
||||||
|
|
||||||
|
return HEXCHAT_EAT_ALL;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command handler for /notice+
|
* Command handler for /notice+
|
||||||
*/
|
*/
|
||||||
static int handle_crypt_notice(char *word[], char *word_eol[], void *userdata)
|
static int handle_crypt_notice(char *word[], char *word_eol[], void *userdata) {
|
||||||
{
|
|
||||||
const char *target = word[2];
|
const char *target = word[2];
|
||||||
const char *notice = word_eol[3];
|
const char *notice = word_eol[3];
|
||||||
char *notice_flag;
|
char *notice_flag;
|
||||||
char *buf;
|
|
||||||
enum fish_mode mode;
|
enum fish_mode mode;
|
||||||
|
GString *command;
|
||||||
|
GSList *encrypted_list, *encrypted_item;
|
||||||
|
|
||||||
if (!*target || !*notice) {
|
if (!*target || !*notice) {
|
||||||
hexchat_print(ph, usage_notice);
|
hexchat_print(ph, usage_notice);
|
||||||
return HEXCHAT_EAT_ALL;
|
return HEXCHAT_EAT_ALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = fish_encrypt_for_nick(target, notice, &mode);
|
/* Check if we can encrypt */
|
||||||
if (buf == NULL) {
|
if (!fish_nick_has_key(target)) {
|
||||||
hexchat_printf(ph, "/notice+ error, no key found for %s.", target);
|
hexchat_printf(ph, "/notice+ error, no key found for %s.", target);
|
||||||
return HEXCHAT_EAT_ALL;
|
return HEXCHAT_EAT_ALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
hexchat_commandf(ph, "quote NOTICE %s :+OK %s", target, buf);
|
command = g_string_new("");
|
||||||
|
g_string_printf(command, "quote NOTICE %s :+OK ", target);
|
||||||
|
|
||||||
|
encrypted_list = fish_encrypt_for_nick(target, notice, &mode, get_prefix_length() + command->len);
|
||||||
|
if (!encrypted_list) {
|
||||||
|
g_string_free(command, TRUE);
|
||||||
|
hexchat_printf(ph, "/notice+ error, can't encrypt %s", target);
|
||||||
|
return HEXCHAT_EAT_ALL;
|
||||||
|
}
|
||||||
|
|
||||||
notice_flag = g_strconcat("[", fish_modes[mode], "] ", notice, NULL);
|
notice_flag = g_strconcat("[", fish_modes[mode], "] ", notice, NULL);
|
||||||
hexchat_emit_print(ph, "Notice Send", target, notice_flag);
|
hexchat_emit_print(ph, "Notice Send", target, notice_flag);
|
||||||
|
|
||||||
|
/* Send encrypted messages */
|
||||||
|
encrypted_item = encrypted_list;
|
||||||
|
while (encrypted_item) {
|
||||||
|
hexchat_commandf(ph, "%s%s", command->str, (char *) encrypted_item->data);
|
||||||
|
|
||||||
|
encrypted_item = encrypted_item->next;
|
||||||
|
}
|
||||||
|
|
||||||
g_free(notice_flag);
|
g_free(notice_flag);
|
||||||
g_free(buf);
|
g_string_free(command, TRUE);
|
||||||
|
g_slist_free_full(encrypted_list, g_free);
|
||||||
|
|
||||||
return HEXCHAT_EAT_ALL;
|
return HEXCHAT_EAT_ALL;
|
||||||
}
|
}
|
||||||
@ -563,21 +682,41 @@ static int handle_crypt_msg(char *word[], char *word_eol[], void *userdata) {
|
|||||||
char *message_flag;
|
char *message_flag;
|
||||||
char *prefix;
|
char *prefix;
|
||||||
hexchat_context *query_ctx;
|
hexchat_context *query_ctx;
|
||||||
char *buf;
|
|
||||||
enum fish_mode mode;
|
enum fish_mode mode;
|
||||||
|
GString *command;
|
||||||
|
GSList *encrypted_list, *encrypted_item;
|
||||||
|
|
||||||
if (!*target || !*message) {
|
if (!*target || !*message) {
|
||||||
hexchat_print(ph, usage_msg);
|
hexchat_print(ph, usage_msg);
|
||||||
return HEXCHAT_EAT_ALL;
|
return HEXCHAT_EAT_ALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = fish_encrypt_for_nick(target, message, &mode);
|
/* Check if we can encrypt */
|
||||||
if (buf == NULL) {
|
if (!fish_nick_has_key(target)) {
|
||||||
hexchat_printf(ph, "/msg+ error, no key found for %s", target);
|
hexchat_printf(ph, "/msg+ error, no key found for %s", target);
|
||||||
return HEXCHAT_EAT_ALL;
|
return HEXCHAT_EAT_ALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
hexchat_commandf(ph, "PRIVMSG %s :+OK %s", target, buf);
|
command = g_string_new("");
|
||||||
|
g_string_printf(command, "PRIVMSG %s :+OK ", target);
|
||||||
|
|
||||||
|
encrypted_list = fish_encrypt_for_nick(target, message, &mode, get_prefix_length() + command->len);
|
||||||
|
if (!encrypted_list) {
|
||||||
|
g_string_free(command, TRUE);
|
||||||
|
hexchat_printf(ph, "/msg+ error, can't encrypt %s", target);
|
||||||
|
return HEXCHAT_EAT_ALL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Send encrypted messages */
|
||||||
|
encrypted_item = encrypted_list;
|
||||||
|
while (encrypted_item) {
|
||||||
|
hexchat_commandf(ph, "%s%s", command->str, (char *) encrypted_item->data);
|
||||||
|
|
||||||
|
encrypted_item = encrypted_item->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_string_free(command, TRUE);
|
||||||
|
g_slist_free_full(encrypted_list, g_free);
|
||||||
|
|
||||||
query_ctx = find_context_on_network(target);
|
query_ctx = find_context_on_network(target);
|
||||||
if (query_ctx) {
|
if (query_ctx) {
|
||||||
@ -587,33 +726,52 @@ static int handle_crypt_msg(char *word[], char *word_eol[], void *userdata) {
|
|||||||
|
|
||||||
/* Add encrypted flag */
|
/* Add encrypted flag */
|
||||||
message_flag = g_strconcat("[", fish_modes[mode], "] ", message, NULL);
|
message_flag = g_strconcat("[", fish_modes[mode], "] ", message, NULL);
|
||||||
hexchat_emit_print(ph, "Your Message", hexchat_get_info(ph, "nick"),
|
hexchat_emit_print(ph, "Your Message", hexchat_get_info(ph, "nick"), message_flag, prefix, NULL);
|
||||||
message_flag, prefix, NULL);
|
|
||||||
g_free(prefix);
|
g_free(prefix);
|
||||||
g_free(message_flag);
|
g_free(message_flag);
|
||||||
} else {
|
} else {
|
||||||
hexchat_emit_print(ph, "Message Send", target, message);
|
hexchat_emit_print(ph, "Message Send", target, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free(buf);
|
|
||||||
return HEXCHAT_EAT_ALL;
|
return HEXCHAT_EAT_ALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int handle_crypt_me(char *word[], char *word_eol[], void *userdata) {
|
static int handle_crypt_me(char *word[], char *word_eol[], void *userdata) {
|
||||||
const char *channel = hexchat_get_info(ph, "channel");
|
const char *channel = hexchat_get_info(ph, "channel");
|
||||||
char *buf;
|
enum fish_mode mode;
|
||||||
enum fish_mode mode;
|
GString *command;
|
||||||
|
GSList *encrypted_list, *encrypted_item;
|
||||||
|
|
||||||
buf = fish_encrypt_for_nick(channel, word_eol[2], &mode);
|
/* Check if we can encrypt */
|
||||||
if (!buf)
|
if (!fish_nick_has_key(channel)) {
|
||||||
return HEXCHAT_EAT_NONE;
|
return HEXCHAT_EAT_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
hexchat_commandf(ph, "PRIVMSG %s :\001ACTION +OK %s \001", channel, buf);
|
command = g_string_new("");
|
||||||
hexchat_emit_print(ph, "Your Action", hexchat_get_info(ph, "nick"),
|
g_string_printf(command, "PRIVMSG %s :\001ACTION +OK ", channel);
|
||||||
word_eol[2], NULL);
|
|
||||||
|
|
||||||
g_free(buf);
|
/* 2 = ' \001' */
|
||||||
return HEXCHAT_EAT_ALL;
|
encrypted_list = fish_encrypt_for_nick(channel, word_eol[2], &mode, get_prefix_length() + command->len + 2);
|
||||||
|
if (!encrypted_list) {
|
||||||
|
g_string_free(command, TRUE);
|
||||||
|
hexchat_printf(ph, "/me error, can't encrypt %s", channel);
|
||||||
|
return HEXCHAT_EAT_ALL;
|
||||||
|
}
|
||||||
|
|
||||||
|
hexchat_emit_print(ph, "Your Action", hexchat_get_info(ph, "nick"), word_eol[2], NULL);
|
||||||
|
|
||||||
|
/* Send encrypted messages */
|
||||||
|
encrypted_item = encrypted_list;
|
||||||
|
while (encrypted_item) {
|
||||||
|
hexchat_commandf(ph, "%s%s \001", command->str, (char *) encrypted_item->data);
|
||||||
|
|
||||||
|
encrypted_item = encrypted_item->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_string_free(command, TRUE);
|
||||||
|
g_slist_free_full(encrypted_list, g_free);
|
||||||
|
|
||||||
|
return HEXCHAT_EAT_ALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -230,6 +230,54 @@ void __base64_cbc_len(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check the calculation of length limit for a plaintext in each encryption mode
|
||||||
|
*/
|
||||||
|
void __max_text_command_len(void) {
|
||||||
|
int max_encoded_len, plaintext_len;
|
||||||
|
enum fish_mode mode;
|
||||||
|
|
||||||
|
for (max_encoded_len = 0; max_encoded_len < 10000; ++max_encoded_len) {
|
||||||
|
for (mode = FISH_ECB_MODE; mode <= FISH_CBC_MODE; ++mode) {
|
||||||
|
plaintext_len = max_text_command_len(max_encoded_len, mode);
|
||||||
|
g_assert_cmpuint(encoded_len(plaintext_len, mode), <= , max_encoded_len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check the calculation of length limit for a plaintext in each encryption mode
|
||||||
|
*/
|
||||||
|
void __foreach_utf8_data_chunks(void) {
|
||||||
|
GRand *rand = NULL;
|
||||||
|
GString *chunks = NULL;
|
||||||
|
int tests, max_chunks_len, chunks_len;
|
||||||
|
char ascii_message[1001];
|
||||||
|
char *data_chunk = NULL;
|
||||||
|
|
||||||
|
rand = g_rand_new();
|
||||||
|
|
||||||
|
for (tests = 0; tests < 1000; ++tests) {
|
||||||
|
|
||||||
|
max_chunks_len = g_rand_int_range(rand, 2, 301);
|
||||||
|
random_string(ascii_message, 1000);
|
||||||
|
|
||||||
|
data_chunk = ascii_message;
|
||||||
|
|
||||||
|
chunks = g_string_new(NULL);
|
||||||
|
|
||||||
|
while (foreach_utf8_data_chunks(data_chunk, max_chunks_len, &chunks_len)) {
|
||||||
|
g_string_append(chunks, g_strndup(data_chunk, chunks_len));
|
||||||
|
/* Next chunk */
|
||||||
|
data_chunk += chunks_len;
|
||||||
|
}
|
||||||
|
/* Check data loss */
|
||||||
|
g_assert_cmpstr(chunks->str, == , ascii_message);
|
||||||
|
g_string_free(chunks, TRUE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
g_test_init(&argc, &argv, NULL);
|
g_test_init(&argc, &argv, NULL);
|
||||||
@ -240,6 +288,8 @@ int main(int argc, char *argv[]) {
|
|||||||
g_test_add_func("/fishlim/__base64_fish_len", __base64_fish_len);
|
g_test_add_func("/fishlim/__base64_fish_len", __base64_fish_len);
|
||||||
g_test_add_func("/fishlim/__base64_ecb_len", __base64_ecb_len);
|
g_test_add_func("/fishlim/__base64_ecb_len", __base64_ecb_len);
|
||||||
g_test_add_func("/fishlim/__base64_cbc_len", __base64_cbc_len);
|
g_test_add_func("/fishlim/__base64_cbc_len", __base64_cbc_len);
|
||||||
|
g_test_add_func("/fishlim/__max_text_command_len", __max_text_command_len);
|
||||||
|
g_test_add_func("/fishlim/__foreach_utf8_data_chunks", __foreach_utf8_data_chunks);
|
||||||
|
|
||||||
return g_test_run();
|
return g_test_run();
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
#include "fish.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate the length of Base64-encoded string
|
* Calculate the length of Base64-encoded string
|
||||||
@ -68,3 +69,81 @@ unsigned long cbc_len(size_t plaintext_len) {
|
|||||||
unsigned long ecb_len(size_t plaintext_len) {
|
unsigned long ecb_len(size_t plaintext_len) {
|
||||||
return base64_fish_len(plaintext_len);
|
return base64_fish_len(plaintext_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the length of encrypted string in 'mode' mode
|
||||||
|
*
|
||||||
|
* @param plaintext_len Length of plaintext
|
||||||
|
* @param mode Encryption mode
|
||||||
|
* @return Size of encoded string
|
||||||
|
*/
|
||||||
|
unsigned long encoded_len(size_t plaintext_len, enum fish_mode mode) {
|
||||||
|
switch (mode) {
|
||||||
|
|
||||||
|
case FISH_CBC_MODE:
|
||||||
|
return cbc_len(plaintext_len);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FISH_ECB_MODE:
|
||||||
|
return ecb_len(plaintext_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine the maximum length of plaintext for a 'max_len' limit taking care the overload of encryption
|
||||||
|
*
|
||||||
|
* @param max_len Limit for plaintext
|
||||||
|
* @param mode Encryption mode
|
||||||
|
* @return Maximum allowed plaintext length
|
||||||
|
*/
|
||||||
|
int max_text_command_len(size_t max_len, enum fish_mode mode) {
|
||||||
|
int len;
|
||||||
|
|
||||||
|
for (len = max_len; encoded_len(len, mode) > max_len; --len);
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Iterate over 'data' in chunks of 'max_chunk_len' taking care the UTF-8 characters
|
||||||
|
*
|
||||||
|
* @param data Data to iterate
|
||||||
|
* @param max_chunk_len Size of biggest chunk
|
||||||
|
* @param [out] chunk_len Current chunk length
|
||||||
|
* @return Pointer to current chunk position or NULL if not have more chunks
|
||||||
|
*/
|
||||||
|
const char *foreach_utf8_data_chunks(const char *data, int max_chunk_len, int *chunk_len) {
|
||||||
|
int data_len, last_chunk_len = 0;
|
||||||
|
|
||||||
|
if (!*data) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Last chunk of data */
|
||||||
|
data_len = strlen(data);
|
||||||
|
if (data_len <= max_chunk_len) {
|
||||||
|
*chunk_len = data_len;
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
*chunk_len = 0;
|
||||||
|
const char *utf8_character = data;
|
||||||
|
|
||||||
|
/* Not valid UTF-8, but maybe valid text, just split into max length */
|
||||||
|
if (!g_utf8_validate(data, -1, NULL)) {
|
||||||
|
*chunk_len = max_chunk_len;
|
||||||
|
return utf8_character;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (*utf8_character && *chunk_len <= max_chunk_len) {
|
||||||
|
last_chunk_len = *chunk_len;
|
||||||
|
*chunk_len = (g_utf8_next_char(utf8_character) - data) * sizeof(*utf8_character);
|
||||||
|
utf8_character = g_utf8_next_char(utf8_character);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* We need the previous length before overflow the limit */
|
||||||
|
*chunk_len = last_chunk_len;
|
||||||
|
|
||||||
|
return utf8_character;
|
||||||
|
}
|
@ -26,10 +26,14 @@
|
|||||||
#define PLUGIN_HEXCHAT_FISHLIM_UTILS_H
|
#define PLUGIN_HEXCHAT_FISHLIM_UTILS_H
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include "fish.h"
|
||||||
|
|
||||||
unsigned long base64_len(size_t plaintext_len);
|
unsigned long base64_len(size_t plaintext_len);
|
||||||
unsigned long base64_fish_len(size_t plaintext_len);
|
unsigned long base64_fish_len(size_t plaintext_len);
|
||||||
unsigned long cbc_len(size_t plaintext_len);
|
unsigned long cbc_len(size_t plaintext_len);
|
||||||
unsigned long ecb_len(size_t plaintext_len);
|
unsigned long ecb_len(size_t plaintext_len);
|
||||||
|
unsigned long encoded_len(size_t plaintext_len, enum fish_mode mode);
|
||||||
|
int max_text_command_len(size_t max_len, enum fish_mode mode);
|
||||||
|
const char *foreach_utf8_data_chunks(const char *data, int max_chunk_len, int *chunk_len);
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user