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