gsasl.c 3.4 KB

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