OpenVPN 3 Core Library
Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// mbed TLS exception class that allows a error code
13// to be represented.
14
15#ifndef OPENVPN_MBEDTLS_UTIL_ERROR_H
16#define OPENVPN_MBEDTLS_UTIL_ERROR_H
17
18#include <string>
19
20#include <mbedtls/ssl.h>
21#include <mbedtls/pem.h>
22#include <mbedtls/error.h>
23
28
29namespace openvpn {
30
31// string exception class
33{
34 public:
36 : errtxt("mbed TLS"), errnum(0)
37 {
38 }
39
40 explicit MbedTLSException(const std::string &error_text)
41 : errnum(0)
42 {
43 errtxt = "mbed TLS: " + error_text;
44 }
45
46 explicit MbedTLSException(const std::string &error_text, const Error::Type code, const bool fatal)
48 {
49 errtxt = "mbed TLS: " + error_text;
50 }
51
52 explicit MbedTLSException(const std::string &error_text, const int mbedtls_errnum)
53 : errnum(mbedtls_errnum)
54 {
55 errtxt = "mbed TLS: " + error_text + " : " + mbedtls_errtext(mbedtls_errnum);
56
57 // cite forum URL for mbed TLS invalid date
58 // TODO: Get a better URL for such knowledge information record
59 if (mbedtls_errnum == MBEDTLS_ERR_X509_INVALID_DATE)
60 errtxt += ", please see https://forums.openvpn.net/viewtopic.php?f=36&t=21873 for more info";
61
62 // for certain mbed TLS errors, translate them to an OpenVPN error code,
63 // so they can be propagated up to the higher levels (such as UI level)
64 switch (errnum)
65 {
66 case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
68 break;
69 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
70 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
72 break;
75 break;
76 }
77 }
78
79 const char *what() const noexcept override
80 {
81 return errtxt.c_str();
82 }
83 std::string what_str() const
84 {
85 return errtxt;
86 }
87
88 int get_errnum() const
89 {
90 return errnum;
91 }
92
93 virtual ~MbedTLSException() noexcept = default;
94
95 static std::string mbedtls_errtext(int errnum)
96 {
97 char buf[256];
98 mbedtls_strerror(errnum, buf, sizeof(buf));
99 return buf;
100 }
101
102 static std::string mbedtls_verify_flags_errtext(const uint32_t flags)
103 {
104 // get string rendition of flags
105 const size_t BUF_SIZE = 1024;
106 std::unique_ptr<char[]> buf(new char[BUF_SIZE]);
107 buf[0] = '\0';
108 mbedtls_x509_crt_verify_info(buf.get(), BUF_SIZE, "", flags);
109
110 // postprocess string
111 std::string ret;
112 ret.reserve(std::strlen(buf.get()) + 64);
113 bool newline = false;
114 for (size_t i = 0; i < BUF_SIZE; ++i)
115 {
116 const char c = buf[i];
117 if (c == '\0')
118 break;
119 else if (c == '\n')
120 newline = true;
121 else
122 {
123 if (newline)
124 {
125 ret += ", ";
126 newline = false;
127 }
128 ret += c;
129 }
130 }
131 return ret;
132 }
133
134 private:
135 std::string errtxt;
137};
138} // namespace openvpn
139
140#endif
bool fatal() const
Definition excode.hpp:51
Error::Type code() const
Definition excode.hpp:47
void set_code(const Error::Type code)
Definition excode.hpp:36
virtual ~MbedTLSException() noexcept=default
const char * what() const noexcept override
Definition error.hpp:79
MbedTLSException(const std::string &error_text, const int mbedtls_errnum)
Definition error.hpp:52
std::string what_str() const
Definition error.hpp:83
static std::string mbedtls_errtext(int errnum)
Definition error.hpp:95
MbedTLSException(const std::string &error_text)
Definition error.hpp:40
MbedTLSException(const std::string &error_text, const Error::Type code, const bool fatal)
Definition error.hpp:46
static std::string mbedtls_verify_flags_errtext(const uint32_t flags)
Definition error.hpp:102
int get_errnum() const
Definition error.hpp:88
#define MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION
@ PEM_PASSWORD_FAIL
Definition error.hpp:79
reroute_gw flags
std::string ret