]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/ssl_cert.h
Add comments to document this header
[user/henk/code/inspircd.git] / src / modules / extra / ssl_cert.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 #endif
153