-
Notifications
You must be signed in to change notification settings - Fork 153
/
websSSL.c
413 lines (351 loc) · 10 KB
/
websSSL.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* websSSL.c -- MatrixSSL Layer
*
* (C)Copyright 2002-2010 PeerSec Networks
* All Rights Reserved
*
*/
/******************************************************************************/
#ifdef WEBS_SSL_SUPPORT
/******************************** Description *********************************/
/*
* GoAhead Webs HTTPS implementation uses PeerSec Networks MatrixSSL
*/
/********************************* Includes ***********************************/
#include "wsIntrn.h"
#include "websSSL.h"
#include "webs.h"
#include "matrixSSLSocket.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/******************************* Definitions **********************************/
static int sslListenSock = -1; /* Listen socket */
static sslKeys_t *sslKeys = NULL;
/******************************* Prototypes **********************************/
static int websSSLAccept(int sid, char *ipaddr, int port, int listenSid);
static void websSSLSocketEvent(int sid, int mask, void *iwp);
static int websSSLReadEvent(webs_t wp);
/******************************************************************************/
/*
* Start up the SSL Context for the application, and start a listen on the
* SSL port (usually 443, and defined by WEBS_DEFAULT_SSL_PORT)
* Return 0 on success, -1 on failure.
*
* Not freeing keys or calling matrixSslClose on failures because
* websSSLClose will be called at application close to do that
*/
int websSSLOpen()
{
if (matrixSslOpen() < 0) {
return -1;
}
if (matrixSslNewKeys(&sslKeys) < 0) {
trace(0, T("Failed to allocate keys in websSSLOpen\n"));
return -1;
}
if (matrixSslLoadRsaKeys(sslKeys, DEFAULT_CERT_FILE, DEFAULT_KEY_FILE,
NULL /* privPass */,
NULL /* trustedCAFile */) < 0) {
trace(0, T("Failed to read certificate %s in websSSLOpen\n"),
DEFAULT_CERT_FILE);
trace(0, T("SSL support is disabled\n"));
return -1;
}
#ifdef USE_NONBLOCKING_SSL_SOCKETS
sslListenSock = socketOpenConnection(NULL, WEBS_DEFAULT_SSL_PORT,
websSSLAccept, SOCKET_MYOWNBUFFERS);
#else
sslListenSock = socketOpenConnection(NULL, WEBS_DEFAULT_SSL_PORT,
websSSLAccept, SOCKET_BLOCK | SOCKET_MYOWNBUFFERS);
#endif
if (sslListenSock < 0) {
trace(0, T("SSL: Unable to open SSL socket on port %d.\n"),
WEBS_DEFAULT_SSL_PORT);
return -1;
}
trace(0, T("webs: Listening for HTTPS requests at port %d\n"),
WEBS_DEFAULT_SSL_PORT);
return 0;
}
/******************************************************************************/
/*
* Accept a connection
*/
int websSSLAccept(int sid, char *ipaddr, int port, int listenSid)
{
webs_t wp;
int wid;
a_assert(ipaddr && *ipaddr);
a_assert(sid >= 0);
a_assert(port >= 0);
/*
* Allocate a new handle for this accepted connection. This will allocate
* a webs_t structure in the webs[] list
*/
if ((wid = websAlloc(sid)) < 0) {
return -1;
}
wp = webs[wid];
a_assert(wp);
wp->listenSid = listenSid;
ascToUni(wp->ipaddr, ipaddr, min(sizeof(wp->ipaddr), strlen(ipaddr)+1));
/*
* Check if this is a request from a browser on this system. This is useful
* to know for permitting administrative operations only for local access
*/
if (gstrcmp(wp->ipaddr, T("127.0.0.1")) == 0 ||
gstrcmp(wp->ipaddr, websIpaddr) == 0 ||
gstrcmp(wp->ipaddr, websHost) == 0) {
wp->flags |= WEBS_LOCAL_REQUEST;
}
/*
* Since the acceptance came in on this channel, it must be secure
*/
wp->flags |= WEBS_SECURE;
/*
* Arrange for websSocketEvent to be called when read data is available
*/
socketCreateHandler(sid, SOCKET_READABLE, websSSLSocketEvent, wp);
/*
* Arrange for a timeout to kill hung requests
*/
wp->timeout = emfSchedCallback(WEBS_TIMEOUT, websTimeout, (void *) wp);
trace(8, T("websSSLAccept(): webs: accept request\n"));
return 0;
}
/******************************************************************************/
/*
* Perform a read of the SSL socket
*/
int websSSLRead(websSSL_t *wsp, char_t *buf, int len)
{
a_assert(wsp);
a_assert(buf);
return sslRead(wsp->sslConn, buf, len);
}
/******************************************************************************/
/*
* Perform a gets of the SSL socket, returning an balloc'ed string
*
* Get a string from a socket. This returns data in *buf in a malloced string
* after trimming the '\n'. If there is zero bytes returned, *buf will be set
* to NULL. If doing non-blocking I/O, it returns -1 for error, EOF or when
* no complete line yet read. If doing blocking I/O, it will block until an
* entire line is read. If a partial line is read socketInputBuffered or
* socketEof can be used to distinguish between EOF and partial line still
* buffered. This routine eats and ignores carriage returns.
*/
int websSSLGets(websSSL_t *wsp, char_t **buf)
{
socket_t *sp;
ringq_t *lq;
char c;
int len;
webs_t wp;
int sid;
int numBytesReceived;
a_assert(wsp);
a_assert(buf);
*buf = NULL;
wp = wsp->wp;
sid = wp->sid;
if ((sp = socketPtr(sid)) == NULL) {
return -1;
}
lq = &sp->lineBuf;
while (1) {
/* read one byte at a time */
numBytesReceived = sslRead(wsp->sslConn, &c, 1);
if (numBytesReceived < 0) {
return -1;
}
if (numBytesReceived == 0) {
/*
* If there is a partial line and we are at EOF, pretend we saw a '\n'
*/
if (ringqLen(lq) > 0 && (sp->flags & SOCKET_EOF)) {
c = '\n';
} else {
len = ringqLen(lq);
if (len > 0) {
*buf = ballocAscToUni((char *)lq->servp, len);
} else {
*buf = NULL;
}
ringqFlush(lq);
return len;
}
}
/*
* If a newline is seen, return the data excluding the new line to the
* caller. If carriage return is seen, just eat it.
*/
if (c == '\n') {
len = ringqLen(lq);
if (len > 0) {
*buf = ballocAscToUni((char *)lq->servp, len);
} else {
*buf = NULL;
}
ringqFlush(lq);
return len;
} else if (c == '\r') {
continue;
}
ringqPutcA(lq, c);
}
return 0;
}
/******************************************************************************/
/*
* The webs socket handler. Called in response to I/O. We just pass control
* to the relevant read or write handler. A pointer to the webs structure
* is passed as a (void *) in iwp.
*/
static void websSSLSocketEvent(int sid, int mask, void *iwp)
{
webs_t wp;
wp = (webs_t) iwp;
a_assert(wp);
if (! websValid(wp)) {
return;
}
if (mask & SOCKET_READABLE) {
websSSLReadEvent(wp);
}
if (mask & SOCKET_WRITABLE) {
if (wp->writeSocket) {
(*wp->writeSocket)(wp);
}
}
}
/******************************************************************************/
/*
* Handler for SSL Read Events
*/
static int websSSLReadEvent (webs_t wp)
{
int ret = 07, sock, resume;
socket_t *sptr;
sslConn_t* sslConn;
a_assert (wp);
a_assert(websValid(wp));
sptr = socketPtr(wp->sid);
a_assert(sptr);
sock = sptr->sock;
if (wp->wsp == NULL) {
resume = 0;
} else {
resume = 1;
sslConn = (wp->wsp)->sslConn;
}
/*
This handler is essentially two back-to-back calls to sslRead. The
first is here in sslAccept where the handshake is to take place. The
second is in websReadEvent below where it is expected the client was
contacting us to send an HTTP request and we need to read that off.
The introduction of non-blocking sockets has made this a little more
confusing becuase it is possible to return from either of these sslRead
calls in a WOULDBLOCK state. It doesn't ultimately matter, however,
because sslRead is fully stateful so it doesn't matter how/when it gets
invoked later (as long as the correct sslConn is used (which is what the
resume variable is all about))
*/
if (sslAccept(&sslConn, sock, sslKeys, resume, NULL) < 0) {
websTimeoutCancel(wp);
socketCloseConnection(wp->sid);
websFree(wp);
return -1;
}
if (resume == 0) {
/*
* Create the SSL data structure in the wp.
*/
wp->wsp = balloc(B_L, sizeof(websSSL_t));
a_assert (wp->wsp);
(wp->wsp)->sslConn = sslConn;
(wp->wsp)->wp = wp;
}
/*
* Call the default Read Event
*/
websReadEvent(wp);
return ret;
}
/******************************************************************************/
/*
* Return TRUE if websSSL has been opened
*/
int websSSLIsOpen()
{
return (sslListenSock != -1);
}
/******************************************************************************/
/*
* Perform a write to the SSL socket
*/
int websSSLWrite(websSSL_t *wsp, char_t *buf, int len)
{
int sslBytesSent = 0;
a_assert(wsp);
a_assert(buf);
if (wsp == NULL) {
return -1;
}
/*
Send on socket. If non-blocking, 0 may be returned from this call,
indicating that data could not be sent due to EWOULDBLOCK.
*/
if ((sslBytesSent = sslWrite(wsp->sslConn, buf, len)) < 0) {
sslBytesSent = -1;
}
return sslBytesSent;
}
/******************************************************************************/
/*
* Return Eof for the underlying socket
*/
int websSSLEof(websSSL_t *wsp)
{
webs_t wp;
int sid;
a_assert(wsp);
wp = wsp->wp;
sid = wp->sid;
return socketEof(sid);
}
/******************************************************************************/
/*
* Flush stub for compatibility
*/
int websSSLFlush(websSSL_t *wsp)
{
a_assert(wsp);
/* Autoflush - do nothing */
return 0;
}
/******************************************************************************/
/*
* Free SSL resources
*/
int websSSLFree(websSSL_t *wsp)
{
if (wsp != NULL) {
sslWriteClosureAlert(wsp->sslConn);
sslFreeConnection(&wsp->sslConn);
bfree(B_L, wsp);
}
return 0;
}
/******************************************************************************/
/*
* Stops the SSL system
*/
void websSSLClose()
{
matrixSslDeleteKeys(sslKeys);
matrixSslClose();
}
/******************************************************************************/
#endif /* WEBS_SSL_SUPPORT */