curl_sasl.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #ifndef HEADER_CURL_SASL_H
  2. #define HEADER_CURL_SASL_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 2012 - 2015, Daniel Stenberg, <[email protected]>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at http://curl.haxx.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. ***************************************************************************/
  24. #include <curl/curl.h>
  25. struct SessionHandle;
  26. struct connectdata;
  27. #if !defined(CURL_DISABLE_CRYPTO_AUTH)
  28. struct digestdata;
  29. #endif
  30. #if defined(USE_NTLM)
  31. struct ntlmdata;
  32. #endif
  33. #if defined(USE_KERBEROS5)
  34. struct kerberos5data;
  35. #endif
  36. /* Authentication mechanism flags */
  37. #define SASL_MECH_LOGIN (1 << 0)
  38. #define SASL_MECH_PLAIN (1 << 1)
  39. #define SASL_MECH_CRAM_MD5 (1 << 2)
  40. #define SASL_MECH_DIGEST_MD5 (1 << 3)
  41. #define SASL_MECH_GSSAPI (1 << 4)
  42. #define SASL_MECH_EXTERNAL (1 << 5)
  43. #define SASL_MECH_NTLM (1 << 6)
  44. #define SASL_MECH_XOAUTH2 (1 << 7)
  45. /* Authentication mechanism values */
  46. #define SASL_AUTH_NONE 0
  47. #define SASL_AUTH_ANY ~0U
  48. #define SASL_AUTH_DEFAULT (SASL_AUTH_ANY & \
  49. ~(SASL_MECH_EXTERNAL | SASL_MECH_XOAUTH2))
  50. /* Authentication mechanism strings */
  51. #define SASL_MECH_STRING_LOGIN "LOGIN"
  52. #define SASL_MECH_STRING_PLAIN "PLAIN"
  53. #define SASL_MECH_STRING_CRAM_MD5 "CRAM-MD5"
  54. #define SASL_MECH_STRING_DIGEST_MD5 "DIGEST-MD5"
  55. #define SASL_MECH_STRING_GSSAPI "GSSAPI"
  56. #define SASL_MECH_STRING_EXTERNAL "EXTERNAL"
  57. #define SASL_MECH_STRING_NTLM "NTLM"
  58. #define SASL_MECH_STRING_XOAUTH2 "XOAUTH2"
  59. #if !defined(CURL_DISABLE_CRYPTO_AUTH)
  60. #define DIGEST_MAX_VALUE_LENGTH 256
  61. #define DIGEST_MAX_CONTENT_LENGTH 1024
  62. #endif
  63. enum {
  64. CURLDIGESTALGO_MD5,
  65. CURLDIGESTALGO_MD5SESS
  66. };
  67. /* SASL machine states */
  68. typedef enum {
  69. SASL_STOP,
  70. SASL_PLAIN,
  71. SASL_LOGIN,
  72. SASL_LOGIN_PASSWD,
  73. SASL_EXTERNAL,
  74. SASL_CRAMMD5,
  75. SASL_DIGESTMD5,
  76. SASL_DIGESTMD5_RESP,
  77. SASL_NTLM,
  78. SASL_NTLM_TYPE2MSG,
  79. SASL_GSSAPI,
  80. SASL_GSSAPI_TOKEN,
  81. SASL_GSSAPI_NO_DATA,
  82. SASL_XOAUTH2,
  83. SASL_CANCEL,
  84. SASL_FINAL
  85. } saslstate;
  86. /* Progress indicator */
  87. typedef enum {
  88. SASL_IDLE,
  89. SASL_INPROGRESS,
  90. SASL_DONE
  91. } saslprogress;
  92. /* Protocol dependent SASL parameters */
  93. struct SASLproto {
  94. const char *service; /* The service name */
  95. int contcode; /* Code to receive when continuation is expected */
  96. int finalcode; /* Code to receive upon authentication success */
  97. size_t maxirlen; /* Maximum initial response length */
  98. CURLcode (*sendauth)(struct connectdata *conn,
  99. const char *mech, const char *ir);
  100. /* Send authentication command */
  101. CURLcode (*sendcont)(struct connectdata *conn, const char *contauth);
  102. /* Send authentication continuation */
  103. void (*getmessage)(char *buffer, char **outptr);
  104. /* Get SASL response message */
  105. };
  106. /* Per-connection parameters */
  107. struct SASL {
  108. const struct SASLproto *params; /* Protocol dependent parameters */
  109. saslstate state; /* Current machine state */
  110. unsigned int authmechs; /* Accepted authentication mechanisms */
  111. unsigned int prefmech; /* Preferred authentication mechanism */
  112. unsigned int authused; /* Auth mechanism used for the connection */
  113. bool resetprefs; /* For URL auth option parsing. */
  114. bool mutual_auth; /* Mutual authentication enabled (GSSAPI only) */
  115. bool force_ir; /* Protocol always supports initial response */
  116. };
  117. /* This is used to test whether the line starts with the given mechanism */
  118. #define sasl_mech_equal(line, wordlen, mech) \
  119. (wordlen == (sizeof(mech) - 1) / sizeof(char) && \
  120. !memcmp(line, mech, wordlen))
  121. /* This is used to build a SPN string */
  122. #if !defined(USE_WINDOWS_SSPI)
  123. char *Curl_sasl_build_spn(const char *service, const char *instance);
  124. #else
  125. TCHAR *Curl_sasl_build_spn(const char *service, const char *instance);
  126. #endif
  127. /* This is used to extract the realm from a challenge message */
  128. int Curl_sasl_digest_get_pair(const char *str, char *value, char *content,
  129. const char **endptr);
  130. #if defined(HAVE_GSSAPI)
  131. char *Curl_sasl_build_gssapi_spn(const char *service, const char *host);
  132. #endif
  133. #ifndef CURL_DISABLE_CRYPTO_AUTH
  134. /* This is used to generate a base64 encoded DIGEST-MD5 response message */
  135. CURLcode Curl_sasl_create_digest_md5_message(struct SessionHandle *data,
  136. const char *chlg64,
  137. const char *userp,
  138. const char *passwdp,
  139. const char *service,
  140. char **outptr, size_t *outlen);
  141. /* This is used to decode a HTTP DIGEST challenge message */
  142. CURLcode Curl_sasl_decode_digest_http_message(const char *chlg,
  143. struct digestdata *digest);
  144. /* This is used to generate a HTTP DIGEST response message */
  145. CURLcode Curl_sasl_create_digest_http_message(struct SessionHandle *data,
  146. const char *userp,
  147. const char *passwdp,
  148. const unsigned char *request,
  149. const unsigned char *uri,
  150. struct digestdata *digest,
  151. char **outptr, size_t *outlen);
  152. /* This is used to clean up the digest specific data */
  153. void Curl_sasl_digest_cleanup(struct digestdata *digest);
  154. #endif
  155. #ifdef USE_NTLM
  156. /* This is used to generate a base64 encoded NTLM type-1 message */
  157. CURLcode Curl_sasl_create_ntlm_type1_message(const char *userp,
  158. const char *passwdp,
  159. struct ntlmdata *ntlm,
  160. char **outptr,
  161. size_t *outlen);
  162. /* This is used to decode a base64 encoded NTLM type-2 message */
  163. CURLcode Curl_sasl_decode_ntlm_type2_message(struct SessionHandle *data,
  164. const char *type2msg,
  165. struct ntlmdata *ntlm);
  166. /* This is used to generate a base64 encoded NTLM type-3 message */
  167. CURLcode Curl_sasl_create_ntlm_type3_message(struct SessionHandle *data,
  168. const char *userp,
  169. const char *passwdp,
  170. struct ntlmdata *ntlm,
  171. char **outptr, size_t *outlen);
  172. /* This is used to clean up the ntlm specific data */
  173. void Curl_sasl_ntlm_cleanup(struct ntlmdata *ntlm);
  174. #endif /* USE_NTLM */
  175. #if defined(USE_KERBEROS5)
  176. /* This is used to generate a base64 encoded GSSAPI (Kerberos V5) user token
  177. message */
  178. CURLcode Curl_sasl_create_gssapi_user_message(struct SessionHandle *data,
  179. const char *userp,
  180. const char *passwdp,
  181. const char *service,
  182. const bool mutual,
  183. const char *chlg64,
  184. struct kerberos5data *krb5,
  185. char **outptr, size_t *outlen);
  186. /* This is used to generate a base64 encoded GSSAPI (Kerberos V5) security
  187. token message */
  188. CURLcode Curl_sasl_create_gssapi_security_message(struct SessionHandle *data,
  189. const char *input,
  190. struct kerberos5data *krb5,
  191. char **outptr,
  192. size_t *outlen);
  193. /* This is used to clean up the gssapi specific data */
  194. void Curl_sasl_gssapi_cleanup(struct kerberos5data *krb5);
  195. #endif /* USE_KERBEROS5 */
  196. /* This is used to cleanup any libraries or curl modules used by the sasl
  197. functions */
  198. void Curl_sasl_cleanup(struct connectdata *conn, unsigned int authused);
  199. /* Convert a mechanism name to a token */
  200. unsigned int Curl_sasl_decode_mech(const char *ptr,
  201. size_t maxlen, size_t *len);
  202. /* Parse the URL login options */
  203. CURLcode Curl_sasl_parse_url_auth_option(struct SASL *sasl,
  204. const char *value, size_t len);
  205. /* Initializes an SASL structure */
  206. void Curl_sasl_init(struct SASL *sasl, const struct SASLproto *params);
  207. /* Check if we have enough auth data and capabilities to authenticate */
  208. bool Curl_sasl_can_authenticate(struct SASL *sasl, struct connectdata *conn);
  209. /* Calculate the required login details for SASL authentication */
  210. CURLcode Curl_sasl_start(struct SASL *sasl, struct connectdata *conn,
  211. bool force_ir, saslprogress *progress);
  212. /* Continue an SASL authentication */
  213. CURLcode Curl_sasl_continue(struct SASL *sasl, struct connectdata *conn,
  214. int code, saslprogress *progress);
  215. #endif /* HEADER_CURL_SASL_H */