Botan 3.11.0
Crypto and TLS for C&
tls_version.h
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#ifndef BOTAN_TLS_PROTOCOL_VERSION_H_
11#define BOTAN_TLS_PROTOCOL_VERSION_H_
12
13#include <botan/types.h>
14#include <string>
15
16namespace Botan::TLS {
17
18enum class Version_Code : uint16_t {
19 /// TLSv1.1 (no longer supported)
20 TLS_V11 = 0x0302,
21 /// TLSv1.2
22 TLS_V12 = 0x0303,
23 /// TLSv1.3
24 TLS_V13 = 0x0304,
25 /// DTLSv1.2
26 DTLS_V12 = 0xFEFD,
27 /// DTLSv1.3 (not supported yet)
28 DTLS_V13 = 0xFEFC,
29};
30
31/**
32* TLS Protocol Version
33*/
35 public:
36 using enum Version_Code;
37
38 /**
39 * Returns the latest version of the TLS protocol known to the library and
40 * available in the current build.
41 *
42 * @return latest known TLS version
43 */
45
46 /**
47 * Returns the latest version of the DTLS protocol known to the library
48 * and available in the current build.
49 *
50 * @return latest known DTLS version
51 */
53
54 Protocol_Version() : m_version(0) {}
55
56 explicit Protocol_Version(uint16_t code) : m_version(code) {}
57
58 /**
59 * @param named_version a specific named version of the protocol
60 */
61 Protocol_Version(Version_Code named_version) : // NOLINT(*-explicit-conversions)
62 Protocol_Version(static_cast<uint16_t>(named_version)) {}
63
64 /**
65 * @param major the major version
66 * @param minor the minor version
67 */
68 Protocol_Version(uint8_t major, uint8_t minor) :
69 Protocol_Version(static_cast<uint16_t>((static_cast<uint16_t>(major) << 8) | minor)) {}
70
71 /**
72 * @return true if this is a valid protocol version
73 */
74 bool valid() const;
75
76 /**
77 * @return true if this is a protocol version we know about
78 */
79 bool known_version() const;
80
81 /**
82 * @return major version of the protocol version
83 */
84 uint8_t major_version() const { return static_cast<uint8_t>(m_version >> 8); }
85
86 /**
87 * @return minor version of the protocol version
88 */
89 uint8_t minor_version() const { return static_cast<uint8_t>(m_version & 0xFF); }
90
91 /**
92 * @return the version code
93 */
94 uint16_t version_code() const { return m_version; }
95
96 /**
97 * Generate a human readable version string.
98 *
99 * for instance "TLS v1.1" or "DTLS v1.0".
100 *
101 * @return human-readable description of this version
102 */
103 std::string to_string() const;
104
105 /**
106 * @return true iff this is a DTLS version
107 */
108 bool is_datagram_protocol() const;
109
110 /**
111 * @return true if this version indicates (D)TLS 1.2 or older
112 */
113 bool is_pre_tls_13() const;
114
115 /**
116 * @return true if this version indicates a (D)TLS newer than 1.3
117 */
118 bool is_tls_13_or_later() const;
119
120 /**
121 * @return if this version is equal to other
122 */
123 bool operator==(const Protocol_Version& other) const { return (m_version == other.m_version); }
124
125 /**
126 * @return if this version is not equal to other
127 */
128 bool operator!=(const Protocol_Version& other) const { return (m_version != other.m_version); }
129
130 /**
131 * @return if this version is later than other
132 */
133 bool operator>(const Protocol_Version& other) const;
134
135 /**
136 * @return if this version is later than or equal to other
137 */
138 bool operator>=(const Protocol_Version& other) const { return (*this == other || *this > other); }
139
140 /**
141 * @return if this version is earlier to other
142 */
143 bool operator<(const Protocol_Version& other) const { return !(*this >= other); }
144
145 /**
146 * @return if this version is earlier than or equal to other
147 */
148 bool operator<=(const Protocol_Version& other) const { return (*this == other || *this < other); }
149
150 private:
151 uint16_t m_version;
152};
153
154} // namespace Botan::TLS
155
156#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
static Protocol_Version latest_tls_version()
Protocol_Version(uint8_t major, uint8_t minor)
Definition tls_version.h:68
bool operator==(const Protocol_Version &other) const
uint8_t major_version() const
Definition tls_version.h:84
bool operator!=(const Protocol_Version &other) const
bool operator>=(const Protocol_Version &other) const
uint16_t version_code() const
Definition tls_version.h:94
bool operator<=(const Protocol_Version &other) const
Protocol_Version(uint16_t code)
Definition tls_version.h:56
bool operator<(const Protocol_Version &other) const
static Protocol_Version latest_dtls_version()
uint8_t minor_version() const
Definition tls_version.h:89
Protocol_Version(Version_Code named_version)
Definition tls_version.h:61
@ TLS_V11
TLSv1.1 (no longer supported).
Definition tls_version.h:20
@ DTLS_V13
DTLSv1.3 (not supported yet).
Definition tls_version.h:28
bool operator>(const ASN1_Time &t1, const ASN1_Time &t2)
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition exceptn.cpp:13