2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6 * Copyright (C) 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
8 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9 * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
11 * This file is part of InspIRCd. InspIRCd is free software: you can
12 * redistribute it and/or modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation, version 2.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "modules/ssl.h"
29 // Ignore OpenSSL deprecation warnings on OS X Lion and newer.
31 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
34 // Fix warnings about the use of `long long` on C++03.
36 # pragma clang diagnostic ignored "-Wc++11-long-long"
37 #elif defined __GNUC__
38 # pragma GCC diagnostic ignored "-Wlong-long"
41 #include <openssl/ssl.h>
42 #include <openssl/err.h>
45 # pragma comment(lib, "ssleay32.lib")
46 # pragma comment(lib, "libeay32.lib")
49 /* $CompileFlags: pkgconfversion("openssl","0.9.7") pkgconfincludes("openssl","/openssl/ssl.h","") */
50 /* $LinkerFlags: rpath("pkg-config --libs openssl") pkgconflibs("openssl","/libssl.so","-lssl -lcrypto") */
52 #if ((OPENSSL_VERSION_NUMBER >= 0x10000000L) && (!(defined(OPENSSL_NO_ECDH))))
53 // OpenSSL 0.9.8 includes some ECC support, but it's unfinished. Enable only for 1.0.0 and later.
54 #define INSPIRCD_OPENSSL_ENABLE_ECDH
57 enum issl_status { ISSL_NONE, ISSL_HANDSHAKING, ISSL_OPEN };
59 static bool SelfSigned = false;
60 static int exdataindex;
64 return ERR_error_string(ERR_get_error(), NULL);
67 static int OnVerify(int preverify_ok, X509_STORE_CTX* ctx);
68 static void StaticSSLInfoCallback(const SSL* ssl, int where, int rc);
72 class Exception : public ModuleException
75 Exception(const std::string& reason)
76 : ModuleException(reason) { }
84 DHParams(const std::string& filename)
86 BIO* dhpfile = BIO_new_file(filename.c_str(), "r");
88 throw Exception("Couldn't open DH file " + filename);
90 dh = PEM_read_bio_DHparams(dhpfile, NULL, NULL, NULL);
94 throw Exception("Couldn't read DH params from file " + filename);
114 Context(SSL_CTX* context)
117 // Sane default options for OpenSSL see https://www.openssl.org/docs/ssl/SSL_CTX_set_options.html
118 // and when choosing a cipher, use the server's preferences instead of the client preferences.
119 long opts = SSL_OP_NO_SSLv2 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | SSL_OP_CIPHER_SERVER_PREFERENCE | SSL_OP_SINGLE_DH_USE;
120 // Only turn options on if they exist
121 #ifdef SSL_OP_SINGLE_ECDH_USE
122 opts |= SSL_OP_SINGLE_ECDH_USE;
124 #ifdef SSL_OP_NO_TICKET
125 opts |= SSL_OP_NO_TICKET;
128 ctx_options = SSL_CTX_set_options(ctx, opts);
130 long mode = SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER;
131 #ifdef SSL_MODE_RELEASE_BUFFERS
132 mode |= SSL_MODE_RELEASE_BUFFERS;
134 SSL_CTX_set_mode(ctx, mode);
135 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, OnVerify);
136 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
137 SSL_CTX_set_info_callback(ctx, StaticSSLInfoCallback);
145 bool SetDH(DHParams& dh)
148 return (SSL_CTX_set_tmp_dh(ctx, dh.get()) >= 0);
151 #ifdef INSPIRCD_OPENSSL_ENABLE_ECDH
152 void SetECDH(const std::string& curvename)
154 int nid = OBJ_sn2nid(curvename.c_str());
156 throw Exception("Unknown curve: " + curvename);
158 EC_KEY* eckey = EC_KEY_new_by_curve_name(nid);
160 throw Exception("Unable to create EC key object");
163 bool ret = (SSL_CTX_set_tmp_ecdh(ctx, eckey) >= 0);
166 throw Exception("Couldn't set ECDH parameters");
170 bool SetCiphers(const std::string& ciphers)
173 return SSL_CTX_set_cipher_list(ctx, ciphers.c_str());
176 bool SetCerts(const std::string& filename)
179 return SSL_CTX_use_certificate_chain_file(ctx, filename.c_str());
182 bool SetPrivateKey(const std::string& filename)
185 return SSL_CTX_use_PrivateKey_file(ctx, filename.c_str(), SSL_FILETYPE_PEM);
188 bool SetCA(const std::string& filename)
191 return SSL_CTX_load_verify_locations(ctx, filename.c_str(), 0);
194 long GetDefaultContextOptions() const
199 long SetRawContextOptions(long setoptions, long clearoptions)
202 SSL_CTX_clear_options(ctx, SSL_CTX_get_options(ctx));
204 // Set the default options and what is in the conf
205 SSL_CTX_set_options(ctx, ctx_options | setoptions);
206 return SSL_CTX_clear_options(ctx, clearoptions);
209 SSL* CreateServerSession()
211 SSL* sess = SSL_new(ctx);
212 SSL_set_accept_state(sess); // Act as server
216 SSL* CreateClientSession()
218 SSL* sess = SSL_new(ctx);
219 SSL_set_connect_state(sess); // Act as client
224 class Profile : public refcountbase
226 /** Name of this profile
228 const std::string name;
230 /** DH parameters in use
234 /** OpenSSL makes us have two contexts, one for servers and one for clients
239 /** Digest to use when generating fingerprints
241 const EVP_MD* digest;
243 /** Last error, set by error_callback()
247 /** True if renegotiations are allowed, false if not
249 const bool allowrenego;
251 /** Rough max size of records to send
253 const unsigned int outrecsize;
255 static int error_callback(const char* str, size_t len, void* u)
257 Profile* profile = reinterpret_cast<Profile*>(u);
258 profile->lasterr = std::string(str, len - 1);
262 /** Set raw OpenSSL context (SSL_CTX) options from a config tag
263 * @param ctxname Name of the context, client or server
264 * @param tag Config tag defining this profile
265 * @param context Context object to manipulate
267 void SetContextOptions(const std::string& ctxname, ConfigTag* tag, Context& context)
269 long setoptions = tag->getInt(ctxname + "setoptions");
270 long clearoptions = tag->getInt(ctxname + "clearoptions");
271 #ifdef SSL_OP_NO_COMPRESSION
272 if (!tag->getBool("compression", true))
273 setoptions |= SSL_OP_NO_COMPRESSION;
275 if (!tag->getBool("sslv3", true))
276 setoptions |= SSL_OP_NO_SSLv3;
277 if (!tag->getBool("tlsv1", true))
278 setoptions |= SSL_OP_NO_TLSv1;
280 if (!setoptions && !clearoptions)
281 return; // Nothing to do
283 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Setting %s %s context options, default: %ld set: %ld clear: %ld", name.c_str(), ctxname.c_str(), ctx.GetDefaultContextOptions(), setoptions, clearoptions);
284 long final = context.SetRawContextOptions(setoptions, clearoptions);
285 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "%s %s context options: %ld", name.c_str(), ctxname.c_str(), final);
289 Profile(const std::string& profilename, ConfigTag* tag)
291 , dh(ServerInstance->Config->Paths.PrependConfig(tag->getString("dhfile", "dh.pem")))
292 , ctx(SSL_CTX_new(SSLv23_server_method()))
293 , clictx(SSL_CTX_new(SSLv23_client_method()))
294 , allowrenego(tag->getBool("renegotiation")) // Disallow by default
295 , outrecsize(tag->getInt("outrecsize", 2048, 512, 16384))
297 if ((!ctx.SetDH(dh)) || (!clictx.SetDH(dh)))
298 throw Exception("Couldn't set DH parameters");
300 std::string hash = tag->getString("hash", "md5");
301 digest = EVP_get_digestbyname(hash.c_str());
303 throw Exception("Unknown hash type " + hash);
305 std::string ciphers = tag->getString("ciphers");
306 if (!ciphers.empty())
308 if ((!ctx.SetCiphers(ciphers)) || (!clictx.SetCiphers(ciphers)))
310 ERR_print_errors_cb(error_callback, this);
311 throw Exception("Can't set cipher list to \"" + ciphers + "\" " + lasterr);
315 #ifdef INSPIRCD_OPENSSL_ENABLE_ECDH
316 std::string curvename = tag->getString("ecdhcurve", "prime256v1");
317 if (!curvename.empty())
318 ctx.SetECDH(curvename);
321 SetContextOptions("server", tag, ctx);
322 SetContextOptions("client", tag, clictx);
324 /* Load our keys and certificates
325 * NOTE: OpenSSL's error logging API sucks, don't blame us for this clusterfuck.
327 std::string filename = ServerInstance->Config->Paths.PrependConfig(tag->getString("certfile", "cert.pem"));
328 if ((!ctx.SetCerts(filename)) || (!clictx.SetCerts(filename)))
330 ERR_print_errors_cb(error_callback, this);
331 throw Exception("Can't read certificate file: " + lasterr);
334 filename = ServerInstance->Config->Paths.PrependConfig(tag->getString("keyfile", "key.pem"));
335 if ((!ctx.SetPrivateKey(filename)) || (!clictx.SetPrivateKey(filename)))
337 ERR_print_errors_cb(error_callback, this);
338 throw Exception("Can't read key file: " + lasterr);
341 // Load the CAs we trust
342 filename = ServerInstance->Config->Paths.PrependConfig(tag->getString("cafile", "ca.pem"));
343 if ((!ctx.SetCA(filename)) || (!clictx.SetCA(filename)))
345 ERR_print_errors_cb(error_callback, this);
346 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Can't read CA list from %s. This is only a problem if you want to verify client certificates, otherwise it's safe to ignore this message. Error: %s", filename.c_str(), lasterr.c_str());
350 const std::string& GetName() const { return name; }
351 SSL* CreateServerSession() { return ctx.CreateServerSession(); }
352 SSL* CreateClientSession() { return clictx.CreateClientSession(); }
353 const EVP_MD* GetDigest() { return digest; }
354 bool AllowRenegotiation() const { return allowrenego; }
355 unsigned int GetOutgoingRecordSize() const { return outrecsize; }
360 static int create(BIO* bio)
366 static long ctrl(BIO* bio, int cmd, long num, void* ptr)
368 if (cmd == BIO_CTRL_FLUSH)
373 static int read(BIO* bio, char* buf, int len);
374 static int write(BIO* bio, const char* buf, int len);
378 static BIO_METHOD biomethods =
380 (100 | BIO_TYPE_SOURCE_SINK),
382 OpenSSL::BIOMethod::write,
383 OpenSSL::BIOMethod::read,
386 OpenSSL::BIOMethod::ctrl,
387 OpenSSL::BIOMethod::create,
388 NULL, // destroy, NULL causes older OpenSSL to leak memory in BIO_free() (bio_lib.c)
389 NULL // callback_ctrl
392 static int OnVerify(int preverify_ok, X509_STORE_CTX *ctx)
394 /* XXX: This will allow self signed certificates.
395 * In the future if we want an option to not allow this,
396 * we can just return preverify_ok here, and openssl
397 * will boot off self-signed and invalid peer certs.
399 int ve = X509_STORE_CTX_get_error(ctx);
401 SelfSigned = (ve == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT);
406 class OpenSSLIOHook : public SSLIOHook
412 reference<OpenSSL::Profile> profile;
414 // Returns 1 if handshake succeeded, 0 if it is still in progress, -1 if it failed
415 int Handshake(StreamSocket* user)
418 int ret = SSL_do_handshake(sess);
421 int err = SSL_get_error(sess, ret);
423 if (err == SSL_ERROR_WANT_READ)
425 SocketEngine::ChangeEventMask(user, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
426 this->status = ISSL_HANDSHAKING;
429 else if (err == SSL_ERROR_WANT_WRITE)
431 SocketEngine::ChangeEventMask(user, FD_WANT_NO_READ | FD_WANT_SINGLE_WRITE);
432 this->status = ISSL_HANDSHAKING;
443 // Handshake complete.
448 SocketEngine::ChangeEventMask(user, FD_WANT_POLL_READ | FD_WANT_NO_WRITE | FD_ADD_TRIAL_WRITE);
471 void VerifyCertificate()
474 ssl_cert* certinfo = new ssl_cert;
475 this->certificate = certinfo;
477 unsigned char md[EVP_MAX_MD_SIZE];
479 cert = SSL_get_peer_certificate(sess);
483 certinfo->error = "Could not get peer certificate: "+std::string(get_error());
487 certinfo->invalid = (SSL_get_verify_result(sess) != X509_V_OK);
491 certinfo->unknownsigner = false;
492 certinfo->trusted = true;
496 certinfo->unknownsigner = true;
497 certinfo->trusted = false;
501 X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
503 // Make sure there are no chars in the string that we consider invalid
504 if (certinfo->dn.find_first_of("\r\n") != std::string::npos)
505 certinfo->dn.clear();
507 X509_NAME_oneline(X509_get_issuer_name(cert), buf, sizeof(buf));
508 certinfo->issuer = buf;
509 if (certinfo->issuer.find_first_of("\r\n") != std::string::npos)
510 certinfo->issuer.clear();
512 if (!X509_digest(cert, profile->GetDigest(), md, &n))
514 certinfo->error = "Out of memory generating fingerprint";
518 certinfo->fingerprint = BinToHex(md, n);
521 if ((ASN1_UTCTIME_cmp_time_t(X509_get_notAfter(cert), ServerInstance->Time()) == -1) || (ASN1_UTCTIME_cmp_time_t(X509_get_notBefore(cert), ServerInstance->Time()) == 0))
523 certinfo->error = "Not activated, or expired certificate";
529 void SSLInfoCallback(int where, int rc)
531 if ((where & SSL_CB_HANDSHAKE_START) && (status == ISSL_OPEN))
533 if (profile->AllowRenegotiation())
536 // The other side is trying to renegotiate, kill the connection and change status
537 // to ISSL_NONE so CheckRenego() closes the session
539 BIO* bio = SSL_get_rbio(sess);
540 EventHandler* eh = static_cast<StreamSocket*>(bio->ptr);
541 SocketEngine::Shutdown(eh, 2);
545 bool CheckRenego(StreamSocket* sock)
547 if (status != ISSL_NONE)
550 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Session %p killed, attempted to renegotiate", (void*)sess);
552 sock->SetError("Renegotiation is not allowed");
556 // Returns 1 if application I/O should proceed, 0 if it must wait for the underlying protocol to progress, -1 on fatal error
557 int PrepareIO(StreamSocket* sock)
559 if (status == ISSL_OPEN)
561 else if (status == ISSL_HANDSHAKING)
563 // The handshake isn't finished, try to finish it
564 return Handshake(sock);
571 // Calls our private SSLInfoCallback()
572 friend void StaticSSLInfoCallback(const SSL* ssl, int where, int rc);
575 OpenSSLIOHook(IOHookProvider* hookprov, StreamSocket* sock, SSL* session, const reference<OpenSSL::Profile>& sslprofile)
576 : SSLIOHook(hookprov)
579 , data_to_write(false)
580 , profile(sslprofile)
582 // Create BIO instance and store a pointer to the socket in it which will be used by the read and write functions
583 BIO* bio = BIO_new(&biomethods);
585 SSL_set_bio(sess, bio, bio);
587 SSL_set_ex_data(sess, exdataindex, this);
588 sock->AddIOHook(this);
592 void OnStreamSocketClose(StreamSocket* user) CXX11_OVERRIDE
597 int OnStreamSocketRead(StreamSocket* user, std::string& recvq) CXX11_OVERRIDE
599 // Finish handshake if needed
600 int prepret = PrepareIO(user);
604 // If we resumed the handshake then this->status will be ISSL_OPEN
607 char* buffer = ServerInstance->GetReadBuffer();
608 size_t bufsiz = ServerInstance->Config->NetBufferSize;
609 int ret = SSL_read(sess, buffer, bufsiz);
611 if (!CheckRenego(user))
616 recvq.append(buffer, ret);
618 SocketEngine::ChangeEventMask(user, FD_WANT_POLL_READ | FD_WANT_SINGLE_WRITE);
623 // Client closed connection.
625 user->SetError("Connection closed");
630 int err = SSL_get_error(sess, ret);
632 if (err == SSL_ERROR_WANT_READ)
634 SocketEngine::ChangeEventMask(user, FD_WANT_POLL_READ);
637 else if (err == SSL_ERROR_WANT_WRITE)
639 SocketEngine::ChangeEventMask(user, FD_WANT_NO_READ | FD_WANT_SINGLE_WRITE);
651 int OnStreamSocketWrite(StreamSocket* user) CXX11_OVERRIDE
653 // Finish handshake if needed
654 int prepret = PrepareIO(user);
658 data_to_write = true;
660 // Session is ready for transferring application data
661 StreamSocket::SendQueue& sendq = user->GetSendQ();
662 while (!sendq.empty())
665 FlattenSendQueue(sendq, profile->GetOutgoingRecordSize());
666 const StreamSocket::SendQueue::Element& buffer = sendq.front();
667 int ret = SSL_write(sess, buffer.data(), buffer.size());
669 if (!CheckRenego(user))
672 if (ret == (int)buffer.length())
674 // Wrote entire record, continue sending
679 sendq.erase_front(ret);
680 SocketEngine::ChangeEventMask(user, FD_WANT_SINGLE_WRITE);
690 int err = SSL_get_error(sess, ret);
692 if (err == SSL_ERROR_WANT_WRITE)
694 SocketEngine::ChangeEventMask(user, FD_WANT_SINGLE_WRITE);
697 else if (err == SSL_ERROR_WANT_READ)
699 SocketEngine::ChangeEventMask(user, FD_WANT_POLL_READ);
710 data_to_write = false;
711 SocketEngine::ChangeEventMask(user, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
715 void TellCiphersAndFingerprint(LocalUser* user)
719 std::string text = "*** You are connected using SSL cipher '";
720 GetCiphersuite(text);
722 const std::string& fingerprint = certificate->fingerprint;
723 if (!fingerprint.empty())
724 text += " and your SSL certificate fingerprint is " + fingerprint;
726 user->WriteNotice(text);
730 void GetCiphersuite(std::string& out) const
732 out.append(SSL_get_version(sess)).push_back('-');
733 out.append(SSL_get_cipher(sess));
736 bool IsHandshakeDone() const { return (status == ISSL_OPEN); }
739 static void StaticSSLInfoCallback(const SSL* ssl, int where, int rc)
741 OpenSSLIOHook* hook = static_cast<OpenSSLIOHook*>(SSL_get_ex_data(ssl, exdataindex));
742 hook->SSLInfoCallback(where, rc);
745 static int OpenSSL::BIOMethod::write(BIO* bio, const char* buffer, int size)
747 BIO_clear_retry_flags(bio);
749 StreamSocket* sock = static_cast<StreamSocket*>(bio->ptr);
750 if (sock->GetEventMask() & FD_WRITE_WILL_BLOCK)
752 // Writes blocked earlier, don't retry syscall
753 BIO_set_retry_write(bio);
757 int ret = SocketEngine::Send(sock, buffer, size, 0);
758 if ((ret < size) && ((ret > 0) || (SocketEngine::IgnoreError())))
760 // Blocked, set retry flag for OpenSSL
761 SocketEngine::ChangeEventMask(sock, FD_WRITE_WILL_BLOCK);
762 BIO_set_retry_write(bio);
768 static int OpenSSL::BIOMethod::read(BIO* bio, char* buffer, int size)
770 BIO_clear_retry_flags(bio);
772 StreamSocket* sock = static_cast<StreamSocket*>(bio->ptr);
773 if (sock->GetEventMask() & FD_READ_WILL_BLOCK)
775 // Reads blocked earlier, don't retry syscall
776 BIO_set_retry_read(bio);
780 int ret = SocketEngine::Recv(sock, buffer, size, 0);
781 if ((ret < size) && ((ret > 0) || (SocketEngine::IgnoreError())))
783 // Blocked, set retry flag for OpenSSL
784 SocketEngine::ChangeEventMask(sock, FD_READ_WILL_BLOCK);
785 BIO_set_retry_read(bio);
791 class OpenSSLIOHookProvider : public refcountbase, public IOHookProvider
793 reference<OpenSSL::Profile> profile;
796 OpenSSLIOHookProvider(Module* mod, reference<OpenSSL::Profile>& prof)
797 : IOHookProvider(mod, "ssl/" + prof->GetName(), IOHookProvider::IOH_SSL)
800 ServerInstance->Modules->AddService(*this);
803 ~OpenSSLIOHookProvider()
805 ServerInstance->Modules->DelService(*this);
808 void OnAccept(StreamSocket* sock, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) CXX11_OVERRIDE
810 new OpenSSLIOHook(this, sock, profile->CreateServerSession(), profile);
813 void OnConnect(StreamSocket* sock) CXX11_OVERRIDE
815 new OpenSSLIOHook(this, sock, profile->CreateClientSession(), profile);
819 class ModuleSSLOpenSSL : public Module
821 typedef std::vector<reference<OpenSSLIOHookProvider> > ProfileList;
823 ProfileList profiles;
827 ProfileList newprofiles;
828 ConfigTagList tags = ServerInstance->Config->ConfTags("sslprofile");
829 if (tags.first == tags.second)
831 // Create a default profile named "openssl"
832 const std::string defname = "openssl";
833 ConfigTag* tag = ServerInstance->Config->ConfValue(defname);
834 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "No <sslprofile> tags found, using settings from the <openssl> tag");
838 reference<OpenSSL::Profile> profile(new OpenSSL::Profile(defname, tag));
839 newprofiles.push_back(new OpenSSLIOHookProvider(this, profile));
841 catch (OpenSSL::Exception& ex)
843 throw ModuleException("Error while initializing the default SSL profile - " + ex.GetReason());
847 for (ConfigIter i = tags.first; i != tags.second; ++i)
849 ConfigTag* tag = i->second;
850 if (tag->getString("provider") != "openssl")
853 std::string name = tag->getString("name");
856 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Ignoring <sslprofile> tag without name at " + tag->getTagLocation());
860 reference<OpenSSL::Profile> profile;
863 profile = new OpenSSL::Profile(name, tag);
865 catch (CoreException& ex)
867 throw ModuleException("Error while initializing SSL profile \"" + name + "\" at " + tag->getTagLocation() + " - " + ex.GetReason());
870 newprofiles.push_back(new OpenSSLIOHookProvider(this, profile));
873 profiles.swap(newprofiles);
879 // Initialize OpenSSL
881 SSL_load_error_strings();
884 void init() CXX11_OVERRIDE
886 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "OpenSSL lib version \"%s\" module was compiled for \"" OPENSSL_VERSION_TEXT "\"", SSLeay_version(SSLEAY_VERSION));
888 // Register application specific data
889 char exdatastr[] = "inspircd";
890 exdataindex = SSL_get_ex_new_index(0, exdatastr, NULL, NULL, NULL);
892 throw ModuleException("Failed to register application specific data");
897 void OnModuleRehash(User* user, const std::string ¶m) CXX11_OVERRIDE
906 catch (ModuleException& ex)
908 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, ex.GetReason() + " Not applying settings.");
912 void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
914 IOHook* hook = user->eh.GetIOHook();
915 if (hook && hook->prov->creator == this)
916 static_cast<OpenSSLIOHook*>(hook)->TellCiphersAndFingerprint(user);
919 void OnCleanup(int target_type, void* item) CXX11_OVERRIDE
921 if (target_type == TYPE_USER)
923 LocalUser* user = IS_LOCAL((User*)item);
925 if (user && user->eh.GetIOHook() && user->eh.GetIOHook()->prov->creator == this)
927 // User is using SSL, they're a local user, and they're using one of *our* SSL ports.
928 // Potentially there could be multiple SSL modules loaded at once on different ports.
929 ServerInstance->Users->QuitUser(user, "SSL module unloading");
934 ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
936 if ((user->eh.GetIOHook()) && (user->eh.GetIOHook()->prov->creator == this))
938 OpenSSLIOHook* iohook = static_cast<OpenSSLIOHook*>(user->eh.GetIOHook());
939 if (!iohook->IsHandshakeDone())
943 return MOD_RES_PASSTHRU;
946 Version GetVersion() CXX11_OVERRIDE
948 return Version("Provides SSL support for clients", VF_VENDOR);
952 MODULE_INIT(ModuleSSLOpenSSL)