|
| 1 | +/* |
| 2 | + Asynchronous WebServer library for Espressif MCUs |
| 3 | +
|
| 4 | + Copyright (c) 2016 Hristo Gochkov. All rights reserved. |
| 5 | + This file is part of the esp8266 core for Arduino environment. |
| 6 | +
|
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +*/ |
| 21 | +#include "WebAuthentication.h" |
| 22 | +#include <libb64/cencode.h> |
| 23 | +#ifdef ESP32 |
| 24 | +#include "mbedtls/md5.h" |
| 25 | +#include "mbedtls/version.h" |
| 26 | +#else |
| 27 | +#include "md5.h" |
| 28 | +#endif |
| 29 | + |
| 30 | + |
| 31 | +// Basic Auth hash = base64("username:password") |
| 32 | + |
| 33 | +bool checkBasicAuthentication(const char * hash, const char * username, const char * password){ |
| 34 | + if(username == NULL || password == NULL || hash == NULL) |
| 35 | + return false; |
| 36 | + |
| 37 | + size_t toencodeLen = strlen(username)+strlen(password)+1; |
| 38 | + size_t encodedLen = base64_encode_expected_len(toencodeLen); |
| 39 | + if(strlen(hash) != encodedLen) |
| 40 | + return false; |
| 41 | + |
| 42 | + char *toencode = new char[toencodeLen+1]; |
| 43 | + if(toencode == NULL){ |
| 44 | + return false; |
| 45 | + } |
| 46 | + char *encoded = new char[base64_encode_expected_len(toencodeLen)+1]; |
| 47 | + if(encoded == NULL){ |
| 48 | + delete[] toencode; |
| 49 | + return false; |
| 50 | + } |
| 51 | + sprintf(toencode, "%s:%s", username, password); |
| 52 | + if(base64_encode_chars(toencode, toencodeLen, encoded) > 0 && memcmp(hash, encoded, encodedLen) == 0){ |
| 53 | + delete[] toencode; |
| 54 | + delete[] encoded; |
| 55 | + return true; |
| 56 | + } |
| 57 | + delete[] toencode; |
| 58 | + delete[] encoded; |
| 59 | + return false; |
| 60 | +} |
| 61 | + |
| 62 | +static bool getMD5(uint8_t * data, uint16_t len, char * output){//33 bytes or more |
| 63 | +#ifdef ESP32 |
| 64 | + mbedtls_md5_context _ctx; |
| 65 | +#else |
| 66 | + md5_context_t _ctx; |
| 67 | +#endif |
| 68 | + uint8_t i; |
| 69 | + uint8_t * _buf = (uint8_t*)malloc(16); |
| 70 | + if(_buf == NULL) |
| 71 | + return false; |
| 72 | + memset(_buf, 0x00, 16); |
| 73 | +#ifdef ESP32 |
| 74 | + mbedtls_md5_init(&_ctx); |
| 75 | + |
| 76 | + // KH |
| 77 | + #if (MBEDTLS_VERSION_NUMBER < 0x02070000) |
| 78 | + #warning MBEDTLS_VERSION_NUMBER < 2.7.0 |
| 79 | + // Superseded from v2.7.0 |
| 80 | + mbedtls_md5_starts(&_ctx); |
| 81 | + mbedtls_md5_update(&_ctx, data, len); |
| 82 | + mbedtls_md5_finish(&_ctx, _buf); |
| 83 | + #else |
| 84 | + #warning MBEDTLS_VERSION_NUMBER >= 2.7.0 |
| 85 | + mbedtls_md5_starts_ret(&_ctx); |
| 86 | + mbedtls_md5_update_ret(&_ctx, data, len); |
| 87 | + mbedtls_md5_finish_ret(&_ctx, _buf); |
| 88 | + #endif |
| 89 | + ////// |
| 90 | + |
| 91 | +#else |
| 92 | + MD5Init(&_ctx); |
| 93 | + MD5Update(&_ctx, data, len); |
| 94 | + MD5Final(_buf, &_ctx); |
| 95 | +#endif |
| 96 | + for(i = 0; i < 16; i++) { |
| 97 | + sprintf(output + (i * 2), "%02x", _buf[i]); |
| 98 | + } |
| 99 | + free(_buf); |
| 100 | + return true; |
| 101 | +} |
| 102 | + |
| 103 | +static String genRandomMD5(){ |
| 104 | +#ifdef ESP8266 |
| 105 | + uint32_t r = RANDOM_REG32; |
| 106 | +#else |
| 107 | + uint32_t r = rand(); |
| 108 | +#endif |
| 109 | + char * out = (char*)malloc(33); |
| 110 | + if(out == NULL || !getMD5((uint8_t*)(&r), 4, out)) |
| 111 | + return ""; |
| 112 | + String res = String(out); |
| 113 | + free(out); |
| 114 | + return res; |
| 115 | +} |
| 116 | + |
| 117 | +static String stringMD5(const String& in){ |
| 118 | + char * out = (char*)malloc(33); |
| 119 | + if(out == NULL || !getMD5((uint8_t*)(in.c_str()), in.length(), out)) |
| 120 | + return ""; |
| 121 | + String res = String(out); |
| 122 | + free(out); |
| 123 | + return res; |
| 124 | +} |
| 125 | + |
| 126 | +String generateDigestHash(const char * username, const char * password, const char * realm){ |
| 127 | + if(username == NULL || password == NULL || realm == NULL){ |
| 128 | + return ""; |
| 129 | + } |
| 130 | + char * out = (char*)malloc(33); |
| 131 | + String res = String(username); |
| 132 | + res.concat(":"); |
| 133 | + res.concat(realm); |
| 134 | + res.concat(":"); |
| 135 | + String in = res; |
| 136 | + in.concat(password); |
| 137 | + if(out == NULL || !getMD5((uint8_t*)(in.c_str()), in.length(), out)) |
| 138 | + return ""; |
| 139 | + res.concat(out); |
| 140 | + free(out); |
| 141 | + return res; |
| 142 | +} |
| 143 | + |
| 144 | +String requestDigestAuthentication(const char * realm){ |
| 145 | + String header = "realm=\""; |
| 146 | + if(realm == NULL) |
| 147 | + header.concat("asyncesp"); |
| 148 | + else |
| 149 | + header.concat(realm); |
| 150 | + header.concat( "\", qop=\"auth\", nonce=\""); |
| 151 | + header.concat(genRandomMD5()); |
| 152 | + header.concat("\", opaque=\""); |
| 153 | + header.concat(genRandomMD5()); |
| 154 | + header.concat("\""); |
| 155 | + return header; |
| 156 | +} |
| 157 | + |
| 158 | +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){ |
| 159 | + if(username == NULL || password == NULL || header == NULL || method == NULL){ |
| 160 | + //os_printf("AUTH FAIL: missing required fields\n"); |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + String myHeader = String(header); |
| 165 | + int nextBreak = myHeader.indexOf(","); |
| 166 | + if(nextBreak < 0){ |
| 167 | + //os_printf("AUTH FAIL: no variables\n"); |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + String myUsername = String(); |
| 172 | + String myRealm = String(); |
| 173 | + String myNonce = String(); |
| 174 | + String myUri = String(); |
| 175 | + String myResponse = String(); |
| 176 | + String myQop = String(); |
| 177 | + String myNc = String(); |
| 178 | + String myCnonce = String(); |
| 179 | + |
| 180 | + myHeader += ", "; |
| 181 | + do { |
| 182 | + String avLine = myHeader.substring(0, nextBreak); |
| 183 | + avLine.trim(); |
| 184 | + myHeader = myHeader.substring(nextBreak+1); |
| 185 | + nextBreak = myHeader.indexOf(","); |
| 186 | + |
| 187 | + int eqSign = avLine.indexOf("="); |
| 188 | + if(eqSign < 0){ |
| 189 | + //os_printf("AUTH FAIL: no = sign\n"); |
| 190 | + return false; |
| 191 | + } |
| 192 | + String varName = avLine.substring(0, eqSign); |
| 193 | + avLine = avLine.substring(eqSign + 1); |
| 194 | + if(avLine.startsWith("\"")){ |
| 195 | + avLine = avLine.substring(1, avLine.length() - 1); |
| 196 | + } |
| 197 | + |
| 198 | + if(varName.equals("username")){ |
| 199 | + if(!avLine.equals(username)){ |
| 200 | + //os_printf("AUTH FAIL: username\n"); |
| 201 | + return false; |
| 202 | + } |
| 203 | + myUsername = avLine; |
| 204 | + } else if(varName.equals("realm")){ |
| 205 | + if(realm != NULL && !avLine.equals(realm)){ |
| 206 | + //os_printf("AUTH FAIL: realm\n"); |
| 207 | + return false; |
| 208 | + } |
| 209 | + myRealm = avLine; |
| 210 | + } else if(varName.equals("nonce")){ |
| 211 | + if(nonce != NULL && !avLine.equals(nonce)){ |
| 212 | + //os_printf("AUTH FAIL: nonce\n"); |
| 213 | + return false; |
| 214 | + } |
| 215 | + myNonce = avLine; |
| 216 | + } else if(varName.equals("opaque")){ |
| 217 | + if(opaque != NULL && !avLine.equals(opaque)){ |
| 218 | + //os_printf("AUTH FAIL: opaque\n"); |
| 219 | + return false; |
| 220 | + } |
| 221 | + } else if(varName.equals("uri")){ |
| 222 | + if(uri != NULL && !avLine.equals(uri)){ |
| 223 | + //os_printf("AUTH FAIL: uri\n"); |
| 224 | + return false; |
| 225 | + } |
| 226 | + myUri = avLine; |
| 227 | + } else if(varName.equals("response")){ |
| 228 | + myResponse = avLine; |
| 229 | + } else if(varName.equals("qop")){ |
| 230 | + myQop = avLine; |
| 231 | + } else if(varName.equals("nc")){ |
| 232 | + myNc = avLine; |
| 233 | + } else if(varName.equals("cnonce")){ |
| 234 | + myCnonce = avLine; |
| 235 | + } |
| 236 | + } while(nextBreak > 0); |
| 237 | + |
| 238 | + String ha1 = (passwordIsHash) ? String(password) : stringMD5(myUsername + ":" + myRealm + ":" + String(password)); |
| 239 | + String ha2 = String(method) + ":" + myUri; |
| 240 | + String response = ha1 + ":" + myNonce + ":" + myNc + ":" + myCnonce + ":" + myQop + ":" + stringMD5(ha2); |
| 241 | + |
| 242 | + if(myResponse.equals(stringMD5(response))){ |
| 243 | + //os_printf("AUTH SUCCESS\n"); |
| 244 | + return true; |
| 245 | + } |
| 246 | + |
| 247 | + //os_printf("AUTH FAIL: password\n"); |
| 248 | + return false; |
| 249 | +} |
0 commit comments