http_auth.c 4.9 KB

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