http_auth.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <stdarg.h>
  17. #include "esp_netif.h"
  18. #include "lwip/sockets.h"
  19. #include "esp32/rom/md5_hash.h"
  20. #include "mbedtls/base64.h"
  21. #include "esp_system.h"
  22. #include "esp_log.h"
  23. #include "http_utils.h"
  24. #include "http_auth.h"
  25. #define MD5_MAX_LEN (33)
  26. #define HTTP_AUTH_BUF_LEN (1024)
  27. static const char *TAG = "HTTP_AUTH";
  28. /**
  29. * @brief This function hash a formatted string with MD5 and format the result as ascii characters
  30. *
  31. * @param md The buffer will hold the ascii result
  32. * @param[in] fmt The format
  33. *
  34. * @return Length of the result
  35. */
  36. static int md5_printf(char *md, const char *fmt, ...)
  37. {
  38. unsigned char *buf;
  39. unsigned char digest[MD5_MAX_LEN];
  40. int len, i;
  41. struct MD5Context md5_ctx;
  42. va_list ap;
  43. va_start(ap, fmt);
  44. len = vasprintf((char **)&buf, fmt, ap);
  45. if (buf == NULL) {
  46. va_end(ap);
  47. return ESP_FAIL;
  48. }
  49. MD5Init(&md5_ctx);
  50. MD5Update(&md5_ctx, buf, len);
  51. MD5Final(digest, &md5_ctx);
  52. for (i = 0; i < 16; ++i) {
  53. sprintf(&md[i * 2], "%02x", (unsigned int)digest[i]);
  54. }
  55. va_end(ap);
  56. free(buf);
  57. return MD5_MAX_LEN;
  58. }
  59. char *http_auth_digest(const char *username, const char *password, esp_http_auth_data_t *auth_data)
  60. {
  61. char *ha1, *ha2 = NULL;
  62. char *digest = NULL;
  63. char *auth_str = NULL;
  64. char *temp_auth_str = NULL;
  65. if (username == NULL ||
  66. password == NULL ||
  67. auth_data->nonce == NULL ||
  68. auth_data->uri == NULL ||
  69. auth_data->realm == NULL) {
  70. return NULL;
  71. }
  72. ha1 = calloc(1, MD5_MAX_LEN);
  73. HTTP_MEM_CHECK(TAG, ha1, goto _digest_exit);
  74. ha2 = calloc(1, MD5_MAX_LEN);
  75. HTTP_MEM_CHECK(TAG, ha2, goto _digest_exit);
  76. digest = calloc(1, MD5_MAX_LEN);
  77. HTTP_MEM_CHECK(TAG, digest, goto _digest_exit);
  78. if (md5_printf(ha1, "%s:%s:%s", username, auth_data->realm, password) <= 0) {
  79. goto _digest_exit;
  80. }
  81. ESP_LOGD(TAG, "%s %s %s %s\r\n", "Digest", username, auth_data->realm, password);
  82. if (strcasecmp(auth_data->algorithm, "md5-sess") == 0) {
  83. if (md5_printf(ha1, "%s:%s:%016llx", ha1, auth_data->nonce, auth_data->cnonce) <= 0) {
  84. goto _digest_exit;
  85. }
  86. }
  87. if (md5_printf(ha2, "%s:%s", auth_data->method, auth_data->uri) <= 0) {
  88. goto _digest_exit;
  89. }
  90. //support qop = auth
  91. if (auth_data->qop && strcasecmp(auth_data->qop, "auth-int") == 0) {
  92. if (md5_printf(ha2, "%s:%s", ha2, "entity") <= 0) {
  93. goto _digest_exit;
  94. }
  95. }
  96. if (auth_data->qop) {
  97. // response=MD5(HA1:nonce:nonceCount:cnonce:qop:HA2)
  98. 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) {
  99. goto _digest_exit;
  100. }
  101. } else {
  102. // response=MD5(HA1:nonce:HA2)
  103. if (md5_printf(digest, "%s:%s:%s", ha1, auth_data->nonce, ha2) <= 0) {
  104. goto _digest_exit;
  105. }
  106. }
  107. asprintf(&auth_str, "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", algorithm=\"MD5\", "
  108. "response=\"%s\", qop=%s, nc=%08x, cnonce=\"%016llx\"",
  109. username, auth_data->realm, auth_data->nonce, auth_data->uri, digest, auth_data->qop, auth_data->nc, auth_data->cnonce);
  110. if (auth_data->opaque) {
  111. asprintf(&temp_auth_str, "%s, opaque=\"%s\"", auth_str, auth_data->opaque);
  112. free(auth_str);
  113. auth_str = temp_auth_str;
  114. }
  115. _digest_exit:
  116. free(ha1);
  117. free(ha2);
  118. free(digest);
  119. return auth_str;
  120. }
  121. char *http_auth_basic(const char *username, const char *password)
  122. {
  123. int out;
  124. char *user_info = NULL;
  125. char *digest = NULL;
  126. size_t n = 0;
  127. asprintf(&user_info, "%s:%s", username, password);
  128. HTTP_MEM_CHECK(TAG, user_info, return NULL);
  129. mbedtls_base64_encode(NULL, 0, &n, (const unsigned char *)user_info, strlen(user_info));
  130. digest = calloc(1, 6 + n + 1);
  131. HTTP_MEM_CHECK(TAG, digest, goto _basic_exit);
  132. strcpy(digest, "Basic ");
  133. mbedtls_base64_encode((unsigned char *)digest + 6, n, (size_t *)&out, (const unsigned char *)user_info, strlen(user_info));
  134. _basic_exit:
  135. free(user_info);
  136. return digest;
  137. }