26 #include <netinet/in.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
31 #include <arpa/inet.h>
34 #include <openssl/rand.h>
35 #include <openssl/ssl.h>
36 #include <openssl/err.h>
42 #define BUFFERSIZE 16384
48 #define RESPONSE "HTTP/1.1 200 OK" CRLF \
49 "Content-Type: text/html charset=utf-8" CRLF \
50 "Server: ServerTest" CRLF \
52 "<html><head><title>Server Test</title></head><body>This is just a test</body></html>" CRLF
55 #define CERTFILE "sslserverchain.pem"
57 #define KEYFILE "sslserver.key"
151 void panic(
char* msg);
163 int main(
int argv,
char** argc){
171 panic (
"Couldn't initialize SSL");
174 panic (
"Couln't load certificates");
177 panic (
"Couldn't make a TCP Connection");
184 panic(
"Failed on selec()");
188 panic (
"Tragic error accepting client");
203 struct sockaddr_in my_addr;
205 h->
skt = socket(AF_INET, SOCK_STREAM, 0);
209 my_addr.sin_family = AF_INET ;
210 my_addr.sin_port = htons(port);
211 my_addr.sin_addr.s_addr = INADDR_ANY ;
213 if( bind( h->
skt, (
struct sockaddr*)&my_addr,
sizeof(my_addr)) == -1 )
216 if(listen( h->
skt, 10) == -1 )
226 FD_SET(h->
skt, &fds);
231 tv.tv_sec = (int)(timeout);
232 tv.tv_usec = (int)((timeout - (
int)(timeout)) * 1000000.0);
234 int ret = select(h->
skt+1, rset, wset, NULL, &tv);
240 struct sockaddr_in client_addr;
241 socklen_t size_addr =
sizeof(
struct sockaddr_in);
243 if ((h->
client_skt = accept( h->
skt, (
struct sockaddr*)&client_addr, &size_addr))!= -1)
245 printf(
"\nNew client connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), client_addr.sin_port);
249 printf (
"There was a problem with this client connection\n");
250 ERR_print_errors_fp(stderr);
288 if (SSL_accept(h->
ssl) < 1)
307 SSL_load_error_strings();
309 OpenSSL_add_all_algorithms();
313 h->
ctx = SSL_CTX_new(TLSv1_2_server_method());
322 if ( SSL_CTX_use_certificate_chain_file(h->
ctx, cert) < 1 )
326 if ( SSL_CTX_use_PrivateKey_file(h->
ctx, key, SSL_FILETYPE_PEM) <= 0 )
330 if ( !SSL_CTX_check_private_key(h->
ctx) )
332 printf(
"Private key doesn't match the public certificate\n");
342 printf(
"[SERVER] Waiting for connections ");
347 case 0: printf(
"|");
break;
348 case 1: printf(
"/");
break;
349 case 2: printf(
"-");
break;
350 case 3: printf(
"\\");
break;
361 fprintf (stderr,
"Error: %s (errno %d, %s)\n", msg, errno, strerror(errno));
363 ERR_print_errors_fp(stderr);