http_auth.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. if (username == NULL ||
  64. password == NULL ||
  65. auth_data->nonce == NULL ||
  66. auth_data->uri == NULL ||
  67. auth_data->realm == NULL) {
  68. return NULL;
  69. }
  70. ha1 = calloc(1, MD5_MAX_LEN);
  71. HTTP_MEM_CHECK(TAG, ha1, goto _digest_exit);
  72. ha2 = calloc(1, MD5_MAX_LEN);
  73. HTTP_MEM_CHECK(TAG, ha2, goto _digest_exit);
  74. digest = calloc(1, MD5_MAX_LEN);
  75. HTTP_MEM_CHECK(TAG, digest, goto _digest_exit);
  76. if (md5_printf(ha1, "%s:%s:%s", username, auth_data->realm, password) <= 0) {
  77. goto _digest_exit;
  78. }
  79. ESP_LOGD(TAG, "%s %s %s %s\r\n", "Digest", username, auth_data->realm, password);
  80. if (strcasecmp(auth_data->algorithm, "md5-sess") == 0) {
  81. if (md5_printf(ha1, "%s:%s:%016llx", ha1, auth_data->nonce, auth_data->cnonce) <= 0) {
  82. goto _digest_exit;
  83. }
  84. }
  85. if (md5_printf(ha2, "%s:%s", auth_data->method, auth_data->uri) <= 0) {
  86. goto _digest_exit;
  87. }
  88. //support qop = auth
  89. if (auth_data->qop && strcasecmp(auth_data->qop, "auth-int") == 0) {
  90. if (md5_printf(ha2, "%s:%s", ha2, "entity") <= 0) {
  91. goto _digest_exit;
  92. }
  93. }
  94. if (auth_data->qop) {
  95. // response=MD5(HA1:nonce:nonceCount:cnonce:qop:HA2)
  96. 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) {
  97. goto _digest_exit;
  98. }
  99. } else {
  100. // response=MD5(HA1:nonce:HA2)
  101. if (md5_printf(digest, "%s:%s:%s", ha1, auth_data->nonce, ha2) <= 0) {
  102. goto _digest_exit;
  103. }
  104. }
  105. asprintf(&auth_str, "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", algorithm=\"MD5\", "
  106. "response=\"%s\", opaque=\"%s\", qop=%s, nc=%08x, cnonce=\"%016llx\"",
  107. username, auth_data->realm, auth_data->nonce, auth_data->uri, digest, auth_data->opaque, auth_data->qop, auth_data->nc, auth_data->cnonce);
  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. mbedtls_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. mbedtls_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. }