Botan 3.11.0
Crypto and TLS for C&
asio_error.h
Go to the documentation of this file.
1/*
2* TLS Stream Errors
3* (C) 2018-2020 Jack Lloyd
4* 2018-2020 Hannes Rantzsch, Tim Oesterreich, Rene Meusel
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#ifndef BOTAN_ASIO_ERROR_H_
10#define BOTAN_ASIO_ERROR_H_
11
12#include <botan/asio_compat.h>
13#if defined(BOTAN_FOUND_COMPATIBLE_BOOST_ASIO_VERSION)
14
15 #include <boost/system/system_error.hpp>
16
17 #include <botan/exceptn.h>
18 #include <botan/tls_alert.h>
19 #include <botan/tls_exceptn.h>
20
21/*
22 * This file defines Botan-specific subclasses of boost::system::error_category.
23 * In addition to the class definition, each category class is accompanied by function `make_error_code` used to create
24 * a `boost::system::error_code` of the category from some other kind of error in Botan (for example, a TLS alert).
25 * Since error_category instances should be singletons, there's also a method to get/create the instance for each class.
26 */
27
28namespace Botan {
29
30/**
31* Generic base class wrapping boost::system::error_category and
32* adding a (bizarrely missing) virtual destructor.
33*/
34class BoostErrorCategory : public boost::system::error_category {
35 public:
36 virtual ~BoostErrorCategory() = default;
37
38 BoostErrorCategory() = default;
39 BoostErrorCategory(const BoostErrorCategory& other) = delete;
40 BoostErrorCategory(BoostErrorCategory&& other) = delete;
41 BoostErrorCategory& operator=(const BoostErrorCategory& other) = delete;
42 BoostErrorCategory& operator=(BoostErrorCategory&& other) = delete;
43};
44
45namespace TLS {
46
47// NOLINTNEXTLINE(*-use-enum-class)
48enum StreamError : uint8_t { StreamTruncated = 1 };
49
50//! @brief An error category for errors from the TLS::Stream
51class StreamCategory final : public BoostErrorCategory {
52 public:
53 const char* name() const noexcept override { return "Botan TLS Stream"; }
54
55 std::string message(int value) const override {
56 if(value == StreamTruncated) {
57 return "stream truncated";
58 } else {
59 return "generic error";
60 }
61 }
62};
63
64inline const StreamCategory& botan_stream_category() {
65 static const StreamCategory category;
66 return category;
67}
68
69inline boost::system::error_code make_error_code(Botan::TLS::StreamError e) {
70 return boost::system::error_code(static_cast<int>(e), Botan::TLS::botan_stream_category());
71}
72
73//! @brief An error category for TLS alerts
74class BotanAlertCategory final : public BoostErrorCategory {
75 public:
76 const char* name() const noexcept override { return "Botan TLS Alert"; }
77
78 std::string message(int ev) const override {
79 const Botan::TLS::Alert alert(static_cast<Botan::TLS::Alert::Type>(ev));
80 return alert.type_string();
81 }
82};
83
84inline const BotanAlertCategory& botan_alert_category() noexcept {
85 static const BotanAlertCategory category;
86 return category;
87}
88
89inline boost::system::error_code make_error_code(Botan::TLS::Alert::Type c) {
90 return boost::system::error_code(static_cast<int>(c), Botan::TLS::botan_alert_category());
91}
92
93} // namespace TLS
94
95//! @brief An error category for errors from Botan (other than TLS alerts)
96class BotanErrorCategory : public BoostErrorCategory {
97 public:
98 const char* name() const noexcept override { return "Botan"; }
99
100 std::string message(int ev) const override { return Botan::to_string(static_cast<Botan::ErrorType>(ev)); }
101};
102
103inline const BotanErrorCategory& botan_category() noexcept {
104 static const BotanErrorCategory category;
105 return category;
106}
107
108inline boost::system::error_code make_error_code(Botan::ErrorType e) {
109 return boost::system::error_code(static_cast<int>(e), Botan::botan_category());
110}
111
112} // namespace Botan
113
114/*
115 * Add a template specialization of `is_error_code_enum` for each kind of error to allow automatic conversion to an
116 * error code.
117 */
118namespace boost::system {
119
120template <>
121struct is_error_code_enum<Botan::TLS::Alert::Type> {
122 static const bool value = true;
123};
124
125template <>
126struct is_error_code_enum<Botan::TLS::StreamError> {
127 static const bool value = true;
128};
129
130template <>
131struct is_error_code_enum<Botan::ErrorType> {
132 static const bool value = true;
133};
134
135} // namespace boost::system
136
137#endif
138#endif // BOTAN_ASIO_ERROR_H_
AlertType Type
Definition tls_alert.h:72
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition exceptn.cpp:13
ErrorType
Definition exceptn.h:21