-
Notifications
You must be signed in to change notification settings - Fork 15
/
ssl_server.c
executable file
·236 lines (220 loc) · 8.45 KB
/
ssl_server.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
*FileName : ssl_server.c
*Author : JiangInk
*Version : V0.1
*Date : 2015.03.10
*/
/*****************************************************************************/
/*** ssl_server.c ***/
/*** ***/
/*** Demonstrate an SSL server. ***/
/*****************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "ssl_common.h"
/*---------------------------------------------------------------------*/
/*--- open_listener - create server socket ---*/
/*---------------------------------------------------------------------*/
int open_listener(int port)
{
int sd;
struct sockaddr_in addr;
sd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
{
perror("can't bind port");
abort();
}
if ( listen(sd, 10) != 0 )
{
perror("Can't configure listening port");
abort();
}
return sd;
}
/*---------------------------------------------------------------------*/
/*--- init_server_ctx - initialize SSL server and create contexts ---*/
/*---------------------------------------------------------------------*/
SSL_CTX* init_server_ctx(void)
{
SSL_METHOD *method;
SSL_CTX *ctx;
SSL_library_init(); /* init algorithms library */
OpenSSL_add_all_algorithms(); /* load & register all cryptos, etc. */
SSL_load_error_strings(); /* load all error messages */
//method = SSLv23_server_method(); /* create new server-method instance */
method = TLSv1_server_method();
ctx = SSL_CTX_new(method); /* create new context from method */
if ( ctx == NULL )
{
ERR_print_errors_fp(stderr);
abort();
}
return ctx;
}
/*---------------------------------------------------------------------*/
/*--- verify_callback - SSL_CTX_set_verify callback function. ---*/
/*---------------------------------------------------------------------*/
int verify_callback(int ok, X509_STORE_CTX *store)
{
char data[256];
if (ok)
{
fprintf(stderr, "verify_callback\n{\n");
X509 *cert = X509_STORE_CTX_get_current_cert(store);
int depth = X509_STORE_CTX_get_error_depth(store);
int err = X509_STORE_CTX_get_error(store);
fprintf(stderr, "certificate at depth: %i\n", depth);
memset(data, 0, sizeof(data));
X509_NAME_oneline(X509_get_issuer_name(cert), data, 256);
fprintf(stderr, "issuer = %s\n", data);
X509_NAME_oneline(X509_get_subject_name(cert), data, 256);
fprintf(stderr, "subject = %s\n", data);
fprintf(stderr, "error status: %i:%s\n}\n", err, X509_verify_cert_error_string(err));
}
return ok;
}
/*---------------------------------------------------------------------*/
/*--- load_certificates - load from files. ---*/
/*---------------------------------------------------------------------*/
void load_certificates(SSL_CTX* ctx, char* CaFile, char* CertFile, char* KeyFile)
{
#if 1
/* set maximum depth for the certificate chain */
//SSL_CTX_set_verify_depth(ctx, 1);
/* set voluntary certification mode*/
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
/* set mandatory certification mode*/
//SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verify_callback);
/* load CA certificate file */
if (SSL_CTX_load_verify_locations(ctx, CaFile, NULL) <=0)
{
ERR_print_errors_fp(stderr);
abort();
}
#endif
/* set the local certificate from CertFile */
if ( SSL_CTX_use_certificate_file(ctx, CertFile, SSL_FILETYPE_PEM) <= 0 )
{
ERR_print_errors_fp(stderr);
abort();
}
/* set server private key password */
SSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)PRIKEY_PASSWD);
/* set the private key from KeyFile (may be the same as CertFile) */
if ( SSL_CTX_use_PrivateKey_file(ctx, KeyFile, SSL_FILETYPE_PEM) <= 0 )
{
ERR_print_errors_fp(stderr);
abort();
}
/* verify private key */
if ( !SSL_CTX_check_private_key(ctx) )
{
fprintf(stderr, "Private key does not match the public certificate\n");
abort();
}
/* set SSL cipher type */
SSL_CTX_set_cipher_list(ctx, ALGO_TYPE);
}
/*---------------------------------------------------------------------*/
/*--- show_certs_info - print out certificates. ---*/
/*---------------------------------------------------------------------*/
void show_certs_info(SSL* ssl)
{
X509 *cert;
char *line;
/* Get connect use algorithm type */
//printf("SSL connection using %s\n", SSL_get_cipher(ssl));
/* Get certificates (if available) */
cert = SSL_get_peer_certificate(ssl);
if ( cert != NULL )
{
printf("Server certificates:\n");
line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
printf("Subject: %s\n", line);
free(line);
line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
printf("Issuer: %s\n", line);
free(line);
X509_free(cert);
}
else
printf("No certificates.\n");
}
/*---------------------------------------------------------------------*/
/*--- server_handler - SSL servlet ---*/
/*---------------------------------------------------------------------*/
void server_handler(SSL* ssl) /* Serve the connection -- threadable */
{
char buf[1024];
char reply[1280];
int sd, bytes;
if (FAIL == SSL_accept(ssl)) /* do SSL-protocol accept */
{
ERR_print_errors_fp(stderr);
}
else
{
show_certs_info(ssl); /* get any certificates */
bytes = SSL_read(ssl, buf, sizeof(buf)-1); /* get request */
if (FAIL == bytes)
{
ERR_print_errors_fp(stderr);
}
buf[bytes] = '\0';
printf("Client msg: \"%s\"\n", buf);
snprintf(reply, sizeof(reply), SERVERHTML, buf);
bytes = SSL_write(ssl, reply, strlen(reply)); /* send reply */
if (FAIL == bytes)
{
ERR_print_errors_fp(stderr);
}
}
sd = SSL_get_fd(ssl); /* get socket connection */
//SSL_shutdown(ssl); /* shutdown SSL link */
SSL_free(ssl); /* release SSL state */
close(sd); /* close connection */
}
/*---------------------------------------------------------------------*/
/*--- main - create SSL socket server. ---*/
/*---------------------------------------------------------------------*/
int main(int count, char *strings[])
{
SSL_CTX *ctx;
int server;
char *portnum;
if ( count != 2 )
{
printf("Usage: %s <portnum>\n", strings[0]);
exit(0);
}
portnum = strings[1];
ctx = init_server_ctx(); /* initialize SSL */
load_certificates(ctx, ROOTCERTF, SERVER_CERT, SERVER_KEYF); /* load certs */
server = open_listener(atoi(portnum)); /* create server socket */
while (1)
{ struct sockaddr_in addr;
int len = sizeof(addr);
SSL *ssl;
/* accept connection as usual */
int client = accept(server, (struct sockaddr*)&addr, &len);
printf("Connection: %s:%d\n",
inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
ssl = SSL_new(ctx); /* get new SSL state with context */
SSL_set_fd(ssl, client); /* set connection socket to SSL state */
server_handler(ssl); /* service connection */
}
close(server); /* close server socket */
SSL_CTX_free(ctx); /* release context */
return 0;
}