1 /* ssl.c - NeonServ v5.4
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "ClientSocket.h"
24 SSL_load_error_strings();
28 void ssl_connect(struct ClientSocket *client) {
30 client->sslconn = NULL;
31 if(!(client->flags & SOCKET_FLAG_CONNECTED)) return;
32 struct SSLConnection *sslconn = malloc(sizeof(*sslconn));
33 sslconn->sslContext = SSL_CTX_new(SSLv23_client_method());
34 if(!sslconn->sslContext) goto ssl_connect_err;
35 sslconn->sslHandle = SSL_new(sslconn->sslContext);
36 if(!sslconn->sslHandle) goto ssl_connect_err;
37 if(!SSL_set_fd(sslconn->sslHandle, client->sock)) goto ssl_connect_err;
38 if(SSL_connect(sslconn->sslHandle) != 1) goto ssl_connect_err;
39 client->sslconn = sslconn;
46 void ssl_disconnect(struct ClientSocket *client) {
48 if(!client->sslconn) return;
49 SSL_shutdown(client->sslconn->sslHandle);
50 SSL_free(client->sslconn->sslHandle);
51 SSL_CTX_free(client->sslconn->sslContext);
52 free(client->sslconn);
53 client->sslconn = NULL;
57 int ssl_read(struct ClientSocket *client, char *buffer, int len) {
59 if(!client->sslconn) return -2;
60 return SSL_read(client->sslconn->sslHandle, buffer, len);
65 int ssl_write(struct ClientSocket *client, char *buffer, int len) {
67 if(!client->sslconn) return -2;
68 return SSL_write(client->sslconn->sslHandle, buffer, len);