-
Notifications
You must be signed in to change notification settings - Fork 9
/
cio.c
314 lines (272 loc) · 8.04 KB
/
cio.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* Asynchronous SSE Server
* Author: Peter Deak ([email protected])
* License: GPL
*/
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <pwd.h>
#include <openssl/ssl.h>
#include <openssl/bio.h>
#include "hasses.h"
#include "cdata.h"
#include "cio.h"
#define CIO(a) ((struct CioData *)(a->cio))
struct CioData
{
SSL *ssl;
BIO *in_bio;
BIO *out_bio;
};
int no_ssl = 0;
SSL_CTX *ssl_ctx;
int (*cio_high_read_callback)(struct CliConn *,char *) = NULL;
int (*cio_low_write_callback)(struct CliConn *,char *,int) = NULL;
const char *cio_ssl_error_text(int e);
int cio_init(int use_ssl,const char *certfile,const char *pkeyfile)
{
if(cio_high_read_callback == NULL || cio_low_write_callback == NULL)
{
toLog(0,"SSL Error, you must set cio_high_read_callback and cio_low_write_callback functions!\n");
return 1;
}
no_ssl = !use_ssl;
if(no_ssl)
return 0;
toLog(1,"Initialize SSL.\n");
SSL_load_error_strings(); // readable error messages
SSL_library_init(); // initialize library
ssl_ctx = SSL_CTX_new(TLS_server_method()); // create context
if (ssl_ctx) {
SSL_CTX_set_min_proto_version(ssl_ctx, TLS1_2_VERSION);
}
if(!SSL_CTX_use_certificate_file(ssl_ctx,certfile,SSL_FILETYPE_PEM))
{
toLog(0,"SSL Error, failed to add Certificate key file: \"%s\"\n",certfile);
return 1;
}
else
toLog(2,"SSL: Certification key added.\n");
if(!SSL_CTX_use_PrivateKey_file(ssl_ctx,pkeyfile,SSL_FILETYPE_PEM))
{
toLog(0,"SSL Error, failed to add Private key file: \"%s\"\n",pkeyfile);
return 1;
}
else
toLog(2,"SSL: Private key added.\n");
if(!SSL_CTX_check_private_key(ssl_ctx))
{
toLog(0,"SSL Error, Key check failed! (SSL_CTX_check_private_key)\n");
return 1;
}
else
toLog(2,"SSL: Key check succes.\n");
return 0;
}
int cio_init_ssl(struct CliConn *client)
{
client->cio = (struct CioData *)malloc(sizeof(struct CioData));
CIO(client)->ssl = SSL_new(ssl_ctx);
if(!CIO(client)->ssl)
{
toLog(0,"SSL Error: Cannot create new SSL! <%d>\n",client->descr);
return 1;
}
CIO(client)->in_bio = BIO_new(BIO_s_mem());
if(CIO(client)->in_bio == NULL)
{
toLog(0,"SSL Error: Cannot allocate read BIO.\n");
return 1;
}
//BIO_set_mem_eof_return(CIO(client)->in_bio, -1);
CIO(client)->out_bio = BIO_new(BIO_s_mem());
if(CIO(client)->out_bio == NULL)
{
toLog(0,"SSL Error: cannot allocate write BIO.\n");
return 1;
}
//BIO_set_mem_eof_return(CIO(client)->out_bio, -1);
SSL_set_bio(CIO(client)->ssl, CIO(client)->in_bio, CIO(client)->out_bio);
SSL_set_accept_state(CIO(client)->ssl);
client->status = STATUS_SSLACPT;
return 0;
}
void write_outbio_pending_to_low(struct CliConn *client)
{
int pending = BIO_ctrl_pending(CIO(client)->out_bio);
if(pending > 0)
{
int read;
char out_buffer[MAX_READ_SIZE];
read = BIO_read(CIO(client)->out_bio,out_buffer,sizeof(out_buffer));
(*cio_low_write_callback)(client,out_buffer,read);
}
}
int cio_high_write(struct CliConn *client,char *buffer)
{
if(no_ssl)
return (*cio_low_write_callback)(client,buffer,strlen(buffer));
if(client->cio == NULL)
if(cio_init_ssl(client))
return 1;
switch(client->status)
{
case STATUS_UNKNOWN:
case STATUS_NEW:
case STATUS_SSLACPT:
case STATUS_END:
return 1;
default:
break;
}
SSL_write(CIO(client)->ssl,buffer,strlen(buffer));
write_outbio_pending_to_low(client);
return 0;
}
int cio_low_read(struct CliConn *client,char *buffer,int length)
{
if(no_ssl)
{
buffer[length] = '\0';
return (*cio_high_read_callback)(client,buffer);
}
if(client->cio == NULL)
if(cio_init_ssl(client))
return 1;
char rio_buffer[MAX_READ_SIZE];
int written = 0;
int read = 0;
//Waiting data to be read by ssl
if(length > 0)
written = BIO_write(CIO(client)->in_bio,buffer,length);
if(written > 0 || 1)
{
if(!SSL_is_init_finished(CIO(client)->ssl))
{
int rv,rv2;
rv = SSL_accept(CIO(client)->ssl);
if(rv==1)
{
toLog(2,"SSL_accept() succesful, ssl connection established.\n");
client->status = STATUS_NEW;
write_outbio_pending_to_low(client);
}
if(rv==0)
{
rv2 = SSL_get_error(CIO(client)->ssl,rv);
toLog(1,"SSL_accept ERROR, Connection shut down. (0):\nSSL_get_error says %d, %s\n",
rv2,cio_ssl_error_text(rv2));
}
if(rv<0)
{
rv2 = SSL_get_error(CIO(client)->ssl,rv);
if(rv2 == SSL_ERROR_WANT_READ || rv2 == SSL_ERROR_WANT_WRITE)
write_outbio_pending_to_low(client);
else
toLog(1,"SSL_accept returned <0 (%d):\nSSL_get_error says %d, %s\n",
rv,rv2,cio_ssl_error_text(rv2));
}
}
}
if(SSL_is_init_finished(CIO(client)->ssl))
{
int rv2;
read = SSL_read(CIO(client)->ssl, rio_buffer, sizeof(rio_buffer));
if(read > 0)
{
rio_buffer[read]='\0';
if(strlen(rio_buffer) > 0)
(*cio_high_read_callback)(client,rio_buffer);
}
if(read == 0)
{
rv2 = SSL_get_error(CIO(client)->ssl,read);
toLog(1,"SSL_read returns 0, Error: %d, %s\n",rv2,cio_ssl_error_text(rv2));
}
if(read < 0)
{
rv2 = SSL_get_error(CIO(client)->ssl,read);
if(rv2 == SSL_ERROR_WANT_READ || rv2 == SSL_ERROR_WANT_WRITE)
write_outbio_pending_to_low(client);
else
toLog(1,"SSL_read returned <0 (%d):\nSSL_get_error says %d, %s\n",
read,rv2,cio_ssl_error_text(rv2));
}
}
return 0;
}
int cio_client_close(struct CliConn *client)
{
if(client == NULL)
return 0;
if(client->cio != NULL)
{
SSL_shutdown(CIO(client)->ssl);
SSL_free(CIO(client)->ssl);
toLog(2,"SSL: Shutdown/Free connection.\n");
client->cio = NULL;
}
return 0;
}
int cio_info_text(struct CliConn *client,char *buffer,int maxlen)
{
if(client->cio == NULL)
{
strcpy(buffer,"");
return 0;
}
snprintf(buffer,maxlen,"SSL Status:%s Ver:%s Chiper:%s",
SSL_state_string(CIO(client)->ssl),
SSL_get_version(CIO(client)->ssl),
SSL_get_cipher_name(CIO(client)->ssl)
);
return 1;
}
const char *cio_ssl_error_text(int e)
{
switch(e)
{
case SSL_ERROR_NONE:
return "SSL_ERROR_NONE";
case SSL_ERROR_ZERO_RETURN:
return "SSL_ERROR_ZERO_RETURN";
case SSL_ERROR_WANT_READ:
return "SSL_ERROR_WANT_READ";
case SSL_ERROR_WANT_WRITE:
return "SSL_ERROR_WANT_WRITE";
case SSL_ERROR_WANT_CONNECT:
return "SSL_ERROR_WANT_CONNECT";
case SSL_ERROR_WANT_ACCEPT:
return "SSL_ERROR_WANT_ACCEPT";
case SSL_ERROR_WANT_X509_LOOKUP:
return "SSL_ERROR_WANT_X509_LOOKUP";
case SSL_ERROR_SYSCALL:
return "SSL_ERROR_SYSCALL";
case SSL_ERROR_SSL:
return "SSL_ERROR_SSL";
default:
return "Unknown";
}
}
void cio_high_read_SET(int (*c)(struct CliConn *,char *))
{
cio_high_read_callback = c;
}
void cio_low_write_SET(int (*c)(struct CliConn *,char *,int))
{
cio_low_write_callback = c;
}
//end.