summaryrefslogtreecommitdiffstats
path: root/comm/third_party/botan/src/cli/cli.cpp
blob: 1fc5ed116ed14e8b2237b25b9bdb5d89078059aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* (C) 2015,2017 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "cli.h"
#include "argparse.h"
#include <botan/rng.h>
#include <botan/parsing.h>
#include <botan/internal/os_utils.h>
#include <iostream>
#include <fstream>

#if defined(BOTAN_HAS_HEX_CODEC)
   #include <botan/hex.h>
#endif

#if defined(BOTAN_HAS_BASE64_CODEC)
   #include <botan/base64.h>
#endif

#if defined(BOTAN_HAS_BASE58_CODEC)
   #include <botan/base58.h>
#endif

namespace Botan_CLI {

Command::Command(const std::string& cmd_spec) : m_spec(cmd_spec)
   {
   // for checking all spec strings at load time
   //m_args.reset(new Argument_Parser(m_spec));
   }

Command::~Command() { /* for unique_ptr */ }

std::string Command::cmd_name() const
   {
   return m_spec.substr(0, m_spec.find(' '));
   }

std::string Command::help_text() const
   {
   return "Usage: " + m_spec;
   }

int Command::run(const std::vector<std::string>& params)
   {
   try
      {
      m_args.reset(new Argument_Parser(m_spec,
                                       {"verbose", "help"},
                                       {"output", "error-output", "rng-type", "drbg-seed"}));

      m_args->parse_args(params);

      if(m_args->has_arg("output"))
         {
         const std::string output_file = get_arg("output");

         if(output_file != "")
            {
            m_output_stream.reset(new std::ofstream(output_file, std::ios::binary));
            if(!m_output_stream->good())
               throw CLI_IO_Error("opening", output_file);
            }
         }

      if(m_args->has_arg("error-output"))
         {
         const std::string output_file = get_arg("error-output");

         if(output_file != "")
            {
            m_error_output_stream.reset(new std::ofstream(output_file, std::ios::binary));
            if(!m_error_output_stream->good())
               throw CLI_IO_Error("opening", output_file);
            }
         }

      if(flag_set("help"))
         {
         output() << help_text() << "\n";
         return 2;
         }

      this->go();
      return m_return_code;
      }
   catch(CLI_Usage_Error& e)
      {
      error_output() << "Usage error: " << e.what() << "\n";
      error_output() << help_text() << "\n";
      return 1;
      }
   catch(std::exception& e)
      {
      error_output() << "Error: " << e.what() << "\n";
      return 2;
      }
   catch(...)
      {
      error_output() << "Error: unknown exception\n";
      return 2;
      }
   }

bool Command::flag_set(const std::string& flag_name) const
   {
   return m_args->flag_set(flag_name);
   }

std::string Command::get_arg(const std::string& opt_name) const
   {
   return m_args->get_arg(opt_name);
   }

/*
* Like get_arg() but if the argument was not specified or is empty, returns otherwise
*/
std::string Command::get_arg_or(const std::string& opt_name, const std::string& otherwise) const
   {
   return m_args->get_arg_or(opt_name, otherwise);
   }

size_t Command::get_arg_sz(const std::string& opt_name) const
   {
   return m_args->get_arg_sz(opt_name);
   }

uint16_t Command::get_arg_u16(const std::string& opt_name) const
   {
   const size_t val = get_arg_sz(opt_name);
   if(static_cast<uint16_t>(val) != val)
      throw CLI_Usage_Error("Argument " + opt_name + " has value out of allowed range");
   return static_cast<uint16_t>(val);
   }

uint32_t Command::get_arg_u32(const std::string& opt_name) const
   {
   const size_t val = get_arg_sz(opt_name);
   if(static_cast<uint32_t>(val) != val)
      throw CLI_Usage_Error("Argument " + opt_name + " has value out of allowed range");
   return static_cast<uint32_t>(val);
   }

std::vector<std::string> Command::get_arg_list(const std::string& what) const
   {
   return m_args->get_arg_list(what);
   }

std::ostream& Command::output()
   {
   if(m_output_stream.get())
      {
      return *m_output_stream;
      }
   return std::cout;
   }

std::ostream& Command::error_output()
   {
   if(m_error_output_stream.get())
      {
      return *m_error_output_stream;
      }
   return std::cerr;
   }

std::vector<uint8_t> Command::slurp_file(const std::string& input_file,
                                         size_t buf_size) const
   {
   std::vector<uint8_t> buf;
   auto insert_fn = [&](const uint8_t b[], size_t l)
      {
      buf.insert(buf.end(), b, b + l);
      };
   this->read_file(input_file, insert_fn, buf_size);
   return buf;
   }

std::string Command::slurp_file_as_str(const std::string& input_file,
                                       size_t buf_size) const
   {
   std::string str;
   auto insert_fn = [&](const uint8_t b[], size_t l)
      {
      str.append(reinterpret_cast<const char*>(b), l);
      };
   this->read_file(input_file, insert_fn, buf_size);
   return str;
   }

void Command::read_file(const std::string& input_file,
                        std::function<void (uint8_t[], size_t)> consumer_fn,
                        size_t buf_size) const
   {
   if(input_file == "-")
      {
      do_read_file(std::cin, consumer_fn, buf_size);
      }
   else
      {
      std::ifstream in(input_file, std::ios::binary);
      if(!in)
         {
         throw CLI_IO_Error("reading file", input_file);
         }
      do_read_file(in, consumer_fn, buf_size);
      }
   }

void Command::do_read_file(std::istream& in,
                           std::function<void (uint8_t[], size_t)> consumer_fn,
                           size_t buf_size) const
   {
   // Avoid an infinite loop on --buf-size=0
   std::vector<uint8_t> buf(buf_size == 0 ? 4096 : buf_size);

   while(in.good())
      {
      in.read(reinterpret_cast<char*>(buf.data()), buf.size());
      const size_t got = static_cast<size_t>(in.gcount());
      consumer_fn(buf.data(), got);
      }
   }

Botan::RandomNumberGenerator& Command::rng()
   {
   if(m_rng == nullptr)
      {
      m_rng = cli_make_rng(get_arg("rng-type"), get_arg("drbg-seed"));
      }

   return *m_rng.get();
   }

std::string Command::get_passphrase_arg(const std::string& prompt, const std::string& opt_name)
   {
   const std::string s = get_arg(opt_name);
   if(s != "-")
      return s;
   return get_passphrase(prompt);
   }

namespace {

bool echo_suppression_supported()
   {
   auto echo = Botan::OS::suppress_echo_on_terminal();
   return (echo != nullptr);
   }

}

std::string Command::get_passphrase(const std::string& prompt)
   {
   if(echo_suppression_supported() == false)
      error_output() << "Warning: terminal echo suppression not enabled for this platform\n";

   error_output() << prompt << ": " << std::flush;
   std::string pass;

   auto echo_suppress = Botan::OS::suppress_echo_on_terminal();

   std::getline(std::cin, pass);

   return pass;
   }

//static
std::string Command::format_blob(const std::string& format,
                                 const uint8_t bits[], size_t len)
   {
#if defined(BOTAN_HAS_HEX_CODEC)
   if(format == "hex")
      {
      return Botan::hex_encode(bits, len);
      }
#endif

#if defined(BOTAN_HAS_BASE64_CODEC)
   if(format == "base64")
      {
      return Botan::base64_encode(bits, len);
      }
#endif

#if defined(BOTAN_HAS_BASE58_CODEC)
   if(format == "base58")
      {
      return Botan::base58_encode(bits, len);
      }
   if(format == "base58check")
      {
      return Botan::base58_check_encode(bits, len);
      }
#endif

   // If we supported format, we would have already returned
   throw CLI_Usage_Error("Unknown or unsupported format type");
   }

// Registration code

Command::Registration::Registration(const std::string& name, Command::cmd_maker_fn maker_fn)
   {
   std::map<std::string, Command::cmd_maker_fn>& reg = Command::global_registry();

   if(reg.count(name) > 0)
      {
      throw CLI_Error("Duplicated registration of command " + name);
      }

   reg.insert(std::make_pair(name, maker_fn));
   }

//static
std::map<std::string, Command::cmd_maker_fn>& Command::global_registry()
   {
   static std::map<std::string, Command::cmd_maker_fn> g_cmds;
   return g_cmds;
   }

//static
std::vector<std::string> Command::registered_cmds()
   {
   std::vector<std::string> cmds;
   for(auto& cmd : Command::global_registry())
      cmds.push_back(cmd.first);
   return cmds;
   }

//static
std::unique_ptr<Command> Command::get_cmd(const std::string& name)
   {
   const std::map<std::string, Command::cmd_maker_fn>& reg = Command::global_registry();

   std::unique_ptr<Command> r;
   auto i = reg.find(name);
   if(i != reg.end())
      {
      r.reset(i->second());
      }

   return r;
   }

}