Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Namespace List
- Namespace Members

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_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 (const std::string &uri, const Nonce &nonce, std::chrono::milliseconds timeout)
std::vector< Server_Information > | servers_from_str (const std::string &str)

## Variables
const unsigned | request_min_size = 1024

## Function Documentation

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.

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

Referenced by online_request().

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 }

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

Definition at line 237 of file roughtime.cpp.

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().

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 }
Botan::HashFunction::create_or_throw
static std::unique_ptr< HashFunction > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:344
hash
MechanismType hash
Definition: p11_mechanism.cpp:64

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

  url | Roughtime server UDP endpoint (host:port)
  nonce | the nonce to send to the server
  timeout | a timeout on the UDP request

Returns
  Roughtime response

Definition at line 363 of file roughtime.cpp.

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

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 }
Botan::OS::open_socket_udp
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:316
Botan::Roughtime::encode_request
std::array< uint8_t, request_min_size > encode_request(const Nonce &nonce)
Definition: roughtime.cpp:169

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

Definition at line 393 of file roughtime.cpp.

References Botan::base64_decode(), and name.

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> addresses;
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 addresses; }
448 addresses.push_back(address);
449 if(end == std::string::npos)
450 { return addresses; }
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 }
Botan::Ed25519_PublicKey
Definition: ed25519.h:18
name
std::string name
Definition: commoncrypto_hash.cpp:23
Botan::base64_decode
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:181

## Variable Documentation

const unsigned Botan::Roughtime::request_min_size = 1024

Definition at line 23 of file roughtime.h.