1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2012 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
9 * Copyright (C) 2009, 2011, Markus Moeller, <markus_moeller@compuserve.com>
11 * This software is licensed as described in the file COPYING, which
12 * you should have received as part of this distribution. The terms
13 * are also available at https://curl.haxx.se/docs/copyright.html.
15 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
16 * copies of the Software, and permit persons to whom the Software is
17 * furnished to do so, under the terms of the COPYING file.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ***************************************************************************/
24 #include "curl_setup.h"
26 #if defined(USE_WINDOWS_SSPI) && !defined(CURL_DISABLE_PROXY)
34 #include "curl_sspi.h"
35 #include "curl_multibyte.h"
38 /* The last 3 #include files should be in this order */
39 #include "curl_printf.h"
40 #include "curl_memory.h"
44 * Helper sspi error functions.
46 static int check_sspi_err(struct connectdata *conn,
47 SECURITY_STATUS status,
50 if(status != SEC_E_OK &&
51 status != SEC_I_COMPLETE_AND_CONTINUE &&
52 status != SEC_I_COMPLETE_NEEDED &&
53 status != SEC_I_CONTINUE_NEEDED) {
54 failf(conn->data, "SSPI error: %s failed: %s", function,
55 Curl_sspi_strerror(conn, status));
61 /* This is the SSPI-using version of this function */
62 CURLcode Curl_SOCKS5_gssapi_negotiate(int sockindex,
63 struct connectdata *conn)
65 struct Curl_easy *data = conn->data;
66 curl_socket_t sock = conn->sock[sockindex];
71 /* Needs GSS-API authentication */
72 SECURITY_STATUS status;
73 unsigned long sspi_ret_flags = 0;
74 unsigned char gss_enc;
75 SecBuffer sspi_send_token, sspi_recv_token, sspi_w_token[3];
76 SecBufferDesc input_desc, output_desc, wrap_desc;
77 SecPkgContext_Sizes sspi_sizes;
78 CredHandle cred_handle;
79 CtxtHandle sspi_context;
80 PCtxtHandle context_handle = NULL;
81 SecPkgCredentials_Names names;
83 char *service_name = NULL;
84 unsigned short us_length;
86 unsigned char socksreq[4]; /* room for GSS-API exchange header only */
87 const char *service = data->set.str[STRING_PROXY_SERVICE_NAME] ?
88 data->set.str[STRING_PROXY_SERVICE_NAME] : "rcmd";
90 /* GSS-API request looks like
91 * +----+------+-----+----------------+
92 * |VER | MTYP | LEN | TOKEN |
93 * +----+------+----------------------+
94 * | 1 | 1 | 2 | up to 2^16 - 1 |
95 * +----+------+-----+----------------+
98 /* prepare service name */
99 if(strchr(service, '/')) {
100 service_name = strdup(service);
102 return CURLE_OUT_OF_MEMORY;
105 service_name = malloc(strlen(service) + strlen(conn->proxy.name) + 2);
107 return CURLE_OUT_OF_MEMORY;
108 snprintf(service_name, strlen(service) +strlen(conn->proxy.name)+2,
109 "%s/%s", service, conn->proxy.name);
112 input_desc.cBuffers = 1;
113 input_desc.pBuffers = &sspi_recv_token;
114 input_desc.ulVersion = SECBUFFER_VERSION;
116 sspi_recv_token.BufferType = SECBUFFER_TOKEN;
117 sspi_recv_token.cbBuffer = 0;
118 sspi_recv_token.pvBuffer = NULL;
120 output_desc.cBuffers = 1;
121 output_desc.pBuffers = &sspi_send_token;
122 output_desc.ulVersion = SECBUFFER_VERSION;
124 sspi_send_token.BufferType = SECBUFFER_TOKEN;
125 sspi_send_token.cbBuffer = 0;
126 sspi_send_token.pvBuffer = NULL;
128 wrap_desc.cBuffers = 3;
129 wrap_desc.pBuffers = sspi_w_token;
130 wrap_desc.ulVersion = SECBUFFER_VERSION;
132 cred_handle.dwLower = 0;
133 cred_handle.dwUpper = 0;
135 status = s_pSecFn->AcquireCredentialsHandle(NULL,
136 (TCHAR *) TEXT("Kerberos"),
137 SECPKG_CRED_OUTBOUND,
145 if(check_sspi_err(conn, status, "AcquireCredentialsHandle")) {
146 failf(data, "Failed to acquire credentials.");
148 s_pSecFn->FreeCredentialsHandle(&cred_handle);
149 return CURLE_COULDNT_CONNECT;
152 /* As long as we need to keep sending some context info, and there's no */
153 /* errors, keep sending it... */
157 sname = Curl_convert_UTF8_to_tchar(service_name);
159 return CURLE_OUT_OF_MEMORY;
161 status = s_pSecFn->InitializeSecurityContext(&cred_handle,
164 ISC_REQ_MUTUAL_AUTH |
165 ISC_REQ_ALLOCATE_MEMORY |
166 ISC_REQ_CONFIDENTIALITY |
167 ISC_REQ_REPLAY_DETECT,
169 SECURITY_NATIVE_DREP,
177 Curl_unicodefree(sname);
179 if(sspi_recv_token.pvBuffer) {
180 s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
181 sspi_recv_token.pvBuffer = NULL;
182 sspi_recv_token.cbBuffer = 0;
185 if(check_sspi_err(conn, status, "InitializeSecurityContext")) {
187 s_pSecFn->FreeCredentialsHandle(&cred_handle);
188 s_pSecFn->DeleteSecurityContext(&sspi_context);
189 if(sspi_recv_token.pvBuffer)
190 s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
191 failf(data, "Failed to initialise security context.");
192 return CURLE_COULDNT_CONNECT;
195 if(sspi_send_token.cbBuffer != 0) {
196 socksreq[0] = 1; /* GSS-API subnegotiation version */
197 socksreq[1] = 1; /* authentication message type */
198 us_length = htons((short)sspi_send_token.cbBuffer);
199 memcpy(socksreq+2, &us_length, sizeof(short));
201 code = Curl_write_plain(conn, sock, (char *)socksreq, 4, &written);
202 if(code || (4 != written)) {
203 failf(data, "Failed to send SSPI authentication request.");
205 if(sspi_send_token.pvBuffer)
206 s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
207 if(sspi_recv_token.pvBuffer)
208 s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
209 s_pSecFn->FreeCredentialsHandle(&cred_handle);
210 s_pSecFn->DeleteSecurityContext(&sspi_context);
211 return CURLE_COULDNT_CONNECT;
214 code = Curl_write_plain(conn, sock, (char *)sspi_send_token.pvBuffer,
215 sspi_send_token.cbBuffer, &written);
216 if(code || (sspi_send_token.cbBuffer != (size_t)written)) {
217 failf(data, "Failed to send SSPI authentication token.");
219 if(sspi_send_token.pvBuffer)
220 s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
221 if(sspi_recv_token.pvBuffer)
222 s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
223 s_pSecFn->FreeCredentialsHandle(&cred_handle);
224 s_pSecFn->DeleteSecurityContext(&sspi_context);
225 return CURLE_COULDNT_CONNECT;
230 if(sspi_send_token.pvBuffer) {
231 s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
232 sspi_send_token.pvBuffer = NULL;
234 sspi_send_token.cbBuffer = 0;
236 if(sspi_recv_token.pvBuffer) {
237 s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
238 sspi_recv_token.pvBuffer = NULL;
240 sspi_recv_token.cbBuffer = 0;
242 if(status != SEC_I_CONTINUE_NEEDED)
245 /* analyse response */
247 /* GSS-API response looks like
248 * +----+------+-----+----------------+
249 * |VER | MTYP | LEN | TOKEN |
250 * +----+------+----------------------+
251 * | 1 | 1 | 2 | up to 2^16 - 1 |
252 * +----+------+-----+----------------+
255 result = Curl_blockread_all(conn, sock, (char *)socksreq, 4, &actualread);
256 if(result || (actualread != 4)) {
257 failf(data, "Failed to receive SSPI authentication response.");
259 s_pSecFn->FreeCredentialsHandle(&cred_handle);
260 s_pSecFn->DeleteSecurityContext(&sspi_context);
261 return CURLE_COULDNT_CONNECT;
264 /* ignore the first (VER) byte */
265 if(socksreq[1] == 255) { /* status / message type */
266 failf(data, "User was rejected by the SOCKS5 server (%u %u).",
267 (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
269 s_pSecFn->FreeCredentialsHandle(&cred_handle);
270 s_pSecFn->DeleteSecurityContext(&sspi_context);
271 return CURLE_COULDNT_CONNECT;
274 if(socksreq[1] != 1) { /* status / messgae type */
275 failf(data, "Invalid SSPI authentication response type (%u %u).",
276 (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
278 s_pSecFn->FreeCredentialsHandle(&cred_handle);
279 s_pSecFn->DeleteSecurityContext(&sspi_context);
280 return CURLE_COULDNT_CONNECT;
283 memcpy(&us_length, socksreq+2, sizeof(short));
284 us_length = ntohs(us_length);
286 sspi_recv_token.cbBuffer = us_length;
287 sspi_recv_token.pvBuffer = malloc(us_length);
289 if(!sspi_recv_token.pvBuffer) {
291 s_pSecFn->FreeCredentialsHandle(&cred_handle);
292 s_pSecFn->DeleteSecurityContext(&sspi_context);
293 return CURLE_OUT_OF_MEMORY;
295 result = Curl_blockread_all(conn, sock, (char *)sspi_recv_token.pvBuffer,
296 sspi_recv_token.cbBuffer, &actualread);
298 if(result || (actualread != us_length)) {
299 failf(data, "Failed to receive SSPI authentication token.");
301 if(sspi_recv_token.pvBuffer)
302 s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
303 s_pSecFn->FreeCredentialsHandle(&cred_handle);
304 s_pSecFn->DeleteSecurityContext(&sspi_context);
305 return CURLE_COULDNT_CONNECT;
308 context_handle = &sspi_context;
313 /* Everything is good so far, user was authenticated! */
314 status = s_pSecFn->QueryCredentialsAttributes(&cred_handle,
315 SECPKG_CRED_ATTR_NAMES,
317 s_pSecFn->FreeCredentialsHandle(&cred_handle);
318 if(check_sspi_err(conn, status, "QueryCredentialAttributes")) {
319 s_pSecFn->DeleteSecurityContext(&sspi_context);
320 s_pSecFn->FreeContextBuffer(names.sUserName);
321 failf(data, "Failed to determine user name.");
322 return CURLE_COULDNT_CONNECT;
324 infof(data, "SOCKS5 server authencticated user %s with GSS-API.\n",
326 s_pSecFn->FreeContextBuffer(names.sUserName);
329 socksreq[0] = 1; /* GSS-API subnegotiation version */
330 socksreq[1] = 2; /* encryption message type */
332 gss_enc = 0; /* no data protection */
333 /* do confidentiality protection if supported */
334 if(sspi_ret_flags & ISC_REQ_CONFIDENTIALITY)
336 /* else do integrity protection */
337 else if(sspi_ret_flags & ISC_REQ_INTEGRITY)
340 infof(data, "SOCKS5 server supports GSS-API %s data protection.\n",
341 (gss_enc==0)?"no":((gss_enc==1)?"integrity":"confidentiality") );
342 /* force to no data protection, avoid encryption/decryption for now */
345 * Sending the encryption type in clear seems wrong. It should be
346 * protected with gss_seal()/gss_wrap(). See RFC1961 extract below
347 * The NEC reference implementations on which this is based is
350 * +------+------+------+.......................+
351 * + ver | mtyp | len | token |
352 * +------+------+------+.......................+
353 * + 0x01 | 0x02 | 0x02 | up to 2^16 - 1 octets |
354 * +------+------+------+.......................+
358 * - "ver" is the protocol version number, here 1 to represent the
359 * first version of the SOCKS/GSS-API protocol
361 * - "mtyp" is the message type, here 2 to represent a protection
362 * -level negotiation message
364 * - "len" is the length of the "token" field in octets
366 * - "token" is the GSS-API encapsulated protection level
368 * The token is produced by encapsulating an octet containing the
369 * required protection level using gss_seal()/gss_wrap() with conf_req
370 * set to FALSE. The token is verified using gss_unseal()/
375 if(data->set.socks5_gssapi_nec) {
376 us_length = htons((short)1);
377 memcpy(socksreq+2, &us_length, sizeof(short));
380 status = s_pSecFn->QueryContextAttributes(&sspi_context,
383 if(check_sspi_err(conn, status, "QueryContextAttributes")) {
384 s_pSecFn->DeleteSecurityContext(&sspi_context);
385 failf(data, "Failed to query security context attributes.");
386 return CURLE_COULDNT_CONNECT;
389 sspi_w_token[0].cbBuffer = sspi_sizes.cbSecurityTrailer;
390 sspi_w_token[0].BufferType = SECBUFFER_TOKEN;
391 sspi_w_token[0].pvBuffer = malloc(sspi_sizes.cbSecurityTrailer);
393 if(!sspi_w_token[0].pvBuffer) {
394 s_pSecFn->DeleteSecurityContext(&sspi_context);
395 return CURLE_OUT_OF_MEMORY;
398 sspi_w_token[1].cbBuffer = 1;
399 sspi_w_token[1].pvBuffer = malloc(1);
400 if(!sspi_w_token[1].pvBuffer) {
401 s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
402 s_pSecFn->DeleteSecurityContext(&sspi_context);
403 return CURLE_OUT_OF_MEMORY;
406 memcpy(sspi_w_token[1].pvBuffer, &gss_enc, 1);
407 sspi_w_token[2].BufferType = SECBUFFER_PADDING;
408 sspi_w_token[2].cbBuffer = sspi_sizes.cbBlockSize;
409 sspi_w_token[2].pvBuffer = malloc(sspi_sizes.cbBlockSize);
410 if(!sspi_w_token[2].pvBuffer) {
411 s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
412 s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
413 s_pSecFn->DeleteSecurityContext(&sspi_context);
414 return CURLE_OUT_OF_MEMORY;
416 status = s_pSecFn->EncryptMessage(&sspi_context,
417 KERB_WRAP_NO_ENCRYPT,
420 if(check_sspi_err(conn, status, "EncryptMessage")) {
421 s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
422 s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
423 s_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
424 s_pSecFn->DeleteSecurityContext(&sspi_context);
425 failf(data, "Failed to query security context attributes.");
426 return CURLE_COULDNT_CONNECT;
428 sspi_send_token.cbBuffer = sspi_w_token[0].cbBuffer
429 + sspi_w_token[1].cbBuffer
430 + sspi_w_token[2].cbBuffer;
431 sspi_send_token.pvBuffer = malloc(sspi_send_token.cbBuffer);
432 if(!sspi_send_token.pvBuffer) {
433 s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
434 s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
435 s_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
436 s_pSecFn->DeleteSecurityContext(&sspi_context);
437 return CURLE_OUT_OF_MEMORY;
440 memcpy(sspi_send_token.pvBuffer, sspi_w_token[0].pvBuffer,
441 sspi_w_token[0].cbBuffer);
442 memcpy((PUCHAR) sspi_send_token.pvBuffer +(int)sspi_w_token[0].cbBuffer,
443 sspi_w_token[1].pvBuffer, sspi_w_token[1].cbBuffer);
444 memcpy((PUCHAR) sspi_send_token.pvBuffer
445 +sspi_w_token[0].cbBuffer
446 +sspi_w_token[1].cbBuffer,
447 sspi_w_token[2].pvBuffer, sspi_w_token[2].cbBuffer);
449 s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
450 sspi_w_token[0].pvBuffer = NULL;
451 sspi_w_token[0].cbBuffer = 0;
452 s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
453 sspi_w_token[1].pvBuffer = NULL;
454 sspi_w_token[1].cbBuffer = 0;
455 s_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
456 sspi_w_token[2].pvBuffer = NULL;
457 sspi_w_token[2].cbBuffer = 0;
459 us_length = htons((short)sspi_send_token.cbBuffer);
460 memcpy(socksreq+2, &us_length, sizeof(short));
463 code = Curl_write_plain(conn, sock, (char *)socksreq, 4, &written);
464 if(code || (4 != written)) {
465 failf(data, "Failed to send SSPI encryption request.");
466 if(sspi_send_token.pvBuffer)
467 s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
468 s_pSecFn->DeleteSecurityContext(&sspi_context);
469 return CURLE_COULDNT_CONNECT;
472 if(data->set.socks5_gssapi_nec) {
473 memcpy(socksreq, &gss_enc, 1);
474 code = Curl_write_plain(conn, sock, (char *)socksreq, 1, &written);
475 if(code || (1 != written)) {
476 failf(data, "Failed to send SSPI encryption type.");
477 s_pSecFn->DeleteSecurityContext(&sspi_context);
478 return CURLE_COULDNT_CONNECT;
482 code = Curl_write_plain(conn, sock, (char *)sspi_send_token.pvBuffer,
483 sspi_send_token.cbBuffer, &written);
484 if(code || (sspi_send_token.cbBuffer != (size_t)written)) {
485 failf(data, "Failed to send SSPI encryption type.");
486 if(sspi_send_token.pvBuffer)
487 s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
488 s_pSecFn->DeleteSecurityContext(&sspi_context);
489 return CURLE_COULDNT_CONNECT;
491 if(sspi_send_token.pvBuffer)
492 s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
495 result = Curl_blockread_all(conn, sock, (char *)socksreq, 4, &actualread);
496 if(result || (actualread != 4)) {
497 failf(data, "Failed to receive SSPI encryption response.");
498 s_pSecFn->DeleteSecurityContext(&sspi_context);
499 return CURLE_COULDNT_CONNECT;
502 /* ignore the first (VER) byte */
503 if(socksreq[1] == 255) { /* status / message type */
504 failf(data, "User was rejected by the SOCKS5 server (%u %u).",
505 (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
506 s_pSecFn->DeleteSecurityContext(&sspi_context);
507 return CURLE_COULDNT_CONNECT;
510 if(socksreq[1] != 2) { /* status / message type */
511 failf(data, "Invalid SSPI encryption response type (%u %u).",
512 (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
513 s_pSecFn->DeleteSecurityContext(&sspi_context);
514 return CURLE_COULDNT_CONNECT;
517 memcpy(&us_length, socksreq+2, sizeof(short));
518 us_length = ntohs(us_length);
520 sspi_w_token[0].cbBuffer = us_length;
521 sspi_w_token[0].pvBuffer = malloc(us_length);
522 if(!sspi_w_token[0].pvBuffer) {
523 s_pSecFn->DeleteSecurityContext(&sspi_context);
524 return CURLE_OUT_OF_MEMORY;
527 result = Curl_blockread_all(conn, sock, (char *)sspi_w_token[0].pvBuffer,
528 sspi_w_token[0].cbBuffer, &actualread);
530 if(result || (actualread != us_length)) {
531 failf(data, "Failed to receive SSPI encryption type.");
532 s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
533 s_pSecFn->DeleteSecurityContext(&sspi_context);
534 return CURLE_COULDNT_CONNECT;
538 if(!data->set.socks5_gssapi_nec) {
539 wrap_desc.cBuffers = 2;
540 sspi_w_token[0].BufferType = SECBUFFER_STREAM;
541 sspi_w_token[1].BufferType = SECBUFFER_DATA;
542 sspi_w_token[1].cbBuffer = 0;
543 sspi_w_token[1].pvBuffer = NULL;
545 status = s_pSecFn->DecryptMessage(&sspi_context,
550 if(check_sspi_err(conn, status, "DecryptMessage")) {
551 if(sspi_w_token[0].pvBuffer)
552 s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
553 if(sspi_w_token[1].pvBuffer)
554 s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
555 s_pSecFn->DeleteSecurityContext(&sspi_context);
556 failf(data, "Failed to query security context attributes.");
557 return CURLE_COULDNT_CONNECT;
560 if(sspi_w_token[1].cbBuffer != 1) {
561 failf(data, "Invalid SSPI encryption response length (%lu).",
562 (unsigned long)sspi_w_token[1].cbBuffer);
563 if(sspi_w_token[0].pvBuffer)
564 s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
565 if(sspi_w_token[1].pvBuffer)
566 s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
567 s_pSecFn->DeleteSecurityContext(&sspi_context);
568 return CURLE_COULDNT_CONNECT;
571 memcpy(socksreq, sspi_w_token[1].pvBuffer, sspi_w_token[1].cbBuffer);
572 s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
573 s_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
576 if(sspi_w_token[0].cbBuffer != 1) {
577 failf(data, "Invalid SSPI encryption response length (%lu).",
578 (unsigned long)sspi_w_token[0].cbBuffer);
579 s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
580 s_pSecFn->DeleteSecurityContext(&sspi_context);
581 return CURLE_COULDNT_CONNECT;
583 memcpy(socksreq, sspi_w_token[0].pvBuffer, sspi_w_token[0].cbBuffer);
584 s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
587 infof(data, "SOCKS5 access with%s protection granted.\n",
588 (socksreq[0]==0)?"out GSS-API data":
589 ((socksreq[0]==1)?" GSS-API integrity":" GSS-API confidentiality"));
591 /* For later use if encryption is required
592 conn->socks5_gssapi_enctype = socksreq[0];
594 conn->socks5_sspi_context = sspi_context;
596 s_pSecFn->DeleteSecurityContext(&sspi_context);
597 conn->socks5_sspi_context = sspi_context;