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