Botan 3.5.0
Crypto and TLS for C&
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
17namespace Botan {
18
19class RandomNumberGenerator;
20
21namespace Roughtime {
22
23const unsigned request_min_size = 1024;
24
26 public:
27 explicit Roughtime_Error(std::string_view s) : Decoding_Error("Roughtime", s) {}
28
29 ErrorType error_type() const noexcept override { return ErrorType::RoughtimeError; }
30};
31
33 public:
34 Nonce() = default;
35 Nonce(const std::vector<uint8_t>& nonce);
37
38 Nonce(const std::array<uint8_t, 64>& nonce) { m_nonce = nonce; }
39
40 bool operator==(const Nonce& rhs) const { return m_nonce == rhs.m_nonce; }
41
42 const std::array<uint8_t, 64>& get_nonce() const { return m_nonce; }
43
44 private:
45 std::array<uint8_t, 64> m_nonce;
46};
47
48/**
49* An Roughtime request.
50*/
52std::array<uint8_t, request_min_size> encode_request(const Nonce& nonce);
53
54/**
55* An Roughtime response.
56*/
58 public:
59 using microseconds32 = std::chrono::duration<uint32_t, std::micro>;
60 using microseconds64 = std::chrono::duration<uint64_t, std::micro>;
61 using sys_microseconds64 = std::chrono::time_point<std::chrono::system_clock, microseconds64>;
62
63 static Response from_bits(const std::vector<uint8_t>& response, const Nonce& nonce);
64
65 bool validate(const Ed25519_PublicKey& pk) const;
66
67 sys_microseconds64 utc_midpoint() const { return m_utc_midpoint; }
68
69 microseconds32 utc_radius() const { return m_utc_radius; }
70
71 private:
72 Response(const std::array<uint8_t, 72>& dele,
73 const std::array<uint8_t, 64>& sig,
74 sys_microseconds64 utc_midp,
75 microseconds32 utc_radius) :
76 m_cert_dele(dele), m_cert_sig(sig), m_utc_midpoint{utc_midp}, m_utc_radius{utc_radius} {}
77
78 const std::array<uint8_t, 72> m_cert_dele;
79 const std::array<uint8_t, 64> m_cert_sig;
80 const sys_microseconds64 m_utc_midpoint;
81 const microseconds32 m_utc_radius;
82};
83
85 public:
86 Link(const std::vector<uint8_t>& response, const Ed25519_PublicKey& public_key, const Nonce& nonce_or_blind) :
87 m_response{response}, m_public_key{public_key}, m_nonce_or_blind{nonce_or_blind} {}
88
89 const std::vector<uint8_t>& response() const { return m_response; }
90
91 const Ed25519_PublicKey& public_key() const { return m_public_key; }
92
93 const Nonce& nonce_or_blind() const { return m_nonce_or_blind; }
94
95 Nonce& nonce_or_blind() { return m_nonce_or_blind; }
96
97 private:
98 std::vector<uint8_t> m_response;
99 Ed25519_PublicKey m_public_key;
100 Nonce m_nonce_or_blind;
101};
102
104 public:
105 Chain() = default; //empty
106 Chain(std::string_view str);
107
108 const std::vector<Link>& links() const { return m_links; }
109
110 std::vector<Response> responses() const;
111 Nonce next_nonce(const Nonce& blind) const;
112 void append(const Link& new_link, size_t max_chain_size);
113 std::string to_string() const;
114
115 private:
116 std::vector<Link> m_links;
117};
118
119/**
120*/
121BOTAN_PUBLIC_API(2, 13)
122Nonce nonce_from_blind(const std::vector<uint8_t>& previous_response, const Nonce& blind);
123
124/**
125* Makes an online Roughtime request via UDP and returns the Roughtime response.
126* @param url Roughtime server UDP endpoint (host:port)
127* @param nonce the nonce to send to the server
128* @param timeout a timeout on the UDP request
129* @return Roughtime response
130*/
131BOTAN_PUBLIC_API(2, 13)
132std::vector<uint8_t> online_request(std::string_view url,
133 const Nonce& nonce,
134 std::chrono::milliseconds timeout = std::chrono::seconds(3));
135
137 public:
138 Server_Information(std::string_view name,
139 const Ed25519_PublicKey& public_key,
140 const std::vector<std::string>& addresses) :
141 m_name{name}, m_public_key{public_key}, m_addresses{addresses} {}
142
143 const std::string& name() const { return m_name; }
144
145 const Ed25519_PublicKey& public_key() const { return m_public_key; }
146
147 const std::vector<std::string>& addresses() const { return m_addresses; }
148
149 private:
150 std::string m_name;
151 Ed25519_PublicKey m_public_key;
152 std::vector<std::string> m_addresses;
153};
154
155BOTAN_PUBLIC_API(2, 13)
156std::vector<Server_Information> servers_from_str(std::string_view str);
157
158} // namespace Roughtime
159} // namespace Botan
160
161#endif
const std::vector< Link > & links() const
Definition roughtime.h:108
const std::array< uint8_t, 64 > & get_nonce() const
Definition roughtime.h:42
Nonce(const std::array< uint8_t, 64 > &nonce)
Definition roughtime.h:38
bool operator==(const Nonce &rhs) const
Definition roughtime.h:40
std::chrono::time_point< std::chrono::system_clock, microseconds64 > sys_microseconds64
Definition roughtime.h:61
microseconds32 utc_radius() const
Definition roughtime.h:69
sys_microseconds64 utc_midpoint() const
Definition roughtime.h:67
std::chrono::duration< uint32_t, std::micro > microseconds32
Definition roughtime.h:59
std::chrono::duration< uint64_t, std::micro > microseconds64
Definition roughtime.h:60
ErrorType error_type() const noexcept override
Definition roughtime.h:29
Roughtime_Error(std::string_view s)
Definition roughtime.h:27
std::string name
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
std::vector< Server_Information > servers_from_str(std::string_view str)
std::vector< uint8_t > online_request(std::string_view uri, const Nonce &nonce, std::chrono::milliseconds timeout)
Nonce nonce_from_blind(const std::vector< uint8_t > &previous_response, const Nonce &blind)
std::array< uint8_t, request_min_size > encode_request(const Nonce &nonce)
const unsigned request_min_size
Definition roughtime.h:23
ErrorType
Definition exceptn.h:20
const std::string & name() const
Definition roughtime.h:143
const Ed25519_PublicKey & public_key() const
Definition roughtime.h:145
const std::vector< std::string > & addresses() const
Definition roughtime.h:147
Server_Information(std::string_view name, const Ed25519_PublicKey &public_key, const std::vector< std::string > &addresses)
Definition roughtime.h:138