2021-05-31 08:06:09 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-11-13 22:16:20 -05:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2022-11-08 03:44:09 -05:00
|
|
|
#include "sys/socket.h"
|
2020-07-21 05:01:28 -04:00
|
|
|
#include "esp_rom_md5.h"
|
2020-08-04 03:57:06 -04:00
|
|
|
#include "esp_tls_crypto.h"
|
2017-11-13 22:16:20 -05:00
|
|
|
|
|
|
|
#include "esp_log.h"
|
2021-09-30 07:05:57 -04:00
|
|
|
#include "esp_check.h"
|
2017-11-13 22:16:20 -05:00
|
|
|
|
|
|
|
#include "http_utils.h"
|
|
|
|
#include "http_auth.h"
|
|
|
|
|
|
|
|
#define MD5_MAX_LEN (33)
|
|
|
|
#define HTTP_AUTH_BUF_LEN (1024)
|
|
|
|
|
|
|
|
static const char *TAG = "HTTP_AUTH";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief This function hash a formatted string with MD5 and format the result as ascii characters
|
|
|
|
*
|
|
|
|
* @param md The buffer will hold the ascii result
|
|
|
|
* @param[in] fmt The format
|
|
|
|
*
|
|
|
|
* @return Length of the result
|
|
|
|
*/
|
|
|
|
static int md5_printf(char *md, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
unsigned char *buf;
|
|
|
|
unsigned char digest[MD5_MAX_LEN];
|
|
|
|
int len, i;
|
2021-11-06 05:31:05 -04:00
|
|
|
md5_context_t md5_ctx;
|
2017-11-13 22:16:20 -05:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
len = vasprintf((char **)&buf, fmt, ap);
|
|
|
|
if (buf == NULL) {
|
2021-07-20 15:52:53 -04:00
|
|
|
va_end(ap);
|
2018-05-21 23:55:18 -04:00
|
|
|
return ESP_FAIL;
|
2017-11-13 22:16:20 -05:00
|
|
|
}
|
|
|
|
|
2020-07-21 05:01:28 -04:00
|
|
|
esp_rom_md5_init(&md5_ctx);
|
|
|
|
esp_rom_md5_update(&md5_ctx, buf, len);
|
|
|
|
esp_rom_md5_final(digest, &md5_ctx);
|
2017-11-13 22:16:20 -05:00
|
|
|
|
|
|
|
for (i = 0; i < 16; ++i) {
|
|
|
|
sprintf(&md[i * 2], "%02x", (unsigned int)digest[i]);
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
return MD5_MAX_LEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *http_auth_digest(const char *username, const char *password, esp_http_auth_data_t *auth_data)
|
|
|
|
{
|
|
|
|
char *ha1, *ha2 = NULL;
|
|
|
|
char *digest = NULL;
|
|
|
|
char *auth_str = NULL;
|
2020-10-01 05:02:50 -04:00
|
|
|
char *temp_auth_str = NULL;
|
2021-09-30 07:05:57 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2017-11-13 22:16:20 -05:00
|
|
|
|
|
|
|
if (username == NULL ||
|
|
|
|
password == NULL ||
|
|
|
|
auth_data->nonce == NULL ||
|
|
|
|
auth_data->uri == NULL ||
|
|
|
|
auth_data->realm == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ha1 = calloc(1, MD5_MAX_LEN);
|
2021-09-30 07:05:57 -04:00
|
|
|
ESP_GOTO_ON_FALSE(ha1, ESP_FAIL, _digest_exit, TAG, "Memory exhausted");
|
2017-11-13 22:16:20 -05:00
|
|
|
|
|
|
|
ha2 = calloc(1, MD5_MAX_LEN);
|
2021-09-30 07:05:57 -04:00
|
|
|
ESP_GOTO_ON_FALSE(ha2, ESP_FAIL, _digest_exit, TAG, "Memory exhausted");
|
2017-11-13 22:16:20 -05:00
|
|
|
|
|
|
|
digest = calloc(1, MD5_MAX_LEN);
|
2021-09-30 07:05:57 -04:00
|
|
|
ESP_GOTO_ON_FALSE(digest, ESP_FAIL, _digest_exit, TAG, "Memory exhausted");
|
2017-11-13 22:16:20 -05:00
|
|
|
|
|
|
|
if (md5_printf(ha1, "%s:%s:%s", username, auth_data->realm, password) <= 0) {
|
|
|
|
goto _digest_exit;
|
|
|
|
}
|
|
|
|
|
2023-06-08 14:56:11 -04:00
|
|
|
ESP_LOGD(TAG, "%s %s %s %s", "Digest", username, auth_data->realm, password);
|
2017-11-13 22:16:20 -05:00
|
|
|
if (strcasecmp(auth_data->algorithm, "md5-sess") == 0) {
|
|
|
|
if (md5_printf(ha1, "%s:%s:%016llx", ha1, auth_data->nonce, auth_data->cnonce) <= 0) {
|
|
|
|
goto _digest_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (md5_printf(ha2, "%s:%s", auth_data->method, auth_data->uri) <= 0) {
|
|
|
|
goto _digest_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
//support qop = auth
|
|
|
|
if (auth_data->qop && strcasecmp(auth_data->qop, "auth-int") == 0) {
|
|
|
|
if (md5_printf(ha2, "%s:%s", ha2, "entity") <= 0) {
|
|
|
|
goto _digest_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auth_data->qop) {
|
|
|
|
// response=MD5(HA1:nonce:nonceCount:cnonce:qop:HA2)
|
|
|
|
if (md5_printf(digest, "%s:%s:%08x:%016llx:%s:%s", ha1, auth_data->nonce, auth_data->nc, auth_data->cnonce, auth_data->qop, ha2) <= 0) {
|
|
|
|
goto _digest_exit;
|
|
|
|
}
|
|
|
|
} else {
|
2024-01-15 01:38:04 -05:00
|
|
|
/* Although as per RFC-2617, "qop" directive is optional in order to maintain backward compatibality, it is recommended
|
|
|
|
to use it if the server indicated that qop is supported. This enhancement was introduced to protect against attacks
|
|
|
|
like chosen-plaintext attack. */
|
|
|
|
ESP_LOGW(TAG, "\"qop\" directive not found. This may lead to attacks like chosen-plaintext attack");
|
2017-11-13 22:16:20 -05:00
|
|
|
// response=MD5(HA1:nonce:HA2)
|
|
|
|
if (md5_printf(digest, "%s:%s:%s", ha1, auth_data->nonce, ha2) <= 0) {
|
|
|
|
goto _digest_exit;
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 03:44:09 -05:00
|
|
|
int rc = asprintf(&auth_str, "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", algorithm=\"MD5\", "
|
2024-01-15 01:38:04 -05:00
|
|
|
"response=\"%s\"", username, auth_data->realm, auth_data->nonce, auth_data->uri, digest);
|
2022-11-08 03:44:09 -05:00
|
|
|
if (rc < 0) {
|
|
|
|
ESP_LOGE(TAG, "asprintf() returned: %d", rc);
|
|
|
|
ret = ESP_FAIL;
|
|
|
|
goto _digest_exit;
|
|
|
|
}
|
2024-01-15 01:38:04 -05:00
|
|
|
|
|
|
|
if (auth_data->qop) {
|
|
|
|
rc = asprintf(&temp_auth_str, ", qop=%s, nc=%08x, cnonce=\"%016"PRIx64"\"", auth_data->qop, auth_data->nc, auth_data->cnonce);
|
|
|
|
if (rc < 0) {
|
|
|
|
ESP_LOGE(TAG, "asprintf() returned: %d", rc);
|
|
|
|
ret = ESP_FAIL;
|
|
|
|
goto _digest_exit;
|
|
|
|
}
|
|
|
|
auth_str = http_utils_append_string(&auth_str, temp_auth_str, strlen(temp_auth_str));
|
|
|
|
if (!auth_str) {
|
|
|
|
ret = ESP_FAIL;
|
|
|
|
goto _digest_exit;
|
|
|
|
}
|
|
|
|
free(temp_auth_str);
|
|
|
|
auth_data->nc ++;
|
|
|
|
}
|
2020-10-01 05:02:50 -04:00
|
|
|
if (auth_data->opaque) {
|
2022-11-08 03:44:09 -05:00
|
|
|
rc = asprintf(&temp_auth_str, "%s, opaque=\"%s\"", auth_str, auth_data->opaque);
|
2023-01-11 06:27:04 -05:00
|
|
|
// Free the previous memory allocated for `auth_str`
|
|
|
|
free(auth_str);
|
2022-11-08 03:44:09 -05:00
|
|
|
if (rc < 0) {
|
|
|
|
ESP_LOGE(TAG, "asprintf() returned: %d", rc);
|
|
|
|
ret = ESP_FAIL;
|
|
|
|
goto _digest_exit;
|
|
|
|
}
|
2020-10-01 05:02:50 -04:00
|
|
|
auth_str = temp_auth_str;
|
|
|
|
}
|
2017-11-13 22:16:20 -05:00
|
|
|
_digest_exit:
|
|
|
|
free(ha1);
|
|
|
|
free(ha2);
|
|
|
|
free(digest);
|
2021-09-30 07:05:57 -04:00
|
|
|
return (ret == ESP_OK) ? auth_str : NULL;
|
2017-11-13 22:16:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
char *http_auth_basic(const char *username, const char *password)
|
|
|
|
{
|
2022-11-08 03:44:09 -05:00
|
|
|
size_t out;
|
2017-11-13 22:16:20 -05:00
|
|
|
char *user_info = NULL;
|
2018-07-22 21:59:49 -04:00
|
|
|
char *digest = NULL;
|
2021-09-30 07:05:57 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2018-07-22 21:59:49 -04:00
|
|
|
size_t n = 0;
|
2022-11-08 03:44:09 -05:00
|
|
|
if (asprintf(&user_info, "%s:%s", username, password) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-09-30 07:05:57 -04:00
|
|
|
ESP_RETURN_ON_FALSE(user_info, NULL, TAG, "Memory exhausted");
|
2020-08-04 03:57:06 -04:00
|
|
|
esp_crypto_base64_encode(NULL, 0, &n, (const unsigned char *)user_info, strlen(user_info));
|
2018-07-22 21:59:49 -04:00
|
|
|
digest = calloc(1, 6 + n + 1);
|
2021-09-30 07:05:57 -04:00
|
|
|
ESP_GOTO_ON_FALSE(digest, ESP_FAIL, _basic_exit, TAG, "Memory exhausted");
|
2017-11-13 22:16:20 -05:00
|
|
|
strcpy(digest, "Basic ");
|
2022-11-08 03:44:09 -05:00
|
|
|
esp_crypto_base64_encode((unsigned char *)digest + 6, n, &out, (const unsigned char *)user_info, strlen(user_info));
|
2017-11-13 22:16:20 -05:00
|
|
|
_basic_exit:
|
|
|
|
free(user_info);
|
2021-09-30 07:05:57 -04:00
|
|
|
return (ret == ESP_OK) ? digest : NULL;
|
2017-11-13 22:16:20 -05:00
|
|
|
}
|