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 | // Copyright (C) 2021-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
/// @file botan_tls.cc Botan ASIO implementation of the TLS API.
#ifdef WITH_BOTAN
#include <asiolink/asio_wrapper.h>
#include <asiolink/crypto_tls.h>
#include <botan/auto_rng.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <botan/certstor_flatfile.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <botan/data_src.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <botan/pem.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <botan/pkcs8.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc::cryptolink;
namespace isc {
namespace asiolink {
// Classes of Kea certificate stores.
using KeaCertificateStorePath = Botan::Certificate_Store_In_Memory;
using KeaCertificateStoreFile = Botan::Flatfile_Certificate_Store;
// Class of Kea credential managers.
class KeaCredentialsManager : public Botan::Credentials_Manager {
public:
// Constructor.
KeaCredentialsManager() : store_(), use_stores_(true), certs_(), key_() {
}
// Destructor.
virtual ~KeaCredentialsManager() {
}
// CA certificate stores.
// nullptr means do not require or check peer certificate.
std::vector<Botan::Certificate_Store*>
trusted_certificate_authorities(const std::string&,
const std::string&) override {
std::vector<Botan::Certificate_Store*> result;
if (use_stores_ && store_) {
result.push_back(store_.get());
}
return (result);
}
// Certificate chain.
std::vector<Botan::X509_Certificate>
cert_chain(const std::vector<std::string>&,
const std::string&,
const std::string&) override {
return (certs_);
}
// Private key.
Botan::Private_Key*
private_key_for(const Botan::X509_Certificate&,
const std::string&,
const std::string&) override {
return (key_.get());
}
// Set the store from a path.
void setStorePath(const std::string& path) {
store_.reset(new KeaCertificateStorePath(path));
}
// Set the store from a file.
void setStoreFile(const std::string& file) {
store_.reset(new KeaCertificateStoreFile(file));
}
// Get the use of CA certificate stores flag.
bool getUseStores() const {
return (use_stores_);
}
// Set the use of CA certificate stores flag.
void setUseStores(bool use_stores) {
use_stores_ = use_stores;
}
// Set the certificate chain.
void setCertChain(const std::string& file) {
Botan::DataSource_Stream source(file);
certs_.clear();
while (!source.end_of_data()) {
std::string label;<--- The scope of the variable 'label' can be reduced. [+]The scope of the variable 'label' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
std::vector<uint8_t> cert;<--- The scope of the variable 'cert' can be reduced. [+]The scope of the variable 'cert' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
try {
cert = unlock(Botan::PEM_Code::decode(source, label));
if ((label != "CERTIFICATE") &&
(label != "X509 CERTIFICATE") &&
(label != "TRUSTED CERTIFICATE")) {
isc_throw(LibraryError, "Expected a certificate, got '"
<< label << "'");
}
certs_.push_back(Botan::X509_Certificate(cert));
} catch (const std::exception& ex) {
if (certs_.empty()) {
throw;
}
// Got one certificate so skipping garbage.
continue;
}
}
if (certs_.empty()) {
isc_throw(LibraryError, "Found no certificate?");
}
}
// Set the private key.
void setPrivateKey(const std::string& file,
Botan::RandomNumberGenerator& rng,
bool& is_rsa) {
key_.reset(Botan::PKCS8::load_key(file, rng));
if (!key_) {
isc_throw(Unexpected,
"Botan::PKCS8::load_key failed but not threw?");
}
is_rsa = (key_->algo_name() == "RSA");
}
// Pointer to the CA certificate store.
std::unique_ptr<Botan::Certificate_Store> store_;
// Use the CA certificate store flag.
bool use_stores_;
// The certificate chain.
std::vector<Botan::X509_Certificate> certs_;
// Pointer to the private key.
std::unique_ptr<Botan::Private_Key> key_;
};
// Class of Kea policy.
// Use Strict_Policy?
class KeaPolicy : public Botan::TLS::Default_Policy {
public:
// Constructor.
KeaPolicy() : prefer_rsa_(true) {
}
// Destructor.
virtual ~KeaPolicy() {
}
// Allowed signature methods in preference order.
std::vector<std::string> allowed_signature_methods() const override {
if (prefer_rsa_) {
return (AllowedSignatureMethodsRSA);
} else {
return (AllowedSignatureMethodsECDSA);
}
}
// Disable OSCP.
bool require_cert_revocation_info() const override {
return false;
}
// Set the RSA preferred flag.
void setPrefRSA(bool prefer_rsa) {
prefer_rsa_ = prefer_rsa;
}
// Prefer RSA preferred flag.
bool prefer_rsa_;
// Allowed signature methods which prefers RSA.
static const std::vector<std::string> AllowedSignatureMethodsRSA;
// Allowed signature methods which prefers ECDSA.
static const std::vector<std::string> AllowedSignatureMethodsECDSA;
};
// Kea session manager.
using KeaSessionManager = Botan::TLS::Session_Manager_Noop;
// Allowed signature methods which prefers RSA.
const std::vector<std::string>
KeaPolicy::AllowedSignatureMethodsRSA = { "RSA", "DSA", "ECDSA" };
// Allowed signature methods which prefers ECDSA.
const std::vector<std::string>
KeaPolicy::AllowedSignatureMethodsECDSA = { "ECDSA", "RSA", "DSA" };
// Class of Botan TLS context implementations.
class TlsContextImpl {
public:
// Constructor.
TlsContextImpl() : cred_mgr_(), rng_(), sess_mgr_(), policy_() {
}
// Destructor.
virtual ~TlsContextImpl() {
}
// Get the peer certificate requirement mode.
virtual bool getCertRequired() const {
return (cred_mgr_.getUseStores());
}
// Set the peer certificate requirement mode.
//
// With Botan this means to provide or not the CA certificate stores.
virtual void setCertRequired(bool cert_required) {
cred_mgr_.setUseStores(cert_required);
}
// Load the trust anchor aka certificate authority (path).
virtual void loadCaPath(const std::string& ca_path) {
try {
cred_mgr_.setStorePath(ca_path);
} catch (const std::exception& ex) {
isc_throw(LibraryError, ex.what());
}
}
// Load the trust anchor aka certificate authority (file).
virtual void loadCaFile(const std::string& ca_file) {
try {
cred_mgr_.setStoreFile(ca_file);
} catch (const std::exception& ex) {
isc_throw(LibraryError, ex.what());
}
}
/// @brief Load the certificate file.
virtual void loadCertFile(const std::string& cert_file) {
try {
cred_mgr_.setCertChain(cert_file);
} catch (const std::exception& ex) {
isc_throw(LibraryError, ex.what());
}
}
/// @brief Load the private key file.
///
/// As a side effect set the preference for RSA in the policy.
virtual void loadKeyFile(const std::string& key_file) {
try {
bool is_rsa = true;
cred_mgr_.setPrivateKey(key_file, rng_, is_rsa);
policy_.setPrefRSA(is_rsa);
} catch (const std::exception& ex) {
isc_throw(LibraryError, ex.what());
}
}
// Build the context if not yet done.
virtual void build() {
if (context_) {
return;
}
context_.reset(new Botan::TLS::Context(cred_mgr_,
rng_,
sess_mgr_,
policy_));
}
virtual Botan::TLS::Context& get() {
return (*context_);
}
// Credentials Manager.
KeaCredentialsManager cred_mgr_;
// Random Number Generator.
Botan::AutoSeeded_RNG rng_;
// Session Manager.
KeaSessionManager sess_mgr_;
KeaPolicy policy_;
std::unique_ptr<Botan::TLS::Context> context_;
};
TlsContext::~TlsContext() {
}
TlsContext::TlsContext(TlsRole role)
: TlsContextBase(role), impl_(new TlsContextImpl()) {
}
Botan::TLS::Context&
TlsContext::getContext() {
impl_->build();
return (impl_->get());
}
void
TlsContext::setCertRequired(bool cert_required) {
if (!cert_required && (getRole() == TlsRole::CLIENT)) {
isc_throw(BadValue,
"'cert-required' parameter must be true for a TLS client");
}
impl_->setCertRequired(cert_required);
}
bool
TlsContext::getCertRequired() const {
return (impl_->getCertRequired());
}
void
TlsContext::loadCaFile(const std::string& ca_file) {
impl_->loadCaFile(ca_file);
}
void
TlsContext::loadCaPath(const std::string& ca_path) {
impl_->loadCaPath(ca_path);
}
void
TlsContext::loadCertFile(const std::string& cert_file) {
impl_->loadCertFile(cert_file);
}
void
TlsContext::loadKeyFile(const std::string& key_file) {
impl_->loadKeyFile(key_file);
}
} // namespace asiolink
} // namespace isc
#endif // WITH_BOTAN
|