Coverage Report

Created: 2022-06-23 06:44

/src/botan/build/include/botan/tls_version.h
Line
Count
Source (jump to first uncovered line)
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
16
namespace Botan {
17
18
namespace TLS {
19
20
/**
21
* TLS Protocol Version
22
*/
23
class BOTAN_PUBLIC_API(2,0) Protocol_Version final
24
   {
25
   public:
26
      enum Version_Code {
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
      */
36
      static Protocol_Version latest_tls_version()
37
0
         {
38
0
         return Protocol_Version(TLS_V12);
39
0
         }
40
41
      /**
42
      * @return latest known DTLS version
43
      */
44
      static Protocol_Version latest_dtls_version()
45
0
         {
46
0
         return Protocol_Version(DTLS_V12);
47
0
         }
48
49
68.3k
      Protocol_Version() : m_version(0) {}
50
51
169k
      explicit Protocol_Version(uint16_t code) : m_version(code) {}
52
53
      /**
54
      * @param named_version a specific named version of the protocol
55
      */
56
      Protocol_Version(Version_Code named_version) :
57
75.8k
         Protocol_Version(static_cast<uint16_t>(named_version)) {}
58
59
      /**
60
      * @param major the major version
61
      * @param minor the minor version
62
      */
63
      Protocol_Version(uint8_t major, uint8_t minor) :
64
92.6k
         Protocol_Version(static_cast<uint16_t>((static_cast<uint16_t>(major) << 8) | minor)) {}
65
66
      /**
67
      * @return true if this is a valid protocol version
68
      */
69
      bool valid() const;
70
71
      /**
72
      * @return true if this is a protocol version we know about
73
      */
74
      bool known_version() const;
75
76
      /**
77
      * @return major version of the protocol version
78
      */
79
521k
      uint8_t major_version() const { return static_cast<uint8_t>(m_version >> 8); }
80
81
      /**
82
      * @return minor version of the protocol version
83
      */
84
112k
      uint8_t minor_version() const { return static_cast<uint8_t>(m_version & 0xFF); }
85
86
      /**
87
      * @return the version code
88
      */
89
0
      uint16_t version_code() const { return m_version; }
90
91
      /**
92
      * @return human-readable description of this version
93
      */
94
      std::string to_string() const;
95
96
      /**
97
      * @return true iff this is a DTLS version
98
      */
99
      bool is_datagram_protocol() const;
100
101
      /**
102
       * @return true if this version indicates (D)TLS 1.2 or older
103
       */
104
      bool is_pre_tls_13() const;
105
106
      /**
107
      * @return if this version is equal to other
108
      */
109
      bool operator==(const Protocol_Version& other) const
110
24.2k
         {
111
24.2k
         return (m_version == other.m_version);
112
24.2k
         }
113
114
      /**
115
      * @return if this version is not equal to other
116
      */
117
      bool operator!=(const Protocol_Version& other) const
118
14.2k
         {
119
14.2k
         return (m_version != other.m_version);
120
14.2k
         }
121
122
      /**
123
      * @return if this version is later than other
124
      */
125
      bool operator>(const Protocol_Version& other) const;
126
127
      /**
128
      * @return if this version is later than or equal to other
129
      */
130
      bool operator>=(const Protocol_Version& other) const
131
22.5k
         {
132
22.5k
         return (*this == other || *this > other);
133
22.5k
         }
134
135
      /**
136
      * @return if this version is earlier to other
137
      */
138
      bool operator<(const Protocol_Version& other) const
139
0
         {
140
0
         return !(*this >= other);
141
0
         }
142
143
      /**
144
      * @return if this version is earlier than or equal to other
145
      */
146
      bool operator<=(const Protocol_Version& other) const
147
0
         {
148
0
         return (*this == other || *this < other);
149
0
         }
150
151
   private:
152
      uint16_t m_version;
153
   };
154
155
}
156
157
}
158
159
#endif
160