Use OpenSSL EVP api for hashing

This commit is contained in:
2022-12-09 14:27:06 +01:00
parent 7f1299feb4
commit 933aac0f09

View File

@ -17,8 +17,7 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <signal.h> #include <signal.h>
#include <openssl/sha.h> #include <openssl/evp.h>
#include <malloc.h>
int cache_continue = 1; int cache_continue = 1;
@ -81,14 +80,12 @@ int cache_process(void) {
} }
FILE *file; FILE *file;
char *buf = malloc(CACHE_BUF_SIZE); char buf[CACHE_BUF_SIZE], comp_buf[CACHE_BUF_SIZE], filename_comp_gz[256], filename_comp_br[256];
char *comp_buf = malloc(CACHE_BUF_SIZE);
char filename_comp_gz[256];
char filename_comp_br[256];
unsigned long read; unsigned long read;
int compress; int compress;
SHA_CTX ctx; EVP_MD_CTX *ctx;
unsigned char hash[SHA_DIGEST_LENGTH]; unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int md_len;
int cache_changed = 0; int cache_changed = 0;
int p_len_gz, p_len_br; int p_len_gz, p_len_br;
int ret; int ret;
@ -97,7 +94,9 @@ int cache_process(void) {
if (cache[i].filename[0] != 0 && cache[i].meta.etag[0] == 0 && !cache[i].is_updating) { if (cache[i].filename[0] != 0 && cache[i].meta.etag[0] == 0 && !cache[i].is_updating) {
cache[i].is_updating = 1; cache[i].is_updating = 1;
fprintf(stdout, "[cache] Hashing file %s\n", cache[i].filename); fprintf(stdout, "[cache] Hashing file %s\n", cache[i].filename);
SHA1_Init(&ctx);
ctx = EVP_MD_CTX_new();
EVP_DigestInit(ctx, EVP_sha1());
file = fopen(cache[i].filename, "rb"); file = fopen(cache[i].filename, "rb");
compress = mime_is_compressible(cache[i].meta.type); compress = mime_is_compressible(cache[i].meta.type);
@ -131,11 +130,8 @@ int cache_process(void) {
p_len_br = snprintf(filename_comp_br, sizeof(filename_comp_br), p_len_br = snprintf(filename_comp_br, sizeof(filename_comp_br),
"%.*s/.sesimos/cache/%s.br", "%.*s/.sesimos/cache/%s.br",
cache[i].webroot_len, cache[i].filename, buf); cache[i].webroot_len, cache[i].filename, buf);
if (p_len_gz < 0 || p_len_gz >= sizeof(filename_comp_gz) || if (p_len_gz < 0 || p_len_gz >= sizeof(filename_comp_gz) || p_len_br < 0 || p_len_br >= sizeof(filename_comp_br)) {
p_len_br < 0 || p_len_br >= sizeof(filename_comp_br)) fprintf(stderr, ERR_STR "Unable to open cached file: File name for compressed file too long" CLR_STR "\n");
{
fprintf(stderr, ERR_STR "Unable to open cached file: "
"File name for compressed file too long" CLR_STR "\n");
goto comp_err; goto comp_err;
} }
@ -159,21 +155,19 @@ int cache_process(void) {
} }
while ((read = fread(buf, 1, CACHE_BUF_SIZE, file)) > 0) { while ((read = fread(buf, 1, CACHE_BUF_SIZE, file)) > 0) {
SHA1_Update(&ctx, buf, read); EVP_DigestUpdate(ctx, buf, read);
if (compress) { if (compress) {
unsigned long avail_in, avail_out; unsigned long avail_in, avail_out;
avail_in = read; avail_in = read;
do { do {
avail_out = CACHE_BUF_SIZE; avail_out = CACHE_BUF_SIZE;
compress_compress_mode(&comp_ctx, COMPRESS_GZ,buf + read - avail_in, &avail_in, compress_compress_mode(&comp_ctx, COMPRESS_GZ,buf + read - avail_in, &avail_in, comp_buf, &avail_out, feof(file));
comp_buf, &avail_out, feof(file));
fwrite(comp_buf, 1, CACHE_BUF_SIZE - avail_out, comp_file_gz); fwrite(comp_buf, 1, CACHE_BUF_SIZE - avail_out, comp_file_gz);
} while (avail_in != 0 || avail_out != CACHE_BUF_SIZE); } while (avail_in != 0 || avail_out != CACHE_BUF_SIZE);
avail_in = read; avail_in = read;
do { do {
avail_out = CACHE_BUF_SIZE; avail_out = CACHE_BUF_SIZE;
compress_compress_mode(&comp_ctx, COMPRESS_BR, buf + read - avail_in, &avail_in, compress_compress_mode(&comp_ctx, COMPRESS_BR, buf + read - avail_in, &avail_in, comp_buf, &avail_out, feof(file));
comp_buf, &avail_out, feof(file));
fwrite(comp_buf, 1, CACHE_BUF_SIZE - avail_out, comp_file_br); fwrite(comp_buf, 1, CACHE_BUF_SIZE - avail_out, comp_file_br);
} while (avail_in != 0 || avail_out != CACHE_BUF_SIZE); } while (avail_in != 0 || avail_out != CACHE_BUF_SIZE);
} }
@ -190,9 +184,12 @@ int cache_process(void) {
memset(cache[i].meta.filename_comp_gz, 0, sizeof(cache[i].meta.filename_comp_gz)); memset(cache[i].meta.filename_comp_gz, 0, sizeof(cache[i].meta.filename_comp_gz));
memset(cache[i].meta.filename_comp_br, 0, sizeof(cache[i].meta.filename_comp_br)); memset(cache[i].meta.filename_comp_br, 0, sizeof(cache[i].meta.filename_comp_br));
} }
SHA1_Final(hash, &ctx);
EVP_DigestFinal(ctx, hash, &md_len);
EVP_MD_CTX_free(ctx);
memset(cache[i].meta.etag, 0, sizeof(cache[i].meta.etag)); memset(cache[i].meta.etag, 0, sizeof(cache[i].meta.etag));
for (int j = 0; j < SHA_DIGEST_LENGTH; j++) { for (int j = 0; j < md_len; j++) {
sprintf(cache[i].meta.etag + j * 2, "%02x", hash[j]); sprintf(cache[i].meta.etag + j * 2, "%02x", hash[j]);
} }
fclose(file); fclose(file);
@ -207,8 +204,6 @@ int cache_process(void) {
cache_file = fopen("/var/sesimos/server/cache", "wb"); cache_file = fopen("/var/sesimos/server/cache", "wb");
if (cache_file == NULL) { if (cache_file == NULL) {
fprintf(stderr, ERR_STR "Unable to open cache file: %s" CLR_STR "\n", strerror(errno)); fprintf(stderr, ERR_STR "Unable to open cache file: %s" CLR_STR "\n", strerror(errno));
free(buf);
free(comp_buf);
return -1; return -1;
} }
fwrite(cache, sizeof(cache_entry), CACHE_ENTRIES, cache_file); fwrite(cache, sizeof(cache_entry), CACHE_ENTRIES, cache_file);
@ -217,8 +212,7 @@ int cache_process(void) {
sleep(1); sleep(1);
} }
} }
free(buf);
free(comp_buf);
return 0; return 0;
} }