forked from RaveTek/Eaglercraft-1.20-Websocket
-
Notifications
You must be signed in to change notification settings - Fork 4
/
websocket_auth.c
309 lines (260 loc) · 9.3 KB
/
websocket_auth.c
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
#define TRACE_LEVEL WEB_SOCKET_TRACE_LEVEL
#include <stdlib.h>
#include "core/net.h"
#include "web_socket/websocket.h"
#include "web_socket/websocket_auth.h"
#include "encoding/base64.h"
#include "hash/md5.h"
#include "str.h"
#include "debug.h"
//Check TCP/IP stack configuration
#if (WEB_SOCKET_SUPPORT == ENABLED)
/**
* @brief Parse WWW-Authenticate header field
* @param[in] webSocket Handle to a WebSocket
* @param[in] value NULL-terminated string that contains the value of header field
* @return Error code
**/
error_t webSocketParseAuthenticateField(WebSocket *webSocket, char_t *value)
{
#if (WEB_SOCKET_BASIC_AUTH_SUPPORT == ENABLED || WEB_SOCKET_DIGEST_AUTH_SUPPORT == ENABLED)
size_t n;
char_t *p;
char_t *token;
char_t *separator;
char_t *name;
WebSocketAuthContext *authContext;
//Point to the handshake context
authContext = &webSocket->authContext;
//Retrieve the authentication scheme
token = osStrtok_r(value, " \t", &p);
//Any parsing error?
if(token == NULL)
return ERROR_INVALID_SYNTAX;
//Basic access authentication?
if(!osStrcasecmp(token, "Basic"))
{
//Basic authentication is required by the WebSocket server
authContext->requiredAuthMode = WS_AUTH_MODE_BASIC;
}
//Digest access authentication?
else if(!osStrcasecmp(token, "Digest"))
{
//Digest authentication is required by the WebSocket server
authContext->requiredAuthMode = WS_AUTH_MODE_DIGEST;
}
//Unknown authentication scheme?
else
{
//Report an error
return ERROR_INVALID_SYNTAX;
}
//Get the first parameter
token = osStrtok_r(NULL, ",", &p);
//Parse the WWW-Authenticate field
while(token != NULL)
{
//Check whether a separator is present
separator = osStrchr(token, '=');
//Separator found?
if(separator != NULL)
{
//Split the string
*separator = '\0';
//Get field name and value
name = strTrimWhitespace(token);
value = strTrimWhitespace(separator + 1);
//Retrieve the length of the value field
n = osStrlen(value);
//Discard the surrounding quotes
if(n > 0 && value[n - 1] == '\"')
value[n - 1] = '\0';
if(value[0] == '\"')
value++;
//Check parameter name
if(!osStrcasecmp(name, "realm"))
{
//Save realm
strSafeCopy(authContext->realm, value, WEB_SOCKET_REALM_MAX_LEN);
}
#if (WEB_SOCKET_DIGEST_AUTH_SUPPORT == ENABLED)
else if(!osStrcasecmp(name, "nonce"))
{
//Save nonce
strSafeCopy(authContext->nonce, value, WEB_SOCKET_NONCE_MAX_LEN + 1);
}
else if(!osStrcasecmp(name, "opaque"))
{
//Save nonce
strSafeCopy(authContext->opaque, value, WEB_SOCKET_OPAQUE_MAX_LEN + 1);
}
else if(!osStrcasecmp(name, "stale"))
{
//Save stale flag
if(!osStrcasecmp(value, "true"))
{
authContext->stale = TRUE;
}
else
{
authContext->stale = FALSE;
}
}
#endif
//Get next parameter
token = osStrtok_r(NULL, ",", &p);
}
}
#endif
//Successful processing
return NO_ERROR;
}
/**
* @brief Format Authorization header field
* @param[in] webSocket Handle to a WebSocket
* @param[out] output Buffer where to format the header field
* @return Total length of the header field
**/
size_t webSocketAddAuthorizationField(WebSocket *webSocket, char_t *output)
{
size_t n;
#if (WEB_SOCKET_BASIC_AUTH_SUPPORT == ENABLED || WEB_SOCKET_DIGEST_AUTH_SUPPORT == ENABLED)
WebSocketAuthContext *authContext;
//Point to the handshake context
authContext = &webSocket->authContext;
#endif
#if (WEB_SOCKET_BASIC_AUTH_SUPPORT == ENABLED)
//Basic authentication scheme?
if(authContext->selectedAuthMode == WS_AUTH_MODE_BASIC)
{
size_t k;
char_t *temp;
//Temporary buffer
temp = (char_t *) webSocket->rxContext.buffer;
//Format Authorization header field
n = osSprintf(output, "Authorization: Basic ");
//The client sends the userid and password, separated by a single colon
//character, within a Base64-encoded string in the credentials
k = osSprintf(temp, "%s:%s", authContext->username,
authContext->password);
//Encode the resulting string using Base64
base64Encode(temp, k, output + n, &k);
//Update the total length of the header field
n += k;
//Properly terminate the Authorization header field
n += osSprintf(output + n, "\r\n");
}
else
#endif
#if (WEB_SOCKET_DIGEST_AUTH_SUPPORT == ENABLED)
//Digest authentication scheme?
if(authContext->selectedAuthMode == WS_AUTH_MODE_DIGEST)
{
Md5Context md5Context;
char_t ha1[2 * MD5_DIGEST_SIZE + 1];
char_t ha2[2 * MD5_DIGEST_SIZE + 1];
char_t nc[9];
//Count of the number of requests (including the current request)
//that the client has sent with the nonce value in this request
authContext->nc++;
//Convert the value to hex string
osSprintf(nc, "%08x", authContext->nc);
//Compute HA1 = MD5(username : realm : password)
md5Init(&md5Context);
md5Update(&md5Context, authContext->username, osStrlen(authContext->username));
md5Update(&md5Context, ":", 1);
md5Update(&md5Context, authContext->realm, osStrlen(authContext->realm));
md5Update(&md5Context, ":", 1);
md5Update(&md5Context, authContext->password, osStrlen(authContext->password));
md5Final(&md5Context, NULL);
//Convert MD5 hash to hex string
webSocketConvertArrayToHexString(md5Context.digest, MD5_DIGEST_SIZE, ha1);
//Debug message
TRACE_DEBUG(" HA1: %s\r\n", ha1);
//Compute HA2 = MD5(method : uri)
md5Init(&md5Context);
md5Update(&md5Context, "GET", 3);
md5Update(&md5Context, ":", 1);
md5Update(&md5Context, webSocket->uri, osStrlen(webSocket->uri));
md5Final(&md5Context, NULL);
//Convert MD5 hash to hex string
webSocketConvertArrayToHexString(md5Context.digest, MD5_DIGEST_SIZE, ha2);
//Debug message
TRACE_DEBUG(" HA2: %s\r\n", ha2);
//Compute MD5(HA1 : nonce : nc : cnonce : qop : HA2)
md5Init(&md5Context);
md5Update(&md5Context, ha1, osStrlen(ha1));
md5Update(&md5Context, ":", 1);
md5Update(&md5Context, authContext->nonce, osStrlen(authContext->nonce));
md5Update(&md5Context, ":", 1);
md5Update(&md5Context, nc, osStrlen(nc));
md5Update(&md5Context, ":", 1);
md5Update(&md5Context, authContext->cnonce, osStrlen(authContext->cnonce));
md5Update(&md5Context, ":", 1);
md5Update(&md5Context, "auth", 4);
md5Update(&md5Context, ":", 1);
md5Update(&md5Context, ha2, osStrlen(ha2));
md5Final(&md5Context, NULL);
//Convert MD5 hash to hex string
webSocketConvertArrayToHexString(md5Context.digest, MD5_DIGEST_SIZE, ha1);
//Debug message
TRACE_DEBUG(" response: %s\r\n", ha1);
//Format Authorization header field
n = osSprintf(output, "Authorization: Digest ");
//Username
n += osSprintf(output + n, "username=\"%s\", ", authContext->username);
//Realm
n += osSprintf(output + n, "realm=\"%s\", ", authContext->realm);
//Nonce value
n += osSprintf(output + n, "nonce=\"%s\", ", authContext->nonce);
//URI
n += osSprintf(output + n, "uri=\"%s\", ", webSocket->uri);
//Quality of protection
n += osSprintf(output + n, "qop=\"auth\", ");
//Nonce count
n += osSprintf(output + n, "nc=\"%08x\", ", authContext->nc);
//Cnonce value
n += osSprintf(output + n, "cnonce=\"%s\", ", authContext->cnonce);
//Response
n += osSprintf(output + n, "response=\"%s\", ", ha1);
//Opaque parameter
n += osSprintf(output + n, "opaque=\"%s\"\r\n", authContext->opaque);
}
else
#endif
//Unknown authentication scheme?
{
//No need to add the Authorization header field
n = 0;
}
//Return the total length of the Authorization header field
return n;
}
/**
* @brief Convert byte array to hex string
* @param[in] input Point to the byte array
* @param[in] inputLen Length of the byte array
* @param[out] output NULL-terminated string resulting from the conversion
**/
void webSocketConvertArrayToHexString(const uint8_t *input,
size_t inputLen, char_t *output)
{
size_t i;
//Hex conversion table
static const char_t hexDigit[16] =
{
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
//Process byte array
for(i = 0; i < inputLen; i++)
{
//Convert upper nibble
output[i * 2] = hexDigit[(input[i] >> 4) & 0x0F];
//Then convert lower nibble
output[i * 2 + 1] = hexDigit[input[i] & 0x0F];
}
//Properly terminate the string with a NULL character
output[i * 2] = '\0';
}
#endif