http_auth.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <stdarg.h>
  10. #include "lwip/sockets.h"
  11. #include "esp_rom_md5.h"
  12. #include "esp_tls_crypto.h"
  13. #include "esp_system.h"
  14. #include "esp_log.h"
  15. #include "esp_check.h"
  16. #include "http_utils.h"
  17. #include "http_auth.h"
  18. #define MD5_MAX_LEN (33)
  19. #define HTTP_AUTH_BUF_LEN (1024)
  20. static const char *TAG = "HTTP_AUTH";
  21. /**
  22. * @brief This function hash a formatted string with MD5 and format the result as ascii characters
  23. *
  24. * @param md The buffer will hold the ascii result
  25. * @param[in] fmt The format
  26. *
  27. * @return Length of the result
  28. */
  29. static int md5_printf(char *md, const char *fmt, ...)
  30. {
  31. unsigned char *buf;
  32. unsigned char digest[MD5_MAX_LEN];
  33. int len, i;
  34. md5_context_t md5_ctx;
  35. va_list ap;
  36. va_start(ap, fmt);
  37. len = vasprintf((char **)&buf, fmt, ap);
  38. if (buf == NULL) {
  39. va_end(ap);
  40. return ESP_FAIL;
  41. }
  42. esp_rom_md5_init(&md5_ctx);
  43. esp_rom_md5_update(&md5_ctx, buf, len);
  44. esp_rom_md5_final(digest, &md5_ctx);
  45. for (i = 0; i < 16; ++i) {
  46. sprintf(&md[i * 2], "%02x", (unsigned int)digest[i]);
  47. }
  48. va_end(ap);
  49. free(buf);
  50. return MD5_MAX_LEN;
  51. }
  52. char *http_auth_digest(const char *username, const char *password, esp_http_auth_data_t *auth_data)
  53. {
  54. char *ha1, *ha2 = NULL;
  55. char *digest = NULL;
  56. char *auth_str = NULL;
  57. char *temp_auth_str = NULL;
  58. esp_err_t ret = ESP_OK;
  59. if (username == NULL ||
  60. password == NULL ||
  61. auth_data->nonce == NULL ||
  62. auth_data->uri == NULL ||
  63. auth_data->realm == NULL) {
  64. return NULL;
  65. }
  66. ha1 = calloc(1, MD5_MAX_LEN);
  67. ESP_GOTO_ON_FALSE(ha1, ESP_FAIL, _digest_exit, TAG, "Memory exhausted");
  68. ha2 = calloc(1, MD5_MAX_LEN);
  69. ESP_GOTO_ON_FALSE(ha2, ESP_FAIL, _digest_exit, TAG, "Memory exhausted");
  70. digest = calloc(1, MD5_MAX_LEN);
  71. ESP_GOTO_ON_FALSE(digest, ESP_FAIL, _digest_exit, TAG, "Memory exhausted");
  72. if (md5_printf(ha1, "%s:%s:%s", username, auth_data->realm, password) <= 0) {
  73. goto _digest_exit;
  74. }
  75. ESP_LOGD(TAG, "%s %s %s %s\r\n", "Digest", username, auth_data->realm, password);
  76. if (strcasecmp(auth_data->algorithm, "md5-sess") == 0) {
  77. if (md5_printf(ha1, "%s:%s:%016llx", ha1, auth_data->nonce, auth_data->cnonce) <= 0) {
  78. goto _digest_exit;
  79. }
  80. }
  81. if (md5_printf(ha2, "%s:%s", auth_data->method, auth_data->uri) <= 0) {
  82. goto _digest_exit;
  83. }
  84. //support qop = auth
  85. if (auth_data->qop && strcasecmp(auth_data->qop, "auth-int") == 0) {
  86. if (md5_printf(ha2, "%s:%s", ha2, "entity") <= 0) {
  87. goto _digest_exit;
  88. }
  89. }
  90. if (auth_data->qop) {
  91. // response=MD5(HA1:nonce:nonceCount:cnonce:qop:HA2)
  92. 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) {
  93. goto _digest_exit;
  94. }
  95. } else {
  96. // response=MD5(HA1:nonce:HA2)
  97. if (md5_printf(digest, "%s:%s:%s", ha1, auth_data->nonce, ha2) <= 0) {
  98. goto _digest_exit;
  99. }
  100. }
  101. asprintf(&auth_str, "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", algorithm=\"MD5\", "
  102. "response=\"%s\", qop=%s, nc=%08x, cnonce=\"%016llx\"",
  103. username, auth_data->realm, auth_data->nonce, auth_data->uri, digest, auth_data->qop, auth_data->nc, auth_data->cnonce);
  104. if (auth_data->opaque) {
  105. asprintf(&temp_auth_str, "%s, opaque=\"%s\"", auth_str, auth_data->opaque);
  106. free(auth_str);
  107. auth_str = temp_auth_str;
  108. }
  109. _digest_exit:
  110. free(ha1);
  111. free(ha2);
  112. free(digest);
  113. return (ret == ESP_OK) ? auth_str : NULL;
  114. }
  115. char *http_auth_basic(const char *username, const char *password)
  116. {
  117. int out;
  118. char *user_info = NULL;
  119. char *digest = NULL;
  120. esp_err_t ret = ESP_OK;
  121. size_t n = 0;
  122. asprintf(&user_info, "%s:%s", username, password);
  123. ESP_RETURN_ON_FALSE(user_info, NULL, TAG, "Memory exhausted");
  124. esp_crypto_base64_encode(NULL, 0, &n, (const unsigned char *)user_info, strlen(user_info));
  125. digest = calloc(1, 6 + n + 1);
  126. ESP_GOTO_ON_FALSE(digest, ESP_FAIL, _basic_exit, TAG, "Memory exhausted");
  127. strcpy(digest, "Basic ");
  128. esp_crypto_base64_encode((unsigned char *)digest + 6, n, (size_t *)&out, (const unsigned char *)user_info, strlen(user_info));
  129. _basic_exit:
  130. free(user_info);
  131. return (ret == ESP_OK) ? digest : NULL;
  132. }