Skip to content

Commit

Permalink
dcrypt: Fix bugs in 0 and 1 byte payload files
Browse files Browse the repository at this point in the history
  • Loading branch information
cmouse authored and sirainen committed Jun 30, 2016
1 parent 2272c47 commit 822ecda
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/lib-dcrypt/istream-decrypt.c
Expand Up @@ -60,7 +60,7 @@ ssize_t i_stream_decrypt_read_header_v1(struct decrypt_istream *stream,

const unsigned char *digest_pos = NULL, *key_digest_pos = NULL, *key_ct_pos = NULL;

size_t pos = 7;
size_t pos = 9;
size_t digest_len = 0;
size_t key_ct_len = 0;
size_t key_digest_size = 0;
Expand Down Expand Up @@ -585,7 +585,8 @@ ssize_t i_stream_decrypt_read_header(struct decrypt_istream *stream,
uint32_t hdr_len;
if (!get_msb32(&data, end, &hdr_len))
return 0;
if ((size_t)(end-data) < hdr_len)
/* do not forget stream format */
if ((size_t)(end-data)+1 < hdr_len)
return 0;

int ret;
Expand Down Expand Up @@ -669,6 +670,13 @@ i_stream_decrypt_read(struct istream_private *stream)

if (ret == -1 && (size == 0 || stream->parent->stream_errno != 0)) {
stream->istream.stream_errno = stream->parent->stream_errno;

/* file was empty */
if (!dstream->initialized && size == 0 && stream->parent->eof) {
stream->istream.eof = TRUE;
return -1;
}

if (stream->istream.stream_errno != 0)
return -1;

Expand Down
3 changes: 3 additions & 0 deletions src/lib-dcrypt/ostream-encrypt.c
Expand Up @@ -496,6 +496,9 @@ int o_stream_encrypt_flush(struct ostream_private *stream)
const char *error;
struct encrypt_ostream *estream = (struct encrypt_ostream *)stream;

/* if nothing was written, we are done */
if (!estream->prefix_written) return o_stream_flush(stream->parent);

i_assert(!estream->finalized);
estream->finalized = TRUE;

Expand Down

0 comments on commit 822ecda

Please sign in to comment.