1#ifndef HEADER_CURL_SASL_H
2#define HEADER_CURL_SASL_H
3/***************************************************************************
4 * _ _ ____ _
5 * Project ___| | | | _ \| |
6 * / __| | | | |_) | |
7 * | (__| |_| | _ <| |___
8 * \___|\___/|_| \_\_____|
9 *
10 * Copyright (C) 2012 - 2021, 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.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
25#include <curl/curl.h>
26
27struct Curl_easy;
28struct connectdata;
29
30/* Authentication mechanism flags */
31#define SASL_MECH_LOGIN (1 << 0)
32#define SASL_MECH_PLAIN (1 << 1)
33#define SASL_MECH_CRAM_MD5 (1 << 2)
34#define SASL_MECH_DIGEST_MD5 (1 << 3)
35#define SASL_MECH_GSSAPI (1 << 4)
36#define SASL_MECH_EXTERNAL (1 << 5)
37#define SASL_MECH_NTLM (1 << 6)
38#define SASL_MECH_XOAUTH2 (1 << 7)
39#define SASL_MECH_OAUTHBEARER (1 << 8)
40#define SASL_MECH_SCRAM_SHA_1 (1 << 9)
41#define SASL_MECH_SCRAM_SHA_256 (1 << 10)
42
43/* Authentication mechanism values */
44#define SASL_AUTH_NONE 0
45#define SASL_AUTH_ANY 0xffff
46#define SASL_AUTH_DEFAULT (SASL_AUTH_ANY & ~SASL_MECH_EXTERNAL)
47
48/* Authentication mechanism strings */
49#define SASL_MECH_STRING_LOGIN "LOGIN"
50#define SASL_MECH_STRING_PLAIN "PLAIN"
51#define SASL_MECH_STRING_CRAM_MD5 "CRAM-MD5"
52#define SASL_MECH_STRING_DIGEST_MD5 "DIGEST-MD5"
53#define SASL_MECH_STRING_GSSAPI "GSSAPI"
54#define SASL_MECH_STRING_EXTERNAL "EXTERNAL"
55#define SASL_MECH_STRING_NTLM "NTLM"
56#define SASL_MECH_STRING_XOAUTH2 "XOAUTH2"
57#define SASL_MECH_STRING_OAUTHBEARER "OAUTHBEARER"
58#define SASL_MECH_STRING_SCRAM_SHA_1 "SCRAM-SHA-1"
59#define SASL_MECH_STRING_SCRAM_SHA_256 "SCRAM-SHA-256"
60
61/* SASL machine states */
62typedef enum {
63 SASL_STOP,
64 SASL_PLAIN,
65 SASL_LOGIN,
66 SASL_LOGIN_PASSWD,
67 SASL_EXTERNAL,
68 SASL_CRAMMD5,
69 SASL_DIGESTMD5,
70 SASL_DIGESTMD5_RESP,
71 SASL_NTLM,
72 SASL_NTLM_TYPE2MSG,
73 SASL_GSSAPI,
74 SASL_GSSAPI_TOKEN,
75 SASL_GSSAPI_NO_DATA,
76 SASL_OAUTH2,
77 SASL_OAUTH2_RESP,
78 SASL_GSASL,
79 SASL_CANCEL,
80 SASL_FINAL
81} saslstate;
82
83/* Progress indicator */
84typedef enum {
85 SASL_IDLE,
86 SASL_INPROGRESS,
87 SASL_DONE
88} saslprogress;
89
90/* Protocol dependent SASL parameters */
91struct SASLproto {
92 const char *service; /* The service name */
93 int contcode; /* Code to receive when continuation is expected */
94 int finalcode; /* Code to receive upon authentication success */
95 size_t maxirlen; /* Maximum initial response length */
96 CURLcode (*sendauth)(struct Curl_easy *data,
97 struct connectdata *conn,
98 const char *mech, const char *ir);
99 /* Send authentication command */
100 CURLcode (*sendcont)(struct Curl_easy *data,
101 struct connectdata *conn, const char *contauth);
102 /* Send authentication continuation */
103 void (*getmessage)(char *buffer, char **outptr);
104 /* Get SASL response message */
105};
106
107/* Per-connection parameters */
108struct SASL {
109 const struct SASLproto *params; /* Protocol dependent parameters */
110 saslstate state; /* Current machine state */
111 unsigned short authmechs; /* Accepted authentication mechanisms */
112 unsigned short prefmech; /* Preferred authentication mechanism */
113 unsigned short authused; /* Auth mechanism used for the connection */
114 bool resetprefs; /* For URL auth option parsing. */
115 bool mutual_auth; /* Mutual authentication enabled (GSSAPI only) */
116 bool force_ir; /* Protocol always supports initial response */
117};
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
124/* This is used to cleanup any libraries or curl modules used by the sasl
125 functions */
126void Curl_sasl_cleanup(struct connectdata *conn, unsigned int authused);
127
128/* Convert a mechanism name to a token */
129unsigned short Curl_sasl_decode_mech(const char *ptr,
130 size_t maxlen, size_t *len);
131
132/* Parse the URL login options */
133CURLcode Curl_sasl_parse_url_auth_option(struct SASL *sasl,
134 const char *value, size_t len);
135
136/* Initializes an SASL structure */
137void Curl_sasl_init(struct SASL *sasl, const struct SASLproto *params);
138
139/* Check if we have enough auth data and capabilities to authenticate */
140bool Curl_sasl_can_authenticate(struct SASL *sasl, struct connectdata *conn);
141
142/* Calculate the required login details for SASL authentication */
143CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
144 struct connectdata *conn,
145 bool force_ir, saslprogress *progress);
146
147/* Continue an SASL authentication */
148CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
149 struct connectdata *conn,
150 int code, saslprogress *progress);
151
152#endif /* HEADER_CURL_SASL_H */
153