Botan 3.4.0
Crypto and TLS for C&
Public Member Functions | List of all members
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

Definition at line 103 of file roughtime.h.

Constructor & Destructor Documentation

◆ Chain() [1/2]

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

◆ Chain() [2/2]

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

Definition at line 245 of file roughtime.cpp.

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

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 )

Definition at line 306 of file roughtime.cpp.

306 {
307 if(max_chain_size <= 0) {
308 throw Invalid_Argument("Max chain size must be positive");
309 }
310
311 while(m_links.size() >= max_chain_size) {
312 if(m_links.size() == 1) {
313 auto new_link_updated = new_link;
314 new_link_updated.nonce_or_blind() =
315 nonce_from_blind(m_links[0].response(), new_link.nonce_or_blind()); //we need to convert blind to nonce
316 m_links.clear();
317 m_links.push_back(new_link_updated);
318 return;
319 }
320 if(m_links.size() >= 2) {
321 m_links[1].nonce_or_blind() =
322 nonce_from_blind(m_links[0].response(), m_links[1].nonce_or_blind()); //we need to convert blind to nonce
323 }
324 m_links.erase(m_links.begin());
325 }
326 m_links.push_back(new_link);
327}
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

Definition at line 108 of file roughtime.h.

108{ return m_links; }

◆ next_nonce()

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

Definition at line 302 of file roughtime.cpp.

302 {
303 return m_links.empty() ? blind : nonce_from_blind(m_links.back().response(), blind);
304}

References Botan::Roughtime::nonce_from_blind().

◆ responses()

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

Definition at line 288 of file roughtime.cpp.

288 {
289 std::vector<Response> responses;
290 for(unsigned i = 0; i < m_links.size(); ++i) {
291 const auto& l = m_links[i];
292 const auto nonce = i ? nonce_from_blind(m_links[i - 1].response(), l.nonce_or_blind()) : l.nonce_or_blind();
293 const auto response = Response::from_bits(l.response(), nonce);
294 if(!response.validate(l.public_key())) {
295 throw Roughtime_Error("Invalid signature or public key");
296 }
297 responses.push_back(response);
298 }
299 return responses;
300}
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

Definition at line 329 of file roughtime.cpp.

329 {
330 std::string s;
331 s.reserve((7 + 1 + 88 + 1 + 44 + 1 + 480) * m_links.size());
332 for(const auto& link : m_links) {
333 s += "ed25519";
334 s += ' ';
335 s += base64_encode(link.public_key().get_public_key());
336 s += ' ';
337 s += base64_encode(link.nonce_or_blind().get_nonce().data(), link.nonce_or_blind().get_nonce().size());
338 s += ' ';
339 s += base64_encode(link.response());
340 s += '\n';
341 }
342 return s;
343}
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition base64.cpp:146

References Botan::base64_encode().


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