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