Botan 2.19.1
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 169 of file roughtime.cpp.

170 {
171 std::array<uint8_t, request_min_size> buf = {{2, 0, 0, 0, 64, 0, 0, 0, 'N', 'O', 'N', 'C', 'P', 'A', 'D', 0xff}};
172 std::memcpy(buf.data() + 16, nonce.get_nonce().data(), nonce.get_nonce().size());
173 std::memset(buf.data() + 16 + nonce.get_nonce().size(), 0, buf.size() - 16 - nonce.get_nonce().size());
174 return buf;
175 }
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 237 of file roughtime.cpp.

239 {
240 std::array<uint8_t, 64> ret;
241 const auto blind_arr = blind.get_nonce();
242 std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create_or_throw("SHA-512"));
243 hash->update(previous_response);
244 hash->update(hash->final());
245 hash->update(blind_arr.data(), blind_arr.size());
246 hash->final(ret.data());
247
248 return ret;
249 }
static std::unique_ptr< HashFunction > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:344
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 363 of file roughtime.cpp.

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

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 393 of file roughtime.cpp.

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