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/utils.h"
41 #include "alertpanel.h"
42 #include "inputdialog.h"
44 #include "prefs_common.h"
46 #ifndef PASSWORD_CRYPTO_OLD
47 static gchar *_master_password = NULL;
49 static const gchar *master_password()
54 if (!prefs_common_get_prefs()->use_master_password) {
58 if (_master_password != NULL) {
59 debug_print("Master password is in memory, offering it.\n");
60 return _master_password;
64 input = input_dialog_with_invisible(_("Input master password"),
65 _("Input master password"), NULL);
68 debug_print("Cancel pressed at master password dialog.\n");
72 if (master_password_is_correct(input)) {
73 debug_print("Entered master password seems to be correct, remembering it.\n");
74 _master_password = input;
77 alertpanel_error(_("Incorrect master password."));
81 return _master_password;
84 const gboolean master_password_is_set()
86 if (prefs_common_get_prefs()->master_password_hash == NULL
87 || strlen(prefs_common_get_prefs()->master_password_hash) == 0)
93 const gboolean master_password_is_correct(const gchar *input)
96 gchar *stored_hash = prefs_common_get_prefs()->master_password_hash;
97 const GChecksumType hashtype = G_CHECKSUM_SHA512;
98 const gssize hashlen = g_checksum_type_get_length(hashtype);
101 g_return_val_if_fail(input != NULL, FALSE);
103 if (stored_hash == NULL)
106 stored_len = strlen(stored_hash);
107 g_return_val_if_fail(stored_len == 2*hashlen, FALSE);
109 hash = g_compute_checksum_for_string(hashtype, input, -1);
111 if (!strncasecmp(hash, stored_hash, stored_len)) {
120 gboolean master_password_is_entered()
122 return (_master_password == NULL) ? FALSE : TRUE;
125 void master_password_forget()
127 /* If master password is currently in memory (entered by user),
128 * get rid of it. User will have to enter the new one again. */
129 if (_master_password != NULL) {
130 memset(_master_password, 0, strlen(_master_password));
131 g_free(_master_password);
133 _master_password = NULL;
136 void master_password_change(const gchar *newp)
143 /* Make sure the user has to enter the master password before
144 * being able to change it. */
145 master_password_forget();
147 oldp = master_password();
148 g_return_if_fail(oldp != NULL);
150 /* Update master password hash in prefs */
151 if (prefs_common_get_prefs()->master_password_hash != NULL)
152 g_free(prefs_common_get_prefs()->master_password_hash);
155 debug_print("Storing hash of new master password\n");
156 prefs_common_get_prefs()->master_password_hash =
157 g_compute_checksum_for_string(G_CHECKSUM_SHA512, newp, -1);
159 debug_print("Setting master_password_hash to NULL\n");
160 prefs_common_get_prefs()->master_password_hash = NULL;
163 /* Now go over all accounts, reencrypting their passwords using
164 * the new master password. */
167 oldp = PASSCRYPT_KEY;
169 newp = PASSCRYPT_KEY;
171 debug_print("Reencrypting all account passwords...\n");
172 for (cur = account_get_list(); cur != NULL; cur = cur->next) {
173 acc = (PrefsAccount *)cur->data;
174 debug_print("account %s\n", acc->account_name);
176 /* Password for receiving */
177 if (acc->passwd != NULL && strlen(acc->passwd) > 0) {
178 pwd = password_decrypt(acc->passwd, oldp);
180 debug_print("failed to decrypt recv password with old master password\n");
182 newpwd = password_encrypt(pwd, newp);
183 memset(pwd, 0, strlen(pwd));
185 if (newpwd == NULL) {
186 debug_print("failed to encrypt recv password with new master password\n");
189 acc->passwd = newpwd;
194 /* Password for sending */
195 if (acc->smtp_passwd != NULL && strlen(acc->smtp_passwd) > 0) {
196 pwd = password_decrypt(acc->smtp_passwd, oldp);
198 debug_print("failed to decrypt smtp password with old master password\n");
200 newpwd = password_encrypt(pwd, newp);
201 memset(pwd, 0, strlen(pwd));
203 if (newpwd == NULL) {
204 debug_print("failed to encrypt smtp password with new master password\n");
206 g_free(acc->smtp_passwd);
207 acc->smtp_passwd = newpwd;
213 master_password_forget();
217 gchar *password_encrypt_old(const gchar *password)
219 if (!password || strlen(password) == 0) {
223 gchar *encrypted = g_strdup(password);
224 gchar *encoded, *result;
225 gsize len = strlen(password);
227 passcrypt_encrypt(encrypted, len);
228 encoded = g_base64_encode(encrypted, len);
230 result = g_strconcat("!", encoded, NULL);
236 gchar *password_decrypt_old(const gchar *password)
238 if (!password || strlen(password) == 0) {
242 if (*password != '!' || strlen(password) < 2) {
247 gchar *decrypted = g_base64_decode(password + 1, &len);
249 passcrypt_decrypt(decrypted, len);
253 #ifdef PASSWORD_CRYPTO_GNUTLS
256 gchar *password_encrypt_gnutls(const gchar *password,
257 const gchar *encryption_password)
259 /* Another, slightly inferior combination is AES-128-CBC + SHA-256.
260 * Any block cipher in CBC mode with keysize N and a hash algo with
261 * digest length 2*N would do. */
262 gnutls_cipher_algorithm_t algo = GNUTLS_CIPHER_AES_256_CBC;
263 gnutls_digest_algorithm_t digest = GNUTLS_DIG_SHA512;
264 gnutls_cipher_hd_t handle;
265 gnutls_datum_t key, iv;
266 int ivlen, keylen, digestlen, blocklen, ret, i;
267 unsigned char hashbuf[BUFSIZE], *buf, *encbuf, *base, *output;
272 g_return_val_if_fail(password != NULL, NULL);
273 g_return_val_if_fail(encryption_password != NULL, NULL);
275 ivlen = gnutls_cipher_get_iv_size(algo);
276 keylen = gnutls_cipher_get_key_size(algo);
277 blocklen = gnutls_cipher_get_block_size(algo);
278 digestlen = gnutls_hash_get_len(digest);
280 /* Prepare key for cipher - first half of hash of passkey XORed with
282 memset(&hashbuf, 0, BUFSIZE);
283 if ((ret = gnutls_hash_fast(digest, encryption_password,
284 strlen(encryption_password), &hashbuf)) < 0) {
285 debug_print("Hashing passkey failed: %s\n", gnutls_strerror(ret));
288 for (i = 0; i < digestlen/2; i++) {
289 hashbuf[i] = hashbuf[i] ^ hashbuf[i+digestlen/2];
292 key.data = malloc(keylen);
293 memcpy(key.data, &hashbuf, keylen);
297 /* Prepare our source of random data. */
298 rnd = open("/dev/urandom", O_RDONLY);
300 perror("fopen on /dev/urandom");
307 /* Prepare random IV for cipher */
308 iv.data = malloc(ivlen);
311 ret = read(rnd, iv.data, ivlen);
313 perror("read into iv");
321 /* Initialize the encryption */
322 ret = gnutls_cipher_init(&handle, algo, &key, &iv);
332 /* Fill buf with one block of random data, our password, pad the
333 * rest with zero bytes. */
334 buf = malloc(BUFSIZE + blocklen);
335 memset(buf, 0, BUFSIZE);
337 ret = read(rnd, buf, blocklen);
338 if (ret != blocklen) {
339 perror("read into buffer");
344 gnutls_cipher_deinit(handle);
348 /* We don't need any more random data. */
352 memcpy(buf + blocklen, password, strlen(password));
354 /* Encrypt into encbuf */
355 encbuf = malloc(BUFSIZE + blocklen);
356 memset(encbuf, 0, BUFSIZE + blocklen);
357 ret = gnutls_cipher_encrypt2(handle, buf, BUFSIZE + blocklen,
358 encbuf, BUFSIZE + blocklen);
364 gnutls_cipher_deinit(handle);
369 gnutls_cipher_deinit(handle);
374 /* And finally prepare the resulting string:
375 * "{algorithm}base64encodedciphertext" */
376 base = g_base64_encode(encbuf, BUFSIZE);
378 output = g_strdup_printf("{%s}%s", gnutls_cipher_get_name(algo), base);
384 gchar *password_decrypt_gnutls(const gchar *password,
385 const gchar *decryption_password)
387 gchar **tokens, *tmp;
388 gnutls_cipher_algorithm_t algo;
389 gnutls_digest_algorithm_t digest = GNUTLS_DIG_UNKNOWN;
390 gnutls_cipher_hd_t handle;
391 gnutls_datum_t key, iv;
392 int ivlen, keylen, digestlen, blocklen, ret, i;
394 unsigned char hashbuf[BUFSIZE], *buf;
399 g_return_val_if_fail(password != NULL, NULL);
400 g_return_val_if_fail(decryption_password != NULL, NULL);
402 tokens = g_strsplit_set(password, "{}", 3);
404 /* Parse the string, retrieving algorithm and encrypted data.
405 * We expect "{algorithm}base64encodedciphertext". */
406 if (strlen(tokens[0]) != 0 ||
407 (algo = gnutls_cipher_get_id(tokens[1])) == GNUTLS_CIPHER_UNKNOWN ||
408 strlen(tokens[2]) == 0)
411 /* Our hash algo needs to have digest length twice as long as our
412 * cipher algo's key length. */
413 if (algo == GNUTLS_CIPHER_AES_256_CBC) {
414 debug_print("Using AES-256-CBC + SHA-512 for decryption\n");
415 digest = GNUTLS_DIG_SHA512;
416 } else if (algo == GNUTLS_CIPHER_AES_128_CBC) {
417 debug_print("Using AES-128-CBC + SHA-256 for decryption\n");
418 digest = GNUTLS_DIG_SHA256;
420 if (digest == GNUTLS_DIG_UNKNOWN) {
421 debug_print("Password is encrypted with unsupported cipher, giving up.\n");
426 ivlen = gnutls_cipher_get_iv_size(algo);
427 keylen = gnutls_cipher_get_key_size(algo);
428 blocklen = gnutls_cipher_get_block_size(algo);
429 digestlen = gnutls_hash_get_len(digest);
431 /* Prepare key for cipher - first half of hash of passkey XORed with
432 * the second. AES-256 has key length 32 and length of SHA-512 hash
433 * is exactly twice that, 64. */
434 memset(&hashbuf, 0, BUFSIZE);
435 if ((ret = gnutls_hash_fast(digest, decryption_password,
436 strlen(decryption_password), &hashbuf)) < 0) {
437 debug_print("Hashing passkey failed: %s\n", gnutls_strerror(ret));
441 for (i = 0; i < digestlen/2; i++) {
442 hashbuf[i] = hashbuf[i] ^ hashbuf[i+digestlen/2];
445 key.data = malloc(keylen);
446 memcpy(key.data, &hashbuf, keylen);
450 /* Prepare our source of random data. */
451 rnd = open("/dev/urandom", O_RDONLY);
453 perror("fopen on /dev/urandom");
461 /* Prepare random IV for cipher */
462 iv.data = malloc(ivlen);
465 ret = read(rnd, iv.data, ivlen);
467 perror("read into iv");
475 /* We don't need any more random data. */
479 /* Prepare encrypted password string for decryption. */
480 tmp = g_base64_decode(tokens[2], &len);
483 /* Initialize the decryption */
484 ret = gnutls_cipher_init(&handle, algo, &key, &iv);
486 debug_print("Cipher init failed: %s\n", gnutls_strerror(ret));
492 buf = malloc(BUFSIZE + blocklen);
493 memset(buf, 0, BUFSIZE + blocklen);
494 ret = gnutls_cipher_decrypt2(handle, tmp, len,
495 buf, BUFSIZE + blocklen);
497 debug_print("Decryption failed: %s\n", gnutls_strerror(ret));
501 gnutls_cipher_deinit(handle);
506 gnutls_cipher_deinit(handle);
510 tmp = g_strndup(buf + blocklen, MIN(strlen(buf + blocklen), BUFSIZE));
519 gchar *password_encrypt(const gchar *password,
520 const gchar *encryption_password)
522 if (password == NULL || strlen(password) == 0) {
526 #ifndef PASSWORD_CRYPTO_OLD
527 if (encryption_password == NULL)
528 encryption_password = master_password();
530 return password_encrypt_real(password, encryption_password);
533 return password_encrypt_old(password);
536 gchar *password_decrypt(const gchar *password,
537 const gchar *decryption_password)
539 if (password == NULL || strlen(password) == 0) {
543 /* First, check if the password was possibly decrypted using old,
545 if (*password == '!') {
546 debug_print("Trying to decrypt password using the old method...\n");
547 return password_decrypt_old(password);
550 /* Try available crypto backend */
551 #ifndef PASSWORD_CRYPTO_OLD
552 if (decryption_password == NULL)
553 decryption_password = master_password();
555 if (*password == '{') {
556 debug_print("Trying to decrypt password...\n");
557 return password_decrypt_real(password, decryption_password);
561 /* Fallback, in case the configuration is really old and
562 * stored password in plaintext */
563 debug_print("Assuming password was stored plaintext, returning it unchanged\n");
564 return g_strdup(password);