1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include <gnutls/gnutls.h>
16 #include <gnutls/x509.h>
17 #include "transport.h"
21 #pragma comment(lib, "libgnutls-13.lib")
24 /* $ModDesc: Provides SSL support for clients */
25 /* $CompileFlags: pkgconfincludes("gnutls","/gnutls/gnutls.h","") */
26 /* $LinkerFlags: rpath("pkg-config --libs gnutls") pkgconflibs("gnutls","/libgnutls.so","-lgnutls") */
27 /* $ModDep: transport.h */
28 /* $CopyInstall: conf/key.pem $(CONPATH) */
29 /* $CopyInstall: conf/cert.pem $(CONPATH) */
31 enum issl_status { ISSL_NONE, ISSL_HANDSHAKING_READ, ISSL_HANDSHAKING_WRITE, ISSL_HANDSHAKEN, ISSL_CLOSING, ISSL_CLOSED };
33 bool isin(const std::string &host, int port, const std::vector<std::string> &portlist)
35 if (std::find(portlist.begin(), portlist.end(), "*:" + ConvToStr(port)) != portlist.end())
38 if (std::find(portlist.begin(), portlist.end(), ":" + ConvToStr(port)) != portlist.end())
41 return std::find(portlist.begin(), portlist.end(), host + ":" + ConvToStr(port)) != portlist.end();
44 /** Represents an SSL user's extra data
46 class issl_session : public classbase
54 gnutls_session_t sess;
59 class CommandStartTLS : public Command
63 CommandStartTLS (InspIRCd* Instance, Module* mod) : Command(Instance,"STARTTLS", 0, 0, true), Caller(mod)
65 this->source = "m_ssl_gnutls.so";
68 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
70 /* changed from == REG_ALL to catch clients sending STARTTLS
71 * after NICK and USER but before OnUserConnect completes and
72 * give a proper error message (see bug #645) - dz
74 if (user->registered != REG_NONE)
76 ServerInstance->Users->QuitUser(user, "STARTTLS is not permitted after client registration has started");
80 if (!user->GetIOHook())
82 user->WriteNumeric(670, "%s :STARTTLS successful, go ahead with TLS handshake", user->nick.c_str());
83 user->AddIOHook(Caller);
84 Caller->OnRawSocketAccept(user->GetFd(), user->GetIPString(), user->GetPort());
87 user->WriteNumeric(691, "%s :STARTTLS failure", user->nick.c_str());
94 class ModuleSSLGnuTLS : public Module
96 std::vector<std::string> listenports;
98 issl_session* sessions;
100 gnutls_certificate_credentials x509_cred;
101 gnutls_dh_params dh_params;
104 std::string certfile;
107 std::string sslports;
113 CommandStartTLS* starttls;
117 ModuleSSLGnuTLS(InspIRCd* Me)
120 ServerInstance->Modules->PublishInterface("BufferedSocketHook", this);
122 sessions = new issl_session[ServerInstance->SE->GetMaxFds()];
124 gnutls_global_init(); // This must be called once in the program
127 // Needs the flag as it ignores a plain /rehash
128 OnModuleRehash(NULL,"ssl");
130 // Void return, guess we assume success
131 gnutls_certificate_set_dh_params(x509_cred, dh_params);
132 Implementation eventlist[] = { I_On005Numeric, I_OnRawSocketConnect, I_OnRawSocketAccept, I_OnRawSocketClose, I_OnRawSocketRead, I_OnRawSocketWrite, I_OnCleanup,
133 I_OnBufferFlushed, I_OnRequest, I_OnSyncUserMetaData, I_OnDecodeMetaData,
134 I_OnUnloadModule, I_OnRehash, I_OnModuleRehash, I_OnWhois, I_OnPostConnect, I_OnEvent, I_OnHookUserIO };
135 ServerInstance->Modules->Attach(eventlist, this, 18);
137 starttls = new CommandStartTLS(ServerInstance, this);
138 ServerInstance->AddCommand(starttls);
141 virtual void OnRehash(User* user)
143 ConfigReader Conf(ServerInstance);
149 for(int index = 0; index < Conf.Enumerate("bind"); index++)
151 // For each <bind> tag
152 std::string x = Conf.ReadValue("bind", "type", index);
153 if(((x.empty()) || (x == "clients")) && (Conf.ReadValue("bind", "ssl", index) == "gnutls"))
155 // Get the port we're meant to be listening on with SSL
156 std::string port = Conf.ReadValue("bind", "port", index);
157 std::string addr = Conf.ReadValue("bind", "address", index);
161 // normalize address, important for IPv6
163 irc::sockets::sockaddrs bin;
164 if (irc::sockets::aptosa(addr.c_str(), portint, &bin))
165 irc::sockets::satoap(&bin, addr, portint);
168 irc::portparser portrange(port, false);
170 while ((portno = portrange.GetToken()))
175 listenports.push_back(addr + ":" + ConvToStr(portno));
177 for (size_t i = 0; i < ServerInstance->Config->ports.size(); i++)
178 if ((ServerInstance->Config->ports[i]->GetPort() == portno) && (ServerInstance->Config->ports[i]->GetIP() == addr))
179 ServerInstance->Config->ports[i]->SetDescription("ssl");
180 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %ld", portno);
182 sslports.append((addr.empty() ? "*" : addr)).append(":").append(ConvToStr(portno)).append(";");
184 catch (ModuleException &e)
186 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());
192 if (!sslports.empty())
193 sslports.erase(sslports.end() - 1);
196 virtual void OnModuleRehash(User* user, const std::string ¶m)
203 ConfigReader Conf(ServerInstance);
205 std::string confdir(ServerInstance->ConfigFileName);
206 // +1 so we the path ends with a /
207 confdir = confdir.substr(0, confdir.find_last_of('/') + 1);
209 cafile = Conf.ReadValue("gnutls", "cafile", 0);
210 crlfile = Conf.ReadValue("gnutls", "crlfile", 0);
211 certfile = Conf.ReadValue("gnutls", "certfile", 0);
212 keyfile = Conf.ReadValue("gnutls", "keyfile", 0);
213 dh_bits = Conf.ReadInteger("gnutls", "dhbits", 0, false);
215 // Set all the default values needed.
222 if (certfile.empty())
223 certfile = "cert.pem";
228 if((dh_bits != 768) && (dh_bits != 1024) && (dh_bits != 2048) && (dh_bits != 3072) && (dh_bits != 4096))
231 // Prepend relative paths with the path to the config directory.
232 if ((cafile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(cafile)))
233 cafile = confdir + cafile;
235 if ((crlfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(crlfile)))
236 crlfile = confdir + crlfile;
238 if ((certfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(certfile)))
239 certfile = confdir + certfile;
241 if ((keyfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(keyfile)))
242 keyfile = confdir + keyfile;
248 // Deallocate the old credentials
249 gnutls_dh_params_deinit(dh_params);
250 gnutls_certificate_free_credentials(x509_cred);
255 if((ret = gnutls_certificate_allocate_credentials(&x509_cred)) < 0)
256 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to allocate certificate credentials: %s", gnutls_strerror(ret));
258 if((ret = gnutls_dh_params_init(&dh_params)) < 0)
259 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to initialise DH parameters: %s", gnutls_strerror(ret));
261 if((ret =gnutls_certificate_set_x509_trust_file(x509_cred, cafile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
262 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));
264 if((ret = gnutls_certificate_set_x509_crl_file (x509_cred, crlfile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
265 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));
267 if((ret = gnutls_certificate_set_x509_key_file (x509_cred, certfile.c_str(), keyfile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
269 // If this fails, no SSL port will work. At all. So, do the smart thing - throw a ModuleException
270 throw ModuleException("Unable to load GnuTLS server certificate (" + certfile + ", key: " + keyfile + "): " + std::string(gnutls_strerror(ret)));
273 // This may be on a large (once a day or week) timer eventually.
277 void GenerateDHParams()
279 // Generate Diffie Hellman parameters - for use with DHE
280 // kx algorithms. These should be discarded and regenerated
281 // once a day, once a week or once a month. Depending on the
282 // security requirements.
286 if((ret = gnutls_dh_params_generate2(dh_params, dh_bits)) < 0)
287 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to generate DH parameters (%d bits): %s", dh_bits, gnutls_strerror(ret));
290 virtual ~ModuleSSLGnuTLS()
292 gnutls_dh_params_deinit(dh_params);
293 gnutls_certificate_free_credentials(x509_cred);
294 gnutls_global_deinit();
295 ServerInstance->Modules->UnpublishInterface("BufferedSocketHook", this);
299 virtual void OnCleanup(int target_type, void* item)
301 if(target_type == TYPE_USER)
303 User* user = (User*)item;
305 if (user->GetIOHook() == this)
307 // User is using SSL, they're a local user, and they're using one of *our* SSL ports.
308 // Potentially there could be multiple SSL modules loaded at once on different ports.
309 ServerInstance->Users->QuitUser(user, "SSL module unloading");
312 if (user->GetExt("ssl_cert"))
315 user->GetExt("ssl_cert", tofree);
317 user->Shrink("ssl_cert");
322 virtual void OnUnloadModule(Module* mod, const std::string &name)
326 for(unsigned int i = 0; i < listenports.size(); i++)
328 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
329 if (listenports[i] == (ServerInstance->Config->ports[j]->GetIP()+":"+ConvToStr(ServerInstance->Config->ports[j]->GetPort())))
330 ServerInstance->Config->ports[j]->SetDescription("plaintext");
335 virtual Version GetVersion()
337 return Version("$Id$", VF_VENDOR, API_VERSION);
341 virtual void On005Numeric(std::string &output)
343 if (!sslports.empty())
344 output.append(" SSL=" + sslports);
345 output.append(" STARTTLS");
348 virtual void OnHookUserIO(User* user, const std::string &targetip)
350 if (!user->GetIOHook() && isin(targetip,user->GetPort(),listenports))
352 /* Hook the user with our module */
353 user->AddIOHook(this);
357 virtual const char* OnRequest(Request* request)
359 ISHRequest* ISR = (ISHRequest*)request;
360 if (strcmp("IS_NAME", request->GetId()) == 0)
364 else if (strcmp("IS_HOOK", request->GetId()) == 0)
366 const char* ret = "OK";
369 ret = ISR->Sock->AddIOHook((Module*)this) ? "OK" : NULL;
371 catch (ModuleException &e)
377 else if (strcmp("IS_UNHOOK", request->GetId()) == 0)
379 return ISR->Sock->DelIOHook() ? "OK" : NULL;
381 else if (strcmp("IS_HSDONE", request->GetId()) == 0)
383 if (ISR->Sock->GetFd() < 0)
386 issl_session* session = &sessions[ISR->Sock->GetFd()];
387 return (session->status == ISSL_HANDSHAKING_READ || session->status == ISSL_HANDSHAKING_WRITE) ? NULL : "OK";
389 else if (strcmp("IS_ATTACH", request->GetId()) == 0)
391 if (ISR->Sock->GetFd() > -1)
393 issl_session* session = &sessions[ISR->Sock->GetFd()];
396 if ((Extensible*)ServerInstance->SE->GetRef(ISR->Sock->GetFd()) == (Extensible*)(ISR->Sock))
398 VerifyCertificate(session, (BufferedSocket*)ISR->Sock);
408 virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
410 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
411 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
414 issl_session* session = &sessions[fd];
416 /* For STARTTLS: Don't try and init a session on a socket that already has a session */
420 gnutls_init(&session->sess, GNUTLS_SERVER);
422 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
423 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
424 gnutls_dh_set_prime_bits(session->sess, dh_bits);
426 gnutls_transport_set_ptr(session->sess, (gnutls_transport_ptr_t) fd); // Give gnutls the fd for the socket.
428 gnutls_certificate_server_set_request(session->sess, GNUTLS_CERT_REQUEST); // Request client certificate if any.
430 Handshake(session, fd);
433 virtual void OnRawSocketConnect(int fd)
435 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
436 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
439 issl_session* session = &sessions[fd];
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.
448 Handshake(session, fd);
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")))
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, fd))
491 errno = session->status == ISSL_CLOSING ? EIO : EAGAIN;
492 // Couldn't resume handshake.
496 else if (session->status == ISSL_HANDSHAKING_WRITE)
503 // If we resumed the handshake then session->status will be ISSL_HANDSHAKEN.
505 if (session->status == ISSL_HANDSHAKEN)
507 int ret = gnutls_record_recv(session->sess, buffer, count);
515 // Client closed connection.
517 CloseSession(session);
520 else if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
527 ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT,
528 "m_ssl_gnutls.so: Error while reading on fd %d: %s",
529 fd, gnutls_strerror(ret));
531 CloseSession(session);
534 else if(session->status == ISSL_CLOSING)
540 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
542 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
543 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
546 issl_session* session = &sessions[fd];
547 const char* sendbuffer = buffer;
551 CloseSession(session);
555 session->outbuf.append(sendbuffer, count);
556 sendbuffer = session->outbuf.c_str();
557 count = session->outbuf.size();
559 if (session->status == ISSL_HANDSHAKING_WRITE || session->status == ISSL_HANDSHAKING_READ)
561 // The handshake isn't finished, try to finish it.
562 Handshake(session, fd);
563 errno = session->status == ISSL_CLOSING ? EIO : EAGAIN;
569 if (session->status == ISSL_HANDSHAKEN)
571 ret = gnutls_record_send(session->sess, sendbuffer, count);
575 CloseSession(session);
579 if(ret != GNUTLS_E_AGAIN && ret != GNUTLS_E_INTERRUPTED)
581 ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT,
582 "m_ssl_gnutls.so: Error while writing to fd %d: %s",
583 fd, gnutls_strerror(ret));
584 CloseSession(session);
593 session->outbuf = session->outbuf.substr(ret);
599 /* Who's smart idea was it to return 1 when we havent written anything?
600 * This fucks the buffer up in BufferedSocket :p
602 return ret < 1 ? 0 : ret;
605 // :kenny.chatspike.net 320 Om Epy|AFK :is a Secure Connection
606 virtual void OnWhois(User* source, User* dest)
611 // Bugfix, only send this numeric for *our* SSL users
612 if (dest->GetExt("ssl"))
614 ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
618 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
620 // check if the linking module wants to know about OUR metadata
623 // check if this user has an swhois field to send
624 if(user->GetExt(extname))
626 // call this function in the linking module, let it format the data how it
627 // sees fit, and send it on its way. We dont need or want to know how.
628 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, displayable ? "Enabled" : "ON");
633 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
635 // check if its our metadata key, and its associated with a user
636 if ((target_type == TYPE_USER) && (extname == "ssl"))
638 User* dest = (User*)target;
639 // if they dont already have an ssl flag, accept the remote server's
640 if (!dest->GetExt(extname))
642 dest->Extend(extname, "ON");
647 bool Handshake(issl_session* session, int fd)
649 int ret = gnutls_handshake(session->sess);
653 if(ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
655 // Handshake needs resuming later, read() or write() would have blocked.
657 if(gnutls_record_get_direction(session->sess) == 0)
659 // gnutls_handshake() wants to read() again.
660 session->status = ISSL_HANDSHAKING_READ;
664 // gnutls_handshake() wants to write() again.
665 session->status = ISSL_HANDSHAKING_WRITE;
672 ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT,
673 "m_ssl_gnutls.so: Handshake failed on fd %d: %s",
674 fd, gnutls_strerror(ret));
675 CloseSession(session);
676 session->status = ISSL_CLOSING;
683 // Handshake complete.
684 // This will do for setting the ssl flag...it could be done earlier if it's needed. But this seems neater.
685 EventHandler *extendme = ServerInstance->SE->GetRef(fd);
688 if (!extendme->GetExt("ssl"))
689 extendme->Extend("ssl", "ON");
692 // Change the seesion state
693 session->status = ISSL_HANDSHAKEN;
695 // Finish writing, if any left
702 virtual void OnPostConnect(User* user)
704 // This occurs AFTER OnUserConnect so we can be sure the
705 // protocol module has propagated the NICK message.
706 if (user->GetIOHook() == this && (IS_LOCAL(user)))
708 // Tell whatever protocol module we're using that we need to inform other servers of this metadata NOW.
709 ServerInstance->PI->SendMetaData(user, TYPE_USER, "ssl", "on");
711 VerifyCertificate(&sessions[user->GetFd()],user);
712 if (sessions[user->GetFd()].sess)
714 std::string cipher = gnutls_kx_get_name(gnutls_kx_get(sessions[user->GetFd()].sess));
715 cipher.append("-").append(gnutls_cipher_get_name(gnutls_cipher_get(sessions[user->GetFd()].sess))).append("-");
716 cipher.append(gnutls_mac_get_name(gnutls_mac_get(sessions[user->GetFd()].sess)));
717 user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\"", user->nick.c_str(), cipher.c_str());
722 void MakePollWrite(int fd)
724 //OnRawSocketWrite(fd, NULL, 0);
725 EventHandler* eh = ServerInstance->SE->GetRef(fd);
727 ServerInstance->SE->WantWrite(eh);
730 virtual void OnBufferFlushed(User* user)
732 if (user->GetIOHook() == this)
734 issl_session* session = &sessions[user->GetFd()];
735 if (session && session->outbuf.size())
736 OnRawSocketWrite(user->GetFd(), NULL, 0);
740 void CloseSession(issl_session* session)
744 gnutls_bye(session->sess, GNUTLS_SHUT_WR);
745 gnutls_deinit(session->sess);
748 session->outbuf.clear();
749 session->sess = NULL;
750 session->status = ISSL_NONE;
753 void VerifyCertificate(issl_session* session, Extensible* user)
755 if (!session->sess || !user)
759 const gnutls_datum_t* cert_list;
761 unsigned int cert_list_size;
762 gnutls_x509_crt_t cert;
764 unsigned char digest[MAXBUF];
765 size_t digest_size = sizeof(digest);
766 size_t name_size = sizeof(name);
767 ssl_cert* certinfo = new ssl_cert;
769 user->Extend("ssl_cert",certinfo);
771 /* This verification function uses the trusted CAs in the credentials
772 * structure. So you must have installed one or more CA certificates.
774 ret = gnutls_certificate_verify_peers2(session->sess, &status);
778 certinfo->data.insert(std::make_pair("error",std::string(gnutls_strerror(ret))));
782 if (status & GNUTLS_CERT_INVALID)
784 certinfo->data.insert(std::make_pair("invalid",ConvToStr(1)));
788 certinfo->data.insert(std::make_pair("invalid",ConvToStr(0)));
790 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
792 certinfo->data.insert(std::make_pair("unknownsigner",ConvToStr(1)));
796 certinfo->data.insert(std::make_pair("unknownsigner",ConvToStr(0)));
798 if (status & GNUTLS_CERT_REVOKED)
800 certinfo->data.insert(std::make_pair("revoked",ConvToStr(1)));
804 certinfo->data.insert(std::make_pair("revoked",ConvToStr(0)));
806 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
808 certinfo->data.insert(std::make_pair("trusted",ConvToStr(0)));
812 certinfo->data.insert(std::make_pair("trusted",ConvToStr(1)));
815 /* Up to here the process is the same for X.509 certificates and
816 * OpenPGP keys. From now on X.509 certificates are assumed. This can
817 * be easily extended to work with openpgp keys as well.
819 if (gnutls_certificate_type_get(session->sess) != GNUTLS_CRT_X509)
821 certinfo->data.insert(std::make_pair("error","No X509 keys sent"));
825 ret = gnutls_x509_crt_init(&cert);
828 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
833 cert_list = gnutls_certificate_get_peers(session->sess, &cert_list_size);
834 if (cert_list == NULL)
836 certinfo->data.insert(std::make_pair("error","No certificate was found"));
840 /* This is not a real world example, since we only check the first
841 * certificate in the given chain.
844 ret = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
847 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
851 gnutls_x509_crt_get_dn(cert, name, &name_size);
853 certinfo->data.insert(std::make_pair("dn",name));
855 gnutls_x509_crt_get_issuer_dn(cert, name, &name_size);
857 certinfo->data.insert(std::make_pair("issuer",name));
859 if ((ret = gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, digest, &digest_size)) < 0)
861 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
865 certinfo->data.insert(std::make_pair("fingerprint",irc::hex(digest, digest_size)));
868 /* Beware here we do not check for errors.
870 if ((gnutls_x509_crt_get_expiration_time(cert) < ServerInstance->Time()) || (gnutls_x509_crt_get_activation_time(cert) > ServerInstance->Time()))
872 certinfo->data.insert(std::make_pair("error","Not activated, or expired certificate"));
875 gnutls_x509_crt_deinit(cert);
880 void OnEvent(Event* ev)
882 GenericCapHandler(ev, "tls", "tls");
887 Module* server = ServerInstance->Modules->Find("m_spanningtree.so");
888 ServerInstance->Modules->SetPriority(this, I_OnPostConnect, PRIORITY_AFTER, &server);
892 MODULE_INIT(ModuleSSLGnuTLS)