Botan 3.11.0
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
17#if defined(BOTAN_HAS_TLS_13)
18 return Protocol_Version::TLS_V13;
19#elif defined(BOTAN_HAS_TLS_12)
20 return Protocol_Version::TLS_V12;
21#else
22 throw Not_Implemented("This build contains no usable TLS version");
23#endif
24}
25
27#if defined(BOTAN_HAS_TLS_12)
28 return Protocol_Version::DTLS_V12;
29#else
30 throw Not_Implemented("This build contains no usable DTLS version");
31#endif
32}
33
34std::string Protocol_Version::to_string() const {
35 const uint8_t maj = major_version();
36 const uint8_t min = minor_version();
37
38 if(maj == 3 && min == 0) {
39 return "SSL v3";
40 }
41
42 if(maj == 3 && min >= 1) { // TLS v1.x
43 return "TLS v1." + std::to_string(min - 1);
44 }
45
46 if(maj == 254) { // DTLS 1.x
47 return "DTLS v1." + std::to_string(255 - min);
48 }
49
50 // Some very new or very old protocol (or bogus data)
51 return "Unknown " + std::to_string(maj) + "." + std::to_string(min);
52}
53
55 return major_version() > 250;
56}
57
59 return (!is_datagram_protocol() && *this <= Protocol_Version::TLS_V12) ||
60 (is_datagram_protocol() && *this <= Protocol_Version::DTLS_V12);
61}
62
64 return (!is_datagram_protocol() && *this >= Protocol_Version::TLS_V13) ||
65 (is_datagram_protocol() && *this >= Protocol_Version::DTLS_V13);
66}
67
69 if(this->is_datagram_protocol() != other.is_datagram_protocol()) {
70 throw TLS_Exception(Alert::ProtocolVersion, "Version comparing " + to_string() + " with " + other.to_string());
71 }
72
73 if(this->is_datagram_protocol()) {
74 return m_version < other.m_version; // goes backwards
75 }
76
77 return m_version > other.m_version;
78}
79
81 const uint8_t maj = major_version();
82 const uint8_t min = minor_version();
83
84 if(maj == 3 && min <= 4) {
85 // 3.0: SSLv3
86 // 3.1: TLS 1.0
87 // 3.2: TLS 1.1
88 // 3.3: TLS 1.2
89 // 3.4: TLS 1.3
90 return true;
91 }
92
93 if(maj == 254 && (min == 253 || min == 255)) {
94 // 254.253: DTLS 1.2
95 // 254.255: DTLS 1.0
96 return true;
97 }
98
99 return false;
100}
101
103#if defined(BOTAN_HAS_TLS_13)
104 if(m_version == static_cast<uint16_t>(Protocol_Version::TLS_V13)) {
105 return true;
106 }
107#endif
108
109#if defined(BOTAN_HAS_TLS_12)
110 if(m_version == static_cast<uint16_t>(Protocol_Version::TLS_V12)) {
111 return true;
112 }
113 if(m_version == static_cast<uint16_t>(Protocol_Version::DTLS_V12)) {
114 return true;
115 }
116#endif
117
118 return false;
119}
120
121} // namespace Botan::TLS
bool operator>(const Protocol_Version &other) const
static Protocol_Version latest_tls_version()
std::string to_string() const
uint8_t major_version() const
Definition tls_version.h:84
static Protocol_Version latest_dtls_version()
uint8_t minor_version() const
Definition tls_version.h:89