summaryrefslogtreecommitdiffstats
path: root/comm/third_party/botan/src/lib/tls/asio/asio_error.h
blob: bdaf8473f979737a33a127fb4e7af14f75aa5363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* TLS Stream Errors
* (C) 2018-2020 Jack Lloyd
*     2018-2020 Hannes Rantzsch, Tim Oesterreich, Rene Meusel
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#ifndef BOTAN_ASIO_ERROR_H_
#define BOTAN_ASIO_ERROR_H_

#include <botan/build.h>

#include <boost/version.hpp>
#if BOOST_VERSION >= 106600

#include <boost/system/system_error.hpp>

#include <botan/exceptn.h>
#include <botan/tls_alert.h>
#include <botan/tls_exceptn.h>

/*
 * This file defines Botan-specific subclasses of boost::system::error_category.
 * In addition to the class definition, each category class is accompanied by function `make_error_code` used to create
 * a `boost::system::error_code` of the category from some other kind of error in Botan (for example, a TLS alert).
 * Since error_category instances should be singletons, there's also a method to get/create the instance for each class.
 */

namespace Botan {
namespace TLS {

enum StreamError
   {
   StreamTruncated = 1
   };

//! @brief An error category for errors from the TLS::Stream
struct StreamCategory : public boost::system::error_category
   {
   public:
      const char* name() const noexcept override
         {
         return "Botan TLS Stream";
         }

      std::string message(int value) const override
         {
         switch(value)
            {
            case StreamTruncated:
               return "stream truncated";
            default:
               return "generic error";
            }
         }
   };

inline const StreamCategory& botan_stream_category()
   {
   static StreamCategory category;
   return category;
   }

inline boost::system::error_code make_error_code(Botan::TLS::StreamError e)
   {
   return boost::system::error_code(static_cast<int>(e), Botan::TLS::botan_stream_category());
   }

//! @brief An error category for TLS alerts
struct BotanAlertCategory : boost::system::error_category
   {
   const char* name() const noexcept override
      {
      return "Botan TLS Alert";
      }

   std::string message(int ev) const override
      {
      Botan::TLS::Alert alert(static_cast<Botan::TLS::Alert::Type>(ev));
      return alert.type_string();
      }
   };

inline const BotanAlertCategory& botan_alert_category() noexcept
   {
   static BotanAlertCategory category;
   return category;
   }

inline boost::system::error_code make_error_code(Botan::TLS::Alert::Type c)
   {
   return boost::system::error_code(static_cast<int>(c), Botan::TLS::botan_alert_category());
   }

}  // namespace TLS

//! @brief An error category for errors from Botan (other than TLS alerts)
struct BotanErrorCategory : boost::system::error_category
   {
   const char* name() const noexcept override
      {
      return "Botan";
      }

   std::string message(int ev) const override
      {
      return Botan::to_string(static_cast<Botan::ErrorType>(ev));
      }
   };

inline const BotanErrorCategory& botan_category() noexcept
   {
   static BotanErrorCategory category;
   return category;
   }

inline boost::system::error_code make_error_code(Botan::ErrorType e)
   {
   return boost::system::error_code(static_cast<int>(e), Botan::botan_category());
   }

}  // namespace Botan

 /*
 * Add a template specialization of `is_error_code_enum` for each kind of error to allow automatic conversion to an
 * error code.
 */
namespace boost {
namespace system {

template<> struct is_error_code_enum<Botan::TLS::Alert::Type>
   {
   static const bool value = true;
   };

template<> struct is_error_code_enum<Botan::TLS::StreamError>
   {
   static const bool value = true;
   };

template<> struct is_error_code_enum<Botan::ErrorType>
   {
   static const bool value = true;
   };

}  // namespace system
}  // namespace boost

#endif // BOOST_VERSION
#endif // BOTAN_ASIO_ERROR_H_