8#include <botan/roughtime.h>
10#include <botan/base64.h>
11#include <botan/hash.h>
12#include <botan/pubkey.h>
14#include <botan/internal/socket_udp.h>
28struct is_array : std::false_type {};
30template <
class T, std::
size_t N>
31struct is_array<std::array<T, N>> : std::true_type {};
34T impl_from_little_endian(
const uint8_t* t,
const size_t i)
35 requires(
sizeof(
T) <=
sizeof(int64_t))
37 return T(
static_cast<int64_t
>(t[i]) << i * 8) + (i == 0 ?
T(0) : impl_from_little_endian<
T>(t, i - 1));
41T from_little_endian(
const uint8_t* t) {
42 return impl_from_little_endian<T>(t,
sizeof(
T) - 1);
46T copy(
const uint8_t* t)
47 requires(is_array<T>::value)
53T copy(
const uint8_t* t)
54 requires(!is_array<T>::value)
57 return from_little_endian<T>(t);
61std::map<std::string, std::vector<uint8_t>> unpack_roughtime_packet(
T bytes) {
62 if(bytes.size() < 8) {
63 throw Roughtime::Roughtime_Error(
"Map length is under minimum of 8 bytes");
65 const auto buf = bytes.data();
66 const uint32_t num_tags = buf[0];
67 const uint32_t start_content = num_tags * 8;
68 if(start_content > bytes.size()) {
69 throw Roughtime::Roughtime_Error(
"Map length too small to contain all tags");
71 uint32_t start = start_content;
72 std::map<std::string, std::vector<uint8_t>> tags;
73 for(uint32_t i = 0; i < num_tags; ++i) {
75 ((i + 1) == num_tags) ? bytes.size() : start_content + from_little_endian<uint32_t>(buf + 4 + i * 4);
76 if(end > bytes.size()) {
77 throw Roughtime::Roughtime_Error(
"Tag end index out of bounds");
80 throw Roughtime::Roughtime_Error(
"Tag offset must be more than previous tag offset");
83 const char label[] = {label_ptr[0], label_ptr[1], label_ptr[2], label_ptr[3], 0};
84 auto ret = tags.emplace(label, std::vector<uint8_t>(buf + start, buf + end));
86 throw Roughtime::Roughtime_Error(std::string(
"Map has duplicated tag: ") + label);
88 start =
static_cast<uint32_t
>(end);
94T get(
const std::map<std::string, std::vector<uint8_t>>& map,
const std::string& label) {
95 const auto& tag = map.find(label);
96 if(tag == map.end()) {
97 throw Roughtime::Roughtime_Error(
"Tag " + label +
" not found");
99 if(tag->second.size() !=
sizeof(
T)) {
100 throw Roughtime::Roughtime_Error(
"Tag " + label +
" has unexpected size");
102 return copy<T>(tag->second.data());
105const std::vector<uint8_t>& get_v(
const std::map<std::string, std::vector<uint8_t>>& map,
const std::string& label) {
106 const auto& tag = map.find(label);
107 if(tag == map.end()) {
108 throw Roughtime::Roughtime_Error(
"Tag " + label +
" not found");
114 const std::vector<uint8_t>& payload,
115 const std::array<uint8_t, 64>& signature) {
116 const char context[] =
"RoughTime v1 response signature";
117 Ed25519_PublicKey key(std::vector<uint8_t>(pk.data(), pk.data() + pk.size()));
118 PK_Verifier verifier(key,
"Pure");
120 verifier.update(payload);
121 return verifier.check_signature(signature.data(), signature.size());
124std::array<uint8_t, 64> hashLeaf(
const std::array<uint8_t, 64>& leaf) {
125 std::array<uint8_t, 64> ret{};
128 hash->update(leaf.data(), leaf.size());
129 hash->final(ret.data());
133void hashNode(std::array<uint8_t, 64>& hash,
const std::array<uint8_t, 64>& node,
bool reverse) {
137 h->update(node.data(), node.size());
138 h->update(hash.data(), hash.size());
140 h->update(hash.data(), hash.size());
141 h->update(node.data(), node.size());
143 h->final(hash.data());
146template <
size_t N,
typename T>
147std::array<uint8_t, N> vector_to_array(std::vector<uint8_t, T> vec) {
148 if(vec.size() != N) {
149 throw std::logic_error(
"Invalid vector size");
158 if(nonce.size() != 64) {
165 rng.
randomize(m_nonce.data(), m_nonce.size());
169 std::array<uint8_t, request_min_size> buf = {{2, 0, 0, 0, 64, 0, 0, 0,
'N',
'O',
'N',
'C',
'P',
'A',
'D', 0xff}};
171 std::memset(buf.data() + 16 + nonce.
get_nonce().size(), 0, buf.size() - 16 - nonce.
get_nonce().size());
176 const auto response_v = unpack_roughtime_packet(response);
177 const auto cert = unpack_roughtime_packet(get_v(response_v,
"CERT"));
178 const auto cert_dele = get<std::array<uint8_t, 72>>(cert,
"DELE");
179 const auto cert_sig = get<std::array<uint8_t, 64>>(cert,
"SIG");
180 const auto cert_dele_v = unpack_roughtime_packet(cert_dele);
181 const auto srep = get_v(response_v,
"SREP");
182 const auto srep_v = unpack_roughtime_packet(srep);
184 const auto cert_dele_pubk = get<std::array<uint8_t, 32>>(cert_dele_v,
"PUBK");
185 const auto sig = get<std::array<uint8_t, 64>>(response_v,
"SIG");
190 const auto indx = get<uint32_t>(response_v,
"INDX");
191 const auto path = get_v(response_v,
"PATH");
192 const auto srep_root = get<std::array<uint8_t, 64>>(srep_v,
"ROOT");
193 const size_t size = path.size();
194 const size_t levels = size / 64;
197 throw Roughtime_Error(
"Merkle tree path size must be multiple of 64 bytes");
199 if(indx >= (1U << levels)) {
206 while(level < levels) {
207 hashNode(hash,
typecast_copy<std::array<uint8_t, 64>>(path.data() + level * 64), index & 1);
212 if(srep_root != hash) {
216 const auto cert_dele_maxt =
sys_microseconds64(get<microseconds64>(cert_dele_v,
"MAXT"));
217 const auto cert_dele_mint =
sys_microseconds64(get<microseconds64>(cert_dele_v,
"MINT"));
219 const auto srep_radi = get<microseconds32>(srep_v,
"RADI");
220 if(srep_midp < cert_dele_mint) {
223 if(srep_midp > cert_dele_maxt) {
226 return {cert_dele, cert_sig, srep_midp, srep_radi};
230 const char context[] =
"RoughTime v1 delegation signature--";
233 verifier.
update(m_cert_dele.data(), m_cert_dele.size());
238 std::array<uint8_t, 64> ret{};
239 const auto blind_arr = blind.
get_nonce();
241 hash->update(previous_response);
242 hash->update(hash->final());
243 hash->update(blind_arr.data(), blind_arr.size());
244 hash->final(ret.data());
250 std::istringstream ss{std::string(str)};
251 const std::string ERROR_MESSAGE =
"Line does not have 4 space separated fields";
252 for(std::string s; std::getline(ss, s);) {
253 size_t start = 0, end = 0;
254 end = s.find(
' ', start);
255 if(end == std::string::npos) {
258 const auto publicKeyType = s.substr(start, end - start);
259 if(publicKeyType !=
"ed25519") {
264 end = s.find(
' ', start);
265 if(end == std::string::npos) {
271 end = s.find(
' ', start);
272 if(end == std::string::npos) {
275 if((end - start) != 88) {
278 const auto vec =
base64_decode(s.substr(start, end - start));
279 const auto nonceOrBlind =
Nonce(vector_to_array<64>(
base64_decode(s.substr(start, end - start))));
282 end = s.find(
' ', start);
283 if(end != std::string::npos) {
288 m_links.push_back({response, serverPublicKey, nonceOrBlind});
294 for(
unsigned i = 0; i < m_links.size(); ++i) {
295 const auto& l = m_links[i];
296 const auto nonce = i ?
nonce_from_blind(m_links[i - 1].response(), l.nonce_or_blind()) : l.nonce_or_blind();
298 if(!response.validate(l.public_key())) {
307 return m_links.empty() ? blind :
nonce_from_blind(m_links.back().response(), blind);
311 if(max_chain_size <= 0) {
315 while(m_links.size() >= max_chain_size) {
316 if(m_links.size() == 1) {
317 auto new_link_updated = new_link;
321 m_links.push_back(new_link_updated);
324 if(m_links.size() >= 2) {
325 m_links[1].nonce_or_blind() =
328 m_links.erase(m_links.begin());
330 m_links.push_back(new_link);
335 s.reserve((7 + 1 + 88 + 1 + 44 + 1 + 480) * m_links.size());
336 for(
const auto& link : m_links) {
341 s +=
base64_encode(link.nonce_or_blind().get_nonce().data(), link.nonce_or_blind().get_nonce().size());
349std::vector<uint8_t>
online_request(std::string_view uri,
const Nonce& nonce, std::chrono::milliseconds timeout) {
350 const std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now();
357 socket->write(encoded.data(), encoded.size());
359 if(std::chrono::system_clock::now() - start_time > timeout) {
363 std::vector<uint8_t> buffer;
364 buffer.resize(360 + 64 * 10 + 1);
366 const auto n = socket->read(buffer.data(), buffer.size());
368 if(!n || std::chrono::system_clock::now() - start_time > timeout) {
372 if(n == buffer.size()) {
381 std::vector<Server_Information> servers;
382 std::istringstream ss{std::string(str)};
384 const std::string ERROR_MESSAGE =
"Line does not have at least 5 space separated fields";
385 for(std::string s; std::getline(ss, s);) {
386 size_t start = 0, end = 0;
387 end = s.find(
' ', start);
388 if(end == std::string::npos) {
391 const auto name = s.substr(start, end - start);
394 end = s.find(
' ', start);
395 if(end == std::string::npos) {
398 const auto publicKeyType = s.substr(start, end - start);
399 if(publicKeyType !=
"ed25519") {
404 end = s.find(
' ', start);
406 if(end == std::string::npos) {
409 const auto publicKeyBase64 = s.substr(start, end - start);
413 end = s.find(
' ', start);
414 if(end == std::string::npos) {
417 const auto protocol = s.substr(start, end - start);
418 if(protocol !=
"udp") {
422 const auto addresses = [&]() {
423 std::vector<std::string> addr;
426 end = s.find(
' ', start);
427 const auto address = s.substr(start, (end == std::string::npos) ? std::string::npos : end - start);
428 if(address.empty()) {
431 addr.push_back(address);
432 if(end == std::string::npos) {
437 if(addresses.empty()) {
441 servers.push_back({
name, publicKey, addresses});
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
bool check_signature(const uint8_t sig[], size_t length)
void randomize(std::span< uint8_t > output)
void append(const Link &new_link, size_t max_chain_size)
std::string to_string() const
Nonce next_nonce(const Nonce &blind) const
std::vector< Response > responses() const
const Nonce & nonce_or_blind() const
const std::array< uint8_t, 64 > & get_nonce() const
std::chrono::time_point< std::chrono::system_clock, microseconds64 > sys_microseconds64
static Response from_bits(const std::vector< uint8_t > &response, const Nonce &nonce)
bool validate(const Ed25519_PublicKey &pk) const
std::unique_ptr< SocketUDP > BOTAN_TEST_API open_socket_udp(std::string_view hostname, std::string_view service, std::chrono::microseconds timeout)
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
bool verify_signature(std::span< const uint8_t, ED448_LEN > pk, bool phflag, std::span< const uint8_t > context, std::span< const uint8_t > sig, std::span< const uint8_t > msg)
Verify a signature(RFC 8032 5.2.7)
constexpr void typecast_copy(ToR &&out, FromR &&in)
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
size_t base64_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
std::vector< T > unlock(const secure_vector< T > &in)
const char * cast_uint8_ptr_to_char(const uint8_t *b)
const uint8_t * cast_char_ptr_to_uint8(const char *s)