1 /* fides.cc - Light-weight, decentralised trust and authorisation management
2 Copyright (C) 2008-2009 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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/>.
21 #include <sys/types.h>
25 #include <botan/types.h>
26 #include <botan/botan.h>
27 #include <botan/ecdsa.h>
28 #include <botan/look_pk.h>
29 #include <botan/lookup.h>
30 #include <botan/filters.h>
31 #include <botan/sha2_32.h>
37 #define FIDES_DEBUG false
40 #define debug if(FIDES_DEBUG)
45 // Base64 and hex encoding/decoding functions
47 /// Hexadecimal encode data.
49 /// @param in A string containing raw data.
50 /// @return A string containing the hexadecimal encoded data.
51 string hexencode(const std::string &in) {
52 Botan::Pipe pipe(new Botan::Hex_Encoder);
53 pipe.process_msg((Botan::byte *)in.data(), in.size());
54 return pipe.read_all_as_string();
57 /// Decode hexadecimal data.
59 /// @param in A string containing hexadecimal encoded data.
60 /// @return A string containing the raw data.
61 string hexdecode(const std::string &in) {
62 Botan::Pipe pipe(new Botan::Hex_Decoder);
63 pipe.process_msg((Botan::byte *)in.data(), in.size());
64 return pipe.read_all_as_string();
67 /// Base-64 encode data.
69 /// @param in A string containing raw data.
70 /// @return A string containing the base-64 encoded data.
71 string b64encode(const std::string &in) {
72 Botan::Pipe pipe(new Botan::Base64_Encoder);
73 pipe.process_msg((Botan::byte *)in.data(), in.size());
74 return pipe.read_all_as_string();
77 /// Decode base-64 data.
79 /// @param in A string containing base-64 encoded data.
80 /// @return A string containing the raw data.
81 string b64decode(const std::string &in) {
82 Botan::Pipe pipe(new Botan::Base64_Decoder);
83 pipe.process_msg((Botan::byte *)in.data(), in.size());
84 return pipe.read_all_as_string();
87 /// Return the names of all the files in a directory.
89 /// @param path Directory to search for files.
90 /// @return A vector of strings with the name of each file in the directory.
91 /// The filename does not contain the leading path.
92 vector<string> dirlist(const std::string &path) {
95 DIR *dir = opendir(path.c_str());
99 struct dirent entry, *result = &entry;
102 readdir_r(dir, &entry, &result);
106 if(result->d_type == DT_UNKNOWN) {
107 if(stat((path + "/" + result->d_name).c_str(), &st))
109 if(S_ISREG(st.st_mode))
110 files.push_back(result->d_name);
111 } else if(result->d_type == DT_REG) {
112 files.push_back(result->d_name);