Kea  2.1.7-git
common_tls.cc
Go to the documentation of this file.
1 // Copyright (C) 2021-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 
8 
9 #include <config.h>
10 
11 #include <asiolink/asio_wrapper.h>
12 #include <asiolink/crypto_tls.h>
13 #include <util/file_utilities.h>
14 
15 using namespace isc::cryptolink;
16 using namespace isc::util;
17 
18 namespace isc {
19 namespace asiolink {
20 
21 void
22 TlsContextBase::configure(TlsContextPtr& context,
23  TlsRole role,
24  const std::string& ca_file,
25  const std::string& cert_file,
26  const std::string& key_file,
27  bool cert_required) {
28  try {
29  context.reset(new TlsContext(role));
30  context->setCertRequired(cert_required);
31  if (file::isDir(ca_file)) {
32  try {
33  context->loadCaPath(ca_file);
34  } catch (const std::exception& ex) {
35  isc_throw(isc::BadValue, "load of CA directory '"
36  << ca_file << "' failed: " << ex.what());
37  }
38  } else {
39  try {
40  context->loadCaFile(ca_file);
41  } catch (const std::exception& ex) {
42  isc_throw(isc::BadValue, "load of CA file '"
43  << ca_file << "' failed: " << ex.what());
44  }
45  }
46  try {
47  context->loadCertFile(cert_file);
48  } catch (const std::exception& ex) {
49  isc_throw(isc::BadValue, "load of cert file '"
50  << cert_file << "' failed: " << ex.what());
51  }
52  try {
53  context->loadKeyFile(key_file);
54  } catch (const std::exception& ex) {
55  isc_throw(isc::BadValue, "load of private key file '"
56  << key_file << "' failed: " << ex.what());
57  }
58  } catch (...) {
59  context.reset();
60  throw;
61  }
62 }
63 
64 } // namespace asiolink
65 } // 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.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition: edns.h:19
bool isDir(const string &name)
Is a directory predicate.
Defines the logger used by the top-level component of kea-lfc.
TLS API.