Kea  1.9.9-git
common_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 #include <asiolink/asio_wrapper.h>
12 #include <asiolink/crypto_tls.h>
13 
14 #include <sys/stat.h>
15 
16 using namespace isc::cryptolink;
17 
18 namespace { // anonymous namespace
19 
20 // C++17 has this function but Kea is still C++11 so provide it.
21 bool
22 isDir(const std::string& name) {
23  struct stat stats;
24  if (::stat(name.c_str(), &stats) < 0) {
25  return (false);
26  }
27  return ((stats.st_mode & S_IFMT) == S_IFDIR);
28 }
29 
30 } // end of namespace
31 
32 namespace isc {
33 namespace asiolink {
34 
35 void
36 TlsContextBase::configure(TlsContextPtr& context,
37  TlsRole role,
38  const std::string& ca_file,
39  const std::string& cert_file,
40  const std::string& key_file,
41  bool cert_required) {
42  try {
43  context.reset(new TlsContext(role));
44  context->setCertRequired(cert_required);
45  if (isDir(ca_file)) {
46  try {
47  context->loadCaPath(ca_file);
48  } catch (const std::exception& ex) {
49  isc_throw(isc::BadValue, "load of CA directory '"
50  << ca_file << "' failed: " << ex.what());
51  }
52  } else {
53  try {
54  context->loadCaFile(ca_file);
55  } catch (const std::exception& ex) {
56  isc_throw(isc::BadValue, "load of CA file '"
57  << ca_file << "' failed: " << ex.what());
58  }
59  }
60  try {
61  context->loadCertFile(cert_file);
62  } catch (const std::exception& ex) {
63  isc_throw(isc::BadValue, "load of cert file '"
64  << cert_file << "' failed: " << ex.what());
65  }
66  try {
67  context->loadKeyFile(key_file);
68  } catch (const std::exception& ex) {
69  isc_throw(isc::BadValue, "load of private key file '"
70  << key_file << "' failed: " << ex.what());
71  }
72  } catch (...) {
73  context.reset();
74  throw;
75  }
76 }
77 
78 } // namespace asiolink
79 } // namespace isc
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Defines the logger used by the top-level component of kea-dhcp-ddns.
TLS API.