-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpropa_auth.h
More file actions
252 lines (228 loc) · 6.47 KB
/
propa_auth.h
File metadata and controls
252 lines (228 loc) · 6.47 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
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
/*
Kage game server.
Copyright 2019 Shuouma <dreamcast-talk.com>
Copyright 2025 Flyinghead <flyinghead.github@gmail.com>
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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "kage.h"
#include "log.h"
#include <dcserver/shared_this.hpp>
#include <dcserver/asio.hpp>
extern "C" {
#include "blowfish.h"
}
#include <array>
#include <string>
#include <vector>
using namespace std::chrono_literals;
constexpr Game game = Game::PropellerA;
static inline void xor55(uint8_t *data, size_t len)
{
uint8_t *end = data + len;
while (data != end)
*data++ ^= 0x55;
}
class AuthConnection : public SharedThis<AuthConnection>
{
public:
asio::ip::tcp::socket& getSocket() {
return socket;
}
void receive()
{
startTimer();
// TODO packet matcher, use DynamicBuffer?
asio::async_read(socket, asio::buffer(recvBuffer), asio::transfer_at_least(0x68),
std::bind(&AuthConnection::onReceive, shared_from_this(), asio::placeholders::error, asio::placeholders::bytes_transferred));
}
private:
AuthConnection(asio::io_context& io_context)
: socket(io_context), timer(io_context) {}
void send()
{
asio::async_write(socket, asio::buffer(sendBuffer),
std::bind(&AuthConnection::onSent, shared_from_this(),
asio::placeholders::error,
asio::placeholders::bytes_transferred));
}
void onSent(const std::error_code& ec, size_t len)
{
if (ec) {
ERROR_LOG(game, "Send error: %s", ec.message().c_str());
}
else {
DEBUG_LOG(game, "sent %zd bytes", len);
receive();
}
}
void initBlowfish(const uint8_t *key) {
Blowfish_Init(&blowfishCtx, (uint8_t *)key, KEY_SIZE);
}
void encrypt(uint8_t *data, size_t len)
{
for (size_t i = 0; i < len; i += 8)
{
uint32_t l = read32(data, i);
uint32_t r = read32(data, i + 4);
Blowfish_Encrypt(&blowfishCtx, &l, &r);
write32(data, i, l);
write32(data, i + 4, r);
}
}
void decrypt(uint8_t *data, size_t len)
{
for (size_t i = 0; i < len; i += 8)
{
uint32_t l = read32(data, i);
uint32_t r = read32(data, i + 4);
Blowfish_Decrypt(&blowfishCtx, &l, &r);
write32(data, i, l);
write32(data, i + 4, r);
}
}
void onReceive(const std::error_code& ec, size_t len)
{
if (ec || len == 0)
{
if (ec && ec != asio::error::eof)
ERROR_LOG(game, "auth: %s", ec.message().c_str());
return;
}
if (len < 4) {
ERROR_LOG(game, "auth: small packet (%zd bytes)", len);
return;
}
uint32_t msg = read32(recvBuffer.data(), 0);
switch (msg)
{
case 1:
{
// Registration step 1
memcpy(key.data(), recvBuffer.data() + 4, key.size());
xor55(key.data(), key.size());
initBlowfish(key.data());
decrypt(recvBuffer.data() + 0x40, len - 0x40);
INFO_LOG(game, "auth: registration for %s", &recvBuffer[0x54]);
//dumpData(recvBuffer.data() + 0x40, len - 0x40);
// expect 0x38 bytes
// status 0 at offset 0
// uses 16 bytes of key at offset 4-13
// will respond with MSG3
sendBuffer.fill(0);
encrypt(sendBuffer.data(), sendBuffer.size());
send();
break;
}
case 3:
{
// Registration step 2
std::array<uint8_t, KEY_SIZE> key2;
key2 = key;
memset(key2.data(), 0, 16);
initBlowfish(key2.data());
decrypt(recvBuffer.data() + 0x40, len - 0x40);
DEBUG_LOG(game, "MSG3:");
//dumpData(recvBuffer.data() + 0x40, len - 0x40);
// same reply as msg1
// user game id at offset 14
sendBuffer.fill(0);
// Use the player name as game id
strcpy((char *)&sendBuffer[0x14], (const char *)&recvBuffer[0x54]);
encrypt(sendBuffer.data(), sendBuffer.size());
send();
break;
}
case 2:
{
// Login step 1
memcpy(key.data(), recvBuffer.data() + 4, key.size());
xor55(key.data(), key.size());
initBlowfish(key.data());
decrypt(recvBuffer.data() + 0x40, len - 0x40);
INFO_LOG(game, "auth: login for game id %s, user name: %s", &recvBuffer[0x40], &recvBuffer[0x74]);
DEBUG_LOG(game, "Dricas game ID: %.16s", &recvBuffer[0x64]);
// expect 0x38 bytes
// status 0 at offset 0
// uses 16 bytes of key at offset 4-13
// will respond with MSG4
sendBuffer.fill(0);
encrypt(sendBuffer.data(), sendBuffer.size());
send();
break;
}
case 4:
{
// Login step 2
std::array<uint8_t, KEY_SIZE> key2;
key2 = key;
memset(key2.data(), 0, 16);
initBlowfish(key2.data());
decrypt(recvBuffer.data() + 0x40, len - 0x40);
DEBUG_LOG(game, "MSG4: user name: %s", &recvBuffer[0x74]);
//dumpData(recvBuffer.data() + 0x40, len - 0x40);
sendBuffer.fill(0);
encrypt(sendBuffer.data(), sendBuffer.size());
send();
break;
}
default:
ERROR_LOG(game, "auth: unhandled message %d", msg);
receive();
}
}
void startTimer()
{
timer.expires_after(30s);
timer.async_wait([this](const std::error_code& ec) {
if (ec)
return;
std::error_code ignored;
socket.shutdown(asio::socket_base::shutdown_both, ignored);
});
}
static constexpr size_t KEY_SIZE = 56;
asio::ip::tcp::socket socket;
std::array<uint8_t, 0x90> recvBuffer;
std::array<uint8_t, 0x38> sendBuffer;
std::array<uint8_t, KEY_SIZE> key;
BLOWFISH_CTX blowfishCtx;
asio::steady_timer timer;
friend super;
};
class AuthAcceptor
{
public:
AuthAcceptor(asio::io_context& io_context)
: io_context(io_context),
acceptor(asio::ip::tcp::acceptor(io_context,
asio::ip::tcp::endpoint(asio::ip::tcp::v4(), 20200)))
{
asio::socket_base::reuse_address option(true);
acceptor.set_option(option);
}
void start()
{
AuthConnection::Ptr newConnection = AuthConnection::create(io_context);
acceptor.async_accept(newConnection->getSocket(),
[this, newConnection](const std::error_code& error) {
if (!error) {
INFO_LOG(game, "New connection from %s", newConnection->getSocket().remote_endpoint().address().to_string().c_str());
newConnection->receive();
}
start();
});
}
private:
asio::io_context& io_context;
asio::ip::tcp::acceptor acceptor;
};