Botan 3.5.0
Crypto and TLS for C&
psk_db.h
Go to the documentation of this file.
1/*
2* (C) 2017 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_PSK_DB_H_
8#define BOTAN_PSK_DB_H_
9
10#include <botan/secmem.h>
11#include <memory>
12#include <set>
13#include <string>
14
15namespace Botan {
16
17class BlockCipher;
18class MessageAuthenticationCode;
19
20/**
21* This is an interface to a generic PSK (pre-shared key) database.
22* It might be implemented as a plaintext storage or via some mechanism
23* that encrypts the keys and/or values.
24*/
26 public:
27 /**
28 * Return the set of names for which get() will return a value.
29 */
30 virtual std::set<std::string> list_names() const = 0;
31
32 /**
33 * Return the value associated with the specified @param name or otherwise
34 * throw an exception.
35 */
36 virtual secure_vector<uint8_t> get(std::string_view name) const = 0;
37
38 /**
39 * Set a value that can later be accessed with get().
40 * If name already exists in the database, the old value will be overwritten.
41 */
42 virtual void set(std::string_view name, const uint8_t psk[], size_t psk_len) = 0;
43
44 /**
45 * Remove a PSK from the database
46 */
47 virtual void remove(std::string_view name) = 0;
48
49 /**
50 * Returns if the values in the PSK database are encrypted. If
51 * false, saved values are being stored in plaintext.
52 */
53 virtual bool is_encrypted() const = 0;
54
55 /**
56 * Get a PSK in the form of a string (eg if the PSK is a password)
57 */
58 std::string get_str(std::string_view name) const;
59
60 void set_str(std::string_view name, std::string_view psk);
61
62 template <typename Alloc>
63 void set_vec(std::string_view name, const std::vector<uint8_t, Alloc>& psk) {
64 set(name, psk.data(), psk.size());
65 }
66
67 virtual ~PSK_Database() = default;
68};
69
70/**
71* A mixin for an encrypted PSK database.
72* Both keys and values are encrypted with NIST AES-256 key wrapping.
73* Values are padded to obscure their length before encryption, allowing
74* it to be used as a password vault.
75*
76* Subclasses must implement the virtual calls to handle storing and
77* getting raw (base64 encoded) values.
78*/
80 public:
81 /**
82 * @param master_key specifies the master key used to encrypt all
83 * keys and value. It can be of any length, but should be at least 256 bits.
84 *
85 * Subkeys for the cryptographic algorithms used are derived from this
86 * master key. No key stretching is performed; if encrypting a PSK database
87 * using a password, it is recommended to use PBKDF2 to derive the database
88 * master key.
89 */
91
93
94 std::set<std::string> list_names() const override;
95
96 secure_vector<uint8_t> get(std::string_view name) const override;
97
98 void set(std::string_view name, const uint8_t psk[], size_t psk_len) override;
99
100 void remove(std::string_view name) override;
101
102 bool is_encrypted() const override { return true; }
103
104 protected:
105 /**
106 * Save a encrypted (name.value) pair to the database. Both will be base64 encoded strings.
107 */
108 virtual void kv_set(std::string_view index, std::string_view value) = 0;
109
110 /**
111 * Get a value previously saved with set_raw_value. Should return an empty
112 * string if index is not found.
113 */
114 virtual std::string kv_get(std::string_view index) const = 0;
115
116 /**
117 * Remove an index
118 */
119 virtual void kv_del(std::string_view index) = 0;
120
121 /**
122 * Return all indexes in the table.
123 */
124 virtual std::set<std::string> kv_get_all() const = 0;
125
126 private:
127 std::unique_ptr<BlockCipher> m_cipher;
128 std::unique_ptr<MessageAuthenticationCode> m_hmac;
129 secure_vector<uint8_t> m_wrap_key;
130};
131
132class SQL_Database;
133
135 public:
137 std::shared_ptr<SQL_Database> db,
138 std::string_view table_name);
139
141
142 private:
143 void kv_set(std::string_view index, std::string_view value) override;
144 std::string kv_get(std::string_view index) const override;
145 void kv_del(std::string_view index) override;
146 std::set<std::string> kv_get_all() const override;
147
148 std::shared_ptr<SQL_Database> m_db;
149 const std::string m_table_name;
150};
151
152} // namespace Botan
153
154#endif
bool is_encrypted() const override
Definition psk_db.h:102
virtual void kv_set(std::string_view index, std::string_view value)=0
virtual void kv_del(std::string_view index)=0
virtual std::string kv_get(std::string_view index) const =0
virtual std::set< std::string > kv_get_all() const =0
virtual std::set< std::string > list_names() const =0
virtual void set(std::string_view name, const uint8_t psk[], size_t psk_len)=0
virtual bool is_encrypted() const =0
virtual ~PSK_Database()=default
virtual secure_vector< uint8_t > get(std::string_view name) const =0
virtual void remove(std::string_view name)=0
void set_vec(std::string_view name, const std::vector< uint8_t, Alloc > &psk)
Definition psk_db.h:63
std::string name
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61