-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtls_server.c
More file actions
104 lines (87 loc) · 2.76 KB
/
Copy pathtls_server.c
File metadata and controls
104 lines (87 loc) · 2.76 KB
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
/* ------------------------------------------------------------ *
* file: tls_server.c *
* purpose: Example code for building a TLS server to *
* accept connections at post 4433 *
* *
* gcc -o tls_server tls_server.c -lssl -lcrypto *
* ------------------------------------------------------------ */
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "tls_server.h"
int create_socket(int port)
{
int s;
struct sockaddr_in addr;
/* set the type of connection to TCP/IP */
addr.sin_family = AF_INET;
/* set the server port number */
addr.sin_port = htons(port);
/* set our address to any interface */
addr.sin_addr.s_addr = htonl(INADDR_ANY);
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
perror("Unable to create socket");
exit(EXIT_FAILURE);
}
/* bind serv information to s socket */
if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("Unable to bind");
exit(EXIT_FAILURE);
}
/* start listening allowing a queue of up to 1 pending connection */
/* We are allowing only one connection since only one client will
send its data in this socket*/
if (listen(s, 1) < 0) {
perror("Unable to listen");
exit(EXIT_FAILURE);
}
return s;
}
void init_openssl()
{
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
}
void cleanup_openssl()
{
EVP_cleanup();
}
SSL_CTX *create_context()
{
const SSL_METHOD *method;
SSL_CTX *ctx;
/* The actual protocol version used will be negotiated to the
* highest version mutually supported by the client and the server.
* The supported protocols are SSLv3, TLSv1, TLSv1.1 and TLSv1.2.
*/
method = SSLv23_server_method();
/* creates a new SSL_CTX object as framework to establish TLS/SSL or
* DTLS enabled connections. It initializes the list of ciphers, the
* session cache setting, the callbacks, the keys and certificates,
* and the options to its default values
*/
ctx = SSL_CTX_new(method);
if (!ctx) {
perror("Unable to create SSL context");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
return ctx;
}
void configure_context(SSL_CTX *ctx)
{
SSL_CTX_set_ecdh_auto(ctx, 1);
/* Set the key and cert using dedicated pem files */
if (SSL_CTX_use_certificate_file(ctx, "cert.pem", SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
if (SSL_CTX_use_PrivateKey_file(ctx, "key.pem", SSL_FILETYPE_PEM) <= 0 ) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
}