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");
86 user->WriteNumeric(670, "%s :STARTTLS successful, go ahead with TLS handshake", user->nick.c_str());
88 Caller->OnRawSocketAccept(user->GetFd(), user->GetIPString(), user->GetPort());
91 user->WriteNumeric(671, "%s :STARTTLS failure", user->nick.c_str());
98 class ModuleSSLGnuTLS : public Module
105 std::vector<std::string> listenports;
108 issl_session* sessions;
110 gnutls_certificate_credentials x509_cred;
111 gnutls_dh_params dh_params;
114 std::string certfile;
117 std::string sslports;
122 CommandStartTLS* starttls;
126 ModuleSSLGnuTLS(InspIRCd* Me)
129 ServerInstance->Modules->PublishInterface("BufferedSocketHook", this);
131 sessions = new issl_session[ServerInstance->SE->GetMaxFds()];
133 // Not rehashable...because I cba to reduce all the sizes of existing buffers.
134 inbufsize = ServerInstance->Config->NetBufferSize;
136 gnutls_global_init(); // This must be called once in the program
138 if(gnutls_certificate_allocate_credentials(&x509_cred) != 0)
139 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to allocate certificate credentials");
141 // Guessing return meaning
142 if(gnutls_dh_params_init(&dh_params) < 0)
143 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to initialise DH parameters");
145 // Needs the flag as it ignores a plain /rehash
146 OnRehash(NULL,"ssl");
148 // Void return, guess we assume success
149 gnutls_certificate_set_dh_params(x509_cred, dh_params);
150 Implementation eventlist[] = { I_On005Numeric, I_OnRawSocketConnect, I_OnRawSocketAccept, I_OnRawSocketClose, I_OnRawSocketRead, I_OnRawSocketWrite, I_OnCleanup,
151 I_OnBufferFlushed, I_OnRequest, I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnUnloadModule, I_OnRehash, I_OnWhois, I_OnPostConnect, I_OnEvent, I_OnHookUserIO };
152 ServerInstance->Modules->Attach(eventlist, this, 17);
154 starttls = new CommandStartTLS(ServerInstance, this);
155 ServerInstance->AddCommand(starttls);
158 virtual void OnRehash(User* user, const std::string ¶m)
160 Conf = new ConfigReader(ServerInstance);
166 for(int index = 0; index < Conf->Enumerate("bind"); index++)
168 // For each <bind> tag
169 std::string x = Conf->ReadValue("bind", "type", index);
170 if(((x.empty()) || (x == "clients")) && (Conf->ReadValue("bind", "ssl", index) == "gnutls"))
172 // Get the port we're meant to be listening on with SSL
173 std::string port = Conf->ReadValue("bind", "port", index);
174 std::string addr = Conf->ReadValue("bind", "address", index);
176 irc::portparser portrange(port, false);
178 while ((portno = portrange.GetToken()))
183 listenports.push_back(addr + ":" + ConvToStr(portno));
185 for (size_t i = 0; i < ServerInstance->Config->ports.size(); i++)
186 if ((ServerInstance->Config->ports[i]->GetPort() == portno) && (ServerInstance->Config->ports[i]->GetIP() == addr))
187 ServerInstance->Config->ports[i]->SetDescription("ssl");
188 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %ld", portno);
190 sslports.append((addr.empty() ? "*" : addr)).append(":").append(ConvToStr(portno)).append(";");
192 catch (ModuleException &e)
194 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());
200 if (!sslports.empty())
201 sslports.erase(sslports.end() - 1);
209 std::string confdir(ServerInstance->ConfigFileName);
210 // +1 so we the path ends with a /
211 confdir = confdir.substr(0, confdir.find_last_of('/') + 1);
213 cafile = Conf->ReadValue("gnutls", "cafile", 0);
214 crlfile = Conf->ReadValue("gnutls", "crlfile", 0);
215 certfile = Conf->ReadValue("gnutls", "certfile", 0);
216 keyfile = Conf->ReadValue("gnutls", "keyfile", 0);
217 dh_bits = Conf->ReadInteger("gnutls", "dhbits", 0, false);
219 // Set all the default values needed.
226 if (certfile.empty())
227 certfile = "cert.pem";
232 if((dh_bits != 768) && (dh_bits != 1024) && (dh_bits != 2048) && (dh_bits != 3072) && (dh_bits != 4096))
235 // Prepend relative paths with the path to the config directory.
236 if ((cafile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(cafile)))
237 cafile = confdir + cafile;
239 if ((crlfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(crlfile)))
240 crlfile = confdir + crlfile;
242 if ((certfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(certfile)))
243 certfile = confdir + certfile;
245 if ((keyfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(keyfile)))
246 keyfile = confdir + keyfile;
250 if((ret =gnutls_certificate_set_x509_trust_file(x509_cred, cafile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
251 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));
253 if((ret = gnutls_certificate_set_x509_crl_file (x509_cred, crlfile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
254 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));
256 if((ret = gnutls_certificate_set_x509_key_file (x509_cred, certfile.c_str(), keyfile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
258 // If this fails, no SSL port will work. At all. So, do the smart thing - throw a ModuleException
259 throw ModuleException("Unable to load GnuTLS server certificate: " + std::string(gnutls_strerror(ret)));
262 // This may be on a large (once a day or week) timer eventually.
268 void GenerateDHParams()
270 // Generate Diffie Hellman parameters - for use with DHE
271 // kx algorithms. These should be discarded and regenerated
272 // once a day, once a week or once a month. Depending on the
273 // security requirements.
277 if((ret = gnutls_dh_params_generate2(dh_params, dh_bits)) < 0)
278 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to generate DH parameters (%d bits): %s", dh_bits, gnutls_strerror(ret));
281 virtual ~ModuleSSLGnuTLS()
283 gnutls_dh_params_deinit(dh_params);
284 gnutls_certificate_free_credentials(x509_cred);
285 gnutls_global_deinit();
286 ServerInstance->Modules->UnpublishInterface("BufferedSocketHook", this);
290 virtual void OnCleanup(int target_type, void* item)
292 if(target_type == TYPE_USER)
294 User* user = (User*)item;
296 if (user->io == this)
298 // User is using SSL, they're a local user, and they're using one of *our* SSL ports.
299 // Potentially there could be multiple SSL modules loaded at once on different ports.
300 ServerInstance->Users->QuitUser(user, "SSL module unloading");
303 if (user->GetExt("ssl_cert", dummy))
306 user->GetExt("ssl_cert", tofree);
308 user->Shrink("ssl_cert");
313 virtual void OnUnloadModule(Module* mod, const std::string &name)
317 for(unsigned int i = 0; i < listenports.size(); i++)
319 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
320 if (listenports[i] == (ServerInstance->Config->ports[j]->GetIP()+":"+ConvToStr(ServerInstance->Config->ports[j]->GetPort())))
321 ServerInstance->Config->ports[j]->SetDescription("plaintext");
326 virtual Version GetVersion()
328 return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
332 virtual void On005Numeric(std::string &output)
334 output.append(" SSL=" + sslports);
337 virtual void OnHookUserIO(User* user, const std::string &targetip)
339 if (!user->io && isin(targetip,user->GetPort(),listenports))
341 /* Hook the user with our module */
346 virtual const char* OnRequest(Request* request)
348 ISHRequest* ISR = (ISHRequest*)request;
349 if (strcmp("IS_NAME", request->GetId()) == 0)
353 else if (strcmp("IS_HOOK", request->GetId()) == 0)
355 const char* ret = "OK";
358 ret = ServerInstance->Config->AddIOHook((Module*)this, (BufferedSocket*)ISR->Sock) ? "OK" : NULL;
360 catch (ModuleException &e)
366 else if (strcmp("IS_UNHOOK", request->GetId()) == 0)
368 return ServerInstance->Config->DelIOHook((BufferedSocket*)ISR->Sock) ? "OK" : NULL;
370 else if (strcmp("IS_HSDONE", request->GetId()) == 0)
372 if (ISR->Sock->GetFd() < 0)
375 issl_session* session = &sessions[ISR->Sock->GetFd()];
376 return (session->status == ISSL_HANDSHAKING_READ || session->status == ISSL_HANDSHAKING_WRITE) ? NULL : "OK";
378 else if (strcmp("IS_ATTACH", request->GetId()) == 0)
380 if (ISR->Sock->GetFd() > -1)
382 issl_session* session = &sessions[ISR->Sock->GetFd()];
385 if ((Extensible*)ServerInstance->FindDescriptor(ISR->Sock->GetFd()) == (Extensible*)(ISR->Sock))
387 VerifyCertificate(session, (BufferedSocket*)ISR->Sock);
397 virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
399 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
400 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
403 issl_session* session = &sessions[fd];
405 /* For STARTTLS: Don't try and init a session on a socket that already has a session */
410 session->inbuf = new char[inbufsize];
411 session->inbufoffset = 0;
413 gnutls_init(&session->sess, GNUTLS_SERVER);
415 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
416 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
417 gnutls_dh_set_prime_bits(session->sess, dh_bits);
419 /* This is an experimental change to avoid a warning on 64bit systems about casting between integer and pointer of different sizes
420 * This needs testing, but it's easy enough to rollback if need be
421 * Old: gnutls_transport_set_ptr(session->sess, (gnutls_transport_ptr_t) fd); // Give gnutls the fd for the socket.
422 * New: gnutls_transport_set_ptr(session->sess, &fd); // Give gnutls the fd for the socket.
424 * With testing this seems to...not work :/
427 gnutls_transport_set_ptr(session->sess, (gnutls_transport_ptr_t) fd); // Give gnutls the fd for the socket.
429 gnutls_certificate_server_set_request(session->sess, GNUTLS_CERT_REQUEST); // Request client certificate if any.
434 virtual void OnRawSocketConnect(int fd)
436 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
437 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
440 issl_session* session = &sessions[fd];
443 session->inbuf = new char[inbufsize];
444 session->inbufoffset = 0;
446 gnutls_init(&session->sess, GNUTLS_CLIENT);
448 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
449 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
450 gnutls_dh_set_prime_bits(session->sess, dh_bits);
451 gnutls_transport_set_ptr(session->sess, (gnutls_transport_ptr_t) fd); // Give gnutls the fd for the socket.
456 virtual void OnRawSocketClose(int fd)
458 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
459 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds()))
462 CloseSession(&sessions[fd]);
464 EventHandler* user = ServerInstance->SE->GetRef(fd);
466 if ((user) && (user->GetExt("ssl_cert", dummy)))
469 user->GetExt("ssl_cert", tofree);
471 user->Shrink("ssl_cert");
475 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
477 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
478 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
481 issl_session* session = &sessions[fd];
486 CloseSession(session);
490 if (session->status == ISSL_HANDSHAKING_READ)
492 // The handshake isn't finished, try to finish it.
494 if(!Handshake(session))
496 // Couldn't resume handshake.
500 else if (session->status == ISSL_HANDSHAKING_WRITE)
503 MakePollWrite(session);
507 // If we resumed the handshake then session->status will be ISSL_HANDSHAKEN.
509 if (session->status == ISSL_HANDSHAKEN)
511 // Is this right? Not sure if the unencrypted data is garaunteed to be the same length.
512 // Read into the inbuffer, offset from the beginning by the amount of data we have that insp hasn't taken yet.
513 int ret = gnutls_record_recv(session->sess, session->inbuf + session->inbufoffset, inbufsize - session->inbufoffset);
517 // Client closed connection.
519 CloseSession(session);
524 if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
532 CloseSession(session);
537 // Read successfully 'ret' bytes into inbuf + inbufoffset
538 // There are 'ret' + 'inbufoffset' bytes of data in 'inbuf'
539 // 'buffer' is 'count' long
541 unsigned int length = ret + session->inbufoffset;
545 memcpy(buffer, session->inbuf, count);
546 // Move the stuff left in inbuf to the beginning of it
547 memmove(session->inbuf, session->inbuf + count, (length - count));
548 // Now we need to set session->inbufoffset to the amount of data still waiting to be handed to insp.
549 session->inbufoffset = length - count;
550 // Insp uses readresult as the count of how much data there is in buffer, so:
555 // There's not as much in the inbuf as there is space in the buffer, so just copy the whole thing.
556 memcpy(buffer, session->inbuf, length);
557 // Zero the offset, as there's nothing there..
558 session->inbufoffset = 0;
564 else if(session->status == ISSL_CLOSING)
570 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
572 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
573 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
576 issl_session* session = &sessions[fd];
577 const char* sendbuffer = buffer;
581 CloseSession(session);
585 session->outbuf.append(sendbuffer, count);
586 sendbuffer = session->outbuf.c_str();
587 count = session->outbuf.size();
589 if (session->status == ISSL_HANDSHAKING_WRITE)
591 // The handshake isn't finished, try to finish it.
599 if (session->status == ISSL_HANDSHAKEN)
601 ret = gnutls_record_send(session->sess, sendbuffer, count);
605 CloseSession(session);
609 if(ret != GNUTLS_E_AGAIN && ret != GNUTLS_E_INTERRUPTED)
611 CloseSession(session);
620 session->outbuf = session->outbuf.substr(ret);
624 MakePollWrite(session);
626 /* Who's smart idea was it to return 1 when we havent written anything?
627 * This fucks the buffer up in BufferedSocket :p
629 return ret < 1 ? 0 : ret;
632 // :kenny.chatspike.net 320 Om Epy|AFK :is a Secure Connection
633 virtual void OnWhois(User* source, User* dest)
638 // Bugfix, only send this numeric for *our* SSL users
639 if (dest->GetExt("ssl", dummy) || ((IS_LOCAL(dest) && (dest->io == this))))
641 ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
645 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
647 // check if the linking module wants to know about OUR metadata
650 // check if this user has an swhois field to send
651 if(user->GetExt(extname, dummy))
653 // call this function in the linking module, let it format the data how it
654 // sees fit, and send it on its way. We dont need or want to know how.
655 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, displayable ? "Enabled" : "ON");
660 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
662 // check if its our metadata key, and its associated with a user
663 if ((target_type == TYPE_USER) && (extname == "ssl"))
665 User* dest = (User*)target;
666 // if they dont already have an ssl flag, accept the remote server's
667 if (!dest->GetExt(extname, dummy))
669 dest->Extend(extname, "ON");
674 bool Handshake(issl_session* session)
676 int ret = gnutls_handshake(session->sess);
680 if(ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
682 // Handshake needs resuming later, read() or write() would have blocked.
684 if(gnutls_record_get_direction(session->sess) == 0)
686 // gnutls_handshake() wants to read() again.
687 session->status = ISSL_HANDSHAKING_READ;
691 // gnutls_handshake() wants to write() again.
692 session->status = ISSL_HANDSHAKING_WRITE;
693 MakePollWrite(session);
699 CloseSession(session);
700 session->status = ISSL_CLOSING;
707 // Handshake complete.
708 // This will do for setting the ssl flag...it could be done earlier if it's needed. But this seems neater.
709 User* extendme = ServerInstance->FindDescriptor(session->fd);
712 if (!extendme->GetExt("ssl", dummy))
713 extendme->Extend("ssl", "ON");
716 // Change the seesion state
717 session->status = ISSL_HANDSHAKEN;
719 // Finish writing, if any left
720 MakePollWrite(session);
726 virtual void OnPostConnect(User* user)
728 // This occurs AFTER OnUserConnect so we can be sure the
729 // protocol module has propagated the NICK message.
730 if ((user->GetExt("ssl", dummy)) && (IS_LOCAL(user)))
732 // Tell whatever protocol module we're using that we need to inform other servers of this metadata NOW.
733 ServerInstance->PI->SendMetaData(user, TYPE_USER, "SSL", "on");
735 VerifyCertificate(&sessions[user->GetFd()],user);
736 if (sessions[user->GetFd()].sess)
738 std::string cipher = gnutls_kx_get_name(gnutls_kx_get(sessions[user->GetFd()].sess));
739 cipher.append("-").append(gnutls_cipher_get_name(gnutls_cipher_get(sessions[user->GetFd()].sess))).append("-");
740 cipher.append(gnutls_mac_get_name(gnutls_mac_get(sessions[user->GetFd()].sess)));
741 user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\"", user->nick.c_str(), cipher.c_str());
746 void MakePollWrite(issl_session* session)
748 //OnRawSocketWrite(session->fd, NULL, 0);
749 EventHandler* eh = ServerInstance->FindDescriptor(session->fd);
751 ServerInstance->SE->WantWrite(eh);
754 virtual void OnBufferFlushed(User* user)
756 if (user->GetExt("ssl"))
758 issl_session* session = &sessions[user->GetFd()];
759 if (session && session->outbuf.size())
760 OnRawSocketWrite(user->GetFd(), NULL, 0);
764 void CloseSession(issl_session* session)
768 gnutls_bye(session->sess, GNUTLS_SHUT_WR);
769 gnutls_deinit(session->sess);
774 delete[] session->inbuf;
777 session->outbuf.clear();
778 session->inbuf = NULL;
779 session->sess = NULL;
780 session->status = ISSL_NONE;
783 void VerifyCertificate(issl_session* session, Extensible* user)
785 if (!session->sess || !user)
789 const gnutls_datum_t* cert_list;
791 unsigned int cert_list_size;
792 gnutls_x509_crt_t cert;
794 unsigned char digest[MAXBUF];
795 size_t digest_size = sizeof(digest);
796 size_t name_size = sizeof(name);
797 ssl_cert* certinfo = new ssl_cert;
799 user->Extend("ssl_cert",certinfo);
801 /* This verification function uses the trusted CAs in the credentials
802 * structure. So you must have installed one or more CA certificates.
804 ret = gnutls_certificate_verify_peers2(session->sess, &status);
808 certinfo->data.insert(std::make_pair("error",std::string(gnutls_strerror(ret))));
812 if (status & GNUTLS_CERT_INVALID)
814 certinfo->data.insert(std::make_pair("invalid",ConvToStr(1)));
818 certinfo->data.insert(std::make_pair("invalid",ConvToStr(0)));
820 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
822 certinfo->data.insert(std::make_pair("unknownsigner",ConvToStr(1)));
826 certinfo->data.insert(std::make_pair("unknownsigner",ConvToStr(0)));
828 if (status & GNUTLS_CERT_REVOKED)
830 certinfo->data.insert(std::make_pair("revoked",ConvToStr(1)));
834 certinfo->data.insert(std::make_pair("revoked",ConvToStr(0)));
836 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
838 certinfo->data.insert(std::make_pair("trusted",ConvToStr(0)));
842 certinfo->data.insert(std::make_pair("trusted",ConvToStr(1)));
845 /* Up to here the process is the same for X.509 certificates and
846 * OpenPGP keys. From now on X.509 certificates are assumed. This can
847 * be easily extended to work with openpgp keys as well.
849 if (gnutls_certificate_type_get(session->sess) != GNUTLS_CRT_X509)
851 certinfo->data.insert(std::make_pair("error","No X509 keys sent"));
855 ret = gnutls_x509_crt_init(&cert);
858 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
863 cert_list = gnutls_certificate_get_peers(session->sess, &cert_list_size);
864 if (cert_list == NULL)
866 certinfo->data.insert(std::make_pair("error","No certificate was found"));
870 /* This is not a real world example, since we only check the first
871 * certificate in the given chain.
874 ret = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
877 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
881 gnutls_x509_crt_get_dn(cert, name, &name_size);
883 certinfo->data.insert(std::make_pair("dn",name));
885 gnutls_x509_crt_get_issuer_dn(cert, name, &name_size);
887 certinfo->data.insert(std::make_pair("issuer",name));
889 if ((ret = gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, digest, &digest_size)) < 0)
891 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
895 certinfo->data.insert(std::make_pair("fingerprint",irc::hex(digest, digest_size)));
898 /* Beware here we do not check for errors.
900 if ((gnutls_x509_crt_get_expiration_time(cert) < time(0)) || (gnutls_x509_crt_get_activation_time(cert) > time(0)))
902 certinfo->data.insert(std::make_pair("error","Not activated, or expired certificate"));
905 gnutls_x509_crt_deinit(cert);
910 void OnEvent(Event* ev)
912 GenericCapHandler(ev, "tls", "tls");
917 Module* server = ServerInstance->Modules->Find("m_spanningtree.so");
918 ServerInstance->Modules->SetPriority(this, I_OnPostConnect, PRIO_AFTER, &server);
922 MODULE_INIT(ModuleSSLGnuTLS)