]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/ssl.h
Merge pull request #488 from SaberUK/master+loglevel-rename
[user/henk/code/inspircd.git] / src / 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 #ifndef SSL_H
22 #define SSL_H
23
24 #include <map>
25 #include <string>
26
27 /** ssl_cert is a class which abstracts SSL certificate
28  * and key information.
29  *
30  * Because gnutls and openssl represent key information in
31  * wildly different ways, this class allows it to be accessed
32  * in a unified manner. These classes are attached to ssl-
33  * connected local users using SSLCertExt
34  */
35 class ssl_cert : public refcountbase
36 {
37  public:
38         std::string dn;
39         std::string issuer;
40         std::string error;
41         std::string fingerprint;
42         bool trusted, invalid, unknownsigner, revoked;
43
44         ssl_cert() : trusted(false), invalid(true), unknownsigner(true), revoked(false) {}
45
46         /** Get certificate distinguished name
47          * @return Certificate DN
48          */
49         const std::string& GetDN()
50         {
51                 return dn;
52         }
53
54         /** Get Certificate issuer
55          * @return Certificate issuer
56          */
57         const std::string& GetIssuer()
58         {
59                 return issuer;
60         }
61
62         /** Get error string if an error has occured
63          * @return The error associated with this users certificate,
64          * or an empty string if there is no error.
65          */
66         const std::string& GetError()
67         {
68                 return error;
69         }
70
71         /** Get key fingerprint.
72          * @return The key fingerprint as a hex string.
73          */
74         const std::string& GetFingerprint()
75         {
76                 return fingerprint;
77         }
78
79         /** Get trust status
80          * @return True if this is a trusted certificate
81          * (the certificate chain validates)
82          */
83         bool IsTrusted()
84         {
85                 return trusted;
86         }
87
88         /** Get validity status
89          * @return True if the certificate itself is
90          * correctly formed.
91          */
92         bool IsInvalid()
93         {
94                 return invalid;
95         }
96
97         /** Get signer status
98          * @return True if the certificate appears to be
99          * self-signed.
100          */
101         bool IsUnknownSigner()
102         {
103                 return unknownsigner;
104         }
105
106         /** Get revokation status.
107          * @return True if the certificate is revoked.
108          * Note that this only works properly for GnuTLS
109          * right now.
110          */
111         bool IsRevoked()
112         {
113                 return revoked;
114         }
115
116         bool IsCAVerified()
117         {
118                 return trusted && !invalid && !revoked && !unknownsigner && error.empty();
119         }
120
121         std::string GetMetaLine()
122         {
123                 std::stringstream value;
124                 bool hasError = !error.empty();
125                 value << (IsInvalid() ? "v" : "V") << (IsTrusted() ? "T" : "t") << (IsRevoked() ? "R" : "r")
126                         << (IsUnknownSigner() ? "s" : "S") << (hasError ? "E" : "e") << " ";
127                 if (hasError)
128                         value << GetError();
129                 else
130                         value << GetFingerprint() << " " << GetDN() << " " << GetIssuer();
131                 return value.str();
132         }
133 };
134
135 /** Get certificate from a socket (only useful with an SSL module) */
136 struct SocketCertificateRequest : public Request
137 {
138         StreamSocket* const sock;
139         ssl_cert* cert;
140
141         SocketCertificateRequest(StreamSocket* ss, Module* Me)
142                 : Request(Me, ss->GetIOHook(), "GET_SSL_CERT"), sock(ss), cert(NULL)
143         {
144                 Send();
145         }
146
147         std::string GetFingerprint()
148         {
149                 if (cert)
150                         return cert->GetFingerprint();
151                 return "";
152         }
153 };
154
155 /** Get certificate from a user (requires m_sslinfo) */
156 struct UserCertificateRequest : public Request
157 {
158         User* const user;
159         ssl_cert* cert;
160
161         UserCertificateRequest(User* u, Module* Me, Module* info = ServerInstance->Modules->Find("m_sslinfo.so"))
162                 : Request(Me, info, "GET_USER_CERT"), user(u), cert(NULL)
163         {
164                 Send();
165         }
166
167         std::string GetFingerprint()
168         {
169                 if (cert)
170                         return cert->GetFingerprint();
171                 return "";
172         }
173 };
174
175 #endif