1 // Copyright (C) 2014-2018 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
17 namespace isc {
18 namespace cryptolink {
19
20 // For OpenSSL, we use the CryptoLink class object in RAII style
21 class CryptoLinkImpl {
22 };
23
~CryptoLink()24 CryptoLink::~CryptoLink() {
25 delete impl_;
26 }
27
28 /// \brief OpenSSL implementation of RNG.
29 class RNGImpl : public RNG {
30 public:
RNGImpl()31 RNGImpl() { }
32
~RNGImpl()33 ~RNGImpl() { }
34
35 private:
random(size_t len)36 std::vector<uint8_t> random(size_t len) {
37 std::vector<uint8_t> data;
38 if (len > 0) {
39 data.resize(len);
40 if (RAND_bytes(&data[0], len) != 1) {
41 isc_throw(isc::cryptolink::LibraryError,
42 "OpenSSL RAND_bytes() failed");
43 }
44 }
45 return (data);
46 }
47 };
48
49 void
initialize()50 CryptoLink::initialize() {
51 CryptoLink& c = getCryptoLinkInternal();
52 if (!c.impl_) {
53 try {
54 c.impl_ = new CryptoLinkImpl();
55 } catch (const std::exception &ex) {
56 // Should never happen
57 isc_throw(InitializationError,
58 "Error during OpenSSL initialization:" << ex.what());
59 } catch (...) {
60 // Should never happen
61 isc_throw(InitializationError,
62 "Error during OpenSSL initialization");
63 }
64 }
65 if (!c.rng_) {
66 try {
67 c.rng_.reset(new RNGImpl());
68 } catch (const std::exception &ex) {
69 // Should never happen
70 isc_throw(InitializationError,
71 "Error during OpenSSL RNG initialization:" << ex.what());
72 } catch (...) {
73 // Should never happen
74 isc_throw(InitializationError,
75 "Error during OpenSSL RNG initialization");
76 }
77 }
78 }
79
80 std::string
getVersion()81 CryptoLink::getVersion() {
82 return (SSLeay_version(SSLEAY_VERSION));
83 }
84
85 } // namespace cryptolink
86 } // namespace isc
87