Botan 3.9.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
47enum StreamError : uint8_t { StreamTruncated = 1 };
48
49//! @brief An error category for errors from the TLS::Stream
50class StreamCategory final : public BoostErrorCategory {
51 public:
52 const char* name() const noexcept override { return "Botan TLS Stream"; }
53
54 std::string message(int value) const override {
55 if(value == StreamTruncated) {
56 return "stream truncated";
57 } else {
58 return "generic error";
59 }
60 }
61};
62
63inline const StreamCategory& botan_stream_category() {
64 static StreamCategory category;
65 return category;
66}
67
68inline boost::system::error_code make_error_code(Botan::TLS::StreamError e) {
69 return boost::system::error_code(static_cast<int>(e), Botan::TLS::botan_stream_category());
70}
71
72//! @brief An error category for TLS alerts
73class BotanAlertCategory final : public BoostErrorCategory {
74 public:
75 const char* name() const noexcept override { return "Botan TLS Alert"; }
76
77 std::string message(int ev) const override {
78 Botan::TLS::Alert alert(static_cast<Botan::TLS::Alert::Type>(ev));
79 return alert.type_string();
80 }
81};
82
83inline const BotanAlertCategory& botan_alert_category() noexcept {
84 static BotanAlertCategory category;
85 return category;
86}
87
88inline boost::system::error_code make_error_code(Botan::TLS::Alert::Type c) {
89 return boost::system::error_code(static_cast<int>(c), Botan::TLS::botan_alert_category());
90}
91
92} // namespace TLS
93
94//! @brief An error category for errors from Botan (other than TLS alerts)
95class BotanErrorCategory : public BoostErrorCategory {
96 public:
97 const char* name() const noexcept override { return "Botan"; }
98
99 std::string message(int ev) const override { return Botan::to_string(static_cast<Botan::ErrorType>(ev)); }
100};
101
102inline const BotanErrorCategory& botan_category() noexcept {
103 static BotanErrorCategory category;
104 return category;
105}
106
107inline boost::system::error_code make_error_code(Botan::ErrorType e) {
108 return boost::system::error_code(static_cast<int>(e), Botan::botan_category());
109}
110
111} // namespace Botan
112
113/*
114 * Add a template specialization of `is_error_code_enum` for each kind of error to allow automatic conversion to an
115 * error code.
116 */
117namespace boost::system {
118
119template <>
120struct is_error_code_enum<Botan::TLS::Alert::Type> {
121 static const bool value = true;
122};
123
124template <>
125struct is_error_code_enum<Botan::TLS::StreamError> {
126 static const bool value = true;
127};
128
129template <>
130struct is_error_code_enum<Botan::ErrorType> {
131 static const bool value = true;
132};
133
134} // namespace boost::system
135
136#endif
137#endif // BOTAN_ASIO_ERROR_H_
AlertType Type
Definition tls_alert.h:71
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition exceptn.cpp:13
ErrorType
Definition exceptn.h:20