Botan 3.0.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#include <botan/tls_exceptn.h>
12
13namespace Botan::TLS {
14
15std::string Protocol_Version::to_string() const
16 {
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 if(maj == 3 && min >= 1) // TLS v1.x
24 return "TLS v1." + std::to_string(min-1);
25
26 if(maj == 254) // DTLS 1.x
27 return "DTLS v1." + std::to_string(255 - min);
28
29 // Some very new or very old protocol (or bogus data)
30 return "Unknown " + std::to_string(maj) + "." + std::to_string(min);
31 }
32
34 {
35 return major_version() > 250;
36 }
37
39 {
40 return (!is_datagram_protocol() && *this <= Protocol_Version::TLS_V12) ||
41 ( is_datagram_protocol() && *this <= Protocol_Version::DTLS_V12);
42 }
43
45 {
46 return (!is_datagram_protocol() && *this >= Protocol_Version::TLS_V13) ||
47 ( is_datagram_protocol() && *this >= Protocol_Version::DTLS_V13);
48 }
49
50
52 {
53 if(this->is_datagram_protocol() != other.is_datagram_protocol())
54 throw TLS_Exception(Alert::ProtocolVersion,
55 "Version comparing " + to_string() +
56 " with " + other.to_string());
57
58 if(this->is_datagram_protocol())
59 return m_version < other.m_version; // goes backwards
60
61 return m_version > other.m_version;
62 }
63
65 {
66 const uint8_t maj = major_version();
67 const uint8_t min = minor_version();
68
69 if(maj == 3 && min <= 4)
70 // 3.0: SSLv3
71 // 3.1: TLS 1.0
72 // 3.2: TLS 1.1
73 // 3.3: TLS 1.2
74 // 3.4: TLS 1.3
75 return true;
76
77 if(maj == 254 && (min == 253 || min == 255))
78 // 254.253: DTLS 1.2
79 // 254.255: DTLS 1.0
80 return true;
81
82 return false;
83 }
84
86 {
87 return (m_version == static_cast<uint16_t>(Protocol_Version::TLS_V12) ||
88#if defined(BOTAN_HAS_TLS_13)
89 m_version == static_cast<uint16_t>(Protocol_Version::TLS_V13) ||
90#endif
91 m_version == static_cast<uint16_t>(Protocol_Version::DTLS_V12));
92 }
93
94}
bool operator>(const Protocol_Version &other) const
Definition: tls_version.cpp:51
std::string to_string() const
Definition: tls_version.cpp:15
uint8_t major_version() const
Definition: tls_version.h:86
uint8_t minor_version() const
Definition: tls_version.h:91
#define BOTAN_HAS_TLS_13
Definition: build.h:326