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