utils.cpp
20.1 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3.
FlashMQ 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>.
*/
#include "sys/stat.h"
#include "utils.h"
#include "sys/time.h"
#include "sys/random.h"
#include <algorithm>
#include <cstdio>
#include <cstring>
#include "openssl/ssl.h"
#include "openssl/err.h"
#include "exceptions.h"
#include "cirbuf.h"
#include "sslctxmanager.h"
#include "logger.h"
#include "evpencodectxmanager.h"
#ifdef __SSE4_2__
#include "threadlocalutils.h"
thread_local SimdUtils simdUtils;
#endif
std::list<std::string> split(const std::string &input, const char sep, size_t max, bool keep_empty_parts)
{
std::list<std::string> list;
size_t start = 0;
size_t end;
while (list.size() < max && (end = input.find(sep, start)) != std::string::npos)
{
if (start != end || keep_empty_parts)
list.push_back(input.substr(start, end - start));
start = end + 1; // increase by length of seperator.
}
if (start != input.size() || keep_empty_parts)
list.push_back(input.substr(start, std::string::npos));
return list;
}
thread_local std::vector<std::string> subscribeParts;
thread_local std::vector<std::string> publishParts;
bool topicsMatch(const std::string &subscribeTopic, const std::string &publishTopic)
{
if (subscribeTopic.find("+") == std::string::npos && subscribeTopic.find("#") == std::string::npos)
return subscribeTopic == publishTopic;
if (!subscribeTopic.empty() && !publishTopic.empty() && publishTopic[0] == '$' && subscribeTopic[0] != '$')
return false;
subscribeParts.clear();
publishParts.clear();
#ifdef __SSE4_2__
simdUtils.splitTopic(subscribeTopic, subscribeParts);
simdUtils.splitTopic(publishTopic, publishParts);
#else
splitToVector(subscribeTopic, subscribeParts, '/');
splitToVector(publishTopic, publishParts, '/');
#endif
auto subscribe_itr = subscribeParts.begin();
auto publish_itr = publishParts.begin();
bool result = true;
while (subscribe_itr != subscribeParts.end() && publish_itr != publishParts.end())
{
const std::string &subscribe_subtopic = *subscribe_itr++;
const std::string &publish_subtopic = *publish_itr++;
if (subscribe_subtopic == "+")
continue;
if (subscribe_subtopic == "#")
return true;
if (subscribe_subtopic != publish_subtopic)
return false;
}
result = subscribe_itr == subscribeParts.end() && publish_itr == publishParts.end();
return result;
}
bool isValidUtf8Generic(const std::string &s, bool alsoCheckInvalidPublishChars)
{
int multibyte_remain = 0;
int cur_code_point = 0;
for(const char x : s)
{
if (alsoCheckInvalidPublishChars && (x == '#' || x == '+'))
return false;
if(!multibyte_remain)
{
cur_code_point = 0;
if ((x & 0b10000000) == 0) // when the MSB is 0, it's ASCII, most common case
{
cur_code_point += (x & 0b01111111);
}
else if((x & 0b11100000) == 0b11000000) // 2 byte char
{
multibyte_remain = 1;
cur_code_point += ((x & 0b00011111) << 6);
}
else if((x & 0b11110000) == 0b11100000) // 3 byte char
{
multibyte_remain = 2;
cur_code_point += ((x & 0b00001111) << 12);
}
else if((x & 0b11111000) == 0b11110000) // 4 byte char
{
multibyte_remain = 3;
cur_code_point += ((x & 0b00000111) << 18);
}
else if((x & 0b10000000) != 0)
return false;
else
cur_code_point += (x & 0b01111111);
}
else // All remainer bytes of this code point needs to start with 10
{
if((x & 0b11000000) != 0b10000000)
return false;
multibyte_remain--;
cur_code_point += ((x & 0b00111111) << (6*multibyte_remain));
}
if (multibyte_remain == 0)
{
// Invalid range for MQTT. [MQTT-1.5.3-1]
if (cur_code_point >= 0xD800 && cur_code_point <= 0xDFFF) // Dec 55296-57343
return false;
if (cur_code_point <= 0x001F)
return false;
if (cur_code_point >= 0x007F && cur_code_point <= 0x009F)
return false;
if (cur_code_point == 0xFFFF)
return false;
cur_code_point = 0;
}
}
return multibyte_remain == 0;
}
bool isValidUtf8(const std::string &s, bool alsoCheckInvalidPublishChars)
{
#ifdef __SSE4_2__
return simdUtils.isValidUtf8(s, alsoCheckInvalidPublishChars);
#else
return isValidUtf8Generic(s, alsoCheckInvalidPublishChars);
#endif
}
bool strContains(const std::string &s, const std::string &needle)
{
return s.find(needle) != std::string::npos;
}
bool isValidPublishPath(const std::string &s)
{
if (s.empty())
return false;
for (const char c : s)
{
if (c == '#' || c == '+')
return false;
}
return true;
}
bool isValidSubscribePath(const std::string &s)
{
bool plusAllowed = true;
bool nextMustBeSlash = false;
bool poundSeen = false;
for (const char c : s)
{
if (!plusAllowed && c == '+')
return false;
if (nextMustBeSlash && c != '/')
return false;
if (poundSeen)
return false;
plusAllowed = c == '/';
nextMustBeSlash = c == '+';
poundSeen = c == '#';
}
return true;
}
bool containsDangerousCharacters(const std::string &s)
{
if (s.empty())
return false;
for (const char c : s)
{
switch(c)
{
case '#':
return true;
case '+':
return true;
}
}
return false;
}
void splitTopic(const std::string &topic, std::vector<std::string> &output)
{
#ifdef __SSE4_2__
simdUtils.splitTopic(topic, output);
#else
splitToVector(topic, output, '/');
#endif
}
void splitToVector(const std::string &input, std::vector<std::string> &output, const char sep, size_t max, bool keep_empty_parts)
{
const auto subtopic_count = std::count(input.begin(), input.end(), '/') + 1;
output.reserve(subtopic_count);
size_t start = 0;
size_t end;
const auto npos = std::string::npos;
while (output.size() < max && (end = input.find(sep, start)) != npos)
{
if (start != end || keep_empty_parts)
output.push_back(input.substr(start, end - start));
start = end + 1; // increase by length of seperator.
}
if (start != input.size() || keep_empty_parts)
output.push_back(input.substr(start, npos));
}
const std::vector<std::string> splitToVector(const std::string &input, const char sep, size_t max, bool keep_empty_parts)
{
std::vector<std::string> result;
splitToVector(input, result, sep, max, keep_empty_parts);
return result;
}
void ltrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
}
void rtrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), s.end());
}
void trim(std::string &s)
{
ltrim(s);
rtrim(s);
}
std::string &rtrim(std::string &s, unsigned char c)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [=](unsigned char ch) {
return (c != ch);
}).base(), s.end());
return s;
}
bool startsWith(const std::string &s, const std::string &needle)
{
return s.find(needle) == 0;
}
std::string getSecureRandomString(const ssize_t len)
{
std::vector<unsigned char> buf(len);
ssize_t actual_len = getrandom(buf.data(), len, 0);
if (actual_len < 0 || actual_len != len)
{
throw std::runtime_error("Error requesting random data");
}
const std::string possibleCharacters("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrtsuvwxyz1234567890");
const int possibleCharactersCount = possibleCharacters.length();
std::string randomString;
for(const unsigned char c : buf)
{
unsigned int index = c % possibleCharactersCount;
char nextChar = possibleCharacters.at(index);
randomString.push_back(nextChar);
}
return randomString;
}
std::string str_tolower(std::string s)
{
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c){ return std::tolower(c); });
return s;
}
bool stringTruthiness(const std::string &val)
{
std::string val_ = str_tolower(val);
trim(val_);
if (val_ == "yes" || val_ == "true" || val_ == "on")
return true;
if (val_ == "no" || val_ == "false" || val_ == "off")
return false;
throw ConfigFileException("Value '" + val + "' can't be converted to boolean");
}
bool isPowerOfTwo(int n)
{
return (n != 0) && (n & (n - 1)) == 0;
}
bool parseHttpHeader(CirBuf &buf, std::string &websocket_key, int &websocket_version, std::string &subprotocol)
{
const std::string s(buf.tailPtr(), buf.usedBytes());
std::istringstream is(s);
bool doubleEmptyLine = false; // meaning, the HTTP header is complete
bool upgradeHeaderSeen = false;
bool connectionHeaderSeen = false;
bool firstLine = true;
bool subprotocol_seen = false;
std::string line;
while (std::getline(is, line))
{
trim(line);
if (firstLine)
{
firstLine = false;
if (!startsWith(line, "GET"))
throw BadHttpRequest("Websocket request should start with GET.");
continue;
}
if (line.empty())
{
doubleEmptyLine = true;
break;
}
std::list<std::string> fields = split(line, ':', 1);
if (fields.size() != 2)
{
throw BadHttpRequest("This does not look like a HTTP request.");
}
const std::vector<std::string> fields2(fields.begin(), fields.end());
std::string name = str_tolower(fields2[0]);
trim(name);
std::string value = fields2[1];
trim(value);
std::string value_lower = str_tolower(value);
if (name == "upgrade" && value_lower == "websocket")
upgradeHeaderSeen = true;
else if (name == "connection" && strContains(value_lower, "upgrade"))
connectionHeaderSeen = true;
else if (name == "sec-websocket-key")
websocket_key = value;
else if (name == "sec-websocket-version")
websocket_version = stoi(value);
else if (name == "sec-websocket-protocol" && strContains(value_lower, "mqtt"))
{
subprotocol = value;
subprotocol_seen = true;
}
}
if (doubleEmptyLine)
{
if (!connectionHeaderSeen || !upgradeHeaderSeen)
throw BadHttpRequest("HTTP request is not a websocket upgrade request.");
if (!subprotocol_seen)
throw BadHttpRequest("HTTP header Sec-WebSocket-Protocol with value 'mqtt' must be present.");
}
return doubleEmptyLine;
}
std::vector<char> base64Decode(const std::string &s)
{
if (s.length() % 4 != 0)
throw std::runtime_error("Decoding invalid base64 string");
if (s.empty())
throw std::runtime_error("Trying to base64 decode an empty string.");
std::vector<char> tmp(s.size());
int outl = 0;
int outl_total = 0;
EvpEncodeCtxManager b64_ctx;
if (EVP_DecodeUpdate(b64_ctx.ctx, reinterpret_cast<unsigned char*>(tmp.data()), &outl, reinterpret_cast<const unsigned char*>(s.c_str()), s.size()) < 0)
throw std::runtime_error("Failure in EVP_DecodeUpdate()");
outl_total += outl;
if (EVP_DecodeFinal(b64_ctx.ctx, reinterpret_cast<unsigned char*>(tmp[outl_total]), &outl) < 0)
throw std::runtime_error("Failure in EVP_DecodeFinal()");
std::vector<char> result(outl_total);
std::memcpy(result.data(), tmp.data(), outl_total);
return result;
}
std::string base64Encode(const unsigned char *input, const int length)
{
const int pl = 4*((length+2)/3);
char *output = reinterpret_cast<char *>(calloc(pl+1, 1));
const int ol = EVP_EncodeBlock(reinterpret_cast<unsigned char *>(output), input, length);
std::string result(output);
free(output);
if (pl != ol)
throw std::runtime_error("Base64 encode error.");
return result;
}
std::string generateWebsocketAcceptString(const std::string &websocketKey)
{
unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len;
EVP_MD_CTX *mdctx = EVP_MD_CTX_new();;
const EVP_MD *md = EVP_sha1();
EVP_DigestInit_ex(mdctx, md, NULL);
const std::string keyPlusMagic = websocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
EVP_DigestUpdate(mdctx, keyPlusMagic.c_str(), keyPlusMagic.length());
EVP_DigestFinal_ex(mdctx, md_value, &md_len);
EVP_MD_CTX_free(mdctx);
std::string base64 = base64Encode(md_value, md_len);
return base64;
}
std::string generateInvalidWebsocketVersionHttpHeaders(const int wantedVersion)
{
std::ostringstream oss;
oss << "HTTP/1.1 400 Bad Request\r\n";
oss << "Sec-WebSocket-Version: " << wantedVersion;
oss << "\r\n";
oss.flush();
return oss.str();
}
std::string generateBadHttpRequestReponse(const std::string &msg)
{
std::ostringstream oss;
oss << "HTTP/1.1 400 Bad Request\r\n";
oss << "\r\n";
oss << msg;
oss.flush();
return oss.str();
}
std::string generateWebsocketAnswer(const std::string &acceptString, const std::string &subprotocol)
{
std::ostringstream oss;
oss << "HTTP/1.1 101 Switching Protocols\r\n";
oss << "Upgrade: websocket\r\n";
oss << "Connection: Upgrade\r\n";
oss << "Sec-WebSocket-Accept: " << acceptString << "\r\n";
oss << "Sec-WebSocket-Protocol: " << subprotocol << "\r\n";
oss << "\r\n";
oss.flush();
return oss.str();
}
// Using a separate ssl context to test, because it's the easiest way to load certs and key atomitcally.
void testSsl(const std::string &fullchain, const std::string &privkey)
{
if (fullchain.empty() && privkey.empty())
throw ConfigFileException("No privkey and fullchain specified.");
if (fullchain.empty())
throw ConfigFileException("No private key specified for fullchain");
if (privkey.empty())
throw ConfigFileException("No fullchain specified for private key");
if (getFileSize(fullchain) == 0)
throw ConfigFileException(formatString("SSL 'fullchain' file '%s' is empty or invalid", fullchain.c_str()));
if (getFileSize(privkey) == 0)
throw ConfigFileException(formatString("SSL 'privkey' file '%s' is empty or invalid", privkey.c_str()));
SslCtxManager sslCtx;
if (SSL_CTX_use_certificate_file(sslCtx.get(), fullchain.c_str(), SSL_FILETYPE_PEM) != 1)
{
ERR_print_errors_cb(logSslError, NULL);
throw ConfigFileException("Error loading full chain " + fullchain);
}
if (SSL_CTX_use_PrivateKey_file(sslCtx.get(), privkey.c_str(), SSL_FILETYPE_PEM) != 1)
{
ERR_print_errors_cb(logSslError, NULL);
throw ConfigFileException("Error loading private key " + privkey);
}
if (SSL_CTX_check_private_key(sslCtx.get()) != 1)
{
ERR_print_errors_cb(logSslError, NULL);
throw ConfigFileException("Private key and certificate don't match.");
}
}
std::string formatString(const std::string str, ...)
{
char buf[512];
va_list valist;
va_start(valist, str);
vsnprintf(buf, 512, str.c_str(), valist);
va_end(valist);
size_t len = strlen(buf);
std::string result(buf, len);
return result;
}
std::string dirnameOf(const std::string& path)
{
size_t pos = path.find_last_of("\\/");
return (std::string::npos == pos) ? "" : path.substr(0, pos);
}
BindAddr getBindAddr(int family, const std::string &bindAddress, int port)
{
BindAddr result;
if (family == AF_INET)
{
struct sockaddr_in *in_addr_v4 = new sockaddr_in();
result.len = sizeof(struct sockaddr_in);
memset(in_addr_v4, 0, result.len);
if (bindAddress.empty())
in_addr_v4->sin_addr.s_addr = INADDR_ANY;
else
inet_pton(AF_INET, bindAddress.c_str(), &in_addr_v4->sin_addr);
in_addr_v4->sin_family = AF_INET;
in_addr_v4->sin_port = htons(port);
result.p.reset(reinterpret_cast<sockaddr*>(in_addr_v4));
}
if (family == AF_INET6)
{
struct sockaddr_in6 *in_addr_v6 = new sockaddr_in6();
result.len = sizeof(struct sockaddr_in6);
memset(in_addr_v6, 0, result.len);
if (bindAddress.empty())
in_addr_v6->sin6_addr = IN6ADDR_ANY_INIT;
else
inet_pton(AF_INET6, bindAddress.c_str(), &in_addr_v6->sin6_addr);
in_addr_v6->sin6_family = AF_INET6;
in_addr_v6->sin6_port = htons(port);
result.p.reset(reinterpret_cast<sockaddr*>(in_addr_v6));
}
return result;
}
ssize_t getFileSize(const std::string &path)
{
struct stat statbuf;
memset(&statbuf, 0, sizeof(struct stat));
if (stat(path.c_str(), &statbuf) < 0)
return -1;
return statbuf.st_size;
}
std::string sockaddrToString(sockaddr *addr)
{
if (!addr)
return "[unknown address]";
char buf[INET6_ADDRSTRLEN];
void *addr_in = nullptr;
if (addr->sa_family == AF_INET)
{
struct sockaddr_in *ipv4sockAddr = reinterpret_cast<struct sockaddr_in*>(addr);
addr_in = &ipv4sockAddr->sin_addr;
}
else if (addr->sa_family == AF_INET6)
{
struct sockaddr_in6 *ipv6sockAddr = reinterpret_cast<struct sockaddr_in6*>(addr);
addr_in = &ipv6sockAddr->sin6_addr;
}
if (addr_in)
{
const char *rc = inet_ntop(addr->sa_family, addr_in, buf, INET6_ADDRSTRLEN);
if (rc)
{
std::string remote_addr(rc);
return remote_addr;
}
}
return "[unknown address]";
}
const std::string websocketCloseCodeToString(uint16_t code)
{
switch (code) {
case 1000:
return "Normal websocket close";
case 1001:
return "Browser navigating away from page";
default:
return formatString("Websocket status code %d", code);
}
}
const std::string protocolVersionString(ProtocolVersion p)
{
switch (p)
{
case ProtocolVersion::None:
return "none";
case ProtocolVersion::Mqtt31:
return "3.1";
case ProtocolVersion::Mqtt311:
return "3.1.1";
case ProtocolVersion::Mqtt5:
return "5.0";
default:
return "unknown";
}
}
uint32_t ageFromTimePoint(const std::chrono::time_point<std::chrono::steady_clock> &point)
{
auto duration = std::chrono::steady_clock::now() - point;
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
return seconds.count();
}
std::chrono::time_point<std::chrono::steady_clock> timepointFromAge(const uint32_t age)
{
std::chrono::seconds seconds(age);
std::chrono::time_point<std::chrono::steady_clock> newPoint = std::chrono::steady_clock::now() + seconds;
return newPoint;
}
ReasonCodes authResultToReasonCode(AuthResult authResult)
{
switch (authResult)
{
case AuthResult::success:
return ReasonCodes::Success;
case AuthResult::auth_method_not_supported:
return ReasonCodes::BadAuthenticationMethod;
case AuthResult::acl_denied:
case AuthResult::login_denied:
return ReasonCodes::NotAuthorized;
case AuthResult::error:
return ReasonCodes::UnspecifiedError;
case AuthResult::auth_continue:
return ReasonCodes::ContinueAuthentication;
default:
return ReasonCodes::UnspecifiedError;
}
}