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 |
7152 |
/* |
39 |
|
|
* report_crypto_errors - Dump crypto error list to log |
40 |
|
|
*/ |
41 |
|
|
static void |
42 |
|
|
report_crypto_errors(void) |
43 |
|
|
{ |
44 |
|
|
unsigned long e = 0; |
45 |
|
|
|
46 |
|
|
while ((e = ERR_get_error())) |
47 |
|
|
ilog(LOG_TYPE_IRCD, "SSL error: %s", ERR_error_string(e, 0)); |
48 |
|
|
} |
49 |
|
|
|
50 |
michael |
7105 |
static int |
51 |
|
|
always_accept_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx) |
52 |
|
|
{ |
53 |
|
|
return 1; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
/* tls_init() |
57 |
|
|
* |
58 |
|
|
* inputs - nothing |
59 |
|
|
* output - nothing |
60 |
|
|
* side effects - setups SSL context. |
61 |
|
|
*/ |
62 |
|
|
void |
63 |
|
|
tls_init(void) |
64 |
|
|
{ |
65 |
|
|
SSL_load_error_strings(); |
66 |
|
|
SSLeay_add_ssl_algorithms(); |
67 |
|
|
|
68 |
|
|
if (!(ConfigServerInfo.tls_ctx.server_ctx = SSL_CTX_new(SSLv23_server_method()))) |
69 |
|
|
{ |
70 |
|
|
const char *s = ERR_lib_error_string(ERR_get_error()); |
71 |
|
|
|
72 |
|
|
fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n", s); |
73 |
|
|
ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s", s); |
74 |
|
|
exit(EXIT_FAILURE); |
75 |
|
|
return; /* Not reached */ |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
SSL_CTX_set_options(ConfigServerInfo.tls_ctx.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET); |
79 |
|
|
SSL_CTX_set_options(ConfigServerInfo.tls_ctx.server_ctx, SSL_OP_SINGLE_DH_USE|SSL_OP_CIPHER_SERVER_PREFERENCE); |
80 |
|
|
SSL_CTX_set_verify(ConfigServerInfo.tls_ctx.server_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE, |
81 |
|
|
always_accept_verify_cb); |
82 |
|
|
SSL_CTX_set_session_cache_mode(ConfigServerInfo.tls_ctx.server_ctx, SSL_SESS_CACHE_OFF); |
83 |
|
|
SSL_CTX_set_cipher_list(ConfigServerInfo.tls_ctx.server_ctx, "EECDH+HIGH:EDH+HIGH:HIGH:!aNULL"); |
84 |
|
|
|
85 |
|
|
#if OPENSSL_VERSION_NUMBER >= 0x009080FFL && !defined(OPENSSL_NO_ECDH) |
86 |
|
|
{ |
87 |
|
|
EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
88 |
|
|
|
89 |
|
|
if (key) |
90 |
|
|
{ |
91 |
|
|
SSL_CTX_set_tmp_ecdh(ConfigServerInfo.tls_ctx.server_ctx, key); |
92 |
|
|
EC_KEY_free(key); |
93 |
|
|
} |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
SSL_CTX_set_options(ConfigServerInfo.tls_ctx.server_ctx, SSL_OP_SINGLE_ECDH_USE); |
97 |
|
|
#endif |
98 |
|
|
|
99 |
|
|
if (!(ConfigServerInfo.tls_ctx.client_ctx = SSL_CTX_new(SSLv23_client_method()))) |
100 |
|
|
{ |
101 |
|
|
const char *s = ERR_lib_error_string(ERR_get_error()); |
102 |
|
|
|
103 |
|
|
fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n", s); |
104 |
|
|
ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s", s); |
105 |
|
|
exit(EXIT_FAILURE); |
106 |
|
|
return; /* Not reached */ |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
SSL_CTX_set_options(ConfigServerInfo.tls_ctx.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET); |
110 |
|
|
SSL_CTX_set_options(ConfigServerInfo.tls_ctx.client_ctx, SSL_OP_SINGLE_DH_USE); |
111 |
|
|
SSL_CTX_set_verify(ConfigServerInfo.tls_ctx.client_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE, |
112 |
|
|
always_accept_verify_cb); |
113 |
|
|
SSL_CTX_set_session_cache_mode(ConfigServerInfo.tls_ctx.client_ctx, SSL_SESS_CACHE_OFF); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
int |
117 |
|
|
tls_new_cred(void) |
118 |
|
|
{ |
119 |
|
|
if (!ConfigServerInfo.ssl_certificate_file || !ConfigServerInfo.rsa_private_key_file) |
120 |
|
|
return 1; |
121 |
|
|
|
122 |
|
|
if (SSL_CTX_use_certificate_chain_file(ConfigServerInfo.tls_ctx.server_ctx, ConfigServerInfo.ssl_certificate_file) <= 0 || |
123 |
|
|
SSL_CTX_use_certificate_chain_file(ConfigServerInfo.tls_ctx.client_ctx, ConfigServerInfo.ssl_certificate_file) <= 0) |
124 |
|
|
{ |
125 |
|
|
report_crypto_errors(); |
126 |
|
|
return 0; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
if (SSL_CTX_use_PrivateKey_file(ConfigServerInfo.tls_ctx.server_ctx, ConfigServerInfo.rsa_private_key_file, SSL_FILETYPE_PEM) <= 0 || |
130 |
|
|
SSL_CTX_use_PrivateKey_file(ConfigServerInfo.tls_ctx.client_ctx, ConfigServerInfo.rsa_private_key_file, SSL_FILETYPE_PEM) <= 0) |
131 |
|
|
{ |
132 |
|
|
report_crypto_errors(); |
133 |
|
|
return 0; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
if (!SSL_CTX_check_private_key(ConfigServerInfo.tls_ctx.server_ctx) || |
137 |
|
|
!SSL_CTX_check_private_key(ConfigServerInfo.tls_ctx.client_ctx)) |
138 |
|
|
{ |
139 |
|
|
report_crypto_errors(); |
140 |
|
|
return 0; |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
if (ConfigServerInfo.ssl_dh_param_file) |
144 |
|
|
{ |
145 |
|
|
BIO *file = BIO_new_file(ConfigServerInfo.ssl_dh_param_file, "r"); |
146 |
|
|
|
147 |
|
|
if (file) |
148 |
|
|
{ |
149 |
|
|
DH *dh = PEM_read_bio_DHparams(file, NULL, NULL, NULL); |
150 |
|
|
|
151 |
|
|
BIO_free(file); |
152 |
|
|
|
153 |
|
|
if (dh) |
154 |
|
|
{ |
155 |
|
|
if (DH_size(dh) >= 256) |
156 |
|
|
SSL_CTX_set_tmp_dh(ConfigServerInfo.tls_ctx.server_ctx, dh); |
157 |
|
|
else |
158 |
|
|
ilog(LOG_TYPE_IRCD, "Ignoring serverinfo::ssl_dh_param_file -- need at least a 2048 bit DH prime size"); |
159 |
|
|
|
160 |
|
|
DH_free(dh); |
161 |
|
|
} |
162 |
|
|
} |
163 |
|
|
else |
164 |
|
|
ilog(LOG_TYPE_IRCD, "Ignoring serverinfo::ssl_dh_param_file -- could not open/read Diffie-Hellman parameter file"); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
#if OPENSSL_VERSION_NUMBER >= 0x009080FFL && !defined(OPENSSL_NO_ECDH) |
168 |
michael |
7124 |
if (ConfigServerInfo.ssl_dh_elliptic_curve) |
169 |
michael |
7105 |
{ |
170 |
|
|
int nid = 0; |
171 |
|
|
EC_KEY *key = NULL; |
172 |
|
|
|
173 |
|
|
if ((nid = OBJ_sn2nid(ConfigServerInfo.ssl_dh_elliptic_curve)) == 0) |
174 |
|
|
goto set_default_curve; |
175 |
|
|
|
176 |
|
|
if ((key = EC_KEY_new_by_curve_name(nid)) == NULL) |
177 |
|
|
goto set_default_curve; |
178 |
|
|
|
179 |
|
|
SSL_CTX_set_tmp_ecdh(ConfigServerInfo.tls_ctx.server_ctx, key); |
180 |
|
|
EC_KEY_free(key); |
181 |
|
|
} |
182 |
|
|
else |
183 |
|
|
{ |
184 |
|
|
EC_KEY *key; |
185 |
|
|
|
186 |
|
|
set_default_curve: |
187 |
|
|
|
188 |
|
|
key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
189 |
|
|
|
190 |
|
|
if (key) |
191 |
|
|
{ |
192 |
|
|
SSL_CTX_set_tmp_ecdh(ConfigServerInfo.tls_ctx.server_ctx, key); |
193 |
|
|
EC_KEY_free(key); |
194 |
|
|
} |
195 |
|
|
} |
196 |
|
|
#endif |
197 |
|
|
|
198 |
|
|
if (ConfigServerInfo.ssl_message_digest_algorithm == NULL) |
199 |
|
|
ConfigServerInfo.message_digest_algorithm = EVP_sha256(); |
200 |
|
|
else |
201 |
|
|
{ |
202 |
|
|
ConfigServerInfo.message_digest_algorithm = EVP_get_digestbyname(ConfigServerInfo.ssl_message_digest_algorithm); |
203 |
michael |
7124 |
|
204 |
michael |
7105 |
if (ConfigServerInfo.message_digest_algorithm == NULL) |
205 |
|
|
{ |
206 |
|
|
ConfigServerInfo.message_digest_algorithm = EVP_sha256(); |
207 |
|
|
ilog(LOG_TYPE_IRCD, "Ignoring serverinfo::ssl_message_digest_algorithm -- unknown message digest algorithm"); |
208 |
|
|
} |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
return 1; |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
const char * |
215 |
|
|
tls_get_cipher(const tls_data_t *tls_data) |
216 |
|
|
{ |
217 |
|
|
static char buffer[IRCD_BUFSIZE]; |
218 |
|
|
int bits = 0; |
219 |
|
|
SSL *ssl = *tls_data; |
220 |
|
|
|
221 |
|
|
SSL_CIPHER_get_bits(SSL_get_current_cipher(ssl), &bits); |
222 |
|
|
|
223 |
|
|
snprintf(buffer, sizeof(buffer), "%s-%s-%d", SSL_get_version(ssl), |
224 |
|
|
SSL_get_cipher(ssl), bits); |
225 |
|
|
return buffer; |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
int |
229 |
|
|
tls_isusing(tls_data_t *tls_data) |
230 |
|
|
{ |
231 |
|
|
SSL *ssl = *tls_data; |
232 |
michael |
7124 |
return ssl != NULL; |
233 |
michael |
7105 |
} |
234 |
|
|
|
235 |
|
|
void |
236 |
|
|
tls_free(tls_data_t *tls_data) |
237 |
|
|
{ |
238 |
|
|
SSL_free(*tls_data); |
239 |
|
|
*tls_data = NULL; |
240 |
|
|
} |
241 |
|
|
|
242 |
|
|
int |
243 |
|
|
tls_read(tls_data_t *tls_data, char *buf, size_t bufsize, int *want_write) |
244 |
|
|
{ |
245 |
|
|
SSL *ssl = *tls_data; |
246 |
|
|
int length = SSL_read(ssl, buf, bufsize); |
247 |
|
|
|
248 |
|
|
/* Translate openssl error codes, sigh */ |
249 |
|
|
if (length < 0) |
250 |
|
|
{ |
251 |
|
|
switch (SSL_get_error(ssl, length)) |
252 |
|
|
{ |
253 |
|
|
case SSL_ERROR_WANT_WRITE: |
254 |
|
|
{ |
255 |
|
|
/* OpenSSL wants to write, we signal this to the caller and do nothing about that here */ |
256 |
|
|
*want_write = 1; |
257 |
|
|
break; |
258 |
|
|
} |
259 |
|
|
case SSL_ERROR_WANT_READ: |
260 |
|
|
errno = EWOULDBLOCK; |
261 |
|
|
case SSL_ERROR_SYSCALL: |
262 |
|
|
break; |
263 |
|
|
case SSL_ERROR_SSL: |
264 |
|
|
if (errno == EAGAIN) |
265 |
|
|
break; |
266 |
|
|
/* Fall through */ |
267 |
|
|
default: |
268 |
|
|
length = errno = 0; |
269 |
|
|
} |
270 |
|
|
} |
271 |
|
|
|
272 |
|
|
return length; |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
int |
276 |
|
|
tls_write(tls_data_t *tls_data, const char *buf, size_t bufsize, int *want_read) |
277 |
|
|
{ |
278 |
|
|
SSL *ssl = *tls_data; |
279 |
|
|
int retlen = SSL_write(ssl, buf, bufsize); |
280 |
|
|
|
281 |
|
|
/* Translate openssl error codes, sigh */ |
282 |
|
|
if (retlen < 0) |
283 |
|
|
{ |
284 |
|
|
switch (SSL_get_error(ssl, retlen)) |
285 |
|
|
{ |
286 |
|
|
case SSL_ERROR_WANT_READ: |
287 |
|
|
*want_read = 1; |
288 |
|
|
break; /* Retry later, don't register for write events */ |
289 |
|
|
case SSL_ERROR_WANT_WRITE: |
290 |
|
|
errno = EWOULDBLOCK; |
291 |
|
|
break; |
292 |
|
|
case SSL_ERROR_SYSCALL: |
293 |
|
|
break; |
294 |
|
|
case SSL_ERROR_SSL: |
295 |
|
|
if (errno == EAGAIN) |
296 |
|
|
break; |
297 |
|
|
/* Fall through */ |
298 |
|
|
default: |
299 |
|
|
retlen = errno = 0; /* Either an SSL-specific error or EOF */ |
300 |
|
|
} |
301 |
|
|
} |
302 |
|
|
|
303 |
|
|
return retlen; |
304 |
|
|
} |
305 |
|
|
|
306 |
|
|
void |
307 |
|
|
tls_shutdown(tls_data_t *tls_data) |
308 |
|
|
{ |
309 |
|
|
SSL *ssl = *tls_data; |
310 |
|
|
|
311 |
|
|
SSL_set_shutdown(ssl, SSL_RECEIVED_SHUTDOWN); |
312 |
|
|
|
313 |
|
|
if (!SSL_shutdown(ssl)) |
314 |
|
|
SSL_shutdown(ssl); |
315 |
|
|
} |
316 |
|
|
|
317 |
|
|
int |
318 |
|
|
tls_new(tls_data_t *tls_data, int fd, tls_role_t role) |
319 |
|
|
{ |
320 |
|
|
SSL *ssl; |
321 |
|
|
|
322 |
|
|
if (role == TLS_ROLE_SERVER) |
323 |
|
|
ssl = SSL_new(ConfigServerInfo.tls_ctx.server_ctx); |
324 |
|
|
else |
325 |
|
|
ssl = SSL_new(ConfigServerInfo.tls_ctx.client_ctx); |
326 |
|
|
|
327 |
|
|
if (!ssl) |
328 |
|
|
{ |
329 |
|
|
ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s", |
330 |
|
|
ERR_error_string(ERR_get_error(), NULL)); |
331 |
|
|
return 0; |
332 |
|
|
} |
333 |
|
|
|
334 |
|
|
*tls_data = ssl; |
335 |
|
|
SSL_set_fd(ssl, fd); |
336 |
|
|
return 1; |
337 |
|
|
} |
338 |
|
|
|
339 |
|
|
int |
340 |
|
|
tls_set_ciphers(tls_data_t *tls_data, const char *cipher_list) |
341 |
|
|
{ |
342 |
|
|
SSL_set_cipher_list(*tls_data, cipher_list); |
343 |
|
|
return 1; |
344 |
|
|
} |
345 |
|
|
|
346 |
|
|
tls_handshake_status_t |
347 |
|
|
tls_handshake(tls_data_t *tls_data, tls_role_t role, const char **errstr) |
348 |
|
|
{ |
349 |
|
|
SSL *ssl = *tls_data; |
350 |
|
|
int ret; |
351 |
|
|
|
352 |
|
|
if (role == TLS_ROLE_SERVER) |
353 |
|
|
ret = SSL_accept(ssl); |
354 |
|
|
else |
355 |
|
|
ret = SSL_connect(ssl); |
356 |
|
|
|
357 |
|
|
if (ret > 0) |
358 |
|
|
return TLS_HANDSHAKE_DONE; |
359 |
|
|
|
360 |
|
|
switch (SSL_get_error(ssl, ret)) |
361 |
|
|
{ |
362 |
|
|
case SSL_ERROR_WANT_WRITE: |
363 |
|
|
return TLS_HANDSHAKE_WANT_WRITE; |
364 |
|
|
case SSL_ERROR_WANT_READ: |
365 |
|
|
return TLS_HANDSHAKE_WANT_READ; |
366 |
|
|
default: |
367 |
|
|
{ |
368 |
|
|
const char *error = ERR_error_string(ERR_get_error(), NULL); |
369 |
|
|
|
370 |
|
|
if (errstr) |
371 |
|
|
*errstr = error; |
372 |
|
|
|
373 |
|
|
return TLS_HANDSHAKE_ERROR; |
374 |
|
|
} |
375 |
|
|
} |
376 |
|
|
} |
377 |
|
|
|
378 |
|
|
int |
379 |
michael |
7142 |
tls_verify_cert(tls_data_t *tls_data, tls_md_t digest, char **fingerprint) |
380 |
michael |
7105 |
{ |
381 |
|
|
SSL *ssl = *tls_data; |
382 |
|
|
X509 *cert = SSL_get_peer_certificate(ssl); |
383 |
|
|
unsigned int n; |
384 |
|
|
char buf[EVP_MAX_MD_SIZE * 2 + 1]; |
385 |
|
|
unsigned char md[EVP_MAX_MD_SIZE]; |
386 |
|
|
int ret = 0; |
387 |
|
|
|
388 |
|
|
/* Accept NULL return from SSL_get_peer_certificate */ |
389 |
|
|
if (!cert) |
390 |
|
|
return 1; |
391 |
|
|
|
392 |
|
|
int res = SSL_get_verify_result(ssl); |
393 |
|
|
if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN || |
394 |
|
|
res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE || |
395 |
|
|
res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) |
396 |
|
|
{ |
397 |
|
|
ret = 1; |
398 |
|
|
|
399 |
|
|
if (X509_digest(cert, digest, md, &n)) |
400 |
|
|
{ |
401 |
|
|
binary_to_hex(md, buf, n); |
402 |
|
|
*fingerprint = xstrdup(buf); |
403 |
|
|
} |
404 |
|
|
} |
405 |
|
|
|
406 |
|
|
X509_free(cert); |
407 |
|
|
return ret; |
408 |
|
|
} |
409 |
|
|
#endif /* HAVE_TLS_OPENSSL */ |