aboutsummaryrefslogtreecommitdiffstats
path: root/src/tls/tls_session.cpp
blob: c40e9d79e89b32da0ecef3eae8eea86393569e4f (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* TLS Session State
* (C) 2011-2012 Jack Lloyd
*
* Released under the terms of the Botan license
*/

#include <botan/tls_session.h>
#include <botan/der_enc.h>
#include <botan/ber_dec.h>
#include <botan/asn1_str.h>
#include <botan/time.h>
#include <botan/lookup.h>
#include <memory>

namespace Botan {

TLS_Session::TLS_Session(const MemoryRegion<byte>& session_identifier,
                         const MemoryRegion<byte>& master_secret,
                         Version_Code version,
                         u16bit ciphersuite,
                         byte compression_method,
                         Connection_Side side,
                         bool secure_renegotiation_supported,
                         size_t fragment_size,
                         const std::vector<X509_Certificate>& certs,
                         const std::string& sni_hostname,
                         const std::string& srp_identifier) :
   m_start_time(system_time()),
   m_identifier(session_identifier),
   m_master_secret(master_secret),
   m_version(version),
   m_ciphersuite(ciphersuite),
   m_compression_method(compression_method),
   m_connection_side(side),
   m_secure_renegotiation_supported(secure_renegotiation_supported),
   m_fragment_size(fragment_size),
   m_sni_hostname(sni_hostname),
   m_srp_identifier(srp_identifier)
   {
   // FIXME: encode all of them?
   if(certs.size())
      m_peer_certificate = certs[0].BER_encode();
   }

TLS_Session::TLS_Session(const byte ber[], size_t ber_len)
   {
   BER_Decoder decoder(ber, ber_len);

   byte side_code = 0;
   ASN1_String sni_hostname_str;
   ASN1_String srp_identifier_str;

   BER_Decoder(ber, ber_len)
      .decode_and_check(static_cast<size_t>(TLS_SESSION_PARAM_STRUCT_VERSION),
                        "Unknown version in session structure")
      .decode(m_identifier, OCTET_STRING)
      .decode_integer_type(m_start_time)
      .decode_integer_type(m_version)
      .decode_integer_type(m_ciphersuite)
      .decode_integer_type(m_compression_method)
      .decode_integer_type(side_code)
      .decode_integer_type(m_fragment_size)
      .decode(m_secure_renegotiation_supported)
      .decode(m_master_secret, OCTET_STRING)
      .decode(m_peer_certificate, OCTET_STRING)
      .decode(sni_hostname_str)
      .decode(srp_identifier_str);

   m_sni_hostname = sni_hostname_str.value();
   m_srp_identifier = srp_identifier_str.value();
   m_connection_side = static_cast<Connection_Side>(side_code);
   }

SecureVector<byte> TLS_Session::BER_encode() const
   {
   return DER_Encoder()
      .start_cons(SEQUENCE)
         .encode(static_cast<size_t>(TLS_SESSION_PARAM_STRUCT_VERSION))
         .encode(m_identifier, OCTET_STRING)
         .encode(static_cast<size_t>(m_start_time))
         .encode(static_cast<size_t>(m_version))
         .encode(static_cast<size_t>(m_ciphersuite))
         .encode(static_cast<size_t>(m_compression_method))
         .encode(static_cast<size_t>(m_connection_side))
         .encode(static_cast<size_t>(m_fragment_size))
         .encode(m_secure_renegotiation_supported)
         .encode(m_master_secret, OCTET_STRING)
         .encode(m_peer_certificate, OCTET_STRING)
         .encode(ASN1_String(m_sni_hostname, UTF8_STRING))
         .encode(ASN1_String(m_srp_identifier, UTF8_STRING))
      .end_cons()
   .get_contents();
   }

MemoryVector<byte>
TLS_Session::encrypt(const SymmetricKey& master_key,
                     const MemoryRegion<byte>& key_name,
                     RandomNumberGenerator& rng)
   {
   if(key_name.size() != 16)
      throw Encoding_Error("Bad length " + to_string(key_name.size()) +
                           " for key_name in TLS_Session::encrypt");

   if(master_key.length() == 0)
      throw Decoding_Error("TLS_Session master_key not set");

   std::auto_ptr<KDF> kdf(get_kdf("KDF2(SHA-256)"));

   SymmetricKey aes_key = kdf->derive_key(16, master_key.bits_of(),
                                          "session-ticket.cipher-key");

   SymmetricKey hmac_key = kdf->derive_key(32, master_key.bits_of(),
                                           "session-ticket.hmac-key");

   InitializationVector aes_iv(rng, 16);

   std::auto_ptr<MessageAuthenticationCode> mac(get_mac("HMAC(SHA-256)"));
   mac->set_key(hmac_key);

   Pipe pipe(get_cipher("AES-128/CBC", aes_key, aes_iv, ENCRYPTION));
   pipe.process_msg(BER_encode());
   MemoryVector<byte> ctext = pipe.read_all(0);

   MemoryVector<byte> out;
   out += key_name;
   out += aes_iv.bits_of();
   out += ctext;

   mac->update(out);

   out += mac->final();
   return out;
   }

TLS_Session TLS_Session::decrypt(const MemoryRegion<byte>& buf,
                                 const SymmetricKey& master_key,
                                 const MemoryRegion<byte>& key_name)
   {
   try
      {
      if(buf.size() < (16 + 16 + 32 + 1))
         throw Decoding_Error("Encrypted TLS_Session too short to be real");

      if(master_key.length() == 0)
         throw Decoding_Error("TLS_Session master_key not set");

      if(key_name.size() != 16)
         throw Decoding_Error("Bad length " + to_string(key_name.size()) +
                              " for key_name");

      std::auto_ptr<KDF> kdf(get_kdf("KDF2(SHA-256)"));

      SymmetricKey hmac_key = kdf->derive_key(32, master_key.bits_of(),
                                              "session-ticket.hmac-key");

      std::auto_ptr<MessageAuthenticationCode> mac(get_mac("HMAC(SHA-256)"));
      mac->set_key(hmac_key);

      mac->update(&buf[0], buf.size() - 32);
      MemoryVector<byte> computed_mac = mac->final();

      if(!same_mem(&buf[buf.size() - 32], &computed_mac[0], computed_mac.size()))
         throw Decoding_Error("MAC verification failed");

      SymmetricKey aes_key = kdf->derive_key(16, master_key.bits_of(),
                                             "session-ticket.cipher-key");

      InitializationVector aes_iv(&buf[16], 16);

      Pipe pipe(get_cipher("AES-128/CBC", aes_key, aes_iv, DECRYPTION));
      pipe.process_msg(&buf[16], buf.size() - (16 + 32));
      MemoryVector<byte> ber = pipe.read_all();

      return TLS_Session(&ber[0], ber.size());
      }
   catch(...)
      {
      throw Decoding_Error("Failed to decrypt encrypted session");
      }
   }


}