http_auth.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. return ESP_FAIL;
  47. }
  48. MD5Init(&md5_ctx);
  49. MD5Update(&md5_ctx, buf, len);
  50. MD5Final(digest, &md5_ctx);
  51. for (i = 0; i < 16; ++i) {
  52. sprintf(&md[i * 2], "%02x", (unsigned int)digest[i]);
  53. }
  54. va_end(ap);
  55. free(buf);
  56. return MD5_MAX_LEN;
  57. }
  58. char *http_auth_digest(const char *username, const char *password, esp_http_auth_data_t *auth_data)
  59. {
  60. char *ha1, *ha2 = NULL;
  61. char *digest = NULL;
  62. char *auth_str = NULL;
  63. char *temp_auth_str = NULL;
  64. if (username == NULL ||
  65. password == NULL ||
  66. auth_data->nonce == NULL ||
  67. auth_data->uri == NULL ||
  68. auth_data->realm == NULL) {
  69. return NULL;
  70. }
  71. ha1 = calloc(1, MD5_MAX_LEN);
  72. HTTP_MEM_CHECK(TAG, ha1, goto _digest_exit);
  73. ha2 = calloc(1, MD5_MAX_LEN);
  74. HTTP_MEM_CHECK(TAG, ha2, goto _digest_exit);
  75. digest = calloc(1, MD5_MAX_LEN);
  76. HTTP_MEM_CHECK(TAG, digest, goto _digest_exit);
  77. if (md5_printf(ha1, "%s:%s:%s", username, auth_data->realm, password) <= 0) {
  78. goto _digest_exit;
  79. }
  80. ESP_LOGD(TAG, "%s %s %s %s\r\n", "Digest", username, auth_data->realm, password);
  81. if (strcasecmp(auth_data->algorithm, "md5-sess") == 0) {
  82. if (md5_printf(ha1, "%s:%s:%016llx", ha1, auth_data->nonce, auth_data->cnonce) <= 0) {
  83. goto _digest_exit;
  84. }
  85. }
  86. if (md5_printf(ha2, "%s:%s", auth_data->method, auth_data->uri) <= 0) {
  87. goto _digest_exit;
  88. }
  89. //support qop = auth
  90. if (auth_data->qop && strcasecmp(auth_data->qop, "auth-int") == 0) {
  91. if (md5_printf(ha2, "%s:%s", ha2, "entity") <= 0) {
  92. goto _digest_exit;
  93. }
  94. }
  95. if (auth_data->qop) {
  96. // response=MD5(HA1:nonce:nonceCount:cnonce:qop:HA2)
  97. 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) {
  98. goto _digest_exit;
  99. }
  100. } else {
  101. // response=MD5(HA1:nonce:HA2)
  102. if (md5_printf(digest, "%s:%s:%s", ha1, auth_data->nonce, ha2) <= 0) {
  103. goto _digest_exit;
  104. }
  105. }
  106. asprintf(&auth_str, "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", algorithm=\"MD5\", "
  107. "response=\"%s\", qop=%s, nc=%08x, cnonce=\"%016llx\"",
  108. username, auth_data->realm, auth_data->nonce, auth_data->uri, digest, auth_data->qop, auth_data->nc, auth_data->cnonce);
  109. if (auth_data->opaque) {
  110. asprintf(&temp_auth_str, "%s, opaque=\"%s\"", auth_str, auth_data->opaque);
  111. free(auth_str);
  112. auth_str = temp_auth_str;
  113. }
  114. _digest_exit:
  115. free(ha1);
  116. free(ha2);
  117. free(digest);
  118. return auth_str;
  119. }
  120. char *http_auth_basic(const char *username, const char *password)
  121. {
  122. int out;
  123. char *user_info = NULL;
  124. char *digest = NULL;
  125. size_t n = 0;
  126. asprintf(&user_info, "%s:%s", username, password);
  127. HTTP_MEM_CHECK(TAG, user_info, return NULL);
  128. mbedtls_base64_encode(NULL, 0, &n, (const unsigned char *)user_info, strlen(user_info));
  129. digest = calloc(1, 6 + n + 1);
  130. HTTP_MEM_CHECK(TAG, digest, goto _basic_exit);
  131. strcpy(digest, "Basic ");
  132. mbedtls_base64_encode((unsigned char *)digest + 6, n, (size_t *)&out, (const unsigned char *)user_info, strlen(user_info));
  133. _basic_exit:
  134. free(user_info);
  135. return digest;
  136. }