Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Dec 29, 2023. It is now read-only.

Commit 0bc27ef

Browse filesBrowse files
committed
Replace custom base64 encoder with mbedtls base64 encoder
1 parent 4a5e358 commit 0bc27ef
Copy full SHA for 0bc27ef

File tree

Expand file treeCollapse file tree

5 files changed

+27
-78
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+27
-78
lines changed
Open diff view settings
Collapse file

‎src/HTTPConnection.cpp‎

Copy file name to clipboardExpand all lines: src/HTTPConnection.cpp
+22-8Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#include "HTTPConnection.hpp"
2-
#include "Websocket.hpp"
3-
#include <hwcrypto/sha.h>
42

53
namespace httpsserver {
64

7-
85
HTTPConnection::HTTPConnection(ResourceResolver * resResolver):
96
_resResolver(resResolver) {
107
_socket = -1;
@@ -584,12 +581,29 @@ bool HTTPConnection::checkWebsocket() {
584581

585582
std::string HTTPConnection::websocketKeyResponseHash(std::string key) {
586583
std::string newKey = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
587-
uint8_t shaData[20];
584+
uint8_t shaData[HTTPS_SHA1_LENGTH];
588585
esp_sha(SHA1, (uint8_t*)newKey.data(), newKey.length(), shaData);
589-
//GeneralUtils::hexDump(shaData, 20);
590-
std::string retStr;
591-
base64Encode(std::string((char*)shaData, sizeof(shaData)), &retStr);
592-
return retStr;
586+
587+
// Get output size required for base64 representation
588+
size_t b64BufferSize = 0;
589+
mbedtls_base64_encode(nullptr, 0, &b64BufferSize, (const unsigned char*)shaData, HTTPS_SHA1_LENGTH);
590+
591+
// Do the real encoding
592+
unsigned char bufferOut[b64BufferSize];
593+
size_t bytesEncoded = 0;
594+
int res = mbedtls_base64_encode(
595+
bufferOut,
596+
b64BufferSize,
597+
&bytesEncoded,
598+
(const unsigned char*)shaData,
599+
HTTPS_SHA1_LENGTH
600+
);
601+
602+
// Check result and return the encoded string
603+
if (res != 0) {
604+
return std::string();
605+
}
606+
return std::string((char*)bufferOut, bytesEncoded);
593607
} // WebsocketKeyResponseHash
594608

595609
} /* namespace httpsserver */
Collapse file

‎src/HTTPConnection.hpp‎

Copy file name to clipboardExpand all lines: src/HTTPConnection.hpp
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <Arduino.h>
55

66
#include <string>
7+
#include <mbedtls/base64.h>
8+
#include <hwcrypto/sha.h>
79
#include <functional>
810

911
// Required for sockets
Collapse file

‎src/HTTPSServerConstants.hpp‎

Copy file name to clipboardExpand all lines: src/HTTPSServerConstants.hpp
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@
3333
// (time for the client to return notify close flag) - without it, truncation attacks might be possible
3434
#define HTTPS_SHUTDOWN_TIMEOUT 5000
3535

36+
// Length of a SHA1 hash
37+
#define HTTPS_SHA1_LENGTH 20
38+
3639
#endif /* SRC_HTTPSSERVERCONSTANTS_HPP_ */
Collapse file

‎src/util.cpp‎

Copy file name to clipboardExpand all lines: src/util.cpp
-65Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -47,69 +47,4 @@ std::string intToString(int i) {
4747
return std::string(c);
4848
}
4949

50-
static const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
51-
"abcdefghijklmnopqrstuvwxyz"
52-
"0123456789+/";
53-
54-
int base64EncodedLength(size_t length) {
55-
return (length + 2 - ((length + 2) % 3)) / 3 * 4;
56-
} // base64EncodedLength
57-
58-
int base64EncodedLength(const std::string &in) {
59-
return base64EncodedLength(in.length());
60-
} // base64EncodedLength
61-
62-
void a3_to_a4(unsigned char * a4, unsigned char * a3) {
63-
a4[0] = (a3[0] & 0xfc) >> 2;
64-
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
65-
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
66-
a4[3] = (a3[2] & 0x3f);
67-
} // a3_to_a4
68-
69-
/**
70-
* Encode a string into base 64.
71-
*/
72-
bool base64Encode(const std::string &in, std::string *out) {
73-
int i = 0, j = 0;
74-
size_t enc_len = 0;
75-
unsigned char a3[3];
76-
unsigned char a4[4];
77-
78-
out->resize(base64EncodedLength(in));
79-
80-
int input_len = in.size();
81-
std::string::const_iterator input = in.begin();
82-
83-
while (input_len--) {
84-
a3[i++] = *(input++);
85-
if (i == 3) {
86-
a3_to_a4(a4, a3);
87-
88-
for (i = 0; i < 4; i++) {
89-
(*out)[enc_len++] = kBase64Alphabet[a4[i]];
90-
}
91-
92-
i = 0;
93-
}
94-
}
95-
96-
if (i) {
97-
for (j = i; j < 3; j++) {
98-
a3[j] = '\0';
99-
}
100-
101-
a3_to_a4(a4, a3);
102-
103-
for (j = 0; j < i + 1; j++) {
104-
(*out)[enc_len++] = kBase64Alphabet[a4[j]];
105-
}
106-
107-
while ((i++ < 3)) {
108-
(*out)[enc_len++] = '=';
109-
}
110-
}
111-
112-
return (enc_len == out->size());
113-
} // base64Encode
114-
11550
}
Collapse file

‎src/util.hpp‎

Copy file name to clipboardExpand all lines: src/util.hpp
-5Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ int parseInt(std::string s);
1212

1313
std::string intToString(int i);
1414

15-
int base64EncodedLength(size_t length);
16-
int base64EncodedLength(const std::string &in);
17-
void a3_to_a4(unsigned char * a4, unsigned char * a3);
18-
bool base64Encode(const std::string& in, std::string* out);
19-
2015
}
2116

2217
#endif /* SRC_UTIL_HPP_ */

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.