2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2016 The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
25 #ifdef PASSWORD_CRYPTO_GNUTLS
26 # include <gnutls/gnutls.h>
27 # include <gnutls/crypto.h>
31 #include <glib/gi18n.h>
38 #include "common/passcrypt.h"
39 #include "common/plugin.h"
40 #include "common/utils.h"
42 #include "alertpanel.h"
43 #include "inputdialog.h"
45 #include "prefs_common.h"
47 #ifndef PASSWORD_CRYPTO_OLD
48 static gchar *_master_password = NULL;
50 static const gchar *master_password()
55 if (!prefs_common_get_prefs()->use_master_password) {
59 if (_master_password != NULL) {
60 debug_print("Master password is in memory, offering it.\n");
61 return _master_password;
65 input = input_dialog_with_invisible(_("Input master password"),
66 _("Input master password"), NULL);
69 debug_print("Cancel pressed at master password dialog.\n");
73 if (master_password_is_correct(input)) {
74 debug_print("Entered master password seems to be correct, remembering it.\n");
75 _master_password = input;
78 alertpanel_error(_("Incorrect master password."));
82 return _master_password;
85 const gboolean master_password_is_set()
87 if (prefs_common_get_prefs()->master_password_hash == NULL
88 || strlen(prefs_common_get_prefs()->master_password_hash) == 0)
94 const gboolean master_password_is_correct(const gchar *input)
97 gchar *stored_hash = prefs_common_get_prefs()->master_password_hash;
98 const GChecksumType hashtype = G_CHECKSUM_SHA512;
99 const gssize hashlen = g_checksum_type_get_length(hashtype);
102 g_return_val_if_fail(input != NULL, FALSE);
104 if (stored_hash == NULL)
107 stored_len = strlen(stored_hash);
108 g_return_val_if_fail(stored_len == 2*hashlen, FALSE);
110 hash = g_compute_checksum_for_string(hashtype, input, -1);
112 if (!strncasecmp(hash, stored_hash, stored_len)) {
121 gboolean master_password_is_entered()
123 return (_master_password == NULL) ? FALSE : TRUE;
126 void master_password_forget()
128 /* If master password is currently in memory (entered by user),
129 * get rid of it. User will have to enter the new one again. */
130 if (_master_password != NULL) {
131 memset(_master_password, 0, strlen(_master_password));
132 g_free(_master_password);
134 _master_password = NULL;
137 void master_password_change(const gchar *newp)
144 /* Make sure the user has to enter the master password before
145 * being able to change it. */
146 master_password_forget();
148 oldp = master_password();
149 g_return_if_fail(oldp != NULL);
151 /* Update master password hash in prefs */
152 if (prefs_common_get_prefs()->master_password_hash != NULL)
153 g_free(prefs_common_get_prefs()->master_password_hash);
156 debug_print("Storing hash of new master password\n");
157 prefs_common_get_prefs()->master_password_hash =
158 g_compute_checksum_for_string(G_CHECKSUM_SHA512, newp, -1);
160 debug_print("Setting master_password_hash to NULL\n");
161 prefs_common_get_prefs()->master_password_hash = NULL;
164 /* Now go over all accounts, reencrypting their passwords using
165 * the new master password. */
168 oldp = PASSCRYPT_KEY;
170 newp = PASSCRYPT_KEY;
172 debug_print("Reencrypting all account passwords...\n");
173 for (cur = account_get_list(); cur != NULL; cur = cur->next) {
174 acc = (PrefsAccount *)cur->data;
175 debug_print("account %s\n", acc->account_name);
177 /* Password for receiving */
178 if (acc->passwd != NULL && strlen(acc->passwd) > 0) {
179 pwd = password_decrypt(acc->passwd, oldp);
181 debug_print("failed to decrypt recv password with old master password\n");
183 newpwd = password_encrypt(pwd, newp);
184 memset(pwd, 0, strlen(pwd));
186 if (newpwd == NULL) {
187 debug_print("failed to encrypt recv password with new master password\n");
190 acc->passwd = newpwd;
195 /* Password for sending */
196 if (acc->smtp_passwd != NULL && strlen(acc->smtp_passwd) > 0) {
197 pwd = password_decrypt(acc->smtp_passwd, oldp);
199 debug_print("failed to decrypt smtp password with old master password\n");
201 newpwd = password_encrypt(pwd, newp);
202 memset(pwd, 0, strlen(pwd));
204 if (newpwd == NULL) {
205 debug_print("failed to encrypt smtp password with new master password\n");
207 g_free(acc->smtp_passwd);
208 acc->smtp_passwd = newpwd;
214 /* Now reencrypt all plugins passwords fields
215 * FIXME: Unloaded plugins won't be able to update their stored passwords
217 plugins_master_password_change(oldp, newp);
219 master_password_forget();
223 gchar *password_encrypt_old(const gchar *password)
225 if (!password || strlen(password) == 0) {
229 gchar *encrypted = g_strdup(password);
230 gchar *encoded, *result;
231 gsize len = strlen(password);
233 passcrypt_encrypt(encrypted, len);
234 encoded = g_base64_encode(encrypted, len);
236 result = g_strconcat("!", encoded, NULL);
242 gchar *password_decrypt_old(const gchar *password)
244 if (!password || strlen(password) == 0) {
248 if (*password != '!' || strlen(password) < 2) {
253 gchar *decrypted = g_base64_decode(password + 1, &len);
255 passcrypt_decrypt(decrypted, len);
259 #ifdef PASSWORD_CRYPTO_GNUTLS
262 gchar *password_encrypt_gnutls(const gchar *password,
263 const gchar *encryption_password)
265 /* Another, slightly inferior combination is AES-128-CBC + SHA-256.
266 * Any block cipher in CBC mode with keysize N and a hash algo with
267 * digest length 2*N would do. */
268 gnutls_cipher_algorithm_t algo = GNUTLS_CIPHER_AES_256_CBC;
269 gnutls_digest_algorithm_t digest = GNUTLS_DIG_SHA512;
270 gnutls_cipher_hd_t handle;
271 gnutls_datum_t key, iv;
272 int ivlen, keylen, digestlen, blocklen, ret, i;
273 unsigned char hashbuf[BUFSIZE], *buf, *encbuf, *base, *output;
278 g_return_val_if_fail(password != NULL, NULL);
279 g_return_val_if_fail(encryption_password != NULL, NULL);
281 ivlen = gnutls_cipher_get_iv_size(algo);
282 keylen = gnutls_cipher_get_key_size(algo);
283 blocklen = gnutls_cipher_get_block_size(algo);
284 digestlen = gnutls_hash_get_len(digest);
286 /* Prepare key for cipher - first half of hash of passkey XORed with
288 memset(&hashbuf, 0, BUFSIZE);
289 if ((ret = gnutls_hash_fast(digest, encryption_password,
290 strlen(encryption_password), &hashbuf)) < 0) {
291 debug_print("Hashing passkey failed: %s\n", gnutls_strerror(ret));
294 for (i = 0; i < digestlen/2; i++) {
295 hashbuf[i] = hashbuf[i] ^ hashbuf[i+digestlen/2];
298 key.data = malloc(keylen);
299 memcpy(key.data, &hashbuf, keylen);
303 /* Prepare our source of random data. */
304 rnd = open("/dev/urandom", O_RDONLY);
306 perror("fopen on /dev/urandom");
313 /* Prepare random IV for cipher */
314 iv.data = malloc(ivlen);
317 ret = read(rnd, iv.data, ivlen);
319 perror("read into iv");
327 /* Initialize the encryption */
328 ret = gnutls_cipher_init(&handle, algo, &key, &iv);
338 /* Fill buf with one block of random data, our password, pad the
339 * rest with zero bytes. */
340 buf = malloc(BUFSIZE + blocklen);
341 memset(buf, 0, BUFSIZE);
343 ret = read(rnd, buf, blocklen);
344 if (ret != blocklen) {
345 perror("read into buffer");
350 gnutls_cipher_deinit(handle);
354 /* We don't need any more random data. */
358 memcpy(buf + blocklen, password, strlen(password));
360 /* Encrypt into encbuf */
361 encbuf = malloc(BUFSIZE + blocklen);
362 memset(encbuf, 0, BUFSIZE + blocklen);
363 ret = gnutls_cipher_encrypt2(handle, buf, BUFSIZE + blocklen,
364 encbuf, BUFSIZE + blocklen);
370 gnutls_cipher_deinit(handle);
375 gnutls_cipher_deinit(handle);
380 /* And finally prepare the resulting string:
381 * "{algorithm}base64encodedciphertext" */
382 base = g_base64_encode(encbuf, BUFSIZE);
384 output = g_strdup_printf("{%s}%s", gnutls_cipher_get_name(algo), base);
390 gchar *password_decrypt_gnutls(const gchar *password,
391 const gchar *decryption_password)
393 gchar **tokens, *tmp;
394 gnutls_cipher_algorithm_t algo;
395 gnutls_digest_algorithm_t digest = GNUTLS_DIG_UNKNOWN;
396 gnutls_cipher_hd_t handle;
397 gnutls_datum_t key, iv;
398 int ivlen, keylen, digestlen, blocklen, ret, i;
400 unsigned char hashbuf[BUFSIZE], *buf;
405 g_return_val_if_fail(password != NULL, NULL);
406 g_return_val_if_fail(decryption_password != NULL, NULL);
408 tokens = g_strsplit_set(password, "{}", 3);
410 /* Parse the string, retrieving algorithm and encrypted data.
411 * We expect "{algorithm}base64encodedciphertext". */
412 if (strlen(tokens[0]) != 0 ||
413 (algo = gnutls_cipher_get_id(tokens[1])) == GNUTLS_CIPHER_UNKNOWN ||
414 strlen(tokens[2]) == 0)
417 /* Our hash algo needs to have digest length twice as long as our
418 * cipher algo's key length. */
419 if (algo == GNUTLS_CIPHER_AES_256_CBC) {
420 debug_print("Using AES-256-CBC + SHA-512 for decryption\n");
421 digest = GNUTLS_DIG_SHA512;
422 } else if (algo == GNUTLS_CIPHER_AES_128_CBC) {
423 debug_print("Using AES-128-CBC + SHA-256 for decryption\n");
424 digest = GNUTLS_DIG_SHA256;
426 if (digest == GNUTLS_DIG_UNKNOWN) {
427 debug_print("Password is encrypted with unsupported cipher, giving up.\n");
432 ivlen = gnutls_cipher_get_iv_size(algo);
433 keylen = gnutls_cipher_get_key_size(algo);
434 blocklen = gnutls_cipher_get_block_size(algo);
435 digestlen = gnutls_hash_get_len(digest);
437 /* Prepare key for cipher - first half of hash of passkey XORed with
438 * the second. AES-256 has key length 32 and length of SHA-512 hash
439 * is exactly twice that, 64. */
440 memset(&hashbuf, 0, BUFSIZE);
441 if ((ret = gnutls_hash_fast(digest, decryption_password,
442 strlen(decryption_password), &hashbuf)) < 0) {
443 debug_print("Hashing passkey failed: %s\n", gnutls_strerror(ret));
447 for (i = 0; i < digestlen/2; i++) {
448 hashbuf[i] = hashbuf[i] ^ hashbuf[i+digestlen/2];
451 key.data = malloc(keylen);
452 memcpy(key.data, &hashbuf, keylen);
456 /* Prepare our source of random data. */
457 rnd = open("/dev/urandom", O_RDONLY);
459 perror("fopen on /dev/urandom");
467 /* Prepare random IV for cipher */
468 iv.data = malloc(ivlen);
471 ret = read(rnd, iv.data, ivlen);
473 perror("read into iv");
481 /* We don't need any more random data. */
485 /* Prepare encrypted password string for decryption. */
486 tmp = g_base64_decode(tokens[2], &len);
489 /* Initialize the decryption */
490 ret = gnutls_cipher_init(&handle, algo, &key, &iv);
492 debug_print("Cipher init failed: %s\n", gnutls_strerror(ret));
498 buf = malloc(BUFSIZE + blocklen);
499 memset(buf, 0, BUFSIZE + blocklen);
500 ret = gnutls_cipher_decrypt2(handle, tmp, len,
501 buf, BUFSIZE + blocklen);
503 debug_print("Decryption failed: %s\n", gnutls_strerror(ret));
507 gnutls_cipher_deinit(handle);
512 gnutls_cipher_deinit(handle);
516 tmp = g_strndup(buf + blocklen, MIN(strlen(buf + blocklen), BUFSIZE));
525 gchar *password_encrypt(const gchar *password,
526 const gchar *encryption_password)
528 if (password == NULL || strlen(password) == 0) {
532 #ifndef PASSWORD_CRYPTO_OLD
533 if (encryption_password == NULL)
534 encryption_password = master_password();
536 return password_encrypt_real(password, encryption_password);
539 return password_encrypt_old(password);
542 gchar *password_decrypt(const gchar *password,
543 const gchar *decryption_password)
545 if (password == NULL || strlen(password) == 0) {
549 /* First, check if the password was possibly decrypted using old,
551 if (*password == '!') {
552 debug_print("Trying to decrypt password using the old method...\n");
553 return password_decrypt_old(password);
556 /* Try available crypto backend */
557 #ifndef PASSWORD_CRYPTO_OLD
558 if (decryption_password == NULL)
559 decryption_password = master_password();
561 if (*password == '{') {
562 debug_print("Trying to decrypt password...\n");
563 return password_decrypt_real(password, decryption_password);
567 /* Fallback, in case the configuration is really old and
568 * stored password in plaintext */
569 debug_print("Assuming password was stored plaintext, returning it unchanged\n");
570 return g_strdup(password);