Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

roughtime.h
Go to the documentation of this file.
1 /*
2 * Roughtime
3 * (C) 2019 Nuno Goncalves <nunojpg@gmail.com>
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #ifndef BOTAN_ROUGHTIME_H_
9 #define BOTAN_ROUGHTIME_H_
10
11 #include <array>
12 #include <chrono>
13 #include <vector>
14
15 #include <botan/ed25519.h>
16
17 namespace Botan {
18
19 class RandomNumberGenerator;
20
21 namespace Roughtime {
22
23 const unsigned request_min_size = 1024;
24
25 class BOTAN_PUBLIC_API(2, 13) Roughtime_Error final : public Decoding_Error
26 {
27 public:
28 explicit Roughtime_Error(const std::string& s) : Decoding_Error("Roughtime " + s) {}
29 ErrorType error_type() const noexcept override { return ErrorType::RoughtimeError; }
30 };
31
32 class BOTAN_PUBLIC_API(2, 13) Nonce final
33 {
34 public:
35 Nonce() = default;
36 Nonce(const std::vector<uint8_t>& nonce);
37 Nonce(RandomNumberGenerator& rng);
38 Nonce(const std::array<uint8_t, 64>& nonce)
39 {
40 m_nonce = nonce;
41 }
42 bool operator==(const Nonce& rhs) const { return m_nonce == rhs.m_nonce; }
43 const std::array<uint8_t, 64>& get_nonce() const { return m_nonce; }
44 private:
45 std::array<uint8_t, 64> m_nonce;
46 };
47
48
49 /**
50 * An Roughtime request.
51 */
52 BOTAN_PUBLIC_API(2, 13)
53 std::array<uint8_t, request_min_size> encode_request(const Nonce& nonce);
54
55 /**
56 * An Roughtime response.
57 */
58 class BOTAN_PUBLIC_API(2, 13) Response final
59 {
60 public:
61 using microseconds32 = std::chrono::duration<uint32_t, std::micro>;
62 using microseconds64 = std::chrono::duration<uint64_t, std::micro>;
63 using sys_microseconds64 = std::chrono::time_point<std::chrono::system_clock, microseconds64>;
64
65 static Response from_bits(const std::vector<uint8_t>& response, const Nonce& nonce);
66
67 bool validate(const Ed25519_PublicKey& pk) const;
68
69 sys_microseconds64 utc_midpoint() const { return m_utc_midpoint; }
70
71 microseconds32 utc_radius() const { return m_utc_radius; }
72 private:
73 Response(const std::array<uint8_t, 72>& dele,
74 const std::array<uint8_t, 64>& sig,
75 sys_microseconds64 utc_midp,
76 microseconds32 utc_radius)
77 : m_cert_dele(dele)
78 , m_cert_sig(sig)
79 , m_utc_midpoint {utc_midp}
80 , m_utc_radius {utc_radius}
81 {}
82 const std::array<uint8_t, 72> m_cert_dele;
83 const std::array<uint8_t, 64> m_cert_sig;
84 const sys_microseconds64 m_utc_midpoint;
85 const microseconds32 m_utc_radius;
86 };
87
88 class BOTAN_PUBLIC_API(2, 13) Link final
89 {
90 public:
91 Link(const std::vector<uint8_t>& response,
92 const Ed25519_PublicKey& public_key,
93 const Nonce& nonce_or_blind)
94 : m_response{response}
95 , m_public_key{public_key}
96 , m_nonce_or_blind{nonce_or_blind}
97 {}
98 const std::vector<uint8_t>& response() const { return m_response; }
99 const Ed25519_PublicKey& public_key() const { return m_public_key; }
100 const Nonce& nonce_or_blind() const { return m_nonce_or_blind; }
101 Nonce& nonce_or_blind() { return m_nonce_or_blind; }
102
103 private:
104 std::vector<uint8_t> m_response;
105 Ed25519_PublicKey m_public_key;
106 Nonce m_nonce_or_blind;
107 };
108
109 class BOTAN_PUBLIC_API(2, 13) Chain final
110 {
111 public:
112 Chain() = default; //empty
113 Chain(const std::string& str);
114 const std::vector<Link>& links() const { return m_links; }
115 std::vector<Response> responses() const;
116 Nonce next_nonce(const Nonce& blind) const;
117 void append(const Link& new_link, size_t max_chain_size);
118 std::string to_string() const;
119 private:
120 std::vector<Link> m_links;
121 };
122
123 /**
124 */
125 BOTAN_PUBLIC_API(2, 13)
126 Nonce nonce_from_blind(const std::vector<uint8_t>& previous_response,
127 const Nonce& blind);
128
129 /**
130 * Makes an online Roughtime request via UDP and returns the Roughtime response.
131 * @param url Roughtime server UDP endpoint (host:port)
132 * @param nonce the nonce to send to the server
133 * @param timeout a timeout on the UDP request
134 * @return Roughtime response
135 */
136 BOTAN_PUBLIC_API(2, 13)
137 std::vector<uint8_t> online_request(const std::string& url,
138 const Nonce& nonce,
139 std::chrono::milliseconds timeout = std::chrono::seconds(3));
140
141 struct BOTAN_PUBLIC_API(2, 13) Server_Information final
142 {
143 public:
144 Server_Information(const std::string& name,
145 const Botan::Ed25519_PublicKey& public_key,
146 const std::vector<std::string>& addresses)
147 : m_name { name }
148 , m_public_key { public_key }
149 , m_addresses { addresses }
150 {}
151 const std::string& name() const {return m_name;}
152 const Botan::Ed25519_PublicKey& public_key() const {return m_public_key;}
153 const std::vector<std::string>& addresses() const {return m_addresses;}
154
155 private:
156 std::string m_name;
157 Botan::Ed25519_PublicKey m_public_key;
158 std::vector<std::string> m_addresses;
159 };
160
161 BOTAN_PUBLIC_API(2, 13)
162 std::vector<Server_Information> servers_from_str(const std::string& str);
163
164 }
165 }
166
167 #endif
Botan::Roughtime::Nonce::operator==
bool operator==(const Nonce &rhs) const
Definition: roughtime.h:42
m_name
std::string m_name
Definition: openssl_hash.cpp:97
Botan::Roughtime::Response::utc_radius
microseconds32 utc_radius() const
Definition: roughtime.h:71
Botan::Ed25519_PublicKey
Definition: ed25519.h:18
Botan::Roughtime::Nonce::get_nonce
const std::array< uint8_t, 64 > & get_nonce() const
Definition: roughtime.h:43
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan::Roughtime::Link::nonce_or_blind
const Nonce & nonce_or_blind() const
Definition: roughtime.h:100
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:28
BOTAN_PUBLIC_API
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Botan::Roughtime::nonce_from_blind
Nonce nonce_from_blind(const std::vector< uint8_t > &previous_response, const Nonce &blind)
Definition: roughtime.cpp:237
Botan::Roughtime::request_min_size
const unsigned request_min_size
Definition: roughtime.h:23
std
Definition: bigint.h:1135
Botan::Roughtime::Response::microseconds32
std::chrono::duration< uint32_t, std::micro > microseconds32
Definition: roughtime.h:61
Botan::Roughtime::encode_request
std::array< uint8_t, request_min_size > encode_request(const Nonce &nonce)
Definition: roughtime.cpp:169
Botan::Roughtime::Nonce
Definition: roughtime.h:32
Botan::to_string
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11
Botan::Roughtime::Link::Link
Link(const std::vector< uint8_t > &response, const Ed25519_PublicKey &public_key, const Nonce &nonce_or_blind)
Definition: roughtime.h:91
Botan::Decoding_Error
Definition: exceptn.h:199
Botan::Roughtime::Link
Definition: roughtime.h:88
Botan::Roughtime::servers_from_str
std::vector< Server_Information > servers_from_str(const std::string &str)
Definition: roughtime.cpp:393
Botan::Roughtime::Server_Information::public_key
const Botan::Ed25519_PublicKey & public_key() const
Definition: roughtime.h:152
Botan::Roughtime::Link::nonce_or_blind
Nonce & nonce_or_blind()
Definition: roughtime.h:101
name
std::string name
Definition: commoncrypto_hash.cpp:23
Botan::ErrorType
ErrorType
Definition: exceptn.h:20
Botan::Roughtime::Response
Definition: roughtime.h:58
Botan::Roughtime::Server_Information
Definition: roughtime.h:141
Botan::Roughtime::Nonce::Nonce
Nonce(const std::array< uint8_t, 64 > &nonce)
Definition: roughtime.h:38
Botan
Definition: alg_id.cpp:13
Botan::Roughtime::Roughtime_Error
Definition: roughtime.h:25
Botan::Roughtime::Response::utc_midpoint
sys_microseconds64 utc_midpoint() const
Definition: roughtime.h:69
Botan::Roughtime::Link::public_key
const Ed25519_PublicKey & public_key() const
Definition: roughtime.h:99
Botan::Roughtime::Response::microseconds64
std::chrono::duration< uint64_t, std::micro > microseconds64
Definition: roughtime.h:62
Botan::Roughtime::online_request
std::vector< uint8_t > online_request(const std::string &uri, const Nonce &nonce, std::chrono::milliseconds timeout)
Definition: roughtime.cpp:363
Botan::ErrorType::RoughtimeError
Botan::Roughtime::Response::sys_microseconds64
std::chrono::time_point< std::chrono::system_clock, microseconds64 > sys_microseconds64
Definition: roughtime.h:63
Botan::Roughtime::Server_Information::addresses
const std::vector< std::string > & addresses() const
Definition: roughtime.h:153
Botan::Roughtime::Server_Information::Server_Information
Server_Information(const std::string &name, const Botan::Ed25519_PublicKey &public_key, const std::vector< std::string > &addresses)
Definition: roughtime.h:144
Botan::Roughtime::Chain::links
const std::vector< Link > & links() const
Definition: roughtime.h:114
Botan::Roughtime::Link::response
const std::vector< uint8_t > & response() const
Definition: roughtime.h:98
Botan::Roughtime::Chain
Definition: roughtime.h:109
Botan::Roughtime::Server_Information::name
const std::string & name() const
Definition: roughtime.h:151
Botan::Roughtime::Roughtime_Error::error_type
ErrorType error_type() const noexceptoverride
Definition: roughtime.h:29
Botan::Roughtime::Roughtime_Error::Roughtime_Error
Roughtime_Error(const std::string &s)
Definition: roughtime.h:28