curl_sasl.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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, <daniel@haxx.se>, 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 https://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. #define SASL_MECH_OAUTHBEARER (1 << 8)
  46. /* Authentication mechanism values */
  47. #define SASL_AUTH_NONE 0
  48. #define SASL_AUTH_ANY ~0U
  49. #define SASL_AUTH_DEFAULT (SASL_AUTH_ANY & ~SASL_MECH_EXTERNAL)
  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. #define SASL_MECH_STRING_OAUTHBEARER "OAUTHBEARER"
  60. #if !defined(CURL_DISABLE_CRYPTO_AUTH)
  61. #define DIGEST_MAX_VALUE_LENGTH 256
  62. #define DIGEST_MAX_CONTENT_LENGTH 1024
  63. #endif
  64. enum {
  65. CURLDIGESTALGO_MD5,
  66. CURLDIGESTALGO_MD5SESS
  67. };
  68. /* SASL machine states */
  69. typedef enum {
  70. SASL_STOP,
  71. SASL_PLAIN,
  72. SASL_LOGIN,
  73. SASL_LOGIN_PASSWD,
  74. SASL_EXTERNAL,
  75. SASL_CRAMMD5,
  76. SASL_DIGESTMD5,
  77. SASL_DIGESTMD5_RESP,
  78. SASL_NTLM,
  79. SASL_NTLM_TYPE2MSG,
  80. SASL_GSSAPI,
  81. SASL_GSSAPI_TOKEN,
  82. SASL_GSSAPI_NO_DATA,
  83. SASL_OAUTH2,
  84. SASL_OAUTH2_RESP,
  85. SASL_CANCEL,
  86. SASL_FINAL
  87. } saslstate;
  88. /* Progress indicator */
  89. typedef enum {
  90. SASL_IDLE,
  91. SASL_INPROGRESS,
  92. SASL_DONE
  93. } saslprogress;
  94. /* Protocol dependent SASL parameters */
  95. struct SASLproto {
  96. const char *service; /* The service name */
  97. int contcode; /* Code to receive when continuation is expected */
  98. int finalcode; /* Code to receive upon authentication success */
  99. size_t maxirlen; /* Maximum initial response length */
  100. CURLcode (*sendauth)(struct connectdata *conn,
  101. const char *mech, const char *ir);
  102. /* Send authentication command */
  103. CURLcode (*sendcont)(struct connectdata *conn, const char *contauth);
  104. /* Send authentication continuation */
  105. void (*getmessage)(char *buffer, char **outptr);
  106. /* Get SASL response message */
  107. };
  108. /* Per-connection parameters */
  109. struct SASL {
  110. const struct SASLproto *params; /* Protocol dependent parameters */
  111. saslstate state; /* Current machine state */
  112. unsigned int authmechs; /* Accepted authentication mechanisms */
  113. unsigned int prefmech; /* Preferred authentication mechanism */
  114. unsigned int authused; /* Auth mechanism used for the connection */
  115. bool resetprefs; /* For URL auth option parsing. */
  116. bool mutual_auth; /* Mutual authentication enabled (GSSAPI only) */
  117. bool force_ir; /* Protocol always supports initial response */
  118. };
  119. /* This is used to test whether the line starts with the given mechanism */
  120. #define sasl_mech_equal(line, wordlen, mech) \
  121. (wordlen == (sizeof(mech) - 1) / sizeof(char) && \
  122. !memcmp(line, mech, wordlen))
  123. /* This is used to build a SPN string */
  124. #if !defined(USE_WINDOWS_SSPI)
  125. char *Curl_sasl_build_spn(const char *service, const char *instance);
  126. #else
  127. TCHAR *Curl_sasl_build_spn(const char *service, const char *instance);
  128. #endif
  129. #if defined(HAVE_GSSAPI)
  130. char *Curl_sasl_build_gssapi_spn(const char *service, const char *instance);
  131. #endif
  132. #ifndef CURL_DISABLE_CRYPTO_AUTH
  133. /* This is used to extract the realm from a challenge message */
  134. int Curl_sasl_digest_get_pair(const char *str, char *value, char *content,
  135. const char **endptr);
  136. /* This is used to generate a base64 encoded DIGEST-MD5 response message */
  137. CURLcode Curl_sasl_create_digest_md5_message(struct SessionHandle *data,
  138. const char *chlg64,
  139. const char *userp,
  140. const char *passwdp,
  141. const char *service,
  142. char **outptr, size_t *outlen);
  143. /* This is used to decode a HTTP DIGEST challenge message */
  144. CURLcode Curl_sasl_decode_digest_http_message(const char *chlg,
  145. struct digestdata *digest);
  146. /* This is used to generate a HTTP DIGEST response message */
  147. CURLcode Curl_sasl_create_digest_http_message(struct SessionHandle *data,
  148. const char *userp,
  149. const char *passwdp,
  150. const unsigned char *request,
  151. const unsigned char *uri,
  152. struct digestdata *digest,
  153. char **outptr, size_t *outlen);
  154. /* This is used to clean up the digest specific data */
  155. void Curl_sasl_digest_cleanup(struct digestdata *digest);
  156. #endif
  157. #ifdef USE_NTLM
  158. /* This is used to generate a base64 encoded NTLM type-1 message */
  159. CURLcode Curl_sasl_create_ntlm_type1_message(const char *userp,
  160. const char *passwdp,
  161. struct ntlmdata *ntlm,
  162. char **outptr,
  163. size_t *outlen);
  164. /* This is used to decode a base64 encoded NTLM type-2 message */
  165. CURLcode Curl_sasl_decode_ntlm_type2_message(struct SessionHandle *data,
  166. const char *type2msg,
  167. struct ntlmdata *ntlm);
  168. /* This is used to generate a base64 encoded NTLM type-3 message */
  169. CURLcode Curl_sasl_create_ntlm_type3_message(struct SessionHandle *data,
  170. const char *userp,
  171. const char *passwdp,
  172. struct ntlmdata *ntlm,
  173. char **outptr, size_t *outlen);
  174. /* This is used to clean up the ntlm specific data */
  175. void Curl_sasl_ntlm_cleanup(struct ntlmdata *ntlm);
  176. #endif /* USE_NTLM */
  177. #if defined(USE_KERBEROS5)
  178. /* This is used to generate a base64 encoded GSSAPI (Kerberos V5) user token
  179. message */
  180. CURLcode Curl_sasl_create_gssapi_user_message(struct SessionHandle *data,
  181. const char *userp,
  182. const char *passwdp,
  183. const char *service,
  184. const bool mutual,
  185. const char *chlg64,
  186. struct kerberos5data *krb5,
  187. char **outptr, size_t *outlen);
  188. /* This is used to generate a base64 encoded GSSAPI (Kerberos V5) security
  189. token message */
  190. CURLcode Curl_sasl_create_gssapi_security_message(struct SessionHandle *data,
  191. const char *input,
  192. struct kerberos5data *krb5,
  193. char **outptr,
  194. size_t *outlen);
  195. /* This is used to clean up the gssapi specific data */
  196. void Curl_sasl_gssapi_cleanup(struct kerberos5data *krb5);
  197. #endif /* USE_KERBEROS5 */
  198. /* This is used to cleanup any libraries or curl modules used by the sasl
  199. functions */
  200. void Curl_sasl_cleanup(struct connectdata *conn, unsigned int authused);
  201. /* Convert a mechanism name to a token */
  202. unsigned int Curl_sasl_decode_mech(const char *ptr,
  203. size_t maxlen, size_t *len);
  204. /* Parse the URL login options */
  205. CURLcode Curl_sasl_parse_url_auth_option(struct SASL *sasl,
  206. const char *value, size_t len);
  207. /* Initializes an SASL structure */
  208. void Curl_sasl_init(struct SASL *sasl, const struct SASLproto *params);
  209. /* Check if we have enough auth data and capabilities to authenticate */
  210. bool Curl_sasl_can_authenticate(struct SASL *sasl, struct connectdata *conn);
  211. /* Calculate the required login details for SASL authentication */
  212. CURLcode Curl_sasl_start(struct SASL *sasl, struct connectdata *conn,
  213. bool force_ir, saslprogress *progress);
  214. /* Continue an SASL authentication */
  215. CURLcode Curl_sasl_continue(struct SASL *sasl, struct connectdata *conn,
  216. int code, saslprogress *progress);
  217. #endif /* HEADER_CURL_SASL_H */