-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
283 lines (215 loc) · 7.53 KB
/
main.cpp
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
#include "apps.h"
#include <botan/tls_client.h>
#include <botan/pkcs8.h>
#include <botan/hex.h>
#include <string>
#include <iostream>
#include <memory>
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>
#include "credentials.h"
/**
* Code based on TLS client example here:
* http://botan.randombit.net/manual/tls.html#tls-clients
*/
using namespace Botan;
using namespace std::placeholders;
namespace {
/**
* @brief Shows hexdump of returned payload
* see: http://stahlworks.com/dev/index.php?tool=csc01
* @param pAddressIn
* @param lSize
*/
void hexdump(const void *pAddressIn, long lSize) {
char szBuf[100];
long lIndent = 1;
long lOutLen, lIndex, lIndex2, lOutLen2;
long lRelPos;
struct {
char *pData;
unsigned long lSize;
} buf;
unsigned char *pTmp,ucTmp;
unsigned char *pAddress = (unsigned char *)pAddressIn;
buf.pData = (char *)pAddress;
buf.lSize = lSize;
while (buf.lSize > 0) {
pTmp = (unsigned char *)buf.pData;
lOutLen = (int)buf.lSize;
if (lOutLen > 16)
lOutLen = 16;
// create a 64-character formatted output line:
sprintf(szBuf, " > "
" "
" %08lX", pTmp-pAddress);
lOutLen2 = lOutLen;
for(lIndex = 1+lIndent, lIndex2 = 53-15+lIndent, lRelPos = 0;
lOutLen2;
lOutLen2--, lIndex += 2, lIndex2++
)
{
ucTmp = *pTmp++;
sprintf(szBuf + lIndex, "%02X ", (unsigned short)ucTmp);
if(!isprint(ucTmp)) ucTmp = '.'; // nonprintable char
szBuf[lIndex2] = ucTmp;
if (!(++lRelPos & 3)) // extra blank after 4 bytes
{ lIndex++; szBuf[lIndex+2] = ' '; }
}
if (!(lRelPos & 3)) lIndex--;
szBuf[lIndex ] = '<';
szBuf[lIndex+1] = ' ';
printf("%s\n", szBuf);
buf.pData += lOutLen;
buf.lSize -= lOutLen;
}
}
int connect_to_host(const std::string& host, u16bit port, const std::string& transport) {
hostent* host_addr = ::gethostbyname(host.c_str());
if(!host_addr)
throw std::runtime_error("gethostbyname failed for " + host);
if(host_addr->h_addrtype != AF_INET) // no ipv6 support
throw std::runtime_error(host + " has IPv6 address, not supported");
int type = (transport == "tcp") ? SOCK_STREAM : SOCK_DGRAM;
int fd = ::socket(PF_INET, type, 0);
if(fd == -1)
throw std::runtime_error("Unable to acquire socket");
sockaddr_in socket_info;
::memset(&socket_info, 0, sizeof(socket_info));
socket_info.sin_family = AF_INET;
socket_info.sin_port = htons(port);
::memcpy(&socket_info.sin_addr,
host_addr->h_addr,
host_addr->h_length);
socket_info.sin_addr = *(struct in_addr*)host_addr->h_addr;
if(::connect(fd, (sockaddr*)&socket_info, sizeof(struct sockaddr)) != 0) {
::close(fd);
throw std::runtime_error("connect failed");
}
return fd;
}
bool handshake_complete(const TLS::Session& session) {
std::cout << "Handshake complete, " << session.version().to_string()
<< " using " << session.ciphersuite().to_string() << "\n";
if(!session.session_id().empty())
std::cout << "Session ID " << hex_encode(session.session_id()) << "\n";
if(!session.session_ticket().empty())
std::cout << "Session ticket " << hex_encode(session.session_ticket()) << "\n";
return true;
}
/**
* @brief send data through Unix sockets
* @param sockfd
* @param buf
* @param length
*/
void stream_socket_write(int sockfd, const byte buf[], size_t length) {
size_t offset = 0;
while(length) {
std::cout << "stream_socket_write(): About to send: " << std::endl;
hexdump(buf, length);
ssize_t sent = ::send(sockfd, (const char*)buf + offset,
length, MSG_NOSIGNAL);
if(sent == -1) {
if(errno == EINTR)
sent = 0;
else
throw std::runtime_error("Socket::write: Socket write failed");
}
offset += sent;
length -= sent;
}
}
//global status variables
bool got_alert = false;
int got_data = 0;
void alert_received(TLS::Alert alert, const byte data[], size_t data_len) {
got_data = data_len;
std::cout << "Alert: " << alert.type_string() << "\n";
std::cout << "Alert data len: " << data_len << "\n";
hexdump(data, data_len);
got_alert = true;
}
void process_data(const byte buf[], size_t buf_size) {
for(size_t i = 0; i != buf_size; ++i)
std::cout << buf[i];
}
std::string protocol_chooser(const std::vector<std::string>& protocols) {
for(size_t i = 0; i != protocols.size(); ++i)
std::cout << "Protocol " << i << " = " << protocols[i] << "\n";
return "http/1.1";
}
} //end namespace
int main(int argc, char* argv[]) {
if(argc != 2 && argc != 3 && argc != 4) {
std::cout << "Usage " << argv[0] << " host [port]\n";
return 1;
}
try {
AutoSeeded_RNG rng;
TLS::Policy policy;
TLS::Session_Manager_In_Memory session_manager(rng);
Credentials_Manager_Simple creds(rng);
std::string host = argv[1];
u32bit port = argc >= 3 ? Botan::to_u32bit(argv[2]) : 443;
int sockfd = connect_to_host(host, port, "tcp");
auto socket_write = std::bind(stream_socket_write, sockfd, _1, _2);
auto version = TLS::Protocol_Version::latest_tls_version();
//initialize the client and set callback functions defined above
TLS::Client client(socket_write,
process_data,
alert_received,
handshake_complete,
session_manager,
creds,
policy,
rng,
TLS::Server_Information(host, port),
version,
protocol_chooser);
while(!client.is_closed()) {
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
byte buf[4*1024] = { 0 };
ssize_t got = ::read(sockfd, buf, sizeof(buf));
if(got == 0) {
std::cout << "EOF on socket\n";
break;
}
else if(got == -1) {
std::cout << "Socket error: " << errno << " " << strerror(errno) << "\n";
continue;
}
std::cout << "Socket - got " << got << " bytes\n";
client.received_data(buf, got);
//Stop if we received payload data from server
if(got_data > 0) {
client.close();
}
if(client.heartbeat_sending_allowed()) {
std::cout << "Heartbeats allowed, sending some..." << std::endl;
//we dont need any payload => nullptr
//size of the hearbeat payload could go up to 64K
client.heartbeat(nullptr, 1024);
} else {
std::cout << "Heardbeats NOT allowed..." << std::endl;
}
}
::close(sockfd);
} catch(const std::exception& e) {
std::cout << "Exception: " << e.what() << "\n";
return 1;
}
return 0;
}