2011-11-14 15:57:52 +01:00
|
|
|
/*
|
|
|
|
* digest.c, SJ
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <piler.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
|
|
|
|
|
2011-12-29 21:25:40 +01:00
|
|
|
#define MAX(m,n) m <= n ? m : n
|
|
|
|
|
|
|
|
|
|
|
|
int search_header_end(char *p, int n){
|
|
|
|
int hdr_len=0;
|
|
|
|
|
|
|
|
if(strlen(p) < 5) return hdr_len;
|
|
|
|
|
|
|
|
for(; *p; p++){
|
|
|
|
if(hdr_len < n-2 && *p == '\n' && *(p+1) == '\r' && *(p+2) == '\n'){ hdr_len += 3; return MAX(hdr_len, n); }
|
|
|
|
if(hdr_len < n-1 && *p == '\n' && *(p+1) == '\n'){ hdr_len += 2; return MAX(hdr_len, n); }
|
|
|
|
hdr_len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-29 12:11:28 +01:00
|
|
|
int make_digests(struct session_data *sdata, struct __config *cfg){
|
2011-12-29 21:25:40 +01:00
|
|
|
int i=0, n, fd, offset=3, hdr_len=0;
|
|
|
|
char *body=NULL;
|
2011-12-29 12:11:28 +01:00
|
|
|
unsigned char buf[BIGBUFSIZE], md[DIGEST_LENGTH], md2[DIGEST_LENGTH];
|
|
|
|
SHA256_CTX context, context2;
|
2011-11-14 15:57:52 +01:00
|
|
|
|
|
|
|
memset(sdata->bodydigest, 0, 2*DIGEST_LENGTH+1);
|
2011-12-29 12:11:28 +01:00
|
|
|
memset(sdata->digest, 0, 2*DIGEST_LENGTH+1);
|
2011-11-16 14:47:47 +01:00
|
|
|
SHA256_Init(&context);
|
2011-12-29 12:11:28 +01:00
|
|
|
SHA256_Init(&context2);
|
2011-11-14 15:57:52 +01:00
|
|
|
|
2011-12-29 21:25:40 +01:00
|
|
|
|
2012-01-03 00:19:43 +01:00
|
|
|
fd = open(sdata->filename, O_RDONLY);
|
2011-11-14 15:57:52 +01:00
|
|
|
if(fd == -1) return -1;
|
|
|
|
|
2011-11-28 14:21:14 +01:00
|
|
|
while((n = read(fd, buf, sizeof(buf))) > 0){
|
2011-12-09 15:24:15 +01:00
|
|
|
|
2011-12-29 12:11:28 +01:00
|
|
|
SHA256_Update(&context2, buf, n);
|
|
|
|
|
2011-11-14 15:57:52 +01:00
|
|
|
body = (char *)&buf[0];
|
|
|
|
|
2011-11-22 12:31:54 +01:00
|
|
|
if(i == 0){
|
|
|
|
|
2011-12-29 21:25:40 +01:00
|
|
|
hdr_len = search_header_end(body, n);
|
2011-11-22 12:31:54 +01:00
|
|
|
|
2011-12-29 21:25:40 +01:00
|
|
|
if(hdr_len > 0){
|
|
|
|
body += hdr_len;
|
|
|
|
n -= hdr_len;
|
2011-11-22 12:31:54 +01:00
|
|
|
|
2011-12-29 21:25:40 +01:00
|
|
|
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: hdr_len: %d, offset: %d", sdata->ttmpfile, hdr_len, offset);
|
2011-11-14 15:57:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-22 12:31:54 +01:00
|
|
|
|
2011-11-16 14:47:47 +01:00
|
|
|
SHA256_Update(&context, body, n);
|
2011-11-14 15:57:52 +01:00
|
|
|
|
2011-11-22 12:31:54 +01:00
|
|
|
i++;
|
2011-11-14 15:57:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
2011-12-29 21:25:40 +01:00
|
|
|
sdata->hdr_len = hdr_len;
|
|
|
|
|
2011-11-16 14:47:47 +01:00
|
|
|
SHA256_Final(md, &context);
|
2011-12-29 12:11:28 +01:00
|
|
|
SHA256_Final(md2, &context2);
|
2011-11-14 15:57:52 +01:00
|
|
|
|
2011-12-29 12:11:28 +01:00
|
|
|
for(i=0;i<DIGEST_LENGTH;i++){
|
2011-11-16 14:47:47 +01:00
|
|
|
snprintf(sdata->bodydigest + i*2, 2*DIGEST_LENGTH, "%02x", md[i]);
|
2011-12-29 12:11:28 +01:00
|
|
|
snprintf(sdata->digest + i*2, 2*DIGEST_LENGTH, "%02x", md2[i]);
|
|
|
|
}
|
2011-11-14 15:57:52 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-19 21:25:44 +01:00
|
|
|
void digest_file(char *filename, char *digest){
|
|
|
|
int fd, i, n;
|
|
|
|
unsigned char buf[MAXBUFSIZE], md[DIGEST_LENGTH];
|
|
|
|
SHA256_CTX context;
|
|
|
|
|
|
|
|
memset(digest, 0, 2*DIGEST_LENGTH+1);
|
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if(fd == -1) return;
|
|
|
|
|
|
|
|
SHA256_Init(&context);
|
|
|
|
|
2011-11-28 14:21:14 +01:00
|
|
|
while((n = read(fd, buf, sizeof(buf))) > 0){
|
2011-11-19 21:25:44 +01:00
|
|
|
SHA256_Update(&context, buf, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
SHA256_Final(md, &context);
|
|
|
|
|
|
|
|
for(i=0;i<DIGEST_LENGTH;i++)
|
|
|
|
snprintf(digest + i*2, 2*DIGEST_LENGTH, "%02x", md[i]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|