]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/ssl.h
Get rid of the NICKForced extension
[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 <map>
24 #include <string>
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 /** Get certificate from a socket (only useful with an SSL module) */
135 struct SocketCertificateRequest : public Request
136 {
137         StreamSocket* const sock;
138         ssl_cert* cert;
139
140         SocketCertificateRequest(StreamSocket* ss, Module* Me)
141                 : Request(Me, ss->GetIOHook(), "GET_SSL_CERT"), sock(ss), cert(NULL)
142         {
143                 Send();
144         }
145
146         std::string GetFingerprint()
147         {
148                 if (cert)
149                         return cert->GetFingerprint();
150                 return "";
151         }
152 };
153
154 /** Get certificate from a user (requires m_sslinfo) */
155 struct UserCertificateRequest : public Request
156 {
157         User* const user;
158         ssl_cert* cert;
159
160         UserCertificateRequest(User* u, Module* Me, Module* info = ServerInstance->Modules->Find("m_sslinfo.so"))
161                 : Request(Me, info, "GET_USER_CERT"), user(u), cert(NULL)
162         {
163                 Send();
164         }
165
166         std::string GetFingerprint()
167         {
168                 if (cert)
169                         return cert->GetFingerprint();
170                 return "";
171         }
172 };