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

randombit / botan / 37260255903

04 Oct 2026 11:30PM UTC coverage: 92.214% (+0.02%) from 92.191%
37260255903

push

github

web-flow
Merge pull request #5995 from randombit/jack/ec-key-valiation

Harden EC key loading and validation

133278 of 144531 relevant lines covered (92.21%)

8668723.51 hits per line

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

91.09
/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
   #include <sstream>
20

21
namespace Botan_CLI {
22

23
namespace {
24

25
// Format the time point as YYYY-MM-DDTHH:MM:SS in UTC
26
std::string format_utc_datetime(const std::chrono::system_clock::time_point& tp) {
8 ✔
27
   const Botan::calendar_point c(tp);
8 ✔
28
   std::ostringstream out;
8 ✔
29
   out << std::setfill('0') << std::setw(4) << c.year() << "-" << std::setw(2) << c.month() << "-" << std::setw(2)
8 ✔
30
       << c.day() << "T" << std::setw(2) << c.hour() << ":" << std::setw(2) << c.minutes() << ":" << std::setw(2)
8 ✔
31
       << c.seconds();
8 ✔
32
   return out.str();
16 ✔
33
}
8 ✔
34

35
class RoughtimeCheck final : public Command {
36
   public:
37
      RoughtimeCheck() : Command("roughtime_check --raw-time chain-file") {}
8 ✔
38

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

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

43
      void go() override {
3 ✔
44
         const auto chain = Botan::Roughtime::Chain(slurp_file_as_str(get_arg("chain-file")));
8 ✔
45
         unsigned i = 0;
2 ✔
46
         for(const auto& response : chain.responses()) {
8 ✔
47
            output() << std::setw(3) << ++i << ": UTC ";
6 ✔
48
            if(flag_set("raw-time")) {
6 ✔
49
               output()
1 ✔
50
                  << Botan::Roughtime::Response::sys_microseconds64(response.utc_midpoint()).time_since_epoch().count();
1 ✔
51
            } else {
52
               output() << format_utc_datetime(response.utc_midpoint());
10 ✔
53
            }
54
            output() << " (+-" << Botan::Roughtime::Response::microseconds32(response.utc_radius()).count() << "us)\n";
6 ✔
55
         }
2 ✔
56
      }
2 ✔
57
};
58

59
BOTAN_REGISTER_COMMAND("roughtime_check", RoughtimeCheck);
4 ✔
60

61
class Roughtime final : public Command {
62
   public:
63
      Roughtime() :
7 ✔
64
            Command(
65
               "roughtime --raw-time --chain-file=roughtime-chain --max-chain-size=128 --check-local-clock=60 --host= --pubkey= --servers-file=") {
7 ✔
66
      }
7 ✔
67

68
      std::string help_text() const override {
×
69
         return Command::help_text() + R"(
×
70

71
--servers-file=<filename>
72
   List of servers that will queried in sequence.
73

74
   File contents syntax:
75
      <name> <key type> <base 64 encoded public key> <protocol> <host:port>
76

77
   Example servers:
78
      Cloudflare-Roughtime ed25519 0GD7c3yP8xEc4Zl2zeuN2SlLvDVVocjsPSL8/Rl/7zg= udp roughtime.cloudflare.com:2003
79
      Google-Sandbox-Roughtime ed25519 etPaaIxcBMY1oUeGpwvPMCJMwlRVNxv51KK/tktoJTQ= udp roughtime.sandbox.google.com:2002
80

81
--chain-file=<filename>
82
   Successful queries are appended to this file.
83
   If limit of --max-chain-size records is reached, the oldest records are truncated.
84
   This queries records can be replayed using command roughtime_check <chain-file>.
85

86
   File contents syntax:
87
      <key type> <base 64 encoded public key> <base 64 encoded blind or nonce> <base 64 encoded server response>
88
)";
89
      }
90

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

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

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

140
      void go() override {
6 ✔
141
         const auto max_chain_size = get_arg_sz("max-chain-size");
6 ✔
142
         const auto chain_file = get_arg("chain-file");
6 ✔
143
         const auto servers_file = get_arg_or("servers-file", "");
13 ✔
144
         const auto host = get_arg_or("host", "");
13 ✔
145
         const auto pk = get_arg_or("pubkey", "");
13 ✔
146

147
         std::unique_ptr<Botan::Roughtime::Chain> chain;
6 ✔
148
         if(!chain_file.empty() && max_chain_size >= 1) {
6 ✔
149
            try {
4 ✔
150
               chain = std::make_unique<Botan::Roughtime::Chain>(slurp_file_as_str(chain_file));
6 ✔
151
            } catch(const CLI_IO_Error&) {
2 ✔
152
               // file is to still be created
153
               chain = std::make_unique<Botan::Roughtime::Chain>();
4 ✔
154
            }
2 ✔
155
         }
156

157
         const bool from_servers_file = !servers_file.empty();
6 ✔
158
         const bool from_host_and_pk = !host.empty() && !pk.empty();
6 ✔
159
         if(from_servers_file == from_host_and_pk) {
6 ✔
160
            error_output() << "Please specify either --servers-file or --host and --pubkey\n";
1 ✔
161
            set_return_code(1);
1 ✔
162
            return;
1 ✔
163
         }
164

165
         if(!servers_file.empty()) {
5 ✔
166
            const auto servers = Botan::Roughtime::servers_from_str(slurp_file_as_str(servers_file));
4 ✔
167

168
            for(const auto& s : servers) {
4 ✔
169
               output() << std::setw(25) << std::left << s.name() << ": ";
2 ✔
170
               for(const auto& a : s.addresses()) {
2 ✔
171
                  try {
2 ✔
172
                     query(chain, max_chain_size, a, s.public_key());
2 ✔
173
                     break;
174
                  } catch(const std::exception& ex)  //network error, try next address
×
175
                  {
176
                     error_output() << ex.what() << '\n';
×
177
                  }
×
178
               }
179
            }
180

181
         } else {
2 ✔
182
            query(chain, max_chain_size, host, Botan::Ed25519_PublicKey(Botan::base64_decode(pk)));
4 ✔
183
         }
184

185
         if(chain) {
4 ✔
186
            std::ofstream out(chain_file);
3 ✔
187
            out << chain->to_string();
6 ✔
188
         }
3 ✔
189
      }
9 ✔
190
};
191

192
BOTAN_REGISTER_COMMAND("roughtime", Roughtime);
7 ✔
193

194
}  // namespace
195

196
}  // namespace Botan_CLI
197

198
#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