0
0
mirror of https://github.com/vim/vim.git synced 2025-09-23 03:43:49 -04:00

Crypt the swapfile.

This commit is contained in:
Bram Moolenaar
2010-06-21 06:15:46 +02:00
parent 191e0a2bc7
commit a8ffcbbf5d
23 changed files with 824 additions and 155 deletions

View File

@@ -64,7 +64,7 @@ static void check_marks_read __ARGS((void));
#endif
#ifdef FEAT_CRYPT
static int get_crypt_method __ARGS((char *ptr, int len));
static char_u *check_for_cryptkey __ARGS((char_u *cryptkey, char_u *ptr, long *sizep, off_t *filesizep, int newfile, int *did_ask));
static char_u *check_for_cryptkey __ARGS((char_u *cryptkey, char_u *ptr, long *sizep, off_t *filesizep, int newfile, char_u *fname, int *did_ask));
#endif
#ifdef UNIX
static void set_file_time __ARGS((char_u *fname, time_t atime, time_t mtime));
@@ -995,6 +995,13 @@ retry:
#endif
}
#ifdef FEAT_CRYPT
if (cryptkey != NULL)
/* Need to reset the state, but keep the key, don't want to ask for it
* again. */
crypt_pop_state();
#endif
/*
* When retrying with another "fenc" and the first time "fileformat"
* will be reset.
@@ -1426,7 +1433,8 @@ retry:
*/
if (filesize == 0)
cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
&filesize, newfile, &did_ask_for_key);
&filesize, newfile, sfname,
&did_ask_for_key);
/*
* Decrypt the read bytes.
*/
@@ -2277,8 +2285,14 @@ failed:
save_file_ff(curbuf); /* remember the current file format */
#ifdef FEAT_CRYPT
if (cryptkey != curbuf->b_p_key)
free_crypt_key(cryptkey);
if (cryptkey != NULL)
{
crypt_pop_state();
if (cryptkey != curbuf->b_p_key)
free_crypt_key(cryptkey);
/* don't set cryptkey to NULL, it's used below as a flag that
* encryption was used */
}
#endif
#ifdef FEAT_MBYTE
@@ -2869,12 +2883,13 @@ get_crypt_method(ptr, len)
* Return the (new) encryption key, NULL for no encryption.
*/
static char_u *
check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, did_ask)
check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, fname, did_ask)
char_u *cryptkey; /* previous encryption key or NULL */
char_u *ptr; /* pointer to read bytes */
long *sizep; /* length of read bytes */
off_t *filesizep; /* nr of bytes used from file */
int newfile; /* editing a new buffer */
char_u *fname; /* file name to display */
int *did_ask; /* flag: whether already asked for key */
{
int method = get_crypt_method((char *)ptr, *sizep);
@@ -2882,7 +2897,6 @@ check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, did_ask)
if (method >= 0)
{
curbuf->b_p_cm = method;
use_crypt_method = method;
if (method > 0)
(void)blowfish_self_test();
if (cryptkey == NULL && !*did_ask)
@@ -2895,6 +2909,8 @@ check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, did_ask)
* option and don't free it. bf needs hash of the key saved.
* Don't ask for the key again when first time Enter was hit.
* Happens when retrying to detect encoding. */
smsg((char_u *)_(need_key_msg), fname);
msg_scroll = TRUE;
cryptkey = get_crypt_key(newfile, FALSE);
*did_ask = TRUE;
@@ -2913,6 +2929,8 @@ check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, did_ask)
int seed_len = crypt_seed_len[method];
int salt_len = crypt_salt_len[method];
crypt_push_state();
use_crypt_method = method;
if (method == 0)
crypt_init_keys(cryptkey);
else
@@ -2924,7 +2942,8 @@ check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, did_ask)
/* Remove magic number from the text */
*filesizep += CRYPT_MAGIC_LEN + salt_len + seed_len;
*sizep -= CRYPT_MAGIC_LEN + salt_len + seed_len;
mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + salt_len + seed_len, (size_t)*sizep);
mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + salt_len + seed_len,
(size_t)*sizep);
}
}
/* When starting to edit a new file which does not have encryption, clear
@@ -2956,6 +2975,7 @@ prepare_crypt_read(fp)
if (method < 0 || method != curbuf->b_p_cm)
return FAIL;
crypt_push_state();
if (method == 0)
crypt_init_keys(curbuf->b_p_key);
else
@@ -2974,6 +2994,8 @@ prepare_crypt_read(fp)
/*
* Prepare for writing encrypted bytes for buffer "buf".
* Returns a pointer to an allocated header of length "*lenp".
* When out of memory returns NULL.
* Otherwise calls crypt_push_state(), call crypt_pop_state() later.
*/
char_u *
prepare_crypt_write(buf, lenp)
@@ -2990,6 +3012,7 @@ prepare_crypt_write(buf, lenp)
+ CRYPT_SEED_LEN_MAX + 2);
if (header != NULL)
{
crypt_push_state();
use_crypt_method = buf->b_p_cm; /* select pkzip or blowfish */
vim_strncpy(header, (char_u *)crypt_magic[use_crypt_method],
CRYPT_MAGIC_LEN);
@@ -4404,7 +4427,7 @@ restore_backup:
write_info.bw_fd = fd;
#ifdef FEAT_CRYPT
if (*buf->b_p_key && !filtering)
if (*buf->b_p_key != NUL && !filtering)
{
char_u *header;
int header_len;
@@ -4674,6 +4697,10 @@ restore_backup:
if (!backup_copy)
mch_set_acl(wfname, acl);
#endif
#ifdef FEAT_CRYPT
if (wb_flags & FIO_ENCRYPTED)
crypt_pop_state();
#endif
#if defined(FEAT_MBYTE) && defined(FEAT_EVAL)