Botan  2.11.0
Crypto and TLS for C++11
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 */
33  {
34  public:
35  /**
36  * Initialize a new cipher state
37  */
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 
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 
81  {
82  public:
84  uint64_t* sequence,
85  Protocol_Version* protocol_version,
87  : m_data(data), m_sequence(sequence), m_protocol_version(protocol_version),
88  m_type(type), m_size(data.size()) {}
89 
90  secure_vector<uint8_t>& get_data() { return m_data; }
91 
92  Protocol_Version* get_protocol_version() { return m_protocol_version; }
93 
94  uint64_t* get_sequence() { return m_sequence; }
95 
96  Record_Type* get_type() { return m_type; }
97 
98  size_t& get_size() { return m_size; }
99 
100  private:
101  secure_vector<uint8_t>& m_data;
102  uint64_t* m_sequence;
103  Protocol_Version* m_protocol_version;
104  Record_Type* m_type;
105  size_t m_size;
106  };
107 
109  {
110  public:
111  Record_Message(const uint8_t* data, size_t size)
112  : m_type(0), m_sequence(0), m_data(data), m_size(size) {}
113  Record_Message(uint8_t type, uint64_t sequence, const uint8_t* data, size_t size)
114  : m_type(type), m_sequence(sequence), m_data(data),
115  m_size(size) {}
116 
117  uint8_t& get_type() { return m_type; }
118  uint64_t& get_sequence() { return m_sequence; }
119  const uint8_t* get_data() { return m_data; }
120  size_t& get_size() { return m_size; }
121 
122  private:
123  uint8_t m_type;
124  uint64_t m_sequence;
125  const uint8_t* m_data;
126  size_t m_size;
127 };
128 
130  {
131  public:
132  Record_Raw_Input(const uint8_t* data, size_t size, size_t& consumed,
133  bool is_datagram)
134  : m_data(data), m_size(size), m_consumed(consumed),
135  m_is_datagram(is_datagram) {}
136 
137  const uint8_t*& get_data() { return m_data; }
138 
139  size_t& get_size() { return m_size; }
140 
141  size_t& get_consumed() { return m_consumed; }
142  void set_consumed(size_t consumed) { m_consumed = consumed; }
143 
144  bool is_datagram() { return m_is_datagram; }
145 
146  private:
147  const uint8_t* m_data;
148  size_t m_size;
149  size_t& m_consumed;
150  bool m_is_datagram;
151  };
152 
153 
154 /**
155 * Create a TLS record
156 * @param write_buffer the output record is placed here
157 * @param rec_msg is the plaintext message
158 * @param version is the protocol version
159 * @param msg_sequence is the sequence number
160 * @param cipherstate is the writing cipher state
161 * @param rng is a random number generator
162 */
163 void write_record(secure_vector<uint8_t>& write_buffer,
164  Record_Message rec_msg,
165  Protocol_Version version,
166  uint64_t msg_sequence,
167  Connection_Cipher_State* cipherstate,
168  RandomNumberGenerator& rng);
169 
170 // epoch -> cipher state
171 typedef std::function<std::shared_ptr<Connection_Cipher_State> (uint16_t)> get_cipherstate_fn;
172 
173 /**
174 * Decode a TLS record
175 * @return zero if full message, else number of bytes still needed
176 */
177 size_t read_record(secure_vector<uint8_t>& read_buffer,
178  Record_Raw_Input& raw_input,
179  Record& rec,
180  Connection_Sequence_Numbers* sequence_numbers,
181  get_cipherstate_fn get_cipherstate);
182 
183 }
184 
185 }
186 
187 #endif
Record_Message(uint8_t type, uint64_t sequence, const uint8_t *data, size_t size)
Definition: tls_record.h:113
uint64_t * get_sequence()
Definition: tls_record.h:94
size_t nonce_bytes_from_handshake() const
Definition: tls_record.h:59
std::vector< uint8_t > aead_nonce(uint64_t seq, RandomNumberGenerator &rng)
Definition: tls_record.cpp:85
Nonce_Format nonce_format() const
Definition: tls_record.h:62
std::chrono::seconds age() const
Definition: tls_record.h:64
const uint8_t * get_data()
Definition: tls_record.h:119
std::function< std::shared_ptr< Connection_Cipher_State >uint16_t)> get_cipherstate_fn
Definition: tls_record.h:171
size_t read_record(secure_vector< uint8_t > &readbuf, Record_Raw_Input &raw_input, Record &rec, Connection_Sequence_Numbers *sequence_numbers, get_cipherstate_fn get_cipherstate)
Definition: tls_record.cpp:499
int(* final)(unsigned char *, CTX *)
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
MechanismType type
Record_Message(const uint8_t *data, size_t size)
Definition: tls_record.h:111
size_t nonce_bytes_from_record() const
Definition: tls_record.h:60
secure_vector< uint8_t > & get_data()
Definition: tls_record.h:90
std::vector< uint8_t > format_ad(uint64_t seq, uint8_t type, Protocol_Version version, uint16_t ptext_length)
Definition: tls_record.cpp:162
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:107
Record_Raw_Input(const uint8_t *data, size_t size, size_t &consumed, bool is_datagram)
Definition: tls_record.h:132
Definition: alg_id.cpp:13
void set_consumed(size_t consumed)
Definition: tls_record.h:142
Record(secure_vector< uint8_t > &data, uint64_t *sequence, Protocol_Version *protocol_version, Record_Type *type)
Definition: tls_record.h:83
Record_Type * get_type()
Definition: tls_record.h:96
void write_record(secure_vector< uint8_t > &output, Record_Message msg, Protocol_Version version, uint64_t seq, Connection_Cipher_State *cs, RandomNumberGenerator &rng)
Definition: tls_record.cpp:191
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
Protocol_Version * get_protocol_version()
Definition: tls_record.h:92
size_t & get_size()
Definition: tls_record.h:98
const uint8_t *& get_data()
Definition: tls_record.h:137