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