区分未安装证书的客户端和pinning的客户端。若开启tfe.conf中[SSL]root_cert_not_installed_is_not_pinning=1,则未安装证书的客户端不再视为pinning。

This commit is contained in:
zhengchao
2019-07-26 12:28:17 +06:00
committed by luwenpeng
parent 06fe5652c5
commit 30fd8889a8
3 changed files with 138 additions and 37 deletions

View File

@@ -9,7 +9,8 @@
#define PINNING_ST_MAYBE_PINNING 2
struct ssl_service_status
{
char pinning_status;
char cli_pinning_status;
char is_app_not_pinning;
char is_ev;
char is_ct;
char is_mutual_auth;
@@ -29,5 +30,6 @@ struct ssl_service_cache_statistics
long long proto_err_cli_cnt;
long long ev_srv_cnt;
long long ct_srv_cnt;
long long app_not_pinning_cnt;
};
void ssl_service_cache_stat(struct ssl_service_cache* svc_cache, struct ssl_service_cache_statistics* result);

View File

@@ -7,6 +7,7 @@
#define FAIL_AS_PINNING_COUNT 4
#define FAIL_TIME_WINDOW 30
#define FAIL_AS_PROTO_ERR_COUNT 5
#define SUCC_AS_APP_NOT_PINNING 3
struct ssl_svc_client_st
{
time_t last_update_time;
@@ -23,14 +24,22 @@ struct ssl_svc_server_st
long long ct_st_switched;
struct ssl_service_cache* ref_svc_cache;
};
struct ssl_svc_app_st
{
unsigned int down_ssl_success_cnt;
struct ssl_service_cache* ref_svc_cache;
};
struct ssl_service_cache
{
MESA_htable_handle cli_st_hash;
MESA_htable_handle srv_st_hash;
MESA_htable_handle app_st_hash;
struct ssl_service_cache_statistics stat;
unsigned int fail_as_pinning_count;
unsigned int fail_as_cli_pinning_count;
unsigned int fail_as_proto_err_count;
unsigned int fail_time_window;
unsigned int succ_as_app_not_pinning_count;
};
struct ssl_service_write_args
{
@@ -45,7 +54,7 @@ static void ssl_svc_free_client_st(void * data)
{
svc_cache->stat.mutual_auth_cli_cnt--;
}
if(p->suspect_pinning_count>=FAIL_AS_PINNING_COUNT)
if(p->suspect_pinning_count>=svc_cache->fail_as_cli_pinning_count)
{
svc_cache->stat.pinning_cli_cnt--;
}
@@ -67,6 +76,16 @@ static void ssl_svc_free_server_st(void * data)
free(p);
return;
}
static void ssl_svc_free_app_st(void* data)
{
struct ssl_svc_app_st* p= (struct ssl_svc_app_st*)data;
struct ssl_service_cache* svc_cache=p->ref_svc_cache;
if(p->down_ssl_success_cnt>svc_cache->succ_as_app_not_pinning_count)
{
svc_cache->stat.app_not_pinning_cnt--;
}
free(p);
}
static size_t ssl_svc_server_st_mk_key(const struct ssl_chello* chello, const struct tfe_stream_addr * addr, char* key_buff, size_t sz)
{
size_t key_len=0;
@@ -77,14 +96,14 @@ static size_t ssl_svc_server_st_mk_key(const struct ssl_chello* chello, const st
free(addr_str);
return key_len;
}
static size_t ssl_svc_client_st_mk_key(const struct ssl_chello* chello, const struct tfe_stream_addr * addr, char* key_buff, size_t sz)
static size_t ssl_svc_app_st_mk_key(const struct ssl_chello* chello, const struct tfe_stream_addr * addr, char* key_buff, size_t sz)
{
size_t key_len=0;
const char* sip=NULL, *sport=NULL, *dip=NULL, *dport=NULL;
char * addr_str= tfe_stream_addr_to_str(addr);
tfe_stream_addr_str_split(addr_str, &sip, &sport, &dip, &dport);
key_len=snprintf(key_buff, sz, "%s:%d:%d:%s:%s", sip,
chello->min_version.ossl_format,
key_len=snprintf(key_buff, sz, "%d:%d:%s:%s", chello->min_version.ossl_format,
chello->max_version.ossl_format,
chello->sni?chello->sni: dip ,
chello->alpn?chello->alpn:"null");
@@ -106,9 +125,27 @@ static size_t ssl_svc_client_st_mk_key(const struct ssl_chello* chello, const st
free(addr_str);
return key_len;
}
static size_t ssl_svc_client_st_mk_key(const struct ssl_chello* chello, const struct tfe_stream_addr * addr, char* key_buff, size_t sz)
{
size_t key_len=0;
const char* sip=NULL, *sport=NULL, *dip=NULL, *dport=NULL;
char * addr_str= tfe_stream_addr_to_str(addr);
tfe_stream_addr_str_split(addr_str, &sip, &sport, &dip, &dport);
char chello_id_buff[sz];
size_t chello_id_len=0;
key_len=snprintf(key_buff, sz, "%s:", sip);
chello_id_len=ssl_svc_app_st_mk_key(chello, addr, chello_id_buff, sizeof(chello_id_buff));
memcpy(key_buff+key_len, chello_id_buff, MIN(chello_id_len, sz-key_len));
key_len+=chello_id_len;
free(addr_str);
return key_len;
}
static long cli_st_read_cb(void * data, const uchar * key, uint size, void * user_arg)
{
struct ssl_svc_client_st* cli_st=(struct ssl_svc_client_st*)data;
struct ssl_service_cache* svc_cache=cli_st->ref_svc_cache;
struct ssl_service_status* result=(struct ssl_service_status*)user_arg;
if (cli_st == NULL)
@@ -117,17 +154,17 @@ static long cli_st_read_cb(void * data, const uchar * key, uint size, void * use
}
if(cli_st->suspect_pinning_count==0)
{
result->pinning_status=PINNING_ST_NOT_PINNING;
result->cli_pinning_status=PINNING_ST_NOT_PINNING;
}
else if(cli_st->suspect_pinning_count<FAIL_AS_PINNING_COUNT)
else if(cli_st->suspect_pinning_count<svc_cache->fail_as_cli_pinning_count)
{
result->pinning_status=PINNING_ST_MAYBE_PINNING;
result->cli_pinning_status=PINNING_ST_MAYBE_PINNING;
}
else
{
result->pinning_status=PINNING_ST_PINNING;
result->cli_pinning_status=PINNING_ST_PINNING;
}
if(cli_st->protocol_error_count>=FAIL_AS_PROTO_ERR_COUNT)
if(cli_st->protocol_error_count>=svc_cache->fail_as_proto_err_count)
{
result->has_protocol_errors=1;
}
@@ -152,27 +189,27 @@ static long cli_st_write_cb(void * data, const uchar * key, uint size, void * us
}
if (now - cli_st->last_update_time > cache->fail_time_window)
{
if(cli_st->suspect_pinning_count<cache->fail_as_pinning_count) cli_st->suspect_pinning_count=0;
if(cli_st->suspect_pinning_count<cache->fail_as_cli_pinning_count) cli_st->suspect_pinning_count=0;
if(cli_st->protocol_error_count<cache->fail_as_proto_err_count) cli_st->protocol_error_count=0;
}
if(status->pinning_status!=PINNING_ST_NOT_PINNING && cli_st->suspect_pinning_count<cache->fail_as_pinning_count)
if(status->cli_pinning_status!=PINNING_ST_NOT_PINNING && cli_st->suspect_pinning_count<cache->fail_as_cli_pinning_count)
{
if(status->pinning_status==PINNING_ST_PINNING)
if(status->cli_pinning_status==PINNING_ST_PINNING)
{
cli_st->suspect_pinning_count=cache->fail_as_pinning_count;
cli_st->suspect_pinning_count=cache->fail_as_cli_pinning_count;
}
else
{
cli_st->suspect_pinning_count++;
}
if(cli_st->suspect_pinning_count==cache->fail_as_pinning_count)
if(cli_st->suspect_pinning_count==cache->fail_as_cli_pinning_count)
{
cache->stat.pinning_cli_cnt++;
}
}
else if(status->pinning_status==PINNING_ST_PINNING)
else if(status->cli_pinning_status==PINNING_ST_PINNING)
{
cli_st->suspect_pinning_count=cache->fail_as_pinning_count;
cli_st->suspect_pinning_count=cache->fail_as_cli_pinning_count;
}
if(status->has_protocol_errors)
{
@@ -238,10 +275,49 @@ static long srv_st_write_cb(void * data, const uchar * key, uint size, void * us
// assert(srv_st->ev_st_switched<2&&srv_st->ct_st_switched<2);
return 1;
}
static long app_st_read_cb(void * data, const uchar * key, uint size, void * user_arg)
{
struct ssl_svc_app_st* app_st=(struct ssl_svc_app_st*)data;
struct ssl_service_status* result=(struct ssl_service_status*)user_arg;
if (app_st == NULL)
{
return 0;
}
if(app_st->down_ssl_success_cnt>app_st->ref_svc_cache->succ_as_app_not_pinning_count)
{
result->is_app_not_pinning=1;
}
return 1;
}
static long app_st_write_cb(void * data, const uchar * key, uint size, void * user_arg)
{
struct ssl_svc_app_st* app_st=(struct ssl_svc_app_st*)data;
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(app_st==NULL)
{
app_st=ALLOC(struct ssl_svc_app_st, 1);
app_st->ref_svc_cache=cache;
ret = MESA_htable_add(cache->srv_st_hash, key, size, app_st);
assert(ret >= 0);
}
if(status->is_app_not_pinning)
{
app_st->down_ssl_success_cnt++;
}
if(app_st->down_ssl_success_cnt>cache->succ_as_app_not_pinning_count)
{
cache->stat.app_not_pinning_cnt++;
}
return 1;
}
int ssl_service_cache_read(struct ssl_service_cache* svc_cache, const struct ssl_chello* chello, const struct tfe_stream_addr * addr, struct ssl_service_status* result)
{
long cli_st_cb_ret=0, svr_st_cb_ret=0;
long cli_st_cb_ret=0, svr_st_cb_ret=0, app_st_cb_ret=0;
char hash_key[2048];
size_t hash_key_sz=0;
if(chello->sni==NULL)
@@ -252,7 +328,10 @@ int ssl_service_cache_read(struct ssl_service_cache* svc_cache, const struct ssl
MESA_htable_search_cb(svc_cache->cli_st_hash, (unsigned char*) hash_key, (unsigned int) hash_key_sz, cli_st_read_cb, result, &cli_st_cb_ret);
hash_key_sz=ssl_svc_server_st_mk_key(chello, addr, hash_key, sizeof(hash_key));
MESA_htable_search_cb(svc_cache->srv_st_hash, (unsigned char*) hash_key, (unsigned int) hash_key_sz, srv_st_read_cb, result, &svr_st_cb_ret);
if(cli_st_cb_ret||svr_st_cb_ret)
hash_key_sz=ssl_svc_app_st_mk_key(chello, addr, hash_key, sizeof(hash_key));
MESA_htable_search_cb(svc_cache->srv_st_hash, (unsigned char*) hash_key, (unsigned int) hash_key_sz, app_st_read_cb, result, &app_st_cb_ret);
if(cli_st_cb_ret||svr_st_cb_ret||app_st_cb_ret)
{
return 1;
}
@@ -273,7 +352,7 @@ void ssl_service_cache_write(struct ssl_service_cache* svc_cache, const struct s
return;
}
struct ssl_service_write_args write_args={svc_cache, status};
if(status->is_mutual_auth||status->pinning_status!=PINNING_ST_NOT_PINNING||status->has_protocol_errors)
if(status->is_mutual_auth||status->cli_pinning_status!=PINNING_ST_NOT_PINNING||status->has_protocol_errors)
{
hash_key_sz=ssl_svc_client_st_mk_key(chello, addr, hash_key, sizeof(hash_key));
MESA_htable_search_cb(svc_cache->cli_st_hash, (unsigned char*)hash_key, (unsigned int) hash_key_sz, cli_st_write_cb, &write_args, &cli_st_cb_ret);
@@ -283,19 +362,25 @@ void ssl_service_cache_write(struct ssl_service_cache* svc_cache, const struct s
hash_key_sz=ssl_svc_server_st_mk_key(chello, addr, hash_key, sizeof(hash_key));
MESA_htable_search_cb(svc_cache->srv_st_hash, (unsigned char*)hash_key, (unsigned int) hash_key_sz, srv_st_write_cb, &write_args, &svr_st_cb_ret);
}
if(status->is_app_not_pinning)
{
hash_key_sz=ssl_svc_app_st_mk_key(chello, addr, hash_key, sizeof(hash_key));
MESA_htable_search_cb(svc_cache->app_st_hash, (unsigned char*)hash_key, (unsigned int) hash_key_sz, app_st_write_cb, &write_args, &svr_st_cb_ret);
}
}
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)
{
struct ssl_service_cache * cache = ALLOC(struct ssl_service_cache, 1);
unsigned max_num = slot_size * 4;
UNUSED int ret = 0;
MESA_htable_handle htable=NULL, saved[2];
MESA_htable_handle htable=NULL, saved[3];
int i=0, opt_val=0;
cache->fail_as_pinning_count=fail_as_pinning_cnt;
cache->fail_as_cli_pinning_count=fail_as_pinning_cnt;
cache->fail_as_proto_err_count=fail_as_proto_err_cnt;
cache->fail_time_window=fail_time_win;
void (*free_func[])(void *)={ssl_svc_free_client_st, ssl_svc_free_server_st};
for(i=0; i<2; i++)
cache->succ_as_app_not_pinning_count=4;//TODO: read from profile.
void (*free_func[])(void *)={ssl_svc_free_client_st, ssl_svc_free_server_st, ssl_svc_free_app_st};
for(i=0; i<3; i++)
{
htable = MESA_htable_born();
opt_val=0;
@@ -320,7 +405,7 @@ struct ssl_service_cache* ssl_service_cache_create(unsigned int slot_size, unsig
}
cache->cli_st_hash=saved[0];
cache->srv_st_hash=saved[1];
cache->app_st_hash=saved[3];
return cache;
}
void ssl_service_cache_destroy(struct ssl_service_cache* cache)

View File

@@ -121,6 +121,7 @@ struct ssl_mgr
unsigned int no_alpn;
unsigned int no_cert_verify;
unsigned int no_mirror_client_cipher_suite;
unsigned int root_cert_not_installed_is_not_pinning;
CONST_SSL_METHOD * (* sslmethod)(void); //Parameter of SSL_CTX_new
int ssl_min_version, ssl_max_version;
@@ -682,7 +683,8 @@ struct ssl_mgr * ssl_manager_init(const char * ini_profile, const char * section
{
mgr->down_stek_box = sess_ticket_box_create(ev_base_gc, stek_group_num, stek_rotation_time, logger);
}
MESA_load_profile_uint_def(ini_profile, section, "root_cert_not_installed_is_not_pinning",
&(mgr->root_cert_not_installed_is_not_pinning), 0);
MESA_load_profile_uint_def(ini_profile, section, "service_cache_slots",
&(mgr->svc_cache_slots), 4 * 1024 * 1024);
MESA_load_profile_uint_def(ini_profile, section, "service_cache_expire_seconds",
@@ -1173,7 +1175,7 @@ void ssl_stream_process_error(struct ssl_stream * s_stream, unsigned long sslerr
&& s_upstream->is_server_cert_verify_passed
&& s_upstream->verify_result.is_hostmatched)
{
s_upstream->svc_status.pinning_status=PINNING_ST_PINNING;
s_upstream->svc_status.cli_pinning_status=PINNING_ST_PINNING;
ssl_stream_set_cmsg_integer(s_stream, TFE_CMSG_SSL_PINNING_STATE, PINNING_ST_PINNING);
ssl_service_cache_write(mgr->svc_cache, s_upstream->client_hello, s_stream->tcp_stream->addr, &s_upstream->svc_status);
}
@@ -1205,7 +1207,7 @@ void ssl_stream_process_zero_eof(struct ssl_stream * s_stream, struct ssl_mgr* m
s_upstream=&s_stream->peer->up_parts;
if(s_upstream->verify_result.is_hostmatched && s_upstream->is_server_cert_verify_passed )
{
s_upstream->svc_status.pinning_status=PINNING_ST_MAYBE_PINNING;
s_upstream->svc_status.cli_pinning_status=PINNING_ST_MAYBE_PINNING;
ssl_stream_set_cmsg_integer(s_stream, TFE_CMSG_SSL_PINNING_STATE, PINNING_ST_MAYBE_PINNING);
ssl_service_cache_write(mgr->svc_cache, s_stream->peer->up_parts.client_hello, s_stream->tcp_stream->addr, &(s_stream->peer->up_parts.svc_status));
}
@@ -1377,7 +1379,7 @@ static void peek_chello_on_succ(future_result_t * result, void * user)
TFE_LOG_DEBUG(ctx->mgr->logger, "%s %s service status pinning:%d, mauth:%d, err:%d, ct:%d, ev:%d",
addr_string,
chello->sni,
svc_status->pinning_status,
svc_status->cli_pinning_status,
svc_status->is_mutual_auth,
svc_status->has_protocol_errors,
svc_status->is_ct,
@@ -1385,7 +1387,7 @@ static void peek_chello_on_succ(future_result_t * result, void * user)
free(addr_string);
addr_string=NULL;
}
ssl_stream_set_cmsg_integer(s_stream, TFE_CMSG_SSL_PINNING_STATE, svc_status->pinning_status);
ssl_stream_set_cmsg_integer(s_stream, TFE_CMSG_SSL_PINNING_STATE, svc_status->cli_pinning_status);
if(ctx->mgr->on_new_upstream_cb)
{
s_stream->up_parts.action=ctx->mgr->on_new_upstream_cb(s_stream, ctx->mgr->upstream_cb_param);
@@ -1864,6 +1866,10 @@ static void ssl_client_connected_eventcb(struct bufferevent * bev, short events,
}
s_stream->negotiated_version=SSL_version(s_stream->ssl);
ssl_stream_set_cmsg_string(s_stream, TFE_CMSG_SSL_CLIENT_SIDE_VERSION, SSL_get_version(s_stream->ssl));
struct ssl_service_status svc_status;
memset(&svc_status, 0, sizeof(svc_status));
svc_status.is_app_not_pinning=1;
ssl_service_cache_write(mgr->svc_cache, s_upstream->client_hello, s_stream->tcp_stream->addr, &svc_status);
promise_success(p, ctx);
}
@@ -2188,6 +2194,7 @@ int ssl_stream_set_integer_opt(struct ssl_stream *upstream, enum SSL_STREAM_OPT
int ssl_stream_get_integer_opt(struct ssl_stream *upstream, enum SSL_STREAM_OPT opt_type, int *opt_val)
{
struct ssl_service_status* svc=&upstream->up_parts.svc_status;
struct ssl_mgr* mgr=upstream->mgr;
struct tfe_cmsg *cmsg=NULL;
UNUSED int ret=0;
uint16_t out_size=0;
@@ -2203,7 +2210,14 @@ int ssl_stream_get_integer_opt(struct ssl_stream *upstream, enum SSL_STREAM_OPT
*opt_val=svc->is_mutual_auth;
break;
case SSL_STREAM_OPT_PINNING_STATUS:
*opt_val=svc->pinning_status;
if(mgr->root_cert_not_installed_is_not_pinning && svc->is_app_not_pinning)
{
*opt_val=PINNING_ST_NOT_PINNING;
}
else
{
*opt_val=svc->cli_pinning_status;
}
break;
case SSL_STREAM_OPT_HAS_PROTOCOL_ERRORS:
*opt_val=svc->has_protocol_errors;