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
|
#include <botan/botan.h>
#include <botan/tls_server.h>
#include <botan/rsa.h>
#include <botan/dsa.h>
#include <botan/x509self.h>
#include "socket.h"
using namespace Botan;
#include <stdio.h>
#include <string>
#include <iostream>
#include <memory>
class Server_TLS_Policy : public TLS_Policy
{
public:
bool check_cert(const std::vector<X509_Certificate>& certs) const
{
for(size_t i = 0; i != certs.size(); ++i)
{
std::cout << certs[i].to_string();
}
std::cout << "Warning: not checking cert signatures\n";
return true;
}
};
void proc_data(const byte data[], size_t data_len, u16bit alert_info)
{
printf("Block of data %d bytes alert %04X\n", (int)data_len, alert_info);
for(size_t i = 0; i != data_len; ++i)
printf("%c", data[i]);
}
int main(int argc, char* argv[])
{
int port = 4433;
if(argc == 2)
port = to_u32bit(argv[1]);
try
{
LibraryInitializer botan_init;
//SocketInitializer socket_init;
AutoSeeded_RNG rng;
//RSA_PrivateKey key(rng, 1024);
DSA_PrivateKey key(rng, DL_Group("dsa/jce/1024"));
X509_Cert_Options options(
"localhost/US/Syn Ack Labs/Mathematical Munitions Dept");
X509_Certificate cert =
X509::create_self_signed_cert(options, key, "SHA-1", rng);
Server_Socket listener(port);
Server_TLS_Policy policy;
while(true)
{
try {
printf("Listening for new connection on port %d\n", port);
Socket* sock = listener.accept();
printf("Got new connection\n");
TLS_Server tls(
std::tr1::bind(&Socket::write, std::tr1::ref(sock), _1, _2),
proc_data,
policy,
rng,
cert,
key);
SecureVector<byte> buf(1024);
size_t desired = 0;
while(!tls.is_active() || desired)
{
const size_t socket_got = sock->read(&buf[0], desired || 1);
desired = tls.received_data(&buf[0], socket_got);
}
const std::string hostname = tls.server_name_indicator();
if(hostname != "")
printf("Client requested host '%s'\n", hostname.c_str());
printf("Writing some text\n");
char msg[] = "Welcome to the best echo server evar\n";
tls.queue_for_sending((const Botan::byte*)msg, strlen(msg));
while(true)
{
size_t got = sock->read(&buf[0], buf.size(), true);
if(got == 0)
break;
tls.received_data(&buf[0], got);
}
tls.close();
}
catch(std::exception& e) { printf("%s\n", e.what()); }
}
}
catch(std::exception& e)
{
printf("%s\n", e.what());
return 1;
}
return 0;
}
|