]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/ssl.h
4d303502e7d5ba5e849a579c5970248a14f2b6ad
[user/henk/code/inspircd.git] / src / modules / ssl.h
1 #ifndef __SSL_CERT_H__
2 #define __SSL_CERT_H__
3
4 #include <map>
5 #include <string>
6
7 /** A generic container for certificate data
8  */
9 typedef std::map<std::string,std::string> ssl_data;
10
11 /** A shorthand way of representing an iterator into ssl_data
12  */
13 typedef ssl_data::iterator ssl_data_iter;
14
15 /** ssl_cert is a class which abstracts SSL certificate
16  * and key information.
17  *
18  * Because gnutls and openssl represent key information in
19  * wildly different ways, this class allows it to be accessed
20  * in a unified manner. These classes are attached to ssl-
21  * connected local users using Extensible::Extend() and the
22  * key 'ssl_cert'.
23  */
24 class ssl_cert
25 {
26         /** Always contains an empty string
27          */
28         const std::string empty;
29
30  public:
31         /** The data for this certificate
32          */
33         ssl_data data;
34
35         /** Default constructor, initializes 'empty'
36          */
37         ssl_cert() : empty("")
38         {
39         }
40         
41         /** Get certificate distinguished name
42          * @return Certificate DN
43          */
44         const std::string& GetDN()
45         {
46                 ssl_data_iter ssldi = data.find("dn");
47
48                 if (ssldi != data.end())
49                         return ssldi->second;
50                 else
51                         return empty;
52         }
53
54         /** Get Certificate issuer
55          * @return Certificate issuer
56          */
57         const std::string& GetIssuer()
58         {
59                 ssl_data_iter ssldi = data.find("issuer");
60
61                 if (ssldi != data.end())
62                         return ssldi->second;
63                 else
64                         return empty;
65         }
66
67         /** Get error string if an error has occured
68          * @return The error associated with this users certificate,
69          * or an empty string if there is no error.
70          */
71         const std::string& GetError()
72         {
73                 ssl_data_iter ssldi = data.find("error");
74
75                 if (ssldi != data.end())
76                         return ssldi->second;
77                 else
78                         return empty;
79         }
80
81         /** Get key fingerprint.
82          * @return The key fingerprint as a hex string.
83          */
84         const std::string& GetFingerprint()
85         {
86                 ssl_data_iter ssldi = data.find("fingerprint");
87
88                 if (ssldi != data.end())
89                         return ssldi->second;
90                 else
91                         return empty;
92         }
93
94         /** Get trust status
95          * @return True if this is a trusted certificate
96          * (the certificate chain validates)
97          */
98         bool IsTrusted()
99         {
100                 ssl_data_iter ssldi = data.find("trusted");
101
102                 if (ssldi != data.end())
103                         return (ssldi->second == "1");
104                 else
105                         return false;
106         }
107
108         /** Get validity status
109          * @return True if the certificate itself is
110          * correctly formed.
111          */
112         bool IsInvalid()
113         {
114                 ssl_data_iter ssldi = data.find("invalid");
115
116                 if (ssldi != data.end())
117                         return (ssldi->second == "1");
118                 else
119                         return false;
120         }
121
122         /** Get signer status
123          * @return True if the certificate appears to be
124          * self-signed.
125          */
126         bool IsUnknownSigner()
127         {
128                 ssl_data_iter ssldi = data.find("unknownsigner");
129
130                 if (ssldi != data.end())
131                         return (ssldi->second == "1");
132                 else
133                         return false;
134         }
135
136         /** Get revokation status.
137          * @return True if the certificate is revoked.
138          * Note that this only works properly for GnuTLS
139          * right now.
140          */
141         bool IsRevoked()
142         {
143                 ssl_data_iter ssldi = data.find("revoked");
144
145                 if (ssldi != data.end())
146                         return (ssldi->second == "1");
147                 else
148                         return false;
149         }
150 };
151
152 class ISHRequest : public Request
153 {
154  public:
155         InspSocket* Sock;
156
157         ISHRequest(Module* Me, Module* Target, const char* rtype, InspSocket* sock) : Request(Me, Target, rtype), Sock(sock)
158         {
159         }
160 };
161
162 class InspSocketAttachCertRequest : public ISHRequest
163 {
164  public:
165         /** Initialize the request as an attach cert message */
166         InspSocketAttachCertRequest(InspSocket* is, Module* Me, Module* Target) : ISHRequest(Me, Target, "IS_ATTACH", is)
167         {
168         }
169 };
170
171 class InspSocketHSCompleteRequest : public ISHRequest
172 {
173  public:
174         /** Initialize the request as a 'handshake complete?' message */
175         InspSocketHSCompleteRequest(InspSocket* is, Module* Me, Module* Target) : ISHRequest(Me, Target, "IS_HSDONE", is)
176         {
177         }
178 };
179
180 class InspSocketHookRequest : public ISHRequest
181 {
182  public:
183         /** Initialize request as a hook message */
184         InspSocketHookRequest(InspSocket* is, Module* Me, Module* Target) : ISHRequest(Me, Target, "IS_HOOK", is)
185         {
186         }
187 };
188
189 class InspSocketUnhookRequest : public ISHRequest
190 {
191  public:
192         /** Initialize request as an unhook message */
193         InspSocketUnhookRequest(InspSocket* is, Module* Me, Module* Target) : ISHRequest(Me, Target, "IS_UNHOOK", is)
194         {
195         }
196 };
197
198 class InspSocketNameRequest : public ISHRequest
199 {
200  public:
201         /** Initialize request as a get name message */
202         InspSocketNameRequest(Module* Me, Module* Target) : ISHRequest(Me, Target, "IS_NAME", NULL)
203         {
204         }
205 };
206
207 #endif
208