Botan 3.0.0-alpha0
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/build.h>
13
14#include <boost/version.hpp>
15#if BOOST_VERSION >= 106600
16
17#include <boost/system/system_error.hpp>
18
19#include <botan/exceptn.h>
20#include <botan/tls_alert.h>
21#include <botan/tls_exceptn.h>
22
23/*
24 * This file defines Botan-specific subclasses of boost::system::error_category.
25 * In addition to the class definition, each category class is accompanied by function `make_error_code` used to create
26 * a `boost::system::error_code` of the category from some other kind of error in Botan (for example, a TLS alert).
27 * Since error_category instances should be singletons, there's also a method to get/create the instance for each class.
28 */
29
30namespace Botan {
31namespace TLS {
32
34 {
36 };
37
38//! @brief An error category for errors from the TLS::Stream
39struct StreamCategory : public boost::system::error_category
40 {
41 virtual ~StreamCategory() = default;
42
43 const char* name() const noexcept override
44 {
45 return "Botan TLS Stream";
46 }
47
48 std::string message(int value) const override
49 {
50 switch(value)
51 {
52 case StreamTruncated:
53 return "stream truncated";
54 default:
55 return "generic error";
56 }
57 }
58 };
59
61 {
62 static StreamCategory category;
63 return category;
64 }
65
66inline boost::system::error_code make_error_code(Botan::TLS::StreamError e)
67 {
68 return boost::system::error_code(static_cast<int>(e), Botan::TLS::botan_stream_category());
69 }
70
71//! @brief An error category for TLS alerts
72struct BotanAlertCategory : boost::system::error_category
73 {
74 virtual ~BotanAlertCategory() = default;
75
76 const char* name() const noexcept override
77 {
78 return "Botan TLS Alert";
79 }
80
81 std::string message(int ev) const override
82 {
83 Botan::TLS::Alert alert(static_cast<Botan::TLS::Alert::Type>(ev));
84 return alert.type_string();
85 }
86 };
87
89 {
90 static BotanAlertCategory category;
91 return category;
92 }
93
94inline boost::system::error_code make_error_code(Botan::TLS::Alert::Type c)
95 {
96 return boost::system::error_code(static_cast<int>(c), Botan::TLS::botan_alert_category());
97 }
98
99} // namespace TLS
100
101//! @brief An error category for errors from Botan (other than TLS alerts)
102struct BotanErrorCategory : boost::system::error_category
103 {
104 virtual ~BotanErrorCategory() = default;
105
106 const char* name() const noexcept override
107 {
108 return "Botan";
109 }
110
111 std::string message(int ev) const override
112 {
113 return Botan::to_string(static_cast<Botan::ErrorType>(ev));
114 }
115 };
116
117inline const BotanErrorCategory& botan_category() noexcept
118 {
119 static BotanErrorCategory category;
120 return category;
121 }
122
123inline boost::system::error_code make_error_code(Botan::ErrorType e)
124 {
125 return boost::system::error_code(static_cast<int>(e), Botan::botan_category());
126 }
127
128} // namespace Botan
129
130 /*
131 * Add a template specialization of `is_error_code_enum` for each kind of error to allow automatic conversion to an
132 * error code.
133 */
134namespace boost {
135namespace system {
136
137template<> struct is_error_code_enum<Botan::TLS::Alert::Type>
138 {
139 static const bool value = true;
140 };
141
142template<> struct is_error_code_enum<Botan::TLS::StreamError>
143 {
144 static const bool value = true;
145 };
146
147template<> struct is_error_code_enum<Botan::ErrorType>
148 {
149 static const bool value = true;
150 };
151
152} // namespace system
153} // namespace boost
154
155#endif // BOOST_VERSION
156#endif // BOTAN_ASIO_ERROR_H_
std::string type_string() const
Definition: tls_alert.cpp:37
const StreamCategory & botan_stream_category()
Definition: asio_error.h:60
const BotanAlertCategory & botan_alert_category() noexcept
Definition: asio_error.h:88
@ StreamTruncated
Definition: asio_error.h:35
boost::system::error_code make_error_code(Botan::TLS::StreamError e)
Definition: asio_error.h:66
Definition: alg_id.cpp:13
ErrorType
Definition: exceptn.h:20
const BotanErrorCategory & botan_category() noexcept
Definition: asio_error.h:117
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11
boost::system::error_code make_error_code(Botan::ErrorType e)
Definition: asio_error.h:123
An error category for errors from Botan (other than TLS alerts)
Definition: asio_error.h:103
virtual ~BotanErrorCategory()=default
std::string message(int ev) const override
Definition: asio_error.h:111
const char * name() const noexcept override
Definition: asio_error.h:106
An error category for TLS alerts.
Definition: asio_error.h:73
std::string message(int ev) const override
Definition: asio_error.h:81
const char * name() const noexcept override
Definition: asio_error.h:76
virtual ~BotanAlertCategory()=default
An error category for errors from the TLS::Stream.
Definition: asio_error.h:40
virtual ~StreamCategory()=default
const char * name() const noexcept override
Definition: asio_error.h:43
std::string message(int value) const override
Definition: asio_error.h:48