ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/tls_openssl.c
Revision: 7233
Committed: Wed Feb 3 16:07:50 2016 UTC (9 years, 6 months ago) by michael
Content type: text/x-csrc
File size: 10758 byte(s)
Log Message:
- tls_openssl.c: removed extraneous curly braces not needed with c99

File Contents

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

Properties

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