Botan 3.0.0-alpha0
Crypto and TLS for C&
Classes | Functions | Variables
Botan::Roughtime Namespace Reference

Classes

class  Chain
 
class  Link
 
class  Nonce
 
class  Response
 
class  Roughtime_Error
 
struct  Server_Information
 

Functions

std::array< uint8_t, request_min_sizeencode_request (const Nonce &nonce)
 
Nonce nonce_from_blind (const std::vector< uint8_t > &previous_response, const Nonce &blind)
 
std::vector< uint8_t > online_request (const std::string &uri, const Nonce &nonce, std::chrono::milliseconds timeout)
 
std::vector< Server_Informationservers_from_str (const std::string &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.

Definition at line 170 of file roughtime.cpp.

171 {
172 std::array<uint8_t, request_min_size> buf = {{2, 0, 0, 0, 64, 0, 0, 0, 'N', 'O', 'N', 'C', 'P', 'A', 'D', 0xff}};
173 std::memcpy(buf.data() + 16, nonce.get_nonce().data(), nonce.get_nonce().size());
174 std::memset(buf.data() + 16 + nonce.get_nonce().size(), 0, buf.size() - 16 - nonce.get_nonce().size());
175 return buf;
176 }
const std::array< uint8_t, 64 > & get_nonce() const
Definition: roughtime.h:43

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 
)

Definition at line 238 of file roughtime.cpp.

240 {
241 std::array<uint8_t, 64> ret;
242 const auto blind_arr = blind.get_nonce();
243 std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create_or_throw("SHA-512"));
244 hash->update(previous_response);
245 hash->update(hash->final());
246 hash->update(blind_arr.data(), blind_arr.size());
247 hash->final(ret.data());
248
249 return ret;
250 }
static std::unique_ptr< HashFunction > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:312
MechanismType hash

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

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 ( const std::string &  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 364 of file roughtime.cpp.

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

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

◆ servers_from_str()

std::vector< Server_Information > Botan::Roughtime::servers_from_str ( const std::string &  str)

Definition at line 394 of file roughtime.cpp.

395 {
396 std::vector<Server_Information> servers;
397 std::stringstream ss(str);
398 const std::string ERROR_MESSAGE = "Line does not have at least 5 space separated fields";
399 for(std::string s; std::getline(ss, s);)
400 {
401 size_t start = 0, end = 0;
402 end = s.find(' ', start);
403 if(end == std::string::npos)
404 {
405 throw Decoding_Error(ERROR_MESSAGE);
406 }
407 const auto name = s.substr(start, end-start);
408
409 start = end + 1;
410 end = s.find(' ', start);
411 if(end == std::string::npos)
412 {
413 throw Decoding_Error(ERROR_MESSAGE);
414 }
415 const auto publicKeyType = s.substr(start, end-start);
416 if(publicKeyType != "ed25519")
417 { throw Not_Implemented("Only ed25519 publicKeyType is implemented"); }
418
419 start = end + 1;
420 end = s.find(' ', start);
421
422 if(end == std::string::npos)
423 {
424 throw Decoding_Error(ERROR_MESSAGE);
425 }
426 const auto publicKeyBase64 = s.substr(start, end-start);
427 const auto publicKey = Botan::Ed25519_PublicKey(Botan::base64_decode(publicKeyBase64));
428
429 start = end + 1;
430 end = s.find(' ', start);
431 if(end == std::string::npos)
432 {
433 throw Decoding_Error(ERROR_MESSAGE);
434 }
435 const auto protocol = s.substr(start, end-start);
436 if(protocol != "udp")
437 { throw Not_Implemented("Only UDP protocol is implemented"); }
438
439 const auto addresses = [&]()
440 {
441 std::vector<std::string> addr;
442 for(;;)
443 {
444 start = end + 1;
445 end = s.find(' ', start);
446 const auto address = s.substr(start, (end == std::string::npos) ? std::string::npos : end-start);
447 if(address.empty())
448 { return addr; }
449 addr.push_back(address);
450 if(end == std::string::npos)
451 { return addr; }
452 }
453 }
454 ();
455 if(addresses.empty())
456 {
457 throw Decoding_Error(ERROR_MESSAGE);
458 }
459
460 servers.push_back({name, publicKey, addresses});
461 }
462 return servers;
463 }
std::string name
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:200

References Botan::base64_decode(), and name.

Variable Documentation

◆ request_min_size

const unsigned Botan::Roughtime::request_min_size = 1024

Definition at line 23 of file roughtime.h.