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
|
/* Starshatter: The Open Source Project
Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
Copyright (c) 1997-2006, Destroyer Studios LLC.
AUTHOR: John DiCamillo
OVERVIEW
========
This class represents a user connecting to the multiplayer lobby
*/
#include "NetAuth.h"
#include "NetLobby.h"
#include "NetUser.h"
#include "ModConfig.h"
#include "ModInfo.h"
#include "Random.h"
#include "Sha1.h"
static int auth_level = NetAuth::NET_AUTH_MINIMAL;
// +-------------------------------------------------------------------+
int
NetAuth::AuthLevel()
{
return auth_level;
}
void
NetAuth::SetAuthLevel(int n)
{
if (n >= NET_AUTH_MINIMAL && n <= NET_AUTH_SECURE)
auth_level = n;
}
// +-------------------------------------------------------------------+
Text
NetAuth::CreateAuthRequest(NetUser* u)
{
Text request;
if (u) {
u->SetAuthLevel(auth_level);
if (auth_level == NET_AUTH_MINIMAL) {
u->SetAuthState(NET_AUTH_OK);
u->SetSalt("Very Low Sodium");
}
else if (auth_level == NET_AUTH_STANDARD) {
u->SetAuthState(NET_AUTH_INITIAL);
u->SetSalt("Very Low Sodium");
request = "level 1";
}
else {
char salt[33];
for (int i = 0; i < 32; i++)
salt[i] = (char) ('0' + (int) Random(0, 9.4));
salt[32] = 0;
u->SetSalt(salt);
u->SetAuthState(NET_AUTH_INITIAL);
request = "level 2 salt ";
request += salt;
}
}
return request;
}
// +-------------------------------------------------------------------+
static Text Digest(const char* salt, const char* file)
{
int length = 0;
int offset = 0;
char block[4096];
char digest[64];
ZeroMemory(digest, sizeof(digest));
if (file) {
FILE* f;
fopen_s(&f, file, "rb");
if (f) {
SHA1 sha1;
if (salt) {
sha1.Input(salt, strlen(salt));
}
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
while (offset < length) {
int n = fread(block, sizeof(char), 4096, f);
sha1.Input(block, n);
offset += n;
}
fclose(f);
unsigned result[5];
if (sha1.Result(result)) {
sprintf_s(digest, "SHA1_%08X_%08X_%08X_%08X_%08X",
result[0], result[1], result[2], result[3], result[4]);
}
}
}
return digest;
}
// +-------------------------------------------------------------------+
Text
NetAuth::CreateAuthResponse(int level, const char* salt)
{
Text response;
ModConfig* config = ModConfig::GetInstance();
if (level == NET_AUTH_SECURE) {
response += "exe ";
response += Digest(salt, "stars.exe"); // XXX should look up name of this exe
response += " ";
response += "dat ";
response += Digest(salt, "shatter.dat");
response += " ";
response += "etc ";
response += Digest(salt, "start.dat");
response += " ";
}
if (level >= NET_AUTH_STANDARD) {
List<ModInfo>& mods = config->GetModInfoList();
ListIter<ModInfo> mod_iter = mods;
char buffer[32];
sprintf_s(buffer, "num %d ", mods.size());
response += buffer;
while (++mod_iter) {
ModInfo* info = mod_iter.value();
response += "mod \"";
response += info->Name();
response += "\" ver \"";
response += info->Version();
response += "\" ";
if (level == NET_AUTH_SECURE) {
response += "sha ";
response += Digest(salt, info->Filename());
response += " ";
}
}
}
return response;
}
// +-------------------------------------------------------------------+
bool
NetAuth::AuthUser(NetUser* u, Text response)
{
bool authentic = false;
if (auth_level == NET_AUTH_MINIMAL) { // (this case should not occur)
if (u) {
u->SetAuthLevel(auth_level);
u->SetAuthState(NET_AUTH_OK);
}
authentic = (u != 0);
}
else if (u) {
Text expected_response = CreateAuthResponse(auth_level, u->Salt());
if (expected_response == response)
authentic = true;
u->SetAuthState(authentic ? NET_AUTH_OK : NET_AUTH_FAILED);
}
return authentic;
}
// +-------------------------------------------------------------------+
|