Do not create password block when deleting a password from store.
[claws.git] / src / password.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2016 The Claws Mail Team
4  *
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.
9  *
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.
14  *
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/>.
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #ifdef PASSWORD_CRYPTO_GNUTLS
26 # include <gnutls/gnutls.h>
27 # include <gnutls/crypto.h>
28 #endif
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32
33 #if defined G_OS_UNIX
34 #include <fcntl.h>
35 #include <unistd.h>
36 #elif defined G_OS_WIN32
37 #include <windows.h>
38 #include <wincrypt.h>
39 #endif
40
41 #include "common/passcrypt.h"
42 #include "common/plugin.h"
43 #include "common/utils.h"
44 #include "account.h"
45 #include "alertpanel.h"
46 #include "inputdialog.h"
47 #include "password.h"
48 #include "passwordstore.h"
49 #include "prefs_common.h"
50
51 #ifndef PASSWORD_CRYPTO_OLD
52 static gchar *_master_passphrase = NULL;
53
54 static const gchar *master_passphrase()
55 {
56         gchar *input;
57         gboolean end = FALSE;
58
59         if (!prefs_common_get_prefs()->use_master_passphrase) {
60                 return PASSCRYPT_KEY;
61         }
62
63         if (_master_passphrase != NULL) {
64                 debug_print("Master passphrase is in memory, offering it.\n");
65                 return _master_passphrase;
66         }
67
68         while (!end) {
69                 input = input_dialog_with_invisible(_("Input master passphrase"),
70                                 _("Input master passphrase"), NULL);
71
72                 if (input == NULL) {
73                         debug_print("Cancel pressed at master passphrase dialog.\n");
74                         break;
75                 }
76
77                 if (master_passphrase_is_correct(input)) {
78                         debug_print("Entered master passphrase seems to be correct, remembering it.\n");
79                         _master_passphrase = input;
80                         end = TRUE;
81                 } else {
82                         alertpanel_error(_("Incorrect master passphrase."));
83                 }
84         }
85
86         return _master_passphrase;
87 }
88
89 const gboolean master_passphrase_is_set()
90 {
91         if (prefs_common_get_prefs()->master_passphrase_hash == NULL
92                         || strlen(prefs_common_get_prefs()->master_passphrase_hash) == 0)
93                 return FALSE;
94
95         return TRUE;
96 }
97
98 const gboolean master_passphrase_is_correct(const gchar *input)
99 {
100         gchar *hash;
101         gchar *stored_hash = prefs_common_get_prefs()->master_passphrase_hash;
102         const GChecksumType hashtype = G_CHECKSUM_SHA512;
103         const gssize hashlen = g_checksum_type_get_length(hashtype);
104         gssize stored_len;
105
106         g_return_val_if_fail(input != NULL, FALSE);
107
108         if (stored_hash == NULL)
109                 return FALSE;
110
111         debug_print("|stored_hash|%s|\n", stored_hash);
112         stored_len = strlen(stored_hash);
113         g_return_val_if_fail(stored_len == 2*hashlen, FALSE);
114
115         hash = g_compute_checksum_for_string(hashtype, input, -1);
116
117         if (!strncasecmp(hash, stored_hash, stored_len)) {
118                 g_free(hash);
119                 return TRUE;
120         }
121         g_free(hash);
122
123         return FALSE;
124 }
125
126 gboolean master_passphrase_is_entered()
127 {
128         return (_master_passphrase == NULL) ? FALSE : TRUE;
129 }
130
131 void master_passphrase_forget()
132 {
133         /* If master passphrase is currently in memory (entered by user),
134          * get rid of it. User will have to enter the new one again. */
135         if (_master_passphrase != NULL) {
136                 memset(_master_passphrase, 0, strlen(_master_passphrase));
137                 g_free(_master_passphrase);
138         }
139         _master_passphrase = NULL;
140 }
141
142 void master_passphrase_change(const gchar *oldp, const gchar *newp)
143 {
144         if (oldp == NULL) {
145                 /* If oldp is NULL, make sure the user has to enter the
146                  * current master passphrase before being able to change it. */
147                 master_passphrase_forget();
148                 oldp = master_passphrase();
149         }
150         g_return_if_fail(oldp != NULL);
151
152         /* Update master passphrase hash in prefs */
153         if (prefs_common_get_prefs()->master_passphrase_hash != NULL)
154                 g_free(prefs_common_get_prefs()->master_passphrase_hash);
155
156         if (newp != NULL) {
157                 debug_print("Storing hash of new master passphrase\n");
158                 prefs_common_get_prefs()->master_passphrase_hash =
159                         g_compute_checksum_for_string(G_CHECKSUM_SHA512, newp, -1);
160         } else {
161                 debug_print("Setting master_passphrase_hash to NULL\n");
162                 prefs_common_get_prefs()->master_passphrase_hash = NULL;
163         }
164
165         /* Now go over all accounts, reencrypting their passwords using
166          * the new master passphrase. */
167
168         if (oldp == NULL)
169                 oldp = PASSCRYPT_KEY;
170         if (newp == NULL)
171                 newp = PASSCRYPT_KEY;
172
173         debug_print("Reencrypting all account passwords...\n");
174         passwd_store_reencrypt_all(oldp, newp);
175
176         /* Now reencrypt all plugins passwords fields 
177          * FIXME: Unloaded plugins won't be able to update their stored passwords
178          */
179         plugins_master_passphrase_change(oldp, newp);
180
181         master_passphrase_forget();
182 }
183 #endif
184
185 gchar *password_encrypt_old(const gchar *password)
186 {
187         if (!password || strlen(password) == 0) {
188                 return NULL;
189         }
190
191         gchar *encrypted = g_strdup(password);
192         gchar *encoded, *result;
193         gsize len = strlen(password);
194
195         passcrypt_encrypt(encrypted, len);
196         encoded = g_base64_encode(encrypted, len);
197         g_free(encrypted);
198         result = g_strconcat("!", encoded, NULL);
199         g_free(encoded);
200
201         return result;
202 }
203
204 gchar *password_decrypt_old(const gchar *password)
205 {
206         if (!password || strlen(password) == 0) {
207                 return NULL;
208         }
209
210         if (*password != '!' || strlen(password) < 2) {
211                 return NULL;
212         }
213
214         gsize len;
215         gchar *decrypted = g_base64_decode(password + 1, &len);
216
217         passcrypt_decrypt(decrypted, len);
218         return decrypted;
219 }
220
221 #ifdef PASSWORD_CRYPTO_GNUTLS
222 #define BUFSIZE 128
223
224 gchar *password_encrypt_gnutls(const gchar *password,
225                 const gchar *encryption_passphrase)
226 {
227         /* Another, slightly inferior combination is AES-128-CBC + SHA-256.
228          * Any block cipher in CBC mode with keysize N and a hash algo with
229          * digest length 2*N would do. */
230         gnutls_cipher_algorithm_t algo = GNUTLS_CIPHER_AES_256_CBC;
231         gnutls_digest_algorithm_t digest = GNUTLS_DIG_SHA512;
232         gnutls_cipher_hd_t handle;
233         gnutls_datum_t key, iv;
234         int ivlen, keylen, digestlen, blocklen, ret, i;
235         unsigned char hashbuf[BUFSIZE], *buf, *encbuf, *base, *output;
236 #if defined G_OS_UNIX
237         int rnd;
238 #elif defined G_OS_WIN32
239         HCRYPTPROV rnd;
240 #endif
241
242         g_return_val_if_fail(password != NULL, NULL);
243         g_return_val_if_fail(encryption_passphrase != NULL, NULL);
244
245         ivlen = gnutls_cipher_get_iv_size(algo);
246         keylen = gnutls_cipher_get_key_size(algo);
247         blocklen = gnutls_cipher_get_block_size(algo);
248         digestlen = gnutls_hash_get_len(digest);
249
250         /* Prepare key for cipher - first half of hash of passkey XORed with
251          * the second. */
252         memset(&hashbuf, 0, BUFSIZE);
253         if ((ret = gnutls_hash_fast(digest, encryption_passphrase,
254                                         strlen(encryption_passphrase), &hashbuf)) < 0) {
255                 debug_print("Hashing passkey failed: %s\n", gnutls_strerror(ret));
256                 return NULL;
257         }
258         for (i = 0; i < digestlen/2; i++) {
259                 hashbuf[i] = hashbuf[i] ^ hashbuf[i+digestlen/2];
260         }
261
262         key.data = malloc(keylen);
263         memcpy(key.data, &hashbuf, keylen);
264         key.size = keylen;
265
266         /* Prepare our source of random data. */
267 #if defined G_OS_UNIX
268         rnd = open("/dev/urandom", O_RDONLY);
269         if (rnd == -1) {
270                 perror("fopen on /dev/urandom");
271 #elif defined G_OS_WIN32
272         if (!CryptAcquireContext(&rnd, NULL, NULL, PROV_RSA_FULL, 0) &&
273                         !CryptAcquireContext(&rnd, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
274                 debug_print("Could not acquire a CSP handle.\n");
275 #endif
276                 g_free(key.data);
277                 g_free(iv.data);
278                 return NULL;
279         }
280
281         /* Prepare random IV for cipher */
282         iv.data = malloc(ivlen);
283         iv.size = ivlen;
284 #if defined G_OS_UNIX
285         ret = read(rnd, iv.data, ivlen);
286         if (ret != ivlen) {
287                 perror("read into iv");
288                 close(rnd);
289 #elif defined G_OS_WIN32
290         if (!CryptGenRandom(rnd, ivlen, iv.data)) {
291                 debug_print("Could not read random data for IV\n");
292                 CryptReleaseContext(rnd, 0);
293 #endif
294                 g_free(key.data);
295                 g_free(iv.data);
296                 return NULL;
297         }
298
299         /* Initialize the encryption */
300         ret = gnutls_cipher_init(&handle, algo, &key, &iv);
301         if (ret < 0) {
302                 g_free(key.data);
303                 g_free(iv.data);
304 #if defined G_OS_UNIX
305                 close(rnd);
306 #elif defined G_OS_WIN32
307                 CryptReleaseContext(rnd, 0);
308 #endif
309                 return NULL;
310         }
311
312         /* Fill buf with one block of random data, our password, pad the
313          * rest with zero bytes. */
314         buf = malloc(BUFSIZE + blocklen);
315         memset(buf, 0, BUFSIZE);
316 #if defined G_OS_UNIX
317         ret = read(rnd, buf, blocklen);
318         if (ret != blocklen) {
319                 perror("read into buffer");
320                 close(rnd);
321 #elif defined G_OS_WIN32
322         if (!CryptGenRandom(rnd, blocklen, buf)) {
323                 debug_print("Could not read random data for IV\n");
324                 CryptReleaseContext(rnd, 0);
325 #endif
326                 g_free(buf);
327                 g_free(key.data);
328                 g_free(iv.data);
329                 gnutls_cipher_deinit(handle);
330                 return NULL;
331         }
332
333         /* We don't need any more random data. */
334 #if defined G_OS_UNIX
335         close(rnd);
336 #elif defined G_OS_WIN32
337         CryptReleaseContext(rnd, 0);
338 #endif
339
340         memcpy(buf + blocklen, password, strlen(password));
341
342         /* Encrypt into encbuf */
343         encbuf = malloc(BUFSIZE + blocklen);
344         memset(encbuf, 0, BUFSIZE + blocklen);
345         ret = gnutls_cipher_encrypt2(handle, buf, BUFSIZE + blocklen,
346                         encbuf, BUFSIZE + blocklen);
347         if (ret < 0) {
348                 g_free(key.data);
349                 g_free(iv.data);
350                 g_free(buf);
351                 g_free(encbuf);
352                 gnutls_cipher_deinit(handle);
353                 return NULL;
354         }
355
356         /* Cleanup */
357         gnutls_cipher_deinit(handle);
358         g_free(key.data);
359         g_free(iv.data);
360         g_free(buf);
361
362         /* And finally prepare the resulting string:
363          * "{algorithm}base64encodedciphertext" */
364         base = g_base64_encode(encbuf, BUFSIZE);
365         g_free(encbuf);
366         output = g_strdup_printf("{%s}%s", gnutls_cipher_get_name(algo), base);
367         g_free(base);
368
369         return output;
370 }
371
372 gchar *password_decrypt_gnutls(const gchar *password,
373                 const gchar *decryption_passphrase)
374 {
375         gchar **tokens, *tmp;
376         gnutls_cipher_algorithm_t algo;
377         gnutls_digest_algorithm_t digest = GNUTLS_DIG_UNKNOWN;
378         gnutls_cipher_hd_t handle;
379         gnutls_datum_t key, iv;
380         int ivlen, keylen, digestlen, blocklen, ret, i;
381         gsize len;
382         unsigned char hashbuf[BUFSIZE], *buf;
383 #if defined G_OS_UNIX
384         int rnd;
385 #elif defined G_OS_WIN32
386         HCRYPTPROV rnd;
387 #endif
388
389         g_return_val_if_fail(password != NULL, NULL);
390         g_return_val_if_fail(decryption_passphrase != NULL, NULL);
391
392         tokens = g_strsplit_set(password, "{}", 3);
393
394         /* Parse the string, retrieving algorithm and encrypted data.
395          * We expect "{algorithm}base64encodedciphertext". */
396         if (strlen(tokens[0]) != 0 ||
397                         (algo = gnutls_cipher_get_id(tokens[1])) == GNUTLS_CIPHER_UNKNOWN ||
398                         strlen(tokens[2]) == 0)
399                 return NULL;
400
401         /* Our hash algo needs to have digest length twice as long as our
402          * cipher algo's key length. */
403         if (algo == GNUTLS_CIPHER_AES_256_CBC) {
404                 debug_print("Using AES-256-CBC + SHA-512 for decryption\n");
405                 digest = GNUTLS_DIG_SHA512;
406         } else if (algo == GNUTLS_CIPHER_AES_128_CBC) {
407                 debug_print("Using AES-128-CBC + SHA-256 for decryption\n");
408                 digest = GNUTLS_DIG_SHA256;
409         }
410         if (digest == GNUTLS_DIG_UNKNOWN) {
411                 debug_print("Password is encrypted with unsupported cipher, giving up.\n");
412                 g_strfreev(tokens);
413                 return NULL;
414         }
415
416         ivlen = gnutls_cipher_get_iv_size(algo);
417         keylen = gnutls_cipher_get_key_size(algo);
418         blocklen = gnutls_cipher_get_block_size(algo);
419         digestlen = gnutls_hash_get_len(digest);
420
421         /* Prepare key for cipher - first half of hash of passkey XORed with
422          * the second. AES-256 has key length 32 and length of SHA-512 hash
423          * is exactly twice that, 64. */
424         memset(&hashbuf, 0, BUFSIZE);
425         if ((ret = gnutls_hash_fast(digest, decryption_passphrase,
426                                         strlen(decryption_passphrase), &hashbuf)) < 0) {
427                 debug_print("Hashing passkey failed: %s\n", gnutls_strerror(ret));
428                 g_strfreev(tokens);
429                 return NULL;
430         }
431         for (i = 0; i < digestlen/2; i++) {
432                 hashbuf[i] = hashbuf[i] ^ hashbuf[i+digestlen/2];
433         }
434
435         key.data = malloc(keylen);
436         memcpy(key.data, &hashbuf, keylen);
437         key.size = keylen;
438
439         /* Prepare our source of random data. */
440 #if defined G_OS_UNIX
441         rnd = open("/dev/urandom", O_RDONLY);
442         if (rnd == -1) {
443                 perror("fopen on /dev/urandom");
444 #elif defined G_OS_WIN32
445         if (!CryptAcquireContext(&rnd, NULL, NULL, PROV_RSA_FULL, 0) &&
446                         !CryptAcquireContext(&rnd, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
447                 debug_print("Could not acquire a CSP handle.\n");
448 #endif
449                 g_free(key.data);
450                 g_free(iv.data);
451                 g_strfreev(tokens);
452                 return NULL;
453         }
454
455         /* Prepare random IV for cipher */
456         iv.data = malloc(ivlen);
457         iv.size = ivlen;
458 #if defined G_OS_UNIX
459         ret = read(rnd, iv.data, ivlen);
460         if (ret != ivlen) {
461                 perror("read into iv");
462                 close(rnd);
463 #elif defined G_OS_WIN32
464         if (!CryptGenRandom(rnd, ivlen, iv.data)) {
465                 debug_print("Could not read random data for IV\n");
466                 CryptReleaseContext(rnd, 0);
467 #endif
468                 g_free(key.data);
469                 g_free(iv.data);
470                 g_strfreev(tokens);
471                 return NULL;
472         }
473
474         /* We don't need any more random data. */
475 #if defined G_OS_UNIX
476         close(rnd);
477 #elif defined G_OS_WIN32
478         CryptReleaseContext(rnd, 0);
479 #endif
480
481         /* Prepare encrypted password string for decryption. */
482         tmp = g_base64_decode(tokens[2], &len);
483         g_strfreev(tokens);
484
485         /* Initialize the decryption */
486         ret = gnutls_cipher_init(&handle, algo, &key, &iv);
487         if (ret < 0) {
488                 debug_print("Cipher init failed: %s\n", gnutls_strerror(ret));
489                 g_free(key.data);
490                 g_free(iv.data);
491                 return NULL;
492         }
493
494         buf = malloc(BUFSIZE + blocklen);
495         memset(buf, 0, BUFSIZE + blocklen);
496         ret = gnutls_cipher_decrypt2(handle, tmp, len,
497                         buf, BUFSIZE + blocklen);
498         if (ret < 0) {
499                 debug_print("Decryption failed: %s\n", gnutls_strerror(ret));
500                 g_free(key.data);
501                 g_free(iv.data);
502                 g_free(buf);
503                 gnutls_cipher_deinit(handle);
504                 return NULL;
505         }
506
507         /* Cleanup */
508         gnutls_cipher_deinit(handle);
509         g_free(key.data);
510         g_free(iv.data);
511
512         tmp = g_strndup(buf + blocklen, MIN(strlen(buf + blocklen), BUFSIZE));
513         g_free(buf);
514         return tmp;
515 }
516
517 #undef BUFSIZE
518
519 #endif
520
521 gchar *password_encrypt(const gchar *password,
522                 const gchar *encryption_passphrase)
523 {
524         if (password == NULL || strlen(password) == 0) {
525                 return NULL;
526         }
527
528 #ifndef PASSWORD_CRYPTO_OLD
529         if (encryption_passphrase == NULL)
530                 encryption_passphrase = master_passphrase();
531
532         return password_encrypt_real(password, encryption_passphrase);
533 #endif
534
535         return password_encrypt_old(password);
536 }
537
538 gchar *password_decrypt(const gchar *password,
539                 const gchar *decryption_passphrase)
540 {
541         if (password == NULL || strlen(password) == 0) {
542                 return NULL;
543         }
544
545         /* First, check if the password was possibly decrypted using old,
546          * obsolete method */
547         if (*password == '!') {
548                 debug_print("Trying to decrypt password using the old method...\n");
549                 return password_decrypt_old(password);
550         }
551
552         /* Try available crypto backend */
553 #ifndef PASSWORD_CRYPTO_OLD
554         if (decryption_passphrase == NULL)
555                 decryption_passphrase = master_passphrase();
556
557         if (*password == '{') {
558                 debug_print("Trying to decrypt password...\n");
559                 return password_decrypt_real(password, decryption_passphrase);
560         }
561 #endif
562
563         /* Fallback, in case the configuration is really old and
564          * stored password in plaintext */
565         debug_print("Assuming password was stored plaintext, returning it unchanged\n");
566         return g_strdup(password);
567 }