ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/tls_openssl.c
Revision: 9165
Committed: Sat Jan 18 13:01:40 2020 UTC (5 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 11189 byte(s)
Log Message:
- tls_gnutls.c, tls_openssl.c: tls_get_cipher, tls_get_version: reduce buffer sizes

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id