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