create and send Ek(file || H(file)) in one passage - c

I want to send through a socket a message encrypted in symmetric way with his hash.
If possible i want to do all with only one read on the file to be more efficient and sending data when they are ready.
I make this but i can't figure out why doesn't work:
void symmetric_encrypt_send(SSL* connection, char* file_name, unsigned char* key, int key_len){
//generic
int ret;
unsigned char buffer[DIM_BUFFER];
FILE* fd;
int readed_byte;
//encrypt
EVP_CIPHER_CTX* ctx;
unsigned char ciphertex[DIM_BUFFER];
int outlen = 0;
//hash
EVP_MD_CTX sha_ctx;
unsigned int digest_size = 32;
unsigned char hash[digest_size];
//open file
fd = fopen(file_name, "r");
if(fd == NULL){
printf("Impossible to open %s file\n", file_name);
exit(EXIT_FAILURE);
}
//encrypt initialization
ctx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
EVP_CIPHER_CTX_init(ctx);
EVP_EncryptInit(ctx, SYM_CIPHER, key, NULL);
//hash initialization
EVP_MD_CTX_init(&sha_ctx);
EVP_DigestInit(&sha_ctx, EVP_sha256());
//send dimension
struct stat st;
stat(file_name, &st); // file size = st.st_size
int tmp = ((st.st_size + digest_size)/ EVP_CIPHER_block_size(SYM_CIPHER)) + 1;
ret = secure_write(0, &tmp, sizeof(int), connection);
check_ret(ret, sizeof(int));
//read until end of file
//for each read update encrypt and hash
while( 1 ){
readed_byte = fread(buffer, sizeof(char), DIM_BUFFER, fd);
if(readed_byte == -1){
printf("Error");
exit(EXIT_FAILURE);
}
if(readed_byte == 0){ //nel caso in cui la dimensione del file รจ multipla di dim_buffer
break;
}
//Encrypt Update
EVP_EncryptUpdate(ctx, ciphertex, &outlen, buffer, readed_byte);
//hash update
EVP_DigestUpdate(&sha_ctx, buffer, readed_byte);
//send encrypted data at this step
printf("testo inviato: %.*s\n", outlen, ciphertex);
ret = secure_write(0, ciphertex, outlen, connection);
check_ret(ret, outlen);
if(readed_byte != DIM_BUFFER){
break;
}
}
//hashfinal; also clear ctx
EVP_DigestFinal(&sha_ctx, hash, &digest_size);
//encrypt update the hash
EVP_EncryptUpdate(ctx, ciphertex, &outlen, hash, digest_size);
ret = secure_write(0, ciphertex, outlen, connection);
check_ret(ret, outlen);
//EVP_EncryptFinal(); also close ctx so no need for EVP_CIPHER_CTX_cleanup(ctx);
ret = EVP_EncryptFinal(ctx, ciphertex, &outlen);
if(ret == 0)
exit(EXIT_FAILURE);
ret = secure_write(0, ciphertex, outlen, connection);
check_ret(ret, outlen);
//close
free(ctx);
fclose(fd);
}
where
#define DIM_BUFFER 1024
int secure_read(int fd, void* buffer, int count, SSL* sock);
int secure_write(int fd, void* buffer, int count, SSL* sock);

Related

download a file using C Socket Programming

I need to transfer .txt file using sockets. I try this:
Server:
#define CHUNKSIZE 1024
FILE* fd = fopen("download.txt", "rb");
size_t rret, wret;
int bytes_read;
char buffer[CHUNKSIZE];
while (!feof(fd)) {
if ((bytes_read = fread(&buffer, 1, sizeof(buffer), fd)) > 0)
send(Connections[index], buffer, bytes_read, 0);
else
break;
}
fclose(fd);
Client:
char text[1024];
size_t datasize;
FILE* fd = fopen("download.txt", "wb");
while (true)
{
datasize = recv(Connection, (char*)&text, sizeof(text), NULL);
fwrite(&text, 1, datasize, fd);
}
fclose(fd);
But the client does not write anything to download.txt
What could be problem? (I am not pasting the full code because sending messages (recv, send) works well)

C Programming Sending File over Socket issues

I'm trying to setup a client and a server program where the client can download a file from the server.
I have got the client to download a .txt file however, the .txt file is only created, none of the data within it is transferred.
I've figured out the error is either from when the server sends the size of the file to the client OR the client is failing to recognize where the EOF is.
This is my server side code:
int send_all(int clientSocket, const void *buffer, int len) {
const char *pbuf = (const char *) buffer;
while(len > 0) {
int sent = send(clientSocket, pbuf, len, 0);
printf("%d\n", sent);
if(sent < 1) {
printf("%s\n", "Can't write to socket");
return -1;
}
pbuf += sent;
len -= sent;
}
return(0);
}
void SendFileToClient(int clientSocket, char* fileName) {
//int offset;
char buffer[0x1000];
char temp[512] = "/root/Documents/";
const char* filename = fileName;
struct stat s;
strcat(temp, filename);
printf("%s\n", temp);
//append the filenameonto the directory
if(stat(temp, &s) == -1) {
printf("%s\n", "Can't get file info");
return;
}
FILE *file = fopen(temp, "rb");
if(!file) {
printf("%s\n", "Can't open the file for reading");
return;
}
off_t size = s.st_size;
off_t tmp_size = ntohl(size);
if(send_all(clientSocket, &tmp_size, sizeof(tmp_size)) == 0) {
while(size > 0) {
printf("%ld\n", size);
int rval = fread(buffer, 1, min(sizeof(buffer), size), file);
if(rval < 1) {
printf("%s\n", "Cant read from file");
break;
}
if (send_all(clientSocket, buffer, rval) == -1)
break;
size -= rval;
}
}
fclose(file);
}
This is my client side code:
int write_all(FILE *file, const void *buffer, int len) {
const char *pbuf = (const char *) buffer;
while (len > 0) {
int written = fwrite(pbuf, 1, len, file);
if(written < 1) {
printf("%s\n", "Cant write to file");
return -1;
}
pbuf += written;
len -= written;
}
return 0;
}
int read_all(int clientSocket, void *buffer, int len) {
char *pbuf = (char *) buffer;
int total = 0;
while (len > 0) {
int rval = recv(clientSocket, pbuf, len, 0);
if(rval < 0) {
printf("%s\n", "Cant read from socket");
return -1;
}
if(rval == 0) {
printf("%s\n", "Socket disconnected");
return 0;
}
pbuf += rval;
len -= rval;
total += rval;
}
return total;
}
void RecvFile(int clientSocket, const char* filename) {
int rval;
char buffer[0x1000];
//printf("%s\n", filename);
FILE *file = fopen(filename, "wb");
if(!file) {
printf("%s\n", "Cant open file for writing");
return;
}
off_t size = 0;
//this statement is not working
if(read_all(clientSocket, &size, sizeof(size)) == 1) {
size = ntohl(size);
printf("%s%ld\n", "size: ", size);
while(size > 0) {
rval = read_all(clientSocket, buffer, min(sizeof(buffer), size));
printf("%s%d\n", "rval: ", rval);
if(rval < 1)
break;
if(write_all(file, buffer, rval) == -1) {
printf("%s\n", "Cant write to file");
break;
}
}
}
printf("%s\n", "closing file...");
fclose(file);
}
My apologies if the code looks a bit messy - for some reason it didn't paste in with nice formatting.
I believe this is where part of the problem is, but even through various debugging I can't seem to get it to work :(
//this statement is not working
if(read_all(clientSocket, &size, sizeof(size)) == 1) {
Thanks in advance for any help!

C- Socket : Programming a Client/Server-Application to send a file

I want to program an application to send a file with sockets:
Here my Server:
void str_server(int sock)
{
char buf[1025];
const char* filename="test.text";
FILE *file = fopen(filename, "rb");
err_abort("Test");
while (!feof(file))
{
int rval = fread(buf, 1, sizeof(buf), file);
send(sock, buf, rval, 0);
}
}
and here my client:
void RecvFile(int sock, const char* filename)
{
int rval;
char buf[0x1000];
FILE *file = fopen(filename, "wb");
while ((rval = recv(sock, buf, sizeof(buf), 0)) > 0)
{
fwrite(buf, 1, rval, file);
}
close(sock);
}
My problem is that my client create a file....but dont write the content in the file!
Add some error handling to your code, that should help you track down the problem. Also note that send(), recv(), fread() and fwrite() are not guaranteed to write/read the entire buffer you specify, so you should take that into account as well.
Also, since TCP is a byte stream, the server needs to indicate when the file ends so the client knows when to stop reading. If you don't send the file size before sending the actual file, the only option is to close the socket when the transfer is done.
Try something like this:
int send_all(int sock, const void *buf, int len)
{
const char *pbuf = (const char *) buf;
while (len > 0)
{
int sent = send(sock, pbuf, len, 0);
if (sent < 1)
{
// if the socket is non-blocking, then check
// the socket error for WSAEWOULDBLOCK/EAGAIN
// (depending on platform) and if true then
// use select() to wait for a small period of
// time to see if the socket becomes writable
// again before failing the transfer...
printf("Can't write to socket");
return -1;
}
pbuf += sent;
len -= sent;
}
return 0;
}
void str_server(int sock)
{
char buf[0x1000];
const char* filename = "test.text";
struct stat s;
if (stat(filename, &s) == -1)
{
printf("Can't get file info");
return;
}
FILE *file = fopen(filename, "rb");
if (!file)
{
printf("Can't open file for reading");
return;
}
// if you need to handle files > 2GB,
// be sure to use a 64bit integer, and
// a host-to-network function that can
// handle 64bit integers...
long size = s.st_size;
long tmp_size = htonl(size);
if (send_all(sock, &tmp_size, sizeof(tmp_size)) == 0)
{
while (size > 0)
{
int rval = fread(buf, 1, min(sizeof(buf), size), file);
if (rval < 1)
{
printf("Can't read from file");
break;
}
if (send_all(sock, buf, rval) == -1)
break;
size -= rval;
}
}
fclose(file);
}
int write_all(FILE *file, const void *buf, int len)
{
const char *pbuf = (const char *) buf;
while (len > 0)
{
int written = fwrite(pbuf, 1, len, file);
if (written < 1)
{
printf("Can't write to file");
return -1;
}
pbuf += written;
len -= written;
}
return 0;
}
int read_all(int sock, void *buf, int len)
{
char *pbuf = (char *) buf;
int total = 0;
while (len > 0)
{
int rval = recv(sock, pbuf, len, 0);
if (rval < 0)
{
// if the socket is non-blocking, then check
// the socket error for WSAEWOULDBLOCK/EAGAIN
// (depending on platform) and if true then
// use select() to wait for a small period of
// time to see if the socket becomes readable
// again before failing the transfer...
printf("Can't read from socket");
return -1;
}
if (rval == 0)
{
printf("Socket disconnected")
return 0;
}
pbuf += rval;
len -= rval;
total += rval;
}
return total;
}
void RecvFile(int sock, const char* filename)
{
int rval;
char buf[0x1000];
FILE *file = fopen(filename, "wb");
if (!file)
{
printf("Can't open file for writing");
return;
}
// if you need to handle files > 2GB,
// be sure to use a 64bit integer, and
// a network-to-host function that can
// handle 64bit integers...
long size = 0;
if (read_all(sock, &size, sizeof(size)) == 1)
{
size = ntohl(size);
while (size > 0)
{
rval = read_all(sock, buf, min(sizeof(buf), size));
if (rval < 1)
break;
if (write_all(file, buf, rval) == -1)
break;
}
}
fclose(file);
}

Mcrypt library writing result to file

I'm trying to write encrypted data to a file. However, when reading it back into the program and trying to decrypt it, I only get garbage back out. Without writing it to a file it seems to work.. What am I doing wrong?
Here's the code:
MCRYPT td, td2;
char * string = "My secret message";
int i;
char *key; /* created using mcrypt_gen_key */
char *IV;
char * block_buffer;
int blocksize;
int keysize = 32; /* 192 bits == 24 bytes */
key = calloc(1, keysize);
strcpy(key, "This-is-my-key#########");
td = mcrypt_module_open("saferplus", NULL, "cbc", NULL);
td2 = mcrypt_module_open("saferplus", NULL, "cbc", NULL);
blocksize = mcrypt_enc_get_block_size(td);
block_buffer = malloc(blocksize);
IV=malloc(mcrypt_enc_get_iv_size(td));
for (i=0; i < mcrypt_enc_get_iv_size(td); i++) {
IV[i]=rand();
}
mcrypt_generic_init(td, key, keysize, IV);
mcrypt_generic_init(td2, key, keysize, IV);
strcpy(block_buffer, string);
printf("1: %s\n", block_buffer);
mcrypt_generic (td, block_buffer, blocksize);
FILE *myFile;
myFile = fopen("encrypted","ab");
fwrite(block_buffer, 1, blocksize, myFile);
fclose(myFile);
printf("2: %s\n", block_buffer);
myFile = fopen("encrypted","rb");
fread(block_buffer, 1, blocksize, myFile);
fclose(myFile);
printf("2.5: %s\n", block_buffer);
mdecrypt_generic (td2, block_buffer, blocksize);
printf("3: %s\n", block_buffer);
/* deinitialize the encryption thread */
mcrypt_generic_deinit (td);
mcrypt_generic_deinit(td2);
/* Unload the loaded module */
mcrypt_module_close(td);
mcrypt_module_close(td2);
return 0;
This works. I think you just needed to zero initialize block_buffer.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mcrypt.h>
int main()
{
MCRYPT td, td2;
char * string = "My secret message";
int i;
char *key; /* created using mcrypt_gen_key */
char *IV;
char * block_buffer;
int blocksize;
int keysize = 32; /* 192 bits == 24 bytes */
FILE *myFile;
key = calloc(1, keysize);
strcpy(key, "This-is-my-key#########");
td = mcrypt_module_open("saferplus", NULL, "cbc", NULL);
td2 = mcrypt_module_open("saferplus", NULL, "cbc", NULL);
blocksize = mcrypt_enc_get_block_size(td);
block_buffer = calloc(1, blocksize); /* Fixed issue here */
IV = malloc(mcrypt_enc_get_iv_size(td));
if ((block_buffer == NULL) || (IV == NULL)) {
fprintf(stderr, "Failed to allocate memory\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < mcrypt_enc_get_iv_size(td); i++) {
IV[i] = rand();
}
mcrypt_generic_init(td, key, keysize, IV);
mcrypt_generic_init(td2, key, keysize, IV);
strcpy(block_buffer, string);
printf("1: %s\n", block_buffer);
mcrypt_generic (td, block_buffer, blocksize);
myFile = fopen("encrypted","w");
if ((myFile == NULL) || (fwrite(block_buffer, blocksize, 1, myFile) != 1)) {
fprintf(stderr, "Failed to write data\n");
exit(EXIT_FAILURE);
}
fclose(myFile);
printf("2: %s\n", block_buffer);
myFile = fopen("encrypted","r");
if ((myFile == NULL) || (fread(block_buffer, blocksize, 1, myFile) != 1)) {
fprintf(stderr, "Failed to read data\n");
exit(EXIT_FAILURE);
}
fclose(myFile);
printf("2.5: %s\n", block_buffer);
mdecrypt_generic (td2, block_buffer, blocksize);
printf("3: %s\n", block_buffer);
/* deinitialize the encryption thread */
mcrypt_generic_deinit (td);
mcrypt_generic_deinit(td2);
/* Unload the loaded module */
mcrypt_module_close(td);
mcrypt_module_close(td2);
return 0;
}

base64 decoding files of length greater than 8192?

Whenever I try to base64 decode files (using OpenSSL's BIO_f_base64()) larger than 8192, I always seem to get some wrong value.
What is with this magic number 8192? Any help to educate me is greatly appreciated!
UPDATED:
Here is part of my code:
int dgst(char *alg)
{
EVP_MD_CTX ctx;
const EVP_MD *md;
unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len, i;
char *toB64val = NULL;
char *data = NULL;
OpenSSL_add_all_digests();
md = EVP_get_digestbyname(alg);
if(!md) {
printf("Unknown message digest %s\n", alg);
exit(1);
}
data = readFileBuffer("file_out");
printf("strlen(data) %d\n", strlen(data));
EVP_MD_CTX_init(&ctx);
EVP_DigestInit_ex(&ctx, md, NULL);
EVP_DigestUpdate(&ctx, data, strlen(data));
EVP_DigestFinal_ex(&ctx, md_value, &md_len); //retrieve digest from ctx unto md_value and #bytes written is copied into md_len
EVP_MD_CTX_cleanup(&ctx);
unsigned char *copy = malloc(md_len);
memcpy(copy, md_value, md_len);
char *buff = encbase64(copy, md_len);
printf("Digest is:%s\n ", buff);
free(buff);
free(toB64val);
free(data);
return 0;
}
char *readFileBuffer(char *name)
{
FILE *file;
char *buffer = NULL;
unsigned long fileLen;
//Open file
file = fopen(name, "rb");
if (!file)
{
fprintf(stderr, "Unable to open file %s", name);
return;
}
//Get file length
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
printf("file length = %ld\n", fileLen);
fseek(file, 0, SEEK_SET);
//printf("Allocate memory\n");
buffer=(char *)malloc(fileLen+1);
printf("length of write buffer = %d\n", strlen(buffer));
if (!buffer)
{
fprintf(stderr, "Memory error!");
}
long int n = fread(buffer,1, fileLen,file);
buffer[n] = '\0';
printf("Read no. of bytes = %ld into buffer \n", n);
printf("len of buffer %d \n", strlen(buffer));
if (!buffer)
{
fprintf(stderr, "Memory error!");
fclose(file);
}
fclose(file);
return buffer;
}
char *encbase64( char *input, int length)
{
BIO *bmem, *b64;
BUF_MEM *bptr;
b64 = BIO_new(BIO_f_base64());
//BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_write(b64, input, length);
BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
char *buff = (char *)malloc(bptr->length);
memcpy(buff, bptr->data, bptr->length-1);
buff[bptr->length-1] = 0;
BIO_free_all(b64);
return buff;
}
it doesn't seem to have any problems with 8k boundaries. could you show us soucecode how you call it?
update:
int dgst(char *alg)
{
EVP_MD_CTX ctx;
const EVP_MD *md;
unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len, i;
char *toB64val = NULL;
char *data = NULL;
OpenSSL_add_all_digests();
md = EVP_get_digestbyname(alg);
if(!md) {
printf("Unknown message digest %s\n", alg);
exit(1);
}
data = readFileBuffer("file_out");
//printf("strlen(data) %d\n", strlen(data)); <- don't use strlen on binary data
EVP_MD_CTX_init(&ctx);
EVP_DigestInit_ex(&ctx, md, NULL);
EVP_DigestUpdate(&ctx, data, strlen(data));
EVP_DigestFinal_ex(&ctx, md_value, &md_len); //retrieve digest from ctx unto md_value and #bytes written is copied into md_len
EVP_MD_CTX_cleanup(&ctx);
unsigned char *copy = malloc(md_len);
memcpy(copy, md_value, md_len);
char *buff = encbase64(copy, md_len);
printf("Digest is:%s\n ", buff);
free(buff);
free(toB64val);
free(data);
return 0;
}
char *readFileBuffer(char *name)
{
FILE *file;
char *buffer = NULL;
unsigned long fileLen;
//Open file
file = fopen(name, "rb");
if (!file)
{
fprintf(stderr, "Unable to open file %s", name);
return;
}
//Get file length
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
printf("file length = %ld\n", fileLen);
fseek(file, 0, SEEK_SET);
//printf("Allocate memory\n");
buffer=(char *)malloc(fileLen/*+1*/); // <- not a string - no need to +1
//printf("length of write buffer = %d\n", strlen(buffer)); <- again strlen on binary data (you just allocated it, so it contains random bytes)
if (!buffer)
{
fprintf(stderr, "Memory error!");
}
long int n = fread(buffer,1, fileLen,file);
//buffer[n] = '\0'; // not a string - no need to end it
printf("Read no. of bytes = %ld into buffer \n", n);
//printf("len of buffer %d \n", strlen(buffer)); <- buffer length is in 'n'
if (!buffer)
{
fprintf(stderr, "Memory error!");
fclose(file);
}
fclose(file);
return buffer;
}
char *encbase64( char *input, int length)
{
BIO *bmem, *b64;
BUF_MEM *bptr;
b64 = BIO_new(BIO_f_base64());
//BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_write(b64, input, length);
BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
char *buff = (char *)malloc(bptr->length);
memcpy(buff, bptr->data, bptr->length/*-1*/); // removed '+1'
//buff[bptr->length-1] = 0; // not a string
BIO_free_all(b64);
return buff;
}

Resources