]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/ssl.h
Add the ability to have multiple SSL profiles
[user/henk/code/inspircd.git] / include / modules / ssl.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #pragma once
22
23 #include <string>
24 #include "iohook.h"
25
26 /** ssl_cert is a class which abstracts SSL certificate
27  * and key information.
28  *
29  * Because gnutls and openssl represent key information in
30  * wildly different ways, this class allows it to be accessed
31  * in a unified manner. These classes are attached to ssl-
32  * connected local users using SSLCertExt
33  */
34 class ssl_cert : public refcountbase
35 {
36  public:
37         std::string dn;
38         std::string issuer;
39         std::string error;
40         std::string fingerprint;
41         bool trusted, invalid, unknownsigner, revoked;
42
43         ssl_cert() : trusted(false), invalid(true), unknownsigner(true), revoked(false) {}
44
45         /** Get certificate distinguished name
46          * @return Certificate DN
47          */
48         const std::string& GetDN()
49         {
50                 return dn;
51         }
52
53         /** Get Certificate issuer
54          * @return Certificate issuer
55          */
56         const std::string& GetIssuer()
57         {
58                 return issuer;
59         }
60
61         /** Get error string if an error has occured
62          * @return The error associated with this users certificate,
63          * or an empty string if there is no error.
64          */
65         const std::string& GetError()
66         {
67                 return error;
68         }
69
70         /** Get key fingerprint.
71          * @return The key fingerprint as a hex string.
72          */
73         const std::string& GetFingerprint()
74         {
75                 return fingerprint;
76         }
77
78         /** Get trust status
79          * @return True if this is a trusted certificate
80          * (the certificate chain validates)
81          */
82         bool IsTrusted()
83         {
84                 return trusted;
85         }
86
87         /** Get validity status
88          * @return True if the certificate itself is
89          * correctly formed.
90          */
91         bool IsInvalid()
92         {
93                 return invalid;
94         }
95
96         /** Get signer status
97          * @return True if the certificate appears to be
98          * self-signed.
99          */
100         bool IsUnknownSigner()
101         {
102                 return unknownsigner;
103         }
104
105         /** Get revokation status.
106          * @return True if the certificate is revoked.
107          * Note that this only works properly for GnuTLS
108          * right now.
109          */
110         bool IsRevoked()
111         {
112                 return revoked;
113         }
114
115         bool IsCAVerified()
116         {
117                 return trusted && !invalid && !revoked && !unknownsigner && error.empty();
118         }
119
120         std::string GetMetaLine()
121         {
122                 std::stringstream value;
123                 bool hasError = !error.empty();
124                 value << (IsInvalid() ? "v" : "V") << (IsTrusted() ? "T" : "t") << (IsRevoked() ? "R" : "r")
125                         << (IsUnknownSigner() ? "s" : "S") << (hasError ? "E" : "e") << " ";
126                 if (hasError)
127                         value << GetError();
128                 else
129                         value << GetFingerprint() << " " << GetDN() << " " << GetIssuer();
130                 return value.str();
131         }
132 };
133
134 class SSLIOHook : public IOHook
135 {
136  public:
137         SSLIOHook(Module* mod, const std::string& Name)
138                 : IOHook(mod, Name, IOHook::IOH_SSL)
139         {
140         }
141
142         /**
143          * Get the client certificate from a socket
144          * @param sock The socket to get the certificate from, must be using this IOHook
145          * @return The SSL client certificate information
146          */
147         virtual ssl_cert* GetCertificate(StreamSocket* sock) = 0;
148
149         /**
150          * Get the fingerprint of a client certificate from a socket
151          * @param sock The socket to get the certificate fingerprint from, must be using this IOHook
152          * @return The fingerprint of the SSL client certificate sent by the peer,
153          * empty if no cert was sent
154          */
155         std::string GetFingerprint(StreamSocket* sock)
156         {
157                 ssl_cert* cert = GetCertificate(sock);
158                 if (cert)
159                         return cert->GetFingerprint();
160                 return "";
161         }
162 };
163
164 /** Helper functions for obtaining SSL client certificates and key fingerprints
165  * from StreamSockets
166  */
167 class SSLClientCert
168 {
169  public:
170         /**
171          * Get the client certificate from a socket
172          * @param sock The socket to get the certificate from, the socket does not have to use SSL
173          * @return The SSL client certificate information, NULL if the peer is not using SSL
174          */
175         static ssl_cert* GetCertificate(StreamSocket* sock)
176         {
177                 IOHook* iohook = sock->GetIOHook();
178                 if ((!iohook) || (iohook->type != IOHook::IOH_SSL))
179                         return NULL;
180
181                 SSLIOHook* ssliohook = static_cast<SSLIOHook*>(iohook);
182                 return ssliohook->GetCertificate(sock);
183         }
184
185         /**
186          * Get the fingerprint of a client certificate from a socket
187          * @param sock The socket to get the certificate fingerprint from, the
188          * socket does not have to use SSL
189          * @return The key fingerprint from the SSL certificate sent by the peer,
190          * empty if no cert was sent or the peer is not using SSL
191          */
192         static std::string GetFingerprint(StreamSocket* sock)
193         {
194                 ssl_cert* cert = SSLClientCert::GetCertificate(sock);
195                 if (cert)
196                         return cert->GetFingerprint();
197                 return "";
198         }
199 };
200
201 class UserCertificateAPIBase : public DataProvider
202 {
203  public:
204         UserCertificateAPIBase(Module* parent)
205                 : DataProvider(parent, "m_sslinfo_api")
206         {
207         }
208
209         /** Get the SSL certificate of a user
210          * @param user The user whose certificate to get, user may be remote
211          * @return The SSL certificate of the user or NULL if the user is not using SSL
212          */
213         virtual ssl_cert* GetCertificate(User* user) = 0;
214
215         /** Get the key fingerprint from a user's certificate
216          * @param user The user whose key fingerprint to get, user may be remote
217          * @return The key fingerprint from the user's SSL certificate or an empty string
218          * if the user is not using SSL or did not provide a client certificate
219          */
220         std::string GetFingerprint(User* user)
221         {
222                 ssl_cert* cert = GetCertificate(user);
223                 if (cert)
224                         return cert->GetFingerprint();
225                 return "";
226         }
227 };
228
229 /** API implemented by m_sslinfo that allows modules to retrive the SSL certificate
230  * information of local and remote users. It can also be used to find out whether a
231  * user is using SSL or not.
232  */
233 class UserCertificateAPI : public dynamic_reference<UserCertificateAPIBase>
234 {
235  public:
236         UserCertificateAPI(Module* parent)
237                 : dynamic_reference<UserCertificateAPIBase>(parent, "m_sslinfo_api")
238         {
239         }
240 };