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