aboutsummaryrefslogtreecommitdiffstats
path: root/src/cli/psk.cpp
blob: d244acd8d307ca7932dac1ec09cfa05aad66430e (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
/*
* (C) 2017 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "cli.h"

#if defined(BOTAN_HAS_PSK_DB) && defined(BOTAN_HAS_SQLITE3)

#include <botan/psk_db_sql.h>
#include <botan/sqlite3.h>
#include <botan/hex.h>

namespace Botan_CLI {

class PSK_Tool_Base : public Command
   {
   public:
      PSK_Tool_Base(const std::string& spec) : Command(spec) {}

      void go() override
         {
         const std::string db_filename = get_arg("db");
         const Botan::secure_vector<uint8_t> db_key = Botan::hex_decode_locked(get_arg("db_key"));

         std::shared_ptr<Botan::SQL_Database> db = std::make_shared<Botan::Sqlite3_Database>(db_filename);
         Botan::Encrypted_PSK_Database_SQL psk(db_key, db, "psk");

         psk_operation(psk);
         }

   private:
      virtual void psk_operation(Botan::PSK_Database& db) = 0;
   };

class PSK_Tool_Set final : public PSK_Tool_Base
   {
   public:
      PSK_Tool_Set() : PSK_Tool_Base("psk_set db db_key name psk") {}

   private:
      void psk_operation(Botan::PSK_Database& db) override
         {
         const std::string name = get_arg("name");
         Botan::secure_vector<uint8_t> key = Botan::hex_decode_locked(get_arg("psk"));
         db.set_vec(name, key);
         }
   };

class PSK_Tool_Get final : public PSK_Tool_Base
   {
   public:
      PSK_Tool_Get() : PSK_Tool_Base("psk_get db db_key name") {}

   private:
      void psk_operation(Botan::PSK_Database& db) override
         {
         const std::string name = get_arg("name");
         const Botan::secure_vector<uint8_t> val = db.get(name);
         output() << Botan::hex_encode(val) << "\n";
         }
   };

class PSK_Tool_List final : public PSK_Tool_Base
   {
   public:
      PSK_Tool_List() : PSK_Tool_Base("psk_list db db_key") {}

   private:
      void psk_operation(Botan::PSK_Database& db) override
         {
         const std::set<std::string> names = db.list_names();

         for(std::string name : names)
            output() << name << "\n";
         }
   };

BOTAN_REGISTER_COMMAND("psk_set", PSK_Tool_Set);
BOTAN_REGISTER_COMMAND("psk_get", PSK_Tool_Get);
BOTAN_REGISTER_COMMAND("psk_list", PSK_Tool_List);

}

#endif