Botan 3.6.1
Crypto and TLS for C&
tls_version.cpp
Go to the documentation of this file.
1/*
2* TLS Protocol Version Management
3* (C) 2012 Jack Lloyd
4* 2021 Elektrobit Automotive GmbH
5* 2022 René Meusel, Hannes Rantzsch - neXenio GmbH
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#include <botan/tls_version.h>
11
12#include <botan/tls_exceptn.h>
13
14namespace Botan::TLS {
15
16std::string Protocol_Version::to_string() const {
17 const uint8_t maj = major_version();
18 const uint8_t min = minor_version();
19
20 if(maj == 3 && min == 0) {
21 return "SSL v3";
22 }
23
24 if(maj == 3 && min >= 1) { // TLS v1.x
25 return "TLS v1." + std::to_string(min - 1);
26 }
27
28 if(maj == 254) { // DTLS 1.x
29 return "DTLS v1." + std::to_string(255 - min);
30 }
31
32 // Some very new or very old protocol (or bogus data)
33 return "Unknown " + std::to_string(maj) + "." + std::to_string(min);
34}
35
37 return major_version() > 250;
38}
39
41 return (!is_datagram_protocol() && *this <= Protocol_Version::TLS_V12) ||
42 (is_datagram_protocol() && *this <= Protocol_Version::DTLS_V12);
43}
44
46 return (!is_datagram_protocol() && *this >= Protocol_Version::TLS_V13) ||
47 (is_datagram_protocol() && *this >= Protocol_Version::DTLS_V13);
48}
49
51 if(this->is_datagram_protocol() != other.is_datagram_protocol()) {
52 throw TLS_Exception(Alert::ProtocolVersion, "Version comparing " + to_string() + " with " + other.to_string());
53 }
54
55 if(this->is_datagram_protocol()) {
56 return m_version < other.m_version; // goes backwards
57 }
58
59 return m_version > other.m_version;
60}
61
63 const uint8_t maj = major_version();
64 const uint8_t min = minor_version();
65
66 if(maj == 3 && min <= 4) {
67 // 3.0: SSLv3
68 // 3.1: TLS 1.0
69 // 3.2: TLS 1.1
70 // 3.3: TLS 1.2
71 // 3.4: TLS 1.3
72 return true;
73 }
74
75 if(maj == 254 && (min == 253 || min == 255)) {
76 // 254.253: DTLS 1.2
77 // 254.255: DTLS 1.0
78 return true;
79 }
80
81 return false;
82}
83
85 return (m_version == static_cast<uint16_t>(Protocol_Version::TLS_V12) ||
86#if defined(BOTAN_HAS_TLS_13)
87 m_version == static_cast<uint16_t>(Protocol_Version::TLS_V13) ||
88#endif
89 m_version == static_cast<uint16_t>(Protocol_Version::DTLS_V12));
90}
91
92} // namespace Botan::TLS
bool operator>(const Protocol_Version &other) const
std::string to_string() const
uint8_t major_version() const
Definition tls_version.h:89
uint8_t minor_version() const
Definition tls_version.h:94
#define BOTAN_HAS_TLS_13
Definition build.h:402