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 if (addr != "127.0.0.1")
183 sslports.append((addr.empty() ? "*" : addr)).append(":").append(ConvToStr(portno)).append(";");
185 catch (ModuleException &e)
187 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());
193 if (!sslports.empty())
194 sslports.erase(sslports.end() - 1);
197 virtual void OnModuleRehash(User* user, const std::string ¶m)
204 ConfigReader Conf(ServerInstance);
206 std::string confdir(ServerInstance->ConfigFileName);
207 // +1 so we the path ends with a /
208 confdir = confdir.substr(0, confdir.find_last_of('/') + 1);
210 cafile = Conf.ReadValue("gnutls", "cafile", 0);
211 crlfile = Conf.ReadValue("gnutls", "crlfile", 0);
212 certfile = Conf.ReadValue("gnutls", "certfile", 0);
213 keyfile = Conf.ReadValue("gnutls", "keyfile", 0);
214 dh_bits = Conf.ReadInteger("gnutls", "dhbits", 0, false);
216 // Set all the default values needed.
223 if (certfile.empty())
224 certfile = "cert.pem";
229 if((dh_bits != 768) && (dh_bits != 1024) && (dh_bits != 2048) && (dh_bits != 3072) && (dh_bits != 4096))
232 // Prepend relative paths with the path to the config directory.
233 if ((cafile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(cafile)))
234 cafile = confdir + cafile;
236 if ((crlfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(crlfile)))
237 crlfile = confdir + crlfile;
239 if ((certfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(certfile)))
240 certfile = confdir + certfile;
242 if ((keyfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(keyfile)))
243 keyfile = confdir + keyfile;
249 // Deallocate the old credentials
250 gnutls_dh_params_deinit(dh_params);
251 gnutls_certificate_free_credentials(x509_cred);
256 if((ret = gnutls_certificate_allocate_credentials(&x509_cred)) < 0)
257 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to allocate certificate credentials: %s", gnutls_strerror(ret));
259 if((ret = gnutls_dh_params_init(&dh_params)) < 0)
260 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to initialise DH parameters: %s", gnutls_strerror(ret));
262 if((ret =gnutls_certificate_set_x509_trust_file(x509_cred, cafile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
263 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));
265 if((ret = gnutls_certificate_set_x509_crl_file (x509_cred, crlfile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
266 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));
268 if((ret = gnutls_certificate_set_x509_key_file (x509_cred, certfile.c_str(), keyfile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
270 // If this fails, no SSL port will work. At all. So, do the smart thing - throw a ModuleException
271 throw ModuleException("Unable to load GnuTLS server certificate (" + certfile + ", key: " + keyfile + "): " + std::string(gnutls_strerror(ret)));
274 // This may be on a large (once a day or week) timer eventually.
278 void GenerateDHParams()
280 // Generate Diffie Hellman parameters - for use with DHE
281 // kx algorithms. These should be discarded and regenerated
282 // once a day, once a week or once a month. Depending on the
283 // security requirements.
287 if((ret = gnutls_dh_params_generate2(dh_params, dh_bits)) < 0)
288 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to generate DH parameters (%d bits): %s", dh_bits, gnutls_strerror(ret));
291 virtual ~ModuleSSLGnuTLS()
293 gnutls_dh_params_deinit(dh_params);
294 gnutls_certificate_free_credentials(x509_cred);
295 gnutls_global_deinit();
296 ServerInstance->Modules->UnpublishInterface("BufferedSocketHook", this);
300 virtual void OnCleanup(int target_type, void* item)
302 if(target_type == TYPE_USER)
304 User* user = (User*)item;
306 if (user->GetIOHook() == this)
308 // User is using SSL, they're a local user, and they're using one of *our* SSL ports.
309 // Potentially there could be multiple SSL modules loaded at once on different ports.
310 ServerInstance->Users->QuitUser(user, "SSL module unloading");
313 if (user->GetExt("ssl_cert"))
316 user->GetExt("ssl_cert", tofree);
318 user->Shrink("ssl_cert");
323 virtual void OnUnloadModule(Module* mod, const std::string &name)
327 for(unsigned int i = 0; i < listenports.size(); i++)
329 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
330 if (listenports[i] == (ServerInstance->Config->ports[j]->GetIP()+":"+ConvToStr(ServerInstance->Config->ports[j]->GetPort())))
331 ServerInstance->Config->ports[j]->SetDescription("plaintext");
336 virtual Version GetVersion()
338 return Version("$Id$", VF_VENDOR, API_VERSION);
342 virtual void On005Numeric(std::string &output)
344 if (!sslports.empty())
345 output.append(" SSL=" + sslports);
346 output.append(" STARTTLS");
349 virtual void OnHookUserIO(User* user, const std::string &targetip)
351 if (!user->GetIOHook() && isin(targetip,user->GetPort(),listenports))
353 /* Hook the user with our module */
354 user->AddIOHook(this);
358 virtual const char* OnRequest(Request* request)
360 ISHRequest* ISR = (ISHRequest*)request;
361 if (strcmp("IS_NAME", request->GetId()) == 0)
365 else if (strcmp("IS_HOOK", request->GetId()) == 0)
367 const char* ret = "OK";
370 ret = ISR->Sock->AddIOHook((Module*)this) ? "OK" : NULL;
372 catch (ModuleException &e)
378 else if (strcmp("IS_UNHOOK", request->GetId()) == 0)
380 return ISR->Sock->DelIOHook() ? "OK" : NULL;
382 else if (strcmp("IS_HSDONE", request->GetId()) == 0)
384 if (ISR->Sock->GetFd() < 0)
387 issl_session* session = &sessions[ISR->Sock->GetFd()];
388 return (session->status == ISSL_HANDSHAKING_READ || session->status == ISSL_HANDSHAKING_WRITE) ? NULL : "OK";
390 else if (strcmp("IS_ATTACH", request->GetId()) == 0)
392 if (ISR->Sock->GetFd() > -1)
394 issl_session* session = &sessions[ISR->Sock->GetFd()];
397 if ((Extensible*)ServerInstance->SE->GetRef(ISR->Sock->GetFd()) == (Extensible*)(ISR->Sock))
399 VerifyCertificate(session, (BufferedSocket*)ISR->Sock);
405 else if (strcmp("GET_FP", request->GetId()) == 0)
407 if (ISR->Sock->GetFd() > -1)
409 issl_session* session = &sessions[ISR->Sock->GetFd()];
412 Extensible* ext = ISR->Sock;
414 if (ext->GetExt("ssl_cert",certinfo))
415 return certinfo->GetFingerprint().c_str();
423 virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
425 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
426 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
429 issl_session* session = &sessions[fd];
431 /* For STARTTLS: Don't try and init a session on a socket that already has a session */
435 gnutls_init(&session->sess, GNUTLS_SERVER);
437 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
438 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
439 gnutls_dh_set_prime_bits(session->sess, dh_bits);
441 gnutls_transport_set_ptr(session->sess, (gnutls_transport_ptr_t) fd); // Give gnutls the fd for the socket.
443 gnutls_certificate_server_set_request(session->sess, GNUTLS_CERT_REQUEST); // Request client certificate if any.
445 Handshake(session, fd);
448 virtual void OnRawSocketConnect(int fd)
450 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
451 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
454 issl_session* session = &sessions[fd];
456 gnutls_init(&session->sess, GNUTLS_CLIENT);
458 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
459 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
460 gnutls_dh_set_prime_bits(session->sess, dh_bits);
461 gnutls_transport_set_ptr(session->sess, (gnutls_transport_ptr_t) fd); // Give gnutls the fd for the socket.
463 Handshake(session, fd);
466 virtual void OnRawSocketClose(int fd)
468 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
469 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds()))
472 CloseSession(&sessions[fd]);
474 EventHandler* user = ServerInstance->SE->GetRef(fd);
476 if ((user) && (user->GetExt("ssl_cert")))
479 user->GetExt("ssl_cert", tofree);
481 user->Shrink("ssl_cert");
485 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
487 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
488 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
491 issl_session* session = &sessions[fd];
496 CloseSession(session);
500 if (session->status == ISSL_HANDSHAKING_READ)
502 // The handshake isn't finished, try to finish it.
504 if(!Handshake(session, fd))
506 errno = session->status == ISSL_CLOSING ? EIO : EAGAIN;
507 // Couldn't resume handshake.
511 else if (session->status == ISSL_HANDSHAKING_WRITE)
518 // If we resumed the handshake then session->status will be ISSL_HANDSHAKEN.
520 if (session->status == ISSL_HANDSHAKEN)
522 int ret = gnutls_record_recv(session->sess, buffer, count);
530 // Client closed connection.
532 CloseSession(session);
535 else if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
542 ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT,
543 "m_ssl_gnutls.so: Error while reading on fd %d: %s",
544 fd, gnutls_strerror(ret));
546 CloseSession(session);
549 else if(session->status == ISSL_CLOSING)
555 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
557 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
558 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
561 issl_session* session = &sessions[fd];
562 const char* sendbuffer = buffer;
566 CloseSession(session);
570 session->outbuf.append(sendbuffer, count);
571 sendbuffer = session->outbuf.c_str();
572 count = session->outbuf.size();
574 if (session->status == ISSL_HANDSHAKING_WRITE || session->status == ISSL_HANDSHAKING_READ)
576 // The handshake isn't finished, try to finish it.
577 Handshake(session, fd);
578 errno = session->status == ISSL_CLOSING ? EIO : EAGAIN;
584 if (session->status == ISSL_HANDSHAKEN)
586 ret = gnutls_record_send(session->sess, sendbuffer, count);
590 CloseSession(session);
594 if(ret != GNUTLS_E_AGAIN && ret != GNUTLS_E_INTERRUPTED)
596 ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT,
597 "m_ssl_gnutls.so: Error while writing to fd %d: %s",
598 fd, gnutls_strerror(ret));
599 CloseSession(session);
608 session->outbuf = session->outbuf.substr(ret);
614 /* Who's smart idea was it to return 1 when we havent written anything?
615 * This fucks the buffer up in BufferedSocket :p
617 return ret < 1 ? 0 : ret;
620 // :kenny.chatspike.net 320 Om Epy|AFK :is a Secure Connection
621 virtual void OnWhois(User* source, User* dest)
626 // Bugfix, only send this numeric for *our* SSL users
627 if (dest->GetExt("ssl"))
629 ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
633 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
635 // check if the linking module wants to know about OUR metadata
638 // check if this user has an swhois field to send
639 if(user->GetExt(extname))
641 // call this function in the linking module, let it format the data how it
642 // sees fit, and send it on its way. We dont need or want to know how.
643 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, displayable ? "Enabled" : "ON");
648 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
650 // check if its our metadata key, and its associated with a user
651 if ((target_type == TYPE_USER) && (extname == "ssl"))
653 User* dest = (User*)target;
654 // if they dont already have an ssl flag, accept the remote server's
655 if (!dest->GetExt(extname))
657 dest->Extend(extname, "ON");
662 bool Handshake(issl_session* session, int fd)
664 int ret = gnutls_handshake(session->sess);
668 if(ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
670 // Handshake needs resuming later, read() or write() would have blocked.
672 if(gnutls_record_get_direction(session->sess) == 0)
674 // gnutls_handshake() wants to read() again.
675 session->status = ISSL_HANDSHAKING_READ;
679 // gnutls_handshake() wants to write() again.
680 session->status = ISSL_HANDSHAKING_WRITE;
687 ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT,
688 "m_ssl_gnutls.so: Handshake failed on fd %d: %s",
689 fd, gnutls_strerror(ret));
690 CloseSession(session);
691 session->status = ISSL_CLOSING;
698 // Handshake complete.
699 // This will do for setting the ssl flag...it could be done earlier if it's needed. But this seems neater.
700 EventHandler *extendme = ServerInstance->SE->GetRef(fd);
703 if (!extendme->GetExt("ssl"))
704 extendme->Extend("ssl", "ON");
707 // Change the seesion state
708 session->status = ISSL_HANDSHAKEN;
710 // Finish writing, if any left
717 virtual void OnPostConnect(User* user)
719 // This occurs AFTER OnUserConnect so we can be sure the
720 // protocol module has propagated the NICK message.
721 if (user->GetIOHook() == this && (IS_LOCAL(user)))
723 // Tell whatever protocol module we're using that we need to inform other servers of this metadata NOW.
724 ServerInstance->PI->SendMetaData(user, TYPE_USER, "ssl", "on");
726 VerifyCertificate(&sessions[user->GetFd()],user);
727 if (sessions[user->GetFd()].sess)
729 std::string cipher = gnutls_kx_get_name(gnutls_kx_get(sessions[user->GetFd()].sess));
730 cipher.append("-").append(gnutls_cipher_get_name(gnutls_cipher_get(sessions[user->GetFd()].sess))).append("-");
731 cipher.append(gnutls_mac_get_name(gnutls_mac_get(sessions[user->GetFd()].sess)));
732 user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\"", user->nick.c_str(), cipher.c_str());
737 void MakePollWrite(int fd)
739 //OnRawSocketWrite(fd, NULL, 0);
740 EventHandler* eh = ServerInstance->SE->GetRef(fd);
742 ServerInstance->SE->WantWrite(eh);
745 virtual void OnBufferFlushed(User* user)
747 if (user->GetIOHook() == this)
749 issl_session* session = &sessions[user->GetFd()];
750 if (session && session->outbuf.size())
751 OnRawSocketWrite(user->GetFd(), NULL, 0);
755 void CloseSession(issl_session* session)
759 gnutls_bye(session->sess, GNUTLS_SHUT_WR);
760 gnutls_deinit(session->sess);
763 session->outbuf.clear();
764 session->sess = NULL;
765 session->status = ISSL_NONE;
768 void VerifyCertificate(issl_session* session, Extensible* user)
770 if (!session->sess || !user)
774 const gnutls_datum_t* cert_list;
776 unsigned int cert_list_size;
777 gnutls_x509_crt_t cert;
779 unsigned char digest[MAXBUF];
780 size_t digest_size = sizeof(digest);
781 size_t name_size = sizeof(name);
782 ssl_cert* certinfo = new ssl_cert;
784 user->Extend("ssl_cert",certinfo);
786 /* This verification function uses the trusted CAs in the credentials
787 * structure. So you must have installed one or more CA certificates.
789 ret = gnutls_certificate_verify_peers2(session->sess, &status);
793 certinfo->data.insert(std::make_pair("error",std::string(gnutls_strerror(ret))));
797 if (status & GNUTLS_CERT_INVALID)
799 certinfo->data.insert(std::make_pair("invalid",ConvToStr(1)));
803 certinfo->data.insert(std::make_pair("invalid",ConvToStr(0)));
805 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
807 certinfo->data.insert(std::make_pair("unknownsigner",ConvToStr(1)));
811 certinfo->data.insert(std::make_pair("unknownsigner",ConvToStr(0)));
813 if (status & GNUTLS_CERT_REVOKED)
815 certinfo->data.insert(std::make_pair("revoked",ConvToStr(1)));
819 certinfo->data.insert(std::make_pair("revoked",ConvToStr(0)));
821 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
823 certinfo->data.insert(std::make_pair("trusted",ConvToStr(0)));
827 certinfo->data.insert(std::make_pair("trusted",ConvToStr(1)));
830 /* Up to here the process is the same for X.509 certificates and
831 * OpenPGP keys. From now on X.509 certificates are assumed. This can
832 * be easily extended to work with openpgp keys as well.
834 if (gnutls_certificate_type_get(session->sess) != GNUTLS_CRT_X509)
836 certinfo->data.insert(std::make_pair("error","No X509 keys sent"));
840 ret = gnutls_x509_crt_init(&cert);
843 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
848 cert_list = gnutls_certificate_get_peers(session->sess, &cert_list_size);
849 if (cert_list == NULL)
851 certinfo->data.insert(std::make_pair("error","No certificate was found"));
855 /* This is not a real world example, since we only check the first
856 * certificate in the given chain.
859 ret = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
862 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
866 gnutls_x509_crt_get_dn(cert, name, &name_size);
868 certinfo->data.insert(std::make_pair("dn",name));
870 gnutls_x509_crt_get_issuer_dn(cert, name, &name_size);
872 certinfo->data.insert(std::make_pair("issuer",name));
874 if ((ret = gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, digest, &digest_size)) < 0)
876 certinfo->data.insert(std::make_pair("error",gnutls_strerror(ret)));
880 certinfo->data.insert(std::make_pair("fingerprint",irc::hex(digest, digest_size)));
883 /* Beware here we do not check for errors.
885 if ((gnutls_x509_crt_get_expiration_time(cert) < ServerInstance->Time()) || (gnutls_x509_crt_get_activation_time(cert) > ServerInstance->Time()))
887 certinfo->data.insert(std::make_pair("error","Not activated, or expired certificate"));
890 gnutls_x509_crt_deinit(cert);
895 void OnEvent(Event* ev)
897 GenericCapHandler(ev, "tls", "tls");
902 Module* server = ServerInstance->Modules->Find("m_spanningtree.so");
903 ServerInstance->Modules->SetPriority(this, I_OnPostConnect, PRIORITY_AFTER, &server);
907 MODULE_INIT(ModuleSSLGnuTLS)