summaryrefslogtreecommitdiffstats
path: root/client.c
blob: f19266abff2c2ca98a005a3b182f43e983438538 (plain)
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
/*	Copyright (C) 2021 Gentoo-libre Install

	This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.

	Additional permission under GNU GPL version 3 section 7

	If you modify this program, or any covered work, by linking or combining it with the OpenSSL project's OpenSSL library (or a
	modified version of that library), containing parts covered by the terms of the OpenSSL or SSLeay licenses, you are
	granted additional permission to convey the resulting work.
*/

#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <openssl/ssl.h>
#include "client.h"
#define MAX 512

int create_socket(char server[], char port[])
	{
	struct addrinfo hints;
	struct addrinfo *result, *rp;
	int sockfd, host;
	size_t len;

	/* Obtain address(es) matching host */
	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_INET;//IPv4 or IPv6
	hints.ai_socktype = SOCK_STREAM;//stream socket
	hints.ai_flags = 0;
	hints.ai_protocol = 0;//just don't set a protocol as openssl doesn't like that

	/*
	getaddrinfo() returns a list of address structures. Try each address until we successfully connect(2).
	If socket(2) (or connect(2)) fails, we (close the socket and) try the next address.
	*/

	host = getaddrinfo(server, port, &hints, &result);
	if (host != 0){fprintf(stderr, "Host not found\n"); return 0;}//host not found

	for (rp = result; rp != NULL; rp = rp->ai_next)
		{
		sockfd = socket(rp->ai_family, rp->ai_socktype,rp->ai_protocol);
		if (sockfd == -1){continue;}
		if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1){break;}//Success
		close(sockfd);
		}

	/* No address connect()'d successfully */
	if (!rp){fprintf(stderr, "Could not connect() to any host\n"); return 0;}

	freeaddrinfo(result);//No longer needed

	return sockfd;
	}

SSL *connectServer(char server[])
	{
	const SSL_METHOD *method;
	SSL_CTX *ctx;
	SSL *tls;
	int sockfd = 0;

	/* attempt to initalize TLS library */
	if(SSL_library_init() < 0){return 0;}

	/* set TLS client hello */
	method = TLS_client_method();

	/* attempt to create new TLS context */
	if (!(ctx = SSL_CTX_new(method))){return 0;}
	SSL_CTX_set_min_proto_version(ctx,TLS1_3_VERSION);
	SSL_CTX_set_max_proto_version(ctx,TLS1_3_VERSION);

	/* create a new TLS connection state object */
	tls = SSL_new(ctx);
	/* set min and max protocol version to TLSv1.3 */
	SSL_set_min_proto_version(tls,TLS1_3_VERSION);
	SSL_set_max_proto_version(tls,TLS1_3_VERSION);

	char port[6] = "6697";

	/* Copy string as you can't write to hardcoded strings */
	char buff[strlen(server)+1];
	memcpy(buff,server,strlen(server)+1);

	char *pos;
	/* if the server contains a slash, we have a port number */
	if (pos = strchr(buff, '/'))
		{
		/* copy port value into port */
		strncpy(port,pos+1,strlen(port));
		/* terminate server string at slash */
		*pos = '\0';
		}

	/* create a TCP socket connection with the server */
	sockfd = create_socket(buff,port);

	/* return 0 on failure */
	if (!sockfd){return 0;}

	SSL_set_fd(tls, sockfd);

	/* try to tls connect here, success returns 1 */
	if (SSL_connect(tls) != 1){return 0;}//could not make a TLS session

	/* B L O A T */
//	cert = SSL_get_peer_certificate(tls);
//	if (!cert){return 0;}//could not get a certificate
	//free what we aren't using anymore
//	X509_free(cert);

	return tls;
	}

void client()
	{
	/* make a tls session with a server */
	SSL *tlsSession = connectServer("127.0.0.1/6697");
	if (!tlsSession){fprintf(stderr,"Could not make a TLS session with host\n"); exit(1);}

	printf("Session made, you can start typing:\n");

	char buffer[MAX];
	while (1)
		{
		int len = 0;

		char c;
		while ((c = getchar()) != '\n' && len < MAX){buffer[len++] = c;}
		buffer[len] = '\n';
		if (!(memcmp(buffer,"exit",4))){break;}

		SSL_write(tlsSession,buffer,len+1);
		int num = SSL_read(tlsSession,buffer,MAX);
		if (num <= 0){fprintf(stderr, "SSL_read() 0 or failed, giving up\n"); exit(1);}
		buffer[num] = '\0';
		printf("%s", buffer);
		if (!(strncmp(buffer,"Goodbye\n",num))){break;}
		}


	int fd = SSL_get_fd(tlsSession);
	SSL_shutdown(tlsSession);
	if (fd){close(fd);}
        SSL_free(tlsSession);
	}