Coverage Report

Created: 2026-01-25 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/http_ntlm.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM)
27
28
/*
29
 * NTLM details:
30
 *
31
 * https://davenport.sourceforge.net/ntlm.html
32
 * https://www.innovation.ch/java/ntlm.html
33
 */
34
35
#include "urldata.h"
36
#include "curl_trc.h"
37
#include "strcase.h"
38
#include "http_ntlm.h"
39
#include "curl_ntlm_core.h"
40
#include "curlx/base64.h"
41
#include "vauth/vauth.h"
42
#include "url.h"
43
#include "curlx/strparse.h"
44
45
/* SSL backend-specific #if branches in this file must be kept in the order
46
   documented in curl_ntlm_core. */
47
#ifdef USE_WINDOWS_SSPI
48
#include "curl_sspi.h"
49
#endif
50
51
CURLcode Curl_input_ntlm(struct Curl_easy *data,
52
                         bool proxy,         /* if proxy or not */
53
                         const char *header) /* rest of the www-authenticate:
54
                                                header */
55
0
{
56
  /* point to the correct struct with this */
57
0
  curlntlm *state;
58
0
  CURLcode result = CURLE_OK;
59
0
  struct connectdata *conn = data->conn;
60
61
0
  state = proxy ? &conn->proxy_ntlm_state : &conn->http_ntlm_state;
62
63
0
  if(checkprefix("NTLM", header)) {
64
0
    struct ntlmdata *ntlm = Curl_auth_ntlm_get(conn, proxy);
65
0
    if(!ntlm)
66
0
      return CURLE_OUT_OF_MEMORY;
67
68
0
    header += strlen("NTLM");
69
0
    curlx_str_passblanks(&header);
70
0
    if(*header) {
71
0
      unsigned char *hdr;
72
0
      size_t hdrlen;
73
74
0
      result = curlx_base64_decode(header, &hdr, &hdrlen);
75
0
      if(!result) {
76
0
        struct bufref hdrbuf;
77
78
0
        Curl_bufref_init(&hdrbuf);
79
0
        Curl_bufref_set(&hdrbuf, hdr, hdrlen, curl_free);
80
0
        result = Curl_auth_decode_ntlm_type2_message(data, &hdrbuf, ntlm);
81
0
        Curl_bufref_free(&hdrbuf);
82
0
      }
83
0
      if(result)
84
0
        return result;
85
86
0
      *state = NTLMSTATE_TYPE2; /* We got a type-2 message */
87
0
    }
88
0
    else {
89
0
      if(*state == NTLMSTATE_LAST) {
90
0
        infof(data, "NTLM auth restarted");
91
0
        Curl_auth_ntlm_remove(conn, proxy);
92
0
      }
93
0
      else if(*state == NTLMSTATE_TYPE3) {
94
0
        infof(data, "NTLM handshake rejected");
95
0
        Curl_auth_ntlm_remove(conn, proxy);
96
0
        *state = NTLMSTATE_NONE;
97
0
        return CURLE_REMOTE_ACCESS_DENIED;
98
0
      }
99
0
      else if(*state >= NTLMSTATE_TYPE1) {
100
0
        infof(data, "NTLM handshake failure (internal error)");
101
0
        return CURLE_REMOTE_ACCESS_DENIED;
102
0
      }
103
104
0
      *state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
105
0
    }
106
0
  }
107
108
0
  return result;
109
0
}
110
111
/*
112
 * This is for creating NTLM header output
113
 */
114
CURLcode Curl_output_ntlm(struct Curl_easy *data, bool proxy)
115
0
{
116
0
  char *base64 = NULL;
117
0
  size_t len = 0;
118
0
  CURLcode result = CURLE_OK;
119
0
  struct bufref ntlmmsg;
120
121
  /* point to the address of the pointer that holds the string to send to the
122
     server, which is for a plain host or for an HTTP proxy */
123
0
  char **allocuserpwd;
124
125
  /* point to the username, password, service and host */
126
0
  const char *userp;
127
0
  const char *passwdp;
128
0
  const char *service = NULL;
129
0
  const char *hostname = NULL;
130
131
  /* point to the correct struct with this */
132
0
  struct ntlmdata *ntlm;
133
0
  curlntlm *state;
134
0
  struct auth *authp;
135
0
  struct connectdata *conn = data->conn;
136
137
0
  DEBUGASSERT(conn);
138
0
  DEBUGASSERT(data);
139
140
0
  if(proxy) {
141
0
#ifndef CURL_DISABLE_PROXY
142
0
    allocuserpwd = &data->state.aptr.proxyuserpwd;
143
0
    userp = data->state.aptr.proxyuser;
144
0
    passwdp = data->state.aptr.proxypasswd;
145
0
    service = data->set.str[STRING_PROXY_SERVICE_NAME] ?
146
0
              data->set.str[STRING_PROXY_SERVICE_NAME] : "HTTP";
147
0
    hostname = conn->http_proxy.host.name;
148
0
    state = &conn->proxy_ntlm_state;
149
0
    authp = &data->state.authproxy;
150
#else
151
    return CURLE_NOT_BUILT_IN;
152
#endif
153
0
  }
154
0
  else {
155
0
    allocuserpwd = &data->state.aptr.userpwd;
156
0
    userp = data->state.aptr.user;
157
0
    passwdp = data->state.aptr.passwd;
158
0
    service = data->set.str[STRING_SERVICE_NAME] ?
159
0
              data->set.str[STRING_SERVICE_NAME] : "HTTP";
160
0
    hostname = conn->host.name;
161
0
    state = &conn->http_ntlm_state;
162
0
    authp = &data->state.authhost;
163
0
  }
164
0
  ntlm = Curl_auth_ntlm_get(conn, proxy);
165
0
  if(!ntlm)
166
0
    return CURLE_OUT_OF_MEMORY;
167
0
  authp->done = FALSE;
168
169
  /* not set means empty */
170
0
  if(!userp)
171
0
    userp = "";
172
173
0
  if(!passwdp)
174
0
    passwdp = "";
175
176
#ifdef USE_WINDOWS_SSPI
177
  if(!Curl_pSecFn) {
178
    /* not thread-safe and leaks - use curl_global_init() to avoid */
179
    CURLcode err = Curl_sspi_global_init();
180
    if(!Curl_pSecFn)
181
      return err;
182
  }
183
#ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
184
  ntlm->sslContext = conn->sslContext;
185
#endif
186
#endif
187
188
0
  Curl_bufref_init(&ntlmmsg);
189
190
  /* connection is already authenticated, do not send a header in future
191
   * requests so go directly to NTLMSTATE_LAST */
192
0
  if(*state == NTLMSTATE_TYPE3)
193
0
    *state = NTLMSTATE_LAST;
194
195
0
  switch(*state) {
196
0
  case NTLMSTATE_TYPE1:
197
0
  default: /* for the weird cases we (re)start here */
198
    /* Create a type-1 message */
199
0
    result = Curl_auth_create_ntlm_type1_message(data, userp, passwdp, service,
200
0
                                                 hostname, ntlm, &ntlmmsg);
201
0
    if(!result) {
202
0
      DEBUGASSERT(Curl_bufref_len(&ntlmmsg) != 0);
203
0
      result = curlx_base64_encode(Curl_bufref_uptr(&ntlmmsg),
204
0
                                   Curl_bufref_len(&ntlmmsg), &base64, &len);
205
0
      if(!result) {
206
0
        curlx_free(*allocuserpwd);
207
0
        *allocuserpwd = curl_maprintf("%sAuthorization: NTLM %s\r\n",
208
0
                                      proxy ? "Proxy-" : "", base64);
209
0
        curlx_free(base64);
210
0
        if(!*allocuserpwd)
211
0
          result = CURLE_OUT_OF_MEMORY;
212
0
      }
213
0
    }
214
0
    break;
215
216
0
  case NTLMSTATE_TYPE2:
217
    /* We already received the type-2 message, create a type-3 message */
218
0
    result = Curl_auth_create_ntlm_type3_message(data, userp, passwdp,
219
0
                                                 ntlm, &ntlmmsg);
220
0
    if(!result && Curl_bufref_len(&ntlmmsg)) {
221
0
      result = curlx_base64_encode(Curl_bufref_uptr(&ntlmmsg),
222
0
                                   Curl_bufref_len(&ntlmmsg), &base64, &len);
223
0
      if(!result) {
224
0
        curlx_free(*allocuserpwd);
225
0
        *allocuserpwd = curl_maprintf("%sAuthorization: NTLM %s\r\n",
226
0
                                      proxy ? "Proxy-" : "", base64);
227
0
        curlx_free(base64);
228
0
        if(!*allocuserpwd)
229
0
          result = CURLE_OUT_OF_MEMORY;
230
0
        else {
231
0
          *state = NTLMSTATE_TYPE3; /* we send a type-3 */
232
0
          authp->done = TRUE;
233
0
        }
234
0
      }
235
0
    }
236
0
    break;
237
238
0
  case NTLMSTATE_LAST:
239
    /* since this is a little artificial in that this is used without any
240
       outgoing auth headers being set, we need to set the bit by force */
241
0
    if(proxy)
242
0
      data->info.proxyauthpicked = CURLAUTH_NTLM;
243
0
    else
244
0
      data->info.httpauthpicked = CURLAUTH_NTLM;
245
0
    Curl_safefree(*allocuserpwd);
246
0
    authp->done = TRUE;
247
0
    break;
248
0
  }
249
0
  Curl_bufref_free(&ntlmmsg);
250
251
0
  return result;
252
0
}
253
254
#endif /* !CURL_DISABLE_HTTP && USE_NTLM */