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>
36 #elif defined G_OS_WIN32
41 #include "common/passcrypt.h"
42 #include "common/plugin.h"
43 #include "common/pkcs5_pbkdf2.h"
44 #include "common/timing.h"
45 #include "common/utils.h"
47 #include "alertpanel.h"
48 #include "inputdialog.h"
50 #include "passwordstore.h"
51 #include "prefs_common.h"
53 #ifndef PASSWORD_CRYPTO_OLD
54 static gchar *_master_passphrase = NULL;
56 /* Length of stored key derivation, before base64. */
59 /* Length of randomly generated and saved salt, used for key derivation.
60 * Also before base64. */
61 #define KD_SALT_LENGTH 64
63 static void _generate_salt()
65 guchar salt[KD_SALT_LENGTH];
67 if (prefs_common_get_prefs()->master_passphrase_salt != NULL) {
68 g_free(prefs_common_get_prefs()->master_passphrase_salt);
71 if (!get_random_bytes(salt, KD_SALT_LENGTH)) {
72 debug_print("Could not get random bytes for kd salt.\n");
76 prefs_common_get_prefs()->master_passphrase_salt =
77 g_base64_encode(salt, KD_SALT_LENGTH);
82 static guchar *_make_key_deriv(const gchar *passphrase, guint rounds,
86 gchar *saltpref = prefs_common_get_prefs()->master_passphrase_salt;
90 /* Grab our salt, generating and saving a new random one if needed. */
91 if (saltpref == NULL || strlen(saltpref) == 0) {
93 saltpref = prefs_common_get_prefs()->master_passphrase_salt;
95 salt = g_base64_decode(saltpref, &saltlen);
96 kd = g_malloc0(length);
98 START_TIMING("PBKDF2");
99 ret = pkcs5_pbkdf2(passphrase, strlen(passphrase), salt, saltlen,
113 const gchar *master_passphrase()
116 gboolean end = FALSE;
118 if (!prefs_common_get_prefs()->use_master_passphrase) {
119 return PASSCRYPT_KEY;
122 if (_master_passphrase != NULL) {
123 debug_print("Master passphrase is in memory, offering it.\n");
124 return _master_passphrase;
128 input = input_dialog_with_invisible(_("Input master passphrase"),
129 _("Input master passphrase"), NULL);
132 debug_print("Cancel pressed at master passphrase dialog.\n");
136 if (master_passphrase_is_correct(input)) {
137 debug_print("Entered master passphrase seems to be correct, remembering it.\n");
138 _master_passphrase = input;
141 alertpanel_error(_("Incorrect master passphrase."));
145 return _master_passphrase;
148 gboolean master_passphrase_is_set()
150 if (prefs_common_get_prefs()->master_passphrase == NULL
151 || strlen(prefs_common_get_prefs()->master_passphrase) == 0)
157 gboolean master_passphrase_is_correct(const gchar *input)
159 guchar *kd, *input_kd;
161 gchar *stored_kd = prefs_common_get_prefs()->master_passphrase;
166 g_return_val_if_fail(stored_kd != NULL && strlen(stored_kd) > 0, FALSE);
167 g_return_val_if_fail(input != NULL, FALSE);
169 if (stored_kd == NULL)
172 tokens = g_strsplit_set(stored_kd, "{}", 3);
173 if (tokens[0] == NULL ||
174 strlen(tokens[0]) != 0 || /* nothing before { */
176 strncmp(tokens[1], "PBKDF2-HMAC-SHA1,", 17) || /* correct tag */
177 strlen(tokens[1]) <= 17 || /* something after , */
178 (rounds = atoi(tokens[1] + 17)) <= 0 || /* valid rounds # */
180 strlen(tokens[2]) == 0) { /* string continues after } */
181 debug_print("Mangled master_passphrase format in config, can not use it.\n");
186 stored_kd = tokens[2];
187 kd = g_base64_decode(stored_kd, &kd_len); /* should be 64 */
190 if (kd_len != KD_LENGTH) {
191 debug_print("master_passphrase is %ld bytes long, should be %d.\n",
197 input_kd = _make_key_deriv(input, rounds, KD_LENGTH);
198 ret = memcmp(kd, input_kd, kd_len);
209 gboolean master_passphrase_is_entered()
211 return (_master_passphrase == NULL) ? FALSE : TRUE;
214 void master_passphrase_forget()
216 /* If master passphrase is currently in memory (entered by user),
217 * get rid of it. User will have to enter the new one again. */
218 if (_master_passphrase != NULL) {
219 memset(_master_passphrase, 0, strlen(_master_passphrase));
220 g_free(_master_passphrase);
221 _master_passphrase = NULL;
225 void master_passphrase_change(const gchar *oldp, const gchar *newp)
229 guint rounds = prefs_common_get_prefs()->master_passphrase_pbkdf2_rounds;
231 g_return_if_fail(rounds > 0);
234 /* If oldp is NULL, make sure the user has to enter the
235 * current master passphrase before being able to change it. */
236 master_passphrase_forget();
237 oldp = master_passphrase();
239 g_return_if_fail(oldp != NULL);
241 /* Update master passphrase hash in prefs */
242 if (prefs_common_get_prefs()->master_passphrase != NULL)
243 g_free(prefs_common_get_prefs()->master_passphrase);
246 debug_print("Storing key derivation of new master passphrase\n");
247 kd = _make_key_deriv(newp, rounds, KD_LENGTH);
248 base64_kd = g_base64_encode(kd, 64);
249 prefs_common_get_prefs()->master_passphrase =
250 g_strdup_printf("{PBKDF2-HMAC-SHA1,%d}%s", rounds, base64_kd);
254 debug_print("Setting master_passphrase to NULL\n");
255 prefs_common_get_prefs()->master_passphrase = NULL;
258 /* Now go over all accounts, reencrypting their passwords using
259 * the new master passphrase. */
262 oldp = PASSCRYPT_KEY;
264 newp = PASSCRYPT_KEY;
266 debug_print("Reencrypting all account passwords...\n");
267 passwd_store_reencrypt_all(oldp, newp);
269 master_passphrase_forget();
273 gchar *password_encrypt_old(const gchar *password)
275 if (!password || strlen(password) == 0) {
279 gchar *encrypted = g_strdup(password);
280 gchar *encoded, *result;
281 gsize len = strlen(password);
283 passcrypt_encrypt(encrypted, len);
284 encoded = g_base64_encode(encrypted, len);
286 result = g_strconcat("!", encoded, NULL);
292 gchar *password_decrypt_old(const gchar *password)
294 if (!password || strlen(password) == 0) {
298 if (*password != '!' || strlen(password) < 2) {
303 gchar *decrypted = g_base64_decode(password + 1, &len);
305 passcrypt_decrypt(decrypted, len);
309 #ifdef PASSWORD_CRYPTO_GNUTLS
312 /* Since we can't count on having GnuTLS new enough to have
313 * gnutls_cipher_get_iv_size(), we hardcode the IV length for now. */
316 gchar *password_encrypt_gnutls(const gchar *password,
317 const gchar *encryption_passphrase)
319 gnutls_cipher_algorithm_t algo = GNUTLS_CIPHER_AES_256_CBC;
320 gnutls_cipher_hd_t handle;
321 gnutls_datum_t key, iv;
322 int keylen, blocklen, ret;
323 unsigned char *buf, *encbuf, *base, *output;
324 guint rounds = prefs_common_get_prefs()->master_passphrase_pbkdf2_rounds;
326 g_return_val_if_fail(password != NULL, NULL);
327 g_return_val_if_fail(encryption_passphrase != NULL, NULL);
329 /* ivlen = gnutls_cipher_get_iv_size(algo);*/
330 keylen = gnutls_cipher_get_key_size(algo);
331 blocklen = gnutls_cipher_get_block_size(algo);
332 /* digestlen = gnutls_hash_get_len(digest); */
334 /* Take the passphrase and compute a key derivation of suitable
335 * length to be used as encryption key for our block cipher. */
336 key.data = _make_key_deriv(encryption_passphrase, rounds, keylen);
339 /* Prepare random IV for cipher */
340 iv.data = malloc(IVLEN);
342 if (!get_random_bytes(iv.data, IVLEN)) {
348 /* Initialize the encryption */
349 ret = gnutls_cipher_init(&handle, algo, &key, &iv);
356 /* Fill buf with one block of random data, our password, pad the
357 * rest with zero bytes. */
358 buf = malloc(BUFSIZE + blocklen);
359 memset(buf, 0, BUFSIZE + blocklen);
360 if (!get_random_bytes(buf, blocklen)) {
364 gnutls_cipher_deinit(handle);
368 memcpy(buf + blocklen, password, strlen(password));
370 /* Encrypt into encbuf */
371 encbuf = malloc(BUFSIZE + blocklen);
372 memset(encbuf, 0, BUFSIZE + blocklen);
373 ret = gnutls_cipher_encrypt2(handle, buf, BUFSIZE + blocklen,
374 encbuf, BUFSIZE + blocklen);
380 gnutls_cipher_deinit(handle);
385 gnutls_cipher_deinit(handle);
390 /* And finally prepare the resulting string:
391 * "{algorithm,rounds}base64encodedciphertext" */
392 base = g_base64_encode(encbuf, BUFSIZE + blocklen);
394 output = g_strdup_printf("{%s,%d}%s",
395 gnutls_cipher_get_name(algo), rounds, base);
401 gchar *password_decrypt_gnutls(const gchar *password,
402 const gchar *decryption_passphrase)
404 gchar **tokens, *tmp;
405 gnutls_cipher_algorithm_t algo;
406 gnutls_cipher_hd_t handle;
407 gnutls_datum_t key, iv;
408 int keylen, blocklen, ret;
414 g_return_val_if_fail(password != NULL, NULL);
415 g_return_val_if_fail(decryption_passphrase != NULL, NULL);
417 tokens = g_strsplit_set(password, "{}", 3);
419 /* Parse the string, retrieving algorithm and encrypted data.
420 * We expect "{algorithm,rounds}base64encodedciphertext". */
421 if (tokens[0] == NULL || strlen(tokens[0]) != 0 ||
422 tokens[1] == NULL || strlen(tokens[1]) == 0 ||
423 tokens[2] == NULL || strlen(tokens[2]) == 0) {
424 debug_print("Garbled password string.\n");
429 commapos = strcspn(tokens[1], ",");
430 if (commapos == strlen(tokens[1]) || commapos == 0) {
431 debug_print("Garbled algorithm substring.\n");
436 buf = g_strndup(tokens[1], commapos);
437 if ((algo = gnutls_cipher_get_id(buf)) == GNUTLS_CIPHER_UNKNOWN) {
438 debug_print("Password string has unknown algorithm: '%s'\n", buf);
445 if ((rounds = atoi(tokens[1] + commapos + 1)) <= 0) {
446 debug_print("Invalid number of rounds: %d\n", rounds);
451 /* ivlen = gnutls_cipher_get_iv_size(algo); */
452 keylen = gnutls_cipher_get_key_size(algo);
453 blocklen = gnutls_cipher_get_block_size(algo);
454 /* digestlen = gnutls_hash_get_len(digest); */
456 /* Take the passphrase and compute a key derivation of suitable
457 * length to be used as encryption key for our block cipher. */
458 key.data = _make_key_deriv(decryption_passphrase, rounds, keylen);
461 /* Prepare random IV for cipher */
462 iv.data = malloc(IVLEN);
464 if (!get_random_bytes(iv.data, IVLEN)) {
471 /* Prepare encrypted password string for decryption. */
472 tmp = g_base64_decode(tokens[2], &len);
474 if (tmp == NULL || len == 0) {
475 debug_print("Failed base64-decoding of stored password string\n");
482 debug_print("Encrypted password string length: %lu\n", len);
484 /* Initialize the decryption */
485 ret = gnutls_cipher_init(&handle, algo, &key, &iv);
487 debug_print("Cipher init failed: %s\n", gnutls_strerror(ret));
494 /* Allocate the buffer to store decrypted plaintext in. */
499 ret = gnutls_cipher_decrypt2(handle, tmp, len,
503 debug_print("Decryption failed: %s\n", gnutls_strerror(ret));
507 gnutls_cipher_deinit(handle);
512 gnutls_cipher_deinit(handle);
516 /* 'buf+blocklen' should now be pointing to the plaintext
517 * password string. The first block contains random data from the IV. */
518 tmp = g_strndup(buf + blocklen, MIN(strlen(buf + blocklen), BUFSIZE));
529 gchar *password_encrypt(const gchar *password,
530 const gchar *encryption_passphrase)
532 if (password == NULL || strlen(password) == 0) {
536 #ifndef PASSWORD_CRYPTO_OLD
537 if (encryption_passphrase == NULL)
538 encryption_passphrase = master_passphrase();
540 return password_encrypt_real(password, encryption_passphrase);
542 return password_encrypt_old(password);
546 gchar *password_decrypt(const gchar *password,
547 const gchar *decryption_passphrase)
549 if (password == NULL || strlen(password) == 0) {
553 /* First, check if the password was possibly decrypted using old,
555 if (*password == '!') {
556 debug_print("Trying to decrypt password using the old method...\n");
557 return password_decrypt_old(password);
560 /* Try available crypto backend */
561 #ifndef PASSWORD_CRYPTO_OLD
562 if (decryption_passphrase == NULL)
563 decryption_passphrase = master_passphrase();
565 if (*password == '{') {
566 debug_print("Trying to decrypt password...\n");
567 return password_decrypt_real(password, decryption_passphrase);
571 /* Fallback, in case the configuration is really old and
572 * stored password in plaintext */
573 debug_print("Assuming password was stored plaintext, returning it unchanged\n");
574 return g_strdup(password);