Kea: openssl_link.cc Source File

| Kea 3.2.0-git

openssl_link.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2026 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
9#include <cryptolink/cryptolink.h>
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
17namespace isc {
18namespace cryptolink {
19
20// For OpenSSL, we use the CryptoLink class object in RAII style
21class CryptoLinkImpl {
22};
23
24CryptoLink::~CryptoLink() {
25}
26
28class RNGImpl : public RNG {
29public:
30 RNGImpl() { }
31
32 ~RNGImpl() { }
33
34private:
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) {
40 isc_throw(isc::cryptolink::LibraryError,
41 "OpenSSL RAND_bytes() failed");
42 }
43 }
44 return (data);
45 }
46};
47
48void
49CryptoLink::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
55 isc_throw(InitializationError,
56 "Error during OpenSSL initialization:" << ex.what());
57 } catch (...) {
58 // Should never happen
59 isc_throw(InitializationError,
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
68 isc_throw(InitializationError,
69 "Error during OpenSSL RNG initialization:" << ex.what());
70 } catch (...) {
71 // Should never happen
72 isc_throw(InitializationError,
73 "Error during OpenSSL RNG initialization");
74 }
75 }
76}
77
78std::string
79 CryptoLink::getVersion() {
80#ifdef HAVE_OPENSSL_VERSION
81 return (OpenSSL_version(OPENSSL_VERSION));
82#else
83 return (SSLeay_version(SSLEAY_VERSION));
84#endif
85}
86
87} // namespace cryptolink
88} // namespace isc
isc::cryptolink::CryptoLinkImpl
Definition botan_link.cc:22
isc::cryptolink::CryptoLink
Singleton entry point and factory class.
Definition cryptolink.h:136
isc::cryptolink::CryptoLink::getVersion
static std::string getVersion()
Get version string.
Definition botan_link.cc:76
isc::cryptolink::LibraryError
This exception is raised when a general error that was not specifically caught is thrown by the under...
Definition cryptolink.h:86
isc::cryptolink::RNGImpl
Botan implementation of RNG.
Definition botan_link.cc:30
isc::cryptolink::RNGImpl::RNGImpl
RNGImpl()
Definition openssl_link.cc:30
isc::cryptolink::RNGImpl::~RNGImpl
~RNGImpl()
Definition openssl_link.cc:32
isc::cryptolink::RNG
RNG support.
Definition crypto_rng.h:22
crypto_hash.h
crypto_hmac.h
crypto_rng.h
cryptolink.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::cryptolink
Definition botan_common.h:8
isc::cryptolink::random
std::vector< uint8_t > random(size_t len)
Generate random value.
Definition crypto_rng.cc:26
isc::data
Definition base_stamped_element.cc:12
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18