]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_ssl_gnutls.cpp
Use ServiceProvider for inter-module dependencies
[user/henk/code/inspircd.git] / src / modules / extra / m_ssl_gnutls.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include <gnutls/gnutls.h>
16 #include <gnutls/x509.h>
17 #include "ssl.h"
18 #include "m_cap.h"
19
20 #ifdef WINDOWS
21 #pragma comment(lib, "libgnutls-13.lib")
22 #endif
23
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) */
30
31 enum issl_status { ISSL_NONE, ISSL_HANDSHAKING_READ, ISSL_HANDSHAKING_WRITE, ISSL_HANDSHAKEN, ISSL_CLOSING, ISSL_CLOSED };
32
33 static gnutls_x509_crt_t x509_cert;
34 static gnutls_x509_privkey_t x509_key;
35 static int cert_callback (gnutls_session_t session, const gnutls_datum_t * req_ca_rdn, int nreqs,
36         const gnutls_pk_algorithm_t * sign_algos, int sign_algos_length, gnutls_retr_st * st) {
37
38         st->type = GNUTLS_CRT_X509;
39         st->ncerts = 1;
40         st->cert.x509 = &x509_cert;
41         st->key.x509 = x509_key;
42         st->deinit_all = 0;
43
44         return 0;
45 }
46
47 static ssize_t gnutls_pull_wrapper(gnutls_transport_ptr_t user_wrap, void* buffer, size_t size)
48 {
49         StreamSocket* user = reinterpret_cast<StreamSocket*>(user_wrap);
50         if (user->GetEventMask() & FD_READ_WILL_BLOCK)
51         {
52                 errno = EAGAIN;
53                 return -1;
54         }
55         int rv = recv(user->GetFd(), buffer, size, 0);
56         if (rv < (int)size)
57                 ServerInstance->SE->ChangeEventMask(user, FD_READ_WILL_BLOCK);
58         return rv;
59 }
60
61 static ssize_t gnutls_push_wrapper(gnutls_transport_ptr_t user_wrap, const void* buffer, size_t size)
62 {
63         StreamSocket* user = reinterpret_cast<StreamSocket*>(user_wrap);
64         if (user->GetEventMask() & FD_WRITE_WILL_BLOCK)
65         {
66                 errno = EAGAIN;
67                 return -1;
68         }
69         int rv = send(user->GetFd(), buffer, size, 0);
70         if (rv < (int)size)
71                 ServerInstance->SE->ChangeEventMask(user, FD_WRITE_WILL_BLOCK);
72         return rv;
73 }
74
75 /** Represents an SSL user's extra data
76  */
77 class issl_session
78 {
79 public:
80         gnutls_session_t sess;
81         issl_status status;
82         reference<ssl_cert> cert;
83         issl_session() : sess(NULL) {}
84 };
85
86 class CommandStartTLS : public SplitCommand
87 {
88  public:
89         CommandStartTLS (Module* mod) : SplitCommand(mod, "STARTTLS")
90         {
91                 works_before_reg = true;
92         }
93
94         CmdResult HandleLocal(const std::vector<std::string> &parameters, LocalUser *user)
95         {
96                 /* changed from == REG_ALL to catch clients sending STARTTLS
97                  * after NICK and USER but before OnUserConnect completes and
98                  * give a proper error message (see bug #645) - dz
99                  */
100                 if (user->registered != REG_NONE)
101                 {
102                         user->WriteNumeric(691, "%s :STARTTLS is not permitted after client registration has started", user->nick.c_str());
103                 }
104                 else
105                 {
106                         if (!user->eh.GetIOHook())
107                         {
108                                 user->WriteNumeric(670, "%s :STARTTLS successful, go ahead with TLS handshake", user->nick.c_str());
109                                 user->eh.AddIOHook(creator);
110                                 creator->OnStreamSocketAccept(&user->eh, NULL, NULL);
111                         }
112                         else
113                                 user->WriteNumeric(691, "%s :STARTTLS failure", user->nick.c_str());
114                 }
115
116                 return CMD_FAILURE;
117         }
118 };
119
120 class ModuleSSLGnuTLS : public Module
121 {
122         issl_session* sessions;
123
124         gnutls_certificate_credentials x509_cred;
125         gnutls_dh_params dh_params;
126
127         std::string keyfile;
128         std::string certfile;
129
130         std::string cafile;
131         std::string crlfile;
132         std::string sslports;
133         int dh_bits;
134
135         bool cred_alloc;
136
137         CommandStartTLS starttls;
138
139         GenericCap capHandler;
140         ServiceProvider iohook;
141  public:
142
143         ModuleSSLGnuTLS()
144                 : starttls(this), capHandler(this, "tls"), iohook(this, "ssl/gnutls", SERVICE_IOHOOK)
145         {
146                 sessions = new issl_session[ServerInstance->SE->GetMaxFds()];
147
148                 gnutls_global_init(); // This must be called once in the program
149                 gnutls_x509_crt_init(&x509_cert);
150                 gnutls_x509_privkey_init(&x509_key);
151
152                 cred_alloc = false;
153                 // Needs the flag as it ignores a plain /rehash
154                 OnModuleRehash(NULL,"ssl");
155
156                 // Void return, guess we assume success
157                 gnutls_certificate_set_dh_params(x509_cred, dh_params);
158                 Implementation eventlist[] = { I_On005Numeric, I_OnRehash, I_OnModuleRehash, I_OnUserConnect,
159                         I_OnEvent, I_OnHookIO };
160                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
161
162                 ServerInstance->Modules->AddService(iohook);
163                 ServerInstance->AddCommand(&starttls);
164         }
165
166         void OnRehash(User* user)
167         {
168                 ConfigReader Conf;
169
170                 sslports.clear();
171
172                 for (size_t i = 0; i < ServerInstance->ports.size(); i++)
173                 {
174                         ListenSocket* port = ServerInstance->ports[i];
175                         if (port->bind_tag->getString("ssl") != "gnutls")
176                                 continue;
177
178                         const std::string& portid = port->bind_desc;
179                         ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %s", portid.c_str());
180
181                         if (port->bind_tag->getString("type", "clients") == "clients" && port->bind_addr != "127.0.0.1")
182                                 sslports.append(portid).append(";");
183                 }
184
185                 if (!sslports.empty())
186                         sslports.erase(sslports.end() - 1);
187         }
188
189         void OnModuleRehash(User* user, const std::string &param)
190         {
191                 if(param != "ssl")
192                         return;
193
194                 OnRehash(user);
195
196                 ConfigReader Conf;
197
198                 cafile = Conf.ReadValue("gnutls", "cafile", 0);
199                 crlfile = Conf.ReadValue("gnutls", "crlfile", 0);
200                 certfile = Conf.ReadValue("gnutls", "certfile", 0);
201                 keyfile = Conf.ReadValue("gnutls", "keyfile", 0);
202                 dh_bits = Conf.ReadInteger("gnutls", "dhbits", 0, false);
203
204                 // Set all the default values needed.
205                 if (cafile.empty())
206                         cafile = "conf/ca.pem";
207
208                 if (crlfile.empty())
209                         crlfile = "conf/crl.pem";
210
211                 if (certfile.empty())
212                         certfile = "conf/cert.pem";
213
214                 if (keyfile.empty())
215                         keyfile = "conf/key.pem";
216
217                 if((dh_bits != 768) && (dh_bits != 1024) && (dh_bits != 2048) && (dh_bits != 3072) && (dh_bits != 4096))
218                         dh_bits = 1024;
219
220                 int ret;
221
222                 if (cred_alloc)
223                 {
224                         // Deallocate the old credentials
225                         gnutls_dh_params_deinit(dh_params);
226                         gnutls_certificate_free_credentials(x509_cred);
227                 }
228                 else
229                         cred_alloc = true;
230
231                 if((ret = gnutls_certificate_allocate_credentials(&x509_cred)) < 0)
232                         ServerInstance->Logs->Log("m_ssl_gnutls",DEBUG, "m_ssl_gnutls.so: Failed to allocate certificate credentials: %s", gnutls_strerror(ret));
233
234                 if((ret =gnutls_certificate_set_x509_trust_file(x509_cred, cafile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
235                         ServerInstance->Logs->Log("m_ssl_gnutls",DEBUG, "m_ssl_gnutls.so: Failed to set X.509 trust file '%s': %s", cafile.c_str(), gnutls_strerror(ret));
236
237                 if((ret = gnutls_certificate_set_x509_crl_file (x509_cred, crlfile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
238                         ServerInstance->Logs->Log("m_ssl_gnutls",DEBUG, "m_ssl_gnutls.so: Failed to set X.509 CRL file '%s': %s", crlfile.c_str(), gnutls_strerror(ret));
239
240                 FileReader reader;
241
242                 reader.LoadFile(certfile);
243                 std::string cert_string = reader.Contents();
244                 gnutls_datum_t cert_datum = { (unsigned char*)cert_string.data(), cert_string.length() };
245
246                 reader.LoadFile(keyfile);
247                 std::string key_string = reader.Contents();
248                 gnutls_datum_t key_datum = { (unsigned char*)key_string.data(), key_string.length() };
249
250                 // If this fails, no SSL port will work. At all. So, do the smart thing - throw a ModuleException
251                 if((ret = gnutls_x509_crt_import(x509_cert, &cert_datum, GNUTLS_X509_FMT_PEM)) < 0)
252                         throw ModuleException("Unable to load GnuTLS server certificate (" + certfile + "): " + std::string(gnutls_strerror(ret)));
253
254                 if((ret = gnutls_x509_privkey_import(x509_key, &key_datum, GNUTLS_X509_FMT_PEM)) < 0)
255                         throw ModuleException("Unable to load GnuTLS server private key (" + keyfile + "): " + std::string(gnutls_strerror(ret)));
256
257                 if((ret = gnutls_certificate_set_x509_key(x509_cred, &x509_cert, 1, x509_key)) < 0)
258                         throw ModuleException("Unable to set GnuTLS cert/key pair: " + std::string(gnutls_strerror(ret)));
259
260                 gnutls_certificate_client_set_retrieve_function (x509_cred, cert_callback);
261
262                 if((ret = gnutls_dh_params_init(&dh_params)) < 0)
263                         ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to initialise DH parameters: %s", gnutls_strerror(ret));
264
265                 // This may be on a large (once a day or week) timer eventually.
266                 GenerateDHParams();
267         }
268
269         void GenerateDHParams()
270         {
271                 // Generate Diffie Hellman parameters - for use with DHE
272                 // kx algorithms. These should be discarded and regenerated
273                 // once a day, once a week or once a month. Depending on the
274                 // security requirements.
275
276                 int ret;
277
278                 if((ret = gnutls_dh_params_generate2(dh_params, dh_bits)) < 0)
279                         ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to generate DH parameters (%d bits): %s", dh_bits, gnutls_strerror(ret));
280         }
281
282         ~ModuleSSLGnuTLS()
283         {
284                 gnutls_x509_crt_deinit(x509_cert);
285                 gnutls_x509_privkey_deinit(x509_key);
286                 gnutls_dh_params_deinit(dh_params);
287                 gnutls_certificate_free_credentials(x509_cred);
288                 gnutls_global_deinit();
289                 delete[] sessions;
290         }
291
292         void OnCleanup(int target_type, void* item)
293         {
294                 if(target_type == TYPE_USER)
295                 {
296                         LocalUser* user = IS_LOCAL(static_cast<User*>(item));
297
298                         if (user && user->eh.GetIOHook() == this)
299                         {
300                                 // User is using SSL, they're a local user, and they're using one of *our* SSL ports.
301                                 // Potentially there could be multiple SSL modules loaded at once on different ports.
302                                 ServerInstance->Users->QuitUser(user, "SSL module unloading");
303                         }
304                 }
305         }
306
307         Version GetVersion()
308         {
309                 return Version("Provides SSL support for clients", VF_VENDOR);
310         }
311
312
313         void On005Numeric(std::string &output)
314         {
315                 if (!sslports.empty())
316                         output.append(" SSL=" + sslports);
317                 output.append(" STARTTLS");
318         }
319
320         void OnHookIO(StreamSocket* user, ListenSocket* lsb)
321         {
322                 if (!user->GetIOHook() && lsb->bind_tag->getString("ssl") == "gnutls")
323                 {
324                         /* Hook the user with our module */
325                         user->AddIOHook(this);
326                 }
327         }
328
329         void OnRequest(Request& request)
330         {
331                 if (strcmp("GET_SSL_CERT", request.id) == 0)
332                 {
333                         SocketCertificateRequest& req = static_cast<SocketCertificateRequest&>(request);
334                         int fd = req.sock->GetFd();
335                         issl_session* session = &sessions[fd];
336
337                         req.cert = session->cert;
338                 }
339         }
340
341         void OnStreamSocketAccept(StreamSocket* user, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
342         {
343                 int fd = user->GetFd();
344                 issl_session* session = &sessions[fd];
345
346                 /* For STARTTLS: Don't try and init a session on a socket that already has a session */
347                 if (session->sess)
348                         return;
349
350                 gnutls_init(&session->sess, GNUTLS_SERVER);
351
352                 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
353                 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
354                 gnutls_dh_set_prime_bits(session->sess, dh_bits);
355
356                 gnutls_transport_set_ptr(session->sess, reinterpret_cast<gnutls_transport_ptr_t>(user));
357                 gnutls_transport_set_push_function(session->sess, gnutls_push_wrapper);
358                 gnutls_transport_set_pull_function(session->sess, gnutls_pull_wrapper);
359
360                 gnutls_certificate_server_set_request(session->sess, GNUTLS_CERT_REQUEST); // Request client certificate if any.
361
362                 Handshake(session, user);
363         }
364
365         void OnStreamSocketConnect(StreamSocket* user)
366         {
367                 issl_session* session = &sessions[user->GetFd()];
368
369                 gnutls_init(&session->sess, GNUTLS_CLIENT);
370
371                 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
372                 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
373                 gnutls_dh_set_prime_bits(session->sess, dh_bits);
374                 gnutls_transport_set_ptr(session->sess, reinterpret_cast<gnutls_transport_ptr_t>(user));
375                 gnutls_transport_set_push_function(session->sess, gnutls_push_wrapper);
376                 gnutls_transport_set_pull_function(session->sess, gnutls_pull_wrapper);
377
378                 Handshake(session, user);
379         }
380
381         void OnStreamSocketClose(StreamSocket* user)
382         {
383                 CloseSession(&sessions[user->GetFd()]);
384         }
385
386         int OnStreamSocketRead(StreamSocket* user, std::string& recvq)
387         {
388                 issl_session* session = &sessions[user->GetFd()];
389
390                 if (!session->sess)
391                 {
392                         CloseSession(session);
393                         user->SetError("No SSL session");
394                         return -1;
395                 }
396
397                 if (session->status == ISSL_HANDSHAKING_READ || session->status == ISSL_HANDSHAKING_WRITE)
398                 {
399                         // The handshake isn't finished, try to finish it.
400
401                         if(!Handshake(session, user))
402                         {
403                                 if (session->status != ISSL_CLOSING)
404                                         return 0;
405                                 return -1;
406                         }
407                 }
408
409                 // If we resumed the handshake then session->status will be ISSL_HANDSHAKEN.
410
411                 if (session->status == ISSL_HANDSHAKEN)
412                 {
413                         char* buffer = ServerInstance->GetReadBuffer();
414                         size_t bufsiz = ServerInstance->Config->NetBufferSize;
415                         int ret = gnutls_record_recv(session->sess, buffer, bufsiz);
416                         if (ret > 0)
417                         {
418                                 recvq.append(buffer, ret);
419                                 return 1;
420                         }
421                         else if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
422                         {
423                                 return 0;
424                         }
425                         else if (ret == 0)
426                         {
427                                 user->SetError("SSL Connection closed");
428                                 CloseSession(session);
429                                 return -1;
430                         }
431                         else
432                         {
433                                 user->SetError(gnutls_strerror(ret));
434                                 CloseSession(session);
435                                 return -1;
436                         }
437                 }
438                 else if (session->status == ISSL_CLOSING)
439                         return -1;
440
441                 return 0;
442         }
443
444         int OnStreamSocketWrite(StreamSocket* user, std::string& sendq)
445         {
446                 issl_session* session = &sessions[user->GetFd()];
447
448                 if (!session->sess)
449                 {
450                         CloseSession(session);
451                         user->SetError("No SSL session");
452                         return -1;
453                 }
454
455                 if (session->status == ISSL_HANDSHAKING_WRITE || session->status == ISSL_HANDSHAKING_READ)
456                 {
457                         // The handshake isn't finished, try to finish it.
458                         Handshake(session, user);
459                         if (session->status != ISSL_CLOSING)
460                                 return 0;
461                         return -1;
462                 }
463
464                 int ret = 0;
465
466                 if (session->status == ISSL_HANDSHAKEN)
467                 {
468                         ret = gnutls_record_send(session->sess, sendq.data(), sendq.length());
469
470                         if (ret == (int)sendq.length())
471                         {
472                                 ServerInstance->SE->ChangeEventMask(user, FD_WANT_NO_WRITE);
473                                 return 1;
474                         }
475                         else if (ret > 0)
476                         {
477                                 sendq = sendq.substr(ret);
478                                 ServerInstance->SE->ChangeEventMask(user, FD_WANT_SINGLE_WRITE);
479                                 return 0;
480                         }
481                         else if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
482                         {
483                                 ServerInstance->SE->ChangeEventMask(user, FD_WANT_SINGLE_WRITE);
484                                 return 0;
485                         }
486                         else if (ret == 0)
487                         {
488                                 CloseSession(session);
489                                 user->SetError("SSL Connection closed");
490                                 return -1;
491                         }
492                         else // (ret < 0)
493                         {
494                                 user->SetError(gnutls_strerror(ret));
495                                 CloseSession(session);
496                                 return -1;
497                         }
498                 }
499
500                 return 0;
501         }
502
503         bool Handshake(issl_session* session, StreamSocket* user)
504         {
505                 int ret = gnutls_handshake(session->sess);
506
507                 if (ret < 0)
508                 {
509                         if(ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
510                         {
511                                 // Handshake needs resuming later, read() or write() would have blocked.
512
513                                 if(gnutls_record_get_direction(session->sess) == 0)
514                                 {
515                                         // gnutls_handshake() wants to read() again.
516                                         session->status = ISSL_HANDSHAKING_READ;
517                                         ServerInstance->SE->ChangeEventMask(user, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
518                                 }
519                                 else
520                                 {
521                                         // gnutls_handshake() wants to write() again.
522                                         session->status = ISSL_HANDSHAKING_WRITE;
523                                         ServerInstance->SE->ChangeEventMask(user, FD_WANT_NO_READ | FD_WANT_SINGLE_WRITE);
524                                 }
525                         }
526                         else
527                         {
528                                 user->SetError(std::string("Handshake Failed - ") + gnutls_strerror(ret));
529                                 CloseSession(session);
530                                 session->status = ISSL_CLOSING;
531                         }
532
533                         return false;
534                 }
535                 else
536                 {
537                         // Change the seesion state
538                         session->status = ISSL_HANDSHAKEN;
539
540                         VerifyCertificate(session,user);
541
542                         // Finish writing, if any left
543                         ServerInstance->SE->ChangeEventMask(user, FD_WANT_POLL_READ | FD_WANT_NO_WRITE | FD_ADD_TRIAL_WRITE);
544
545                         return true;
546                 }
547         }
548
549         void OnUserConnect(LocalUser* user)
550         {
551                 if (user->eh.GetIOHook() == this)
552                 {
553                         if (sessions[user->GetFd()].sess)
554                         {
555                                 SSLCertSubmission(user, this, ServerInstance->Modules->Find("m_sslinfo.so"), sessions[user->GetFd()].cert);
556                                 std::string cipher = gnutls_kx_get_name(gnutls_kx_get(sessions[user->GetFd()].sess));
557                                 cipher.append("-").append(gnutls_cipher_get_name(gnutls_cipher_get(sessions[user->GetFd()].sess))).append("-");
558                                 cipher.append(gnutls_mac_get_name(gnutls_mac_get(sessions[user->GetFd()].sess)));
559                                 user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\"", user->nick.c_str(), cipher.c_str());
560                         }
561                 }
562         }
563
564         void CloseSession(issl_session* session)
565         {
566                 if (session->sess)
567                 {
568                         gnutls_bye(session->sess, GNUTLS_SHUT_WR);
569                         gnutls_deinit(session->sess);
570                 }
571                 session->sess = NULL;
572                 session->cert = NULL;
573                 session->status = ISSL_NONE;
574         }
575
576         void VerifyCertificate(issl_session* session, StreamSocket* user)
577         {
578                 if (!session->sess || !user || session->cert)
579                         return;
580
581                 unsigned int status;
582                 const gnutls_datum_t* cert_list;
583                 int ret;
584                 unsigned int cert_list_size;
585                 gnutls_x509_crt_t cert;
586                 char name[MAXBUF];
587                 unsigned char digest[MAXBUF];
588                 size_t digest_size = sizeof(digest);
589                 size_t name_size = sizeof(name);
590                 ssl_cert* certinfo = new ssl_cert;
591                 session->cert = certinfo;
592
593                 /* This verification function uses the trusted CAs in the credentials
594                  * structure. So you must have installed one or more CA certificates.
595                  */
596                 ret = gnutls_certificate_verify_peers2(session->sess, &status);
597
598                 if (ret < 0)
599                 {
600                         certinfo->error = std::string(gnutls_strerror(ret));
601                         return;
602                 }
603
604                 certinfo->invalid = (status & GNUTLS_CERT_INVALID);
605                 certinfo->unknownsigner = (status & GNUTLS_CERT_SIGNER_NOT_FOUND);
606                 certinfo->revoked = (status & GNUTLS_CERT_REVOKED);
607                 certinfo->trusted = !(status & GNUTLS_CERT_SIGNER_NOT_CA);
608
609                 /* Up to here the process is the same for X.509 certificates and
610                  * OpenPGP keys. From now on X.509 certificates are assumed. This can
611                  * be easily extended to work with openpgp keys as well.
612                  */
613                 if (gnutls_certificate_type_get(session->sess) != GNUTLS_CRT_X509)
614                 {
615                         certinfo->error = "No X509 keys sent";
616                         return;
617                 }
618
619                 ret = gnutls_x509_crt_init(&cert);
620                 if (ret < 0)
621                 {
622                         certinfo->error = gnutls_strerror(ret);
623                         return;
624                 }
625
626                 cert_list_size = 0;
627                 cert_list = gnutls_certificate_get_peers(session->sess, &cert_list_size);
628                 if (cert_list == NULL)
629                 {
630                         certinfo->error = "No certificate was found";
631                         goto info_done_dealloc;
632                 }
633
634                 /* This is not a real world example, since we only check the first
635                  * certificate in the given chain.
636                  */
637
638                 ret = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
639                 if (ret < 0)
640                 {
641                         certinfo->error = gnutls_strerror(ret);
642                         goto info_done_dealloc;
643                 }
644
645                 gnutls_x509_crt_get_dn(cert, name, &name_size);
646                 certinfo->dn = name;
647
648                 gnutls_x509_crt_get_issuer_dn(cert, name, &name_size);
649                 certinfo->issuer = name;
650
651                 if ((ret = gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, digest, &digest_size)) < 0)
652                 {
653                         certinfo->error = gnutls_strerror(ret);
654                 }
655                 else
656                 {
657                         certinfo->fingerprint = irc::hex(digest, digest_size);
658                 }
659
660                 /* Beware here we do not check for errors.
661                  */
662                 if ((gnutls_x509_crt_get_expiration_time(cert) < ServerInstance->Time()) || (gnutls_x509_crt_get_activation_time(cert) > ServerInstance->Time()))
663                 {
664                         certinfo->error = "Not activated, or expired certificate";
665                 }
666
667 info_done_dealloc:
668                 gnutls_x509_crt_deinit(cert);
669         }
670
671         void OnEvent(Event& ev)
672         {
673                 capHandler.HandleEvent(ev);
674         }
675 };
676
677 MODULE_INIT(ModuleSSLGnuTLS)