Botan 3.13.0
Crypto and TLS for C&
Botan::Roughtime::Chain Class Referencefinal

#include <roughtime.h>

Public Member Functions

void append (const Link &new_link, size_t max_chain_size)
 Chain ()=default
 Chain (std::string_view str)
const std::vector< Link > & links () const
Nonce next_nonce (const Nonce &blind) const
std::vector< Responseresponses () const
std::string to_string () const

Detailed Description

A chain of Roughtime responses

Definition at line 207 of file roughtime.h.

Constructor & Destructor Documentation

◆ Chain() [1/2]

Botan::Roughtime::Chain::Chain ( )
default

Create an empty chain

References Chain().

Referenced by Chain().

◆ Chain() [2/2]

Botan::Roughtime::Chain::Chain ( std::string_view str)
explicit

Parse a chain from its string representation

Parameters
strthe chain to parse

Definition at line 254 of file roughtime.cpp.

254 {
255 std::istringstream ss{std::string(str)}; // FIXME C++23 avoid copy
256 const std::string ERROR_MESSAGE = "Line does not have 4 space separated fields";
257 for(std::string s; std::getline(ss, s);) {
258 size_t start = 0;
259 size_t end = 0;
260 end = s.find(' ', start);
261 if(end == std::string::npos) {
262 throw Decoding_Error(ERROR_MESSAGE);
263 }
264 const auto publicKeyType = s.substr(start, end - start);
265 if(publicKeyType != "ed25519") {
266 throw Not_Implemented("Only ed25519 publicKeyType is implemented");
267 }
268
269 start = end + 1;
270 end = s.find(' ', start);
271 if(end == std::string::npos) {
272 throw Decoding_Error(ERROR_MESSAGE);
273 }
274 const auto serverPublicKey = Ed25519_PublicKey(base64_decode(s.substr(start, end - start)));
275
276 start = end + 1;
277 end = s.find(' ', start);
278 if(end == std::string::npos) {
279 throw Decoding_Error(ERROR_MESSAGE);
280 }
281 if((end - start) != 88) {
282 throw Decoding_Error("Nonce has invalid length");
283 }
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))));
286
287 start = end + 1;
288 end = s.find(' ', start);
289 if(end != std::string::npos) {
290 throw Decoding_Error(ERROR_MESSAGE);
291 }
292 const auto response = unlock(base64_decode(s.substr(start)));
293
294 m_links.push_back({response, serverPublicKey, nonceOrBlind});
295 }
296}
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:169
std::vector< T > unlock(const secure_vector< T > &in)
Definition secmem.h:155

References Botan::base64_decode(), and Botan::unlock().

Member Function Documentation

◆ append()

void Botan::Roughtime::Chain::append ( const Link & new_link,
size_t max_chain_size )

Append a link, dropping the oldest links if needed

Parameters
new_linkthe link to append
max_chain_sizethe maximum number of links to retain

Definition at line 316 of file roughtime.cpp.

316 {
317 if(max_chain_size <= 0) {
318 throw Invalid_Argument("Max chain size must be positive");
319 }
320
321 while(m_links.size() >= max_chain_size) {
322 if(m_links.size() == 1) {
323 auto new_link_updated = new_link;
324 new_link_updated.nonce_or_blind() =
325 nonce_from_blind(m_links[0].response(), new_link.nonce_or_blind()); //we need to convert blind to nonce
326 m_links.clear();
327 m_links.push_back(new_link_updated);
328 return;
329 }
330 if(m_links.size() >= 2) {
331 m_links[1].nonce_or_blind() =
332 nonce_from_blind(m_links[0].response(), m_links[1].nonce_or_blind()); //we need to convert blind to nonce
333 }
334 m_links.erase(m_links.begin());
335 }
336 m_links.push_back(new_link);
337}
Nonce nonce_from_blind(const std::vector< uint8_t > &previous_response, const Nonce &blind)

References Botan::Roughtime::nonce_from_blind(), and Botan::Roughtime::Link::nonce_or_blind().

◆ links()

const std::vector< Link > & Botan::Roughtime::Chain::links ( ) const
inline

Access the links of this chain

Returns
the links

Definition at line 224 of file roughtime.h.

224{ return m_links; }

◆ next_nonce()

Nonce Botan::Roughtime::Chain::next_nonce ( const Nonce & blind) const

Compute the nonce to use for the next request

Parameters
blindthe blind to use
Returns
the next nonce

Definition at line 312 of file roughtime.cpp.

312 {
313 return m_links.empty() ? blind : nonce_from_blind(m_links.back().response(), blind);
314}

References Botan::Roughtime::nonce_from_blind().

◆ responses()

std::vector< Response > Botan::Roughtime::Chain::responses ( ) const

Parse and validate every response in the chain

Returns
the parsed responses

Definition at line 298 of file roughtime.cpp.

298 {
299 std::vector<Response> responses;
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();
303 const auto response = Response::from_bits(l.response(), nonce);
304 if(!response.validate(l.public_key())) {
305 throw Roughtime_Error("Invalid signature or public key");
306 }
307 responses.push_back(response);
308 }
309 return responses;
310}
std::vector< Response > responses() const
static Response from_bits(const std::vector< uint8_t > &response, const Nonce &nonce)

References Botan::Roughtime::Response::from_bits(), Botan::Roughtime::nonce_from_blind(), and responses().

Referenced by responses().

◆ to_string()

std::string Botan::Roughtime::Chain::to_string ( ) const

Format this chain as a string

Returns
the string representation of the chain

Definition at line 339 of file roughtime.cpp.

339 {
340 std::string s;
341 s.reserve((7 + 1 + 88 + 1 + 44 + 1 + 480) * m_links.size());
342 for(const auto& link : m_links) {
343 s += "ed25519";
344 s += ' ';
345 s += base64_encode(link.public_key().get_public_key());
346 s += ' ';
347 s += base64_encode(link.nonce_or_blind().get_nonce().data(), link.nonce_or_blind().get_nonce().size());
348 s += ' ';
349 s += base64_encode(link.response());
350 s += '\n';
351 }
352 return s;
353}
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition base64.cpp:161

References Botan::base64_encode().


The documentation for this class was generated from the following files: