Botan  1.11.15
src/lib/tls/tls_alert.cpp
Go to the documentation of this file.
00001 /*
00002 * Alert Message
00003 * (C) 2004-2006,2011 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/tls_alert.h>
00009 #include <botan/exceptn.h>
00010 
00011 namespace Botan {
00012 
00013 namespace TLS {
00014 
00015 Alert::Alert(const secure_vector<byte>& buf)
00016    {
00017    if(buf.size() != 2)
00018       throw Decoding_Error("Alert: Bad size " + std::to_string(buf.size()) +
00019                            " for alert message");
00020 
00021    if(buf[0] == 1)      m_fatal = false;
00022    else if(buf[0] == 2) m_fatal = true;
00023    else
00024       throw Decoding_Error("Alert: Bad code for alert level");
00025 
00026    const byte dc = buf[1];
00027 
00028    m_type_code = static_cast<Type>(dc);
00029    }
00030 
00031 std::vector<byte> Alert::serialize() const
00032    {
00033    return std::vector<byte>({
00034       static_cast<byte>(is_fatal() ? 2 : 1),
00035       static_cast<byte>(type())
00036       });
00037    }
00038 
00039 std::string Alert::type_string() const
00040    {
00041    switch(type())
00042       {
00043       case CLOSE_NOTIFY:
00044          return "close_notify";
00045       case UNEXPECTED_MESSAGE:
00046          return "unexpected_message";
00047       case BAD_RECORD_MAC:
00048          return "bad_record_mac";
00049       case DECRYPTION_FAILED:
00050          return "decryption_failed";
00051       case RECORD_OVERFLOW:
00052          return "record_overflow";
00053       case DECOMPRESSION_FAILURE:
00054          return "decompression_failure";
00055       case HANDSHAKE_FAILURE:
00056          return "handshake_failure";
00057       case NO_CERTIFICATE:
00058          return "no_certificate";
00059       case BAD_CERTIFICATE:
00060          return "bad_certificate";
00061       case UNSUPPORTED_CERTIFICATE:
00062          return "unsupported_certificate";
00063       case CERTIFICATE_REVOKED:
00064          return "certificate_revoked";
00065       case CERTIFICATE_EXPIRED:
00066          return "certificate_expired";
00067       case CERTIFICATE_UNKNOWN:
00068          return "certificate_unknown";
00069       case ILLEGAL_PARAMETER:
00070          return "illegal_parameter";
00071       case UNKNOWN_CA:
00072          return "unknown_ca";
00073       case ACCESS_DENIED:
00074          return "access_denied";
00075       case DECODE_ERROR:
00076          return "decode_error";
00077       case DECRYPT_ERROR:
00078          return "decrypt_error";
00079       case EXPORT_RESTRICTION:
00080          return "export_restriction";
00081       case PROTOCOL_VERSION:
00082          return "protocol_version";
00083       case INSUFFICIENT_SECURITY:
00084          return "insufficient_security";
00085       case INTERNAL_ERROR:
00086          return "internal_error";
00087       case INAPPROPRIATE_FALLBACK:
00088          return "inappropriate_fallback";
00089       case USER_CANCELED:
00090          return "user_canceled";
00091       case NO_RENEGOTIATION:
00092          return "no_renegotiation";
00093 
00094       case UNSUPPORTED_EXTENSION:
00095          return "unsupported_extension";
00096       case CERTIFICATE_UNOBTAINABLE:
00097          return "certificate_unobtainable";
00098       case UNRECOGNIZED_NAME:
00099          return "unrecognized_name";
00100       case BAD_CERTIFICATE_STATUS_RESPONSE:
00101          return "bad_certificate_status_response";
00102       case BAD_CERTIFICATE_HASH_VALUE:
00103          return "bad_certificate_hash_value";
00104       case UNKNOWN_PSK_IDENTITY:
00105          return "unknown_psk_identity";
00106 
00107       case NULL_ALERT:
00108          return "none";
00109 
00110       case HEARTBEAT_PAYLOAD:
00111          return "heartbeat_payload";
00112       }
00113 
00114    /*
00115    * This is effectively the default case for the switch above, but we
00116    * leave it out so that when an alert type is added to the enum the
00117    * compiler can warn us that it is not included in the switch
00118    * statement.
00119    */
00120    return "unrecognized_alert_" + std::to_string(type());
00121    }
00122 
00123 }
00124 
00125 }