-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.cpp
More file actions
executable file
·173 lines (149 loc) · 5.06 KB
/
server.cpp
File metadata and controls
executable file
·173 lines (149 loc) · 5.06 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
172
173
/*
* tls-example
* Copyright (c) 2020-2023 Peter Nebe <mail@peter-nebe.dev>
*
* This file is part of tls-example.
*
* tls-example is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* tls-example is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with tls-example. If not, see <https://www.gnu.org/licenses/>.
*/
#include "boost/asio.hpp"
#include "boost/asio/ssl.hpp"
#include "boost/algorithm/string/split.hpp"
#include <iostream>
#include <string>
using boost::asio::ip::tcp;
using namespace boost::asio;
using namespace std;
const size_t maxLength = 1000;
class Session : public enable_shared_from_this<Session>
{
public:
Session(ssl::stream<tcp::socket> sock)
: socket(move(sock)),
readbuf(maxLength, '\0')
{
}
void start()
{
handshake();
}
private:
void handshake()
{
auto self = shared_from_this();
socket.async_handshake(ssl::stream_base::server, [this, self](const boost::system::error_code &error)
{
if(error)
cout << "Handshake failed: " << error.message() << endl;
else
receiveRequest();
});
}
void receiveRequest()
{
auto self = shared_from_this();
socket.async_read_some(buffer(readbuf), [this, self](const boost::system::error_code &error, size_t length)
{
if(!error)
sendResponse(length);
});
}
void sendResponse(size_t length)
{
auto self = shared_from_this();
vector<string> words;
boost::algorithm::split(words, readbuf.substr(0, length), [](char c){ return c == ' '; });
ostringstream oss;
if(!words.empty())
{
auto wi = words.crbegin();
oss << *wi++;
for( ; wi != words.crend(); ++wi)
oss << ' ' << *wi;
}
writebuf = oss.str();
async_write(socket, buffer(writebuf), [this, self](const boost::system::error_code &error, size_t)
{
if(!error)
receiveRequest();
});
}
ssl::stream<tcp::socket> socket;
string readbuf;
string writebuf;
}; // class Session
class Server
{
public:
Server(io_context &ioctx, uint16_t port)
: acceptor(ioctx, tcp::endpoint(tcp::v4(), port)),
sslctx(ssl::context::tls_server)
{
sslctx.set_password_callback([](size_t, ssl::context::password_purpose){ return "tls-example"s; });
sslctx.use_private_key_file("tls-example-server.key", ssl::context::pem);
sslctx.use_certificate_file("tls-example-server.crt", ssl::context::pem);
#ifdef NO_CLIENT_AUTHENTICATION
sslctx.set_verify_mode(ssl::verify_none);
#else
sslctx.load_verify_file("tls-example-ca.crt");
sslctx.set_verify_mode(ssl::verify_peer | ssl::verify_fail_if_no_peer_cert);
sslctx.set_verify_callback(verifyClient);
#endif
accept();
}
private:
static bool verifyClient(bool preverified, ssl::verify_context &ctx)
{
if(!preverified)
return false;
if(X509_STORE_CTX_get_error_depth(ctx.native_handle()) > 0)
return true;
X509_NAME *subjectName = X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx.native_handle()));
const X509_NAME_ENTRY *nameEntry = X509_NAME_get_entry(subjectName, X509_NAME_get_index_by_NID(subjectName, NID_commonName, -1));
const string commonName = reinterpret_cast<const char*>(X509_NAME_ENTRY_get_data(nameEntry)->data);
return commonName.starts_with("tls-example-client");
}
void accept()
{
acceptor.async_accept([this](const boost::system::error_code &error, tcp::socket sock)
{
if(!error)
make_shared<Session>(ssl::stream<tcp::socket>(move(sock), sslctx))->start();
accept();
});
}
tcp::acceptor acceptor;
ssl::context sslctx;
}; // class Server
int main(int argc, char *argv[])
{
try
{
if(argc != 2)
{
cerr << "Usage: " << argv[0] << " PORT" << endl;
return 1;
}
const uint16_t port = stoul(argv[1]);
io_context ioctx;
Server svr(ioctx, port);
ioctx.run();
}
catch(const exception &ex)
{
cerr << "Exception: " << ex.what() << endl;
return 1;
}
return 0;
}