aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples/tls_server.cpp
blob: d5cb77a8485347579aa57fd55d869a4493dab01b (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
/*
* (C) 2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/init.h>
#include <botan/tls_server.h>
#include <botan/unx_sock.h>

#include <botan/rsa.h>
#include <botan/dsa.h>
#include <botan/x509self.h>

using namespace Botan;

#include <stdio.h>
#include <string>
#include <iostream>
#include <memory>

int main()
   {
   try
      {
      LibraryInitializer init;

      std::auto_ptr<RandomNumberGenerator> rng(
         RandomNumberGenerator::make_rng());

      RSA_PrivateKey key(*rng, 512);
      //DSA_PrivateKey key(get_dl_group("DSA-1024"));

      X509_Cert_Options options(
         "www.randombit.net/US/Syn Ack Labs/Mathematical Munitions Dept");

      X509_Certificate cert =
         X509::create_self_signed_cert(options, key, "SHA-1", *rng);

      Unix_Server_Socket listener(4433);

      printf("Now listening...\n");

      while(true)
         {
         try {
            Socket* sock = listener.accept();

            printf("Got new connection\n");

            TLS_Server tls(*rng, *sock, cert, key);

            char msg[] = "Foo\nBar\nBaz\nQuux\n";
            tls.write((const byte*)msg, strlen(msg));

            char buf[10] = { 0 };
            u32bit got = tls.read((byte*)buf, 9);
            printf("%d: '%s'\n", got, buf);

            tls.close();
            }
         catch(std::exception& e) { printf("%s\n", e.what()); }
         }
   }
   catch(std::exception& e)
      {
      printf("%s\n", e.what());
      return 1;
      }
   return 0;
   }