http_auth.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "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. struct MD5Context 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. 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. HTTP_MEM_CHECK(TAG, ha1, goto _digest_exit);
  67. ha2 = calloc(1, MD5_MAX_LEN);
  68. HTTP_MEM_CHECK(TAG, ha2, goto _digest_exit);
  69. digest = calloc(1, MD5_MAX_LEN);
  70. HTTP_MEM_CHECK(TAG, digest, goto _digest_exit);
  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\r\n", "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. asprintf(&auth_str, "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", algorithm=\"MD5\", "
  101. "response=\"%s\", qop=%s, nc=%08x, cnonce=\"%016llx\"",
  102. username, auth_data->realm, auth_data->nonce, auth_data->uri, digest, auth_data->qop, auth_data->nc, auth_data->cnonce);
  103. if (auth_data->opaque) {
  104. asprintf(&temp_auth_str, "%s, opaque=\"%s\"", auth_str, auth_data->opaque);
  105. free(auth_str);
  106. auth_str = temp_auth_str;
  107. }
  108. _digest_exit:
  109. free(ha1);
  110. free(ha2);
  111. free(digest);
  112. return auth_str;
  113. }
  114. char *http_auth_basic(const char *username, const char *password)
  115. {
  116. int out;
  117. char *user_info = NULL;
  118. char *digest = NULL;
  119. size_t n = 0;
  120. asprintf(&user_info, "%s:%s", username, password);
  121. HTTP_MEM_CHECK(TAG, user_info, return NULL);
  122. esp_crypto_base64_encode(NULL, 0, &n, (const unsigned char *)user_info, strlen(user_info));
  123. digest = calloc(1, 6 + n + 1);
  124. HTTP_MEM_CHECK(TAG, digest, goto _basic_exit);
  125. strcpy(digest, "Basic ");
  126. esp_crypto_base64_encode((unsigned char *)digest + 6, n, (size_t *)&out, (const unsigned char *)user_info, strlen(user_info));
  127. _basic_exit:
  128. free(user_info);
  129. return digest;
  130. }