This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAsyncWebAuthentication_RP2040W.cpp
374 lines (273 loc) · 8.39 KB
/
AsyncWebAuthentication_RP2040W.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/****************************************************************************************************************************
AsyncWebAuthentication_RP2040W.cpp
For RP2040W with CYW43439 WiFi
AsyncWebServer_RP2040W is a library for the RP2040W with CYW43439 WiFi
Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
Built by Khoi Hoang https://github.com/khoih-prog/AsyncWebServer_RP2040W
Licensed under GPLv3 license
Version: 1.5.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 13/08/2022 Initial coding for RP2040W with CYW43439 WiFi
...
1.3.0 K Hoang 10/10/2022 Fix crash when using AsyncWebSockets server
1.3.1 K Hoang 10/10/2022 Improve robustness of AsyncWebSockets server
1.4.0 K Hoang 20/10/2022 Add LittleFS functions such as AsyncFSWebServer
1.4.1 K Hoang 10/11/2022 Add examples to demo how to use beginChunkedResponse() to send in chunks
1.4.2 K Hoang 28/01/2023 Add Async_AdvancedWebServer_SendChunked_MQTT and AsyncWebServer_MQTT_RP2040W examples
1.5.0 K Hoang 30/01/2023 Fix _catchAllHandler not working bug
*****************************************************************************************************************************/
#if !defined(_RP2040W_AWS_LOGLEVEL_)
#define _RP2040W_AWS_LOGLEVEL_ 1
#endif
#include "AsyncWebServer_RP2040W_Debug.h"
#include "AsyncWebAuthentication_RP2040W.h"
#include <libb64/cencode.h>
// For RP2040W
#include "Crypto/md5.h"
#include "Crypto/bearssl_hash.h"
#include "Crypto/Hash.h"
/////////////////////////////////////////////////
// Basic Auth hash = base64("username:password")
bool checkBasicAuthentication(const char * hash, const char * username, const char * password)
{
if (username == NULL || password == NULL || hash == NULL)
{
AWS_LOGDEBUG("checkBasicAuthentication: Fail: NULL username/password/hash");
return false;
}
size_t toencodeLen = strlen(username) + strlen(password) + 1;
size_t encodedLen = base64_encode_expected_len(toencodeLen);
if (strlen(hash) != encodedLen)
{
AWS_LOGDEBUG3("checkBasicAuthentication: Fail: strlen(hash) = ", strlen(hash), " != encodedLen = ", encodedLen );
return false;
}
char *toencode = new char[toencodeLen + 1];
if (toencode == NULL)
{
AWS_LOGDEBUG("checkBasicAuthentication: NULL toencode");
return false;
}
char *encoded = new char[base64_encode_expected_len(toencodeLen) + 1];
if (encoded == NULL)
{
AWS_LOGDEBUG("checkBasicAuthentication: NULL encoded");
delete[] toencode;
return false;
}
sprintf(toencode, "%s:%s", username, password);
if (base64_encode_chars(toencode, toencodeLen, encoded) > 0 && memcmp(hash, encoded, encodedLen) == 0)
{
AWS_LOGDEBUG("checkBasicAuthentication: OK");
delete[] toencode;
delete[] encoded;
return true;
}
AWS_LOGDEBUG("checkBasicAuthentication: Failed");
delete[] toencode;
delete[] encoded;
return false;
}
/////////////////////////////////////////////////
static bool getMD5(uint8_t * data, uint16_t len, char * output)
{
//33 bytes or more
// For RP2040W
br_md5_context _ctx;
uint8_t i;
uint8_t * _buf = (uint8_t*) malloc(16);
if (_buf == NULL)
{
AWS_LOGDEBUG("getMD5: Can malloc _buf");
return false;
}
memset(_buf, 0x00, 16);
// For RP2040W
br_md5_init(&_ctx);
br_md5_update(&_ctx, data, len);
br_md5_out(&_ctx, _buf);
for (i = 0; i < 16; i++)
{
sprintf(output + (i * 2), "%02x", _buf[i]);
}
free(_buf);
AWS_LOGDEBUG("getMD5: Success");
return true;
}
/////////////////////////////////////////////////
static String genRandomMD5()
{
// For RP2040W
uint32_t r = rand();
char * out = (char*) malloc(33);
if (out == NULL || !getMD5((uint8_t*)(&r), 4, out))
return "";
String res = String(out);
free(out);
AWS_LOGDEBUG1("genRandomMD5: res = ", res);
return res;
}
/////////////////////////////////////////////////
static String stringMD5(const String& in)
{
char * out = (char*) malloc(33);
if (out == NULL || !getMD5((uint8_t*)(in.c_str()), in.length(), out))
return "";
String res = String(out);
free(out);
AWS_LOGDEBUG1("stringMD5: res = ", res);
return res;
}
/////////////////////////////////////////////////
String generateDigestHash(const char * username, const char * password, const char * realm)
{
if (username == NULL || password == NULL || realm == NULL)
{
return "";
}
char * out = (char*) malloc(33);
String res = String(username);
res.concat(":");
res.concat(realm);
res.concat(":");
String in = res;
in.concat(password);
if (out == NULL || !getMD5((uint8_t*)(in.c_str()), in.length(), out))
return "";
res.concat(out);
free(out);
AWS_LOGDEBUG1("generateDigestHash: res = ", res);
return res;
}
/////////////////////////////////////////////////
String requestDigestAuthentication(const char * realm)
{
String header = "realm=\"";
if (realm == NULL)
header.concat("asyncesp");
else
header.concat(realm);
header.concat( "\", qop=\"auth\", nonce=\"");
header.concat(genRandomMD5());
header.concat("\", opaque=\"");
header.concat(genRandomMD5());
header.concat("\"");
AWS_LOGDEBUG1("requestDigestAuthentication: header = ", header);
return header;
}
/////////////////////////////////////////////////
bool checkDigestAuthentication(const char * header, const char * method, const char * username, const char * password,
const char * realm, bool passwordIsHash, const char * nonce, const char * opaque, const char * uri)
{
if (username == NULL || password == NULL || header == NULL || method == NULL)
{
AWS_LOGDEBUG("AUTH FAIL: missing required fields");
return false;
}
String myHeader = String(header);
int nextBreak = myHeader.indexOf(",");
if (nextBreak < 0)
{
AWS_LOGDEBUG("AUTH FAIL: no variables");
return false;
}
String myUsername = String();
String myRealm = String();
String myNonce = String();
String myUri = String();
String myResponse = String();
String myQop = String();
String myNc = String();
String myCnonce = String();
myHeader += ", ";
do
{
String avLine = myHeader.substring(0, nextBreak);
avLine.trim();
myHeader = myHeader.substring(nextBreak + 1);
nextBreak = myHeader.indexOf(",");
int eqSign = avLine.indexOf("=");
if (eqSign < 0)
{
AWS_LOGDEBUG("AUTH FAIL: no = sign");
return false;
}
String varName = avLine.substring(0, eqSign);
avLine = avLine.substring(eqSign + 1);
if (avLine.startsWith("\""))
{
avLine = avLine.substring(1, avLine.length() - 1);
}
if (varName.equals("username"))
{
if (!avLine.equals(username))
{
AWS_LOGDEBUG("AUTH FAIL: username");
return false;
}
myUsername = avLine;
}
else if (varName.equals("realm"))
{
if (realm != NULL && !avLine.equals(realm))
{
AWS_LOGDEBUG("AUTH FAIL: realm");
return false;
}
myRealm = avLine;
}
else if (varName.equals("nonce"))
{
if (nonce != NULL && !avLine.equals(nonce))
{
AWS_LOGDEBUG("AUTH FAIL: nonce");
return false;
}
myNonce = avLine;
}
else if (varName.equals("opaque"))
{
if (opaque != NULL && !avLine.equals(opaque))
{
AWS_LOGDEBUG("AUTH FAIL: opaque");
return false;
}
}
else if (varName.equals("uri"))
{
if (uri != NULL && !avLine.equals(uri))
{
AWS_LOGDEBUG("AUTH FAIL: uri");
return false;
}
myUri = avLine;
}
else if (varName.equals("response"))
{
myResponse = avLine;
}
else if (varName.equals("qop"))
{
myQop = avLine;
}
else if (varName.equals("nc"))
{
myNc = avLine;
}
else if (varName.equals("cnonce"))
{
myCnonce = avLine;
}
} while (nextBreak > 0);
String ha1 = (passwordIsHash) ? String(password) : stringMD5(myUsername + ":" + myRealm + ":" + String(password));
String ha2 = String(method) + ":" + myUri;
String response = ha1 + ":" + myNonce + ":" + myNc + ":" + myCnonce + ":" + myQop + ":" + stringMD5(ha2);
if (myResponse.equals(stringMD5(response)))
{
AWS_LOGDEBUG("AUTH SUCCESS");
return true;
}
AWS_LOGDEBUG("AUTH FAIL: password");
return false;
}