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