• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 23225340130

18 Mar 2026 01:53AM UTC coverage: 89.677% (-0.001%) from 89.678%
23225340130

push

github

web-flow
Merge pull request #5456 from randombit/jack/clang-tidy-22

Fix various warnings from clang-tidy 22

104438 of 116460 relevant lines covered (89.68%)

11819947.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.55
/src/cli/roughtime.cpp
1
/*
2
* Roughtime
3
* (C) 2019 Nuno Goncalves <nunojpg@gmail.com>
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "cli.h"
9

10
#if defined(BOTAN_HAS_ROUGHTIME)
11

12
   #include <botan/base64.h>
13
   #include <botan/ed25519.h>
14
   #include <botan/roughtime.h>
15
   #include <botan/internal/calendar.h>
16

17
   #include <fstream>
18
   #include <iomanip>
19

20
namespace Botan_CLI {
21

22
namespace {
23

24
class RoughtimeCheck final : public Command {
25
   public:
26
      RoughtimeCheck() : Command("roughtime_check --raw-time chain-file") {}
8 ✔
27

28
      std::string group() const override { return "misc"; }
1 ✔
29

30
      std::string description() const override { return "Parse and validate Roughtime chain file"; }
1 ✔
31

32
      void go() override {
3 ✔
33
         const auto chain = Botan::Roughtime::Chain(slurp_file_as_str(get_arg("chain-file")));
6 ✔
34
         unsigned i = 0;
3 ✔
35
         for(const auto& response : chain.responses()) {
9 ✔
36
            output() << std::setw(3) << ++i << ": UTC ";
6 ✔
37
            if(flag_set("raw-time")) {
6 ✔
38
               output()
1 ✔
39
                  << Botan::Roughtime::Response::sys_microseconds64(response.utc_midpoint()).time_since_epoch().count();
1 ✔
40
            } else {
41
               output() << Botan::calendar_point(response.utc_midpoint()).to_string();
10 ✔
42
            }
43
            output() << " (+-" << Botan::Roughtime::Response::microseconds32(response.utc_radius()).count() << "us)\n";
6 ✔
44
         }
2 ✔
45
      }
2 ✔
46
};
47

48
BOTAN_REGISTER_COMMAND("roughtime_check", RoughtimeCheck);
4 ✔
49

50
class Roughtime final : public Command {
51
   public:
52
      Roughtime() :
7 ✔
53
            Command(
54
               "roughtime --raw-time --chain-file=roughtime-chain --max-chain-size=128 --check-local-clock=60 --host= --pubkey= --servers-file=") {
7 ✔
55
      }
7 ✔
56

57
      std::string help_text() const override {
×
58
         return Command::help_text() + R"(
×
59

60
--servers-file=<filename>
61
   List of servers that will queried in sequence.
62

63
   File contents syntax:
64
      <name> <key type> <base 64 encoded public key> <protocol> <host:port>
65

66
   Example servers:
67
      Cloudflare-Roughtime ed25519 0GD7c3yP8xEc4Zl2zeuN2SlLvDVVocjsPSL8/Rl/7zg= udp roughtime.cloudflare.com:2003
68
      Google-Sandbox-Roughtime ed25519 etPaaIxcBMY1oUeGpwvPMCJMwlRVNxv51KK/tktoJTQ= udp roughtime.sandbox.google.com:2002
69

70
--chain-file=<filename>
71
   Successful queries are appended to this file.
72
   If limit of --max-chain-size records is reached, the oldest records are truncated.
73
   This queries records can be replayed using command roughtime_check <chain-file>.
74

75
   File contents syntax:
76
      <key type> <base 64 encoded public key> <base 64 encoded blind or nonce> <base 64 encoded server response>
77
)";
78
      }
79

80
      std::string group() const override { return "misc"; }
1 ✔
81

82
      std::string description() const override { return "Retrieve time from Roughtime server"; }
1 ✔
83

84
      void query(std::unique_ptr<Botan::Roughtime::Chain>& chain,
5 ✔
85
                 const size_t max_chain_size,
86
                 const std::string& address,
87
                 const Botan::Ed25519_PublicKey& public_key) {
88
         Botan::Roughtime::Nonce nonce{};
5 ✔
89
         Botan::Roughtime::Nonce blind{};
5 ✔
90
         if(chain) {
5 ✔
91
            blind = Botan::Roughtime::Nonce(rng());
3 ✔
92
            nonce = chain->next_nonce(blind);
3 ✔
93
         } else {
94
            nonce = Botan::Roughtime::Nonce(rng());
2 ✔
95
         }
96
         const auto response_raw = Botan::Roughtime::online_request(address, nonce, std::chrono::seconds(5));
5 ✔
97
         const auto response = Botan::Roughtime::Response::from_bits(response_raw, nonce);
5 ✔
98
         if(flag_set("raw-time")) {
5 ✔
99
            output()
1 ✔
100
               << "UTC "
1 ✔
101
               << Botan::Roughtime::Response::sys_microseconds64(response.utc_midpoint()).time_since_epoch().count();
1 ✔
102
         } else {
103
            output() << "UTC " << Botan::calendar_point(response.utc_midpoint()).to_string();
8 ✔
104
         }
105
         output() << " (+-" << Botan::Roughtime::Response::microseconds32(response.utc_radius()).count() << "us)";
5 ✔
106
         if(!response.validate(public_key)) {
5 ✔
107
            error_output() << "ERROR: Public key does not match!\n";
1 ✔
108
            set_return_code(1);
1 ✔
109
            return;
1 ✔
110
         }
111
         const auto tolerance = get_arg_sz("check-local-clock");
4 ✔
112
         if(tolerance > 0) {
4 ✔
113
            const auto now = std::chrono::system_clock::now();
1 ✔
114
            const auto diff_abs =
1 ✔
115
               now >= response.utc_midpoint() ? now - response.utc_midpoint() : response.utc_midpoint() - now;
1 ✔
116
            if(diff_abs > (response.utc_radius() + std::chrono::seconds(tolerance))) {
1 ✔
117
               error_output() << "ERROR: Local clock mismatch\n";
1 ✔
118
               set_return_code(1);
1 ✔
119
               return;
1 ✔
120
            }
121
            output() << " Local clock match";
×
122
         }
123
         if(chain) {
3 ✔
124
            chain->append({response_raw, public_key, blind}, max_chain_size);
3 ✔
125
         }
126
         output() << '\n';
3 ✔
127
      }
5 ✔
128

129
      void go() override {
6 ✔
130
         const auto max_chain_size = get_arg_sz("max-chain-size");
6 ✔
131
         const auto chain_file = get_arg("chain-file");
6 ✔
132
         const auto servers_file = get_arg_or("servers-file", "");
12 ✔
133
         const auto host = get_arg_or("host", "");
12 ✔
134
         const auto pk = get_arg_or("pubkey", "");
12 ✔
135

136
         std::unique_ptr<Botan::Roughtime::Chain> chain;
6 ✔
137
         if(!chain_file.empty() && max_chain_size >= 1) {
6 ✔
138
            try {
4 ✔
139
               chain = std::make_unique<Botan::Roughtime::Chain>(slurp_file_as_str(chain_file));
6 ✔
140
            } catch(const CLI_IO_Error&) {
2 ✔
141
               // file is to still be created
142
               chain = std::make_unique<Botan::Roughtime::Chain>();
4 ✔
143
            }
2 ✔
144
         }
145

146
         const bool from_servers_file = !servers_file.empty();
6 ✔
147
         const bool from_host_and_pk = !host.empty() && !pk.empty();
6 ✔
148
         if(from_servers_file == from_host_and_pk) {
6 ✔
149
            error_output() << "Please specify either --servers-file or --host and --pubkey\n";
1 ✔
150
            set_return_code(1);
1 ✔
151
            return;
1 ✔
152
         }
153

154
         if(!servers_file.empty()) {
5 ✔
155
            const auto servers = Botan::Roughtime::servers_from_str(slurp_file_as_str(servers_file));
3 ✔
156

157
            for(const auto& s : servers) {
6 ✔
158
               output() << std::setw(25) << std::left << s.name() << ": ";
3 ✔
159
               for(const auto& a : s.addresses()) {
3 ✔
160
                  try {
3 ✔
161
                     query(chain, max_chain_size, a, s.public_key());
3 ✔
162
                     break;
163
                  } catch(const std::exception& ex)  //network error, try next address
×
164
                  {
165
                     error_output() << ex.what() << '\n';
×
166
                  }
×
167
               }
168
            }
169

170
         } else {
3 ✔
171
            query(chain, max_chain_size, host, Botan::Ed25519_PublicKey(Botan::base64_decode(pk)));
4 ✔
172
         }
173

174
         if(chain) {
5 ✔
175
            std::ofstream out(chain_file);
3 ✔
176
            out << chain->to_string();
6 ✔
177
         }
3 ✔
178
      }
6 ✔
179
};
180

181
BOTAN_REGISTER_COMMAND("roughtime", Roughtime);
7 ✔
182

183
}  // namespace
184

185
}  // namespace Botan_CLI
186

187
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc