From c94746703a3e0b59372b3bbf9355112c6a85d2dc Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Wed, 14 Mar 2018 12:18:31 +0100 Subject: Implement master password encrypted string tags --- libsylph/imap.c | 2 +- libsylph/masterpassword.c | 31 +++++++++++++++++++++---------- libsylph/masterpassword.h | 7 ++++--- libsylph/news.c | 12 +++++------- libsylph/pop.c | 12 ++++-------- src/send_message.c | 15 ++++++--------- 6 files changed, 41 insertions(+), 38 deletions(-) diff --git a/libsylph/imap.c b/libsylph/imap.c index 9ee4b15..e773ad4 100644 --- a/libsylph/imap.c +++ b/libsylph/imap.c @@ -707,7 +707,7 @@ static gint imap_session_connect(IMAPSession *session) log_message(_("creating IMAP4 connection to %s:%d ...\n"), SESSION(session)->server, SESSION(session)->port); - + /* TODO: check of the format is correct | possible memory leak */ pass = decrypt_with_master_password(account->passwd); if (!pass) pass = account->tmp_pass; diff --git a/libsylph/masterpassword.c b/libsylph/masterpassword.c index d240a7a..23e8f2f 100644 --- a/libsylph/masterpassword.c +++ b/libsylph/masterpassword.c @@ -48,6 +48,15 @@ void unload_master_password(void) { } +gint mpes_string_prefix(const gchar *str) { + + /* this function will be expanded in time as the format changes */ + if (g_str_has_prefix(str, "mpes1:")) + return 6; + return 0; + +} + gboolean master_password_active(void) { #if USE_SSL @@ -59,38 +68,40 @@ gboolean master_password_active(void) { } -gchar *decrypt_with_master_password(gchar *str) { +gchar *decrypt_with_master_password(const gchar *str) { #if USE_SSL gchar *new_str; + gint str_prefix; if ((!str) || (!master_password_active())) - return str; /* do nothing */ + return g_strdup(str); + str_prefix = mpes_string_prefix(str); if (decrypt_data(&new_str, - str, + str + str_prefix, master_password, - strlen(str)) != RC_OK) { + strlen(str) - str_prefix) != RC_OK) { OPENSSL_cleanse(new_str, strlen(new_str)); g_free(new_str); - return str; + return g_strdup(str); } return new_str; #else - return str; /* do nothing */ + return g_strdup(str); #endif } -gchar *encrypt_with_master_password(gchar *str) { +gchar *encrypt_with_master_password(const gchar *str) { #if USE_SSL gchar *new_str; gint length_encrypted; if ((!str) || (!master_password_active())) - return str; /* do nothing */ + return g_strdup(str); if (encrypt_data(&new_str, &length_encrypted, @@ -101,12 +112,12 @@ gchar *encrypt_with_master_password(gchar *str) { TRUE) != RC_OK) { OPENSSL_cleanse(new_str, strlen(new_str)); g_free(new_str); - return str; + return g_strdup(str); } return new_str; #else - return str; /* do nothing */ + return g_strdup(str); #endif } diff --git a/libsylph/masterpassword.h b/libsylph/masterpassword.h index 9d00384..91f3793 100644 --- a/libsylph/masterpassword.h +++ b/libsylph/masterpassword.h @@ -1,6 +1,6 @@ /* * LibSylph -- E-Mail client library - * Copyright (C) 1999-2006 Hiroyuki Yamamoto + * Copyright (C) 1999-2018 Hiroyuki Yamamoto * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -28,9 +28,10 @@ extern gchar *master_password; void set_master_password(const char *password); gchar *get_master_password(void); void unload_master_password(void); +gint mpes_string_prefix(const gchar *str); gboolean master_password_active(void); -gchar *decrypt_with_master_password(gchar *str); -gchar *encrypt_with_master_password(gchar *str); +gchar *decrypt_with_master_password(const gchar *str); +gchar *encrypt_with_master_password(const gchar *str); #if USE_SSL gint set_master_password_interactively(guint max_attempts); diff --git a/libsylph/news.c b/libsylph/news.c index b7a922a..e75976e 100644 --- a/libsylph/news.c +++ b/libsylph/news.c @@ -250,14 +250,12 @@ static Session *news_session_new_for_folder(Folder *folder) ac = folder->account; if (ac->use_nntp_auth && ac->userid && ac->userid[0]) { userid = ac->userid; - if (ac->passwd && ac->passwd[0]) - if (master_password_active()) { - passwd = decrypt_with_master_password(ac->passwd); - } else { - passwd = g_strdup(ac->passwd); - } - else + if (ac->passwd && ac->passwd[0]) { + /* TODO: check if format is correct */ + passwd = decrypt_with_master_password(ac->passwd); + } else { passwd = input_query_password(ac->nntp_server, userid); + } } if (ac->use_socks && ac->use_socks_for_recv && ac->proxy_host) { diff --git a/libsylph/pop.c b/libsylph/pop.c index a387f5e..96f347e 100644 --- a/libsylph/pop.c +++ b/libsylph/pop.c @@ -438,14 +438,10 @@ Session *pop3_session_new(PrefsAccount *account) session->error_msg = NULL; session->user = g_strdup(account->userid); - if (master_password_active()) { - session->pass = account->passwd ? decrypt_with_master_password( - account->passwd) : account->tmp_pass ? g_strdup( - account->tmp_pass) : NULL; - } else { - session->pass = account->passwd ? g_strdup(account->passwd) : - account->tmp_pass ? g_strdup(account->tmp_pass) : NULL; - } + /* TODO: check format */ + session->pass = account->passwd ? decrypt_with_master_password( + account->passwd) : account->tmp_pass ? g_strdup( + account->tmp_pass) : NULL; SESSION(session)->server = g_strdup(account->recv_server); diff --git a/src/send_message.c b/src/send_message.c index d5fc6e8..c0a7f59 100644 --- a/src/send_message.c +++ b/src/send_message.c @@ -649,17 +649,14 @@ static gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp) if (ac_prefs->smtp_userid) { smtp_session->user = g_strdup(ac_prefs->smtp_userid); - if (ac_prefs->smtp_passwd) - if (master_password_active()) { - smtp_session->pass = decrypt_with_master_password( - ac_prefs->smtp_passwd); - } else { - smtp_session->pass = g_strdup(ac_prefs->smtp_passwd); - } - else if (ac_prefs->tmp_smtp_pass) + if (ac_prefs->smtp_passwd) { + /* TODO: check if the format is correct */ + smtp_session->pass = decrypt_with_master_password( + ac_prefs->smtp_passwd); + } else if (ac_prefs->tmp_smtp_pass) { smtp_session->pass = g_strdup(ac_prefs->tmp_smtp_pass); - else { + } else { smtp_session->pass = input_query_password (ac_prefs->smtp_server, -- cgit v1.3