Merge branch 'dev' of https://github.com/ntop/nDPI into ndpi_icmp
This commit is contained in:
@@ -98,7 +98,7 @@ typedef enum {
|
||||
NDPI_PROTOCOL_QQLIVE = 61,
|
||||
NDPI_PROTOCOL_THUNDER = 62,
|
||||
NDPI_PROTOCOL_SOULSEEK = 63,
|
||||
NDPI_PROTOCOL_TLS_NO_CERT = 64,
|
||||
NDPI_FREE_64 = 64,
|
||||
NDPI_PROTOCOL_IRC = 65,
|
||||
NDPI_PROTOCOL_AYIYA = 66,
|
||||
NDPI_PROTOCOL_UNENCRYPTED_JABBER = 67,
|
||||
|
||||
@@ -616,12 +616,18 @@ struct ndpi_flow_tcp_struct {
|
||||
/* NDPI_PROTOCOL_TELNET */
|
||||
u_int32_t telnet_stage:2; // 0 - 2
|
||||
|
||||
void* tls_srv_cert_fingerprint_ctx;
|
||||
|
||||
/* NDPI_PROTOCOL_TLS */
|
||||
u_int8_t ssl_seen_client_cert:1,
|
||||
ssl_seen_server_cert:1,
|
||||
ssl_seen_certificate:1,
|
||||
ssl_stage:2; // 0 - 5
|
||||
|
||||
u_int8_t tls_seen_client_cert:1,
|
||||
tls_seen_server_cert:1,
|
||||
tls_seen_certificate:1,
|
||||
tls_srv_cert_fingerprint_found:1,
|
||||
tls_srv_cert_fingerprint_processed:1,
|
||||
tls_stage:2, _pad:1; // 0 - 5
|
||||
int16_t tls_record_offset, tls_fingerprint_len; /* Need to be signed */
|
||||
u_int8_t tls_sha1_certificate_fingerprint[20];
|
||||
|
||||
/* NDPI_PROTOCOL_POSTGRES */
|
||||
u_int32_t postgres_stage:3;
|
||||
|
||||
@@ -807,7 +813,7 @@ struct ndpi_packet_struct {
|
||||
u_int8_t tcp_retransmission;
|
||||
u_int8_t l4_protocol;
|
||||
|
||||
u_int8_t ssl_certificate_detected:4, ssl_certificate_num_checks:4;
|
||||
u_int8_t tls_certificate_detected:4, tls_certificate_num_checks:4;
|
||||
u_int8_t packet_lines_parsed_complete:1,
|
||||
packet_direction:1, empty_line_position_set:1, pad:5;
|
||||
};
|
||||
@@ -1124,7 +1130,7 @@ struct ndpi_flow_struct {
|
||||
|
||||
/* init parameter, internal used to set up timestamp,... */
|
||||
u_int16_t guessed_protocol_id, guessed_host_protocol_id, guessed_category, guessed_header_category;
|
||||
u_int8_t protocol_id_already_guessed:1, host_already_guessed:1, init_finished:1, setup_packet_direction:1, packet_direction:1, check_extra_packets:1;
|
||||
u_int8_t l4_proto, protocol_id_already_guessed:1, host_already_guessed:1, init_finished:1, setup_packet_direction:1, packet_direction:1, check_extra_packets:1;
|
||||
|
||||
/*
|
||||
if ndpi_struct->direction_detect_disable == 1
|
||||
|
||||
+46
-34
@@ -173,6 +173,7 @@ u_int16_t ndpi_get_proto_by_name(struct ndpi_detection_module_struct *ndpi_mod,
|
||||
|
||||
/* ************************************************************************************* */
|
||||
|
||||
#ifdef CODE_UNUSED
|
||||
ndpi_port_range * ndpi_build_default_ports_range(ndpi_port_range *ports,
|
||||
u_int16_t portA_low, u_int16_t portA_high,
|
||||
u_int16_t portB_low, u_int16_t portB_high,
|
||||
@@ -189,6 +190,7 @@ ndpi_port_range * ndpi_build_default_ports_range(ndpi_port_range *ports,
|
||||
|
||||
return(ports);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* *********************************************************************************** */
|
||||
|
||||
@@ -483,6 +485,7 @@ static int ndpi_add_host_url_subprotocol(struct ndpi_detection_module_struct *nd
|
||||
|
||||
/* ****************************************************** */
|
||||
|
||||
#ifdef CODE_UNUSED
|
||||
int ndpi_add_content_subprotocol(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
char *value, int protocol_id,
|
||||
ndpi_protocol_category_t category,
|
||||
@@ -490,6 +493,7 @@ int ndpi_add_content_subprotocol(struct ndpi_detection_module_struct *ndpi_struc
|
||||
return(ndpi_string_to_automa(ndpi_struct, &ndpi_struct->content_automa,
|
||||
value, protocol_id, category, breed));
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ****************************************************** */
|
||||
|
||||
@@ -1118,10 +1122,9 @@ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndp
|
||||
ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */,
|
||||
ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */);
|
||||
|
||||
custom_master[0] = NDPI_PROTOCOL_TLS, custom_master[1] = NDPI_PROTOCOL_UNKNOWN;
|
||||
ndpi_set_proto_defaults(ndpi_mod, NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_TLS_NO_CERT,
|
||||
1 /* can_have_a_subprotocol */, custom_master,
|
||||
no_master, "TLS_No_Cert", NDPI_PROTOCOL_CATEGORY_WEB,
|
||||
ndpi_set_proto_defaults(ndpi_mod, NDPI_PROTOCOL_FUN, NDPI_FREE_64,
|
||||
0 /* can_have_a_subprotocol */, no_master,
|
||||
no_master, "Free64", NDPI_PROTOCOL_CATEGORY_DOWNLOAD_FT,
|
||||
ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */,
|
||||
ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */);
|
||||
ndpi_set_proto_defaults(ndpi_mod, NDPI_PROTOCOL_UNSAFE, NDPI_PROTOCOL_IRC,
|
||||
@@ -1262,10 +1265,9 @@ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndp
|
||||
ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */,
|
||||
ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */);
|
||||
|
||||
custom_master[0] = NDPI_PROTOCOL_TLS_NO_CERT, custom_master[1] = NDPI_PROTOCOL_UNKNOWN;
|
||||
ndpi_set_proto_defaults(ndpi_mod, NDPI_PROTOCOL_SAFE, NDPI_PROTOCOL_TLS,
|
||||
1 /* can_have_a_subprotocol */, no_master,
|
||||
custom_master, "TLS", NDPI_PROTOCOL_CATEGORY_WEB,
|
||||
no_master, "TLS", NDPI_PROTOCOL_CATEGORY_WEB,
|
||||
ndpi_build_default_ports(ports_a, 443, 0, 0, 0, 0) /* TCP */,
|
||||
ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */);
|
||||
ndpi_set_proto_defaults(ndpi_mod, NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_SSH,
|
||||
@@ -1769,7 +1771,7 @@ static int ac_match_handler(AC_MATCH_t *m, AC_TEXT_t *txt, AC_REP_t *match) {
|
||||
buf[min_buf_len] = '\0';
|
||||
|
||||
#ifdef MATCH_DEBUG
|
||||
printf("Searching [to search: %s/%u][pattern: %s/%u] [len: %u][match_num: %u][%s]\n",
|
||||
printf("Searching [to search: %s/%u][pattern: %s/%u] [len: %d][match_num: %u][%s]\n",
|
||||
buf, (unigned int)txt->length, m->patterns->astring, m->patterns->length, min_len,
|
||||
m->match_num, m->patterns->astring);
|
||||
#endif
|
||||
@@ -1809,7 +1811,7 @@ static int ac_match_handler(AC_MATCH_t *m, AC_TEXT_t *txt, AC_REP_t *match) {
|
||||
|| (strncmp(buf, m->patterns->astring, min_len) == 0) /* begins with */
|
||||
) {
|
||||
#ifdef MATCH_DEBUG
|
||||
printf("Found match [%s][%s] [len: %u]"
|
||||
printf("Found match [%s][%s] [len: %d]"
|
||||
// "[proto_id: %u]"
|
||||
"\n",
|
||||
buf, m->patterns->astring, min_len /* , *matching_protocol_id */);
|
||||
@@ -1956,7 +1958,7 @@ void ndpi_debug_printf(unsigned int proto, struct ndpi_detection_module_struct *
|
||||
va_end(args);
|
||||
|
||||
if(ndpi_str != NULL) {
|
||||
printf("%s:%s:%-3u - [%s]: %s",
|
||||
printf("%s:%s:%-3d - [%s]: %s",
|
||||
file_name, func_name, line_number, ndpi_get_proto_name(ndpi_str, proto), str);
|
||||
} else {
|
||||
printf("Proto: %u, %s", proto, str);
|
||||
@@ -2720,7 +2722,7 @@ int ndpi_load_protocols_file(struct ndpi_detection_module_struct *ndpi_mod, cons
|
||||
goto close_fd;
|
||||
}
|
||||
|
||||
while(fd) {
|
||||
while(1) {
|
||||
char *line = buffer;
|
||||
int line_len = buffer_len;
|
||||
|
||||
@@ -3532,8 +3534,7 @@ void ndpi_apply_flow_protocol_to_packet(struct ndpi_flow_struct *flow,
|
||||
|
||||
static int ndpi_init_packet_header(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
struct ndpi_flow_struct *flow,
|
||||
unsigned short packetlen)
|
||||
{
|
||||
unsigned short packetlen) {
|
||||
const struct ndpi_iphdr *decaps_iph = NULL;
|
||||
u_int16_t l3len;
|
||||
u_int16_t l4len;
|
||||
@@ -3541,17 +3542,15 @@ static int ndpi_init_packet_header(struct ndpi_detection_module_struct *ndpi_str
|
||||
u_int8_t l4protocol;
|
||||
u_int8_t l4_result;
|
||||
|
||||
if (!flow) {
|
||||
return 1;
|
||||
}
|
||||
if (!flow)
|
||||
return 1;
|
||||
|
||||
/* reset payload_packet_len, will be set if ipv4 tcp or udp */
|
||||
flow->packet.payload_packet_len = 0;
|
||||
flow->packet.l4_packet_len = 0;
|
||||
flow->packet.l3_packet_len = packetlen;
|
||||
|
||||
flow->packet.tcp = NULL;
|
||||
flow->packet.udp = NULL;
|
||||
flow->packet.tcp = NULL, flow->packet.udp = NULL;
|
||||
flow->packet.generic_l4_ptr = NULL;
|
||||
#ifdef NDPI_DETECTION_SUPPORT_IPV6
|
||||
flow->packet.iphv6 = NULL;
|
||||
@@ -3587,14 +3586,12 @@ static int ndpi_init_packet_header(struct ndpi_detection_module_struct *ndpi_str
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* needed:
|
||||
* - unfragmented packets
|
||||
* - ip header <= packet len
|
||||
* - ip total length >= packet len
|
||||
*/
|
||||
|
||||
|
||||
l4ptr = NULL;
|
||||
l4len = 0;
|
||||
l4protocol = 0;
|
||||
@@ -3608,12 +3605,12 @@ static int ndpi_init_packet_header(struct ndpi_detection_module_struct *ndpi_str
|
||||
|
||||
flow->packet.l4_protocol = l4protocol;
|
||||
flow->packet.l4_packet_len = l4len;
|
||||
|
||||
flow->l4_proto = l4protocol;
|
||||
|
||||
/* tcp / udp detection */
|
||||
if(l4protocol == IPPROTO_TCP && flow->packet.l4_packet_len >= 20 /* min size of tcp */ ) {
|
||||
/* tcp */
|
||||
flow->packet.tcp = (struct ndpi_tcphdr *) l4ptr;
|
||||
|
||||
if(flow->packet.l4_packet_len >=flow->packet.tcp->doff * 4) {
|
||||
flow->packet.payload_packet_len =
|
||||
flow->packet.l4_packet_len -flow->packet.tcp->doff * 4;
|
||||
@@ -3863,6 +3860,7 @@ void check_ndpi_udp_flow_func(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
&& NDPI_BITMASK_COMPARE(ndpi_struct->callback_buffer_udp[a].detection_bitmask,
|
||||
detection_bitmask) != 0) {
|
||||
ndpi_struct->callback_buffer_udp[a].func(ndpi_struct, flow);
|
||||
|
||||
// NDPI_LOG_DBG(ndpi_struct, "[UDP,CALL] dissector of protocol as callback_buffer idx = %d\n",a);
|
||||
if(flow->detected_protocol_stack[0] != NDPI_PROTOCOL_UNKNOWN)
|
||||
break; /* Stop after detecting the first protocol */
|
||||
@@ -4059,7 +4057,7 @@ ndpi_protocol ndpi_detection_giveup(struct ndpi_detection_module_struct *ndpi_st
|
||||
|| (flow->guessed_protocol_id == NDPI_PROTOCOL_MESSENGER)
|
||||
|| (flow->guessed_protocol_id == NDPI_PROTOCOL_WHATSAPP_VOICE))
|
||||
ndpi_set_detected_protocol(ndpi_struct, flow, flow->guessed_protocol_id, NDPI_PROTOCOL_UNKNOWN);
|
||||
else if((flow->l4.tcp.ssl_seen_client_cert == 1)
|
||||
else if((flow->l4.tcp.tls_seen_client_cert == 1)
|
||||
&& (flow->protos.stun_ssl.ssl.client_certificate[0] != '\0')) {
|
||||
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_TLS, NDPI_PROTOCOL_UNKNOWN);
|
||||
} else {
|
||||
@@ -4074,8 +4072,8 @@ ndpi_protocol ndpi_detection_giveup(struct ndpi_detection_module_struct *ndpi_st
|
||||
|
||||
if((flow->guessed_protocol_id == NDPI_PROTOCOL_UNKNOWN)
|
||||
&& (flow->packet.l4_protocol == IPPROTO_TCP)
|
||||
&& (flow->l4.tcp.ssl_stage > 1))
|
||||
flow->guessed_protocol_id = NDPI_PROTOCOL_TLS_NO_CERT;
|
||||
&& (flow->l4.tcp.tls_stage > 1))
|
||||
flow->guessed_protocol_id = NDPI_PROTOCOL_TLS;
|
||||
|
||||
guessed_protocol_id = flow->guessed_protocol_id, guessed_host_protocol_id = flow->guessed_host_protocol_id;
|
||||
|
||||
@@ -4448,7 +4446,7 @@ void ndpi_fill_protocol_category(struct ndpi_detection_module_struct *ndpi_struc
|
||||
}
|
||||
}
|
||||
|
||||
if((flow->l4.tcp.ssl_seen_client_cert == 1) && (flow->protos.stun_ssl.ssl.client_certificate[0] != '\0')) {
|
||||
if((flow->l4.tcp.tls_seen_client_cert == 1) && (flow->protos.stun_ssl.ssl.client_certificate[0] != '\0')) {
|
||||
unsigned long id;
|
||||
int rc = ndpi_match_custom_category(ndpi_struct,
|
||||
(char *)flow->protos.stun_ssl.ssl.client_certificate,
|
||||
@@ -4488,6 +4486,9 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
|
||||
|
||||
flow->num_processed_pkts++;
|
||||
|
||||
/* Init default */
|
||||
ret.master_protocol = flow->detected_protocol_stack[1], ret.app_protocol = flow->detected_protocol_stack[0];
|
||||
|
||||
if(flow->server_id == NULL) flow->server_id = dst; /* Default */
|
||||
if(flow->detected_protocol_stack[0] != NDPI_PROTOCOL_UNKNOWN) {
|
||||
/*
|
||||
@@ -4496,12 +4497,11 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
|
||||
*/
|
||||
if(flow->check_extra_packets
|
||||
/*
|
||||
&& ((flow->detected_protocol_stack[0] == NDPI_PROTOCOL_TLS)
|
||||
|| (flow->detected_protocol_stack[1] == NDPI_PROTOCOL_TLS))
|
||||
&& (flow->detected_protocol_stack[0] == NDPI_PROTOCOL_TLS)
|
||||
*/
|
||||
) {
|
||||
ndpi_process_extra_packet(ndpi_struct, flow, packet, packetlen, current_tick_l, src, dst);
|
||||
ret.master_protocol = flow->detected_protocol_stack[1], ret.app_protocol = flow->detected_protocol_stack[0];
|
||||
|
||||
return(ret);
|
||||
} else
|
||||
goto ret_protocols;
|
||||
@@ -4729,8 +4729,9 @@ u_int32_t ndpi_bytestream_to_number(const u_int8_t * str, u_int16_t max_chars_to
|
||||
|
||||
/* ********************************************************************************* */
|
||||
|
||||
u_int32_t ndpi_bytestream_dec_or_hex_to_number(const u_int8_t * str, u_int16_t max_chars_to_read, u_int16_t * bytes_read)
|
||||
{
|
||||
#ifdef CODE_UNUSED
|
||||
u_int32_t ndpi_bytestream_dec_or_hex_to_number(const u_int8_t * str, u_int16_t max_chars_to_read,
|
||||
u_int16_t * bytes_read) {
|
||||
u_int32_t val;
|
||||
val = 0;
|
||||
if(max_chars_to_read <= 2 || str[0] != '0' || str[1] != 'x') {
|
||||
@@ -4762,10 +4763,12 @@ u_int32_t ndpi_bytestream_dec_or_hex_to_number(const u_int8_t * str, u_int16_t m
|
||||
return (val);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ********************************************************************************* */
|
||||
|
||||
u_int64_t ndpi_bytestream_to_number64(const u_int8_t * str, u_int16_t max_chars_to_read, u_int16_t * bytes_read)
|
||||
{
|
||||
u_int64_t ndpi_bytestream_to_number64(const u_int8_t * str, u_int16_t max_chars_to_read,
|
||||
u_int16_t * bytes_read) {
|
||||
u_int64_t val;
|
||||
val = 0;
|
||||
// cancel if eof, ' ' or line end chars are reached
|
||||
@@ -5400,11 +5403,13 @@ void NDPI_PROTOCOL_IP_clear(ndpi_ip_addr_t * ip) {
|
||||
|
||||
/* ********************************************************************************* */
|
||||
|
||||
#ifdef CODE_UNUSED
|
||||
/* NTOP */
|
||||
int NDPI_PROTOCOL_IP_is_set(const ndpi_ip_addr_t * ip)
|
||||
{
|
||||
return memcmp(ip, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", sizeof(ndpi_ip_addr_t)) != 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ********************************************************************************* */
|
||||
|
||||
@@ -6117,6 +6122,12 @@ void ndpi_free_flow(struct ndpi_flow_struct *flow) {
|
||||
if(flow) {
|
||||
if(flow->http.url) ndpi_free(flow->http.url);
|
||||
if(flow->http.content_type) ndpi_free(flow->http.content_type);
|
||||
|
||||
if(flow->l4_proto == IPPROTO_TCP) {
|
||||
if(flow->l4.tcp.tls_srv_cert_fingerprint_ctx)
|
||||
ndpi_free(flow->l4.tcp.tls_srv_cert_fingerprint_ctx);
|
||||
}
|
||||
|
||||
ndpi_free(flow);
|
||||
}
|
||||
}
|
||||
@@ -6130,8 +6141,7 @@ char* ndpi_revision() { return(NDPI_GIT_RELEASE); }
|
||||
#ifdef WIN32
|
||||
|
||||
/* https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows */
|
||||
int gettimeofday(struct timeval * tp, struct timezone * tzp)
|
||||
{
|
||||
int gettimeofday(struct timeval * tp, struct timezone * tzp) {
|
||||
// Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
|
||||
// This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
|
||||
// until 00:00:00 January 1, 1970
|
||||
@@ -6163,6 +6173,7 @@ int NDPI_BITMASK_COMPARE(NDPI_PROTOCOL_BITMASK a, NDPI_PROTOCOL_BITMASK b) {
|
||||
return(0);
|
||||
}
|
||||
|
||||
#ifdef CODE_UNUSED
|
||||
int NDPI_BITMASK_IS_EMPTY(NDPI_PROTOCOL_BITMASK a) {
|
||||
int i;
|
||||
|
||||
@@ -6180,6 +6191,7 @@ void NDPI_DUMP_BITMASK(NDPI_PROTOCOL_BITMASK a) {
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
u_int8_t ndpi_get_api_version() {
|
||||
return(NDPI_API_VERSION);
|
||||
|
||||
@@ -83,7 +83,8 @@ static void ndpi_check_skype(struct ndpi_detection_module_struct *ndpi_struct, s
|
||||
&& flow->l4.tcp.seen_syn
|
||||
&& flow->l4.tcp.seen_syn_ack
|
||||
&& flow->l4.tcp.seen_ack) {
|
||||
|
||||
/* Disabled this logic as it's too weak and leads to false positives */
|
||||
#if 0
|
||||
if((payload_len == 8) || (payload_len == 3) || (payload_len == 17)) {
|
||||
// printf("[SKYPE] payload_len=%u\n", payload_len);
|
||||
/* printf("[SKYPE] %u/%u\n", ntohs(packet->tcp->source), ntohs(packet->tcp->dest)); */
|
||||
@@ -95,6 +96,7 @@ static void ndpi_check_skype(struct ndpi_detection_module_struct *ndpi_struct, s
|
||||
}
|
||||
|
||||
/* printf("[SKYPE] [id: %u][len: %d]\n", flow->l4.tcp.skype_packet_id, payload_len); */
|
||||
#endif
|
||||
} else {
|
||||
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
|
||||
}
|
||||
|
||||
+277
-80
@@ -27,9 +27,14 @@
|
||||
|
||||
#include "ndpi_api.h"
|
||||
#include "ndpi_md5.h"
|
||||
#include "ndpi_sha1.h"
|
||||
|
||||
extern char *strptime(const char *s, const char *format, struct tm *tm);
|
||||
|
||||
// #define DEBUG_TLS 1
|
||||
|
||||
#define DEBUG_FINGERPRINT 1
|
||||
|
||||
/*
|
||||
NOTE
|
||||
|
||||
@@ -41,6 +46,8 @@
|
||||
3. openssl x509 -noout -fingerprint -sha1 -inform pem -in certificate.der
|
||||
SHA1 Fingerprint=15:9A:76....
|
||||
|
||||
$ shasum -a 1 www.grc.com.bin
|
||||
159a76.....
|
||||
*/
|
||||
|
||||
#define NDPI_MAX_TLS_REQUEST_SIZE 10000
|
||||
@@ -58,19 +65,11 @@ static u_int32_t ndpi_tls_refine_master_protocol(struct ndpi_detection_module_st
|
||||
struct ndpi_flow_struct *flow, u_int32_t protocol) {
|
||||
struct ndpi_packet_struct *packet = &flow->packet;
|
||||
|
||||
if(((flow->l4.tcp.ssl_seen_client_cert == 1) && (flow->protos.stun_ssl.ssl.ja3_client[0] != '\0'))
|
||||
|| ((flow->l4.tcp.ssl_seen_server_cert == 1) && (flow->protos.stun_ssl.ssl.ja3_server[0] != '\0'))
|
||||
// || (flow->host_server_name[0] != '\0')
|
||||
)
|
||||
protocol = NDPI_PROTOCOL_TLS;
|
||||
else
|
||||
protocol = NDPI_PROTOCOL_TLS_NO_CERT;
|
||||
protocol = NDPI_PROTOCOL_TLS;
|
||||
|
||||
if(packet->tcp != NULL) {
|
||||
switch(protocol) {
|
||||
|
||||
case NDPI_PROTOCOL_TLS:
|
||||
case NDPI_PROTOCOL_TLS_NO_CERT:
|
||||
{
|
||||
/*
|
||||
In case of SSL there are probably sub-protocols
|
||||
@@ -97,9 +96,9 @@ static u_int32_t ndpi_tls_refine_master_protocol(struct ndpi_detection_module_st
|
||||
|
||||
static void ndpi_int_tls_add_connection(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
struct ndpi_flow_struct *flow, u_int32_t protocol) {
|
||||
if((protocol != NDPI_PROTOCOL_TLS) && (protocol != NDPI_PROTOCOL_TLS_NO_CERT)) {
|
||||
if(protocol != NDPI_PROTOCOL_TLS)
|
||||
;
|
||||
} else
|
||||
else
|
||||
protocol = ndpi_tls_refine_master_protocol(ndpi_struct, flow, protocol);
|
||||
|
||||
ndpi_set_detected_protocol(ndpi_struct, flow, protocol, NDPI_PROTOCOL_TLS);
|
||||
@@ -191,12 +190,12 @@ int getTLScertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
char *buffer, int buffer_len) {
|
||||
struct ndpi_packet_struct *packet = &flow->packet;
|
||||
struct ja3_info ja3;
|
||||
int i;
|
||||
u_int8_t invalid_ja3 = 0;
|
||||
u_int16_t pkt_tls_version = (packet->payload[1] << 8) + packet->payload[2], ja3_str_len;
|
||||
char ja3_str[JA3_STR_LEN];
|
||||
ndpi_MD5_CTX ctx;
|
||||
u_char md5_hash[16];
|
||||
int i;
|
||||
|
||||
if(packet->udp) {
|
||||
/* Check if this is DTLS or return */
|
||||
@@ -257,7 +256,8 @@ int getTLScertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
|| (handshake_protocol == 0x0b) /* Server Hello and Certificate message types are interesting for us */) {
|
||||
u_int num_found = 0;
|
||||
u_int16_t tls_version;
|
||||
|
||||
int i;
|
||||
|
||||
if(packet->tcp)
|
||||
tls_version = ntohs(*((u_int16_t*)&packet->payload[header_len+4]));
|
||||
else
|
||||
@@ -352,9 +352,9 @@ int getTLScertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
printf("[JA3] Server: %s \n", flow->protos.stun_ssl.ssl.ja3_server);
|
||||
#endif
|
||||
|
||||
flow->l4.tcp.ssl_seen_server_cert = 1;
|
||||
flow->l4.tcp.tls_seen_server_cert = 1;
|
||||
} else
|
||||
flow->l4.tcp.ssl_seen_certificate = 1;
|
||||
flow->l4.tcp.tls_seen_certificate = 1;
|
||||
|
||||
/* Check after handshake protocol header (5 bytes) and message header (4 bytes) */
|
||||
for(i = 9; i < packet->payload_packet_len-3; i++) {
|
||||
@@ -448,7 +448,7 @@ int getTLScertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
u_int16_t *id = (u_int16_t*)&packet->payload[cipher_offset+i];
|
||||
|
||||
#ifdef DEBUG_TLS
|
||||
printf("Client SSL [cipher suite: %u/0x%04X] [%u/%u]\n", ntohs(*id), ntohs(*id), i, cipher_len);
|
||||
printf("Client SSL [cipher suite: %u/0x%04X] [%d/%u]\n", ntohs(*id), ntohs(*id), i, cipher_len);
|
||||
#endif
|
||||
if((*id == 0) || (packet->payload[cipher_offset+i] != packet->payload[cipher_offset+i+1])) {
|
||||
/*
|
||||
@@ -477,7 +477,7 @@ int getTLScertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
|
||||
offset = base_offset + session_id_len + cipher_len + 2;
|
||||
|
||||
flow->l4.tcp.ssl_seen_client_cert = 1;
|
||||
flow->l4.tcp.tls_seen_client_cert = 1;
|
||||
|
||||
if(offset < total_len) {
|
||||
u_int16_t compression_len;
|
||||
@@ -683,22 +683,181 @@ int getTLScertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
|
||||
/* **************************************** */
|
||||
|
||||
/* See https://blog.catchpoint.com/2017/05/12/dissecting-tls-using-wireshark/ */
|
||||
int getSSCertificateFingerprint(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
struct ndpi_flow_struct *flow) {
|
||||
struct ndpi_packet_struct *packet = &flow->packet;
|
||||
u_int8_t multiple_messages;
|
||||
|
||||
if(flow->l4.tcp.tls_srv_cert_fingerprint_processed)
|
||||
return(0); /* We're good */
|
||||
|
||||
#ifdef DEBUG_TLS
|
||||
printf("=>> [TLS] %s() [tls_record_offset=%d][payload_packet_len=%u][direction: %u][%02X %02X %02X...]\n",
|
||||
__FUNCTION__, flow->l4.tcp.tls_record_offset, packet->payload_packet_len,
|
||||
packet->packet_direction,
|
||||
packet->payload[0], packet->payload[1], packet->payload[2]);
|
||||
#endif
|
||||
|
||||
if((packet->packet_direction == 0) /* Client -> Server */
|
||||
|| (packet->payload_packet_len == 0))
|
||||
return(1); /* More packets please */
|
||||
else if(flow->l4.tcp.tls_srv_cert_fingerprint_processed)
|
||||
return(0); /* We're good */
|
||||
|
||||
if(flow->l4.tcp.tls_fingerprint_len > 0) {
|
||||
unsigned int avail = packet->payload_packet_len - flow->l4.tcp.tls_record_offset;
|
||||
|
||||
if(avail > flow->l4.tcp.tls_fingerprint_len)
|
||||
avail = flow->l4.tcp.tls_fingerprint_len;
|
||||
|
||||
#ifdef DEBUG_TLS
|
||||
printf("=>> [TLS] Certificate record [%02X %02X %02X...][missing: %u][offset: %u][avail: %u] (B)\n",
|
||||
packet->payload[flow->l4.tcp.tls_record_offset],
|
||||
packet->payload[flow->l4.tcp.tls_record_offset+1],
|
||||
packet->payload[flow->l4.tcp.tls_record_offset+2],
|
||||
flow->l4.tcp.tls_fingerprint_len, flow->l4.tcp.tls_record_offset, avail
|
||||
);
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_CERTIFICATE_HASH
|
||||
for(i=0;i<avail;i++)
|
||||
printf("%02X ", packet->payload[flow->l4.tcp.tls_record_offset+i]);
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
SHA1Update(flow->l4.tcp.tls_srv_cert_fingerprint_ctx,
|
||||
&packet->payload[flow->l4.tcp.tls_record_offset],
|
||||
avail);
|
||||
|
||||
flow->l4.tcp.tls_fingerprint_len -= avail;
|
||||
|
||||
if(flow->l4.tcp.tls_fingerprint_len == 0) {
|
||||
SHA1Final(flow->l4.tcp.tls_sha1_certificate_fingerprint, flow->l4.tcp.tls_srv_cert_fingerprint_ctx);
|
||||
|
||||
#ifdef DEBUG_TLS
|
||||
{
|
||||
int i;
|
||||
|
||||
printf("=>> [TLS] SHA-1: ");
|
||||
for(i=0;i<20;i++)
|
||||
printf("%s%02X", (i > 0) ? ":" : "", flow->l4.tcp.tls_sha1_certificate_fingerprint[i]);
|
||||
printf("\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
flow->l4.tcp.tls_srv_cert_fingerprint_processed = 1;
|
||||
return(0); /* We're good */
|
||||
} else {
|
||||
flow->l4.tcp.tls_record_offset = 0;
|
||||
#ifdef DEBUG_TLS
|
||||
printf("=>> [TLS] Certificate record: still missing %u bytes\n", flow->l4.tcp.tls_fingerprint_len);
|
||||
#endif
|
||||
return(1); /* More packets please */
|
||||
}
|
||||
}
|
||||
|
||||
if(packet->payload[flow->l4.tcp.tls_record_offset] == 0x15 /* Alert */) {
|
||||
u_int len = ntohs(*(u_int16_t*)&packet->payload[flow->l4.tcp.tls_record_offset+3]) + 5 /* SSL header len */;
|
||||
|
||||
if(len < 10 /* Sanity check */) {
|
||||
if((flow->l4.tcp.tls_record_offset+len) < packet->payload_packet_len)
|
||||
flow->l4.tcp.tls_record_offset += len;
|
||||
} else
|
||||
goto invalid_len;
|
||||
}
|
||||
|
||||
multiple_messages = (packet->payload[flow->l4.tcp.tls_record_offset] == 0x16 /* Handshake */) ? 0 : 1;
|
||||
|
||||
#ifdef DEBUG_TLS
|
||||
printf("=>> [TLS] [multiple_messages: %d]\n", multiple_messages);
|
||||
#endif
|
||||
|
||||
if((!multiple_messages) && (packet->payload[flow->l4.tcp.tls_record_offset] != 0x16 /* Handshake */))
|
||||
return(1);
|
||||
else if(((!multiple_messages) && (packet->payload[flow->l4.tcp.tls_record_offset+5] == 0xb) /* Certificate */)
|
||||
|| (packet->payload[flow->l4.tcp.tls_record_offset] == 0xb) /* Certificate */) {
|
||||
/* TODO: Do not take into account all certificates but only the first one */
|
||||
#ifdef DEBUG_TLS
|
||||
printf("=>> [TLS] Certificate found\n");
|
||||
#endif
|
||||
|
||||
if(flow->l4.tcp.tls_srv_cert_fingerprint_ctx == NULL)
|
||||
flow->l4.tcp.tls_srv_cert_fingerprint_ctx = (void*)ndpi_malloc(sizeof(SHA1_CTX));
|
||||
else
|
||||
printf("[TLS] Internal error: double allocation\n:");
|
||||
|
||||
if(flow->l4.tcp.tls_srv_cert_fingerprint_ctx) {
|
||||
SHA1Init(flow->l4.tcp.tls_srv_cert_fingerprint_ctx);
|
||||
flow->l4.tcp.tls_srv_cert_fingerprint_found = 1;
|
||||
flow->l4.tcp.tls_record_offset += (!multiple_messages) ? 13 : 8;
|
||||
flow->l4.tcp.tls_fingerprint_len = ntohs(*(u_int16_t*)&packet->payload[flow->l4.tcp.tls_record_offset]);
|
||||
flow->l4.tcp.tls_record_offset = flow->l4.tcp.tls_record_offset+2;
|
||||
#ifdef DEBUG_TLS
|
||||
printf("=>> [TLS] Certificate [total certificate len: %u][certificate initial offset: %u]\n",
|
||||
flow->l4.tcp.tls_fingerprint_len, flow->l4.tcp.tls_record_offset);
|
||||
#endif
|
||||
return(getSSCertificateFingerprint(ndpi_struct, flow));
|
||||
} else
|
||||
return(0); /* That's all */
|
||||
} else if(flow->l4.tcp.tls_seen_certificate)
|
||||
return(0); /* That's all */
|
||||
else {
|
||||
/* This is a handshake but not a certificate record */
|
||||
u_int16_t len = ntohs(*(u_int16_t*)&packet->payload[flow->l4.tcp.tls_record_offset+7]);
|
||||
|
||||
#ifdef DEBUG_TLS
|
||||
printf("=>> [TLS] Found record %02X [len: %u]\n",
|
||||
packet->payload[flow->l4.tcp.tls_record_offset+5], len);
|
||||
#endif
|
||||
|
||||
if(len > 4096) {
|
||||
invalid_len:
|
||||
/* This looks an invalid len: we giveup */
|
||||
flow->l4.tcp.tls_record_offset = 0, flow->l4.tcp.tls_srv_cert_fingerprint_processed = 1;
|
||||
#ifdef DEBUG_TLS
|
||||
printf("=>> [TLS] Invalid fingerprint processing %u <-> %u\n",
|
||||
ntohs(packet->tcp->source), ntohs(packet->tcp->dest));
|
||||
#endif
|
||||
return(0);
|
||||
} else {
|
||||
flow->l4.tcp.tls_record_offset += len + 9;
|
||||
|
||||
if(flow->l4.tcp.tls_record_offset < packet->payload_packet_len)
|
||||
return(getSSCertificateFingerprint(ndpi_struct, flow));
|
||||
else {
|
||||
flow->l4.tcp.tls_record_offset -= packet->payload_packet_len;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return(1);
|
||||
}
|
||||
|
||||
/* **************************************** */
|
||||
|
||||
/* See https://blog.catchpoint.com/2017/05/12/dissecting-tls-using-wireshark/ */
|
||||
void getSSLorganization(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
struct ndpi_flow_struct *flow,
|
||||
char *buffer, int buffer_len) {
|
||||
struct ndpi_packet_struct *packet = &flow->packet;
|
||||
|
||||
u_int16_t total_len;
|
||||
u_int8_t handshake_protocol;
|
||||
|
||||
if(packet->payload[0] != 0x16 /* Handshake */)
|
||||
return;
|
||||
|
||||
u_int16_t total_len = (packet->payload[3] << 8) + packet->payload[4] + 5 /* SSL Header */;
|
||||
u_int8_t handshake_protocol = packet->payload[5]; /* handshake protocol a bit misleading, it is message type according TLS specs */
|
||||
|
||||
total_len = (packet->payload[3] << 8) + packet->payload[4] + 5 /* SSL Header */;
|
||||
handshake_protocol = packet->payload[5]; /* handshake protocol a bit misleading, it is message type according TLS specs */
|
||||
|
||||
if((handshake_protocol != 0x02)
|
||||
&& (handshake_protocol != 0xb) /* Server Hello and Certificate message types are interesting for us */)
|
||||
return;
|
||||
|
||||
#ifdef DEBUG_TLS
|
||||
printf("=>> [TLS] Certificate [total_len: %u/%u]\n", ntohs(*(u_int16_t*)&packet->payload[3]), total_len);
|
||||
#endif
|
||||
|
||||
/* Truncate total len, search at least in incomplete packet */
|
||||
if(total_len > packet->payload_packet_len)
|
||||
total_len = packet->payload_packet_len;
|
||||
@@ -759,19 +918,20 @@ void getSSLorganization(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
|
||||
if(len < (sizeof(utcDate)-1)) {
|
||||
struct tm utc;
|
||||
|
||||
|
||||
strncpy(utcDate, (const char*)&packet->payload[i+4], len);
|
||||
utcDate[len] = '\0';
|
||||
|
||||
/* 141021000000Z */
|
||||
/* 141021000000Z */
|
||||
if(strptime(utcDate, "%y%m%d%H%M%SZ", &utc) != NULL) {
|
||||
#ifdef DEBUG_TLS
|
||||
printf("[CERTIFICATE] notBefore %u [%s]\n", mktime(&utc), utcDate);
|
||||
printf("[CERTIFICATE] notBefore %u [%s]\n",
|
||||
(unsigned int)mktime(&utc), utcDate);
|
||||
#endif
|
||||
flow->protos.stun_ssl.ssl.notBefore = timegm(&utc);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset += len;
|
||||
|
||||
if((offset+1) < packet->payload_packet_len) {
|
||||
@@ -788,14 +948,15 @@ void getSSLorganization(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
|
||||
if(len < (sizeof(utcDate)-1)) {
|
||||
struct tm utc;
|
||||
|
||||
|
||||
strncpy(utcDate, (const char*)&packet->payload[offset], len);
|
||||
utcDate[len] = '\0';
|
||||
|
||||
/* 141021000000Z */
|
||||
|
||||
/* 141021000000Z */
|
||||
if(strptime(utcDate, "%y%m%d%H%M%SZ", &utc) != NULL) {
|
||||
#ifdef DEBUG_TLS
|
||||
printf("[CERTIFICATE] notAfter %u [%s]\n", mktime(&utc), utcDate);
|
||||
printf("[CERTIFICATE] notAfter %u [%s]\n",
|
||||
(unsigned int)mktime(&utc), utcDate);
|
||||
#endif
|
||||
flow->protos.stun_ssl.ssl.notAfter = timegm(&utc);
|
||||
}
|
||||
@@ -812,7 +973,14 @@ void getSSLorganization(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
int sslTryAndRetrieveServerCertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
struct ndpi_flow_struct *flow) {
|
||||
struct ndpi_packet_struct *packet = &flow->packet;
|
||||
|
||||
int rc = 1;
|
||||
|
||||
if(packet->tcp) {
|
||||
if(!flow->l4.tcp.tls_srv_cert_fingerprint_processed)
|
||||
getSSCertificateFingerprint(ndpi_struct, flow);
|
||||
}
|
||||
|
||||
#if 1
|
||||
/* consider only specific SSL packets (handshake) */
|
||||
if((packet->payload_packet_len > 9) && (packet->payload[0] == 0x16)) {
|
||||
char certificate[64];
|
||||
@@ -820,7 +988,7 @@ int sslTryAndRetrieveServerCertificate(struct ndpi_detection_module_struct *ndpi
|
||||
|
||||
certificate[0] = '\0';
|
||||
rc = getTLScertificate(ndpi_struct, flow, certificate, sizeof(certificate));
|
||||
packet->ssl_certificate_num_checks++;
|
||||
packet->tls_certificate_num_checks++;
|
||||
|
||||
if(rc > 0) {
|
||||
char organization[64];
|
||||
@@ -829,26 +997,34 @@ int sslTryAndRetrieveServerCertificate(struct ndpi_detection_module_struct *ndpi
|
||||
organization[0] = '\0';
|
||||
getSSLorganization(ndpi_struct, flow, organization, sizeof(organization));
|
||||
|
||||
packet->ssl_certificate_detected++;
|
||||
if((flow->l4.tcp.ssl_seen_server_cert == 1) && (flow->protos.stun_ssl.ssl.server_certificate[0] != '\0'))
|
||||
/* 0 means we're done processing extra packets (since we found what we wanted) */
|
||||
packet->tls_certificate_detected++;
|
||||
#if 0
|
||||
if((flow->l4.tcp.tls_seen_server_cert == 1)
|
||||
&& (flow->protos.stun_ssl.ssl.server_certificate[0] != '\0'))
|
||||
/* 0 means we've done processing extra packets (since we found what we wanted) */
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
if(flow->l4.tcp.tls_record_offset == 0) {
|
||||
/* Client hello, Server Hello, and certificate packets probably all checked in this case */
|
||||
if(((packet->ssl_certificate_num_checks >= 3)
|
||||
&& (flow->l4.tcp.seen_syn)
|
||||
&& (flow->l4.tcp.seen_syn_ack)
|
||||
&& (flow->l4.tcp.seen_ack) /* We have seen the 3-way handshake */)
|
||||
|| (flow->protos.stun_ssl.ssl.ja3_server[0] != '\0')
|
||||
) {
|
||||
/* We're done processing extra packets since we've probably checked all possible cert packets */
|
||||
return 0;
|
||||
if(((packet->tls_certificate_num_checks >= 3)
|
||||
&& (flow->l4.tcp.seen_syn)
|
||||
&& (flow->l4.tcp.seen_syn_ack)
|
||||
&& (flow->l4.tcp.seen_ack) /* We have seen the 3-way handshake */
|
||||
&& flow->l4.tcp.tls_srv_cert_fingerprint_processed)
|
||||
/* || (flow->protos.stun_ssl.ssl.ja3_server[0] != '\0') */
|
||||
) {
|
||||
/* We're done processing extra packets since we've probably checked all possible cert packets */
|
||||
return(rc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* 1 means keep looking for more packets */
|
||||
return 1;
|
||||
if(!flow->l4.tcp.tls_srv_cert_fingerprint_processed) rc = 1;
|
||||
return(rc);
|
||||
}
|
||||
|
||||
/* **************************************** */
|
||||
@@ -866,9 +1042,15 @@ void sslInitExtraPacketProcessing(int caseNum, struct ndpi_flow_struct *flow) {
|
||||
/* **************************************** */
|
||||
|
||||
int tlsDetectProtocolFromCertificate(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
struct ndpi_flow_struct *flow) {
|
||||
struct ndpi_flow_struct *flow,
|
||||
u_int8_t skip_cert_processing) {
|
||||
struct ndpi_packet_struct *packet = &flow->packet;
|
||||
|
||||
if((!skip_cert_processing) && packet->tcp) {
|
||||
if(!flow->l4.tcp.tls_srv_cert_fingerprint_processed)
|
||||
getSSCertificateFingerprint(ndpi_struct, flow);
|
||||
}
|
||||
|
||||
if((packet->payload_packet_len > 9)
|
||||
&& (packet->payload[0] == 0x16 /* consider only specific SSL packets (handshake) */)) {
|
||||
if((packet->detected_protocol_stack[0] == NDPI_PROTOCOL_UNKNOWN)
|
||||
@@ -878,10 +1060,10 @@ int tlsDetectProtocolFromCertificate(struct ndpi_detection_module_struct *ndpi_s
|
||||
|
||||
certificate[0] = '\0';
|
||||
rc = getTLScertificate(ndpi_struct, flow, certificate, sizeof(certificate));
|
||||
packet->ssl_certificate_num_checks++;
|
||||
packet->tls_certificate_num_checks++;
|
||||
|
||||
if(rc > 0) {
|
||||
packet->ssl_certificate_detected++;
|
||||
packet->tls_certificate_detected++;
|
||||
#ifdef DEBUG_TLS
|
||||
NDPI_LOG_DBG2(ndpi_struct, "***** [SSL] %s\n", certificate);
|
||||
#endif
|
||||
@@ -895,8 +1077,8 @@ int tlsDetectProtocolFromCertificate(struct ndpi_detection_module_struct *ndpi_s
|
||||
/* If we've detected the subprotocol from client certificate but haven't had a chance
|
||||
* to see the server certificate yet, set up extra packet processing to wait
|
||||
* a few more packets. */
|
||||
if(((flow->l4.tcp.ssl_seen_client_cert == 1) && (flow->protos.stun_ssl.ssl.client_certificate[0] != '\0'))
|
||||
&& ((flow->l4.tcp.ssl_seen_server_cert != 1) && (flow->protos.stun_ssl.ssl.server_certificate[0] == '\0'))) {
|
||||
if(((flow->l4.tcp.tls_seen_client_cert == 1) && (flow->protos.stun_ssl.ssl.client_certificate[0] != '\0'))
|
||||
&& ((flow->l4.tcp.tls_seen_server_cert != 1) && (flow->protos.stun_ssl.ssl.server_certificate[0] == '\0'))) {
|
||||
sslInitExtraPacketProcessing(0, flow);
|
||||
}
|
||||
|
||||
@@ -909,30 +1091,40 @@ int tlsDetectProtocolFromCertificate(struct ndpi_detection_module_struct *ndpi_s
|
||||
return(rc);
|
||||
}
|
||||
|
||||
if(((packet->ssl_certificate_num_checks >= 3)
|
||||
if(((packet->tls_certificate_num_checks >= 3)
|
||||
&& flow->l4.tcp.seen_syn
|
||||
&& flow->l4.tcp.seen_syn_ack
|
||||
&& flow->l4.tcp.seen_ack /* We have seen the 3-way handshake */)
|
||||
|| ((flow->l4.tcp.ssl_seen_certificate == 1)
|
||||
&& (flow->l4.tcp.ssl_seen_server_cert == 1)
|
||||
&& flow->l4.tcp.seen_ack /* We have seen the 3-way handshake */
|
||||
&& flow->l4.tcp.tls_srv_cert_fingerprint_processed
|
||||
)
|
||||
/*
|
||||
|| ((flow->l4.tcp.tls_seen_certificate == 1)
|
||||
&& (flow->l4.tcp.tls_seen_server_cert == 1)
|
||||
&& (flow->protos.stun_ssl.ssl.server_certificate[0] != '\0'))
|
||||
/* || ((flow->l4.tcp.ssl_seen_client_cert == 1) && (flow->protos.stun_ssl.ssl.client_certificate[0] != '\0')) */
|
||||
*/
|
||||
/* || ((flow->l4.tcp.tls_seen_client_cert == 1) && (flow->protos.stun_ssl.ssl.client_certificate[0] != '\0')) */
|
||||
) {
|
||||
ndpi_int_tls_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_TLS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return(0);
|
||||
}
|
||||
|
||||
/* **************************************** */
|
||||
|
||||
static void tls_mark_and_payload_search_for_other_protocols(struct ndpi_detection_module_struct
|
||||
*ndpi_struct, struct ndpi_flow_struct *flow) {
|
||||
static void tls_mark_and_payload_search(struct ndpi_detection_module_struct
|
||||
*ndpi_struct, struct ndpi_flow_struct *flow,
|
||||
u_int8_t skip_cert_processing) {
|
||||
struct ndpi_packet_struct *packet = &flow->packet;
|
||||
u_int32_t a;
|
||||
u_int32_t end;
|
||||
|
||||
#ifdef DEBUG_TLS
|
||||
printf("[TLS] %s()\n", __FUNCTION__);
|
||||
#endif
|
||||
|
||||
if(NDPI_COMPARE_PROTOCOL_TO_BITMASK(ndpi_struct->detection_bitmask, NDPI_PROTOCOL_UNENCRYPTED_JABBER) != 0)
|
||||
goto check_for_tls_payload;
|
||||
|
||||
@@ -996,14 +1188,15 @@ static void tls_mark_and_payload_search_for_other_protocols(struct ndpi_detectio
|
||||
no_check_for_tls_payload:
|
||||
if(packet->detected_protocol_stack[0] == NDPI_PROTOCOL_UNKNOWN) {
|
||||
NDPI_LOG_DBG(ndpi_struct, "found ssl connection\n");
|
||||
tlsDetectProtocolFromCertificate(ndpi_struct, flow);
|
||||
|
||||
if(!packet->ssl_certificate_detected
|
||||
&& (!(flow->l4.tcp.ssl_seen_client_cert && flow->l4.tcp.ssl_seen_server_cert))) {
|
||||
tlsDetectProtocolFromCertificate(ndpi_struct, flow, skip_cert_processing);
|
||||
|
||||
if(!packet->tls_certificate_detected
|
||||
&& (!(flow->l4.tcp.tls_seen_client_cert && flow->l4.tcp.tls_seen_server_cert))) {
|
||||
/* SSL without certificate (Skype, Ultrasurf?) */
|
||||
NDPI_LOG_INFO(ndpi_struct, "found ssl NO_CERT\n");
|
||||
ndpi_int_tls_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_TLS_NO_CERT);
|
||||
} else if(packet->ssl_certificate_num_checks >= 3) {
|
||||
ndpi_int_tls_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_TLS);
|
||||
} else if((packet->tls_certificate_num_checks >= 3)
|
||||
&& flow->l4.tcp.tls_srv_cert_fingerprint_processed) {
|
||||
NDPI_LOG_INFO(ndpi_struct, "found ssl\n");
|
||||
ndpi_int_tls_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_TLS);
|
||||
}
|
||||
@@ -1120,14 +1313,14 @@ static u_int8_t ndpi_search_tlsv3_direction1(struct ndpi_detection_module_struct
|
||||
void ndpi_search_tls_tcp_udp(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
struct ndpi_flow_struct *flow) {
|
||||
struct ndpi_packet_struct *packet = &flow->packet;
|
||||
u_int8_t ret;
|
||||
u_int8_t ret, skip_cert_processing = 0;
|
||||
|
||||
if(packet->udp != NULL) {
|
||||
/* DTLS dissector */
|
||||
int rc = sslTryAndRetrieveServerCertificate(ndpi_struct, flow);
|
||||
|
||||
#ifdef DEBUG_TLS
|
||||
printf("==>> %u [rc: %u][len: %u][%s][version: %u]\n",
|
||||
printf("==>> %u [rc: %d][len: %u][%s][version: %u]\n",
|
||||
flow->guessed_host_protocol_id, rc, packet->payload_packet_len, flow->protos.stun_ssl.ssl.ja3_server,
|
||||
flow->protos.stun_ssl.ssl.ssl_version);
|
||||
#endif
|
||||
@@ -1142,8 +1335,10 @@ void ndpi_search_tls_tcp_udp(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
ndpi_struct->stun_cache = ndpi_lru_cache_init(1024);
|
||||
ndpi_lru_add_to_cache(ndpi_struct->stun_cache, key, NDPI_PROTOCOL_SIGNAL);
|
||||
|
||||
#ifdef DEBUG_TLS
|
||||
printf("[LRU] Adding Signal cached key %u\n", key);
|
||||
|
||||
#endif
|
||||
|
||||
/* In Signal protocol STUN turns into DTLS... */
|
||||
ndpi_int_tls_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_SIGNAL);
|
||||
} else if(flow->protos.stun_ssl.ssl.ja3_server[0] != '\0') {
|
||||
@@ -1156,13 +1351,13 @@ void ndpi_search_tls_tcp_udp(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
}
|
||||
|
||||
if(packet->detected_protocol_stack[0] == NDPI_PROTOCOL_TLS) {
|
||||
if(flow->l4.tcp.ssl_stage == 3 && packet->payload_packet_len > 20 && flow->packet_counter < 5) {
|
||||
if(flow->l4.tcp.tls_stage == 3 && packet->payload_packet_len > 20 && flow->packet_counter < 5) {
|
||||
/* this should only happen, when we detected SSL with a packet that had parts of the certificate in subsequent packets
|
||||
* so go on checking for certificate patterns for a couple more packets
|
||||
*/
|
||||
NDPI_LOG_DBG2(ndpi_struct,
|
||||
"ssl flow but check another packet for patterns\n");
|
||||
tls_mark_and_payload_search_for_other_protocols(ndpi_struct, flow);
|
||||
tls_mark_and_payload_search(ndpi_struct, flow, skip_cert_processing);
|
||||
|
||||
if(packet->detected_protocol_stack[0] == NDPI_PROTOCOL_TLS) {
|
||||
/* still ssl so check another packet */
|
||||
@@ -1194,18 +1389,20 @@ void ndpi_search_tls_tcp_udp(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
return;
|
||||
} else {
|
||||
/* No whatsapp, let's try SSL */
|
||||
if(tlsDetectProtocolFromCertificate(ndpi_struct, flow) > 0)
|
||||
if(tlsDetectProtocolFromCertificate(ndpi_struct, flow, skip_cert_processing) > 0)
|
||||
return;
|
||||
else
|
||||
skip_cert_processing = 1;
|
||||
}
|
||||
|
||||
if(packet->payload_packet_len > 40 && flow->l4.tcp.ssl_stage == 0) {
|
||||
if(packet->payload_packet_len > 40 && flow->l4.tcp.tls_stage == 0) {
|
||||
NDPI_LOG_DBG2(ndpi_struct, "first ssl packet\n");
|
||||
// SSLv2 Record
|
||||
if(packet->payload[2] == 0x01 && packet->payload[3] == 0x03
|
||||
&& (packet->payload[4] == 0x00 || packet->payload[4] == 0x01 || packet->payload[4] == 0x02)
|
||||
&& (packet->payload_packet_len - packet->payload[1] == 2)) {
|
||||
NDPI_LOG_DBG2(ndpi_struct, "sslv2 len match\n");
|
||||
flow->l4.tcp.ssl_stage = 1 + packet->packet_direction;
|
||||
flow->l4.tcp.tls_stage = 1 + packet->packet_direction;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1214,7 +1411,7 @@ void ndpi_search_tls_tcp_udp(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
&& (packet->payload_packet_len - ntohs(get_u_int16_t(packet->payload, 3)) == 5)) {
|
||||
// SSLv3 Record
|
||||
NDPI_LOG_DBG2(ndpi_struct, "sslv3 len match\n");
|
||||
flow->l4.tcp.ssl_stage = 1 + packet->packet_direction;
|
||||
flow->l4.tcp.tls_stage = 1 + packet->packet_direction;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1224,41 +1421,41 @@ void ndpi_search_tls_tcp_udp(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
packet->payload[2] == 0x02 || packet->payload[2] == 0x03)) {
|
||||
if(packet->payload_packet_len - ntohs(get_u_int16_t(packet->payload, 3)) == 5) {
|
||||
NDPI_LOG_DBG2(ndpi_struct, "TLS len match\n");
|
||||
flow->l4.tcp.ssl_stage = 1 + packet->packet_direction;
|
||||
flow->l4.tcp.tls_stage = 1 + packet->packet_direction;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(packet->payload_packet_len > 40 &&
|
||||
flow->l4.tcp.ssl_stage == 1 + packet->packet_direction
|
||||
flow->l4.tcp.tls_stage == 1 + packet->packet_direction
|
||||
&& flow->packet_direction_counter[packet->packet_direction] < 5) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(packet->payload_packet_len > 40 && flow->l4.tcp.ssl_stage == 2 - packet->packet_direction) {
|
||||
if(packet->payload_packet_len > 40 && flow->l4.tcp.tls_stage == 2 - packet->packet_direction) {
|
||||
NDPI_LOG_DBG2(ndpi_struct, "second ssl packet\n");
|
||||
// SSLv2 Record
|
||||
if(packet->payload[2] == 0x01 && packet->payload[3] == 0x03
|
||||
&& (packet->payload[4] == 0x00 || packet->payload[4] == 0x01 || packet->payload[4] == 0x02)
|
||||
&& (packet->payload_packet_len - 2) >= packet->payload[1]) {
|
||||
NDPI_LOG_DBG2(ndpi_struct, "sslv2 server len match\n");
|
||||
tls_mark_and_payload_search_for_other_protocols(ndpi_struct, flow);
|
||||
tls_mark_and_payload_search(ndpi_struct, flow, skip_cert_processing);
|
||||
return;
|
||||
}
|
||||
|
||||
ret = ndpi_search_tlsv3_direction1(ndpi_struct, flow);
|
||||
if(ret == 1) {
|
||||
NDPI_LOG_DBG2(ndpi_struct, "sslv3 server len match\n");
|
||||
tls_mark_and_payload_search_for_other_protocols(ndpi_struct, flow);
|
||||
tls_mark_and_payload_search(ndpi_struct, flow, skip_cert_processing);
|
||||
return;
|
||||
} else if(ret == 2) {
|
||||
NDPI_LOG_DBG2(ndpi_struct,
|
||||
"sslv3 server len match with split packet -> check some more packets for SSL patterns\n");
|
||||
tls_mark_and_payload_search_for_other_protocols(ndpi_struct, flow);
|
||||
if(packet->detected_protocol_stack[0] == NDPI_PROTOCOL_TLS) {
|
||||
flow->l4.tcp.ssl_stage = 3;
|
||||
}
|
||||
tls_mark_and_payload_search(ndpi_struct, flow, skip_cert_processing);
|
||||
|
||||
if(packet->detected_protocol_stack[0] == NDPI_PROTOCOL_TLS)
|
||||
flow->l4.tcp.tls_stage = 3;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ void ndpi_search_tor(struct ndpi_detection_module_struct *ndpi_struct, struct nd
|
||||
NDPI_LOG_DBG(ndpi_struct, "search for TOR\n");
|
||||
|
||||
if((packet->tcp != NULL)
|
||||
&& (!packet->ssl_certificate_detected)) {
|
||||
&& (!packet->tls_certificate_detected)) {
|
||||
u_int16_t dport, sport;
|
||||
|
||||
sport = ntohs(packet->tcp->source), dport = ntohs(packet->tcp->dest);
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
/* ================ sha1.h ================ */
|
||||
/*
|
||||
SHA-1 in C
|
||||
By Steve Reid <steve@edmweb.com>
|
||||
100% Public Domain
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
u_int32_t state[5];
|
||||
u_int32_t count[2];
|
||||
unsigned char buffer[64];
|
||||
} SHA1_CTX;
|
||||
|
||||
void SHA1Transform(u_int32_t state[5], const unsigned char buffer[64]);
|
||||
void SHA1Init(SHA1_CTX* context);
|
||||
void SHA1Update(SHA1_CTX* context, const unsigned char* data, u_int32_t len);
|
||||
void SHA1Final(unsigned char digest[20], SHA1_CTX* context);
|
||||
Vendored
+276
@@ -0,0 +1,276 @@
|
||||
|
||||
/* from valgrind tests */
|
||||
|
||||
/* ================ sha1.c ================ */
|
||||
/*
|
||||
SHA-1 in C
|
||||
By Steve Reid <steve@edmweb.com>
|
||||
100% Public Domain
|
||||
|
||||
Test Vectors (from FIPS PUB 180-1)
|
||||
"abc"
|
||||
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
|
||||
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
|
||||
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
|
||||
A million repetitions of "a"
|
||||
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
|
||||
*/
|
||||
|
||||
/* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
|
||||
/* #define SHA1HANDSOFF * Copies data before messing with it. */
|
||||
|
||||
#define SHA1HANDSOFF
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h> /* for u_int*_t */
|
||||
#if defined(__sun)
|
||||
#include "solarisfixes.h"
|
||||
#endif
|
||||
#include "ndpi_sha1.h"
|
||||
|
||||
#ifndef BYTE_ORDER
|
||||
#if (BSD >= 199103)
|
||||
# include <machine/endian.h>
|
||||
#else
|
||||
#if defined(linux) || defined(__linux__)
|
||||
# include <endian.h>
|
||||
#else
|
||||
#define LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */
|
||||
#define BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */
|
||||
#define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp)*/
|
||||
|
||||
#if defined(vax) || defined(ns32000) || defined(sun386) || defined(__i386__) || \
|
||||
defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \
|
||||
defined(__alpha__) || defined(__alpha)
|
||||
#define BYTE_ORDER LITTLE_ENDIAN
|
||||
#endif
|
||||
|
||||
#if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || \
|
||||
defined(is68k) || defined(tahoe) || defined(ibm032) || defined(ibm370) || \
|
||||
defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX) ||\
|
||||
defined(apollo) || defined(__convex__) || defined(_CRAY) || \
|
||||
defined(__hppa) || defined(__hp9000) || \
|
||||
defined(__hp9000s300) || defined(__hp9000s700) || \
|
||||
defined (BIT_ZERO_ON_LEFT) || defined(m68k) || defined(__sparc)
|
||||
#define BYTE_ORDER BIG_ENDIAN
|
||||
#endif
|
||||
#endif /* linux */
|
||||
#endif /* BSD */
|
||||
#endif /* BYTE_ORDER */
|
||||
|
||||
#if defined(__BYTE_ORDER) && !defined(BYTE_ORDER)
|
||||
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
|
||||
#define BYTE_ORDER LITTLE_ENDIAN
|
||||
#else
|
||||
#define BYTE_ORDER BIG_ENDIAN
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if !defined(BYTE_ORDER) || \
|
||||
(BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN && \
|
||||
BYTE_ORDER != PDP_ENDIAN)
|
||||
/* you must determine what the correct bit order is for
|
||||
* your compiler - the next line is an intentional error
|
||||
* which will force your compiles to bomb until you fix
|
||||
* the above macros.
|
||||
*/
|
||||
#error "Undefined or invalid BYTE_ORDER"
|
||||
#endif
|
||||
|
||||
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
|
||||
|
||||
/* blk0() and blk() perform the initial expand. */
|
||||
/* I got the idea of expanding during the round function from SSLeay */
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
|
||||
|(rol(block->l[i],8)&0x00FF00FF))
|
||||
#elif BYTE_ORDER == BIG_ENDIAN
|
||||
#define blk0(i) block->l[i]
|
||||
#else
|
||||
#error "Endianness not defined!"
|
||||
#endif
|
||||
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
|
||||
^block->l[(i+2)&15]^block->l[i&15],1))
|
||||
|
||||
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
|
||||
#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
|
||||
#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
|
||||
#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
|
||||
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
|
||||
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
|
||||
|
||||
|
||||
/* Hash a single 512-bit block. This is the core of the algorithm. */
|
||||
|
||||
void SHA1Transform(u_int32_t state[5], const unsigned char buffer[64])
|
||||
{
|
||||
u_int32_t a, b, c, d, e;
|
||||
typedef union {
|
||||
unsigned char c[64];
|
||||
u_int32_t l[16];
|
||||
} CHAR64LONG16;
|
||||
#ifdef SHA1HANDSOFF
|
||||
CHAR64LONG16 block[1]; /* use array to appear as a pointer */
|
||||
memcpy(block, buffer, 64);
|
||||
#else
|
||||
/* The following had better never be used because it causes the
|
||||
* pointer-to-const buffer to be cast into a pointer to non-const.
|
||||
* And the result is written through. I threw a "const" in, hoping
|
||||
* this will cause a diagnostic.
|
||||
*/
|
||||
CHAR64LONG16* block = (const CHAR64LONG16*)buffer;
|
||||
#endif
|
||||
/* Copy context->state[] to working vars */
|
||||
a = state[0];
|
||||
b = state[1];
|
||||
c = state[2];
|
||||
d = state[3];
|
||||
e = state[4];
|
||||
/* 4 rounds of 20 operations each. Loop unrolled. */
|
||||
R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
|
||||
R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
|
||||
R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
|
||||
R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
|
||||
R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
|
||||
R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
|
||||
R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
|
||||
R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
|
||||
R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
|
||||
R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
|
||||
R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
|
||||
R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
|
||||
R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
|
||||
R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
|
||||
R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
|
||||
R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
|
||||
R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
|
||||
R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
|
||||
R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
|
||||
R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
|
||||
/* Add the working vars back into context.state[] */
|
||||
state[0] += a;
|
||||
state[1] += b;
|
||||
state[2] += c;
|
||||
state[3] += d;
|
||||
state[4] += e;
|
||||
/* Wipe variables */
|
||||
a = b = c = d = e = 0;
|
||||
#ifdef SHA1HANDSOFF
|
||||
memset(block, '\0', sizeof(block));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/* SHA1Init - Initialize new context */
|
||||
|
||||
void SHA1Init(SHA1_CTX* context)
|
||||
{
|
||||
/* SHA1 initialization constants */
|
||||
context->state[0] = 0x67452301;
|
||||
context->state[1] = 0xEFCDAB89;
|
||||
context->state[2] = 0x98BADCFE;
|
||||
context->state[3] = 0x10325476;
|
||||
context->state[4] = 0xC3D2E1F0;
|
||||
context->count[0] = context->count[1] = 0;
|
||||
}
|
||||
|
||||
|
||||
/* Run your data through this. */
|
||||
|
||||
void SHA1Update(SHA1_CTX* context, const unsigned char* data, u_int32_t len)
|
||||
{
|
||||
u_int32_t i;
|
||||
u_int32_t j;
|
||||
|
||||
j = context->count[0];
|
||||
if ((context->count[0] += len << 3) < j)
|
||||
context->count[1]++;
|
||||
context->count[1] += (len>>29);
|
||||
j = (j >> 3) & 63;
|
||||
if ((j + len) > 63) {
|
||||
memcpy(&context->buffer[j], data, (i = 64-j));
|
||||
SHA1Transform(context->state, context->buffer);
|
||||
for ( ; i + 63 < len; i += 64) {
|
||||
SHA1Transform(context->state, &data[i]);
|
||||
}
|
||||
j = 0;
|
||||
}
|
||||
else i = 0;
|
||||
memcpy(&context->buffer[j], &data[i], len - i);
|
||||
}
|
||||
|
||||
|
||||
/* Add padding and return the message digest. */
|
||||
|
||||
void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
|
||||
{
|
||||
unsigned i;
|
||||
unsigned char finalcount[8];
|
||||
unsigned char c;
|
||||
|
||||
#if 0 /* untested "improvement" by DHR */
|
||||
/* Convert context->count to a sequence of bytes
|
||||
* in finalcount. Second element first, but
|
||||
* big-endian order within element.
|
||||
* But we do it all backwards.
|
||||
*/
|
||||
unsigned char *fcp = &finalcount[8];
|
||||
|
||||
for (i = 0; i < 2; i++)
|
||||
{
|
||||
u_int32_t t = context->count[i];
|
||||
int j;
|
||||
|
||||
for (j = 0; j < 4; t >>= 8, j++)
|
||||
*--fcp = (unsigned char) t
|
||||
}
|
||||
#else
|
||||
for (i = 0; i < 8; i++) {
|
||||
finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
|
||||
>> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
|
||||
}
|
||||
#endif
|
||||
c = 0200;
|
||||
SHA1Update(context, &c, 1);
|
||||
while ((context->count[0] & 504) != 448) {
|
||||
c = 0000;
|
||||
SHA1Update(context, &c, 1);
|
||||
}
|
||||
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
|
||||
for (i = 0; i < 20; i++) {
|
||||
digest[i] = (unsigned char)
|
||||
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
|
||||
}
|
||||
/* Wipe variables */
|
||||
memset(context, '\0', sizeof(*context));
|
||||
memset(&finalcount, '\0', sizeof(finalcount));
|
||||
}
|
||||
/* ================ end of sha1.c ================ */
|
||||
|
||||
#if 0
|
||||
#define BUFSIZE 4096
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
SHA1_CTX ctx;
|
||||
unsigned char hash[20], buf[BUFSIZE];
|
||||
int i;
|
||||
|
||||
for(i=0;i<BUFSIZE;i++)
|
||||
buf[i] = i;
|
||||
|
||||
SHA1Init(&ctx);
|
||||
for(i=0;i<1000;i++)
|
||||
SHA1Update(&ctx, buf, BUFSIZE);
|
||||
SHA1Final(hash, &ctx);
|
||||
|
||||
printf("SHA1=");
|
||||
for(i=0;i<20;i++)
|
||||
printf("%02x", hash[i]);
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user