-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelloWorld.cxx
More file actions
172 lines (131 loc) · 4.75 KB
/
helloWorld.cxx
File metadata and controls
172 lines (131 loc) · 4.75 KB
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
#include "helloWorld.hxx"
#include <iostream>
#include <string>
#include <fstream>
#include <stdexcept>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/bind.hpp>
#include <boost/bind/arg.hpp>
#include <boost/program_options.hpp>
#include <openssl/ssl.h>
#include <openssl/safestack.h>
using namespace boost::asio;
using namespace boost::asio::ssl;
namespace po = boost::program_options;
typedef stream<ip::tcp::socket> ssl_socket;
SslFixedReplySession::SslFixedReplySession(io_service& io_, context& sslContext_)
: socket(io_, sslContext_)
{
}
void SslFixedReplySession::start() {
socket.async_handshake(boost::asio::ssl::stream_base::server,
boost::bind(&SslFixedReplySession::handle_handShake, this, placeholders::error));
}
void SslFixedReplySession::handle_handShake(const boost::system::error_code& ec) {
if(!ec) {
write(socket, buffer("Hello World\n"));
socket.async_shutdown(boost::bind(&SslFixedReplySession::handle_shutdown, this, placeholders::error));
} else {
std::cerr << "Handshake error occured: " << ec.message() << "\n";
delete this;
}
}
void SslFixedReplySession::handle_shutdown(const boost::system::error_code& ec) {
if(!ec) {
std::cout << "Shutdown succesful.\n";
} else {
if(ec == error::eof) {
// Expected, ignore
} else {
std::cerr << "Error occured: " << ec.message();
}
}
delete this;
}
ssl_socket::lowest_layer_type& SslFixedReplySession::socketType() {
return socket.lowest_layer();
}
SslFixedReplyServer::SslFixedReplyServer(io_service& io_, int port,
const std::string& cert, const std::string& privateKey,
const std::vector<std::string>& trustedCaCertificateFiles)
: io(io_),
acceptor(io, ip::tcp::endpoint(ip::tcp::v4(), port)),
sslContext(context::sslv3) {
for(std::string caCertFile: trustedCaCertificateFiles) {
addTrustedCaCertificate(caCertFile, true);
}
sslContext.set_verify_mode(verify_fail_if_no_peer_cert | verify_peer);
sslContext.set_verify_callback(boost::bind(&SslFixedReplyServer::handle_verify, this, _1, _2));
sslContext.set_password_callback(boost::bind(&SslFixedReplyServer::get_password, this));
sslContext.use_certificate_file(cert, context::pem);
sslContext.use_private_key_file(privateKey, context::pem);
start_accept();
}
void SslFixedReplyServer::addTrustedCaCertificate(std::string fileName, bool addToClientCaList) {
sslContext.load_verify_file(fileName);
if (addToClientCaList) {
STACK_OF(X509_NAME) *old_cert_names = SSL_CTX_get_client_CA_list(sslContext.native_handle());
if(old_cert_names != nullptr && sk_X509_NAME_num(old_cert_names) > 0) {
throw new std::logic_error("TODO: implement merging stacks of client CAs");
}
SSL_CTX_set_client_CA_list(sslContext.native_handle(), SSL_load_client_CA_file(fileName.c_str()));
}
}
std::string SslFixedReplyServer::get_password() const {
return "";
}
void SslFixedReplyServer::start_accept() {
SslFixedReplySession* s = new SslFixedReplySession(io, sslContext);
acceptor.async_accept(s->socketType(),
boost::bind(&SslFixedReplyServer::handle_accept, this, s, placeholders::error));
}
void SslFixedReplyServer::handle_accept(SslFixedReplySession* session, const boost::system::error_code& ec) {
std::cout << "Accepted connection\n";
if(!ec) {
session->start();
} else {
std::cerr << "Error occured: " << ec.message();
delete session;
}
start_accept();
}
bool SslFixedReplyServer::handle_verify(bool preverified, verify_context& context) {
std::cout << "Server Verifying: preverified" << (preverified ? "true" : "false") << "\n";
return preverified;
}
int main(int argc, char* argv[]) {
int port;
std::string certFile;
std::string certPrivateKeyFile;
std::vector<std::string> caFiles;
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "display this message")
("port,p", po::value<int>(&port)->required(), "port to listen on")
("certFile", po::value<std::string>(&certFile)->required(), "certificate to send as authentication")
("certKey", po::value<std::string>(&certPrivateKeyFile)->required(), "private key for certificate")
("caFile", po::value<std::vector<std::string> >(&caFiles), "List certificate files of trusted CA's")
;
try {
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
if(vm.count("help")) {
desc.print(std::cout);
exit(0);
}
po::notify(vm);
} catch (std::exception& ex) {
std::cerr << "Missing required option: \n";
std::cerr << ex.what() << "\n";
std::cerr << "Useage:\n";
desc.print(std::cerr);
exit(1);
}
boost::asio::io_service io;
SslFixedReplyServer server(io, port, certFile, certPrivateKeyFile, caFiles);
std::cout << "Waiting...\n";
io.run();
}
/* vim: set tabstop=4 shiftwidth=4 formatoptions=cqwan autoindent: */