2 * MatrixSSL helper functions
4 * Copyright (C) 2005 Nicolas Thill <nthill@free.fr>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * Portions borrowed from MatrixSSL example code
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netinet/tcp.h>
23 #include <arpa/inet.h>
30 #include "matrixssl_helper.h"
32 #define SSL_SOCKET_EOF 0x0001
33 #define SSL_SOCKET_CLOSE_NOTIFY 0x0002
35 #define min(a, b) ( (a) < (b) ) ? (a) : (b)
37 static int _ssl_read(SSL
*ssl
, char *buf
, int len
);
38 static int _ssl_write(SSL
*ssl
, char *buf
, int len
);
39 static void _ssl_setSocketBlock(int fd
);
40 static void _ssl_setSocketNonblock(int fd
);
41 static void _ssl_closeSocket(int fd
);
44 SSL
* SSL_new(sslKeys_t
*keys
)
47 ssl
= (SSL
*)malloc(sizeof(SSL
));
52 if ( matrixSslNewSession(&(ssl
->ssl
), ssl
->keys
, NULL
, SSL_FLAGS_SERVER
) < 0 ) {
55 ssl
->insock
.size
= 1024;
56 ssl
->insock
.buf
= ssl
->insock
.start
= ssl
->insock
.end
=
57 (unsigned char *)malloc(ssl
->insock
.size
);
59 ssl
->outsock
.size
= 1024;
60 ssl
->outsock
.buf
= ssl
->outsock
.start
= ssl
->outsock
.end
=
61 (unsigned char *)malloc(ssl
->outsock
.size
);
64 ssl
->inbuf
.buf
= ssl
->inbuf
.start
= ssl
->inbuf
.end
= NULL
;
70 int SSL_accept(SSL
*ssl
) {
72 unsigned char buf
[1024];
76 rc
= _ssl_read(ssl
, buf
, sizeof(buf
));
78 if (ssl
->status
== SSL_SOCKET_EOF
|| ssl
->status
== SSL_SOCKET_CLOSE_NOTIFY
) {
82 if (matrixSslHandshakeIsComplete(ssl
->ssl
) == 0) {
96 void SSL_set_fd(SSL
*ssl
, int fd
) {
101 int SSL_read(SSL
*ssl
, char *buf
, int len
) {
104 rc
= _ssl_read(ssl
, buf
, len
);
106 if (rc
< 0 || ssl
->status
== SSL_SOCKET_EOF
|| ssl
->status
== SSL_SOCKET_CLOSE_NOTIFY
) {
107 _ssl_closeSocket(ssl
->fd
);
116 int SSL_write(SSL
*ssl
, char *buf
, int len
) {
119 rc
= _ssl_write(ssl
, buf
, len
);
130 void SSL_free(SSL
* ssl
)
132 matrixSslDeleteSession(ssl
->ssl
);
133 if (ssl
->insock
.buf
) {
134 free(ssl
->insock
.buf
);
136 if (ssl
->outsock
.buf
) {
137 free(ssl
->outsock
.buf
);
139 if (ssl
->inbuf
.buf
) {
140 free(ssl
->inbuf
.buf
);
147 static void _ssl_setSocketBlock(int fd
)
149 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) & ~O_NONBLOCK
);
150 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
154 static void _ssl_setSocketNonblock(int fd
)
156 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
160 static void _ssl_closeSocket(int fd
)
165 _ssl_setSocketNonblock(fd
);
166 if (shutdown(fd
, 1) >= 0) {
167 while (recv(fd
, buf
, sizeof(buf
), 0) > 0);
174 static int _ssl_read(SSL
*ssl
, char *buf
, int len
)
176 int bytes
, rc
, remaining
;
177 unsigned char error
, alertLevel
, alertDescription
, performRead
;
181 if (ssl
->ssl
== NULL
|| len
<= 0) {
185 If inbuf is valid, then we have previously decoded data that must be
186 returned, return as much as possible. Once all buffered data is
187 returned, free the inbuf.
189 if (ssl
->inbuf
.buf
) {
190 if (ssl
->inbuf
.start
< ssl
->inbuf
.end
) {
191 remaining
= (int)(ssl
->inbuf
.end
- ssl
->inbuf
.start
);
192 bytes
= (int)min(len
, remaining
);
193 memcpy(buf
, ssl
->inbuf
.start
, bytes
);
194 ssl
->inbuf
.start
+= bytes
;
197 free(ssl
->inbuf
.buf
);
198 ssl
->inbuf
.buf
= NULL
;
201 Pack the buffered socket data (if any) so that start is at zero.
203 if (ssl
->insock
.buf
< ssl
->insock
.start
) {
204 if (ssl
->insock
.start
== ssl
->insock
.end
) {
205 ssl
->insock
.start
= ssl
->insock
.end
= ssl
->insock
.buf
;
207 memmove(ssl
->insock
.buf
, ssl
->insock
.start
, ssl
->insock
.end
- ssl
->insock
.start
);
208 ssl
->insock
.end
-= (ssl
->insock
.start
- ssl
->insock
.buf
);
209 ssl
->insock
.start
= ssl
->insock
.buf
;
213 Read up to as many bytes as there are remaining in the buffer. We could
214 Have encrypted data already cached in conn->insock, but might as well read more
219 if (ssl
->insock
.end
== ssl
->insock
.start
|| performRead
) {
221 bytes
= recv(ssl
->fd
, (char *)ssl
->insock
.end
,
222 (int)((ssl
->insock
.buf
+ ssl
->insock
.size
) - ssl
->insock
.end
), MSG_NOSIGNAL
);
228 ssl
->status
= SSL_SOCKET_EOF
;
231 ssl
->insock
.end
+= bytes
;
234 Define a temporary sslBuf
236 ssl
->inbuf
.start
= ssl
->inbuf
.end
= ssl
->inbuf
.buf
= (unsigned char *)malloc(len
);
237 ssl
->inbuf
.size
= len
;
239 Decode the data we just read from the socket
244 alertDescription
= 0;
246 rc
= matrixSslDecode(ssl
->ssl
, &ssl
->insock
, &ssl
->inbuf
, &error
, &alertLevel
,
250 Successfully decoded a record that did not return data or require a response.
255 Successfully decoded an application data record, and placed in tmp buf
257 case SSL_PROCESS_DATA
:
259 Copy as much as we can from the temp buffer into the caller's buffer
260 and leave the remainder in conn->inbuf until the next call to read
261 It is possible that len > data in buffer if the encoded record
262 was longer than len, but the decoded record isn't!
264 rc
= (int)(ssl
->inbuf
.end
- ssl
->inbuf
.start
);
266 memcpy(buf
, ssl
->inbuf
.start
, rc
);
267 ssl
->inbuf
.start
+= rc
;
270 We've decoded a record that requires a response into tmp
271 If there is no data to be flushed in the out buffer, we can write out
272 the contents of the tmp buffer. Otherwise, we need to append the data
273 to the outgoing data buffer and flush it out.
275 case SSL_SEND_RESPONSE
:
276 bytes
= send(ssl
->fd
, (char *)ssl
->inbuf
.start
,
277 (int)(ssl
->inbuf
.end
- ssl
->inbuf
.start
), MSG_NOSIGNAL
);
280 if (ssl
->status
!= EAGAIN
) {
285 ssl
->inbuf
.start
+= bytes
;
286 if (ssl
->inbuf
.start
< ssl
->inbuf
.end
) {
288 This must be a non-blocking socket since it didn't all get sent
289 out and there was no error. We want to finish the send here
290 simply because we are likely in the SSL handshake.
292 _ssl_setSocketBlock(ssl
->fd
);
293 bytes
= send(ssl
->fd
, (char *)ssl
->inbuf
.start
,
294 (int)(ssl
->inbuf
.end
- ssl
->inbuf
.start
), MSG_NOSIGNAL
);
299 ssl
->inbuf
.start
+= bytes
;
301 Can safely set back to non-blocking because we wouldn't
302 have got here if this socket wasn't non-blocking to begin with.
304 _ssl_setSocketNonblock(ssl
->fd
);
306 ssl
->inbuf
.start
= ssl
->inbuf
.end
= ssl
->inbuf
.buf
;
309 There was an error decoding the data, or encoding the out buffer.
310 There may be a response data in the out buffer, so try to send.
311 We try a single hail-mary send of the data, and then close the socket.
312 Since we're closing on error, we don't worry too much about a clean flush.
315 if (ssl
->inbuf
.start
< ssl
->inbuf
.end
) {
316 _ssl_setSocketNonblock(ssl
->fd
);
317 bytes
= send(ssl
->fd
, (char *)ssl
->inbuf
.start
,
318 (int)(ssl
->inbuf
.end
- ssl
->inbuf
.start
), MSG_NOSIGNAL
);
322 We've decoded an alert. The level and description passed into
323 matrixSslDecode are filled in with the specifics.
326 if (alertDescription
== SSL_ALERT_CLOSE_NOTIFY
) {
327 ssl
->status
= SSL_SOCKET_CLOSE_NOTIFY
;
332 We have a partial record, we need to read more data off the socket.
333 If we have a completely full conn->insock buffer, we'll need to grow it
334 here so that we CAN read more data when called the next time.
337 if (ssl
->insock
.start
== ssl
->insock
.buf
&& ssl
->insock
.end
==
338 (ssl
->insock
.buf
+ ssl
->insock
.size
)) {
339 if (ssl
->insock
.size
> SSL_MAX_BUF_SIZE
) {
342 ssl
->insock
.size
*= 2;
343 ssl
->insock
.start
= ssl
->insock
.buf
=
344 (unsigned char *)realloc(ssl
->insock
.buf
, ssl
->insock
.size
);
345 ssl
->insock
.end
= ssl
->insock
.buf
+ (ssl
->insock
.size
/ 2);
349 free(ssl
->inbuf
.buf
);
350 ssl
->inbuf
.buf
= NULL
;
356 The out buffer is too small to fit the decoded or response
357 data. Increase the size of the buffer and call decode again
360 ssl
->inbuf
.size
*= 2;
361 if (ssl
->inbuf
.buf
!= (unsigned char*)buf
) {
362 free(ssl
->inbuf
.buf
);
363 ssl
->inbuf
.buf
= NULL
;
365 ssl
->inbuf
.start
= ssl
->inbuf
.end
= ssl
->inbuf
.buf
=
366 (unsigned char *)malloc(ssl
->inbuf
.size
);
370 We consolidated some of the returns here because we must ensure
371 that conn->inbuf is cleared if pointing at caller's buffer, otherwise
372 it will be freed later on.
375 if (ssl
->inbuf
.buf
== (unsigned char*)buf
) {
376 ssl
->inbuf
.buf
= NULL
;
380 if (ssl
->inbuf
.buf
== (unsigned char*)buf
) {
381 ssl
->inbuf
.buf
= NULL
;
387 int _ssl_write(SSL
*ssl
, char *buf
, int len
)
393 Pack the buffered socket data (if any) so that start is at zero.
395 if (ssl
->outsock
.buf
< ssl
->outsock
.start
) {
396 if (ssl
->outsock
.start
== ssl
->outsock
.end
) {
397 ssl
->outsock
.start
= ssl
->outsock
.end
= ssl
->outsock
.buf
;
399 memmove(ssl
->outsock
.buf
, ssl
->outsock
.start
, ssl
->outsock
.end
- ssl
->outsock
.start
);
400 ssl
->outsock
.end
-= (ssl
->outsock
.start
- ssl
->outsock
.buf
);
401 ssl
->outsock
.start
= ssl
->outsock
.buf
;
405 If there is buffered output data, the caller must be trying to
406 send the same amount of data as last time. We don't support
407 sending additional data until the original buffered request has
408 been completely sent.
410 if (ssl
->outBufferCount
> 0 && len
!= ssl
->outBufferCount
) {
414 If we don't have buffered data, encode the caller's data
416 if (ssl
->outBufferCount
== 0) {
418 rc
= matrixSslEncode(ssl
->ssl
, (unsigned char *)buf
, len
, &ssl
->outsock
);
423 if (ssl
->outsock
.size
> SSL_MAX_BUF_SIZE
) {
426 ssl
->outsock
.size
*= 2;
428 (unsigned char *)realloc(ssl
->outsock
.buf
, ssl
->outsock
.size
);
429 ssl
->outsock
.end
= ssl
->outsock
.buf
+ (ssl
->outsock
.end
- ssl
->outsock
.start
);
430 ssl
->outsock
.start
= ssl
->outsock
.buf
;
435 We've got data to send.
437 rc
= send(ssl
->fd
, (char *)ssl
->outsock
.start
,
438 (int)(ssl
->outsock
.end
- ssl
->outsock
.start
), MSG_NOSIGNAL
);
443 ssl
->outsock
.start
+= rc
;
445 If we wrote it all return the length, otherwise remember the number of
446 bytes passed in, and return 0 to be called again later.
448 if (ssl
->outsock
.start
== ssl
->outsock
.end
) {
449 ssl
->outBufferCount
= 0;
452 ssl
->outBufferCount
= len
;
This page took 0.06995 seconds and 5 git commands to generate.