aboutsummaryrefslogtreecommitdiffstats
path: root/src/tls/tls_record.h
blob: 8e89b9f8a18f1443598f561fd52fb56c2f4c2654 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* TLS Record Handling
* (C) 2004-2012 Jack Lloyd
*
* Released under the terms of the Botan license
*/

#ifndef BOTAN_TLS_RECORDS_H__
#define BOTAN_TLS_RECORDS_H__

#include <botan/tls_suites.h>
#include <botan/pipe.h>
#include <botan/mac.h>
#include <botan/secqueue.h>
#include <vector>

#if defined(BOTAN_USE_STD_TR1)

#if defined(BOTAN_BUILD_COMPILER_IS_MSVC)
    #include <functional>
#else
    #include <tr1/functional>
#endif

#elif defined(BOTAN_USE_BOOST_TR1)
  #include <boost/tr1/functional.hpp>
#else
  #error "No TR1 library defined for use"
#endif

namespace Botan {

class SessionKeys;

/**
* TLS Record Writer
*/
class BOTAN_DLL Record_Writer
   {
   public:
      void send(byte type, const byte input[], size_t length);
      void send(byte type, byte val) { send(type, &val, 1); }

      void alert(Alert_Level level, Alert_Type type);

      void activate(const TLS_Cipher_Suite& suite,
                    const SessionKeys& keys,
                    Connection_Side side);

      void set_version(Version_Code version);

      Version_Code get_version() const;

      void reset();

      void set_maximum_fragment_size(size_t max_fragment);

      Record_Writer(std::tr1::function<void (const byte[], size_t)> output_fn);

      ~Record_Writer() { delete m_mac; }
   private:
      void send_record(byte type, const byte input[], size_t length);

      std::tr1::function<void (const byte[], size_t)> m_output_fn;

      MemoryVector<byte> m_writebuf;

      Pipe m_cipher;
      MessageAuthenticationCode* m_mac;

      size_t m_block_size, m_mac_size, m_iv_size, m_max_fragment;

      u64bit m_seq_no;
      byte m_major, m_minor;
   };

/**
* TLS Record Reader
*/
class BOTAN_DLL Record_Reader
   {
   public:
      void add_input(const byte input[], size_t input_size);

      /**
      * @param msg_type (output variable)
      * @param buffer (output variable)
      * @return Number of bytes still needed (minimum), or 0 if success
      */
      size_t get_record(byte& msg_type,
                        MemoryVector<byte>& buffer);

      SecureVector<byte> get_record(byte& msg_type);

      void activate(const TLS_Cipher_Suite& suite,
                    const SessionKeys& keys,
                    Connection_Side side);

      void set_version(Version_Code version);

      Version_Code get_version() const;

      void reset();

      bool currently_empty() const { return m_input_queue.size() == 0; }

      void set_maximum_fragment_size(size_t max_fragment);

      Record_Reader();

      ~Record_Reader() { delete m_mac; }
   private:
      MemoryVector<byte> m_readbuf;
      SecureQueue m_input_queue;

      Pipe m_cipher;
      MessageAuthenticationCode* m_mac;
      size_t m_block_size, m_mac_size, m_iv_size, m_max_fragment;
      u64bit m_seq_no;
      byte m_major, m_minor;
   };

}

#endif