2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2005-2008 Florian Westphal (fw@strlen.de).
4 * Copyright (c)2008-2014 Alexander Barton (alex@barton.de) and Contributors.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * Please read the file COPYING, README and AUTHORS for more information.
17 * SSL wrapper functions
34 #include "conn-func.h"
40 extern struct SSLOptions Conf_SSLOptions;
43 #include <openssl/err.h>
44 #include <openssl/rand.h>
45 #include <openssl/dh.h>
47 static SSL_CTX * ssl_ctx;
50 static bool ConnSSL_LoadServerKey_openssl PARAMS(( SSL_CTX *c ));
54 #include <sys/types.h>
58 #include <gnutls/x509.h>
61 #define DH_BITS_MIN 1024
63 #define MAX_HASH_SIZE 64 /* from gnutls-int.h */
67 gnutls_certificate_credentials_t x509_cred;
68 gnutls_dh_params_t dh_params;
71 static array x509_creds = INIT_ARRAY;
72 static size_t x509_cred_idx;
74 static gnutls_dh_params_t dh_params;
75 static gnutls_priority_t priorities_cache = NULL;
76 static bool ConnSSL_LoadServerKey_gnutls PARAMS(( void ));
79 #define SHA256_STRING_LEN (32 * 2 + 1)
81 static bool ConnSSL_Init_SSL PARAMS(( CONNECTION *c ));
82 static int ConnectAccept PARAMS(( CONNECTION *c, bool connect ));
83 static int ConnSSL_HandleError PARAMS(( CONNECTION *c, const int code, const char *fname ));
86 static char * openreadclose(const char *name, size_t *len)
91 int fd = open(name, O_RDONLY);
93 Log(LOG_ERR, "Could not open %s: %s", name, strerror(errno));
97 Log(LOG_ERR, "Could not fstat %s: %s", name, strerror(errno));
100 if (!S_ISREG(s.st_mode)) {
101 Log(LOG_ERR, "%s: Not a regular file", name);
104 if (s.st_size <= 0) {
105 Log(LOG_ERR, "%s: invalid file length (size %ld <= 0)", name, (long) s.st_size);
108 buf = malloc(s.st_size);
110 Log(LOG_ERR, "Could not malloc %lu bytes for file %s: %s", s.st_size, name, strerror(errno));
113 br = read(fd, buf, s.st_size);
114 if (br != (ssize_t)s.st_size) {
115 Log(LOG_ERR, "Could not read file %s: read returned %ld, expected %ld: %s",
116 name, (long) br, (long) s.st_size, br == -1 ? strerror(errno):"short read?!");
117 memset(buf, 0, s.st_size);
132 * Log OpenSSL error message.
134 * @param msg The error message.
135 * @param info Additional information text or NULL.
138 LogOpenSSLError(const char *error, const char *info)
140 unsigned long err = ERR_get_error();
142 ? ERR_error_string(err, NULL)
143 : "Unable to determine error";
145 assert(error != NULL);
148 Log(LOG_ERR, "%s: %s (%s)", error, info, errmsg);
150 Log(LOG_ERR, "%s: %s", error, errmsg);
155 pem_passwd_cb(char *buf, int size, int rwflag, void *password)
157 array *pass = password;
160 (void)rwflag; /* rwflag is unused if DEBUG is not set. */
161 assert(rwflag == 0); /* 0 -> callback used for decryption.
162 * See SSL_CTX_set_default_passwd_cb(3) */
164 passlen = (int) array_bytes(pass);
166 LogDebug("pem_passwd_cb buf size %d, array size %d", size, passlen);
167 assert(passlen >= 0);
169 Log(LOG_ERR, "PEM password required but not set [in pem_passwd_cb()]!");
172 size = passlen > size ? size : passlen;
173 memcpy(buf, (char *)(array_start(pass)), size);
179 Verify_openssl(UNUSED int preverify_ok, UNUSED X509_STORE_CTX *x509_ctx)
193 if (!Conf_SSLOptions.DHFile) {
194 Log(LOG_NOTICE, "Configuration option \"DHFile\" not set!");
197 fp = fopen(Conf_SSLOptions.DHFile, "r");
199 Log(LOG_ERR, "%s: %s", Conf_SSLOptions.DHFile, strerror(errno));
202 dh_params = PEM_read_DHparams(fp, NULL, NULL, NULL);
204 Log(LOG_ERR, "%s: Failed to read SSL DH parameters!",
205 Conf_SSLOptions.DHFile);
211 #ifdef HAVE_LIBGNUTLS
212 bool need_dhgenerate = true;
214 gnutls_dh_params_t tmp_dh_params;
216 err = gnutls_dh_params_init(&tmp_dh_params);
218 Log(LOG_ERR, "Failed to initialize SSL DH parameters: %s",
219 gnutls_strerror(err));
222 if (Conf_SSLOptions.DHFile) {
223 gnutls_datum_t dhparms;
225 dhparms.data = (unsigned char *) openreadclose(Conf_SSLOptions.DHFile, &size);
228 err = gnutls_dh_params_import_pkcs3(tmp_dh_params, &dhparms, GNUTLS_X509_FMT_PEM);
230 need_dhgenerate = false;
233 "Failed to initialize SSL DH parameters: %s",
234 gnutls_strerror(err));
236 memset(dhparms.data, 0, size);
240 if (need_dhgenerate) {
242 "DHFile not set, generating %u bit DH parameters. This may take a while ...",
244 err = gnutls_dh_params_generate2(tmp_dh_params, DH_BITS);
246 Log(LOG_ERR, "Failed to generate SSL DH parameters: %s",
247 gnutls_strerror(err));
251 dh_params = tmp_dh_params;
257 void ConnSSL_Free(CONNECTION *c)
260 SSL *ssl = c->ssl_state.ssl;
264 c->ssl_state.ssl = NULL;
265 if (c->ssl_state.fingerprint) {
266 free(c->ssl_state.fingerprint);
267 c->ssl_state.fingerprint = NULL;
271 #ifdef HAVE_LIBGNUTLS
272 gnutls_session_t sess = c->ssl_state.gnutls_session;
273 if (Conn_OPTION_ISSET(c, CONN_SSL)) {
274 gnutls_bye(sess, GNUTLS_SHUT_RDWR);
277 x509_cred_slot *slot = array_get(&x509_creds, sizeof(x509_cred_slot), c->ssl_state.x509_cred_idx);
278 assert(slot != NULL);
279 assert(slot->refcnt > 0);
280 assert(slot->x509_cred != NULL);
282 if ((c->ssl_state.x509_cred_idx != x509_cred_idx) && (slot->refcnt <= 0)) {
283 Log(LOG_INFO, "Discarding X509 certificate credentials from slot %zd.",
284 c->ssl_state.x509_cred_idx);
285 gnutls_certificate_free_keys(slot->x509_cred);
286 gnutls_certificate_free_credentials(slot->x509_cred);
287 slot->x509_cred = NULL;
288 gnutls_dh_params_deinit(slot->dh_params);
289 slot->dh_params = NULL;
293 assert(Conn_OPTION_ISSET(c, CONN_SSL));
294 /* can't just set bitmask to 0 -- there are other, non-ssl related flags, e.g. CONN_ZIP. */
295 Conn_OPTION_DEL(c, CONN_SSL_FLAGS_ALL);
300 ConnSSL_InitLibrary( void )
302 if (!Conf_SSLInUse()) {
303 LogDebug("SSL not in use, skipping initialization.");
310 #if OPENSSL_API_COMPAT < 0x10100000L
313 SSL_load_error_strings();
317 if (!RAND_status()) {
318 Log(LOG_ERR, "OpenSSL PRNG not seeded: /dev/urandom missing?");
320 * it is probably best to fail and let the user install EGD or
321 * a similar program if no kernel random device is available.
322 * According to OpenSSL RAND_egd(3): "The automatic query of
323 * /var/run/egd-pool et al was added in OpenSSL 0.9.7";
324 * so it makes little sense to deal with PRNGD seeding ourselves.
326 array_free(&Conf_SSLOptions.ListenPorts);
330 newctx = SSL_CTX_new(SSLv23_method());
332 LogOpenSSLError("Failed to create SSL context", NULL);
333 array_free(&Conf_SSLOptions.ListenPorts);
337 if (!ConnSSL_LoadServerKey_openssl(newctx)) {
338 /* Failed to read new key but an old ssl context
339 * already exists -> reuse old context */
341 SSL_CTX_free(newctx);
343 "Re-Initializing of SSL failed, using old keys!");
346 /* No preexisting old context -> error. */
350 if (SSL_CTX_set_cipher_list(newctx, Conf_SSLOptions.CipherList) == 0) {
351 Log(LOG_ERR, "Failed to apply OpenSSL cipher list \"%s\"!",
352 Conf_SSLOptions.CipherList);
356 SSL_CTX_set_session_id_context(newctx, (unsigned char *)"ngircd", 6);
357 SSL_CTX_set_options(newctx, SSL_OP_SINGLE_DH_USE|SSL_OP_NO_SSLv2);
358 SSL_CTX_set_mode(newctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
359 SSL_CTX_set_verify(newctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
361 SSL_CTX_free(ssl_ctx);
363 Log(LOG_INFO, "%s initialized.", OpenSSL_version(OPENSSL_VERSION));
366 SSL_CTX_free(newctx);
367 array_free(&Conf_SSLOptions.ListenPorts);
370 #ifdef HAVE_LIBGNUTLS
372 static bool initialized;
375 err = gnutls_global_init();
377 Log(LOG_ERR, "Failed to initialize GnuTLS: %s",
378 gnutls_strerror(err));
383 if (!ConnSSL_LoadServerKey_gnutls())
386 if (priorities_cache != NULL) {
387 gnutls_priority_deinit(priorities_cache);
389 if (gnutls_priority_init(&priorities_cache, Conf_SSLOptions.CipherList,
390 NULL) != GNUTLS_E_SUCCESS) {
392 "Failed to apply GnuTLS cipher list \"%s\"!",
393 Conf_SSLOptions.CipherList);
397 Log(LOG_INFO, "GnuTLS %s initialized.", gnutls_check_version(NULL));
401 array_free(&Conf_SSLOptions.ListenPorts);
407 #ifdef HAVE_LIBGNUTLS
409 ConnSSL_LoadServerKey_gnutls(void)
412 const char *cert_file;
414 x509_cred_slot *slot = NULL;
415 gnutls_certificate_credentials_t x509_cred;
417 err = gnutls_certificate_allocate_credentials(&x509_cred);
419 Log(LOG_ERR, "Failed to allocate certificate credentials: %s",
420 gnutls_strerror(err));
424 cert_file = Conf_SSLOptions.CertFile ? Conf_SSLOptions.CertFile:Conf_SSLOptions.KeyFile;
426 Log(LOG_ERR, "No SSL server key configured!");
430 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
432 "Ignoring SSL \"KeyFilePassword\": Not supported by GnuTLS.");
434 if (!Load_DH_params())
437 gnutls_certificate_set_dh_params(x509_cred, dh_params);
438 err = gnutls_certificate_set_x509_key_file(x509_cred, cert_file, Conf_SSLOptions.KeyFile, GNUTLS_X509_FMT_PEM);
441 "Failed to set certificate key file (cert %s, key %s): %s",
443 Conf_SSLOptions.KeyFile ? Conf_SSLOptions.KeyFile : "(NULL)",
444 gnutls_strerror(err));
448 /* Free currently active x509 context (if any) unless it is still in use */
449 slot = array_get(&x509_creds, sizeof(x509_cred_slot), x509_cred_idx);
450 if ((slot != NULL) && (slot->refcnt <= 0) && (slot->x509_cred != NULL)) {
451 Log(LOG_INFO, "Discarding X509 certificate credentials from slot %zd.", x509_cred_idx);
452 gnutls_certificate_free_keys(slot->x509_cred);
453 gnutls_certificate_free_credentials(slot->x509_cred);
454 slot->x509_cred = NULL;
455 gnutls_dh_params_deinit(slot->dh_params);
456 slot->dh_params = NULL;
461 x509_cred_idx = (size_t) -1;
463 for (slot = array_start(&x509_creds), i = 0;
464 i < array_length(&x509_creds, sizeof(x509_cred_slot));
466 if (slot->refcnt <= 0) {
471 /* ... allocate new slot otherwise. */
472 if (x509_cred_idx == (size_t) -1) {
473 x509_cred_idx = array_length(&x509_creds, sizeof(x509_cred_slot));
474 slot = array_alloc(&x509_creds, sizeof(x509_cred_slot), x509_cred_idx);
476 Log(LOG_ERR, "Failed to allocate new slot for certificate credentials");
480 Log(LOG_INFO, "Storing new X509 certificate credentials in slot %zd.", x509_cred_idx);
481 slot->x509_cred = x509_cred;
491 ConnSSL_LoadServerKey_openssl(SSL_CTX *ctx)
496 if (!Conf_SSLOptions.KeyFile) {
497 Log(LOG_ERR, "No SSL server key configured!");
501 SSL_CTX_set_default_passwd_cb(ctx, pem_passwd_cb);
502 SSL_CTX_set_default_passwd_cb_userdata(ctx, &Conf_SSLOptions.KeyFilePassword);
504 if (SSL_CTX_use_PrivateKey_file(ctx, Conf_SSLOptions.KeyFile, SSL_FILETYPE_PEM) != 1) {
505 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
506 LogOpenSSLError("Failed to add private key", Conf_SSLOptions.KeyFile);
510 cert_key = Conf_SSLOptions.CertFile ? Conf_SSLOptions.CertFile:Conf_SSLOptions.KeyFile;
511 if (SSL_CTX_use_certificate_chain_file(ctx, cert_key) != 1) {
512 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
513 LogOpenSSLError("Failed to load certificate chain", cert_key);
517 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
519 if (!SSL_CTX_check_private_key(ctx)) {
520 LogOpenSSLError("Server private key does not match certificate", NULL);
523 if (Load_DH_params()) {
524 if (SSL_CTX_set_tmp_dh(ctx, dh_params) != 1)
525 LogOpenSSLError("Error setting DH parameters", Conf_SSLOptions.DHFile);
526 /* don't return false here: the non-DH modes will still work */
536 ConnSSL_Init_SSL(CONNECTION *c)
540 LogDebug("Initializing SSL ...");
546 "Can't initialize SSL context, OpenSSL initialization failed at startup!");
549 assert(c->ssl_state.ssl == NULL);
550 assert(c->ssl_state.fingerprint == NULL);
552 c->ssl_state.ssl = SSL_new(ssl_ctx);
553 if (!c->ssl_state.ssl) {
554 LogOpenSSLError("Failed to create SSL structure", NULL);
557 Conn_OPTION_ADD(c, CONN_SSL);
559 ret = SSL_set_fd(c->ssl_state.ssl, c->sock);
561 LogOpenSSLError("Failed to set SSL file descriptor", NULL);
566 #ifdef HAVE_LIBGNUTLS
567 Conn_OPTION_ADD(c, CONN_SSL);
568 ret = gnutls_priority_set(c->ssl_state.gnutls_session, priorities_cache);
569 if (ret != GNUTLS_E_SUCCESS) {
570 Log(LOG_ERR, "Failed to set GnuTLS session priorities: %s",
571 gnutls_strerror(ret));
576 * The intermediate (long) cast is here to avoid a warning like:
577 * "cast to pointer from integer of different size" on 64-bit platforms.
578 * There doesn't seem to be an alternate GNUTLS API we could use instead, see e.g.
579 * http://www.mail-archive.com/help-gnutls@gnu.org/msg00286.html
581 gnutls_transport_set_ptr(c->ssl_state.gnutls_session,
582 (gnutls_transport_ptr_t) (long) c->sock);
583 gnutls_certificate_server_set_request(c->ssl_state.gnutls_session,
584 GNUTLS_CERT_REQUEST);
586 Log(LOG_INFO, "Using X509 credentials from slot %zd", x509_cred_idx);
587 c->ssl_state.x509_cred_idx = x509_cred_idx;
588 x509_cred_slot *slot = array_get(&x509_creds, sizeof(x509_cred_slot), x509_cred_idx);
590 ret = gnutls_credentials_set(c->ssl_state.gnutls_session,
591 GNUTLS_CRD_CERTIFICATE, slot->x509_cred);
593 Log(LOG_ERR, "Failed to set SSL credentials: %s",
594 gnutls_strerror(ret));
598 gnutls_dh_set_prime_bits(c->ssl_state.gnutls_session, DH_BITS_MIN);
605 ConnSSL_PrepareConnect(CONNECTION *c, UNUSED CONF_SERVER *s)
608 #ifdef HAVE_LIBGNUTLS
611 err = gnutls_init(&c->ssl_state.gnutls_session, GNUTLS_CLIENT);
613 Log(LOG_ERR, "Failed to initialize new SSL session: %s",
614 gnutls_strerror(err));
618 ret = ConnSSL_Init_SSL(c);
621 Conn_OPTION_ADD(c, CONN_SSL_CONNECT);
623 assert(c->ssl_state.ssl);
624 SSL_set_verify(c->ssl_state.ssl, SSL_VERIFY_NONE, NULL);
631 * Check and handle error return codes after failed calls to SSL functions.
634 * SSL_connect(), SSL_accept(), SSL_do_handshake(), SSL_read(), SSL_peek(), or
635 * SSL_write() on ssl.
638 * gnutlsssl_read(), gnutls_write() or gnutls_handshake().
640 * @param c The connection handle.
641 * @prarm code The return code.
642 * @param fname The name of the function in which the error occurred.
643 * @return -1 on fatal errors, 0 if we can try again later.
646 ConnSSL_HandleError(CONNECTION * c, const int code, const char *fname)
649 int ret = SSL_ERROR_SYSCALL;
650 unsigned long sslerr;
651 int real_errno = errno;
653 ret = SSL_get_error(c->ssl_state.ssl, code);
656 case SSL_ERROR_WANT_READ:
657 io_event_del(c->sock, IO_WANTWRITE);
658 Conn_OPTION_ADD(c, CONN_SSL_WANT_READ);
659 return 0; /* try again later */
660 case SSL_ERROR_WANT_WRITE:
661 io_event_del(c->sock, IO_WANTREAD);
662 Conn_OPTION_ADD(c, CONN_SSL_WANT_WRITE); /* fall through */
664 return 0; /* try again later */
665 case SSL_ERROR_ZERO_RETURN:
666 LogDebug("SSL connection shut down normally.");
668 case SSL_ERROR_SYSCALL:
669 /* SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT,
670 * and SSL_ERROR_WANT_X509_LOOKUP */
671 sslerr = ERR_get_error();
673 Log(LOG_ERR, "SSL error: %s [in %s()]!",
674 ERR_error_string(sslerr, NULL), fname);
676 switch (code) { /* EOF that violated protocol */
679 "SSL error, client disconnected [in %s()]!",
682 case -1: /* low level socket I/O error, check errno */
683 Log(LOG_ERR, "SSL error: %s [in %s()]!",
684 strerror(real_errno), fname);
689 LogOpenSSLError("SSL protocol error", fname);
692 Log(LOG_ERR, "Unknown SSL error %d [in %s()]!", ret, fname);
697 #ifdef HAVE_LIBGNUTLS
700 case GNUTLS_E_INTERRUPTED:
701 if (gnutls_record_get_direction(c->ssl_state.gnutls_session)) {
702 Conn_OPTION_ADD(c, CONN_SSL_WANT_WRITE);
703 io_event_del(c->sock, IO_WANTREAD);
705 Conn_OPTION_ADD(c, CONN_SSL_WANT_READ);
706 io_event_del(c->sock, IO_WANTWRITE);
711 if (gnutls_error_is_fatal(code)) {
712 Log(LOG_ERR, "SSL error: %s [%s].",
713 gnutls_strerror(code), fname);
724 ConnSSL_LogCertInfo( CONNECTION *c )
727 SSL *ssl = c->ssl_state.ssl;
731 Log(LOG_INFO, "Connection %d: initialized %s using cipher %s.",
732 c->sock, SSL_get_version(ssl), SSL_get_cipher(ssl));
734 #ifdef HAVE_LIBGNUTLS
735 gnutls_session_t sess = c->ssl_state.gnutls_session;
736 gnutls_cipher_algorithm_t cipher = gnutls_cipher_get(sess);
738 Log(LOG_INFO, "Connection %d: initialized %s using cipher %s-%s.",
740 gnutls_protocol_get_name(gnutls_protocol_get_version(sess)),
741 gnutls_cipher_get_name(cipher),
742 gnutls_mac_get_name(gnutls_mac_get(sess)));
748 Accept incoming SSL connection.
750 1: SSL Connection established
752 -1: SSL Connection not established due to fatal error.
755 ConnSSL_Accept( CONNECTION *c )
758 if (!Conn_OPTION_ISSET(c, CONN_SSL)) {
759 #ifdef HAVE_LIBGNUTLS
760 int err = gnutls_init(&c->ssl_state.gnutls_session, GNUTLS_SERVER);
762 Log(LOG_ERR, "Failed to initialize new SSL session: %s",
763 gnutls_strerror(err));
767 if (!ConnSSL_Init_SSL(c))
770 return ConnectAccept(c, false );
775 ConnSSL_Connect( CONNECTION *c )
779 assert(c->ssl_state.ssl);
781 assert(Conn_OPTION_ISSET(c, CONN_SSL));
782 return ConnectAccept(c, true);
786 ConnSSL_InitCertFp( CONNECTION *c )
788 const char hex[] = "0123456789abcdef";
792 unsigned char digest[EVP_MAX_MD_SIZE];
793 unsigned int digest_size;
796 cert = SSL_get_peer_certificate(c->ssl_state.ssl);
800 if (!X509_digest(cert, EVP_sha256(), digest, &digest_size)) {
806 #endif /* HAVE_LIBSSL */
807 #ifdef HAVE_LIBGNUTLS
808 gnutls_x509_crt_t cert;
809 unsigned int cert_list_size;
810 const gnutls_datum_t *cert_list;
811 unsigned char digest[MAX_HASH_SIZE];
814 if (gnutls_certificate_type_get(c->ssl_state.gnutls_session) !=
818 if (gnutls_x509_crt_init(&cert) != GNUTLS_E_SUCCESS)
822 cert_list = gnutls_certificate_get_peers(c->ssl_state.gnutls_session,
825 gnutls_x509_crt_deinit(cert);
829 if (gnutls_x509_crt_import(cert, &cert_list[0],
830 GNUTLS_X509_FMT_DER) != GNUTLS_E_SUCCESS) {
831 gnutls_x509_crt_deinit(cert);
835 digest_size = sizeof(digest);
836 if (gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_SHA256, digest,
838 gnutls_x509_crt_deinit(cert);
842 gnutls_x509_crt_deinit(cert);
843 #endif /* HAVE_LIBGNUTLS */
845 assert(c->ssl_state.fingerprint == NULL);
847 c->ssl_state.fingerprint = malloc(SHA256_STRING_LEN);
848 if (!c->ssl_state.fingerprint)
851 for (i = 0; i < (int)digest_size; i++) {
852 c->ssl_state.fingerprint[i * 2] = hex[digest[i] / 16];
853 c->ssl_state.fingerprint[i * 2 + 1] = hex[digest[i] % 16];
855 c->ssl_state.fingerprint[i * 2] = '\0';
860 /* accept/connect wrapper. if connect is true, connect to peer, otherwise wait for incoming connection */
862 ConnectAccept( CONNECTION *c, bool connect)
866 SSL *ssl = c->ssl_state.ssl;
869 ret = connect ? SSL_connect(ssl) : SSL_accept(ssl);
871 return ConnSSL_HandleError(c, ret, connect ? "SSL_connect": "SSL_accept");
873 #ifdef HAVE_LIBGNUTLS
875 ret = gnutls_handshake(c->ssl_state.gnutls_session);
877 return ConnSSL_HandleError(c, ret, "gnutls_handshake");
879 (void)ConnSSL_InitCertFp(c);
881 Conn_OPTION_DEL(c, (CONN_SSL_WANT_WRITE|CONN_SSL_WANT_READ|CONN_SSL_CONNECT));
882 ConnSSL_LogCertInfo(c);
884 Conn_StartLogin(CONNECTION2ID(c));
890 ConnSSL_Write(CONNECTION *c, const void *buf, size_t count)
894 Conn_OPTION_DEL(c, CONN_SSL_WANT_WRITE|CONN_SSL_WANT_READ);
898 bw = (ssize_t) SSL_write(c->ssl_state.ssl, buf, count);
900 #ifdef HAVE_LIBGNUTLS
901 bw = gnutls_write(c->ssl_state.gnutls_session, buf, count);
905 if (ConnSSL_HandleError( c, bw, "ConnSSL_Write") == 0)
906 errno = EAGAIN; /* try again */
912 ConnSSL_Read(CONNECTION *c, void * buf, size_t count)
916 Conn_OPTION_DEL(c, CONN_SSL_WANT_WRITE|CONN_SSL_WANT_READ);
918 br = (ssize_t) SSL_read(c->ssl_state.ssl, buf, count);
919 if (br > 0) /* on EOF we have to call ConnSSL_HandleError(), see SSL_read(3) */
922 #ifdef HAVE_LIBGNUTLS
923 br = gnutls_read(c->ssl_state.gnutls_session, buf, count);
924 if (br >= 0) /* on EOF we must _not_ call ConnSSL_HandleError, see gnutls_record_recv(3) */
927 /* error on read: switch ConnSSL_HandleError() return values -> 0 is "try again", so return -1 and set EAGAIN */
928 if (ConnSSL_HandleError(c, br, "ConnSSL_Read") == 0) {
937 ConnSSL_GetCipherInfo(CONNECTION *c, char *buf, size_t len)
941 SSL *ssl = c->ssl_state.ssl;
946 SSL_CIPHER_description(SSL_get_current_cipher(ssl), buf, len);
947 nl = strchr(buf, '\n');
952 #ifdef HAVE_LIBGNUTLS
953 if (Conn_OPTION_ISSET(c, CONN_SSL)) {
954 const char *name_cipher, *name_mac, *name_proto, *name_keyexchange;
957 gnutls_session_t sess = c->ssl_state.gnutls_session;
958 gnutls_cipher_algorithm_t cipher = gnutls_cipher_get(sess);
959 name_cipher = gnutls_cipher_get_name(cipher);
960 name_mac = gnutls_mac_get_name(gnutls_mac_get(sess));
961 keysize = gnutls_cipher_get_key_size(cipher) * 8;
962 name_proto = gnutls_protocol_get_name(gnutls_protocol_get_version(sess));
963 name_keyexchange = gnutls_kx_get_name(gnutls_kx_get(sess));
965 return snprintf(buf, len, "%s-%s%15s Kx=%s Enc=%s(%u) Mac=%s",
966 name_cipher, name_mac, name_proto, name_keyexchange, name_cipher, keysize, name_mac) > 0;
973 ConnSSL_GetCertFp(CONNECTION *c)
975 return c->ssl_state.fingerprint;
979 ConnSSL_SetCertFp(CONNECTION *c, const char *fingerprint)
982 c->ssl_state.fingerprint = strndup(fingerprint, SHA256_STRING_LEN - 1);
983 return c->ssl_state.fingerprint != NULL;
988 ConnSSL_InitLibrary(void)
993 #endif /* SSL_SUPPORT */