-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.c
299 lines (258 loc) · 8.78 KB
/
client.c
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "echo_client.h"
#include <arpa/nameser.h>
#include <netinet/in.h>
#include <resolv.h>
#include <string.h>
#include <pthread.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <openssl/provider.h>
#define BUF_SIZE 10000
#if MODE
int DNS = 1;
#else
int DNS = 0;
#endif
// 0 = false; //normal TLS 1.3
// 1 = true; //ZTLS
struct DNS_info{
struct {
time_t validity_period_not_before; //gmt unix time
time_t validity_period_not_after; //gmt unix time
uint32_t dns_cache_id;
uint32_t max_early_data_size;
} DNSCacheInfo;
struct {
uint8_t *extension_type;
uint16_t *extension_data;
} EncryptedExtensions;
struct {
uint8_t group;
EVP_PKEY *skey; // server's keyshare
} KeyShareEntry;
X509* cert; // server's cert
struct {
uint8_t certificate_request_context;
uint16_t extensions;
} CertRequest;
struct {
uint16_t signature_algorithms;
unsigned char cert_verify[BUF_SIZE]; // signature
} CertVerifyEntry;
} dns_info;
static void init_openssl();
static SSL_CTX *create_context();
static void keylog_callback(const SSL* ssl, const char *line);
static size_t resolve_hostname(const char *host, const char *port, struct sockaddr_storage *addr);
static void configure_connection(SSL *ssl);
static void error_handling(char *message);
static int dns_info_add_cb(SSL *s, unsigned int ext_type,
unsigned int context,
const unsigned char **out,
size_t *outlen, X509 *x, size_t chainidx,
int *al, void *arg);
static void dns_info_free_cb(SSL *s, unsigned int ext_type,
unsigned int context,
const unsigned char *out,
void *add_arg);
static int ext_parse_cb(SSL *s, unsigned int ext_type,
const unsigned char *in,
size_t inlen, int *al, void *parse_arg);
static time_t is_datetime(const char *datetime);
static void init_tcp_sync(char *argv[], struct sockaddr_storage * addr, int sock, int * is_start);
struct arg_struct2 {
char ** argv;
unsigned char * query_buffer;
int buffer_size;
unsigned char ** tlsa_record_all;
int * is_start;
};
int main(int argc, char *argv[]){
res_init();
const char *hostname = "192.168.232.128";
init_openssl();
SSL_CTX *ctx = create_context();
// static ctx configurations
SSL_CTX_load_verify_locations(ctx, "./dns/cert/dilithium2_crt.pem", "./dns/cert/");
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); // SSL_VERIFY_NONE
SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
SSL_CTX_set_keylog_callback(ctx, keylog_callback);
SSL * ssl = NULL;
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8000);
if(argc != 2){
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock < 0){
error_handling("socket() error");
}
if (inet_pton(AF_INET, hostname, &server_addr.sin_addr) <= 0) {
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Connection Failed");
exit(EXIT_FAILURE);
}
char * ztls_cert;
struct sockaddr_storage addr;
char txt_record_except_signature[BUF_SIZE];
char *txt_record_all;
unsigned char *tlsa_record_all;
char tlsa_record[BUF_SIZE];
unsigned char query_buffer[4096];
unsigned char query_buffer2[4096];
unsigned char hex_buffer[2000] = "";
unsigned char hex_out[2000];
unsigned char hex_out_cert[4096] ="";
int response;
ns_type type;
type= ns_t_txt;
ns_msg nsMsg;
ns_rr rr;
int is_start = -1;
// log
printf("****start****\n");
if (!DNS) {
struct timespec begin;
clock_gettime(CLOCK_MONOTONIC, &begin);
printf("start : %f\n",(begin.tv_sec) + (begin.tv_nsec) / 1000000000.0);
}
//=============================================================
// Dynamic interaction start
//=============================================================
// get TXT record & dynamic ctx configurations for ZTLS
//OSSL_PROVIDER_load(OSSL_LIB_CTX_new(), "oqsprovider");
is_start = 1;
//init_tcp_sync(argv, &addr, sock, &is_start);
ssl = SSL_new(ctx);
//printf("SSL_set1_groups_list: %d\n",SSL_set1_groups_list(ssl, "kyber512"));
if(!SSL_set1_groups_list(ssl, "kyber512"))
error_handling("fail to set kyber512");
SSL_set_wfd(ssl, DNS); // fd : 1 => ZTLS, fd : 0 => TLS 1.3
// threads join
SSL_set_fd(ssl, sock);
/*
* handshake start
*/
configure_connection(ssl); // SSL do handshake
char message[BUF_SIZE];
int str_len;
struct timespec send_ctos, receive_ctos;
if(!DNS){ // normal TLS 1.3
memcpy(message, "hello\n", 6);
SSL_write(ssl, message, strlen(message));
clock_gettime(CLOCK_MONOTONIC, &send_ctos);
printf("send : %s", message);
printf("%f\n",(send_ctos.tv_sec) + (send_ctos.tv_nsec) / 1000000000.0);
if((str_len = SSL_read(ssl, message, BUF_SIZE-1))<=0){
printf("error\n");
}
message[str_len] = 0;
clock_gettime(CLOCK_MONOTONIC, &receive_ctos);
printf("Message from server: %s", message);
printf("%f\n",(receive_ctos.tv_sec) + (receive_ctos.tv_nsec) / 1000000000.0);
}
SSL_free(ssl);
close(sock);
SSL_CTX_free(ctx);
EVP_cleanup();
return 0;
}
static void init_tcp_sync(char *argv[], struct sockaddr_storage * addr, int sock, int * is_start) {
while(*is_start < 0) { //for prototyping. next, use signal.
//nothing
}
struct timespec begin1, begin2;
clock_gettime(CLOCK_MONOTONIC, &begin1);
printf("start A and AAAA DNS records query : %f\n",(begin1.tv_sec) + (begin1.tv_nsec) / 1000000000.0);
size_t len = resolve_hostname(argv[1], argv[2], addr);
clock_gettime(CLOCK_MONOTONIC, &begin2);
printf("complete A and AAAA DNS records query : %f\n",(begin2.tv_sec) + (begin2.tv_nsec) / 1000000000.0);
if(connect(sock, (struct sockaddr*) addr, len) < 0){
error_handling("connect() error!");
}else{
clock_gettime(CLOCK_MONOTONIC, &begin2);
printf("complete TCP Sync : %f\n",(begin2.tv_sec) + (begin2.tv_nsec) / 1000000000.0);
}
}
static void init_openssl(){
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
}
static SSL_CTX *create_context(){
SSL_CTX* ctx = SSL_CTX_new(SSLv23_client_method());
if(!ctx) error_handling("fail to create ssl context");
SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
return ctx;
}
/*
* verify
* set version
*/
static void keylog_callback(const SSL* ssl, const char *line){
// printf("==============================================\n");
// printf("%s\n", line);
}
static size_t resolve_hostname(const char *host, const char *port, struct sockaddr_storage *addr){
struct addrinfo hint;
memset(&hint, 0, sizeof(struct addrinfo));
hint.ai_family = AF_INET;
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = IPPROTO_TCP;
struct addrinfo *res = 0;
if(getaddrinfo(host, port, &hint, &res) != 0)
error_handling("fail to transform address");
size_t len = res->ai_addrlen;
memcpy(addr, res->ai_addr, len);
freeaddrinfo(res);
return len;
}
static void configure_connection(SSL *ssl){
SSL_set_tlsext_host_name(ssl, "ztls.net");
SSL_set_connect_state(ssl);
if(SSL_do_handshake(ssl) <= 0){
ERR_print_errors_fp(stderr);
error_handling("fail to do handshake");
}
}
static void error_handling(char *message){
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
static int dns_info_add_cb(SSL *s, unsigned int ext_type,
unsigned int context,
const unsigned char **out,
size_t *outlen, X509 *x, size_t chainidx,
int *al, void *arg)
{
if (context == SSL_EXT_CLIENT_HELLO) {
*out = (unsigned char*)malloc(sizeof(char*)*4);
memcpy((void*)*out, &(&dns_info)->DNSCacheInfo.dns_cache_id, 4);
*outlen = 4;
}
return 1;
}
static void dns_info_free_cb(SSL *s, unsigned int ext_type,
unsigned int context,
const unsigned char *out,
void *add_arg){
OPENSSL_free((unsigned char *)out);
}
static int ext_parse_cb(SSL *s, unsigned int ext_type,
const unsigned char *in,
size_t inlen, int *al, void *parse_arg)
{
return 1;
}
static time_t is_datetime(const char *datetime){
// datetime format is YYYYMMDDHHMMSSz
struct tm time_val;
strptime(datetime, "%Y%m%d%H%M%Sz", &time_val);
return mktime(&time_val); // Invalid
}