Fix #includes and copyright statements.
[fides] / lib / fides.h
1 /* fides.h - Light-weight, decentralised trust and authorisation management
2    Copyright (C) 2008-2009  Guus Sliepen <guus@tinc-vpn.org>
3   
4    Fides is free software; you can redistribute it and/or modify
5    it under the terms of the GNU Lesser General Public License as
6    published by the Free Software Foundation; either version 2.1 of
7    the License, or (at your option) any later version.
8   
9    Fides is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12    GNU Lesser General Public License for more details.
13   
14    You should have received a copy of the GNU Lesser General Public
15    License along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifndef __FIDES_H__
19 #define __FIDES_H__
20
21 #include <stdexcept>
22 #include <regex.h>
23 #include <botan/botan.h>
24 #include <botan/ecdsa.h>
25 #include <sys/time.h>
26 #include <map>
27
28 class fides {
29         std::string homedir;
30         std::string certdir;
31         std::string obsoletedir;
32         std::string keydir;
33
34         bool firstrun;
35         struct timeval latest;
36         static Botan::AutoSeeded_RNG rng;
37
38         public:
39         // Utility functions
40
41         static std::string b64encode(const std::string &in);
42         static std::string b64decode(const std::string &in);
43         static std::string hexencode(const std::string &in);
44         static std::string hexdecode(const std::string &in);
45
46         class regexp {
47                 regex_t comp;
48
49                 public:
50                 static const int EXTENDED = REG_EXTENDED;
51                 static const int ICASE = REG_ICASE;
52                 static const int NOSUB = REG_NOSUB;
53                 static const int NEWLINE = REG_NEWLINE;
54
55                 static const int NOTBOL = REG_NOTBOL;
56                 static const int NOTEAL = REG_NOTEOL;
57
58                 regexp(const std::string &exp, int cflags = 0) {
59                         int err = regcomp(&comp, exp.c_str(), cflags | NOSUB);
60                         if(err)
61                                 throw exception("Could not compile regular expression");
62                 }
63
64                 ~regexp() {
65                         regfree(&comp);
66                 }
67
68                 bool match(const std::string &in, int eflags = 0) {
69                         return regexec(&comp, in.c_str(), 0, 0, eflags) == 0;
70                 }
71         };
72
73         // Exception class
74
75         class exception: public std::runtime_error {
76                 public:
77                 exception(const std::string reason): runtime_error(reason) {}
78         };
79
80         // Objects manipulated by fides
81
82         class publickey {
83                 protected:
84                 Botan::ECDSA_PublicKey *pub;
85
86                 public:
87                 publickey();
88                 ~publickey();
89
90                 int trust;
91                 void load(std::istream &in);
92                 void save(std::ostream &out);
93                 void load(const std::string &filename);
94                 void save(const std::string &filename);
95                 bool verify(const std::string &data, const std::string &signature);
96                 std::string to_string();
97                 void from_string(const std::string &in);
98                 std::string fingerprint(unsigned int bits = 64);
99         };
100
101         class privatekey: public publickey {
102                 Botan::ECDSA_PrivateKey *priv;
103
104                 public:
105                 privatekey();
106                 ~privatekey();
107
108                 void load_private(std::istream &in);
109                 void save_private(std::ostream &out);
110                 void load_private(const std::string &filename);
111                 void save_private(const std::string &filename);
112                 void generate(const std::string &field);
113                 void generate(unsigned int bits = 224);
114                 std::string sign(const std::string &data);
115         };
116
117         class certificate {
118                 friend class fides;
119                 publickey *signer;
120                 struct timeval timestamp;
121                 std::string statement;
122                 std::string signature;
123
124                 public:
125                 certificate(publickey *pub, struct timeval timestamp, const std::string &statement, const std::string &signature);
126                 certificate(privatekey *priv, struct timeval timestamp, const std::string &statement);
127
128                 std::string to_string() const;
129                 std::string fingerprint(unsigned int bits = 64);
130                 bool validate();
131         };
132
133         // Fides class itself
134
135         private:
136         privatekey mykey;
137         std::map<std::string, publickey *> keys;
138         std::map<std::string, certificate *> certs;
139         std::set<publickey *> trustedkeys;
140
141         void merge(certificate *cert);
142         void merge(publickey *key);
143
144         public:
145         fides(const std::string &homedir = "");
146         ~fides();
147
148         bool is_firstrun();
149         bool fsck();
150         std::string get_homedir();
151
152         void sign(const std::string &statement);
153
154         void allow(const std::string &statement, publickey *key = 0);
155         void dontcare(const std::string &statement, publickey *key = 0);
156         void deny(const std::string &statement, publickey *key = 0);
157         bool is_allowed(const std::string &statement, publickey *key = 0);
158         bool is_denied(const std::string &statement, publickey *key = 0);
159
160         void auth_stats(const std::string &statement, int &self, int &trusted, int &all);
161         void trust(publickey *key);
162         void dctrust(publickey *key);
163         void distrust(publickey *key);
164         bool is_trusted(publickey *key);
165         bool is_distrusted(publickey *key);
166         publickey *find_key(const std::string &fingerprint);
167         void update_trust();
168
169         std::vector<certificate *> find_certificates(publickey *key, const std::string &statement);
170         std::vector<certificate *> find_certificates(const std::string &statement);
171         std::vector<certificate *> find_certificates(publickey *key);
172
173         certificate *import_certificate(const std::string &certificate);
174         std::string export_certificate(const certificate *);
175
176         publickey *import_key(const std::string &key);
177         std::string export_key(const publickey *key);
178
179         void import_all(std::istream &in);
180         void export_all(std::ostream &out);
181
182         certificate *certificate_from_string(const std::string &certificate);
183         certificate *certificate_load(const std::string &filename);
184         void certificate_save(const certificate *cert, const std::string &filename);
185
186 };
187
188 #endif