summaryrefslogtreecommitdiff
path: root/platform/src/ssl_fetch_cert.cpp
blob: 9dd725ba700944a88d49db782ce8f06210929197 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// Created by lwp on 2019/10/16.
//

#include <assert.h>
#include <cjson/cJSON.h>

#include <ssl_utils.h>
#include <tfe_kafka_logger.h>
#include <MESA/MESA_prof_load.h>

typedef struct x509_object_st
{
	/* one of the above types */
	X509_LOOKUP_TYPE type;
	union
	{
		char     *ptr;
		X509     *x509;
		X509_CRL *crl;
		EVP_PKEY *pkey;
	} data;
} X509_OBJECT;

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"},
};

static tfe_kafka_logger_t *g_kafka_logger = NULL;

void ssl_mid_cert_kafka_logger_destory(void)
{
	tfe_kafka_logger_destroy(g_kafka_logger);
}

int ssl_mid_cert_kafka_logger_create(const char *profile, const char *section)
{
	int enable = 0, vsystem_id = 0;
	char nic_name[TFE_SYMBOL_MAX] = {0};
	char broker_list[TFE_SYMBOL_MAX] = {0};
	char topic_name[TFE_SYMBOL_MAX] = {0};
	char sasl_username[TFE_STRING_MAX] = {0};
	char sasl_passwd[TFE_STRING_MAX] = {0};

	MESA_load_profile_int_def(profile, section, "mc_cache_enable", &enable, 0);
	MESA_load_profile_int_def(profile, section, "mc_vsystem_id", &vsystem_id, 1);
	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");
	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), "");

	if (!enable)
		goto skip;
	if (MESA_load_profile_string_def(profile, section, "mc_cache_broker_list", broker_list, sizeof(broker_list), NULL) < 0)
	{
		TFE_LOG_ERROR(g_default_logger, "Fail to get mc_cache_broker_list in profile %s section %s.", profile, section);
		return -1;
	}
skip:
	g_kafka_logger = tfe_kafka_logger_create(enable, nic_name, broker_list, topic_name, sasl_username, sasl_passwd, g_default_logger);
	if (g_kafka_logger)
	{
		g_kafka_logger->t_vsys_id=vsystem_id;
		return 0;
	}
	else
	{
		return -1;
	}
}

static void ssl_mid_cert_kafka_logger_send(const char *sni, const char *fingerprint, const char *cert)
{
	if (g_kafka_logger->enable == 0)
	{
		return;
	}
	cJSON *obj = NULL;
	cJSON *dup = NULL;
	char  *msg = NULL;

	obj = cJSON_CreateObject();
	cJSON_AddStringToObject(obj, "sni", sni);
	cJSON_AddStringToObject(obj, "fingerprint", fingerprint);
	cJSON_AddNumberToObject(obj, "vsys_id", g_kafka_logger->t_vsys_id);
	cJSON_AddStringToObject(obj, "cert", cert);
	cJSON_AddStringToObject(obj, "tfe_ip", g_kafka_logger->local_ip_str);
	dup = cJSON_Duplicate(obj, 1);
	msg = cJSON_PrintUnformatted(dup);
	TFE_LOG_DEBUG(g_default_logger, "log to [%s] msg:%s", g_kafka_logger->topic_name, msg);
	tfe_kafka_logger_send(g_kafka_logger, msg, strlen(msg));

	free(msg);
	cJSON_Delete(dup);
	cJSON_Delete(obj);
}

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;
	int          deep;
	char        *pem = NULL;
	char        *subj = NULL;
	char        *issuer = NULL;
	char        *fingerprint = NULL;
	X509        *cert = NULL;
	X509_OBJECT *obj = NULL;
	if (!g_kafka_logger || !g_kafka_logger->enable)
	{
		return;
	}

	deep = sk_X509_num(cert_chain);
	for (int i = 0; i < deep; i++)
	{
		cert = sk_X509_value(cert_chain, i);
		assert(cert);

		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;
			}
		}

		obj = X509_OBJECT_new();
		assert(obj);
		obj->type = X509_LU_X509;
		obj->data.x509 = (X509 *)cert;
		X509_OBJECT_up_ref_count(obj);

		// not in trusted store
		if (X509_OBJECT_retrieve_match(X509_STORE_get0_objects(trusted_store), obj) == NULL)
		{
			in_store = 0;
		}
		// in trusted store
		else
		{
			in_store = 1;
		}
		X509_OBJECT_free(obj);

		if (!in_store && fingerprint && pem)
		{
			ssl_mid_cert_kafka_logger_send(hostname, fingerprint, pem);
		}

	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 : " ..."));
		if (pem)
		{
			free(pem);
			pem = NULL;
		}
		if (subj)
		{
			free(subj);
			subj = NULL;
		}
		if (issuer)
		{
			free(issuer);
			issuer = NULL;
		}
		if (fingerprint)
		{
			free(fingerprint);
			fingerprint = NULL;
		}
	}
}