Kea  2.1.7-git
botan_link.cc
Go to the documentation of this file.
1 // Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
10 #include <cryptolink/crypto_hash.h>
11 #include <cryptolink/crypto_hmac.h>
12 #include <cryptolink/crypto_rng.h>
13 
14 #include <botan/exceptn.h>
15 #include <botan/version.h>
16 #include <botan/auto_rng.h>
17 
18 namespace isc {
19 namespace cryptolink {
20 
21 // For Botan, we use the CryptoLink class object in RAII style
23  // empty class
24 };
25 
26 CryptoLink::~CryptoLink() {
27 }
28 
30 class RNGImpl : public RNG {
31 public:
32  RNGImpl() {
33  rng.reset(new Botan::AutoSeeded_RNG());
34  }
35 
37  }
38 
39 private:
40  std::vector<uint8_t> random(size_t len) {
41  std::vector<uint8_t> data;
42  if (len > 0) {
43  data.resize(len);
44  try {
45  rng->randomize(&data[0], len);
46  } catch (const Botan::Exception& ex) {
48  "Botan error: " << ex.what());
49  }
50  }
51  return (data);
52  }
53 
54  boost::shared_ptr<Botan::RandomNumberGenerator> rng;
55 };
56 
57 void
58 CryptoLink::initialize(CryptoLink& c) {
59  if (!c.impl_) {
60  try {
61  c.impl_.reset(new CryptoLinkImpl());
62  } catch (const Botan::Exception& ex) {
63  isc_throw(InitializationError, "Botan error: " << ex.what());
64  }
65  }
66  if (!c.rng_) {
67  try {
68  c.rng_.reset(new RNGImpl());
69  } catch (const Botan::Exception& ex) {
70  isc_throw(InitializationError, "Botan error: " << ex.what());
71  }
72  }
73  // A not yet fixed bug makes RNG to be destroyed after memory pool...
74  atexit([]{ getCryptoLink().getRNG().reset(); });
75 }
76 
77 std::string
79  return (Botan::version_string());
80 }
81 
82 } // namespace cryptolink
83 } // namespace isc
84 
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the logger used by the top-level component of kea-lfc.