Coverage Report

Created: 2025-10-10 06:31

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