2
0

gsasl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Simon Josefsson, <simon@josefsson.org>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. * RFC5802 SCRAM-SHA-1 authentication
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #ifdef USE_GSASL
  28. #include <curl/curl.h>
  29. #include "vauth/vauth.h"
  30. #include "urldata.h"
  31. #include "sendf.h"
  32. #include <gsasl.h>
  33. /* The last 3 #include files should be in this order */
  34. #include "curl_printf.h"
  35. #include "curl_memory.h"
  36. #include "memdebug.h"
  37. bool Curl_auth_gsasl_is_supported(struct Curl_easy *data,
  38. const char *mech,
  39. struct gsasldata *gsasl)
  40. {
  41. int res;
  42. res = gsasl_init(&gsasl->ctx);
  43. if(res != GSASL_OK) {
  44. failf(data, "gsasl init: %s\n", gsasl_strerror(res));
  45. return FALSE;
  46. }
  47. res = gsasl_client_start(gsasl->ctx, mech, &gsasl->client);
  48. if(res != GSASL_OK) {
  49. gsasl_done(gsasl->ctx);
  50. return FALSE;
  51. }
  52. return TRUE;
  53. }
  54. CURLcode Curl_auth_gsasl_start(struct Curl_easy *data,
  55. const char *userp,
  56. const char *passwdp,
  57. struct gsasldata *gsasl)
  58. {
  59. #if GSASL_VERSION_NUMBER >= 0x010b00
  60. int res;
  61. res =
  62. #endif
  63. gsasl_property_set(gsasl->client, GSASL_AUTHID, userp);
  64. #if GSASL_VERSION_NUMBER >= 0x010b00
  65. if(res != GSASL_OK) {
  66. failf(data, "setting AUTHID failed: %s\n", gsasl_strerror(res));
  67. return CURLE_OUT_OF_MEMORY;
  68. }
  69. #endif
  70. #if GSASL_VERSION_NUMBER >= 0x010b00
  71. res =
  72. #endif
  73. gsasl_property_set(gsasl->client, GSASL_PASSWORD, passwdp);
  74. #if GSASL_VERSION_NUMBER >= 0x010b00
  75. if(res != GSASL_OK) {
  76. failf(data, "setting PASSWORD failed: %s\n", gsasl_strerror(res));
  77. return CURLE_OUT_OF_MEMORY;
  78. }
  79. #endif
  80. (void)data;
  81. return CURLE_OK;
  82. }
  83. CURLcode Curl_auth_gsasl_token(struct Curl_easy *data,
  84. const struct bufref *chlg,
  85. struct gsasldata *gsasl,
  86. struct bufref *out)
  87. {
  88. int res;
  89. char *response;
  90. size_t outlen;
  91. res = gsasl_step(gsasl->client,
  92. (const char *) Curl_bufref_ptr(chlg), Curl_bufref_len(chlg),
  93. &response, &outlen);
  94. if(res != GSASL_OK && res != GSASL_NEEDS_MORE) {
  95. failf(data, "GSASL step: %s\n", gsasl_strerror(res));
  96. return CURLE_BAD_CONTENT_ENCODING;
  97. }
  98. Curl_bufref_set(out, response, outlen, gsasl_free);
  99. return CURLE_OK;
  100. }
  101. void Curl_auth_gsasl_cleanup(struct gsasldata *gsasl)
  102. {
  103. gsasl_finish(gsasl->client);
  104. gsasl->client = NULL;
  105. gsasl_done(gsasl->ctx);
  106. gsasl->ctx = NULL;
  107. }
  108. #endif