1 |
michael |
7105 |
/* |
2 |
|
|
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
|
* |
4 |
michael |
7148 |
* Copyright (c) 2015 Attila Molnar <attilamolnar@hush.com> |
5 |
|
|
* Copyright (c) 2015 Adam <Adam@anope.org> |
6 |
michael |
7164 |
* Copyright (c) 2005-2016 ircd-hybrid development team |
7 |
michael |
7105 |
* |
8 |
|
|
* This program is free software; you can redistribute it and/or modify |
9 |
|
|
* it under the terms of the GNU General Public License as published by |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
11 |
|
|
* (at your option) any later version. |
12 |
|
|
* |
13 |
|
|
* This program is distributed in the hope that it will be useful, |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
|
|
* GNU General Public License for more details. |
17 |
|
|
* |
18 |
|
|
* You should have received a copy of the GNU General Public License |
19 |
|
|
* along with this program; if not, write to the Free Software |
20 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
21 |
|
|
* USA |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
/*! \file tls_openssl.c |
25 |
|
|
* \brief Includes all OpenSSL-specific TLS functions |
26 |
|
|
* \version $Id$ |
27 |
|
|
*/ |
28 |
|
|
|
29 |
|
|
#include "stdinc.h" |
30 |
|
|
#include "tls.h" |
31 |
|
|
#include "conf.h" |
32 |
|
|
#include "log.h" |
33 |
michael |
7152 |
#include "misc.h" |
34 |
michael |
7105 |
#include "memory.h" |
35 |
|
|
|
36 |
|
|
#ifdef HAVE_TLS_OPENSSL |
37 |
|
|
|
38 |
michael |
7271 |
static int TLS_initialized; |
39 |
|
|
|
40 |
michael |
7152 |
/* |
41 |
|
|
* report_crypto_errors - Dump crypto error list to log |
42 |
|
|
*/ |
43 |
|
|
static void |
44 |
|
|
report_crypto_errors(void) |
45 |
|
|
{ |
46 |
|
|
unsigned long e = 0; |
47 |
|
|
|
48 |
|
|
while ((e = ERR_get_error())) |
49 |
|
|
ilog(LOG_TYPE_IRCD, "SSL error: %s", ERR_error_string(e, 0)); |
50 |
|
|
} |
51 |
|
|
|
52 |
michael |
7105 |
static int |
53 |
|
|
always_accept_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx) |
54 |
|
|
{ |
55 |
|
|
return 1; |
56 |
|
|
} |
57 |
|
|
|
58 |
michael |
7271 |
int |
59 |
|
|
tls_is_initialized(void) |
60 |
|
|
{ |
61 |
|
|
return TLS_initialized; |
62 |
|
|
} |
63 |
|
|
|
64 |
michael |
7105 |
/* tls_init() |
65 |
|
|
* |
66 |
|
|
* inputs - nothing |
67 |
|
|
* output - nothing |
68 |
|
|
* side effects - setups SSL context. |
69 |
|
|
*/ |
70 |
|
|
void |
71 |
|
|
tls_init(void) |
72 |
|
|
{ |
73 |
|
|
SSL_load_error_strings(); |
74 |
|
|
SSLeay_add_ssl_algorithms(); |
75 |
|
|
|
76 |
|
|
if (!(ConfigServerInfo.tls_ctx.server_ctx = SSL_CTX_new(SSLv23_server_method()))) |
77 |
|
|
{ |
78 |
|
|
const char *s = ERR_lib_error_string(ERR_get_error()); |
79 |
|
|
|
80 |
michael |
7278 |
ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the TLS Server context -- %s", s); |
81 |
michael |
7105 |
exit(EXIT_FAILURE); |
82 |
|
|
return; /* Not reached */ |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
SSL_CTX_set_options(ConfigServerInfo.tls_ctx.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET); |
86 |
|
|
SSL_CTX_set_options(ConfigServerInfo.tls_ctx.server_ctx, SSL_OP_SINGLE_DH_USE|SSL_OP_CIPHER_SERVER_PREFERENCE); |
87 |
|
|
SSL_CTX_set_verify(ConfigServerInfo.tls_ctx.server_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE, |
88 |
|
|
always_accept_verify_cb); |
89 |
|
|
SSL_CTX_set_session_cache_mode(ConfigServerInfo.tls_ctx.server_ctx, SSL_SESS_CACHE_OFF); |
90 |
|
|
SSL_CTX_set_cipher_list(ConfigServerInfo.tls_ctx.server_ctx, "EECDH+HIGH:EDH+HIGH:HIGH:!aNULL"); |
91 |
|
|
|
92 |
michael |
7192 |
#ifndef OPENSSL_NO_ECDH |
93 |
michael |
7233 |
EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
94 |
|
|
|
95 |
|
|
if (key) |
96 |
michael |
7105 |
{ |
97 |
michael |
7233 |
SSL_CTX_set_tmp_ecdh(ConfigServerInfo.tls_ctx.server_ctx, key); |
98 |
|
|
EC_KEY_free(key); |
99 |
michael |
7105 |
} |
100 |
|
|
|
101 |
|
|
SSL_CTX_set_options(ConfigServerInfo.tls_ctx.server_ctx, SSL_OP_SINGLE_ECDH_USE); |
102 |
|
|
#endif |
103 |
|
|
|
104 |
|
|
if (!(ConfigServerInfo.tls_ctx.client_ctx = SSL_CTX_new(SSLv23_client_method()))) |
105 |
|
|
{ |
106 |
|
|
const char *s = ERR_lib_error_string(ERR_get_error()); |
107 |
|
|
|
108 |
michael |
7278 |
ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the TLS Client context -- %s", s); |
109 |
michael |
7105 |
exit(EXIT_FAILURE); |
110 |
|
|
return; /* Not reached */ |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
SSL_CTX_set_options(ConfigServerInfo.tls_ctx.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET); |
114 |
|
|
SSL_CTX_set_options(ConfigServerInfo.tls_ctx.client_ctx, SSL_OP_SINGLE_DH_USE); |
115 |
|
|
SSL_CTX_set_verify(ConfigServerInfo.tls_ctx.client_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE, |
116 |
|
|
always_accept_verify_cb); |
117 |
|
|
SSL_CTX_set_session_cache_mode(ConfigServerInfo.tls_ctx.client_ctx, SSL_SESS_CACHE_OFF); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
int |
121 |
|
|
tls_new_cred(void) |
122 |
|
|
{ |
123 |
michael |
7271 |
TLS_initialized = 0; |
124 |
|
|
|
125 |
michael |
7105 |
if (!ConfigServerInfo.ssl_certificate_file || !ConfigServerInfo.rsa_private_key_file) |
126 |
|
|
return 1; |
127 |
|
|
|
128 |
|
|
if (SSL_CTX_use_certificate_chain_file(ConfigServerInfo.tls_ctx.server_ctx, ConfigServerInfo.ssl_certificate_file) <= 0 || |
129 |
|
|
SSL_CTX_use_certificate_chain_file(ConfigServerInfo.tls_ctx.client_ctx, ConfigServerInfo.ssl_certificate_file) <= 0) |
130 |
|
|
{ |
131 |
|
|
report_crypto_errors(); |
132 |
|
|
return 0; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
if (SSL_CTX_use_PrivateKey_file(ConfigServerInfo.tls_ctx.server_ctx, ConfigServerInfo.rsa_private_key_file, SSL_FILETYPE_PEM) <= 0 || |
136 |
|
|
SSL_CTX_use_PrivateKey_file(ConfigServerInfo.tls_ctx.client_ctx, ConfigServerInfo.rsa_private_key_file, SSL_FILETYPE_PEM) <= 0) |
137 |
|
|
{ |
138 |
|
|
report_crypto_errors(); |
139 |
|
|
return 0; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
if (!SSL_CTX_check_private_key(ConfigServerInfo.tls_ctx.server_ctx) || |
143 |
|
|
!SSL_CTX_check_private_key(ConfigServerInfo.tls_ctx.client_ctx)) |
144 |
|
|
{ |
145 |
|
|
report_crypto_errors(); |
146 |
|
|
return 0; |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
if (ConfigServerInfo.ssl_dh_param_file) |
150 |
|
|
{ |
151 |
|
|
BIO *file = BIO_new_file(ConfigServerInfo.ssl_dh_param_file, "r"); |
152 |
|
|
|
153 |
|
|
if (file) |
154 |
|
|
{ |
155 |
|
|
DH *dh = PEM_read_bio_DHparams(file, NULL, NULL, NULL); |
156 |
|
|
|
157 |
|
|
BIO_free(file); |
158 |
|
|
|
159 |
|
|
if (dh) |
160 |
|
|
{ |
161 |
|
|
if (DH_size(dh) >= 256) |
162 |
|
|
SSL_CTX_set_tmp_dh(ConfigServerInfo.tls_ctx.server_ctx, dh); |
163 |
|
|
else |
164 |
|
|
ilog(LOG_TYPE_IRCD, "Ignoring serverinfo::ssl_dh_param_file -- need at least a 2048 bit DH prime size"); |
165 |
|
|
|
166 |
|
|
DH_free(dh); |
167 |
|
|
} |
168 |
|
|
} |
169 |
|
|
else |
170 |
|
|
ilog(LOG_TYPE_IRCD, "Ignoring serverinfo::ssl_dh_param_file -- could not open/read Diffie-Hellman parameter file"); |
171 |
|
|
} |
172 |
|
|
|
173 |
michael |
7192 |
#ifndef OPENSSL_NO_ECDH |
174 |
michael |
7124 |
if (ConfigServerInfo.ssl_dh_elliptic_curve) |
175 |
michael |
7105 |
{ |
176 |
|
|
int nid = 0; |
177 |
|
|
EC_KEY *key = NULL; |
178 |
|
|
|
179 |
|
|
if ((nid = OBJ_sn2nid(ConfigServerInfo.ssl_dh_elliptic_curve)) == 0) |
180 |
|
|
goto set_default_curve; |
181 |
|
|
|
182 |
|
|
if ((key = EC_KEY_new_by_curve_name(nid)) == NULL) |
183 |
|
|
goto set_default_curve; |
184 |
|
|
|
185 |
|
|
SSL_CTX_set_tmp_ecdh(ConfigServerInfo.tls_ctx.server_ctx, key); |
186 |
|
|
EC_KEY_free(key); |
187 |
|
|
} |
188 |
|
|
else |
189 |
|
|
{ |
190 |
|
|
EC_KEY *key; |
191 |
|
|
|
192 |
|
|
set_default_curve: |
193 |
|
|
|
194 |
|
|
key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
195 |
|
|
|
196 |
|
|
if (key) |
197 |
|
|
{ |
198 |
|
|
SSL_CTX_set_tmp_ecdh(ConfigServerInfo.tls_ctx.server_ctx, key); |
199 |
|
|
EC_KEY_free(key); |
200 |
|
|
} |
201 |
|
|
} |
202 |
|
|
#endif |
203 |
|
|
|
204 |
|
|
if (ConfigServerInfo.ssl_message_digest_algorithm == NULL) |
205 |
|
|
ConfigServerInfo.message_digest_algorithm = EVP_sha256(); |
206 |
|
|
else |
207 |
|
|
{ |
208 |
|
|
ConfigServerInfo.message_digest_algorithm = EVP_get_digestbyname(ConfigServerInfo.ssl_message_digest_algorithm); |
209 |
michael |
7124 |
|
210 |
michael |
7105 |
if (ConfigServerInfo.message_digest_algorithm == NULL) |
211 |
|
|
{ |
212 |
|
|
ConfigServerInfo.message_digest_algorithm = EVP_sha256(); |
213 |
|
|
ilog(LOG_TYPE_IRCD, "Ignoring serverinfo::ssl_message_digest_algorithm -- unknown message digest algorithm"); |
214 |
|
|
} |
215 |
|
|
} |
216 |
|
|
|
217 |
michael |
7190 |
if (ConfigServerInfo.ssl_cipher_list) |
218 |
|
|
SSL_CTX_set_cipher_list(ConfigServerInfo.tls_ctx.server_ctx, ConfigServerInfo.ssl_cipher_list); |
219 |
|
|
|
220 |
michael |
7271 |
TLS_initialized = 1; |
221 |
michael |
7105 |
return 1; |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
const char * |
225 |
|
|
tls_get_cipher(const tls_data_t *tls_data) |
226 |
|
|
{ |
227 |
|
|
static char buffer[IRCD_BUFSIZE]; |
228 |
|
|
int bits = 0; |
229 |
|
|
SSL *ssl = *tls_data; |
230 |
|
|
|
231 |
|
|
SSL_CIPHER_get_bits(SSL_get_current_cipher(ssl), &bits); |
232 |
|
|
|
233 |
|
|
snprintf(buffer, sizeof(buffer), "%s-%s-%d", SSL_get_version(ssl), |
234 |
|
|
SSL_get_cipher(ssl), bits); |
235 |
|
|
return buffer; |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
int |
239 |
|
|
tls_isusing(tls_data_t *tls_data) |
240 |
|
|
{ |
241 |
|
|
SSL *ssl = *tls_data; |
242 |
michael |
7124 |
return ssl != NULL; |
243 |
michael |
7105 |
} |
244 |
|
|
|
245 |
|
|
void |
246 |
|
|
tls_free(tls_data_t *tls_data) |
247 |
|
|
{ |
248 |
|
|
SSL_free(*tls_data); |
249 |
|
|
*tls_data = NULL; |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
int |
253 |
|
|
tls_read(tls_data_t *tls_data, char *buf, size_t bufsize, int *want_write) |
254 |
|
|
{ |
255 |
|
|
SSL *ssl = *tls_data; |
256 |
|
|
int length = SSL_read(ssl, buf, bufsize); |
257 |
|
|
|
258 |
|
|
/* Translate openssl error codes, sigh */ |
259 |
|
|
if (length < 0) |
260 |
|
|
{ |
261 |
|
|
switch (SSL_get_error(ssl, length)) |
262 |
|
|
{ |
263 |
|
|
case SSL_ERROR_WANT_WRITE: |
264 |
|
|
{ |
265 |
|
|
/* OpenSSL wants to write, we signal this to the caller and do nothing about that here */ |
266 |
|
|
*want_write = 1; |
267 |
|
|
break; |
268 |
|
|
} |
269 |
|
|
case SSL_ERROR_WANT_READ: |
270 |
|
|
errno = EWOULDBLOCK; |
271 |
|
|
case SSL_ERROR_SYSCALL: |
272 |
|
|
break; |
273 |
|
|
case SSL_ERROR_SSL: |
274 |
|
|
if (errno == EAGAIN) |
275 |
|
|
break; |
276 |
|
|
/* Fall through */ |
277 |
|
|
default: |
278 |
|
|
length = errno = 0; |
279 |
|
|
} |
280 |
|
|
} |
281 |
|
|
|
282 |
|
|
return length; |
283 |
|
|
} |
284 |
|
|
|
285 |
|
|
int |
286 |
|
|
tls_write(tls_data_t *tls_data, const char *buf, size_t bufsize, int *want_read) |
287 |
|
|
{ |
288 |
|
|
SSL *ssl = *tls_data; |
289 |
|
|
int retlen = SSL_write(ssl, buf, bufsize); |
290 |
|
|
|
291 |
|
|
/* Translate openssl error codes, sigh */ |
292 |
|
|
if (retlen < 0) |
293 |
|
|
{ |
294 |
|
|
switch (SSL_get_error(ssl, retlen)) |
295 |
|
|
{ |
296 |
|
|
case SSL_ERROR_WANT_READ: |
297 |
|
|
*want_read = 1; |
298 |
|
|
break; /* Retry later, don't register for write events */ |
299 |
|
|
case SSL_ERROR_WANT_WRITE: |
300 |
|
|
errno = EWOULDBLOCK; |
301 |
|
|
break; |
302 |
|
|
case SSL_ERROR_SYSCALL: |
303 |
|
|
break; |
304 |
|
|
case SSL_ERROR_SSL: |
305 |
|
|
if (errno == EAGAIN) |
306 |
|
|
break; |
307 |
|
|
/* Fall through */ |
308 |
|
|
default: |
309 |
|
|
retlen = errno = 0; /* Either an SSL-specific error or EOF */ |
310 |
|
|
} |
311 |
|
|
} |
312 |
|
|
|
313 |
|
|
return retlen; |
314 |
|
|
} |
315 |
|
|
|
316 |
|
|
void |
317 |
|
|
tls_shutdown(tls_data_t *tls_data) |
318 |
|
|
{ |
319 |
|
|
SSL *ssl = *tls_data; |
320 |
|
|
|
321 |
|
|
SSL_set_shutdown(ssl, SSL_RECEIVED_SHUTDOWN); |
322 |
|
|
|
323 |
|
|
if (!SSL_shutdown(ssl)) |
324 |
|
|
SSL_shutdown(ssl); |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
int |
328 |
|
|
tls_new(tls_data_t *tls_data, int fd, tls_role_t role) |
329 |
|
|
{ |
330 |
|
|
SSL *ssl; |
331 |
|
|
|
332 |
michael |
7274 |
if (!TLS_initialized) |
333 |
|
|
return 0; |
334 |
|
|
|
335 |
michael |
7105 |
if (role == TLS_ROLE_SERVER) |
336 |
|
|
ssl = SSL_new(ConfigServerInfo.tls_ctx.server_ctx); |
337 |
|
|
else |
338 |
|
|
ssl = SSL_new(ConfigServerInfo.tls_ctx.client_ctx); |
339 |
|
|
|
340 |
|
|
if (!ssl) |
341 |
|
|
{ |
342 |
|
|
ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s", |
343 |
|
|
ERR_error_string(ERR_get_error(), NULL)); |
344 |
|
|
return 0; |
345 |
|
|
} |
346 |
|
|
|
347 |
|
|
*tls_data = ssl; |
348 |
|
|
SSL_set_fd(ssl, fd); |
349 |
|
|
return 1; |
350 |
|
|
} |
351 |
|
|
|
352 |
|
|
int |
353 |
|
|
tls_set_ciphers(tls_data_t *tls_data, const char *cipher_list) |
354 |
|
|
{ |
355 |
|
|
SSL_set_cipher_list(*tls_data, cipher_list); |
356 |
|
|
return 1; |
357 |
|
|
} |
358 |
|
|
|
359 |
|
|
tls_handshake_status_t |
360 |
|
|
tls_handshake(tls_data_t *tls_data, tls_role_t role, const char **errstr) |
361 |
|
|
{ |
362 |
|
|
SSL *ssl = *tls_data; |
363 |
|
|
int ret; |
364 |
|
|
|
365 |
|
|
if (role == TLS_ROLE_SERVER) |
366 |
|
|
ret = SSL_accept(ssl); |
367 |
|
|
else |
368 |
|
|
ret = SSL_connect(ssl); |
369 |
|
|
|
370 |
|
|
if (ret > 0) |
371 |
|
|
return TLS_HANDSHAKE_DONE; |
372 |
|
|
|
373 |
|
|
switch (SSL_get_error(ssl, ret)) |
374 |
|
|
{ |
375 |
|
|
case SSL_ERROR_WANT_WRITE: |
376 |
|
|
return TLS_HANDSHAKE_WANT_WRITE; |
377 |
|
|
case SSL_ERROR_WANT_READ: |
378 |
|
|
return TLS_HANDSHAKE_WANT_READ; |
379 |
|
|
default: |
380 |
|
|
{ |
381 |
|
|
const char *error = ERR_error_string(ERR_get_error(), NULL); |
382 |
|
|
|
383 |
|
|
if (errstr) |
384 |
|
|
*errstr = error; |
385 |
|
|
|
386 |
|
|
return TLS_HANDSHAKE_ERROR; |
387 |
|
|
} |
388 |
|
|
} |
389 |
|
|
} |
390 |
|
|
|
391 |
|
|
int |
392 |
michael |
7142 |
tls_verify_cert(tls_data_t *tls_data, tls_md_t digest, char **fingerprint) |
393 |
michael |
7105 |
{ |
394 |
|
|
SSL *ssl = *tls_data; |
395 |
|
|
X509 *cert = SSL_get_peer_certificate(ssl); |
396 |
|
|
unsigned int n; |
397 |
|
|
char buf[EVP_MAX_MD_SIZE * 2 + 1]; |
398 |
|
|
unsigned char md[EVP_MAX_MD_SIZE]; |
399 |
|
|
int ret = 0; |
400 |
|
|
|
401 |
|
|
/* Accept NULL return from SSL_get_peer_certificate */ |
402 |
|
|
if (!cert) |
403 |
|
|
return 1; |
404 |
|
|
|
405 |
|
|
int res = SSL_get_verify_result(ssl); |
406 |
|
|
if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN || |
407 |
|
|
res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE || |
408 |
|
|
res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) |
409 |
|
|
{ |
410 |
|
|
ret = 1; |
411 |
|
|
|
412 |
|
|
if (X509_digest(cert, digest, md, &n)) |
413 |
|
|
{ |
414 |
|
|
binary_to_hex(md, buf, n); |
415 |
|
|
*fingerprint = xstrdup(buf); |
416 |
|
|
} |
417 |
|
|
} |
418 |
|
|
|
419 |
|
|
X509_free(cert); |
420 |
|
|
return ret; |
421 |
|
|
} |
422 |
|
|
#endif /* HAVE_TLS_OPENSSL */ |