Kea  1.9.9-git
openssl_tls.cc
Go to the documentation of this file.
1 // Copyright (C) 2021 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 
8 
9 #include <config.h>
10 
11 #ifdef WITH_OPENSSL
12 
13 #include <asiolink/asio_wrapper.h>
14 #include <asiolink/crypto_tls.h>
15 
16 #include <sys/stat.h>
17 
18 #include <openssl/opensslv.h>
19 
20 using namespace boost::asio;
21 using namespace boost::asio::ssl;
22 using namespace boost::system;
23 using namespace isc::cryptolink;
24 
25 namespace isc {
26 namespace asiolink {
27 
28 // Enforce TLS 1.2 when the generic TLS method is not available (i.e.
29 // the boost version is older than 1.64.0).
30 TlsContext::TlsContext(TlsRole role)
31  : TlsContextBase(role), cert_required_(true),
32 #ifdef HAVE_GENERIC_TLS_METHOD
33  context_(context::method::tls)
34 #else
35 #ifdef HAVE_TLS_1_2_METHOD
36  context_(context::method::tlsv12)
37 #else
38  context_(context::method::tlsv1)
39 #endif
40 #endif
41 {
42  // Not leave the verify mode to OpenSSL default.
43  setCertRequired(true);
44 }
45 
46 boost::asio::ssl::context&
47 TlsContext::getContext() {
48  return (context_);
49 }
50 
51 ::SSL_CTX*
52 TlsContext::getNativeContext() {
53  return (context_.native_handle());
54 }
55 
56 void
57 TlsContext::setCertRequired(bool cert_required) {
58  if (!cert_required && (getRole() == TlsRole::CLIENT)) {
59  isc_throw(BadValue,
60  "'cert-required' parameter must be true for a TLS client");
61  }
62  cert_required_ = cert_required;
63  error_code ec;
64  int mode = verify_peer | verify_fail_if_no_peer_cert;
65  if (!cert_required_) {
66  mode = verify_none;
67  }
68  context_.set_verify_mode(mode, ec);
69  if (ec) {
70  isc_throw(LibraryError, ec.message());
71  }
72 }
73 
74 bool
75 TlsContext::getCertRequired() const {
76  return (cert_required_);
77 }
78 
79 void
80 TlsContext::loadCaFile(const std::string& ca_file) {
81  error_code ec;
82  context_.load_verify_file(ca_file, ec);
83  if (ec) {
84  isc_throw(LibraryError, ec.message());
85  }
86 }
87 
88 void
89 TlsContext::loadCaPath(const std::string& ca_path) {
90  error_code ec;
91  context_.add_verify_path(ca_path, ec);
92  if (ec) {
93  isc_throw(LibraryError, ec.message());
94  }
95 }
96 
97 void
98 TlsContext::loadCertFile(const std::string& cert_file) {
99  error_code ec;
100  context_.use_certificate_chain_file(cert_file, ec);
101  if (ec) {
102  isc_throw(LibraryError, ec.message());
103  }
104 }
105 
106 void
107 TlsContext::loadKeyFile(const std::string& key_file) {
108  error_code ec;
109  context_.use_private_key_file(key_file, context::file_format::pem, ec);
110  if (ec) {
111  isc_throw(LibraryError, ec.message());
112  }
113 }
114 
115 } // namespace asiolink
116 } // namespace isc
117 
118 #endif // WITH_OPENSSL
#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-dhcp-ddns.
TLS API.