comparison src/fileio.c @ 2239:732cb7b31956 vim73

Crypt the text in the undo file if the file itself is crypted.
author Bram Moolenaar <bram@vim.org>
date Sun, 30 May 2010 22:48:02 +0200
parents 3d0a7beb0d75
children 60da25e3aab7
comparison
equal deleted inserted replaced
2238:3d0a7beb0d75 2239:732cb7b31956
2825 * the ' parameter after opening a buffer. */ 2825 * the ' parameter after opening a buffer. */
2826 curbuf->b_marks_read = TRUE; 2826 curbuf->b_marks_read = TRUE;
2827 } 2827 }
2828 #endif 2828 #endif
2829 2829
2830 #ifdef FEAT_CRYPT 2830 #if defined(FEAT_CRYPT) || defined(PROTO)
2831 /* 2831 /*
2832 * Get the crypt method used for a file from "ptr[len]", the magic text at the 2832 * Get the crypt method used for a file from "ptr[len]", the magic text at the
2833 * start of the file. 2833 * start of the file.
2834 * Returns -1 when no encryption used. 2834 * Returns -1 when no encryption used.
2835 */ 2835 */
2924 else if (newfile && *curbuf->b_p_key && !starting) 2924 else if (newfile && *curbuf->b_p_key && !starting)
2925 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); 2925 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2926 2926
2927 return cryptkey; 2927 return cryptkey;
2928 } 2928 }
2929 #endif 2929
2930 /*
2931 * Check for magic number used for encryption. Applies to the current buffer.
2932 * If found and decryption is possible returns OK;
2933 */
2934 int
2935 prepare_crypt_read(fp)
2936 FILE *fp;
2937 {
2938 int method;
2939 char_u buffer[CRYPT_MAGIC_LEN + CRYPT_SEED_LEN_MAX + 2];
2940
2941 if (fread(buffer, CRYPT_MAGIC_LEN, 1, fp) != 1)
2942 return FAIL;
2943 method = get_crypt_method((char *)buffer,
2944 CRYPT_MAGIC_LEN + CRYPT_SEED_LEN_MAX);
2945 if (method < 0 || method != curbuf->b_p_cm)
2946 return FAIL;
2947
2948 if (method == 0)
2949 crypt_init_keys(curbuf->b_p_key);
2950 else
2951 {
2952 int seed_len = crypt_seed_len[method];
2953
2954 if (fread(buffer, seed_len, 1, fp) != 1)
2955 return FAIL;
2956 bf_key_init(curbuf->b_p_key);
2957 bf_ofb_init(buffer, seed_len);
2958 }
2959 return OK;
2960 }
2961
2962 /*
2963 * Prepare for writing encrypted bytes for buffer "buf".
2964 * Returns a pointer to an allocated header of length "*lenp".
2965 */
2966 char_u *
2967 prepare_crypt_write(buf, lenp)
2968 buf_T *buf;
2969 int *lenp;
2970 {
2971 char_u *header;
2972 int seed_len = crypt_seed_len[buf->b_p_cm];
2973
2974 header = alloc_clear(CRYPT_MAGIC_LEN + CRYPT_SEED_LEN_MAX + 2);
2975 if (header != NULL)
2976 {
2977 use_crypt_method = buf->b_p_cm; /* select pkzip or blowfish */
2978 vim_strncpy(header, (char_u *)crypt_magic[use_crypt_method],
2979 CRYPT_MAGIC_LEN);
2980 if (buf->b_p_cm == 0)
2981 crypt_init_keys(buf->b_p_key);
2982 else
2983 {
2984 /* Using blowfish, add seed. */
2985 sha2_seed(header + CRYPT_MAGIC_LEN, seed_len); /* create iv */
2986 bf_ofb_init(header + CRYPT_MAGIC_LEN, seed_len);
2987 bf_key_init(buf->b_p_key);
2988 }
2989 }
2990 *lenp = CRYPT_MAGIC_LEN + seed_len;
2991 return header;
2992 }
2993
2994 /*
2995 * Like fwrite() but crypt the bytes when 'key' is set.
2996 * Returns 1 if successful.
2997 */
2998 size_t
2999 fwrite_crypt(buf, ptr, len, fp)
3000 buf_T *buf;
3001 char_u *ptr;
3002 size_t len;
3003 FILE *fp;
3004 {
3005 char_u *copy;
3006 char_u small_buf[100];
3007 int ztemp, t;
3008 size_t i;
3009
3010 if (*buf->b_p_key == NUL)
3011 return fwrite(ptr, len, (size_t)1, fp);
3012 if (len < 100)
3013 copy = small_buf; /* no malloc()/free() for short strings */
3014 else
3015 {
3016 copy = lalloc(len, FALSE);
3017 if (copy == NULL)
3018 return 0;
3019 }
3020 for (i = 0; i < len; ++i)
3021 {
3022 ztemp = ptr[i];
3023 copy[i] = ZENCODE(ztemp, t);
3024 }
3025 i = fwrite(copy, len, (size_t)1, fp);
3026 if (copy != small_buf)
3027 vim_free(copy);
3028 return i;
3029 }
3030
3031 /*
3032 * Read a string of length "len" from "fd".
3033 * When 'key' is set decrypt the bytes.
3034 */
3035 char_u *
3036 read_string_decrypt(buf, fd, len)
3037 buf_T *buf;
3038 FILE *fd;
3039 int len;
3040 {
3041 char_u *ptr;
3042 char_u *p;
3043
3044 ptr = read_string(fd, len);
3045 if (ptr != NULL || *buf->b_p_key != NUL)
3046 for (p = ptr; p < ptr + len; ++p)
3047 ZDECODE(*p);
3048 return ptr;
3049 }
3050
3051 #endif /* FEAT_CRYPT */
2930 3052
2931 #ifdef UNIX 3053 #ifdef UNIX
2932 static void 3054 static void
2933 set_file_time(fname, atime, mtime) 3055 set_file_time(fname, atime, mtime)
2934 char_u *fname; 3056 char_u *fname;
4321 write_info.bw_fd = fd; 4443 write_info.bw_fd = fd;
4322 4444
4323 #ifdef FEAT_CRYPT 4445 #ifdef FEAT_CRYPT
4324 if (*buf->b_p_key && !filtering) 4446 if (*buf->b_p_key && !filtering)
4325 { 4447 {
4326 char_u header[CRYPT_MAGIC_LEN + CRYPT_SEED_LEN_MAX + 2]; 4448 char_u *header;
4327 int seed_len = crypt_seed_len[buf->b_p_cm]; 4449 int header_len;
4328 4450
4329 use_crypt_method = buf->b_p_cm; /* select pkzip or blowfish */ 4451 header = prepare_crypt_write(buf, &header_len);
4330 4452 if (header == NULL)
4331 vim_memset(header, 0, sizeof(header)); 4453 end = 0;
4332 vim_strncpy(header, (char_u *)crypt_magic[use_crypt_method],
4333 CRYPT_MAGIC_LEN);
4334
4335 if (buf->b_p_cm == 0)
4336 crypt_init_keys(buf->b_p_key);
4337 else 4454 else
4338 { 4455 {
4339 /* Using blowfish, add seed. */ 4456 /* Write magic number, so that Vim knows that this file is
4340 sha2_seed(header + CRYPT_MAGIC_LEN, seed_len); /* create iv */ 4457 * encrypted when reading it again. This also undergoes utf-8 to
4341 bf_ofb_init(header + CRYPT_MAGIC_LEN, seed_len); 4458 * ucs-2/4 conversion when needed. */
4342 bf_key_init(buf->b_p_key); 4459 write_info.bw_buf = header;
4343 } 4460 write_info.bw_len = header_len;
4344 4461 write_info.bw_flags = FIO_NOCONVERT;
4345 /* Write magic number, so that Vim knows that this file is 4462 if (buf_write_bytes(&write_info) == FAIL)
4346 * encrypted when reading it again. This also undergoes utf-8 to 4463 end = 0;
4347 * ucs-2/4 conversion when needed. */ 4464 wb_flags |= FIO_ENCRYPTED;
4348 write_info.bw_buf = (char_u *)header; 4465 vim_free(header);
4349 write_info.bw_len = CRYPT_MAGIC_LEN + seed_len; 4466 }
4350 write_info.bw_flags = FIO_NOCONVERT;
4351 if (buf_write_bytes(&write_info) == FAIL)
4352 end = 0;
4353 wb_flags |= FIO_ENCRYPTED;
4354 } 4467 }
4355 #endif 4468 #endif
4356 4469
4357 write_info.bw_buf = buffer; 4470 write_info.bw_buf = buffer;
4358 nchars = 0; 4471 nchars = 0;
5556 { 5669 {
5557 int ztemp, t, i; 5670 int ztemp, t, i;
5558 5671
5559 for (i = 0; i < len; i++) 5672 for (i = 0; i < len; i++)
5560 { 5673 {
5561 ztemp = buf[i]; 5674 ztemp = buf[i];
5562 buf[i] = ZENCODE(ztemp, t); 5675 buf[i] = ZENCODE(ztemp, t);
5563 } 5676 }
5564 } 5677 }
5565 #endif 5678 #endif
5566 5679