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 void master_password_forget()
122 /* If master password is currently in memory (entered by user),
123 * get rid of it. User will have to enter the new one again. */
124 if (_master_password != NULL) {
125 memset(_master_password, 0, strlen(_master_password));
126 g_free(_master_password);
128 _master_password = NULL;
131 void master_password_change(const gchar *newp)
138 /* Make sure the user has to enter the master password before
139 * being able to change it. */
140 master_password_forget();
142 oldp = master_password();
143 g_return_if_fail(oldp != NULL);
145 /* Update master password hash in prefs */
146 if (prefs_common_get_prefs()->master_password_hash != NULL)
147 g_free(prefs_common_get_prefs()->master_password_hash);
150 debug_print("Storing hash of new master password\n");
151 prefs_common_get_prefs()->master_password_hash =
152 g_compute_checksum_for_string(G_CHECKSUM_SHA512, newp, -1);
154 debug_print("Setting master_password_hash to NULL\n");
155 prefs_common_get_prefs()->master_password_hash = NULL;
158 /* Now go over all accounts, reencrypting their passwords using
159 * the new master password. */
162 oldp = PASSCRYPT_KEY;
164 newp = PASSCRYPT_KEY;
166 debug_print("Reencrypting all account passwords...\n");
167 for (cur = account_get_list(); cur != NULL; cur = cur->next) {
168 acc = (PrefsAccount *)cur->data;
169 debug_print("account %s\n", acc->account_name);
171 /* Password for receiving */
172 if (acc->passwd != NULL && strlen(acc->passwd) > 0) {
173 pwd = password_decrypt(acc->passwd, oldp);
175 debug_print("failed to decrypt recv password with old master password\n");
177 newpwd = password_encrypt(pwd, newp);
178 memset(pwd, 0, strlen(pwd));
180 if (newpwd == NULL) {
181 debug_print("failed to encrypt recv password with new master password\n");
184 acc->passwd = newpwd;
189 /* Password for sending */
190 if (acc->smtp_passwd != NULL && strlen(acc->smtp_passwd) > 0) {
191 pwd = password_decrypt(acc->smtp_passwd, oldp);
193 debug_print("failed to decrypt smtp password with old master password\n");
195 newpwd = password_encrypt(pwd, newp);
196 memset(pwd, 0, strlen(pwd));
198 if (newpwd == NULL) {
199 debug_print("failed to encrypt smtp password with new master password\n");
201 g_free(acc->smtp_passwd);
202 acc->smtp_passwd = newpwd;
208 master_password_forget();
212 gchar *password_encrypt_old(const gchar *password)
214 if (!password || strlen(password) == 0) {
218 gchar *encrypted = g_strdup(password);
219 gchar *encoded, *result;
220 gsize len = strlen(password);
222 passcrypt_encrypt(encrypted, len);
223 encoded = g_base64_encode(encrypted, len);
225 result = g_strconcat("!", encoded, NULL);
231 gchar *password_decrypt_old(const gchar *password)
233 if (!password || strlen(password) == 0) {
237 if (*password != '!' || strlen(password) < 2) {
242 gchar *decrypted = g_base64_decode(password + 1, &len);
244 passcrypt_decrypt(decrypted, len);
248 #ifdef PASSWORD_CRYPTO_GNUTLS
251 gchar *password_encrypt_gnutls(const gchar *password,
252 const gchar *encryption_password)
254 /* Another, slightly inferior combination is AES-128-CBC + SHA-256.
255 * Any block cipher in CBC mode with keysize N and a hash algo with
256 * digest length 2*N would do. */
257 gnutls_cipher_algorithm_t algo = GNUTLS_CIPHER_AES_256_CBC;
258 gnutls_digest_algorithm_t digest = GNUTLS_DIG_SHA512;
259 gnutls_cipher_hd_t handle;
260 gnutls_datum_t key, iv;
261 int ivlen, keylen, digestlen, blocklen, ret, i;
262 unsigned char hashbuf[BUFSIZE], *buf, *encbuf, *base, *output;
267 g_return_val_if_fail(password != NULL, NULL);
268 g_return_val_if_fail(encryption_password != NULL, NULL);
270 ivlen = gnutls_cipher_get_iv_size(algo);
271 keylen = gnutls_cipher_get_key_size(algo);
272 blocklen = gnutls_cipher_get_block_size(algo);
273 digestlen = gnutls_hash_get_len(digest);
275 /* Prepare key for cipher - first half of hash of passkey XORed with
277 memset(&hashbuf, 0, BUFSIZE);
278 if ((ret = gnutls_hash_fast(digest, encryption_password,
279 strlen(encryption_password), &hashbuf)) < 0) {
280 debug_print("Hashing passkey failed: %s\n", gnutls_strerror(ret));
283 for (i = 0; i < digestlen/2; i++) {
284 hashbuf[i] = hashbuf[i] ^ hashbuf[i+digestlen/2];
287 key.data = malloc(keylen);
288 memcpy(key.data, &hashbuf, keylen);
292 /* Prepare our source of random data. */
293 rnd = open("/dev/urandom", O_RDONLY);
295 perror("fopen on /dev/urandom");
302 /* Prepare random IV for cipher */
303 iv.data = malloc(ivlen);
306 ret = read(rnd, iv.data, ivlen);
308 perror("read into iv");
316 /* Initialize the encryption */
317 ret = gnutls_cipher_init(&handle, algo, &key, &iv);
327 /* Fill buf with one block of random data, our password, pad the
328 * rest with zero bytes. */
329 buf = malloc(BUFSIZE + blocklen);
330 memset(buf, 0, BUFSIZE);
332 ret = read(rnd, buf, blocklen);
333 if (ret != blocklen) {
334 perror("read into buffer");
339 gnutls_cipher_deinit(handle);
343 /* We don't need any more random data. */
347 memcpy(buf + blocklen, password, strlen(password));
349 /* Encrypt into encbuf */
350 encbuf = malloc(BUFSIZE + blocklen);
351 memset(encbuf, 0, BUFSIZE + blocklen);
352 ret = gnutls_cipher_encrypt2(handle, buf, BUFSIZE + blocklen,
353 encbuf, BUFSIZE + blocklen);
359 gnutls_cipher_deinit(handle);
364 gnutls_cipher_deinit(handle);
369 /* And finally prepare the resulting string:
370 * "{algorithm}base64encodedciphertext" */
371 base = g_base64_encode(encbuf, BUFSIZE);
373 output = g_strdup_printf("{%s}%s", gnutls_cipher_get_name(algo), base);
379 gchar *password_decrypt_gnutls(const gchar *password,
380 const gchar *decryption_password)
382 gchar **tokens, *tmp;
383 gnutls_cipher_algorithm_t algo;
384 gnutls_digest_algorithm_t digest = GNUTLS_DIG_UNKNOWN;
385 gnutls_cipher_hd_t handle;
386 gnutls_datum_t key, iv;
387 int ivlen, keylen, digestlen, blocklen, ret, i;
389 unsigned char hashbuf[BUFSIZE], *buf;
394 g_return_val_if_fail(password != NULL, NULL);
395 g_return_val_if_fail(decryption_password != NULL, NULL);
397 tokens = g_strsplit_set(password, "{}", 3);
399 /* Parse the string, retrieving algorithm and encrypted data.
400 * We expect "{algorithm}base64encodedciphertext". */
401 if (strlen(tokens[0]) != 0 ||
402 (algo = gnutls_cipher_get_id(tokens[1])) == GNUTLS_CIPHER_UNKNOWN ||
403 strlen(tokens[2]) == 0)
406 /* Our hash algo needs to have digest length twice as long as our
407 * cipher algo's key length. */
408 if (algo == GNUTLS_CIPHER_AES_256_CBC) {
409 debug_print("Using AES-256-CBC + SHA-512 for decryption\n");
410 digest = GNUTLS_DIG_SHA512;
411 } else if (algo == GNUTLS_CIPHER_AES_128_CBC) {
412 debug_print("Using AES-128-CBC + SHA-256 for decryption\n");
413 digest = GNUTLS_DIG_SHA256;
415 if (digest == GNUTLS_DIG_UNKNOWN) {
416 debug_print("Password is encrypted with unsupported cipher, giving up.\n");
421 ivlen = gnutls_cipher_get_iv_size(algo);
422 keylen = gnutls_cipher_get_key_size(algo);
423 blocklen = gnutls_cipher_get_block_size(algo);
424 digestlen = gnutls_hash_get_len(digest);
426 /* Prepare key for cipher - first half of hash of passkey XORed with
427 * the second. AES-256 has key length 32 and length of SHA-512 hash
428 * is exactly twice that, 64. */
429 memset(&hashbuf, 0, BUFSIZE);
430 if ((ret = gnutls_hash_fast(digest, decryption_password,
431 strlen(decryption_password), &hashbuf)) < 0) {
432 debug_print("Hashing passkey failed: %s\n", gnutls_strerror(ret));
436 for (i = 0; i < digestlen/2; i++) {
437 hashbuf[i] = hashbuf[i] ^ hashbuf[i+digestlen/2];
440 key.data = malloc(keylen);
441 memcpy(key.data, &hashbuf, keylen);
445 /* Prepare our source of random data. */
446 rnd = open("/dev/urandom", O_RDONLY);
448 perror("fopen on /dev/urandom");
456 /* Prepare random IV for cipher */
457 iv.data = malloc(ivlen);
460 ret = read(rnd, iv.data, ivlen);
462 perror("read into iv");
470 /* We don't need any more random data. */
474 /* Prepare encrypted password string for decryption. */
475 tmp = g_base64_decode(tokens[2], &len);
478 /* Initialize the decryption */
479 ret = gnutls_cipher_init(&handle, algo, &key, &iv);
481 debug_print("Cipher init failed: %s\n", gnutls_strerror(ret));
487 buf = malloc(BUFSIZE + blocklen);
488 memset(buf, 0, BUFSIZE + blocklen);
489 ret = gnutls_cipher_decrypt2(handle, tmp, len,
490 buf, BUFSIZE + blocklen);
492 debug_print("Decryption failed: %s\n", gnutls_strerror(ret));
496 gnutls_cipher_deinit(handle);
501 gnutls_cipher_deinit(handle);
505 tmp = g_strndup(buf + blocklen, MIN(strlen(buf + blocklen), BUFSIZE));
514 gchar *password_encrypt(const gchar *password,
515 const gchar *encryption_password)
517 if (password == NULL || strlen(password) == 0) {
521 #ifndef PASSWORD_CRYPTO_OLD
522 if (encryption_password == NULL)
523 encryption_password = master_password();
525 return password_encrypt_real(password, encryption_password);
528 return password_encrypt_old(password);
531 gchar *password_decrypt(const gchar *password,
532 const gchar *decryption_password)
534 if (password == NULL || strlen(password) == 0) {
538 /* First, check if the password was possibly decrypted using old,
540 if (*password == '!') {
541 debug_print("Trying to decrypt password using the old method...\n");
542 return password_decrypt_old(password);
545 /* Try available crypto backend */
546 #ifndef PASSWORD_CRYPTO_OLD
547 if (decryption_password == NULL)
548 decryption_password = master_password();
550 if (*password == '{') {
551 debug_print("Trying to decrypt password...\n");
552 return password_decrypt_real(password, decryption_password);
556 /* Fallback, in case the configuration is really old and
557 * stored password in plaintext */
558 debug_print("Assuming password was stored plaintext, returning it unchanged\n");
559 return g_strdup(password);