Botan 3.6.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.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
40 * (currently TLS v1.3)
41 *
42 * @return latest known TLS version
43 */
45#if defined(BOTAN_HAS_TLS_13)
47#else
49#endif
50 }
51
52 /**
53 * Returns the latest version of the DTLS protocol known to the library
54 * (currently DTLS v1.2)
55 *
56 * @return latest known DTLS version
57 */
59
60 Protocol_Version() : m_version(0) {}
61
62 explicit Protocol_Version(uint16_t code) : m_version(code) {}
63
64 /**
65 * @param named_version a specific named version of the protocol
66 */
67 Protocol_Version(Version_Code named_version) : Protocol_Version(static_cast<uint16_t>(named_version)) {}
68
69 /**
70 * @param major the major version
71 * @param minor the minor version
72 */
73 Protocol_Version(uint8_t major, uint8_t minor) :
74 Protocol_Version(static_cast<uint16_t>((static_cast<uint16_t>(major) << 8) | minor)) {}
75
76 /**
77 * @return true if this is a valid protocol version
78 */
79 bool valid() const;
80
81 /**
82 * @return true if this is a protocol version we know about
83 */
84 bool known_version() const;
85
86 /**
87 * @return major version of the protocol version
88 */
89 uint8_t major_version() const { return static_cast<uint8_t>(m_version >> 8); }
90
91 /**
92 * @return minor version of the protocol version
93 */
94 uint8_t minor_version() const { return static_cast<uint8_t>(m_version & 0xFF); }
95
96 /**
97 * @return the version code
98 */
99 uint16_t version_code() const { return m_version; }
100
101 /**
102 * Generate a human readable version string.
103 *
104 * for instance "TLS v1.1" or "DTLS v1.0".
105 *
106 * @return human-readable description of this version
107 */
108 std::string to_string() const;
109
110 /**
111 * @return true iff this is a DTLS version
112 */
113 bool is_datagram_protocol() const;
114
115 /**
116 * @return true if this version indicates (D)TLS 1.2 or older
117 */
118 bool is_pre_tls_13() const;
119
120 /**
121 * @return true if this version indicates a (D)TLS newer than 1.3
122 */
123 bool is_tls_13_or_later() const;
124
125 /**
126 * @return if this version is 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 not equal to other
132 */
133 bool operator!=(const Protocol_Version& other) const { return (m_version != other.m_version); }
134
135 /**
136 * @return if this version is later than other
137 */
138 bool operator>(const Protocol_Version& other) const;
139
140 /**
141 * @return if this version is later than or equal to other
142 */
143 bool operator>=(const Protocol_Version& other) const { return (*this == other || *this > other); }
144
145 /**
146 * @return if this version is earlier to other
147 */
148 bool operator<(const Protocol_Version& other) const { return !(*this >= other); }
149
150 /**
151 * @return if this version is earlier than or equal to other
152 */
153 bool operator<=(const Protocol_Version& other) const { return (*this == other || *this < other); }
154
155 private:
156 uint16_t m_version;
157};
158
159} // namespace Botan::TLS
160
161#endif
Protocol_Version(uint8_t major, uint8_t minor)
Definition tls_version.h:73
static Protocol_Version latest_dtls_version()
Definition tls_version.h:58
bool operator==(const Protocol_Version &other) const
uint8_t major_version() const
Definition tls_version.h:89
bool operator!=(const Protocol_Version &other) const
static Protocol_Version latest_tls_version()
Definition tls_version.h:44
bool operator>=(const Protocol_Version &other) const
uint16_t version_code() const
Definition tls_version.h:99
bool operator<=(const Protocol_Version &other) const
Protocol_Version(uint16_t code)
Definition tls_version.h:62
bool operator<(const Protocol_Version &other) const
uint8_t minor_version() const
Definition tls_version.h:94
Protocol_Version(Version_Code named_version)
Definition tls_version.h:67
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
@ TLS_V11
TLSv1.1 (no longer supported)
@ DTLS_V13
DTLSv1.3 (not supported yet)
bool operator>(const ASN1_Time &, const ASN1_Time &)