http_auth.c 4.6 KB

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