2019-05-16 20:33:42 +08:00
|
|
|
#include <ssl_service_cache.h>
|
2023-03-30 19:39:18 +08:00
|
|
|
#include <MESA/maat.h>
|
2020-12-25 21:52:14 +06:00
|
|
|
#include <tfe_resource.h>
|
|
|
|
|
#include <ssl_stream.h>
|
|
|
|
|
|
|
|
|
|
struct ssl_ja3_enforcer
|
|
|
|
|
{
|
2023-03-30 19:39:18 +08:00
|
|
|
struct maat *maat;
|
2020-12-25 21:52:14 +06:00
|
|
|
int table_id;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ssl_svc_ja3
|
|
|
|
|
{
|
|
|
|
|
char ja3_hash[33];
|
|
|
|
|
int fingerprint_id;
|
|
|
|
|
int pinning_state;
|
|
|
|
|
int is_valid;
|
|
|
|
|
int ref_cnt;
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-28 18:01:32 +08:00
|
|
|
struct ssl_svc_addr
|
|
|
|
|
{
|
|
|
|
|
const char *sip;
|
|
|
|
|
const char *sport;
|
|
|
|
|
const char *dip;
|
|
|
|
|
const char *dport;
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-25 21:52:14 +06:00
|
|
|
static struct ssl_ja3_enforcer g_static_enforcer = {0};
|
|
|
|
|
|
2023-03-30 19:39:18 +08:00
|
|
|
static void ssl_svc_ja3_param_dup_cb(int table_id, void **to, void **from, long argl, void *argp)
|
2020-12-25 21:52:14 +06:00
|
|
|
{
|
|
|
|
|
struct ssl_svc_ja3 *param = (struct ssl_svc_ja3 *)*from;
|
|
|
|
|
if (param)
|
|
|
|
|
{
|
|
|
|
|
__sync_add_and_fetch(&(param->ref_cnt), 1);
|
|
|
|
|
*to = param;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*to = NULL;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-30 19:39:18 +08:00
|
|
|
static void ssl_svc_ja3_param_new_cb(const char *table_name, int table_id, const char *key, const char *table_line, void **ad, long argl, void *argp)
|
2020-12-25 21:52:14 +06:00
|
|
|
{
|
|
|
|
|
int is_valid = 0;
|
|
|
|
|
int pinning_state = 0;
|
|
|
|
|
int fingerprint_id = 0;
|
|
|
|
|
char ja3_hash[33] = {0};
|
|
|
|
|
|
|
|
|
|
if (sscanf(table_line, "%d\t%s\t%d\t%d", &fingerprint_id, ja3_hash, &pinning_state, &is_valid) != 4)
|
|
|
|
|
{
|
|
|
|
|
TFE_LOG_ERROR(g_default_logger, "Invalid JA3 policy: %s", table_line);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ssl_svc_ja3 *param = ALLOC(struct ssl_svc_ja3, 1);
|
|
|
|
|
param->fingerprint_id = fingerprint_id;
|
|
|
|
|
memcpy(param->ja3_hash, ja3_hash, 32);
|
|
|
|
|
param->pinning_state = pinning_state;
|
|
|
|
|
param->is_valid = is_valid;
|
|
|
|
|
param->ref_cnt = 1;
|
|
|
|
|
|
|
|
|
|
*ad = param;
|
|
|
|
|
TFE_LOG_INFO(g_default_logger, "Add JA3 policy: id:%d, ja3_hash:%s, pinning_state:%d, is_valid:%d, ref_cnt:%d",
|
|
|
|
|
param->fingerprint_id, param->ja3_hash, param->pinning_state, param->is_valid, param->ref_cnt);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-30 19:39:18 +08:00
|
|
|
static void ssl_svc_ja3_param_free_cb(int table_id, void **ad, long argl, void *argp)
|
2020-12-25 21:52:14 +06:00
|
|
|
{
|
|
|
|
|
struct ssl_svc_ja3 *param = (struct ssl_svc_ja3 *)*ad;
|
|
|
|
|
if (param == NULL)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((__sync_sub_and_fetch(¶m->ref_cnt, 1) == 0))
|
|
|
|
|
{
|
|
|
|
|
TFE_LOG_INFO(g_default_logger, "Del JA3 policy: id:%d, ja3_hash:%s, pinning_state:%d, is_valid:%d, ref_cnt:%d",
|
|
|
|
|
param->fingerprint_id, param->ja3_hash, param->pinning_state, param->is_valid, param->ref_cnt);
|
|
|
|
|
free(param);
|
|
|
|
|
*ad = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ssl_svc_ja3_param_free(struct ssl_svc_ja3 *param)
|
|
|
|
|
{
|
|
|
|
|
ssl_svc_ja3_param_free_cb(0, (void **)¶m, 0, NULL);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int ssl_svc_ja3_init(const char *table_name)
|
|
|
|
|
{
|
2023-03-30 19:39:18 +08:00
|
|
|
g_static_enforcer.maat = (struct maat *)tfe_bussiness_resouce_get(STATIC_MAAT);
|
|
|
|
|
g_static_enforcer.table_id = maat_get_table_id(g_static_enforcer.maat, table_name);
|
2020-12-25 21:52:14 +06:00
|
|
|
if (g_static_enforcer.table_id < 0)
|
|
|
|
|
{
|
|
|
|
|
TFE_LOG_ERROR(g_default_logger, "Maat table %s register failed.", table_name);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-03-30 19:39:18 +08:00
|
|
|
int ret = maat_plugin_table_ex_schema_register(g_static_enforcer.maat,
|
|
|
|
|
table_name,
|
|
|
|
|
ssl_svc_ja3_param_new_cb,
|
|
|
|
|
ssl_svc_ja3_param_free_cb,
|
|
|
|
|
ssl_svc_ja3_param_dup_cb,
|
|
|
|
|
0,
|
|
|
|
|
&g_static_enforcer);
|
2020-12-25 21:52:14 +06:00
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
TFE_LOG_ERROR(g_default_logger, "failed at Maat_plugin_EX_register(%s), table_id = %d, ret = %d",
|
|
|
|
|
table_name, g_static_enforcer.table_id, ret);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 18:01:32 +08:00
|
|
|
enum ssl_ja3_pinning_status ssl_svc_ja3_scan(char *ja3_hash, const char *addr_str)
|
2020-12-25 21:52:14 +06:00
|
|
|
{
|
|
|
|
|
enum ssl_ja3_pinning_status ret = JA3_PINNING_STATUS_UNKNOWN;
|
|
|
|
|
struct ssl_svc_ja3 *param = NULL;
|
2023-03-30 19:39:18 +08:00
|
|
|
|
|
|
|
|
param = (struct ssl_svc_ja3 *)maat_plugin_table_get_ex_data(g_static_enforcer.maat, g_static_enforcer.table_id, ja3_hash);
|
2020-12-25 21:52:14 +06:00
|
|
|
if (param == NULL)
|
|
|
|
|
{
|
|
|
|
|
ret = JA3_PINNING_STATUS_UNKNOWN;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
TFE_LOG_INFO(g_default_logger, "Hit JA3 policy: id:%d, ja3_hash:%s, pinning_state:%d, is_valid:%d, ref_cnt:%d, addr:%s",
|
|
|
|
|
param->fingerprint_id, param->ja3_hash, param->pinning_state, param->is_valid, param->ref_cnt, addr_str);
|
|
|
|
|
|
|
|
|
|
if (!param->is_valid)
|
|
|
|
|
{
|
|
|
|
|
ret = JA3_PINNING_STATUS_UNKNOWN;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1 - pinning
|
|
|
|
|
if (param->pinning_state)
|
|
|
|
|
{
|
|
|
|
|
ret = JA3_PINNING_STATUS_IS_PINNING;
|
|
|
|
|
}
|
|
|
|
|
// 0 - not pinning
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ret = JA3_PINNING_STATUS_NOT_PINNING;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
if (param)
|
|
|
|
|
{
|
|
|
|
|
ssl_svc_ja3_param_free(param);
|
|
|
|
|
param = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2019-05-16 20:33:42 +08:00
|
|
|
|
2019-05-20 16:56:37 +08:00
|
|
|
struct ssl_svc_client_st
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
|
|
|
|
time_t last_update_time;
|
2019-05-21 11:47:09 +08:00
|
|
|
unsigned int suspect_pinning_count;
|
|
|
|
|
unsigned int protocol_error_count;
|
2019-05-16 20:33:42 +08:00
|
|
|
char is_mutual_auth;
|
2019-05-20 16:56:37 +08:00
|
|
|
struct ssl_service_cache* ref_svc_cache;
|
2019-05-16 20:33:42 +08:00
|
|
|
};
|
2019-05-20 16:56:37 +08:00
|
|
|
struct ssl_svc_server_st
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
|
|
|
|
char is_ev;
|
|
|
|
|
char is_ct;
|
|
|
|
|
long long ev_st_switched;
|
|
|
|
|
long long ct_st_switched;
|
2019-05-20 16:56:37 +08:00
|
|
|
struct ssl_service_cache* ref_svc_cache;
|
2019-05-16 20:33:42 +08:00
|
|
|
};
|
|
|
|
|
struct ssl_service_write_args
|
|
|
|
|
{
|
|
|
|
|
struct ssl_service_cache* cache;
|
|
|
|
|
const struct ssl_service_status* status;
|
|
|
|
|
};
|
2019-05-20 16:56:37 +08:00
|
|
|
static void ssl_svc_free_client_st(void * data)
|
|
|
|
|
{
|
|
|
|
|
struct ssl_svc_client_st* p = (struct ssl_svc_client_st *) data;
|
|
|
|
|
struct ssl_service_cache* svc_cache=p->ref_svc_cache;
|
|
|
|
|
if(p->is_mutual_auth)
|
|
|
|
|
{
|
|
|
|
|
svc_cache->stat.mutual_auth_cli_cnt--;
|
|
|
|
|
}
|
2019-07-26 12:28:17 +06:00
|
|
|
if(p->suspect_pinning_count>=svc_cache->fail_as_cli_pinning_count)
|
2019-05-20 16:56:37 +08:00
|
|
|
{
|
|
|
|
|
svc_cache->stat.pinning_cli_cnt--;
|
|
|
|
|
}
|
2019-08-26 14:56:47 +08:00
|
|
|
|
|
|
|
|
if (p->protocol_error_count >= svc_cache->fail_as_proto_err_count)
|
|
|
|
|
{
|
|
|
|
|
svc_cache->stat.proto_err_cli_cnt--;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 16:56:37 +08:00
|
|
|
free(p);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
static void ssl_svc_free_server_st(void * data)
|
|
|
|
|
{
|
|
|
|
|
struct ssl_svc_server_st* p = (struct ssl_svc_server_st *) data;
|
|
|
|
|
struct ssl_service_cache* svc_cache=p->ref_svc_cache;
|
|
|
|
|
if(p->is_ct)
|
|
|
|
|
{
|
|
|
|
|
svc_cache->stat.ct_srv_cnt--;
|
|
|
|
|
}
|
|
|
|
|
if(p->is_ev)
|
|
|
|
|
{
|
|
|
|
|
svc_cache->stat.ev_srv_cnt--;
|
|
|
|
|
}
|
|
|
|
|
free(p);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-28 18:01:32 +08:00
|
|
|
static size_t ssl_svc_server_st_mk_key(struct ssl_svc_addr *addr_info, const struct ssl_chello* chello, const struct tfe_stream *tcp_stream, char* key_buff, size_t sz)
|
2019-06-10 18:39:01 +08:00
|
|
|
{
|
|
|
|
|
size_t key_len=0;
|
2021-04-28 18:01:32 +08:00
|
|
|
key_len=snprintf(key_buff, sz, "%s:%s:%s:", addr_info->dip, addr_info->dport, chello->sni ? chello->sni : "null");
|
2019-06-10 18:39:01 +08:00
|
|
|
return key_len;
|
|
|
|
|
}
|
2021-04-28 18:01:32 +08:00
|
|
|
static size_t ssl_svc_app_st_mk_key(struct ssl_svc_addr *addr_info, const struct ssl_chello *chello, const struct tfe_stream *tcp_stream, char *key_buff, size_t sz)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
2020-08-24 17:52:55 +08:00
|
|
|
char ja3_val[64] = {0};
|
|
|
|
|
uint16_t ja3_len = 0;
|
|
|
|
|
size_t key_len = 0;
|
|
|
|
|
struct tfe_cmsg *cmsg = tfe_stream_get0_cmsg(tcp_stream);
|
|
|
|
|
if (cmsg != NULL)
|
|
|
|
|
{
|
|
|
|
|
int ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_SSL_CLIENT_JA3_FINGERPRINT, (unsigned char *)ja3_val, sizeof(ja3_val), &ja3_len);
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
{
|
2021-04-28 18:01:32 +08:00
|
|
|
TFE_LOG_ERROR(g_default_logger, "failed at fetch ssl client ja3 fingerprint from cmsg: %s, %s", strerror(-ret), tcp_stream->str_stream_info);
|
2020-08-24 17:52:55 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-04-28 18:01:32 +08:00
|
|
|
TFE_LOG_DEBUG(g_default_logger, "fetch ssl client ja3 fingerprint:%s addr: %s", ja3_val, tcp_stream->str_stream_info);
|
2020-08-24 17:52:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
2019-07-26 12:28:17 +06:00
|
|
|
|
2020-08-24 17:52:55 +08:00
|
|
|
// If ja3 is successfully obtained, use ja3 to generate hashkey
|
|
|
|
|
if (strlen(ja3_val))
|
|
|
|
|
{
|
2021-04-28 18:01:32 +08:00
|
|
|
key_len = snprintf(key_buff, sz, "%s:%s", ja3_val, chello->sni ? chello->sni : addr_info->dip);
|
2020-08-24 17:52:55 +08:00
|
|
|
return key_len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// otherwise, splicing ssl attributes to generate hashkey
|
|
|
|
|
key_len=snprintf(key_buff, sz, "%d:%d:%s:%s", chello->min_version.ossl_format,
|
2019-07-26 12:28:17 +06:00
|
|
|
chello->max_version.ossl_format,
|
2021-04-28 18:01:32 +08:00
|
|
|
chello->sni?chello->sni: addr_info->dip ,
|
2019-07-26 12:28:17 +06:00
|
|
|
chello->alpn?chello->alpn:"null");
|
2019-05-24 20:42:19 +08:00
|
|
|
if(chello->cipher_suites && sz-key_len>chello->cipher_suites_len)
|
|
|
|
|
{
|
|
|
|
|
memcpy(key_buff+key_len, chello->cipher_suites, chello->cipher_suites_len);
|
|
|
|
|
key_len+=chello->cipher_suites_len;
|
|
|
|
|
}
|
|
|
|
|
if(chello->sign_algos && sz-key_len > chello->sign_algos_len)
|
|
|
|
|
{
|
|
|
|
|
memcpy(key_buff+key_len, chello->sign_algos, chello->sign_algos_len);
|
|
|
|
|
key_len+=chello->sign_algos_len;
|
|
|
|
|
}
|
|
|
|
|
if(chello->supported_groups && sz-key_len > chello->supported_groups_len)
|
|
|
|
|
{
|
|
|
|
|
memcpy(key_buff+key_len, chello->supported_groups, chello->supported_groups_len);
|
|
|
|
|
key_len+=chello->supported_groups_len;
|
2021-04-28 18:01:32 +08:00
|
|
|
}
|
2019-07-26 12:28:17 +06:00
|
|
|
return key_len;
|
|
|
|
|
}
|
2021-04-28 18:01:32 +08:00
|
|
|
static size_t ssl_svc_client_st_mk_key(struct ssl_svc_addr *addr_info, const struct ssl_chello *chello, const struct tfe_stream *tcp_stream, char *key_buff, size_t sz)
|
2019-07-26 12:28:17 +06:00
|
|
|
{
|
|
|
|
|
size_t key_len=0;
|
|
|
|
|
char chello_id_buff[sz];
|
|
|
|
|
size_t chello_id_len=0;
|
|
|
|
|
|
2021-04-28 18:01:32 +08:00
|
|
|
key_len=snprintf(key_buff, sz, "%s:", addr_info->sip);
|
|
|
|
|
chello_id_len = ssl_svc_app_st_mk_key(addr_info, chello, tcp_stream, chello_id_buff, sizeof(chello_id_buff));
|
2020-08-24 17:52:55 +08:00
|
|
|
memcpy(key_buff+key_len, chello_id_buff, MIN(chello_id_len, sz-key_len));
|
2019-08-15 13:15:54 +08:00
|
|
|
key_len += MIN(chello_id_len, sz-key_len);
|
2019-07-26 12:28:17 +06:00
|
|
|
|
2019-05-24 20:42:19 +08:00
|
|
|
return key_len;
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
|
|
|
|
static long cli_st_read_cb(void * data, const uchar * key, uint size, void * user_arg)
|
|
|
|
|
{
|
2019-05-20 16:56:37 +08:00
|
|
|
struct ssl_svc_client_st* cli_st=(struct ssl_svc_client_st*)data;
|
2019-05-16 20:33:42 +08:00
|
|
|
struct ssl_service_status* result=(struct ssl_service_status*)user_arg;
|
|
|
|
|
|
|
|
|
|
if (cli_st == NULL)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-07-26 14:16:07 +06:00
|
|
|
struct ssl_service_cache* svc_cache=cli_st->ref_svc_cache;
|
2019-05-21 11:47:09 +08:00
|
|
|
if(cli_st->suspect_pinning_count==0)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
2019-07-26 12:28:17 +06:00
|
|
|
result->cli_pinning_status=PINNING_ST_NOT_PINNING;
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
2019-07-26 12:28:17 +06:00
|
|
|
else if(cli_st->suspect_pinning_count<svc_cache->fail_as_cli_pinning_count)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
2019-07-26 12:28:17 +06:00
|
|
|
result->cli_pinning_status=PINNING_ST_MAYBE_PINNING;
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-07-26 12:28:17 +06:00
|
|
|
result->cli_pinning_status=PINNING_ST_PINNING;
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
2019-07-26 12:28:17 +06:00
|
|
|
if(cli_st->protocol_error_count>=svc_cache->fail_as_proto_err_count)
|
2019-05-21 11:47:09 +08:00
|
|
|
{
|
|
|
|
|
result->has_protocol_errors=1;
|
|
|
|
|
}
|
2019-05-16 20:33:42 +08:00
|
|
|
result->is_mutual_auth=cli_st->is_mutual_auth;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
static long cli_st_write_cb(void * data, const uchar * key, uint size, void * user_arg)
|
|
|
|
|
{
|
2019-05-20 16:56:37 +08:00
|
|
|
struct ssl_svc_client_st* cli_st=(struct ssl_svc_client_st*)data;
|
2019-05-16 20:33:42 +08:00
|
|
|
struct ssl_service_write_args* args=(struct ssl_service_write_args*)user_arg;
|
|
|
|
|
const struct ssl_service_status* status=args->status;
|
|
|
|
|
struct ssl_service_cache* cache=args->cache;
|
|
|
|
|
UNUSED int ret = 0;
|
|
|
|
|
time_t now=time(NULL);
|
|
|
|
|
if(cli_st==NULL)
|
|
|
|
|
{
|
2019-05-20 16:56:37 +08:00
|
|
|
cli_st=ALLOC(struct ssl_svc_client_st, 1);
|
|
|
|
|
cli_st->ref_svc_cache=cache;
|
2019-07-31 14:31:32 +08:00
|
|
|
cli_st->last_update_time = now;
|
2019-05-16 20:33:42 +08:00
|
|
|
ret = MESA_htable_add(cache->cli_st_hash, key, size, cli_st);
|
|
|
|
|
assert(ret >= 0);
|
|
|
|
|
}
|
2019-07-31 14:31:32 +08:00
|
|
|
if (now - cli_st->last_update_time > cache->fail_time_window)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
2019-07-26 12:28:17 +06:00
|
|
|
if(cli_st->suspect_pinning_count<cache->fail_as_cli_pinning_count) cli_st->suspect_pinning_count=0;
|
2019-06-10 18:39:01 +08:00
|
|
|
if(cli_st->protocol_error_count<cache->fail_as_proto_err_count) cli_st->protocol_error_count=0;
|
2022-05-12 12:03:18 +08:00
|
|
|
cli_st->last_update_time = now;
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
2019-07-26 12:28:17 +06:00
|
|
|
if(status->cli_pinning_status!=PINNING_ST_NOT_PINNING && cli_st->suspect_pinning_count<cache->fail_as_cli_pinning_count)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
2019-07-26 12:28:17 +06:00
|
|
|
if(status->cli_pinning_status==PINNING_ST_PINNING)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
2019-07-26 12:28:17 +06:00
|
|
|
cli_st->suspect_pinning_count=cache->fail_as_cli_pinning_count;
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-05-21 11:47:09 +08:00
|
|
|
cli_st->suspect_pinning_count++;
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
2019-07-26 12:28:17 +06:00
|
|
|
if(cli_st->suspect_pinning_count==cache->fail_as_cli_pinning_count)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
2019-05-20 16:56:37 +08:00
|
|
|
cache->stat.pinning_cli_cnt++;
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
|
|
|
|
}
|
2019-07-26 12:28:17 +06:00
|
|
|
else if(status->cli_pinning_status==PINNING_ST_PINNING)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
2019-07-26 12:28:17 +06:00
|
|
|
cli_st->suspect_pinning_count=cache->fail_as_cli_pinning_count;
|
2019-05-21 11:47:09 +08:00
|
|
|
}
|
|
|
|
|
if(status->has_protocol_errors)
|
|
|
|
|
{
|
|
|
|
|
cli_st->protocol_error_count++;
|
2019-06-10 18:39:01 +08:00
|
|
|
if(cli_st->protocol_error_count==cache->fail_as_proto_err_count)
|
2019-05-21 11:47:09 +08:00
|
|
|
{
|
|
|
|
|
cache->stat.proto_err_cli_cnt++;
|
|
|
|
|
}
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
|
|
|
|
if(status->is_mutual_auth==1&&cli_st->is_mutual_auth==0)
|
|
|
|
|
{
|
2019-05-20 16:56:37 +08:00
|
|
|
cache->stat.mutual_auth_cli_cnt++;
|
2019-05-16 20:33:42 +08:00
|
|
|
cli_st->is_mutual_auth=1;
|
|
|
|
|
}
|
2019-05-21 11:47:09 +08:00
|
|
|
cli_st->last_update_time=now;
|
2019-05-16 20:33:42 +08:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static long srv_st_read_cb(void * data, const uchar * key, uint size, void * user_arg)
|
|
|
|
|
{
|
2019-05-20 16:56:37 +08:00
|
|
|
struct ssl_svc_server_st* srv_st=(struct ssl_svc_server_st*)data;
|
2019-05-16 20:33:42 +08:00
|
|
|
struct ssl_service_status* result=(struct ssl_service_status*)user_arg;
|
|
|
|
|
if (srv_st == NULL)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
result->is_ct=srv_st->is_ct;
|
|
|
|
|
result->is_ev=srv_st->is_ev;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
static long srv_st_write_cb(void * data, const uchar * key, uint size, void * user_arg)
|
|
|
|
|
{
|
2019-05-20 16:56:37 +08:00
|
|
|
struct ssl_svc_server_st* srv_st=(struct ssl_svc_server_st*)data;
|
2019-05-16 20:33:42 +08:00
|
|
|
struct ssl_service_write_args* args=(struct ssl_service_write_args*)user_arg;
|
|
|
|
|
const struct ssl_service_status* status=args->status;
|
|
|
|
|
struct ssl_service_cache* cache=args->cache;
|
|
|
|
|
UNUSED int ret = 0;
|
|
|
|
|
if(srv_st==NULL)
|
|
|
|
|
{
|
2019-05-20 16:56:37 +08:00
|
|
|
srv_st=ALLOC(struct ssl_svc_server_st, 1);
|
|
|
|
|
srv_st->ref_svc_cache=cache;
|
2019-05-16 20:33:42 +08:00
|
|
|
ret = MESA_htable_add(cache->srv_st_hash, key, size, srv_st);
|
|
|
|
|
assert(ret >= 0);
|
|
|
|
|
}
|
|
|
|
|
if(status->is_ev==1&&srv_st->is_ev==0)
|
|
|
|
|
{
|
|
|
|
|
srv_st->is_ev=1;
|
2019-05-20 16:56:37 +08:00
|
|
|
cache->stat.ev_srv_cnt++;
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
|
|
|
|
if(status->is_ev!=srv_st->is_ev)
|
|
|
|
|
{
|
|
|
|
|
srv_st->ev_st_switched++;
|
|
|
|
|
}
|
|
|
|
|
if(status->is_ct==1&&srv_st->is_ct==0)
|
|
|
|
|
{
|
|
|
|
|
srv_st->is_ct=1;
|
2019-05-20 16:56:37 +08:00
|
|
|
cache->stat.ct_srv_cnt++;
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
|
|
|
|
if(status->is_ct!=srv_st->is_ct)
|
|
|
|
|
{
|
|
|
|
|
srv_st->ct_st_switched++;
|
|
|
|
|
}
|
2019-07-24 21:30:08 +06:00
|
|
|
// assert(srv_st->ev_st_switched<2&&srv_st->ct_st_switched<2);
|
2019-05-16 20:33:42 +08:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 17:52:55 +08:00
|
|
|
int ssl_service_cache_read(struct ssl_service_cache *svc_cache, const struct ssl_chello *chello, const struct tfe_stream *tcp_stream, struct ssl_service_status *result)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
2019-07-26 12:28:17 +06:00
|
|
|
long cli_st_cb_ret=0, svr_st_cb_ret=0, app_st_cb_ret=0;
|
2019-08-16 14:51:43 +08:00
|
|
|
char temp_key[2048];
|
|
|
|
|
unsigned char hash_key[4096]; // Size must be twice the size of temp_key
|
|
|
|
|
size_t temp_key_sz=0;
|
2019-06-10 18:39:01 +08:00
|
|
|
size_t hash_key_sz=0;
|
2019-05-16 20:33:42 +08:00
|
|
|
if(chello->sni==NULL)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-08-16 14:51:43 +08:00
|
|
|
|
2021-04-28 18:01:32 +08:00
|
|
|
struct ssl_svc_addr addr_info = { NULL, NULL, NULL, NULL};
|
|
|
|
|
char *addr_str = tfe_strdup(tcp_stream->str_stream_info);
|
|
|
|
|
tfe_stream_addr_str_split(addr_str, &(addr_info.sip), &(addr_info.sport), &(addr_info.dip), &(addr_info.dport));
|
|
|
|
|
|
2019-08-16 14:51:43 +08:00
|
|
|
memset(hash_key, 0, sizeof(hash_key));
|
2021-04-28 18:01:32 +08:00
|
|
|
temp_key_sz = ssl_svc_client_st_mk_key(&addr_info, chello, tcp_stream, temp_key, sizeof(temp_key));
|
2020-08-24 17:52:55 +08:00
|
|
|
hash_key_sz = tfe_hexdump(hash_key, (unsigned char *)temp_key, temp_key_sz) - hash_key;
|
2019-08-28 14:30:24 +08:00
|
|
|
MESA_htable_search_cb(svc_cache->cli_st_hash, hash_key, (unsigned int) hash_key_sz, cli_st_read_cb, result, &cli_st_cb_ret);
|
2020-08-24 17:52:55 +08:00
|
|
|
TFE_LOG_DEBUG(g_default_logger, "client table, hash:%s, found:%d, sni:%s, addr:%s, mutual:%d, pinning:%d, err:%d",
|
2021-04-28 18:01:32 +08:00
|
|
|
hash_key, cli_st_cb_ret, chello->sni, tcp_stream->str_stream_info, result->is_mutual_auth, result->cli_pinning_status, result->has_protocol_errors);
|
2019-08-16 14:51:43 +08:00
|
|
|
|
|
|
|
|
memset(hash_key, 0, sizeof(hash_key));
|
2021-04-28 18:01:32 +08:00
|
|
|
temp_key_sz = ssl_svc_server_st_mk_key(&addr_info, chello, tcp_stream, temp_key, sizeof(temp_key));
|
2020-08-24 17:52:55 +08:00
|
|
|
hash_key_sz = tfe_hexdump(hash_key, (unsigned char *)temp_key, temp_key_sz) - hash_key;
|
2019-08-16 14:51:43 +08:00
|
|
|
MESA_htable_search_cb(svc_cache->srv_st_hash, hash_key, (unsigned int) hash_key_sz, srv_st_read_cb, result, &svr_st_cb_ret);
|
2020-08-24 17:52:55 +08:00
|
|
|
TFE_LOG_DEBUG(g_default_logger, "server table, hash:%s, found:%d, sni:%s, addr:%s, ct:%d, ev:%d",
|
2021-04-28 18:01:32 +08:00
|
|
|
hash_key, svr_st_cb_ret, chello->sni, tcp_stream->str_stream_info, result->is_ct, result->is_ev);
|
2019-08-16 14:51:43 +08:00
|
|
|
|
2020-12-25 21:52:14 +06:00
|
|
|
char ja3_hash[64] = {0};
|
|
|
|
|
uint16_t ja3_len = 0;
|
|
|
|
|
result->ja3_pinning_status = JA3_PINNING_STATUS_UNKNOWN;
|
|
|
|
|
struct tfe_cmsg *cmsg = tfe_stream_get0_cmsg(tcp_stream);
|
|
|
|
|
if (cmsg)
|
|
|
|
|
{
|
|
|
|
|
int ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_SSL_CLIENT_JA3_FINGERPRINT, (unsigned char *)ja3_hash, sizeof(ja3_hash), &ja3_len);
|
|
|
|
|
if (ret == 0)
|
|
|
|
|
{
|
2021-04-28 18:01:32 +08:00
|
|
|
result->ja3_pinning_status = ssl_svc_ja3_scan(ja3_hash, tcp_stream->str_stream_info);
|
2020-12-25 21:52:14 +06:00
|
|
|
if (result->ja3_pinning_status != JA3_PINNING_STATUS_UNKNOWN)
|
|
|
|
|
{
|
|
|
|
|
app_st_cb_ret = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
TFE_LOG_DEBUG(g_default_logger, "app table, hash:%s, found:%d, sni:%s, addr:%s, ja3_pinning_status:%d",
|
2021-04-28 18:01:32 +08:00
|
|
|
ja3_hash, app_st_cb_ret, chello->sni, tcp_stream->str_stream_info, result->ja3_pinning_status);
|
2020-07-06 11:19:02 +08:00
|
|
|
|
2020-08-24 17:52:55 +08:00
|
|
|
free(addr_str);
|
2019-07-26 12:28:17 +06:00
|
|
|
|
2020-12-25 21:52:14 +06:00
|
|
|
if(cli_st_cb_ret||svr_st_cb_ret||app_st_cb_ret)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 17:52:55 +08:00
|
|
|
void ssl_service_cache_write(struct ssl_service_cache *svc_cache, const struct ssl_chello *chello, const struct tfe_stream *tcp_stream, const struct ssl_service_status *status)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
|
|
|
|
long cli_st_cb_ret=0, svr_st_cb_ret=0;
|
2019-08-16 14:51:43 +08:00
|
|
|
char temp_key[2048];
|
|
|
|
|
unsigned char hash_key[4096]; // Size must be twice the size of temp_key
|
|
|
|
|
size_t temp_key_sz=0;
|
2019-06-10 18:39:01 +08:00
|
|
|
size_t hash_key_sz=0;
|
2019-07-21 23:14:31 +08:00
|
|
|
|
|
|
|
|
if(chello == NULL || chello->sni==NULL)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-28 18:01:32 +08:00
|
|
|
|
|
|
|
|
struct ssl_svc_addr addr_info = { NULL, NULL, NULL, NULL};
|
|
|
|
|
char *addr_str = tfe_strdup(tcp_stream->str_stream_info);
|
|
|
|
|
tfe_stream_addr_str_split(addr_str, &(addr_info.sip), &(addr_info.sport), &(addr_info.dip), &(addr_info.dport));
|
|
|
|
|
|
2020-08-24 17:52:55 +08:00
|
|
|
struct ssl_service_write_args write_args={svc_cache, status};
|
2019-07-26 12:28:17 +06:00
|
|
|
if(status->is_mutual_auth||status->cli_pinning_status!=PINNING_ST_NOT_PINNING||status->has_protocol_errors)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
2019-08-16 14:51:43 +08:00
|
|
|
memset(hash_key, 0, sizeof(hash_key));
|
2021-04-28 18:01:32 +08:00
|
|
|
temp_key_sz = ssl_svc_client_st_mk_key(&addr_info, chello, tcp_stream, temp_key, sizeof(temp_key));
|
2020-08-24 17:52:55 +08:00
|
|
|
hash_key_sz = tfe_hexdump(hash_key, (unsigned char *)temp_key, temp_key_sz) - hash_key;
|
|
|
|
|
TFE_LOG_DEBUG(g_default_logger, "client table, hash:%s, sni:%s, addr:%s, mutual:%d, pinning:%d, err:%d",
|
2021-04-28 18:01:32 +08:00
|
|
|
hash_key, chello->sni, tcp_stream->str_stream_info, status->is_mutual_auth, status->cli_pinning_status, status->has_protocol_errors);
|
2020-08-24 17:52:55 +08:00
|
|
|
MESA_htable_search_cb(svc_cache->cli_st_hash, hash_key, (unsigned int) hash_key_sz, cli_st_write_cb, &write_args, &cli_st_cb_ret);
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
|
|
|
|
if(status->is_ct||status->is_ev)
|
2019-06-10 18:39:01 +08:00
|
|
|
{
|
2019-08-16 14:51:43 +08:00
|
|
|
memset(hash_key, 0, sizeof(hash_key));
|
2021-04-28 18:01:32 +08:00
|
|
|
temp_key_sz = ssl_svc_server_st_mk_key(&addr_info, chello, tcp_stream, temp_key, sizeof(temp_key));
|
2020-08-24 17:52:55 +08:00
|
|
|
hash_key_sz = tfe_hexdump(hash_key, (unsigned char *)temp_key, temp_key_sz) - hash_key;
|
|
|
|
|
TFE_LOG_DEBUG(g_default_logger, "server table, hash:%s, sni:%s, addr:%s, ct:%d, ev:%d",
|
2021-04-28 18:01:32 +08:00
|
|
|
hash_key, chello->sni, tcp_stream->str_stream_info, status->is_ct, status->is_ev);
|
2020-08-24 17:52:55 +08:00
|
|
|
MESA_htable_search_cb(svc_cache->srv_st_hash, hash_key, (unsigned int) hash_key_sz, srv_st_write_cb, &write_args, &svr_st_cb_ret);
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
2020-07-06 11:19:02 +08:00
|
|
|
|
|
|
|
|
free(addr_str);
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
2020-12-25 21:52:14 +06:00
|
|
|
struct ssl_service_cache *ssl_service_cache_create(unsigned int slot_size, unsigned int expire_seconds, int fail_as_pinning_cnt, int fail_as_proto_err_cnt, int fail_time_win
|
|
|
|
|
, char *ja3_table_name)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
2020-12-25 21:52:14 +06:00
|
|
|
if (ssl_svc_ja3_init(ja3_table_name) == 0)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ssl_service_cache * cache = ALLOC(struct ssl_service_cache, 1);
|
2019-05-16 20:33:42 +08:00
|
|
|
unsigned max_num = slot_size * 4;
|
|
|
|
|
UNUSED int ret = 0;
|
2019-07-26 12:28:17 +06:00
|
|
|
MESA_htable_handle htable=NULL, saved[3];
|
2019-05-16 20:33:42 +08:00
|
|
|
int i=0, opt_val=0;
|
2019-07-26 12:28:17 +06:00
|
|
|
cache->fail_as_cli_pinning_count=fail_as_pinning_cnt;
|
2019-06-10 18:39:01 +08:00
|
|
|
cache->fail_as_proto_err_count=fail_as_proto_err_cnt;
|
|
|
|
|
cache->fail_time_window=fail_time_win;
|
2020-12-25 21:52:14 +06:00
|
|
|
void (*free_func[])(void *)={ssl_svc_free_client_st, ssl_svc_free_server_st};
|
|
|
|
|
for(i=0; i<2; i++)
|
2019-05-16 20:33:42 +08:00
|
|
|
{
|
|
|
|
|
htable = MESA_htable_born();
|
|
|
|
|
opt_val=0;
|
|
|
|
|
ret = MESA_htable_set_opt(htable, MHO_SCREEN_PRINT_CTRL, &opt_val, sizeof(opt_val));
|
|
|
|
|
opt_val=1;
|
|
|
|
|
ret = MESA_htable_set_opt(htable, MHO_THREAD_SAFE, &opt_val, sizeof(opt_val));
|
|
|
|
|
opt_val=16;
|
|
|
|
|
ret = MESA_htable_set_opt(htable, MHO_MUTEX_NUM, &opt_val, sizeof(opt_val));
|
|
|
|
|
ret = MESA_htable_set_opt(htable, MHO_HASH_SLOT_SIZE, &slot_size, sizeof(slot_size));
|
|
|
|
|
ret = MESA_htable_set_opt(htable, MHO_HASH_MAX_ELEMENT_NUM, &max_num, sizeof(max_num));
|
2019-08-28 14:41:38 +08:00
|
|
|
// for client table and server table
|
2020-12-25 21:52:14 +06:00
|
|
|
ret = MESA_htable_set_opt(htable, MHO_EXPIRE_TIME, &expire_seconds, sizeof(expire_seconds));
|
|
|
|
|
opt_val = HASH_ELIMINATE_ALGO_FIFO;
|
|
|
|
|
ret = MESA_htable_set_opt(htable, MHO_ELIMIMINATE_TYPE, &opt_val, sizeof(int));
|
2019-08-30 21:14:47 +08:00
|
|
|
|
2019-05-16 20:33:42 +08:00
|
|
|
ret = MESA_htable_set_opt(htable, MHO_CBFUN_DATA_FREE,
|
2019-05-20 16:56:37 +08:00
|
|
|
(void*)free_func[i], sizeof(free_func[i]));
|
2019-05-16 20:33:42 +08:00
|
|
|
|
|
|
|
|
ret = MESA_htable_mature(htable);
|
|
|
|
|
assert(ret == 0);
|
|
|
|
|
saved[i]=htable;
|
|
|
|
|
}
|
|
|
|
|
cache->cli_st_hash=saved[0];
|
|
|
|
|
cache->srv_st_hash=saved[1];
|
2020-12-25 21:52:14 +06:00
|
|
|
|
|
|
|
|
return cache;
|
2019-05-16 20:33:42 +08:00
|
|
|
}
|
|
|
|
|
void ssl_service_cache_destroy(struct ssl_service_cache* cache)
|
|
|
|
|
{
|
|
|
|
|
MESA_htable_destroy(cache->cli_st_hash, NULL);
|
|
|
|
|
cache->cli_st_hash=NULL;
|
|
|
|
|
MESA_htable_destroy(cache->srv_st_hash, NULL);
|
|
|
|
|
cache->srv_st_hash=NULL;
|
|
|
|
|
free(cache);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-05-20 16:56:37 +08:00
|
|
|
void ssl_service_cache_stat(struct ssl_service_cache* svc_cache, struct ssl_service_cache_statistics* result)
|
|
|
|
|
{
|
|
|
|
|
*result=svc_cache->stat;
|
|
|
|
|
return;
|
2020-12-25 21:52:14 +06:00
|
|
|
}
|