gsasl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Simon Josefsson, <[email protected]>, 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.h"
  30. #include "../urldata.h"
  31. #include "../sendf.h"
  32. #include <gsasl.h>
  33. /* The last 2 #include files should be in this order */
  34. #include "../curl_memory.h"
  35. #include "../memdebug.h"
  36. bool Curl_auth_gsasl_is_supported(struct Curl_easy *data,
  37. const char *mech,
  38. struct gsasldata *gsasl)
  39. {
  40. int res;
  41. res = gsasl_init(&gsasl->ctx);
  42. if(res != GSASL_OK) {
  43. failf(data, "gsasl init: %s", gsasl_strerror(res));
  44. return FALSE;
  45. }
  46. res = gsasl_client_start(gsasl->ctx, mech, &gsasl->client);
  47. if(res != GSASL_OK) {
  48. gsasl_done(gsasl->ctx);
  49. return FALSE;
  50. }
  51. return TRUE;
  52. }
  53. CURLcode Curl_auth_gsasl_start(struct Curl_easy *data,
  54. const char *userp,
  55. const char *passwdp,
  56. struct gsasldata *gsasl)
  57. {
  58. #if GSASL_VERSION_NUMBER >= 0x010b00
  59. int res;
  60. res =
  61. #endif
  62. gsasl_property_set(gsasl->client, GSASL_AUTHID, userp);
  63. #if GSASL_VERSION_NUMBER >= 0x010b00
  64. if(res != GSASL_OK) {
  65. failf(data, "setting AUTHID failed: %s", gsasl_strerror(res));
  66. return CURLE_OUT_OF_MEMORY;
  67. }
  68. #endif
  69. #if GSASL_VERSION_NUMBER >= 0x010b00
  70. res =
  71. #endif
  72. gsasl_property_set(gsasl->client, GSASL_PASSWORD, passwdp);
  73. #if GSASL_VERSION_NUMBER >= 0x010b00
  74. if(res != GSASL_OK) {
  75. failf(data, "setting PASSWORD failed: %s", gsasl_strerror(res));
  76. return CURLE_OUT_OF_MEMORY;
  77. }
  78. #endif
  79. (void)data;
  80. return CURLE_OK;
  81. }
  82. CURLcode Curl_auth_gsasl_token(struct Curl_easy *data,
  83. const struct bufref *chlg,
  84. struct gsasldata *gsasl,
  85. struct bufref *out)
  86. {
  87. int res;
  88. char *response;
  89. size_t outlen;
  90. res = gsasl_step(gsasl->client,
  91. (const char *) Curl_bufref_ptr(chlg), Curl_bufref_len(chlg),
  92. &response, &outlen);
  93. if(res != GSASL_OK && res != GSASL_NEEDS_MORE) {
  94. failf(data, "GSASL step: %s", gsasl_strerror(res));
  95. return CURLE_BAD_CONTENT_ENCODING;
  96. }
  97. Curl_bufref_set(out, response, outlen, gsasl_free);
  98. return CURLE_OK;
  99. }
  100. void Curl_auth_gsasl_cleanup(struct gsasldata *gsasl)
  101. {
  102. gsasl_finish(gsasl->client);
  103. gsasl->client = NULL;
  104. gsasl_done(gsasl->ctx);
  105. gsasl->ctx = NULL;
  106. }
  107. #endif