Botan 3.4.0
Crypto and TLS for C&
tls_alert.cpp
Go to the documentation of this file.
1/*
2* Alert Message
3* (C) 2004-2006,2011 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/tls_alert.h>
9
10#include <botan/tls_exceptn.h>
11
12namespace Botan::TLS {
13
15 if(buf.size() != 2) {
16 throw Decoding_Error("Bad size (" + std::to_string(buf.size()) + ") for TLS alert message");
17 }
18
19 if(buf[0] == 1) {
20 m_fatal = false;
21 } else if(buf[0] == 2) {
22 m_fatal = true;
23 } else {
24 throw TLS_Exception(Alert::IllegalParameter, "Bad code for TLS alert level");
25 }
26
27 const uint8_t dc = buf[1];
28
29 m_type_code = static_cast<Type>(dc);
30}
31
32std::vector<uint8_t> Alert::serialize() const {
33 return std::vector<uint8_t>({static_cast<uint8_t>(is_fatal() ? 2 : 1), static_cast<uint8_t>(type())});
34}
35
36namespace {
37
38const char* alert_type_to_string(AlertType type) {
39 switch(type) {
41 return "close_notify";
43 return "unexpected_message";
45 return "bad_record_mac";
47 return "decryption_failed";
49 return "record_overflow";
51 return "decompression_failure";
53 return "handshake_failure";
55 return "no_certificate";
57 return "bad_certificate";
59 return "unsupported_certificate";
61 return "certificate_revoked";
63 return "certificate_expired";
65 return "certificate_unknown";
67 return "illegal_parameter";
69 return "unknown_ca";
71 return "access_denied";
73 return "decode_error";
75 return "decrypt_error";
77 return "export_restriction";
79 return "protocol_version";
81 return "insufficient_security";
83 return "internal_error";
85 return "inappropriate_fallback";
87 return "user_canceled";
89 return "no_renegotiation";
91 return "missing_extension";
93 return "unsupported_extension";
95 return "certificate_unobtainable";
97 return "unrecognized_name";
99 return "bad_certificate_status_response";
101 return "bad_certificate_hash_value";
103 return "unknown_psk_identity";
105 return "certificate_required";
107 return "no_application_protocol";
108
109 case AlertType::None:
110 return "none";
111 }
112
113 return nullptr;
114}
115
116} // namespace
117
118std::string Alert::type_string() const {
119 if(const char* known_alert = alert_type_to_string(type())) {
120 return std::string(known_alert);
121 }
122
123 return "unrecognized_alert_" + std::to_string(static_cast<size_t>(type()));
124}
125
126} // namespace Botan::TLS
std::vector< uint8_t > serialize() const
Definition tls_alert.cpp:32
std::string type_string() const
bool is_fatal() const
Definition tls_alert.h:88
Type type() const
Definition tls_alert.h:93
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61