-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
175 lines (137 loc) · 4.86 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
/* SSLDemo 16/03/2015
$$$$$$$$$$$$$$$$$$$
$ SSLDemo.cpp $
$$$$$$$$$$$$$$$$$$$
Copyright (C) 2015 W.B. Yates
This program 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.
This program 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 this program. If not, see http://www.gnu.org/licenses/
History:
Very simple secure socket class
We use the OpenSSL interface, see https://wiki.openssl.org/index.php/Main_Page
and the TLS protocol, see https://tools.ietf.org/html/rfc5246
We support session resumption on server and client sides
*** Updated 24/10/23 ***
The implementation of OpenSSL used here is OpenSSL 3.1.3 19 Sep 2023
We recommend LibreSSL see https://www.libressl.org
See also:
https://en.wikipedia.org/wiki/X.509
https://en.wikipedia.org/wiki/Transport_Layer_Security
Compile with the library flags: -lssl -lcrypto
*/
#include <iostream>
#include <thread>
#include <unistd.h>
#ifndef __SSLSOCKET_H__
#include "SSLSocket.h"
#endif
const int PORT = 4123;
void
sender1( SSLSocket *client )
{
int count = 3;
if (client->open()) // 127.0.0.1
{
std::cout << "client1 connecting" << std::endl;
if (client->connect("127.0.0.1", PORT))
{
while (count--)
{
std::cout << " client1 sending 'Hello World'" << std::endl;
client->send(std::string("Hello World"));
std::string msg;
if (client->receive(msg))
std::cout << " client1 receiving " << msg << std::endl;
sleep(1);
}
}
}
client->close(); // close sends the EOF that allows receiver to read the message
std::cout << "client1 closed" << std::endl;
}
void
sender2( SSLSocket *client )
{
int count = 3;
std::cout << "client2 re-connecting" << std::endl;
if (client->open() && client->reconnect())
{
while (count--)
{
std::cout << " client2 sending 'Hello World'" << std::endl;
client->send(std::string("Hello World"));
std::string msg;
if (client->receive(msg))
std::cout << " client2 receiving " << msg << std::endl;
sleep(1);
}
}
client->close(); // close sends the EOF that allows receiver to read the message
std::cout << "client2 closed" << std::endl;
}
void
rec( SSLSocket *server )
{
server->open();
server->bind(PORT);
server->listen();
int count = 3;
SSLSocket client;
// Socket client;
// we block here
if ( server->accept(client) )
{
while (count--)
{
std::string msgIn;
if ( client.receive(msgIn) )
{
std::cout << "Server recieved message: '" << msgIn << "'" << std::endl;
std::cout << "Server sending ACK" << std::endl;
client.send("ACK");
}
sleep(1);
}
}
server->close();
std::cout << "Server closed" << std::endl;
}
int
main(int argc, char *argv[])
{
std::cout << "\nSSLDemo Copyright (C) 2023, W. B. Yates" << std::endl;
std::cout << "\nThis program comes with ABSOLUTELY NO WARRANTY;" << std::endl;
std::cout << "for details see http://www.gnu.org/licenses/." << std::endl;
std::cout << "This is free software, and you are welcome to redistribute it" << std::endl;
std::cout << "under certain conditions; see http://www.gnu.org/licenses/\n" << std::endl;
// name, keyfile, PEM pass phrase
SSLContext ctx1("client", "client.pem", "password");
SSLContext ctx2("server", "server.pem", "password", true);
SSLSocket::registerContext( &ctx1 );
SSLSocket::registerContext( &ctx2 );
SSLSocket client;
client.setContext("client");
SSLSocket server("server");
//Socket client;
//Socket server;
for (int i = 0; i < 5; ++i)
{
std::cout << "\nBegin socket test " << i + 1 << std::endl;
std::thread thread1 = std::thread(rec, &server);
std::thread thread2;
if (i == 0)
thread2 = std::thread(sender1, &client);
else thread2 = std::thread(sender2, &client);
thread1.join();
thread2.join();
}
std::cout << "End socket test" << std::endl;
SSLSocket::clearRegister();
}