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