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: exec("libgnutls-config --cflags") */
26 /* $LinkerFlags: rpath("libgnutls-config --libs") exec("libgnutls-config --libs") */
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
101 std::vector<std::string> listenports;
103 issl_session* sessions;
105 gnutls_certificate_credentials x509_cred;
106 gnutls_dh_params dh_params;
109 std::string certfile;
112 std::string sslports;
118 CommandStartTLS* starttls;
122 ModuleSSLGnuTLS(InspIRCd* Me)
125 ServerInstance->Modules->PublishInterface("BufferedSocketHook", this);
127 sessions = new issl_session[ServerInstance->SE->GetMaxFds()];
129 gnutls_global_init(); // This must be called once in the program
132 // Needs the flag as it ignores a plain /rehash
133 OnRehash(NULL,"ssl");
135 // Void return, guess we assume success
136 gnutls_certificate_set_dh_params(x509_cred, dh_params);
137 Implementation eventlist[] = { I_On005Numeric, I_OnRawSocketConnect, I_OnRawSocketAccept, I_OnRawSocketClose, I_OnRawSocketRead, I_OnRawSocketWrite, I_OnCleanup,
138 I_OnBufferFlushed, I_OnRequest, I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnUnloadModule, I_OnRehash, I_OnWhois, I_OnPostConnect, I_OnEvent, I_OnHookUserIO };
139 ServerInstance->Modules->Attach(eventlist, this, 17);
141 starttls = new CommandStartTLS(ServerInstance, this);
142 ServerInstance->AddCommand(starttls);
145 virtual void OnRehash(User* user, const std::string ¶m)
147 Conf = new ConfigReader(ServerInstance);
153 for(int index = 0; index < Conf->Enumerate("bind"); index++)
155 // For each <bind> tag
156 std::string x = Conf->ReadValue("bind", "type", index);
157 if(((x.empty()) || (x == "clients")) && (Conf->ReadValue("bind", "ssl", index) == "gnutls"))
159 // Get the port we're meant to be listening on with SSL
160 std::string port = Conf->ReadValue("bind", "port", index);
161 std::string addr = Conf->ReadValue("bind", "address", index);
165 // normalize address, important for IPv6
167 irc::sockets::sockaddrs bin;
168 if (irc::sockets::aptosa(addr.c_str(), portint, &bin))
169 irc::sockets::satoap(&bin, addr, portint);
172 irc::portparser portrange(port, false);
174 while ((portno = portrange.GetToken()))
179 listenports.push_back(addr + ":" + ConvToStr(portno));
181 for (size_t i = 0; i < ServerInstance->Config->ports.size(); i++)
182 if ((ServerInstance->Config->ports[i]->GetPort() == portno) && (ServerInstance->Config->ports[i]->GetIP() == addr))
183 ServerInstance->Config->ports[i]->SetDescription("ssl");
184 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %ld", portno);
186 sslports.append((addr.empty() ? "*" : addr)).append(":").append(ConvToStr(portno)).append(";");
188 catch (ModuleException &e)
190 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());
196 if (!sslports.empty())
197 sslports.erase(sslports.end() - 1);
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.
279 void GenerateDHParams()
281 // Generate Diffie Hellman parameters - for use with DHE
282 // kx algorithms. These should be discarded and regenerated
283 // once a day, once a week or once a month. Depending on the
284 // security requirements.
288 if((ret = gnutls_dh_params_generate2(dh_params, dh_bits)) < 0)
289 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to generate DH parameters (%d bits): %s", dh_bits, gnutls_strerror(ret));
292 virtual ~ModuleSSLGnuTLS()
294 gnutls_dh_params_deinit(dh_params);
295 gnutls_certificate_free_credentials(x509_cred);
296 gnutls_global_deinit();
297 ServerInstance->Modules->UnpublishInterface("BufferedSocketHook", this);
301 virtual void OnCleanup(int target_type, void* item)
303 if(target_type == TYPE_USER)
305 User* user = (User*)item;
307 if (user->GetIOHook() == this)
309 // User is using SSL, they're a local user, and they're using one of *our* SSL ports.
310 // Potentially there could be multiple SSL modules loaded at once on different ports.
311 ServerInstance->Users->QuitUser(user, "SSL module unloading");
314 if (user->GetExt("ssl_cert", dummy))
317 user->GetExt("ssl_cert", tofree);
319 user->Shrink("ssl_cert");
324 virtual void OnUnloadModule(Module* mod, const std::string &name)
328 for(unsigned int i = 0; i < listenports.size(); i++)
330 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
331 if (listenports[i] == (ServerInstance->Config->ports[j]->GetIP()+":"+ConvToStr(ServerInstance->Config->ports[j]->GetPort())))
332 ServerInstance->Config->ports[j]->SetDescription("plaintext");
337 virtual Version GetVersion()
339 return Version("$Id$", VF_VENDOR, API_VERSION);
343 virtual void On005Numeric(std::string &output)
345 if (!sslports.empty())
346 output.append(" SSL=" + sslports);
347 output.append(" STARTTLS");
350 virtual void OnHookUserIO(User* user, const std::string &targetip)
352 if (!user->GetIOHook() && isin(targetip,user->GetPort(),listenports))
354 /* Hook the user with our module */
355 user->AddIOHook(this);
359 virtual const char* OnRequest(Request* request)
361 ISHRequest* ISR = (ISHRequest*)request;
362 if (strcmp("IS_NAME", request->GetId()) == 0)
366 else if (strcmp("IS_HOOK", request->GetId()) == 0)
368 const char* ret = "OK";
371 ret = ISR->Sock->AddIOHook((Module*)this) ? "OK" : NULL;
373 catch (ModuleException &e)
379 else if (strcmp("IS_UNHOOK", request->GetId()) == 0)
381 return ISR->Sock->DelIOHook() ? "OK" : NULL;
383 else if (strcmp("IS_HSDONE", request->GetId()) == 0)
385 if (ISR->Sock->GetFd() < 0)
388 issl_session* session = &sessions[ISR->Sock->GetFd()];
389 return (session->status == ISSL_HANDSHAKING_READ || session->status == ISSL_HANDSHAKING_WRITE) ? NULL : "OK";
391 else if (strcmp("IS_ATTACH", request->GetId()) == 0)
393 if (ISR->Sock->GetFd() > -1)
395 issl_session* session = &sessions[ISR->Sock->GetFd()];
398 if ((Extensible*)ServerInstance->SE->GetRef(ISR->Sock->GetFd()) == (Extensible*)(ISR->Sock))
400 VerifyCertificate(session, (BufferedSocket*)ISR->Sock);
410 virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
412 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
413 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
416 issl_session* session = &sessions[fd];
418 /* For STARTTLS: Don't try and init a session on a socket that already has a session */
422 gnutls_init(&session->sess, GNUTLS_SERVER);
424 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
425 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
426 gnutls_dh_set_prime_bits(session->sess, dh_bits);
428 gnutls_transport_set_ptr(session->sess, (gnutls_transport_ptr_t) fd); // Give gnutls the fd for the socket.
430 gnutls_certificate_server_set_request(session->sess, GNUTLS_CERT_REQUEST); // Request client certificate if any.
432 Handshake(session, fd);
435 virtual void OnRawSocketConnect(int fd)
437 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
438 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
441 issl_session* session = &sessions[fd];
443 gnutls_init(&session->sess, GNUTLS_CLIENT);
445 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
446 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
447 gnutls_dh_set_prime_bits(session->sess, dh_bits);
448 gnutls_transport_set_ptr(session->sess, (gnutls_transport_ptr_t) fd); // Give gnutls the fd for the socket.
450 Handshake(session, fd);
453 virtual void OnRawSocketClose(int fd)
455 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
456 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds()))
459 CloseSession(&sessions[fd]);
461 EventHandler* user = ServerInstance->SE->GetRef(fd);
463 if ((user) && (user->GetExt("ssl_cert", dummy)))
466 user->GetExt("ssl_cert", tofree);
468 user->Shrink("ssl_cert");
472 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
474 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
475 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
478 issl_session* session = &sessions[fd];
483 CloseSession(session);
487 if (session->status == ISSL_HANDSHAKING_READ)
489 // The handshake isn't finished, try to finish it.
491 if(!Handshake(session, fd))
493 errno = session->status == ISSL_CLOSING ? EIO : EAGAIN;
494 // Couldn't resume handshake.
498 else if (session->status == ISSL_HANDSHAKING_WRITE)
505 // If we resumed the handshake then session->status will be ISSL_HANDSHAKEN.
507 if (session->status == ISSL_HANDSHAKEN)
509 int ret = gnutls_record_recv(session->sess, buffer, count);
517 // Client closed connection.
519 CloseSession(session);
522 else if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
529 ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT,
530 "m_ssl_gnutls.so: Error while reading on fd %d: %s",
531 fd, gnutls_strerror(ret));
533 CloseSession(session);
536 else if(session->status == ISSL_CLOSING)
542 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
544 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
545 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
548 issl_session* session = &sessions[fd];
549 const char* sendbuffer = buffer;
553 CloseSession(session);
557 session->outbuf.append(sendbuffer, count);
558 sendbuffer = session->outbuf.c_str();
559 count = session->outbuf.size();
561 if (session->status == ISSL_HANDSHAKING_WRITE || session->status == ISSL_HANDSHAKING_READ)
563 // The handshake isn't finished, try to finish it.
564 Handshake(session, fd);
565 errno = session->status == ISSL_CLOSING ? EIO : EAGAIN;
571 if (session->status == ISSL_HANDSHAKEN)
573 ret = gnutls_record_send(session->sess, sendbuffer, count);
577 CloseSession(session);
581 if(ret != GNUTLS_E_AGAIN && ret != GNUTLS_E_INTERRUPTED)
583 ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT,
584 "m_ssl_gnutls.so: Error while writing to fd %d: %s",
585 fd, gnutls_strerror(ret));
586 CloseSession(session);
595 session->outbuf = session->outbuf.substr(ret);
601 /* Who's smart idea was it to return 1 when we havent written anything?
602 * This fucks the buffer up in BufferedSocket :p
604 return ret < 1 ? 0 : ret;
607 // :kenny.chatspike.net 320 Om Epy|AFK :is a Secure Connection
608 virtual void OnWhois(User* source, User* dest)
613 // Bugfix, only send this numeric for *our* SSL users
614 if (dest->GetExt("ssl", dummy))
616 ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
620 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
622 // check if the linking module wants to know about OUR metadata
625 // check if this user has an swhois field to send
626 if(user->GetExt(extname, dummy))
628 // call this function in the linking module, let it format the data how it
629 // sees fit, and send it on its way. We dont need or want to know how.
630 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, displayable ? "Enabled" : "ON");
635 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
637 // check if its our metadata key, and its associated with a user
638 if ((target_type == TYPE_USER) && (extname == "ssl"))
640 User* dest = (User*)target;
641 // if they dont already have an ssl flag, accept the remote server's
642 if (!dest->GetExt(extname, dummy))
644 dest->Extend(extname, "ON");
649 bool Handshake(issl_session* session, int fd)
651 int ret = gnutls_handshake(session->sess);
655 if(ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
657 // Handshake needs resuming later, read() or write() would have blocked.
659 if(gnutls_record_get_direction(session->sess) == 0)
661 // gnutls_handshake() wants to read() again.
662 session->status = ISSL_HANDSHAKING_READ;
666 // gnutls_handshake() wants to write() again.
667 session->status = ISSL_HANDSHAKING_WRITE;
674 ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT,
675 "m_ssl_gnutls.so: Handshake failed on fd %d: %s",
676 fd, gnutls_strerror(ret));
677 CloseSession(session);
678 session->status = ISSL_CLOSING;
685 // Handshake complete.
686 // This will do for setting the ssl flag...it could be done earlier if it's needed. But this seems neater.
687 EventHandler *extendme = ServerInstance->SE->GetRef(fd);
690 if (!extendme->GetExt("ssl", dummy))
691 extendme->Extend("ssl", "ON");
694 // Change the seesion state
695 session->status = ISSL_HANDSHAKEN;
697 // Finish writing, if any left
704 virtual void OnPostConnect(User* user)
706 // This occurs AFTER OnUserConnect so we can be sure the
707 // protocol module has propagated the NICK message.
708 if (user->GetIOHook() == this && (IS_LOCAL(user)))
710 // Tell whatever protocol module we're using that we need to inform other servers of this metadata NOW.
711 ServerInstance->PI->SendMetaData(user, TYPE_USER, "ssl", "on");
713 VerifyCertificate(&sessions[user->GetFd()],user);
714 if (sessions[user->GetFd()].sess)
716 std::string cipher = gnutls_kx_get_name(gnutls_kx_get(sessions[user->GetFd()].sess));
717 cipher.append("-").append(gnutls_cipher_get_name(gnutls_cipher_get(sessions[user->GetFd()].sess))).append("-");
718 cipher.append(gnutls_mac_get_name(gnutls_mac_get(sessions[user->GetFd()].sess)));
719 user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\"", user->nick.c_str(), cipher.c_str());
724 void MakePollWrite(int fd)
726 //OnRawSocketWrite(fd, NULL, 0);
727 EventHandler* eh = ServerInstance->SE->GetRef(fd);
729 ServerInstance->SE->WantWrite(eh);
732 virtual void OnBufferFlushed(User* user)
734 if (user->GetIOHook() == this)
736 issl_session* session = &sessions[user->GetFd()];
737 if (session && session->outbuf.size())
738 OnRawSocketWrite(user->GetFd(), NULL, 0);
742 void CloseSession(issl_session* session)
746 gnutls_bye(session->sess, GNUTLS_SHUT_WR);
747 gnutls_deinit(session->sess);
750 session->outbuf.clear();
751 session->sess = NULL;
752 session->status = ISSL_NONE;
755 void VerifyCertificate(issl_session* session, Extensible* user)
757 if (!session->sess || !user)
761 const gnutls_datum_t* cert_list;
763 unsigned int cert_list_size;
764 gnutls_x509_crt_t cert;
766 unsigned char digest[MAXBUF];
767 size_t digest_size = sizeof(digest);
768 size_t name_size = sizeof(name);
769 ssl_cert* certinfo = new ssl_cert;
771 user->Extend("ssl_cert",certinfo);
773 /* This verification function uses the trusted CAs in the credentials
774 * structure. So you must have installed one or more CA certificates.
776 ret = gnutls_certificate_verify_peers2(session->sess, &status);
780 certinfo->data.insert(std::make_pair("error",std::string(gnutls_strerror(ret))));
784 if (status & GNUTLS_CERT_INVALID)
786 certinfo->data.insert(std::make_pair("invalid",ConvToStr(1)));
790 certinfo->data.insert(std::make_pair("invalid",ConvToStr(0)));
792 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
794 certinfo->data.insert(std::make_pair("unknownsigner",ConvToStr(1)));
798 certinfo->data.insert(std::make_pair("unknownsigner",ConvToStr(0)));
800 if (status & GNUTLS_CERT_REVOKED)
802 certinfo->data.insert(std::make_pair("revoked",ConvToStr(1)));
806 certinfo->data.insert(std::make_pair("revoked",ConvToStr(0)));
808 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
810 certinfo->data.insert(std::make_pair("trusted",ConvToStr(0)));
814 certinfo->data.insert(std::make_pair("trusted",ConvToStr(1)));
817 /* Up to here the process is the same for X.509 certificates and
818 * OpenPGP keys. From now on X.509 certificates are assumed. This can
819 * be easily extended to work with openpgp keys as well.
821 if (gnutls_certificate_type_get(session->sess) != GNUTLS_CRT_X509)
823 certinfo->data.insert(std::make_pair("error","No X509 keys sent"));
827 ret = gnutls_x509_crt_init(&cert);
830 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
835 cert_list = gnutls_certificate_get_peers(session->sess, &cert_list_size);
836 if (cert_list == NULL)
838 certinfo->data.insert(std::make_pair("error","No certificate was found"));
842 /* This is not a real world example, since we only check the first
843 * certificate in the given chain.
846 ret = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
849 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
853 gnutls_x509_crt_get_dn(cert, name, &name_size);
855 certinfo->data.insert(std::make_pair("dn",name));
857 gnutls_x509_crt_get_issuer_dn(cert, name, &name_size);
859 certinfo->data.insert(std::make_pair("issuer",name));
861 if ((ret = gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, digest, &digest_size)) < 0)
863 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
867 certinfo->data.insert(std::make_pair("fingerprint",irc::hex(digest, digest_size)));
870 /* Beware here we do not check for errors.
872 if ((gnutls_x509_crt_get_expiration_time(cert) < ServerInstance->Time()) || (gnutls_x509_crt_get_activation_time(cert) > ServerInstance->Time()))
874 certinfo->data.insert(std::make_pair("error","Not activated, or expired certificate"));
877 gnutls_x509_crt_deinit(cert);
882 void OnEvent(Event* ev)
884 GenericCapHandler(ev, "tls", "tls");
889 Module* server = ServerInstance->Modules->Find("m_spanningtree.so");
890 ServerInstance->Modules->SetPriority(this, I_OnPostConnect, PRIORITY_AFTER, &server);
894 MODULE_INIT(ModuleSSLGnuTLS)