aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/unit_tls.cpp
blob: 875fe8a4812e5503b45abacd3edb5fdd801595ba (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

#include "tests.h"

#if defined(BOTAN_HAS_TLS)

#include <botan/auto_rng.h>
#include <botan/tls_server.h>
#include <botan/tls_client.h>
#include <botan/pkcs10.h>
#include <botan/x509self.h>
#include <botan/rsa.h>
#include <botan/x509_ca.h>
#include <botan/hex.h>

#include <iostream>
#include <vector>
#include <memory>

using namespace Botan;

namespace {

class Credentials_Manager_Test : public Botan::Credentials_Manager
   {
   public:
      Credentials_Manager_Test(const X509_Certificate& server_cert,
                               const X509_Certificate& ca_cert,
                               Private_Key* server_key) :
         m_server_cert(server_cert),
         m_ca_cert(ca_cert),
         m_key(server_key)
         {
         auto store = new Certificate_Store_In_Memory;
         store->add_certificate(m_ca_cert);
         m_stores.push_back(store);
         }

      std::vector<Certificate_Store*>
      trusted_certificate_authorities(const std::string&,
                                      const std::string&) override
         {
         return m_stores;
         }

      std::vector<X509_Certificate> cert_chain(
         const std::vector<std::string>& cert_key_types,
         const std::string& type,
         const std::string&) override
         {
         std::vector<X509_Certificate> chain;

         if(type == "tls-server")
            {
            bool have_match = false;
            for(size_t i = 0; i != cert_key_types.size(); ++i)
               if(cert_key_types[i] == m_key->algo_name())
                  have_match = true;

            if(have_match)
               {
               chain.push_back(m_server_cert);
               chain.push_back(m_ca_cert);
               }
            }

         return chain;
         }

      void verify_certificate_chain(
         const std::string& type,
         const std::string& purported_hostname,
         const std::vector<Botan::X509_Certificate>& cert_chain) override
         {
         try
            {
            Credentials_Manager::verify_certificate_chain(type,
                                                          purported_hostname,
                                                          cert_chain);
            }
         catch(std::exception& e)
            {
            std::cout << "Certificate verification failed - " << e.what() << " - but will ignore\n";
            }
         }

      Private_Key* private_key_for(const X509_Certificate&,
                                   const std::string&,
                                   const std::string&) override
         {
         return m_key;
         }
   public:
      X509_Certificate m_server_cert, m_ca_cert;
      Private_Key* m_key;
      std::vector<Certificate_Store*> m_stores;
   };

Credentials_Manager* create_creds(RandomNumberGenerator& rng)
   {
   std::auto_ptr<Private_Key> ca_key(new RSA_PrivateKey(rng, 1024));

   X509_Cert_Options ca_opts;
   ca_opts.common_name = "Test CA";
   ca_opts.country = "US";
   ca_opts.CA_key(1);

   X509_Certificate ca_cert =
      X509::create_self_signed_cert(ca_opts,
                                    *ca_key,
                                    "SHA-256",
                                    rng);

   Private_Key* server_key = new RSA_PrivateKey(rng, 1024);

   X509_Cert_Options server_opts;
   server_opts.common_name = "localhost";
   server_opts.country = "US";

   PKCS10_Request req = X509::create_cert_req(server_opts,
                                              *server_key,
                                              "SHA-256",
                                              rng);

   X509_CA ca(ca_cert, *ca_key, "SHA-256");

   auto now = std::chrono::system_clock::now();
   X509_Time start_time(now);
   typedef std::chrono::duration<int, std::ratio<31556926>> years;
   X509_Time end_time(now + years(1));

   X509_Certificate server_cert = ca.sign_request(req,
                                                  rng,
                                                  start_time,
                                                  end_time);

   return new Credentials_Manager_Test(server_cert, ca_cert, server_key);
   }

size_t basic_test_handshake(RandomNumberGenerator& rng,
                            TLS::Protocol_Version offer_version,
                            Credentials_Manager& creds,
                            TLS::Policy& policy)
   {
   TLS::Session_Manager_In_Memory server_sessions(rng);
   TLS::Session_Manager_In_Memory client_sessions(rng);

   std::vector<byte> c2s_q, s2c_q, c2s_data, s2c_data;

   auto handshake_complete = [&](const TLS::Session& session) -> bool
   {
      if(session.version() != offer_version)
         std::cout << "Wrong version negotiated\n";
      return true;
   };

   auto print_alert = [&](TLS::Alert alert, const byte[], size_t)
   {
      if(alert.is_valid())
         std::cout << "Server recvd alert " << alert.type_string() << "\n";
   };

   auto save_server_data = [&](const byte buf[], size_t sz)
   {
      c2s_data.insert(c2s_data.end(), buf, buf+sz);
   };

   auto save_client_data = [&](const byte buf[], size_t sz)
   {
      s2c_data.insert(s2c_data.end(), buf, buf+sz);
   };

   TLS::Server server([&](const byte buf[], size_t sz)
                      { s2c_q.insert(s2c_q.end(), buf, buf+sz); },
                      save_server_data,
                      print_alert,
                      handshake_complete,
                      server_sessions,
                      creds,
                      policy,
                      rng,
                      { "test/1", "test/2" });

   auto next_protocol_chooser = [&](std::vector<std::string> protos) {
      if(protos.size() != 2)
         std::cout << "Bad protocol size\n";
      if(protos[0] != "test/1" || protos[1] != "test/2")
         std::cout << "Bad protocol values\n";
      return "test/3";
   };

   TLS::Client client([&](const byte buf[], size_t sz)
                      { c2s_q.insert(c2s_q.end(), buf, buf+sz); },
                      save_client_data,
                      print_alert,
                      handshake_complete,
                      client_sessions,
                      creds,
                      policy,
                      rng,
                      TLS::Server_Information(),
                      offer_version,
                      next_protocol_chooser);

   while(true)
      {
      if(client.is_active())
         client.send("1");
      if(server.is_active())
         {
         if(server.next_protocol() != "test/3")
            std::cout << "Wrong protocol " << server.next_protocol() << "\n";
         server.send("2");
         }

      /*
      * Use this as a temp value to hold the queues as otherwise they
      * might end up appending more in response to messages during the
      * handshake.
      */
      std::vector<byte> input;
      std::swap(c2s_q, input);

      try
         {
         server.received_data(&input[0], input.size());
         }
      catch(std::exception& e)
         {
         std::cout << "Server error - " << e.what() << "\n";
         break;
         }

      input.clear();
      std::swap(s2c_q, input);

      try
         {
         client.received_data(&input[0], input.size());
         }
      catch(std::exception& e)
         {
         std::cout << "Client error - " << e.what() << "\n";
         break;
         }

      if(c2s_data.size())
         {
         if(c2s_data[0] != '1')
            {
            std::cout << "Error\n";
            return 1;
            }
         }

      if(s2c_data.size())
         {
         if(s2c_data[0] != '2')
            {
            std::cout << "Error\n";
            return 1;
            }
         }

      if(s2c_data.size() && c2s_data.size())
         break;
      }

   return 0;
   }

class Test_Policy : public TLS::Policy
   {
   public:
      bool acceptable_protocol_version(TLS::Protocol_Version) const { return true; }
   };

}

size_t test_tls()
   {
   size_t errors = 0;

   Test_Policy default_policy;
   AutoSeeded_RNG rng;
   std::auto_ptr<Credentials_Manager> basic_creds(create_creds(rng));

   errors += basic_test_handshake(rng, TLS::Protocol_Version::SSL_V3, *basic_creds, default_policy);
   errors += basic_test_handshake(rng, TLS::Protocol_Version::TLS_V10, *basic_creds, default_policy);
   errors += basic_test_handshake(rng, TLS::Protocol_Version::TLS_V11, *basic_creds, default_policy);
   errors += basic_test_handshake(rng, TLS::Protocol_Version::TLS_V12, *basic_creds, default_policy);

   test_report("TLS", 4, errors);

   return errors;
   }

#else
size_t test_tls() { return 0; }
#endif