ssl: gnutls: bump dh bitsize to 2048
[ngircd-alex.git] / src / ngircd / conn-ssl.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c) 2005-2008 Florian Westphal <fw@strlen.de>
4  */
5
6 #include "portab.h"
7
8 /**
9  * @file
10  * SSL wrapper functions
11  */
12
13 #include "imp.h"
14 #include "conf-ssl.h"
15
16 #ifdef SSL_SUPPORT
17
18 #include "io.h"
19 #include <assert.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <errno.h>
24
25 #define CONN_MODULE
26 #include "conn.h"
27 #include "conf.h"
28 #include "conn-func.h"
29 #include "conn-ssl.h"
30 #include "log.h"
31
32 #include "exp.h"
33 #include "defines.h"
34
35 extern struct SSLOptions Conf_SSLOptions;
36
37 #ifdef HAVE_LIBSSL
38 #include <openssl/err.h>
39 #include <openssl/rand.h>
40
41 static SSL_CTX * ssl_ctx;
42 static DH *dh_params;
43
44 static bool ConnSSL_LoadServerKey_openssl PARAMS(( SSL_CTX *c ));
45 #endif
46
47 #ifdef HAVE_LIBGNUTLS
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <fcntl.h>
51 #include <unistd.h>
52 #include <gnutls/x509.h>
53
54 #define DH_BITS 2048
55 static gnutls_certificate_credentials_t x509_cred;
56 static gnutls_dh_params_t dh_params;
57
58 static bool ConnSSL_LoadServerKey_gnutls PARAMS(( void ));
59 #endif
60
61 static bool ConnSSL_Init_SSL PARAMS(( CONNECTION *c ));
62 static int ConnectAccept PARAMS(( CONNECTION *c, bool connect ));
63 static int ConnSSL_HandleError PARAMS(( CONNECTION *c, const int code, const char *fname ));
64
65 #ifdef HAVE_LIBGNUTLS
66 static char * openreadclose(const char *name, size_t *len)
67 {
68         char *buf = NULL;
69         struct stat s;
70         ssize_t br;
71         int fd = open(name, O_RDONLY);
72         if (fd < 0) {
73                 Log(LOG_ERR, "Could not open %s: %s", name, strerror(errno));
74                 return NULL;
75         }
76         if (fstat(fd, &s)) {
77                 Log(LOG_ERR, "Could not fstat %s: %s", name, strerror(errno));
78                 goto out;
79         }
80         if (!S_ISREG(s.st_mode)) {
81                 Log(LOG_ERR, "%s: Not a regular file", name);
82                 goto out;
83         }
84         if (s.st_size <= 0) {
85                 Log(LOG_ERR, "%s: invalid file length (size %ld <= 0)", name, (long) s.st_size);
86                 goto out;
87         }
88         buf = malloc(s.st_size);
89         if (!buf) {
90                 Log(LOG_ERR, "Could not malloc %lu bytes for file %s: %s", s.st_size, name, strerror(errno));
91                 goto out;
92         }
93         br = read(fd, buf, s.st_size);
94         if (br != (ssize_t)s.st_size) {
95                 Log(LOG_ERR, "Could not read file %s: read returned %ld, expected %ld: %s",
96                         name, (long) br, (long) s.st_size, br == -1 ? strerror(errno):"short read?!");
97                 memset(buf, 0, s.st_size);
98                 free(buf);
99                 buf = NULL;
100         } else {
101                 *len = br;
102         }
103 out:
104         close(fd);
105         return buf;
106 }
107 #endif
108
109
110 #ifdef HAVE_LIBSSL
111 static void
112 LogOpenSSLError( const char *msg, const char *msg2 )
113 {
114         unsigned long err = ERR_get_error();
115         char * errmsg = err ? ERR_error_string(err, NULL) : "Unable to determine error";
116
117         if (!msg) msg = "SSL Error";
118         if (msg2)
119                 Log( LOG_ERR, "%s: %s: %s", msg, msg2, errmsg);
120         else
121                 Log( LOG_ERR, "%s: %s", msg, errmsg);
122 }
123
124
125 static int
126 pem_passwd_cb(char *buf, int size, int rwflag, void *password)
127 {
128         array *pass = password;
129         int passlen;
130
131         (void)rwflag;           /* rwflag is unused if DEBUG is not set. */
132         assert(rwflag == 0);    /* 0 -> callback used for decryption.
133                                  * See SSL_CTX_set_default_passwd_cb(3) */
134
135         passlen = (int) array_bytes(pass);
136
137         LogDebug("pem_passwd_cb buf size %d, array size %d", size, passlen);
138         assert(passlen >= 0);
139         if (passlen <= 0) {
140                 Log(LOG_ERR, "pem_passwd_cb: password required, but not set");
141                 return 0;
142         }
143         size = passlen > size ? size : passlen;
144         memcpy(buf, (char *)(array_start(pass)), size);
145         return size;
146 }
147 #endif
148
149
150 static bool
151 Load_DH_params(void)
152 {
153 #ifdef HAVE_LIBSSL
154         FILE *fp;
155         bool ret = true;
156
157         if (!Conf_SSLOptions.DHFile) {
158                 Log(LOG_NOTICE, "Configuration option \"SSLDHFile\" not set!");
159                 return false;
160         }
161         fp = fopen(Conf_SSLOptions.DHFile, "r");
162         if (!fp) {
163                 Log(LOG_ERR, "%s: %s", Conf_SSLOptions.DHFile, strerror(errno));
164                 return false;
165         }
166         dh_params = PEM_read_DHparams(fp, NULL, NULL, NULL);
167         if (!dh_params) {
168                 Log(LOG_ERR, "%s: PEM_read_DHparams failed!",
169                     Conf_SSLOptions.DHFile);
170                 ret = false;
171         }
172         fclose(fp);
173         return ret;
174 #endif
175 #ifdef HAVE_LIBGNUTLS
176         bool need_dhgenerate = true;
177         int err;
178         gnutls_dh_params_t tmp_dh_params;
179
180         err = gnutls_dh_params_init(&tmp_dh_params);
181         if (err < 0) {
182                 Log(LOG_ERR, "gnutls_dh_params_init: %s", gnutls_strerror(err));
183                 return false;
184         }
185         if (Conf_SSLOptions.DHFile) {
186                 gnutls_datum_t dhparms;
187                 size_t size;
188                 dhparms.data = (unsigned char *) openreadclose(Conf_SSLOptions.DHFile, &size);
189                 if (dhparms.data) {
190                         dhparms.size = size;
191                         err = gnutls_dh_params_import_pkcs3(tmp_dh_params, &dhparms, GNUTLS_X509_FMT_PEM);
192                         if (err == 0)
193                                 need_dhgenerate = false;
194                         else
195                                 Log(LOG_ERR, "gnutls_dh_params_init: %s", gnutls_strerror(err));
196
197                         memset(dhparms.data, 0, size);
198                         free(dhparms.data);
199                 }
200         }
201         if (need_dhgenerate) {
202                 Log(LOG_WARNING,
203                     "SSLDHFile not set, generating %u bit DH parameters. This may take a while ...",
204                     DH_BITS);
205                 err = gnutls_dh_params_generate2(tmp_dh_params, DH_BITS);
206                 if (err < 0) {
207                         Log(LOG_ERR, "gnutls_dh_params_generate2: %s", gnutls_strerror(err));
208                         return false;
209                 }
210         }
211         dh_params = tmp_dh_params;
212         return true;
213 #endif
214 }
215
216
217 void ConnSSL_Free(CONNECTION *c)
218 {
219 #ifdef HAVE_LIBSSL
220         SSL *ssl = c->ssl_state.ssl;
221         if (ssl) {
222                 SSL_shutdown(ssl);
223                 SSL_free(ssl);
224                 c->ssl_state.ssl = NULL;
225         }
226 #endif
227 #ifdef HAVE_LIBGNUTLS
228         gnutls_session_t sess = c->ssl_state.gnutls_session;
229         if (Conn_OPTION_ISSET(c, CONN_SSL)) {
230                 gnutls_bye(sess, GNUTLS_SHUT_RDWR);
231                 gnutls_deinit(sess);
232         }
233 #endif
234         assert(Conn_OPTION_ISSET(c, CONN_SSL));
235         /* can't just set bitmask to 0 -- there are other, non-ssl related flags, e.g. CONN_ZIP. */
236         Conn_OPTION_DEL(c, CONN_SSL_FLAGS_ALL);
237 }
238
239
240 bool
241 ConnSSL_InitLibrary( void )
242 {
243 #ifdef HAVE_LIBSSL
244         SSL_CTX *newctx;
245
246         if (!ssl_ctx) {
247                 SSL_library_init();
248                 SSL_load_error_strings();
249         }
250
251         if (!RAND_status()) {
252                 Log(LOG_ERR, "OpenSSL PRNG not seeded: /dev/urandom missing?");
253                 /*
254                  * it is probably best to fail and let the user install EGD or a similar program if no kernel random device is available.
255                  * According to OpenSSL RAND_egd(3): "The automatic query of /var/run/egd-pool et al was added in OpenSSL 0.9.7";
256                  * so it makes little sense to deal with PRNGD seeding ourselves.
257                  */
258                 return false;
259         }
260
261         newctx = SSL_CTX_new(SSLv23_method());
262         if (!newctx) {
263                 LogOpenSSLError("SSL_CTX_new()", NULL);
264                 return false;
265         }
266
267         if (!ConnSSL_LoadServerKey_openssl(newctx))
268                 goto out;
269
270         SSL_CTX_set_options(newctx, SSL_OP_SINGLE_DH_USE|SSL_OP_NO_SSLv2);
271         SSL_CTX_set_mode(newctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
272         SSL_CTX_free(ssl_ctx);
273         ssl_ctx = newctx;
274         Log(LOG_INFO, "%s initialized.", SSLeay_version(SSLEAY_VERSION));
275         return true;
276 out:
277         SSL_CTX_free(newctx);
278         return false;
279 #endif
280 #ifdef HAVE_LIBGNUTLS
281         int err;
282         static bool initialized;
283         if (initialized) /* TODO: cannot reload gnutls keys: can't simply free x509 context -- it may still be in use */
284                 return false;
285
286         err = gnutls_global_init();
287         if (err) {
288                 Log(LOG_ERR, "gnutls_global_init(): %s", gnutls_strerror(err));
289                 return false;
290         }
291         if (!ConnSSL_LoadServerKey_gnutls())
292                 return false;
293         Log(LOG_INFO, "gnutls %s initialized.", gnutls_check_version(NULL));
294         initialized = true;
295         return true;
296 #endif
297 }
298
299
300 #ifdef HAVE_LIBGNUTLS
301 static bool
302 ConnSSL_LoadServerKey_gnutls(void)
303 {
304         int err;
305         const char *cert_file;
306
307         err = gnutls_certificate_allocate_credentials(&x509_cred);
308         if (err < 0) {
309                 Log(LOG_ERR, "gnutls_certificate_allocate_credentials: %s", gnutls_strerror(err));
310                 return false;
311         }
312
313         cert_file = Conf_SSLOptions.CertFile ? Conf_SSLOptions.CertFile:Conf_SSLOptions.KeyFile;
314         if (!cert_file) {
315                 Log(LOG_NOTICE, "No SSL server key configured, SSL disabled.");
316                 return false;
317         }
318
319         if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
320                 Log(LOG_WARNING,
321                     "Ignoring KeyFilePassword: Not supported by GNUTLS.");
322
323         if (!Load_DH_params())
324                 return false;
325
326         gnutls_certificate_set_dh_params(x509_cred, dh_params);
327         err = gnutls_certificate_set_x509_key_file(x509_cred, cert_file, Conf_SSLOptions.KeyFile, GNUTLS_X509_FMT_PEM);
328         if (err < 0) {
329                 Log(LOG_ERR, "gnutls_certificate_set_x509_key_file (cert %s, key %s): %s",
330                                 cert_file, Conf_SSLOptions.KeyFile ? Conf_SSLOptions.KeyFile : "(NULL)", gnutls_strerror(err));
331                 return false;
332         }
333         return true;
334 }
335 #endif
336
337
338 #ifdef HAVE_LIBSSL
339 static bool
340 ConnSSL_LoadServerKey_openssl(SSL_CTX *ctx)
341 {
342         char *cert_key;
343
344         assert(ctx);
345         if (!Conf_SSLOptions.KeyFile) {
346                 Log(LOG_NOTICE, "No SSL server key configured, SSL disabled.");
347                 return false;
348         }
349
350         SSL_CTX_set_default_passwd_cb(ctx, pem_passwd_cb);
351         SSL_CTX_set_default_passwd_cb_userdata(ctx, &Conf_SSLOptions.KeyFilePassword);
352
353         if (SSL_CTX_use_PrivateKey_file(ctx, Conf_SSLOptions.KeyFile, SSL_FILETYPE_PEM) != 1) {
354                 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
355                 LogOpenSSLError("SSL_CTX_use_PrivateKey_file",  Conf_SSLOptions.KeyFile);
356                 return false;
357         }
358
359         cert_key = Conf_SSLOptions.CertFile ? Conf_SSLOptions.CertFile:Conf_SSLOptions.KeyFile;
360         if (SSL_CTX_use_certificate_chain_file(ctx, cert_key) != 1) {
361                 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
362                 LogOpenSSLError("SSL_CTX_use_certificate_file", cert_key);
363                 return false;
364         }
365
366         array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
367
368         if (!SSL_CTX_check_private_key(ctx)) {
369                 LogOpenSSLError("Server Private Key does not match certificate", NULL);
370                 return false;
371         }
372         if (Load_DH_params()) {
373                 if (SSL_CTX_set_tmp_dh(ctx, dh_params) != 1)
374                         LogOpenSSLError("Error setting DH Parameters", Conf_SSLOptions.DHFile);
375                 /* don't return false here: the non-DH modes will still work */
376                 DH_free(dh_params);
377                 dh_params = NULL;
378         }
379         return true;
380 }
381
382
383 #endif
384 static bool
385 ConnSSL_Init_SSL(CONNECTION *c)
386 {
387         int ret;
388         assert(c != NULL);
389 #ifdef HAVE_LIBSSL
390         if (!ssl_ctx) {
391                 Log(LOG_ERR, "Cannot init ssl_ctx: OpenSSL initialization failed at startup");
392                 return false;
393         }
394         assert(c->ssl_state.ssl == NULL);
395
396         c->ssl_state.ssl = SSL_new(ssl_ctx);
397         if (!c->ssl_state.ssl) {
398                 LogOpenSSLError("SSL_new()", NULL);
399                 return false;
400         }
401
402         ret = SSL_set_fd(c->ssl_state.ssl, c->sock);
403         if (ret != 1) {
404                 LogOpenSSLError("SSL_set_fd()", NULL);
405                 ConnSSL_Free(c);
406                 return false;
407         }
408 #endif
409 #ifdef HAVE_LIBGNUTLS
410         ret = gnutls_set_default_priority(c->ssl_state.gnutls_session);
411         if (ret < 0) {
412                 Log(LOG_ERR, "gnutls_set_default_priority: %s", gnutls_strerror(ret));
413                 ConnSSL_Free(c);
414                 return false;
415         }
416         /*
417          * The intermediate (long) cast is here to avoid a warning like:
418          * "cast to pointer from integer of different size" on 64-bit platforms.
419          * There doesn't seem to be an alternate GNUTLS API we could use instead, see e.g.
420          * http://www.mail-archive.com/help-gnutls@gnu.org/msg00286.html
421          */
422         gnutls_transport_set_ptr(c->ssl_state.gnutls_session, (gnutls_transport_ptr_t) (long) c->sock);
423         ret = gnutls_credentials_set(c->ssl_state.gnutls_session, GNUTLS_CRD_CERTIFICATE, x509_cred);
424         if (ret < 0) {
425                 Log(LOG_ERR, "gnutls_credentials_set: %s", gnutls_strerror(ret));
426                 ConnSSL_Free(c);
427                 return false;
428         }
429         gnutls_dh_set_prime_bits(c->ssl_state.gnutls_session, DH_BITS);
430 #endif
431         Conn_OPTION_ADD(c, CONN_SSL);
432         return true;
433 }
434
435
436 bool
437 ConnSSL_PrepareConnect(CONNECTION *c, UNUSED CONF_SERVER *s)
438 {
439         bool ret;
440 #ifdef HAVE_LIBGNUTLS
441         int err;
442
443         err = gnutls_init(&c->ssl_state.gnutls_session, GNUTLS_CLIENT);
444         if (err) {
445                 Log(LOG_ERR, "gnutls_init: %s", gnutls_strerror(err));
446                 return false;
447         }
448 #endif
449         ret = ConnSSL_Init_SSL(c);
450         if (!ret)
451                 return false;
452         Conn_OPTION_ADD(c, CONN_SSL_CONNECT);
453 #ifdef HAVE_LIBSSL
454         assert(c->ssl_state.ssl);
455         SSL_set_verify(c->ssl_state.ssl, SSL_VERIFY_NONE, NULL);
456 #endif
457         return true;
458 }
459
460
461 /*
462   Check an Handle Error return code after failed calls to ssl/tls functions.
463   OpenSSL:
464   SSL_connect(), SSL_accept(), SSL_do_handshake(), SSL_read(), SSL_peek(), or SSL_write() on ssl.
465   GNUTLS:
466   gnutlsssl_read(), gnutls_write() or gnutls_handshake().
467   Return: -1 on fatal error, 0 if we can try again later.
468  */
469 static int
470 ConnSSL_HandleError( CONNECTION *c, const int code, const char *fname )
471 {
472 #ifdef HAVE_LIBSSL
473         int ret = SSL_ERROR_SYSCALL;
474         unsigned long sslerr;
475         int real_errno = errno;
476
477         ret = SSL_get_error(c->ssl_state.ssl, code);
478         switch (ret) {
479         case SSL_ERROR_WANT_READ:
480                 io_event_del(c->sock, IO_WANTWRITE);
481                 Conn_OPTION_ADD(c, CONN_SSL_WANT_READ);
482                 return 0;       /* try again later */
483         case SSL_ERROR_WANT_WRITE:
484                 io_event_del(c->sock, IO_WANTREAD);
485                 Conn_OPTION_ADD(c, CONN_SSL_WANT_WRITE); /* fall through */
486         case SSL_ERROR_NONE:
487                 return 0;       /* try again later */
488         case SSL_ERROR_ZERO_RETURN:
489                 LogDebug("TLS/SSL connection shut down normally");
490                 break;
491         /*
492         SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT, SSL_ERROR_WANT_X509_LOOKUP
493         */
494         case SSL_ERROR_SYSCALL:
495                 sslerr = ERR_get_error();
496                 if (sslerr) {
497                         Log( LOG_ERR, "%s: %s", fname, ERR_error_string(sslerr, NULL ));
498                 } else {
499
500                         switch (code) { /* EOF that violated protocol */
501                         case 0:
502                                 Log(LOG_ERR, "%s: Client Disconnected", fname );
503                                 break;
504                         case -1: /* low level socket I/O error, check errno */
505                                 Log(LOG_ERR, "%s: %s", fname, strerror(real_errno));
506                         }
507                 }
508                 break;
509         case SSL_ERROR_SSL:
510                 LogOpenSSLError("TLS/SSL Protocol Error", fname);
511                 break;
512         default:
513                 Log( LOG_ERR, "%s: Unknown error %d!", fname, ret);
514         }
515         ConnSSL_Free(c);
516         return -1;
517 #endif
518 #ifdef HAVE_LIBGNUTLS
519         switch (code) {
520         case GNUTLS_E_AGAIN:
521         case GNUTLS_E_INTERRUPTED:
522                 if (gnutls_record_get_direction(c->ssl_state.gnutls_session)) {
523                         Conn_OPTION_ADD(c, CONN_SSL_WANT_WRITE);
524                         io_event_del(c->sock, IO_WANTREAD);
525                 } else {
526                         Conn_OPTION_ADD(c, CONN_SSL_WANT_READ);
527                         io_event_del(c->sock, IO_WANTWRITE);
528                 }
529                 break;
530         default:
531                 assert(code < 0);
532                 if (gnutls_error_is_fatal(code)) {
533                         Log(LOG_ERR, "%s: %s", fname, gnutls_strerror(code));
534                         ConnSSL_Free(c);
535                         return -1;
536                 }
537         }
538         return 0;
539 #endif
540 }
541
542
543 static void
544 ConnSSL_LogCertInfo( CONNECTION *c )
545 {
546 #ifdef HAVE_LIBSSL
547         SSL *ssl = c->ssl_state.ssl;
548
549         assert(ssl);
550
551         Log(LOG_INFO, "New %s connection using cipher %s on socket %d.",
552                 SSL_get_version(ssl), SSL_get_cipher(ssl), c->sock);
553 #endif
554 #ifdef HAVE_LIBGNUTLS
555         gnutls_session_t sess = c->ssl_state.gnutls_session;
556         gnutls_cipher_algorithm_t cipher = gnutls_cipher_get(sess);
557
558         Log(LOG_INFO, "New %s connection using cipher %s-%s on socket %d.",
559             gnutls_protocol_get_name(gnutls_protocol_get_version(sess)),
560             gnutls_cipher_get_name(cipher),
561             gnutls_mac_get_name(gnutls_mac_get(sess)), c->sock);
562 #endif
563 }
564
565
566 /*
567  Accept incoming SSL connection.
568  Return Values:
569          1: SSL Connection established
570          0: try again
571         -1: SSL Connection not established due to fatal error.
572 */
573 int
574 ConnSSL_Accept( CONNECTION *c )
575 {
576         assert(c != NULL);
577         if (!Conn_OPTION_ISSET(c, CONN_SSL)) {
578 #ifdef HAVE_LIBGNUTLS
579                 int err = gnutls_init(&c->ssl_state.gnutls_session, GNUTLS_SERVER);
580                 if (err) {
581                         Log(LOG_ERR, "gnutls_init: %s", gnutls_strerror(err));
582                         return false;
583                 }
584 #endif
585                 LogDebug("ConnSSL_Accept: Initializing SSL data");
586                 if (!ConnSSL_Init_SSL(c))
587                         return -1;
588         }
589         return ConnectAccept(c, false );
590 }
591
592
593 int
594 ConnSSL_Connect( CONNECTION *c )
595 {
596         assert(c != NULL);
597 #ifdef HAVE_LIBSSL
598         assert(c->ssl_state.ssl);
599 #endif
600         assert(Conn_OPTION_ISSET(c, CONN_SSL));
601         return ConnectAccept(c, true);
602 }
603
604
605 /* accept/connect wrapper. if connect is true, connect to peer, otherwise wait for incoming connection */
606 static int
607 ConnectAccept( CONNECTION *c, bool connect)
608 {
609         int ret;
610 #ifdef HAVE_LIBSSL
611         SSL *ssl = c->ssl_state.ssl;
612         assert(ssl != NULL);
613
614         ret = connect ? SSL_connect(ssl) : SSL_accept(ssl);
615         if (1 != ret)
616                 return ConnSSL_HandleError(c, ret, connect ? "SSL_connect": "SSL_accept");
617 #endif
618 #ifdef HAVE_LIBGNUTLS
619         (void) connect;
620         ret = gnutls_handshake(c->ssl_state.gnutls_session);
621         if (ret)
622                 return ConnSSL_HandleError(c, ret, "gnutls_handshake");
623 #endif /* _GNUTLS */
624         Conn_OPTION_DEL(c, (CONN_SSL_WANT_WRITE|CONN_SSL_WANT_READ|CONN_SSL_CONNECT));
625         ConnSSL_LogCertInfo(c);
626         return 1;
627 }
628
629
630 ssize_t
631 ConnSSL_Write(CONNECTION *c, const void *buf, size_t count)
632 {
633         ssize_t bw;
634
635         Conn_OPTION_DEL(c, CONN_SSL_WANT_WRITE|CONN_SSL_WANT_READ);
636
637         assert(count > 0);
638 #ifdef HAVE_LIBSSL
639         bw = (ssize_t) SSL_write(c->ssl_state.ssl, buf, count);
640 #endif
641 #ifdef HAVE_LIBGNUTLS
642         bw = gnutls_write(c->ssl_state.gnutls_session, buf, count);
643 #endif
644         if (bw > 0)
645                 return bw;
646         if (ConnSSL_HandleError( c, bw, "ConnSSL_Write") == 0)
647                 errno = EAGAIN; /* try again */
648         return -1;
649 }
650
651
652 ssize_t
653 ConnSSL_Read(CONNECTION *c, void * buf, size_t count)
654 {
655         ssize_t br;
656
657         Conn_OPTION_DEL(c, CONN_SSL_WANT_WRITE|CONN_SSL_WANT_READ);
658 #ifdef HAVE_LIBSSL
659         br = (ssize_t) SSL_read(c->ssl_state.ssl, buf, count);
660         if (br > 0)     /* on EOF we have to call ConnSSL_HandleError(), see SSL_read(3) */
661                 return br;
662 #endif
663 #ifdef HAVE_LIBGNUTLS
664         br = gnutls_read(c->ssl_state.gnutls_session, buf, count);
665         if (br >= 0)    /* on EOF we must _not_ call ConnSSL_HandleError, see gnutls_record_recv(3) */
666                 return br;
667 #endif
668         /* error on read: switch ConnSSL_HandleError() return values -> 0 is "try again", so return -1 and set EAGAIN */
669         if (ConnSSL_HandleError(c, br, "ConnSSL_Read") == 0) {
670                 errno = EAGAIN;
671                 return -1;
672         }
673         return 0;
674 }
675
676
677 bool
678 ConnSSL_GetCipherInfo(CONNECTION *c, char *buf, size_t len)
679 {
680 #ifdef HAVE_LIBSSL
681         char *nl;
682         SSL *ssl = c->ssl_state.ssl;
683
684         if (!ssl)
685                 return false;
686         *buf = 0;
687         SSL_CIPHER_description(SSL_get_current_cipher(ssl), buf, len);
688         nl = strchr(buf, '\n');
689         if (nl)
690                 *nl = 0;
691         return true;
692 #endif
693 #ifdef HAVE_LIBGNUTLS
694         if (Conn_OPTION_ISSET(c, CONN_SSL)) {
695                 const char *name_cipher, *name_mac, *name_proto, *name_keyexchange;
696                 unsigned keysize;
697
698                 gnutls_session_t sess = c->ssl_state.gnutls_session;
699                 gnutls_cipher_algorithm_t cipher = gnutls_cipher_get(sess);
700                 name_cipher = gnutls_cipher_get_name(cipher);
701                 name_mac = gnutls_mac_get_name(gnutls_mac_get(sess));
702                 keysize = gnutls_cipher_get_key_size(cipher) * 8;
703                 name_proto = gnutls_protocol_get_name(gnutls_protocol_get_version(sess));
704                 name_keyexchange = gnutls_kx_get_name(gnutls_kx_get(sess));
705
706                 return snprintf(buf, len, "%s-%s%15s Kx=%s      Enc=%s(%u) Mac=%s",
707                         name_cipher, name_mac, name_proto, name_keyexchange, name_cipher, keysize, name_mac) > 0;
708         }
709         return false;
710 #endif
711 }
712
713
714 #endif /* SSL_SUPPORT */
715 /* -eof- */
716
717