Botan 3.0.0-alpha0
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 {
17
18namespace TLS {
19
20/**
21* TLS Protocol Version
22*/
24 {
25 public:
27 TLS_V11 = 0x0302, // not supported by Botan
28 TLS_V12 = 0x0303,
29 TLS_V13 = 0x0304,
30 DTLS_V12 = 0xFEFD
31 };
32
33 /**
34 * @return latest known TLS version
35 */
37 {
38#if defined(BOTAN_HAS_TLS_13)
39 return Protocol_Version(TLS_V13);
40#else
41 return Protocol_Version(TLS_V12);
42#endif
43 }
44
45 /**
46 * @return latest known DTLS version
47 */
49 {
50 return Protocol_Version(DTLS_V12);
51 }
52
53 Protocol_Version() : m_version(0) {}
54
55 explicit Protocol_Version(uint16_t code) : m_version(code) {}
56
57 /**
58 * @param named_version a specific named version of the protocol
59 */
61 Protocol_Version(static_cast<uint16_t>(named_version)) {}
62
63 /**
64 * @param major the major version
65 * @param minor the minor version
66 */
67 Protocol_Version(uint8_t major, uint8_t minor) :
68 Protocol_Version(static_cast<uint16_t>((static_cast<uint16_t>(major) << 8) | minor)) {}
69
70 /**
71 * @return true if this is a valid protocol version
72 */
73 bool valid() const;
74
75 /**
76 * @return true if this is a protocol version we know about
77 */
78 bool known_version() const;
79
80 /**
81 * @return major version of the protocol version
82 */
83 uint8_t major_version() const { return static_cast<uint8_t>(m_version >> 8); }
84
85 /**
86 * @return minor version of the protocol version
87 */
88 uint8_t minor_version() const { return static_cast<uint8_t>(m_version & 0xFF); }
89
90 /**
91 * @return the version code
92 */
93 uint16_t version_code() const { return m_version; }
94
95 /**
96 * @return human-readable description of this version
97 */
98 std::string to_string() const;
99
100 /**
101 * @return true iff this is a DTLS version
102 */
103 bool is_datagram_protocol() const;
104
105 /**
106 * @return true if this version indicates (D)TLS 1.2 or older
107 */
108 bool is_pre_tls_13() const;
109
110 /**
111 * @return if this version is equal to other
112 */
113 bool operator==(const Protocol_Version& other) const
114 {
115 return (m_version == other.m_version);
116 }
117
118 /**
119 * @return if this version is not equal to other
120 */
121 bool operator!=(const Protocol_Version& other) const
122 {
123 return (m_version != other.m_version);
124 }
125
126 /**
127 * @return if this version is later than other
128 */
129 bool operator>(const Protocol_Version& other) const;
130
131 /**
132 * @return if this version is later than or equal to other
133 */
134 bool operator>=(const Protocol_Version& other) const
135 {
136 return (*this == other || *this > other);
137 }
138
139 /**
140 * @return if this version is earlier to other
141 */
142 bool operator<(const Protocol_Version& other) const
143 {
144 return !(*this >= other);
145 }
146
147 /**
148 * @return if this version is earlier than or equal to other
149 */
150 bool operator<=(const Protocol_Version& other) const
151 {
152 return (*this == other || *this < other);
153 }
154
155 private:
156 uint16_t m_version;
157 };
158
159}
160
161}
162
163#endif
164
Protocol_Version(uint8_t major, uint8_t minor)
Definition: tls_version.h:67
static Protocol_Version latest_dtls_version()
Definition: tls_version.h:48
bool operator==(const Protocol_Version &other) const
Definition: tls_version.h:113
uint8_t major_version() const
Definition: tls_version.h:83
bool operator!=(const Protocol_Version &other) const
Definition: tls_version.h:121
static Protocol_Version latest_tls_version()
Definition: tls_version.h:36
bool operator>=(const Protocol_Version &other) const
Definition: tls_version.h:134
uint16_t version_code() const
Definition: tls_version.h:93
bool operator<=(const Protocol_Version &other) const
Definition: tls_version.h:150
Protocol_Version(uint16_t code)
Definition: tls_version.h:55
bool operator<(const Protocol_Version &other) const
Definition: tls_version.h:142
uint8_t minor_version() const
Definition: tls_version.h:88
Protocol_Version(Version_Code named_version)
Definition: tls_version.h:60
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: alg_id.cpp:13
bool operator>(const ASN1_Time &, const ASN1_Time &)
Definition: asn1_time.cpp:276
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11