Botan 3.14.1
Crypto and TLS for C&
Botan::Roughtime Namespace Reference

Classes

class  Chain
class  Nonce
class  Response
class  Roughtime_Error
struct  Server_Information

Functions

std::array< uint8_t, request_min_size > encode_request (const Nonce &nonce)
Nonce nonce_from_blind (const std::vector< uint8_t > &previous_response, const Nonce &blind)
std::vector< uint8_t > online_request (std::string_view uri, const Nonce &nonce, std::chrono::milliseconds timeout)
std::vector< Server_Information > servers_from_str (std::string_view str)

Variables

const unsigned request_min_size = 1024

Function Documentation

◆ encode_request()

std::array< uint8_t, request_min_size > Botan::Roughtime::encode_request ( const Nonce & nonce)

An Roughtime request.

Parameters
noncethe nonce to include in the request
Returns
the encoded request

Definition at line 179 of file roughtime.cpp.

179 {
180 std::array<uint8_t, request_min_size> buf = {{2, 0, 0, 0, 64, 0, 0, 0, 'N', 'O', 'N', 'C', 'P', 'A', 'D', 0xff}};
181 std::memcpy(buf.data() + 16, nonce.get_nonce().data(), nonce.get_nonce().size());
182 std::memset(buf.data() + 16 + nonce.get_nonce().size(), 0, buf.size() - 16 - nonce.get_nonce().size());
183 return buf;
184}
const std::array< uint8_t, 64 > & get_nonce() const
Definition roughtime.h:85

References Botan::Roughtime::Nonce::get_nonce().

Referenced by online_request().

◆ nonce_from_blind()

Nonce Botan::Roughtime::nonce_from_blind ( const std::vector< uint8_t > & previous_response,
const Nonce & blind )

Derive a nonce from the previous response and a blind

Parameters
previous_responsethe raw bytes of the previous response
blindthe blind to use
Returns
the derived nonce

Definition at line 247 of file roughtime.cpp.

247 {
248 std::array<uint8_t, 64> ret{};
249 const auto blind_arr = blind.get_nonce();
250 auto hash = HashFunction::create_or_throw("SHA-512");
251 hash->update(previous_response);
252 hash->update(hash->final());
253 hash->update(blind_arr.data(), blind_arr.size());
254 hash->final(ret.data());
255
256 return Nonce(ret);
257}
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:311

References Botan::HashFunction::create_or_throw(), and Botan::Roughtime::Nonce::get_nonce().

Referenced by Botan::Roughtime::Chain::append(), Botan::Roughtime::Chain::next_nonce(), and Botan::Roughtime::Chain::responses().

◆ online_request()

std::vector< uint8_t > Botan::Roughtime::online_request ( std::string_view url,
const Nonce & nonce,
std::chrono::milliseconds timeout = std::chrono::seconds(3) )

Makes an online Roughtime request via UDP and returns the Roughtime response.

Parameters
urlRoughtime server UDP endpoint (host:port)
noncethe nonce to send to the server
timeouta timeout on the UDP request
Returns
Roughtime response

Definition at line 360 of file roughtime.cpp.

360 {
361 const std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now();
362 auto socket = OS::open_socket_udp(uri, timeout);
363 if(!socket) {
364 throw Not_Implemented("No socket support enabled in build");
365 }
366
367 const auto encoded = encode_request(nonce);
368 socket->write(encoded.data(), encoded.size());
369
370 if(std::chrono::system_clock::now() - start_time > timeout) {
371 throw System_Error("Timeout during socket write");
372 }
373
374 std::vector<uint8_t> buffer;
375 buffer.resize(360 + 64 * 10 + 1); //response basic size is 360 bytes + 64 bytes for each level of merkle tree
376 //add one additional byte to be able to differentiate if datagram got truncated
377 const auto n = socket->read(buffer.data(), buffer.size());
378
379 if(n == 0 || std::chrono::system_clock::now() - start_time > timeout) {
380 throw System_Error("Timeout waiting for response");
381 }
382
383 if(n == buffer.size()) {
384 throw System_Error("Buffer too small");
385 }
386
387 buffer.resize(n);
388 return buffer;
389}
std::unique_ptr< SocketUDP > BOTAN_TEST_API open_socket_udp(std::string_view hostname, std::string_view service, std::chrono::microseconds timeout)
std::array< uint8_t, request_min_size > encode_request(const Nonce &nonce)

References encode_request(), and Botan::OS::open_socket_udp().

◆ servers_from_str()

std::vector< Server_Information > Botan::Roughtime::servers_from_str ( std::string_view str)

Parse a list of server descriptions

Parameters
strthe server list to parse
Returns
the parsed servers

Definition at line 391 of file roughtime.cpp.

391 {
392 std::vector<Server_Information> servers;
393 std::istringstream ss{std::string(str)}; // FIXME C++23 avoid copy
394
395 const std::string ERROR_MESSAGE = "Line does not have at least 5 space separated fields";
396 for(std::string s; std::getline(ss, s);) {
397 size_t start = 0;
398 size_t end = 0;
399 end = s.find(' ', start);
400 if(end == std::string::npos) {
401 throw Decoding_Error(ERROR_MESSAGE);
402 }
403 const auto name = s.substr(start, end - start);
404
405 start = end + 1;
406 end = s.find(' ', start);
407 if(end == std::string::npos) {
408 throw Decoding_Error(ERROR_MESSAGE);
409 }
410 const auto publicKeyType = s.substr(start, end - start);
411 if(publicKeyType != "ed25519") {
412 throw Not_Implemented("Only ed25519 publicKeyType is implemented");
413 }
414
415 start = end + 1;
416 end = s.find(' ', start);
417
418 if(end == std::string::npos) {
419 throw Decoding_Error(ERROR_MESSAGE);
420 }
421 const auto publicKeyBase64 = s.substr(start, end - start);
422 const auto publicKey = Ed25519_PublicKey(base64_decode(publicKeyBase64));
423
424 start = end + 1;
425 end = s.find(' ', start);
426 if(end == std::string::npos) {
427 throw Decoding_Error(ERROR_MESSAGE);
428 }
429 const auto protocol = s.substr(start, end - start);
430 if(protocol != "udp") {
431 throw Not_Implemented("Only UDP protocol is implemented");
432 }
433
434 const auto addresses = [&]() {
435 std::vector<std::string> addr;
436 for(;;) {
437 start = end + 1;
438 end = s.find(' ', start);
439 const auto address = s.substr(start, (end == std::string::npos) ? std::string::npos : end - start);
440 if(address.empty()) {
441 return addr;
442 }
443 addr.push_back(address);
444 if(end == std::string::npos) {
445 return addr;
446 }
447 }
448 }();
449 if(addresses.empty()) {
450 throw Decoding_Error(ERROR_MESSAGE);
451 }
452
453 servers.push_back({name, publicKey, addresses});
454 }
455 return servers;
456}
const char * name
Definition ffi_tls.h:59
size_t base64_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
Definition base64.cpp:203

References Botan::base64_decode(), and name.

Variable Documentation

◆ request_min_size

const unsigned Botan::Roughtime::request_min_size = 1024

The fixed size of a Roughtime request

Definition at line 26 of file roughtime.h.