Skip to content

Commit 48814ad

Browse files
committed
Remove dependency on sodium to make it compile with 4.4
1 parent 15f4da7 commit 48814ad

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

libraries/WebServer/examples/HttpBasicAuthSHA1orBearerToken/HttpBasicAuthSHA1orBearerToken.ino

+23-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <ArduinoOTA.h>
44
#include <WebServer.h>
55
#include "mbedtls/sha1.h"
6-
#include "sodium/utils.h"
76

87

98
/// We have two options - we either come in with a bearer
@@ -47,28 +46,26 @@ const char* www_password_hex = "8cb124f8c277c16ec0b2ee00569fd151a08e342b";
4746
const char* www_username_base64 = "base64admin";
4847
const char* www_password_base64 = "jLEk+MJ3wW7Asu4AVp/RUaCONCs=";
4948

49+
static unsigned char _bearer[20];
5050
String * check_bearer_or_auth(HTTPAuthMethod mode, String authReq, String params[]) {
5151
// we expect authReq to be "bearer some-secret"
5252
//
5353
String lcAuthReq = authReq;
54-
lcAuthReq.toLowerCase();
54+
lcAuthReq.toLowerCase();
5555
if (mode == OTHER_AUTH && (lcAuthReq.startsWith("bearer "))) {
5656
String secret = authReq.substring(7);
5757
secret.trim();
5858

5959
uint8_t sha1[20];
6060
mbedtls_sha1((const uint8_t*) secret.c_str(), secret.length(), sha1);
6161

62-
char sha1calc[48]; // large enough for base64 and Hex represenation
63-
sodium_bin2hex(sha1calc, sizeof(sha1calc), sha1, sizeof(sha1));
64-
65-
if (secret_token_hex.equalsConstantTime(sha1calc))
66-
return new String("anything non null");
62+
if (0 == memcpy(_bearer, sha1, sizeof(_bearer)))
63+
return new String("anything non null");
6764
};
6865

69-
// that failed - so do a normal auth
70-
//
71-
return server.authenticateBasicSHA1(www_username_hex, www_password_hex) ?
66+
// that failed - so do a normal auth
67+
//
68+
return server.authenticateBasicSHA1(www_username_hex, www_password_hex) ?
7269
new String(params[0]) : NULL;
7370
};
7471

@@ -83,6 +80,22 @@ void setup() {
8380
}
8481
ArduinoOTA.begin();
8582

83+
84+
// Convert token to a convenient binary representation.
85+
//
86+
if (secret_token_hex.length() != 2 * 20) {
87+
Serial.println("Bearer token does not look like a hex string ?!");
88+
}
89+
90+
#define _H2D(x) (((x)>='0' && ((x) <='9')) ? ((x)-'0') : (((x)>='a' && (x)<='f') ? ((x)-'a') : 0))
91+
#define H2D(x) (_H2D(tolower((x))))
92+
const char * _shaBase64 = secret_token_hex.c_str();
93+
for (int i = 0; i < 20; i++) {
94+
unsigned char c = _shaBase64[2 * i + 0];
95+
unsigned char d = _shaBase64[2 * i + 1];
96+
_bearer[i] = (H2D(c) << 4) | H2D(d);
97+
};
98+
8699
server.on("/", []() {
87100
if (!server.authenticate(&check_bearer_or_auth)) {
88101
Serial.println("No/failed authentication");

libraries/WebServer/src/WebServer.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include "mbedtls/md5.h"
3434
#include "mbedtls/sha1.h"
3535
#include "mbedtls/base64.h"
36-
#include "sodium/utils.h"
3736

3837
static const char AUTHORIZATION_HEADER[] = "Authorization";
3938
static const char qop_auth[] PROGMEM = "qop=auth";
@@ -162,9 +161,16 @@ bool WebServer::authenticateBasicSHA1(const char * _username, const char * _sha1
162161
// or encode the sha we calculated. We pick the latter as encoding of a
163162
// fixed array of 20 bytes s safer than operating on something external.
164163
//
165-
if (strlen(_sha1Base64orHex) == 20 * 2)
166-
sodium_bin2hex(sha1calc, sizeof(sha1calc), sha1, sizeof(sha1));
167-
else
164+
#define _H2D(x) (((x)>='0' && ((x) <='9')) ? ((x)-'0') : (((x)>='a' && (x)<='f') ? ((x)-'a') : 0))
165+
#define H2D(x) (_H2D(tolower((x))))
166+
if (strlen(_sha1Base64orHex) == 20 * 2) {
167+
for(int i = 0; i < 20; i++) {
168+
unsigned char c = _sha1Base64orHex[2*i];
169+
unsigned char d = _sha1Base64orHex[2*i+1];
170+
sha1calc[i] = (H2D(c)<<4) | H2D(d);
171+
};
172+
ret = 0;
173+
} else
168174
ret = mbedtls_base64_encode((uint8_t*)sha1calc, sizeof(sha1calc), &olen, sha1, sizeof(sha1));
169175

170176
return ((username.equalsConstantTime(_username)) &&

0 commit comments

Comments
 (0)