8#include <botan/roughtime.h>
10#include <botan/base64.h>
11#include <botan/hash.h>
12#include <botan/mem_ops.h>
13#include <botan/pubkey.h>
15#include <botan/internal/buffer_slicer.h>
16#include <botan/internal/int_utils.h>
17#include <botan/internal/socket_udp.h>
30struct is_array : std::false_type {};
32template <
class T, std::
size_t N>
33struct is_array<std::array<T, N>> : std::true_type {};
36T impl_from_little_endian(
const uint8_t* t,
const size_t i)
37 requires(
sizeof(T) <=
sizeof(int64_t))
39 return T(
static_cast<int64_t
>(t[i]) << i * 8) + (i == 0 ? T(0) : impl_from_little_endian<T>(t, i - 1));
43T from_little_endian(
const uint8_t* t) {
44 return impl_from_little_endian<T>(t,
sizeof(T) - 1);
48T copy(
const uint8_t* t)
49 requires(is_array<T>::value)
55T copy(
const uint8_t* t)
56 requires(!is_array<T>::value)
59 return from_little_endian<T>(t);
63std::map<std::string, std::vector<uint8_t>> unpack_roughtime_packet(T bytes) {
64 if(bytes.size() < 8) {
67 const auto buf = bytes.data();
68 const uint32_t num_tags = buf[0];
69 const uint32_t start_content = num_tags * 8;
70 if(start_content > bytes.size()) {
73 uint32_t start = start_content;
74 std::map<std::string, std::vector<uint8_t>> tags;
75 for(uint32_t i = 0; i < num_tags; ++i) {
76 size_t end = bytes.size();
77 if((i + 1) != num_tags) {
78 const auto tag_end =
checked_add(start_content, from_little_endian<uint32_t>(buf + 4 + i * 4));
79 if(!tag_end.has_value()) {
82 end = tag_end.value();
84 if(end > bytes.size()) {
91 const char label[] = {label_ptr[0], label_ptr[1], label_ptr[2], label_ptr[3], 0};
92 auto ret = tags.emplace(label, std::vector<uint8_t>(buf + start, buf + end));
96 start =
static_cast<uint32_t
>(end);
102T get(
const std::map<std::string, std::vector<uint8_t>>& map,
const std::string& label) {
103 const auto& tag = map.find(label);
104 if(tag == map.end()) {
107 if(tag->second.size() !=
sizeof(T)) {
110 return copy<T>(tag->second.data());
113const std::vector<uint8_t>& get_v(
const std::map<std::string, std::vector<uint8_t>>& map,
const std::string& label) {
114 const auto& tag = map.find(label);
115 if(tag == map.end()) {
122 const std::vector<uint8_t>& payload,
123 const std::array<uint8_t, 64>& signature) {
124 constexpr std::string_view context(
"RoughTime v1 response signature\0", 32);
125 const Ed25519_PublicKey key(std::vector<uint8_t>(pk.data(), pk.data() + pk.size()));
127 verifier.update(context);
128 verifier.update(payload);
129 return verifier.check_signature(signature.data(), signature.size());
132std::array<uint8_t, 64> hashLeaf(
const std::array<uint8_t, 64>& leaf) {
133 std::array<uint8_t, 64> ret{};
136 hash->update(leaf.data(), leaf.size());
137 hash->final(ret.data());
141void hashNode(std::span<uint8_t, 64> hash, std::span<const uint8_t, 64> node,
bool reverse) {
145 h->update(node.data(), node.size());
146 h->update(hash.data(), hash.size());
148 h->update(hash.data(), hash.size());
149 h->update(node.data(), node.size());
151 h->final(hash.data());
154template <
size_t N,
typename T>
155std::array<uint8_t, N> vector_to_array(std::vector<uint8_t, T> vec) {
156 if(vec.size() != N) {
157 throw std::logic_error(
"Invalid vector size");
166 if(nonce.size() != 64) {
175 std::array<uint8_t, request_min_size> buf = {{2, 0, 0, 0, 64, 0, 0, 0,
'N',
'O',
'N',
'C',
'P',
'A',
'D', 0xff}};
177 std::memset(buf.data() + 16 + nonce.
get_nonce().size(), 0, buf.size() - 16 - nonce.
get_nonce().size());
182 const auto response_v = unpack_roughtime_packet(response);
183 const auto cert = unpack_roughtime_packet(get_v(response_v,
"CERT"));
184 const auto cert_dele = get<std::array<uint8_t, 72>>(cert,
"DELE");
185 const auto cert_sig = get<std::array<uint8_t, 64>>(cert,
"SIG");
186 const auto cert_dele_v = unpack_roughtime_packet(cert_dele);
187 const auto srep = get_v(response_v,
"SREP");
188 const auto srep_v = unpack_roughtime_packet(srep);
190 const auto cert_dele_pubk = get<std::array<uint8_t, 32>>(cert_dele_v,
"PUBK");
191 const auto sig = get<std::array<uint8_t, 64>>(response_v,
"SIG");
196 const auto indx = get<uint32_t>(response_v,
"INDX");
197 const auto path = get_v(response_v,
"PATH");
198 const auto srep_root = get<std::array<uint8_t, 64>>(srep_v,
"ROOT");
199 const size_t size = path.size();
200 const size_t levels = size / 64;
203 throw Roughtime_Error(
"Merkle tree path size must be multiple of 64 bytes");
205 if(levels >= 32 || indx >= (uint32_t(1) << levels)) {
212 for(std::size_t level = 0; level < levels; ++level) {
213 hashNode(hash, slicer.
take<64>(), index % 2 == 1);
217 if(srep_root != hash) {
221 const auto cert_dele_maxt =
sys_microseconds64(get<microseconds64>(cert_dele_v,
"MAXT"));
222 const auto cert_dele_mint =
sys_microseconds64(get<microseconds64>(cert_dele_v,
"MINT"));
224 const auto srep_radi = get<microseconds32>(srep_v,
"RADI");
225 if(srep_midp < cert_dele_mint) {
228 if(srep_midp > cert_dele_maxt) {
231 return {cert_dele, cert_sig, srep_midp, srep_radi};
235 constexpr std::string_view context(
"RoughTime v1 delegation signature--\0", 36);
238 verifier.
update(m_cert_dele.data(), m_cert_dele.size());
243 std::array<uint8_t, 64> ret{};
244 const auto blind_arr = blind.
get_nonce();
246 hash->update(previous_response);
247 hash->update(hash->final());
248 hash->update(blind_arr.data(), blind_arr.size());
249 hash->final(ret.data());
255 std::istringstream ss{std::string(str)};
256 const std::string ERROR_MESSAGE =
"Line does not have 4 space separated fields";
257 for(std::string s; std::getline(ss, s);) {
260 end = s.find(
' ', start);
261 if(end == std::string::npos) {
264 const auto publicKeyType = s.substr(start, end - start);
265 if(publicKeyType !=
"ed25519") {
270 end = s.find(
' ', start);
271 if(end == std::string::npos) {
277 end = s.find(
' ', start);
278 if(end == std::string::npos) {
281 if((end - start) != 88) {
284 const auto vec =
base64_decode(s.substr(start, end - start));
285 const auto nonceOrBlind =
Nonce(vector_to_array<64>(
base64_decode(s.substr(start, end - start))));
288 end = s.find(
' ', start);
289 if(end != std::string::npos) {
294 m_links.push_back({response, serverPublicKey, nonceOrBlind});
300 for(
size_t i = 0; i < m_links.size(); ++i) {
301 const auto& l = m_links[i];
302 const auto nonce = i > 0 ?
nonce_from_blind(m_links[i - 1].response(), l.nonce_or_blind()) : l.nonce_or_blind();
304 if(!response.validate(l.public_key())) {
313 return m_links.empty() ? blind :
nonce_from_blind(m_links.back().response(), blind);
317 if(max_chain_size <= 0) {
321 while(m_links.size() >= max_chain_size) {
322 if(m_links.size() == 1) {
323 auto new_link_updated = new_link;
327 m_links.push_back(new_link_updated);
330 if(m_links.size() >= 2) {
331 m_links[1].nonce_or_blind() =
334 m_links.erase(m_links.begin());
336 m_links.push_back(new_link);
341 s.reserve((7 + 1 + 88 + 1 + 44 + 1 + 480) * m_links.size());
342 for(
const auto& link : m_links) {
347 s +=
base64_encode(link.nonce_or_blind().get_nonce().data(), link.nonce_or_blind().get_nonce().size());
355std::vector<uint8_t>
online_request(std::string_view uri,
const Nonce& nonce, std::chrono::milliseconds timeout) {
356 const std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now();
363 socket->write(encoded.data(), encoded.size());
365 if(std::chrono::system_clock::now() - start_time > timeout) {
369 std::vector<uint8_t> buffer;
370 buffer.resize(360 + 64 * 10 + 1);
372 const auto n = socket->read(buffer.data(), buffer.size());
374 if(n == 0 || std::chrono::system_clock::now() - start_time > timeout) {
378 if(n == buffer.size()) {
387 std::vector<Server_Information> servers;
388 std::istringstream ss{std::string(str)};
390 const std::string ERROR_MESSAGE =
"Line does not have at least 5 space separated fields";
391 for(std::string s; std::getline(ss, s);) {
394 end = s.find(
' ', start);
395 if(end == std::string::npos) {
398 const auto name = s.substr(start, end - start);
401 end = s.find(
' ', start);
402 if(end == std::string::npos) {
405 const auto publicKeyType = s.substr(start, end - start);
406 if(publicKeyType !=
"ed25519") {
411 end = s.find(
' ', start);
413 if(end == std::string::npos) {
416 const auto publicKeyBase64 = s.substr(start, end - start);
420 end = s.find(
' ', start);
421 if(end == std::string::npos) {
424 const auto protocol = s.substr(start, end - start);
425 if(protocol !=
"udp") {
429 const auto addresses = [&]() {
430 std::vector<std::string> addr;
433 end = s.find(
' ', start);
434 const auto address = s.substr(start, (end == std::string::npos) ? std::string::npos : end - start);
435 if(address.empty()) {
438 addr.push_back(address);
439 if(end == std::string::npos) {
444 if(addresses.empty()) {
448 servers.push_back({name, publicKey, addresses});
std::span< const uint8_t > take(const size_t count)
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 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
constexpr std::optional< T > checked_add(T a, T b)
constexpr void typecast_copy(ToR &&out, const 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)
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).
const char * cast_uint8_ptr_to_char(const uint8_t *b)