2019-10-23 10:29:30 +08:00
|
|
|
//
|
|
|
|
|
// Created by lwp on 2019/10/16.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
2019-12-06 15:51:03 +08:00
|
|
|
#include <cjson/cJSON.h>
|
2020-03-09 16:28:56 +08:00
|
|
|
|
|
|
|
|
#include <ssl_utils.h>
|
|
|
|
|
#include <tfe_kafka_logger.h>
|
2019-10-28 17:10:38 +08:00
|
|
|
#include <MESA/MESA_prof_load.h>
|
2019-10-23 10:29:30 +08:00
|
|
|
|
2020-07-01 14:54:04 +08:00
|
|
|
typedef struct x509_object_st
|
|
|
|
|
{
|
2020-03-09 16:28:56 +08:00
|
|
|
/* one of the above types */
|
|
|
|
|
X509_LOOKUP_TYPE type;
|
2020-07-01 14:54:04 +08:00
|
|
|
union
|
|
|
|
|
{
|
2020-03-09 16:28:56 +08:00
|
|
|
char *ptr;
|
|
|
|
|
X509 *x509;
|
|
|
|
|
X509_CRL *crl;
|
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
|
} data;
|
2019-10-23 10:29:30 +08:00
|
|
|
} X509_OBJECT;
|
|
|
|
|
|
2020-07-01 14:54:04 +08:00
|
|
|
enum CERT_TYPE {
|
|
|
|
|
ENTITY_CERT = 0,
|
|
|
|
|
INTERMEDIATE_CERT = 1,
|
|
|
|
|
ROOT_CERT = 2,
|
|
|
|
|
MAX_TYPE = 3,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static char cert_type_desc[MAX_TYPE][64] = {
|
|
|
|
|
{"Entity certificate"},
|
|
|
|
|
{"Intermediate certificate"},
|
|
|
|
|
{"Root certificate"},
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-09 16:28:56 +08:00
|
|
|
static tfe_kafka_logger_t *g_kafka_logger = NULL;
|
2019-10-28 17:10:38 +08:00
|
|
|
|
2020-03-09 16:28:56 +08:00
|
|
|
void ssl_mid_cert_kafka_logger_destory(void)
|
|
|
|
|
{
|
|
|
|
|
tfe_kafka_logger_destroy(g_kafka_logger);
|
2019-10-28 17:10:38 +08:00
|
|
|
}
|
|
|
|
|
|
2019-12-06 15:51:03 +08:00
|
|
|
int ssl_mid_cert_kafka_logger_create(const char *profile, const char *section)
|
|
|
|
|
{
|
2022-09-23 15:34:50 +08:00
|
|
|
int enable = 0, vsystem_id = 0;
|
2022-09-09 10:44:11 +08:00
|
|
|
char nic_name[TFE_SYMBOL_MAX] = {0};
|
2020-03-09 16:28:56 +08:00
|
|
|
char broker_list[TFE_SYMBOL_MAX] = {0};
|
|
|
|
|
char topic_name[TFE_SYMBOL_MAX] = {0};
|
2021-08-19 16:24:19 +08:00
|
|
|
char sasl_username[TFE_STRING_MAX] = {0};
|
|
|
|
|
char sasl_passwd[TFE_STRING_MAX] = {0};
|
2020-03-09 16:28:56 +08:00
|
|
|
|
|
|
|
|
MESA_load_profile_int_def(profile, section, "mc_cache_enable", &enable, 0);
|
2022-09-23 15:34:50 +08:00
|
|
|
MESA_load_profile_int_def(profile, section, "mc_vsystem_id", &vsystem_id, 1);
|
2020-03-09 16:28:56 +08:00
|
|
|
MESA_load_profile_string_def(profile, section, "mc_cache_eth", nic_name, sizeof(nic_name), "eth0");
|
|
|
|
|
MESA_load_profile_string_def(profile, section, "mc_cache_topic", topic_name, sizeof(topic_name), "PXY-EXCH-INTERMEDIA-CERT");
|
2021-08-19 16:24:19 +08:00
|
|
|
MESA_load_profile_string_def(profile, section, "SASL_USERNAME", sasl_username, sizeof(sasl_username), "");
|
|
|
|
|
MESA_load_profile_string_def(profile, section, "SASL_PASSWD", sasl_passwd, sizeof(sasl_passwd), "");
|
|
|
|
|
|
2020-07-01 14:54:04 +08:00
|
|
|
if (!enable)
|
2020-03-09 16:56:39 +08:00
|
|
|
goto skip;
|
2020-03-09 16:28:56 +08:00
|
|
|
if (MESA_load_profile_string_def(profile, section, "mc_cache_broker_list", broker_list, sizeof(broker_list), NULL) < 0)
|
|
|
|
|
{
|
2020-07-01 14:54:04 +08:00
|
|
|
TFE_LOG_ERROR(g_default_logger, "Fail to get mc_cache_broker_list in profile %s section %s.", profile, section);
|
2020-03-09 16:28:56 +08:00
|
|
|
return -1;
|
2019-10-28 17:10:38 +08:00
|
|
|
}
|
2020-03-09 16:56:39 +08:00
|
|
|
skip:
|
2021-08-19 16:24:19 +08:00
|
|
|
g_kafka_logger = tfe_kafka_logger_create(enable, nic_name, broker_list, topic_name, sasl_username, sasl_passwd, g_default_logger);
|
2020-03-09 16:28:56 +08:00
|
|
|
if (g_kafka_logger)
|
2022-09-23 15:34:50 +08:00
|
|
|
{
|
2022-10-26 14:30:45 +08:00
|
|
|
g_kafka_logger->t_vsys_id=vsystem_id;
|
2020-03-09 16:28:56 +08:00
|
|
|
return 0;
|
2022-09-23 15:34:50 +08:00
|
|
|
}
|
2020-03-09 16:28:56 +08:00
|
|
|
else
|
2022-09-23 15:34:50 +08:00
|
|
|
{
|
2020-03-09 16:28:56 +08:00
|
|
|
return -1;
|
2022-09-23 15:34:50 +08:00
|
|
|
}
|
2019-10-28 17:10:38 +08:00
|
|
|
}
|
|
|
|
|
|
2020-03-09 16:28:56 +08:00
|
|
|
static void ssl_mid_cert_kafka_logger_send(const char *sni, const char *fingerprint, const char *cert)
|
2019-12-06 15:51:03 +08:00
|
|
|
{
|
2020-03-09 16:28:56 +08:00
|
|
|
if (g_kafka_logger->enable == 0)
|
2019-12-06 15:51:03 +08:00
|
|
|
{
|
2019-10-28 17:10:38 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2019-12-06 15:51:03 +08:00
|
|
|
cJSON *obj = NULL;
|
|
|
|
|
cJSON *dup = NULL;
|
|
|
|
|
char *msg = NULL;
|
|
|
|
|
|
|
|
|
|
obj = cJSON_CreateObject();
|
2019-12-06 17:23:21 +08:00
|
|
|
cJSON_AddStringToObject(obj, "sni", sni);
|
|
|
|
|
cJSON_AddStringToObject(obj, "fingerprint", fingerprint);
|
2022-11-11 10:30:11 +08:00
|
|
|
cJSON_AddNumberToObject(obj, "vsys_id", g_kafka_logger->t_vsys_id);
|
2019-12-06 15:51:03 +08:00
|
|
|
cJSON_AddStringToObject(obj, "cert", cert);
|
2020-03-09 16:28:56 +08:00
|
|
|
cJSON_AddStringToObject(obj, "tfe_ip", g_kafka_logger->local_ip_str);
|
2019-12-06 15:51:03 +08:00
|
|
|
dup = cJSON_Duplicate(obj, 1);
|
|
|
|
|
msg = cJSON_PrintUnformatted(dup);
|
2023-12-14 15:08:19 +08:00
|
|
|
TFE_LOG_DEBUG(g_default_logger, "log to [%s] msg:%s", g_kafka_logger->topic_name[TOPIC_LOGGER], msg);
|
|
|
|
|
tfe_kafka_logger_send(g_kafka_logger, TOPIC_LOGGER, msg, strlen(msg));
|
2019-12-06 15:51:03 +08:00
|
|
|
|
|
|
|
|
free(msg);
|
2019-12-06 17:23:21 +08:00
|
|
|
cJSON_Delete(dup);
|
2019-12-06 15:51:03 +08:00
|
|
|
cJSON_Delete(obj);
|
2019-10-28 17:10:38 +08:00
|
|
|
}
|
2019-10-23 10:29:30 +08:00
|
|
|
|
2020-07-01 14:54:04 +08:00
|
|
|
static int is_x509v3_ca_cert(X509 *x)
|
|
|
|
|
{
|
|
|
|
|
/* what is Basic Constraint Extension:
|
|
|
|
|
* http://www.pkiglobe.org/
|
|
|
|
|
* https://tools.ietf.org/html/rfc5280#section-4.2.1.9
|
|
|
|
|
*
|
|
|
|
|
* how to visit Basic Constraint Extension:
|
|
|
|
|
* https://stackoverflow.com/questions/40609792/how-can-i-verify-certificate-has-ca-true-basic-constraint
|
|
|
|
|
*/
|
|
|
|
|
int is_ca = 0;
|
|
|
|
|
BASIC_CONSTRAINTS *bs = NULL;
|
|
|
|
|
|
|
|
|
|
if (X509_get_version(x) != 2)
|
|
|
|
|
{
|
|
|
|
|
return is_ca;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bs = (BASIC_CONSTRAINTS *)X509_get_ext_d2i(x, NID_basic_constraints, NULL, NULL);
|
|
|
|
|
if (bs)
|
|
|
|
|
{
|
|
|
|
|
if (bs->ca)
|
|
|
|
|
{
|
|
|
|
|
is_ca = 1;
|
|
|
|
|
}
|
|
|
|
|
BASIC_CONSTRAINTS_free(bs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return is_ca;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ssl_fetch_trusted_cert_from_chain(STACK_OF(X509) * cert_chain, X509_STORE *trusted_store, const char *hostname)
|
|
|
|
|
{
|
|
|
|
|
int in_store;
|
|
|
|
|
int type;
|
2019-12-06 15:51:03 +08:00
|
|
|
int deep;
|
2020-01-02 18:45:42 +08:00
|
|
|
char *pem = NULL;
|
2019-12-06 15:51:03 +08:00
|
|
|
char *subj = NULL;
|
|
|
|
|
char *issuer = NULL;
|
|
|
|
|
char *fingerprint = NULL;
|
|
|
|
|
X509 *cert = NULL;
|
2020-01-06 18:17:13 +08:00
|
|
|
X509_OBJECT *obj = NULL;
|
2020-07-01 14:54:04 +08:00
|
|
|
if (!g_kafka_logger || !g_kafka_logger->enable)
|
|
|
|
|
{
|
2019-10-28 17:10:38 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 15:51:03 +08:00
|
|
|
deep = sk_X509_num(cert_chain);
|
2020-07-01 14:54:04 +08:00
|
|
|
for (int i = 0; i < deep; i++)
|
|
|
|
|
{
|
2019-12-06 15:51:03 +08:00
|
|
|
cert = sk_X509_value(cert_chain, i);
|
2019-10-23 10:29:30 +08:00
|
|
|
assert(cert);
|
|
|
|
|
|
2020-07-01 14:54:04 +08:00
|
|
|
in_store = 0;
|
|
|
|
|
subj = ssl_x509_subject(cert);
|
|
|
|
|
issuer = ssl_x509_issuer(cert);
|
|
|
|
|
fingerprint = ssl_x509_fingerprint(cert, 0);
|
|
|
|
|
pem = ssl_x509_to_pem(cert);
|
|
|
|
|
|
|
|
|
|
if (!is_x509v3_ca_cert(cert))
|
|
|
|
|
{
|
|
|
|
|
type = ENTITY_CERT;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (subj && issuer && strcmp(subj, issuer) == 0)
|
|
|
|
|
{
|
|
|
|
|
type = ROOT_CERT;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
type = INTERMEDIATE_CERT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-06 18:17:13 +08:00
|
|
|
obj = X509_OBJECT_new();
|
|
|
|
|
assert(obj);
|
|
|
|
|
obj->type = X509_LU_X509;
|
|
|
|
|
obj->data.x509 = (X509 *)cert;
|
2020-06-12 21:15:39 +08:00
|
|
|
X509_OBJECT_up_ref_count(obj);
|
|
|
|
|
|
2020-01-06 18:17:13 +08:00
|
|
|
// not in trusted store
|
2020-03-09 16:28:56 +08:00
|
|
|
if (X509_OBJECT_retrieve_match(X509_STORE_get0_objects(trusted_store), obj) == NULL)
|
2020-01-06 18:17:13 +08:00
|
|
|
{
|
2020-07-01 14:54:04 +08:00
|
|
|
in_store = 0;
|
2020-01-06 18:17:13 +08:00
|
|
|
}
|
|
|
|
|
// in trusted store
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-07-01 14:54:04 +08:00
|
|
|
in_store = 1;
|
2020-01-06 18:17:13 +08:00
|
|
|
}
|
|
|
|
|
X509_OBJECT_free(obj);
|
|
|
|
|
|
2020-07-01 14:54:04 +08:00
|
|
|
if (!in_store && fingerprint && pem)
|
|
|
|
|
{
|
2020-01-02 18:45:42 +08:00
|
|
|
ssl_mid_cert_kafka_logger_send(hostname, fingerprint, pem);
|
2019-10-23 10:29:30 +08:00
|
|
|
}
|
2020-07-01 14:54:04 +08:00
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
TFE_LOG_DEBUG(g_default_logger, "[dep:%d/%d] is %s, in_trusted_store:%d, sin:%s; subject:(%s); issuer:(%s); fingerprint:%s; cert:%s",
|
|
|
|
|
i, deep, cert_type_desc[type], in_store, (hostname ? hostname : "NULL"), (subj ? subj : "NULL"), (issuer ? issuer : "NULL"), (fingerprint ? fingerprint : "NULL"),
|
|
|
|
|
((pem && g_kafka_logger->enable == 0x10) ? pem : " ..."));
|
2020-01-02 18:45:42 +08:00
|
|
|
if (pem)
|
2020-07-01 14:54:04 +08:00
|
|
|
{
|
2020-01-02 18:45:42 +08:00
|
|
|
free(pem);
|
2020-07-01 14:54:04 +08:00
|
|
|
pem = NULL;
|
|
|
|
|
}
|
2019-12-06 15:51:03 +08:00
|
|
|
if (subj)
|
2020-07-01 14:54:04 +08:00
|
|
|
{
|
2019-12-06 15:51:03 +08:00
|
|
|
free(subj);
|
2020-07-01 14:54:04 +08:00
|
|
|
subj = NULL;
|
|
|
|
|
}
|
2019-12-06 15:51:03 +08:00
|
|
|
if (issuer)
|
2020-07-01 14:54:04 +08:00
|
|
|
{
|
2019-12-06 15:51:03 +08:00
|
|
|
free(issuer);
|
2020-07-01 14:54:04 +08:00
|
|
|
issuer = NULL;
|
|
|
|
|
}
|
2019-12-06 15:51:03 +08:00
|
|
|
if (fingerprint)
|
2020-07-01 14:54:04 +08:00
|
|
|
{
|
2019-12-06 15:51:03 +08:00
|
|
|
free(fingerprint);
|
2020-07-01 14:54:04 +08:00
|
|
|
fingerprint = NULL;
|
|
|
|
|
}
|
2019-10-23 10:29:30 +08:00
|
|
|
}
|
|
|
|
|
}
|