1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 #include <gnutls/gnutls.h>
17 #include <gnutls/x509.h>
19 #include "inspircd_config.h"
20 #include "configreader.h"
26 #include "transport.h"
30 #pragma comment(lib, "libgnutls-13.lib")
33 /* $ModDesc: Provides SSL support for clients */
34 /* $CompileFlags: exec("libgnutls-config --cflags") */
35 /* $LinkerFlags: rpath("libgnutls-config --libs") exec("libgnutls-config --libs") */
36 /* $ModDep: transport.h */
37 /* $CopyInstall: conf/key.pem $(CONPATH) */
38 /* $CopyInstall: conf/cert.pem $(CONPATH) */
40 enum issl_status { ISSL_NONE, ISSL_HANDSHAKING_READ, ISSL_HANDSHAKING_WRITE, ISSL_HANDSHAKEN, ISSL_CLOSING, ISSL_CLOSED };
42 bool isin(const std::string &host, int port, const std::vector<std::string> &portlist)
44 if (std::find(portlist.begin(), portlist.end(), "*:" + ConvToStr(port)) != portlist.end())
47 if (std::find(portlist.begin(), portlist.end(), ":" + ConvToStr(port)) != portlist.end())
50 return std::find(portlist.begin(), portlist.end(), host + ":" + ConvToStr(port)) != portlist.end();
53 /** Represents an SSL user's extra data
55 class issl_session : public classbase
58 gnutls_session_t sess;
66 class CommandStartTLS : public Command
70 /* Command 'dalinfo', takes no parameters and needs no special modes */
71 CommandStartTLS (InspIRCd* Instance, Module* mod) : Command(Instance,"STARTTLS", 0, 0, true), Caller(mod)
73 this->source = "m_ssl_gnutls.so";
76 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
78 if (user->registered == REG_ALL)
80 ServerInstance->Users->QuitUser(user, "STARTTLS not allowed after client registration");
85 Caller->OnRawSocketAccept(user->GetFd(), user->GetIPString(), user->GetPort());
92 class ModuleSSLGnuTLS : public Module
99 std::vector<std::string> listenports;
102 issl_session* sessions;
104 gnutls_certificate_credentials x509_cred;
105 gnutls_dh_params dh_params;
108 std::string certfile;
111 std::string sslports;
116 CommandStartTLS* starttls;
120 ModuleSSLGnuTLS(InspIRCd* Me)
123 ServerInstance->Modules->PublishInterface("BufferedSocketHook", this);
125 sessions = new issl_session[ServerInstance->SE->GetMaxFds()];
127 // Not rehashable...because I cba to reduce all the sizes of existing buffers.
128 inbufsize = ServerInstance->Config->NetBufferSize;
130 gnutls_global_init(); // This must be called once in the program
132 if(gnutls_certificate_allocate_credentials(&x509_cred) != 0)
133 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to allocate certificate credentials");
135 // Guessing return meaning
136 if(gnutls_dh_params_init(&dh_params) < 0)
137 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to initialise DH parameters");
139 // Needs the flag as it ignores a plain /rehash
140 OnRehash(NULL,"ssl");
142 // Void return, guess we assume success
143 gnutls_certificate_set_dh_params(x509_cred, dh_params);
144 Implementation eventlist[] = { I_On005Numeric, I_OnRawSocketConnect, I_OnRawSocketAccept, I_OnRawSocketClose, I_OnRawSocketRead, I_OnRawSocketWrite, I_OnCleanup,
145 I_OnBufferFlushed, I_OnRequest, I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnUnloadModule, I_OnRehash, I_OnWhois, I_OnPostConnect, I_OnEvent, I_OnHookUserIO };
146 ServerInstance->Modules->Attach(eventlist, this, 17);
148 starttls = new CommandStartTLS(ServerInstance, this);
149 ServerInstance->AddCommand(starttls);
152 virtual void OnRehash(User* user, const std::string ¶m)
154 Conf = new ConfigReader(ServerInstance);
160 for(int index = 0; index < Conf->Enumerate("bind"); index++)
162 // For each <bind> tag
163 std::string x = Conf->ReadValue("bind", "type", index);
164 if(((x.empty()) || (x == "clients")) && (Conf->ReadValue("bind", "ssl", index) == "gnutls"))
166 // Get the port we're meant to be listening on with SSL
167 std::string port = Conf->ReadValue("bind", "port", index);
168 std::string addr = Conf->ReadValue("bind", "address", index);
170 irc::portparser portrange(port, false);
172 while ((portno = portrange.GetToken()))
177 listenports.push_back(addr + ":" + ConvToStr(portno));
179 for (size_t i = 0; i < ServerInstance->Config->ports.size(); i++)
180 if ((ServerInstance->Config->ports[i]->GetPort() == portno) && (ServerInstance->Config->ports[i]->GetIP() == addr))
181 ServerInstance->Config->ports[i]->SetDescription("ssl");
182 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %ld", portno);
184 sslports.append((addr.empty() ? "*" : addr)).append(":").append(ConvToStr(portno)).append(";");
186 catch (ModuleException &e)
188 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: FAILED to enable SSL on port %ld: %s. Maybe it's already hooked by the same port on a different IP, or you have an other SSL or similar module loaded?", portno, e.GetReason());
194 if (!sslports.empty())
195 sslports.erase(sslports.end() - 1);
203 std::string confdir(ServerInstance->ConfigFileName);
204 // +1 so we the path ends with a /
205 confdir = confdir.substr(0, confdir.find_last_of('/') + 1);
207 cafile = Conf->ReadValue("gnutls", "cafile", 0);
208 crlfile = Conf->ReadValue("gnutls", "crlfile", 0);
209 certfile = Conf->ReadValue("gnutls", "certfile", 0);
210 keyfile = Conf->ReadValue("gnutls", "keyfile", 0);
211 dh_bits = Conf->ReadInteger("gnutls", "dhbits", 0, false);
213 // Set all the default values needed.
220 if (certfile.empty())
221 certfile = "cert.pem";
226 if((dh_bits != 768) && (dh_bits != 1024) && (dh_bits != 2048) && (dh_bits != 3072) && (dh_bits != 4096))
229 // Prepend relative paths with the path to the config directory.
230 if ((cafile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(cafile)))
231 cafile = confdir + cafile;
233 if ((crlfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(crlfile)))
234 crlfile = confdir + crlfile;
236 if ((certfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(certfile)))
237 certfile = confdir + certfile;
239 if ((keyfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(keyfile)))
240 keyfile = confdir + keyfile;
244 if((ret =gnutls_certificate_set_x509_trust_file(x509_cred, cafile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
245 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to set X.509 trust file '%s': %s", cafile.c_str(), gnutls_strerror(ret));
247 if((ret = gnutls_certificate_set_x509_crl_file (x509_cred, crlfile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
248 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to set X.509 CRL file '%s': %s", crlfile.c_str(), gnutls_strerror(ret));
250 if((ret = gnutls_certificate_set_x509_key_file (x509_cred, certfile.c_str(), keyfile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
252 // If this fails, no SSL port will work. At all. So, do the smart thing - throw a ModuleException
253 throw ModuleException("Unable to load GnuTLS server certificate: " + std::string(gnutls_strerror(ret)));
256 // This may be on a large (once a day or week) timer eventually.
262 void GenerateDHParams()
264 // Generate Diffie Hellman parameters - for use with DHE
265 // kx algorithms. These should be discarded and regenerated
266 // once a day, once a week or once a month. Depending on the
267 // security requirements.
271 if((ret = gnutls_dh_params_generate2(dh_params, dh_bits)) < 0)
272 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to generate DH parameters (%d bits): %s", dh_bits, gnutls_strerror(ret));
275 virtual ~ModuleSSLGnuTLS()
277 gnutls_dh_params_deinit(dh_params);
278 gnutls_certificate_free_credentials(x509_cred);
279 gnutls_global_deinit();
280 ServerInstance->Modules->UnpublishInterface("BufferedSocketHook", this);
284 virtual void OnCleanup(int target_type, void* item)
286 if(target_type == TYPE_USER)
288 User* user = (User*)item;
292 // User is using SSL, they're a local user, and they're using one of *our* SSL ports.
293 // Potentially there could be multiple SSL modules loaded at once on different ports.
294 ServerInstance->Users->QuitUser(user, "SSL module unloading");
296 if (user->GetExt("ssl_cert", dummy))
299 user->GetExt("ssl_cert", tofree);
301 user->Shrink("ssl_cert");
308 virtual void OnUnloadModule(Module* mod, const std::string &name)
312 for(unsigned int i = 0; i < listenports.size(); i++)
314 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
315 if (listenports[i] == (ServerInstance->Config->ports[j]->GetIP()+":"+ConvToStr(ServerInstance->Config->ports[j]->GetPort())))
316 ServerInstance->Config->ports[j]->SetDescription("plaintext");
321 virtual Version GetVersion()
323 return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
327 virtual void On005Numeric(std::string &output)
329 output.append(" SSL=" + sslports);
332 virtual void OnHookUserIO(User* user, const std::string &targetip)
334 if (!user->io && isin(targetip,user->GetPort(),listenports))
336 /* Hook the user with our module */
341 virtual const char* OnRequest(Request* request)
343 ISHRequest* ISR = (ISHRequest*)request;
344 if (strcmp("IS_NAME", request->GetId()) == 0)
348 else if (strcmp("IS_HOOK", request->GetId()) == 0)
350 const char* ret = "OK";
353 ret = ServerInstance->Config->AddIOHook((Module*)this, (BufferedSocket*)ISR->Sock) ? "OK" : NULL;
355 catch (ModuleException &e)
361 else if (strcmp("IS_UNHOOK", request->GetId()) == 0)
363 return ServerInstance->Config->DelIOHook((BufferedSocket*)ISR->Sock) ? "OK" : NULL;
365 else if (strcmp("IS_HSDONE", request->GetId()) == 0)
367 if (ISR->Sock->GetFd() < 0)
370 issl_session* session = &sessions[ISR->Sock->GetFd()];
371 return (session->status == ISSL_HANDSHAKING_READ || session->status == ISSL_HANDSHAKING_WRITE) ? NULL : "OK";
373 else if (strcmp("IS_ATTACH", request->GetId()) == 0)
375 if (ISR->Sock->GetFd() > -1)
377 issl_session* session = &sessions[ISR->Sock->GetFd()];
380 if ((Extensible*)ServerInstance->FindDescriptor(ISR->Sock->GetFd()) == (Extensible*)(ISR->Sock))
382 VerifyCertificate(session, (BufferedSocket*)ISR->Sock);
392 virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
394 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
395 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
398 issl_session* session = &sessions[fd];
400 /* For STARTTLS: Don't try and init a session on a socket that already has a session */
405 session->inbuf = new char[inbufsize];
406 session->inbufoffset = 0;
408 gnutls_init(&session->sess, GNUTLS_SERVER);
410 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
411 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
412 gnutls_dh_set_prime_bits(session->sess, dh_bits);
414 /* This is an experimental change to avoid a warning on 64bit systems about casting between integer and pointer of different sizes
415 * This needs testing, but it's easy enough to rollback if need be
416 * Old: gnutls_transport_set_ptr(session->sess, (gnutls_transport_ptr_t) fd); // Give gnutls the fd for the socket.
417 * New: gnutls_transport_set_ptr(session->sess, &fd); // Give gnutls the fd for the socket.
419 * With testing this seems to...not work :/
422 gnutls_transport_set_ptr(session->sess, (gnutls_transport_ptr_t) fd); // Give gnutls the fd for the socket.
424 gnutls_certificate_server_set_request(session->sess, GNUTLS_CERT_REQUEST); // Request client certificate if any.
429 virtual void OnRawSocketConnect(int fd)
431 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
432 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
435 issl_session* session = &sessions[fd];
438 session->inbuf = new char[inbufsize];
439 session->inbufoffset = 0;
441 gnutls_init(&session->sess, GNUTLS_CLIENT);
443 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
444 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
445 gnutls_dh_set_prime_bits(session->sess, dh_bits);
446 gnutls_transport_set_ptr(session->sess, (gnutls_transport_ptr_t) fd); // Give gnutls the fd for the socket.
451 virtual void OnRawSocketClose(int fd)
453 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
454 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds()))
457 CloseSession(&sessions[fd]);
459 EventHandler* user = ServerInstance->SE->GetRef(fd);
461 if ((user) && (user->GetExt("ssl_cert", dummy)))
464 user->GetExt("ssl_cert", tofree);
466 user->Shrink("ssl_cert");
470 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
472 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
473 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
476 issl_session* session = &sessions[fd];
481 CloseSession(session);
485 if (session->status == ISSL_HANDSHAKING_READ)
487 // The handshake isn't finished, try to finish it.
489 if(!Handshake(session))
491 // Couldn't resume handshake.
495 else if (session->status == ISSL_HANDSHAKING_WRITE)
498 MakePollWrite(session);
502 // If we resumed the handshake then session->status will be ISSL_HANDSHAKEN.
504 if (session->status == ISSL_HANDSHAKEN)
506 // Is this right? Not sure if the unencrypted data is garaunteed to be the same length.
507 // Read into the inbuffer, offset from the beginning by the amount of data we have that insp hasn't taken yet.
508 int ret = gnutls_record_recv(session->sess, session->inbuf + session->inbufoffset, inbufsize - session->inbufoffset);
512 // Client closed connection.
514 CloseSession(session);
519 if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
527 CloseSession(session);
532 // Read successfully 'ret' bytes into inbuf + inbufoffset
533 // There are 'ret' + 'inbufoffset' bytes of data in 'inbuf'
534 // 'buffer' is 'count' long
536 unsigned int length = ret + session->inbufoffset;
540 memcpy(buffer, session->inbuf, count);
541 // Move the stuff left in inbuf to the beginning of it
542 memcpy(session->inbuf, session->inbuf + count, (length - count));
543 // Now we need to set session->inbufoffset to the amount of data still waiting to be handed to insp.
544 session->inbufoffset = length - count;
545 // Insp uses readresult as the count of how much data there is in buffer, so:
550 // There's not as much in the inbuf as there is space in the buffer, so just copy the whole thing.
551 memcpy(buffer, session->inbuf, length);
552 // Zero the offset, as there's nothing there..
553 session->inbufoffset = 0;
559 else if(session->status == ISSL_CLOSING)
565 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
567 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
568 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
571 issl_session* session = &sessions[fd];
572 const char* sendbuffer = buffer;
576 CloseSession(session);
580 session->outbuf.append(sendbuffer, count);
581 sendbuffer = session->outbuf.c_str();
582 count = session->outbuf.size();
584 if (session->status == ISSL_HANDSHAKING_WRITE)
586 // The handshake isn't finished, try to finish it.
594 if (session->status == ISSL_HANDSHAKEN)
596 ret = gnutls_record_send(session->sess, sendbuffer, count);
600 CloseSession(session);
604 if(ret != GNUTLS_E_AGAIN && ret != GNUTLS_E_INTERRUPTED)
606 CloseSession(session);
615 session->outbuf = session->outbuf.substr(ret);
619 MakePollWrite(session);
621 /* Who's smart idea was it to return 1 when we havent written anything?
622 * This fucks the buffer up in BufferedSocket :p
624 return ret < 1 ? 0 : ret;
627 // :kenny.chatspike.net 320 Om Epy|AFK :is a Secure Connection
628 virtual void OnWhois(User* source, User* dest)
633 // Bugfix, only send this numeric for *our* SSL users
634 if (dest->GetExt("ssl", dummy) || ((IS_LOCAL(dest) && (dest->io == this))))
636 ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
640 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
642 // check if the linking module wants to know about OUR metadata
645 // check if this user has an swhois field to send
646 if(user->GetExt(extname, dummy))
648 // call this function in the linking module, let it format the data how it
649 // sees fit, and send it on its way. We dont need or want to know how.
650 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, displayable ? "Enabled" : "ON");
655 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
657 // check if its our metadata key, and its associated with a user
658 if ((target_type == TYPE_USER) && (extname == "ssl"))
660 User* dest = (User*)target;
661 // if they dont already have an ssl flag, accept the remote server's
662 if (!dest->GetExt(extname, dummy))
664 dest->Extend(extname, "ON");
669 bool Handshake(issl_session* session)
671 int ret = gnutls_handshake(session->sess);
675 if(ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
677 // Handshake needs resuming later, read() or write() would have blocked.
679 if(gnutls_record_get_direction(session->sess) == 0)
681 // gnutls_handshake() wants to read() again.
682 session->status = ISSL_HANDSHAKING_READ;
686 // gnutls_handshake() wants to write() again.
687 session->status = ISSL_HANDSHAKING_WRITE;
688 MakePollWrite(session);
694 CloseSession(session);
695 session->status = ISSL_CLOSING;
702 // Handshake complete.
703 // This will do for setting the ssl flag...it could be done earlier if it's needed. But this seems neater.
704 User* extendme = ServerInstance->FindDescriptor(session->fd);
707 if (!extendme->GetExt("ssl", dummy))
708 extendme->Extend("ssl", "ON");
711 // Change the seesion state
712 session->status = ISSL_HANDSHAKEN;
714 // Finish writing, if any left
715 MakePollWrite(session);
721 virtual void OnPostConnect(User* user)
723 // This occurs AFTER OnUserConnect so we can be sure the
724 // protocol module has propagated the NICK message.
725 if ((user->GetExt("ssl", dummy)) && (IS_LOCAL(user)))
727 // Tell whatever protocol module we're using that we need to inform other servers of this metadata NOW.
728 ServerInstance->PI->SendMetaData(user, TYPE_USER, "SSL", "on");
730 VerifyCertificate(&sessions[user->GetFd()],user);
731 if (sessions[user->GetFd()].sess)
733 std::string cipher = gnutls_kx_get_name(gnutls_kx_get(sessions[user->GetFd()].sess));
734 cipher.append("-").append(gnutls_cipher_get_name(gnutls_cipher_get(sessions[user->GetFd()].sess))).append("-");
735 cipher.append(gnutls_mac_get_name(gnutls_mac_get(sessions[user->GetFd()].sess)));
736 user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\"", user->nick.c_str(), cipher.c_str());
741 void MakePollWrite(issl_session* session)
743 //OnRawSocketWrite(session->fd, NULL, 0);
744 EventHandler* eh = ServerInstance->FindDescriptor(session->fd);
746 ServerInstance->SE->WantWrite(eh);
749 virtual void OnBufferFlushed(User* user)
751 if (user->GetExt("ssl"))
753 issl_session* session = &sessions[user->GetFd()];
754 if (session && session->outbuf.size())
755 OnRawSocketWrite(user->GetFd(), NULL, 0);
759 void CloseSession(issl_session* session)
763 gnutls_bye(session->sess, GNUTLS_SHUT_WR);
764 gnutls_deinit(session->sess);
769 delete[] session->inbuf;
772 session->outbuf.clear();
773 session->inbuf = NULL;
774 session->sess = NULL;
775 session->status = ISSL_NONE;
778 void VerifyCertificate(issl_session* session, Extensible* user)
780 if (!session->sess || !user)
784 const gnutls_datum_t* cert_list;
786 unsigned int cert_list_size;
787 gnutls_x509_crt_t cert;
789 unsigned char digest[MAXBUF];
790 size_t digest_size = sizeof(digest);
791 size_t name_size = sizeof(name);
792 ssl_cert* certinfo = new ssl_cert;
794 user->Extend("ssl_cert",certinfo);
796 /* This verification function uses the trusted CAs in the credentials
797 * structure. So you must have installed one or more CA certificates.
799 ret = gnutls_certificate_verify_peers2(session->sess, &status);
803 certinfo->data.insert(std::make_pair("error",std::string(gnutls_strerror(ret))));
807 if (status & GNUTLS_CERT_INVALID)
809 certinfo->data.insert(std::make_pair("invalid",ConvToStr(1)));
813 certinfo->data.insert(std::make_pair("invalid",ConvToStr(0)));
815 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
817 certinfo->data.insert(std::make_pair("unknownsigner",ConvToStr(1)));
821 certinfo->data.insert(std::make_pair("unknownsigner",ConvToStr(0)));
823 if (status & GNUTLS_CERT_REVOKED)
825 certinfo->data.insert(std::make_pair("revoked",ConvToStr(1)));
829 certinfo->data.insert(std::make_pair("revoked",ConvToStr(0)));
831 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
833 certinfo->data.insert(std::make_pair("trusted",ConvToStr(0)));
837 certinfo->data.insert(std::make_pair("trusted",ConvToStr(1)));
840 /* Up to here the process is the same for X.509 certificates and
841 * OpenPGP keys. From now on X.509 certificates are assumed. This can
842 * be easily extended to work with openpgp keys as well.
844 if (gnutls_certificate_type_get(session->sess) != GNUTLS_CRT_X509)
846 certinfo->data.insert(std::make_pair("error","No X509 keys sent"));
850 ret = gnutls_x509_crt_init(&cert);
853 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
858 cert_list = gnutls_certificate_get_peers(session->sess, &cert_list_size);
859 if (cert_list == NULL)
861 certinfo->data.insert(std::make_pair("error","No certificate was found"));
865 /* This is not a real world example, since we only check the first
866 * certificate in the given chain.
869 ret = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
872 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
876 gnutls_x509_crt_get_dn(cert, name, &name_size);
878 certinfo->data.insert(std::make_pair("dn",name));
880 gnutls_x509_crt_get_issuer_dn(cert, name, &name_size);
882 certinfo->data.insert(std::make_pair("issuer",name));
884 if ((ret = gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, digest, &digest_size)) < 0)
886 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
890 certinfo->data.insert(std::make_pair("fingerprint",irc::hex(digest, digest_size)));
893 /* Beware here we do not check for errors.
895 if ((gnutls_x509_crt_get_expiration_time(cert) < time(0)) || (gnutls_x509_crt_get_activation_time(cert) > time(0)))
897 certinfo->data.insert(std::make_pair("error","Not activated, or expired certificate"));
900 gnutls_x509_crt_deinit(cert);
905 void OnEvent(Event* ev)
907 GenericCapHandler(ev, "tls", "tls");
912 Module* server = ServerInstance->Modules->Find("m_spanningtree.so");
913 ServerInstance->Modules->SetPriority(this, I_OnPostConnect, PRIO_AFTER, &server);
917 MODULE_INIT(ModuleSSLGnuTLS)