1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Tidied pgp memory allocations

This commit is contained in:
James Booth 2015-08-11 01:00:23 +01:00
parent e2a528cad4
commit 2af5c151a0
6 changed files with 78 additions and 64 deletions

View File

@ -27,28 +27,12 @@
... ...
} }
# Ignore history module, needs to be rewritten # glib
{ {
history_next glib
Memcheck:Leak Memcheck:Leak
... ...
fun:history_next fun:start_thread
... fun:clone
}
{
history_previous
Memcheck:Leak
...
fun:history_previous
...
}
{
history_append
Memcheck:Leak
...
fun:history_append
...
} }

View File

@ -218,6 +218,7 @@ sv_ev_incoming_message(char *barejid, char *resource, char *message, char *enc_m
ui_incoming_msg(chatwin, resource, decrypted, NULL, new_win); ui_incoming_msg(chatwin, resource, decrypted, NULL, new_win);
chat_log_pgp_msg_in(barejid, decrypted); chat_log_pgp_msg_in(barejid, decrypted);
chatwin->enc_mode = PROF_ENC_PGP; chatwin->enc_mode = PROF_ENC_PGP;
p_gpg_free_decrypted(decrypted);
} else { } else {
ui_incoming_msg(chatwin, resource, message, NULL, new_win); ui_incoming_msg(chatwin, resource, message, NULL, new_win);
chat_log_msg_in(barejid, message); chat_log_msg_in(barejid, message);
@ -267,6 +268,7 @@ sv_ev_incoming_message(char *barejid, char *resource, char *message, char *enc_m
ui_incoming_msg(chatwin, resource, decrypted, NULL, new_win); ui_incoming_msg(chatwin, resource, decrypted, NULL, new_win);
chat_log_pgp_msg_in(barejid, decrypted); chat_log_pgp_msg_in(barejid, decrypted);
chatwin->enc_mode = PROF_ENC_PGP; chatwin->enc_mode = PROF_ENC_PGP;
p_gpg_free_decrypted(decrypted);
} else { } else {
ui_incoming_msg(chatwin, resource, message, NULL, new_win); ui_incoming_msg(chatwin, resource, message, NULL, new_win);
chat_log_msg_in(barejid, message); chat_log_msg_in(barejid, message);

View File

@ -132,6 +132,7 @@ p_gpg_on_connect(const char * const barejid)
gpgme_ctx_t ctx; gpgme_ctx_t ctx;
gpgme_error_t error = gpgme_new(&ctx); gpgme_error_t error = gpgme_new(&ctx);
if (error) { if (error) {
log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error)); log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error));
g_strfreev(jids); g_strfreev(jids);
@ -157,7 +158,7 @@ p_gpg_on_connect(const char * const barejid)
} }
g_hash_table_replace(fingerprints, strdup(jid), strdup(key->subkeys->fpr)); g_hash_table_replace(fingerprints, strdup(jid), strdup(key->subkeys->fpr));
gpgme_key_release(key); gpgme_key_unref(key);
} }
} }
@ -196,6 +197,8 @@ p_gpg_addkey(const char * const jid, const char * const keyid)
gpgme_key_t key = NULL; gpgme_key_t key = NULL;
error = gpgme_get_key(ctx, keyid, &key, 1); error = gpgme_get_key(ctx, keyid, &key, 1);
gpgme_release(ctx);
if (error || key == NULL) { if (error || key == NULL) {
log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error)); log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
return FALSE; return FALSE;
@ -207,7 +210,7 @@ p_gpg_addkey(const char * const jid, const char * const keyid)
// update in memory fingerprint list // update in memory fingerprint list
g_hash_table_replace(fingerprints, strdup(jid), strdup(key->subkeys->fpr)); g_hash_table_replace(fingerprints, strdup(jid), strdup(key->subkeys->fpr));
gpgme_key_release(key); gpgme_key_unref(key);
return TRUE; return TRUE;
} }
@ -216,11 +219,11 @@ GSList *
p_gpg_list_keys(void) p_gpg_list_keys(void)
{ {
gpgme_error_t error; gpgme_error_t error;
gpgme_ctx_t ctx;
gpgme_key_t key;
GSList *result = NULL; GSList *result = NULL;
gpgme_ctx_t ctx;
error = gpgme_new(&ctx); error = gpgme_new(&ctx);
if (error) { if (error) {
log_error("GPG: Could not list keys. %s %s", gpgme_strsource(error), gpgme_strerror(error)); log_error("GPG: Could not list keys. %s %s", gpgme_strsource(error), gpgme_strerror(error));
return NULL; return NULL;
@ -229,7 +232,9 @@ p_gpg_list_keys(void)
error = gpgme_op_keylist_start(ctx, NULL, 1); error = gpgme_op_keylist_start(ctx, NULL, 1);
if (error == GPG_ERR_NO_ERROR) { if (error == GPG_ERR_NO_ERROR) {
while (!error) { while (!error) {
gpgme_key_t key;
error = gpgme_op_keylist_next(ctx, &key); error = gpgme_op_keylist_next(ctx, &key);
if (error) { if (error) {
break; break;
} }
@ -241,7 +246,7 @@ p_gpg_list_keys(void)
result = g_slist_append(result, p_pgpkey); result = g_slist_append(result, p_pgpkey);
gpgme_key_release(key); gpgme_key_unref(key);
} }
} else { } else {
log_error("GPG: Could not list keys. %s %s", gpgme_strsource(error), gpgme_strerror(error)); log_error("GPG: Could not list keys. %s %s", gpgme_strsource(error), gpgme_strerror(error));
@ -291,18 +296,24 @@ p_gpg_verify(const char * const barejid, const char *const sign)
gpgme_ctx_t ctx; gpgme_ctx_t ctx;
gpgme_error_t error = gpgme_new(&ctx); gpgme_error_t error = gpgme_new(&ctx);
if (error) { if (error) {
log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error)); log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error));
return; return;
} }
gpgme_data_t sign_data;
gpgme_data_t plain_data;
char *sign_with_header_footer = _add_header_footer(sign, PGP_SIGNATURE_HEADER, PGP_SIGNATURE_FOOTER); char *sign_with_header_footer = _add_header_footer(sign, PGP_SIGNATURE_HEADER, PGP_SIGNATURE_FOOTER);
gpgme_data_t sign_data;
gpgme_data_new_from_mem(&sign_data, sign_with_header_footer, strlen(sign_with_header_footer), 1); gpgme_data_new_from_mem(&sign_data, sign_with_header_footer, strlen(sign_with_header_footer), 1);
free(sign_with_header_footer);
gpgme_data_t plain_data;
gpgme_data_new(&plain_data); gpgme_data_new(&plain_data);
error = gpgme_op_verify(ctx, sign_data, NULL, plain_data); error = gpgme_op_verify(ctx, sign_data, NULL, plain_data);
gpgme_data_release(sign_data);
gpgme_data_release(plain_data);
if (error) { if (error) {
log_error("GPG: Failed to verify. %s %s", gpgme_strsource(error), gpgme_strerror(error)); log_error("GPG: Failed to verify. %s %s", gpgme_strsource(error), gpgme_strerror(error));
gpgme_release(ctx); gpgme_release(ctx);
@ -317,10 +328,7 @@ p_gpg_verify(const char * const barejid, const char *const sign)
} }
} }
gpgme_data_release(sign_data);
gpgme_data_release(plain_data);
gpgme_release(ctx); gpgme_release(ctx);
free(sign_with_header_footer);
} }
char* char*
@ -335,55 +343,58 @@ p_gpg_sign(const char * const str, const char * const fp)
gpgme_key_t key = NULL; gpgme_key_t key = NULL;
error = gpgme_get_key(ctx, fp, &key, 1); error = gpgme_get_key(ctx, fp, &key, 1);
if (error || key == NULL) { if (error || key == NULL) {
log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error)); log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
gpgme_release(ctx); gpgme_release(ctx);
if (key) {
gpgme_key_unref(key);
}
return NULL; return NULL;
} }
gpgme_signers_clear(ctx); gpgme_signers_clear(ctx);
error = gpgme_signers_add(ctx, key); error = gpgme_signers_add(ctx, key);
gpgme_key_unref(key); gpgme_key_unref(key);
if (error) { if (error) {
log_error("GPG: Failed to load signer. %s %s", gpgme_strsource(error), gpgme_strerror(error)); log_error("GPG: Failed to load signer. %s %s", gpgme_strsource(error), gpgme_strerror(error));
gpgme_release(ctx); gpgme_release(ctx);
return NULL; return NULL;
} }
gpgme_data_t str_data;
gpgme_data_t signed_data;
char *str_or_empty = NULL; char *str_or_empty = NULL;
if (str) { if (str) {
str_or_empty = strdup(str); str_or_empty = strdup(str);
} else { } else {
str_or_empty = strdup(""); str_or_empty = strdup("");
} }
gpgme_data_t str_data;
gpgme_data_new_from_mem(&str_data, str_or_empty, strlen(str_or_empty), 1); gpgme_data_new_from_mem(&str_data, str_or_empty, strlen(str_or_empty), 1);
free(str_or_empty);
gpgme_data_t signed_data;
gpgme_data_new(&signed_data); gpgme_data_new(&signed_data);
gpgme_set_armor(ctx,1); gpgme_set_armor(ctx,1);
error = gpgme_op_sign(ctx,str_data,signed_data,GPGME_SIG_MODE_DETACH); error = gpgme_op_sign(ctx, str_data, signed_data, GPGME_SIG_MODE_DETACH);
gpgme_data_release(str_data);
gpgme_release(ctx);
if (error) { if (error) {
log_error("GPG: Failed to sign string. %s %s", gpgme_strsource(error), gpgme_strerror(error)); log_error("GPG: Failed to sign string. %s %s", gpgme_strsource(error), gpgme_strerror(error));
gpgme_release(ctx); gpgme_data_release(signed_data);
return NULL; return NULL;
} }
char *result = NULL; char *result = NULL;
gpgme_data_release(str_data);
size_t len = 0; size_t len = 0;
char *signed_str = gpgme_data_release_and_get_mem(signed_data, &len); char *signed_str = gpgme_data_release_and_get_mem(signed_data, &len);
if (signed_str) { if (signed_str) {
signed_str[len] = 0; GString *signed_gstr = g_string_new("");
result = _remove_header_footer(signed_str, PGP_SIGNATURE_FOOTER); g_string_append_len(signed_gstr, signed_str, len);
result = _remove_header_footer(signed_gstr->str, PGP_SIGNATURE_FOOTER);
g_string_free(signed_gstr, TRUE);
gpgme_free(signed_str);
} }
gpgme_free(signed_str);
gpgme_release(ctx);
free(str_or_empty);
return result; return result;
} }
@ -411,6 +422,7 @@ p_gpg_encrypt(const char * const barejid, const char * const message)
gpgme_key_t key; gpgme_key_t key;
error = gpgme_get_key(ctx, fp, &key, 0); error = gpgme_get_key(ctx, fp, &key, 0);
if (error || key == NULL) { if (error || key == NULL) {
log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error)); log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
gpgme_release(ctx); gpgme_release(ctx);
@ -420,29 +432,33 @@ p_gpg_encrypt(const char * const barejid, const char * const message)
keys[0] = key; keys[0] = key;
gpgme_data_t plain; gpgme_data_t plain;
gpgme_data_t cipher;
gpgme_data_new_from_mem(&plain, message, strlen(message), 1); gpgme_data_new_from_mem(&plain, message, strlen(message), 1);
gpgme_data_t cipher;
gpgme_data_new(&cipher); gpgme_data_new(&cipher);
gpgme_set_armor(ctx, 1); gpgme_set_armor(ctx, 1);
error = gpgme_op_encrypt(ctx, keys, GPGME_ENCRYPT_ALWAYS_TRUST, plain, cipher); error = gpgme_op_encrypt(ctx, keys, GPGME_ENCRYPT_ALWAYS_TRUST, plain, cipher);
gpgme_data_release(plain);
gpgme_release(ctx);
gpgme_key_unref(key);
if (error) { if (error) {
log_error("GPG: Failed to encrypt message. %s %s", gpgme_strsource(error), gpgme_strerror(error)); log_error("GPG: Failed to encrypt message. %s %s", gpgme_strsource(error), gpgme_strerror(error));
gpgme_release(ctx);
return NULL; return NULL;
} }
gpgme_data_release(plain);
char *cipher_str = NULL;
char *result = NULL;
size_t len; size_t len;
cipher_str = gpgme_data_release_and_get_mem(cipher, &len); char *cipher_str = gpgme_data_release_and_get_mem(cipher, &len);
if (cipher_str) {
result = _remove_header_footer(cipher_str, PGP_MESSAGE_FOOTER);
}
gpgme_free(cipher_str); char *result = NULL;
gpgme_release(ctx); if (cipher_str) {
GString *cipher_gstr = g_string_new("");
g_string_append_len(cipher_gstr, cipher_str, len);
result = _remove_header_footer(cipher_gstr->str, PGP_MESSAGE_FOOTER);
g_string_free(cipher_gstr, TRUE);
gpgme_free(cipher_str);
}
return result; return result;
} }
@ -450,29 +466,32 @@ p_gpg_encrypt(const char * const barejid, const char * const message)
char * char *
p_gpg_decrypt(const char * const barejid, const char * const cipher) p_gpg_decrypt(const char * const barejid, const char * const cipher)
{ {
char *cipher_with_headers = _add_header_footer(cipher, PGP_MESSAGE_HEADER, PGP_MESSAGE_FOOTER);
gpgme_ctx_t ctx; gpgme_ctx_t ctx;
gpgme_error_t error = gpgme_new(&ctx); gpgme_error_t error = gpgme_new(&ctx);
if (error) { if (error) {
log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error)); log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error));
return NULL; return NULL;
} }
gpgme_data_t plain_data; char *cipher_with_headers = _add_header_footer(cipher, PGP_MESSAGE_HEADER, PGP_MESSAGE_FOOTER);
gpgme_data_t cipher_data; gpgme_data_t cipher_data;
gpgme_data_new_from_mem (&cipher_data, cipher_with_headers, strlen(cipher_with_headers), 1); gpgme_data_new_from_mem(&cipher_data, cipher_with_headers, strlen(cipher_with_headers), 1);
free(cipher_with_headers);
gpgme_data_t plain_data;
gpgme_data_new(&plain_data); gpgme_data_new(&plain_data);
error = gpgme_op_decrypt(ctx, cipher_data, plain_data); error = gpgme_op_decrypt(ctx, cipher_data, plain_data);
gpgme_data_release(cipher_data);
gpgme_release(ctx);
if (error) { if (error) {
log_error("GPG: Failed to encrypt message. %s %s", gpgme_strsource(error), gpgme_strerror(error)); log_error("GPG: Failed to encrypt message. %s %s", gpgme_strsource(error), gpgme_strerror(error));
gpgme_release(ctx); gpgme_data_release(plain_data);
return NULL; return NULL;
} }
gpgme_data_release(cipher_data);
size_t len = 0; size_t len = 0;
char *plain_str = gpgme_data_release_and_get_mem(plain_data, &len); char *plain_str = gpgme_data_release_and_get_mem(plain_data, &len);
char *result = NULL; char *result = NULL;
@ -482,11 +501,15 @@ p_gpg_decrypt(const char * const barejid, const char * const cipher)
} }
gpgme_free(plain_str); gpgme_free(plain_str);
gpgme_release(ctx);
return result; return result;
} }
void
p_gpg_free_decrypted(char *decrypted)
{
g_free(decrypted);
}
static char* static char*
_remove_header_footer(char *str, const char * const footer) _remove_header_footer(char *str, const char * const footer)
{ {

View File

@ -55,5 +55,6 @@ char* p_gpg_sign(const char * const str, const char * const fp);
void p_gpg_verify(const char * const barejid, const char *const sign); void p_gpg_verify(const char * const barejid, const char *const sign);
char* p_gpg_encrypt(const char * const barejid, const char * const message); char* p_gpg_encrypt(const char * const barejid, const char * const message);
char* p_gpg_decrypt(const char * const barejid, const char * const cipher); char* p_gpg_decrypt(const char * const barejid, const char * const cipher);
void p_gpg_free_decrypted(char *decrypted);
#endif #endif

View File

@ -174,6 +174,7 @@ message_send_chat_pgp(const char * const barejid, const char * const msg)
} else { } else {
message = stanza_create_message(ctx, id, jid, STANZA_TYPE_CHAT, msg); message = stanza_create_message(ctx, id, jid, STANZA_TYPE_CHAT, msg);
} }
account_free(account);
#else #else
message = stanza_create_message(ctx, id, jid, STANZA_TYPE_CHAT, msg); message = stanza_create_message(ctx, id, jid, STANZA_TYPE_CHAT, msg);
#endif #endif
@ -772,6 +773,7 @@ _chat_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * con
} }
// standard chat message, use jid without resource // standard chat message, use jid without resource
xmpp_ctx_t *ctx = connection_get_ctx();
GDateTime *timestamp = stanza_get_delay(stanza); GDateTime *timestamp = stanza_get_delay(stanza);
if (body) { if (body) {
char *message = xmpp_stanza_get_text(body); char *message = xmpp_stanza_get_text(body);
@ -785,11 +787,11 @@ _chat_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * con
enc_message = xmpp_stanza_get_text(x); enc_message = xmpp_stanza_get_text(x);
} }
sv_ev_incoming_message(jid->barejid, jid->resourcepart, message, enc_message); sv_ev_incoming_message(jid->barejid, jid->resourcepart, message, enc_message);
xmpp_free(ctx, enc_message);
} }
_receipt_request_handler(stanza); _receipt_request_handler(stanza);
xmpp_ctx_t *ctx = connection_get_ctx();
xmpp_free(ctx, message); xmpp_free(ctx, message);
} }
} }

View File

@ -47,3 +47,5 @@ gboolean p_gpg_addkey(const char * const jid, const char * const keyid)
return TRUE; return TRUE;
} }
void p_gpg_free_decrypted(char *decrypted) {}