Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

tls_record.h
Go to the documentation of this file.
1 /*
2 * TLS Record Handling
3 * (C) 2004-2012 Jack Lloyd
4 * 2016 Matthias Gierlings
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9 #ifndef BOTAN_TLS_RECORDS_H_
10 #define BOTAN_TLS_RECORDS_H_
11
12 #include <botan/tls_algos.h>
13 #include <botan/tls_magic.h>
14 #include <botan/tls_version.h>
15 #include <botan/aead.h>
16 #include <vector>
17 #include <chrono>
18 #include <functional>
19
20 namespace Botan {
21
22 namespace TLS {
23
24 class Ciphersuite;
25 class Session_Keys;
26
27 class Connection_Sequence_Numbers;
28
29 /**
30 * TLS Cipher State
31 */
32 class Connection_Cipher_State final
33 {
34 public:
35 /**
36 * Initialize a new cipher state
37 */
38 Connection_Cipher_State(Protocol_Version version,
39 Connection_Side which_side,
40 bool is_our_side,
41 const Ciphersuite& suite,
42 const Session_Keys& keys,
43 bool uses_encrypt_then_mac);
44
45 AEAD_Mode& aead()
46 {
47 BOTAN_ASSERT_NONNULL(m_aead.get());
48 return *m_aead.get();
49 }
50
51 std::vector<uint8_t> aead_nonce(uint64_t seq, RandomNumberGenerator& rng);
52
53 std::vector<uint8_t> aead_nonce(const uint8_t record[], size_t record_len, uint64_t seq);
54
55 std::vector<uint8_t> format_ad(uint64_t seq, uint8_t type,
56 Protocol_Version version,
57 uint16_t ptext_length);
58
59 size_t nonce_bytes_from_handshake() const { return m_nonce_bytes_from_handshake; }
60 size_t nonce_bytes_from_record() const { return m_nonce_bytes_from_record; }
61
62 Nonce_Format nonce_format() const { return m_nonce_format; }
63
64 std::chrono::seconds age() const
65 {
66 return std::chrono::duration_cast<std::chrono::seconds>(
67 std::chrono::system_clock::now() - m_start_time);
68 }
69
70 private:
71 std::chrono::system_clock::time_point m_start_time;
72 std::unique_ptr<AEAD_Mode> m_aead;
73
74 std::vector<uint8_t> m_nonce;
75 Nonce_Format m_nonce_format;
76 size_t m_nonce_bytes_from_handshake;
77 size_t m_nonce_bytes_from_record;
78 };
79
80 class Record_Header final
81 {
82 public:
83 Record_Header(uint64_t sequence,
84 Protocol_Version version,
85 Record_Type type) :
86 m_needed(0),
87 m_sequence(sequence),
88 m_version(version),
89 m_type(type)
90 {}
91
92 Record_Header(size_t needed) :
93 m_needed(needed),
94 m_sequence(0),
95 m_version(Protocol_Version()),
96 m_type(NO_RECORD)
97 {}
98
99 size_t needed() const { return m_needed; }
100
101 Protocol_Version version() const
102 {
103 BOTAN_ASSERT_NOMSG(m_needed == 0);
104 return m_version;
105 }
106
107 uint64_t sequence() const
108 {
109 BOTAN_ASSERT_NOMSG(m_needed == 0);
110 return m_sequence;
111 }
112
113 uint16_t epoch() const
114 {
115 return static_cast<uint16_t>(sequence() >> 48);
116 }
117
118 Record_Type type() const
119 {
120 BOTAN_ASSERT_NOMSG(m_needed == 0);
121 return m_type;
122 }
123
124 private:
125 size_t m_needed;
126 uint64_t m_sequence;
127 Protocol_Version m_version;
128 Record_Type m_type;
129 };
130
131 /**
132 * Create a TLS record
133 * @param write_buffer the output record is placed here
134 * @param record_type the record layer type
135 * @param record_version the record layer version
136 * @param record_sequence the record layer sequence number
137 * @param message the record contents
138 * @param message_len is size of message
139 * @param cipherstate is the writing cipher state
140 * @param rng is a random number generator
141 */
142 void write_record(secure_vector<uint8_t>& write_buffer,
143 uint8_t record_type,
144 Protocol_Version record_version,
145 uint64_t record_sequence,
146 const uint8_t* message,
147 size_t message_len,
148 Connection_Cipher_State* cipherstate,
149 RandomNumberGenerator& rng);
150
151 // epoch -> cipher state
152 typedef std::function<std::shared_ptr<Connection_Cipher_State> (uint16_t)> get_cipherstate_fn;
153
154 /**
155 * Decode a TLS record
156 * @return zero if full message, else number of bytes still needed
157 */
158 Record_Header read_record(bool is_datagram,
159 secure_vector<uint8_t>& read_buffer,
160 const uint8_t input[],
161 size_t input_len,
162 size_t& consumed,
163 secure_vector<uint8_t>& record_buf,
164 Connection_Sequence_Numbers* sequence_numbers,
165 get_cipherstate_fn get_cipherstate,
166 bool allow_epoch0_restart);
167
168 }
169
170 }
171
172 #endif
Botan::TLS::read_record
Record_Header read_record(bool is_datagram, secure_vector< uint8_t > &readbuf, const uint8_t input[], size_t input_len, size_t &consumed, secure_vector< uint8_t > &recbuf, Connection_Sequence_Numbers *sequence_numbers, get_cipherstate_fn get_cipherstate, bool allow_epoch0_restart)
Definition: tls_record.cpp:497
Botan::TLS::Connection_Cipher_State
Definition: tls_record.h:32
Botan::TLS::Connection_Cipher_State::nonce_format
Nonce_Format nonce_format() const
Definition: tls_record.h:62
Botan::TLS::Connection_Cipher_State::aead_nonce
std::vector< uint8_t > aead_nonce(uint64_t seq, RandomNumberGenerator &rng)
Definition: tls_record.cpp:86
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan::TLS::Connection_Cipher_State::aead
AEAD_Mode & aead()
Definition: tls_record.h:45
Botan::TLS::Connection_Cipher_State::nonce_bytes_from_record
size_t nonce_bytes_from_record() const
Definition: tls_record.h:60
Botan::TLS::get_cipherstate_fn
std::function< std::shared_ptr< Connection_Cipher_State >uint16_t)> get_cipherstate_fn
Definition: tls_record.h:152
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:28
Botan::TLS::Record_Header::version
Protocol_Version version() const
Definition: tls_record.h:101
Botan::TLS::write_record
void write_record(secure_vector< uint8_t > &output, uint8_t record_type, Protocol_Version version, uint64_t record_sequence, const uint8_t *message, size_t message_len, Connection_Cipher_State *cs, RandomNumberGenerator &rng)
Definition: tls_record.cpp:192
BOTAN_ASSERT_NOMSG
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:68
Botan::TLS::Connection_Cipher_State::Connection_Cipher_State
Connection_Cipher_State(Protocol_Version version, Connection_Side which_side, bool is_our_side, const Ciphersuite &suite, const Session_Keys &keys, bool uses_encrypt_then_mac)
Definition: tls_record.cpp:28
Botan::TLS::Protocol_Version
Definition: tls_version.h:21
type
MechanismType type
Definition: p11_mechanism.cpp:44
Botan::TLS::Session_Keys
Definition: tls_session_key.h:23
Botan::TLS::Ciphersuite
Definition: tls_ciphersuite.h:24
Botan::TLS::Connection_Side
Connection_Side
Definition: tls_magic.h:32
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:65
Botan::TLS::Record_Header
Definition: tls_record.h:80
Botan::TLS::Connection_Cipher_State::format_ad
std::vector< uint8_t > format_ad(uint64_t seq, uint8_t type, Protocol_Version version, uint16_t ptext_length)
Definition: tls_record.cpp:163
BOTAN_ASSERT_NONNULL
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:107
Botan::TLS::Record_Header::Record_Header
Record_Header(size_t needed)
Definition: tls_record.h:92
Botan::TLS::Record_Header::needed
size_t needed() const
Definition: tls_record.h:99
Botan
Definition: alg_id.cpp:13
Botan::TLS::Connection_Sequence_Numbers
Definition: tls_seq_numbers.h:18
Botan::TLS::Record_Type
Record_Type
Definition: tls_magic.h:35
Botan::TLS::Record_Header::type
Record_Type type() const
Definition: tls_record.h:118
Botan::AEAD_Mode
Definition: aead.h:22
Botan::TLS::Record_Header::sequence
uint64_t sequence() const
Definition: tls_record.h:107
Botan::TLS::Connection_Cipher_State::nonce_bytes_from_handshake
size_t nonce_bytes_from_handshake() const
Definition: tls_record.h:59
Botan::TLS::NO_RECORD
Definition: tls_magic.h:41
Botan::TLS::Nonce_Format
Nonce_Format
Definition: tls_algos.h:62
Botan::TLS::Record_Header::epoch
uint16_t epoch() const
Definition: tls_record.h:113
Botan::TLS::Record_Header::Record_Header
Record_Header(uint64_t sequence, Protocol_Version version, Record_Type type)
Definition: tls_record.h:83
Botan::TLS::Connection_Cipher_State::age
std::chrono::seconds age() const
Definition: tls_record.h:64