Kea  2.3.2-git
openssl_link.cc
Go to the documentation of this file.
1 // Copyright (C) 2014-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_rng.h>
11 #include <cryptolink/crypto_hash.h>
12 #include <cryptolink/crypto_hmac.h>
13 
14 #include <openssl/crypto.h>
15 #include <openssl/rand.h>
16 
17 namespace isc {
18 namespace cryptolink {
19 
20 // For OpenSSL, we use the CryptoLink class object in RAII style
21 class CryptoLinkImpl {
22 };
23 
24 CryptoLink::~CryptoLink() {
25 }
26 
28 class RNGImpl : public RNG {
29 public:
30  RNGImpl() { }
31 
32  ~RNGImpl() { }
33 
34 private:
35  std::vector<uint8_t> random(size_t len) {
36  std::vector<uint8_t> data;
37  if (len > 0) {
38  data.resize(len);
39  if (RAND_bytes(&data[0], len) != 1) {
41  "OpenSSL RAND_bytes() failed");
42  }
43  }
44  return (data);
45  }
46 };
47 
48 void
49 CryptoLink::initialize(CryptoLink& c) {
50  if (!c.impl_) {
51  try {
52  c.impl_.reset(new CryptoLinkImpl());
53  } catch (const std::exception &ex) {
54  // Should never happen
56  "Error during OpenSSL initialization:" << ex.what());
57  } catch (...) {
58  // Should never happen
60  "Error during OpenSSL initialization");
61  }
62  }
63  if (!c.rng_) {
64  try {
65  c.rng_.reset(new RNGImpl());
66  } catch (const std::exception &ex) {
67  // Should never happen
69  "Error during OpenSSL RNG initialization:" << ex.what());
70  } catch (...) {
71  // Should never happen
73  "Error during OpenSSL RNG initialization");
74  }
75  }
76 }
77 
78 std::string
80  return (SSLeay_version(SSLEAY_VERSION));
81 }
82 
83 } // namespace cryptolink
84 } // namespace isc
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.