1 #include <botan/botan.h>
2 #include <botan/tls_server.h>
3
4 #include <botan/rsa.h>
5 #include <botan/dsa.h>
6 #include <botan/x509self.h>
7
8 #include "socket.h"
9
10 using namespace Botan;
11
12 #include <stdio.h>
13 #include <string>
14 #include <iostream>
15 #include <memory>
16
17 class Server_TLS_Policy : public TLS_Policy
18 {
19 public:
check_cert(const std::vector<X509_Certificate> & certs) const20 bool check_cert(const std::vector<X509_Certificate>& certs) const
21 {
22 for(size_t i = 0; i != certs.size(); ++i)
23 {
24 std::cout << certs[i].to_string();
25 }
26
27 std::cout << "Warning: not checking cert signatures\n";
28
29 return true;
30 }
31 };
32
main(int argc,char * argv[])33 int main(int argc, char* argv[])
34 {
35 int port = 4433;
36
37 if(argc == 2)
38 port = to_u32bit(argv[1]);
39
40 try
41 {
42 LibraryInitializer botan_init;
43 SocketInitializer socket_init;
44
45 AutoSeeded_RNG rng;
46
47 //RSA_PrivateKey key(rng, 1024);
48 DSA_PrivateKey key(rng, DL_Group("dsa/jce/1024"));
49
50 X509_Cert_Options options(
51 "localhost/US/Syn Ack Labs/Mathematical Munitions Dept");
52
53 X509_Certificate cert =
54 X509::create_self_signed_cert(options, key, "SHA-1", rng);
55
56 Server_Socket listener(port);
57
58 Server_TLS_Policy policy;
59
60 while(true)
61 {
62 try {
63 printf("Listening for new connection on port %d\n", port);
64
65 Socket* sock = listener.accept();
66
67 printf("Got new connection\n");
68
69 TLS_Server tls(
70 std::tr1::bind(&Socket::read, std::tr1::ref(sock), _1, _2),
71 std::tr1::bind(&Socket::write, std::tr1::ref(sock), _1, _2),
72 policy,
73 rng,
74 cert,
75 key);
76
77 std::string hostname = tls.requested_hostname();
78
79 if(hostname != "")
80 printf("Client requested host '%s'\n", hostname.c_str());
81
82 printf("Writing some text\n");
83
84 char msg[] = "Foo\nBar\nBaz\nQuux\n";
85 tls.write((const Botan::byte*)msg, strlen(msg));
86
87 printf("Now trying a read...\n");
88
89 char buf[1024] = { 0 };
90 u32bit got = tls.read((Botan::byte*)buf, sizeof(buf)-1);
91 printf("%d: '%s'\n", got, buf);
92
93 tls.close();
94 }
95 catch(std::exception& e) { printf("%s\n", e.what()); }
96 }
97 }
98 catch(std::exception& e)
99 {
100 printf("%s\n", e.what());
101 return 1;
102 }
103 return 0;
104 }
105