Imported Upstream version 7.50.2
[platform/upstream/curl.git] / lib / socks_sspi.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2012 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
9  * Copyright (C) 2009, 2011, Markus Moeller, <markus_moeller@compuserve.com>
10  *
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.
14  *
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.
18  *
19  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20  * KIND, either express or implied.
21  *
22  ***************************************************************************/
23
24 #include "curl_setup.h"
25
26 #if defined(USE_WINDOWS_SSPI) && !defined(CURL_DISABLE_PROXY)
27
28 #include "urldata.h"
29 #include "sendf.h"
30 #include "connect.h"
31 #include "strerror.h"
32 #include "timeval.h"
33 #include "socks.h"
34 #include "curl_sspi.h"
35 #include "curl_multibyte.h"
36 #include "warnless.h"
37 #include "strdup.h"
38 /* The last 3 #include files should be in this order */
39 #include "curl_printf.h"
40 #include "curl_memory.h"
41 #include "memdebug.h"
42
43 /*
44  * Helper sspi error functions.
45  */
46 static int check_sspi_err(struct connectdata *conn,
47                           SECURITY_STATUS status,
48                           const char* function)
49 {
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));
56     return 1;
57   }
58   return 0;
59 }
60
61 /* This is the SSPI-using version of this function */
62 CURLcode Curl_SOCKS5_gssapi_negotiate(int sockindex,
63                                       struct connectdata *conn)
64 {
65   struct Curl_easy *data = conn->data;
66   curl_socket_t sock = conn->sock[sockindex];
67   CURLcode code;
68   ssize_t actualread;
69   ssize_t written;
70   int result;
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;
82   TimeStamp expiry;
83   char *service_name = NULL;
84   unsigned short us_length;
85   unsigned long qop;
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";
89
90   /*   GSS-API request looks like
91    * +----+------+-----+----------------+
92    * |VER | MTYP | LEN |     TOKEN      |
93    * +----+------+----------------------+
94    * | 1  |  1   |  2  | up to 2^16 - 1 |
95    * +----+------+-----+----------------+
96    */
97
98   /* prepare service name */
99   if(strchr(service, '/')) {
100     service_name = strdup(service);
101     if(!service_name)
102       return CURLE_OUT_OF_MEMORY;
103   }
104   else {
105     service_name = malloc(strlen(service) + strlen(conn->proxy.name) + 2);
106     if(!service_name)
107       return CURLE_OUT_OF_MEMORY;
108     snprintf(service_name, strlen(service) +strlen(conn->proxy.name)+2,
109              "%s/%s", service, conn->proxy.name);
110   }
111
112   input_desc.cBuffers = 1;
113   input_desc.pBuffers = &sspi_recv_token;
114   input_desc.ulVersion = SECBUFFER_VERSION;
115
116   sspi_recv_token.BufferType = SECBUFFER_TOKEN;
117   sspi_recv_token.cbBuffer = 0;
118   sspi_recv_token.pvBuffer = NULL;
119
120   output_desc.cBuffers = 1;
121   output_desc.pBuffers = &sspi_send_token;
122   output_desc.ulVersion = SECBUFFER_VERSION;
123
124   sspi_send_token.BufferType = SECBUFFER_TOKEN;
125   sspi_send_token.cbBuffer = 0;
126   sspi_send_token.pvBuffer = NULL;
127
128   wrap_desc.cBuffers = 3;
129   wrap_desc.pBuffers = sspi_w_token;
130   wrap_desc.ulVersion = SECBUFFER_VERSION;
131
132   cred_handle.dwLower = 0;
133   cred_handle.dwUpper = 0;
134
135   status = s_pSecFn->AcquireCredentialsHandle(NULL,
136                                               (TCHAR *) TEXT("Kerberos"),
137                                               SECPKG_CRED_OUTBOUND,
138                                               NULL,
139                                               NULL,
140                                               NULL,
141                                               NULL,
142                                               &cred_handle,
143                                               &expiry);
144
145   if(check_sspi_err(conn, status, "AcquireCredentialsHandle")) {
146     failf(data, "Failed to acquire credentials.");
147     free(service_name);
148     s_pSecFn->FreeCredentialsHandle(&cred_handle);
149     return CURLE_COULDNT_CONNECT;
150   }
151
152   /* As long as we need to keep sending some context info, and there's no  */
153   /* errors, keep sending it...                                            */
154   for(;;) {
155     TCHAR *sname;
156
157     sname = Curl_convert_UTF8_to_tchar(service_name);
158     if(!sname)
159       return CURLE_OUT_OF_MEMORY;
160
161     status = s_pSecFn->InitializeSecurityContext(&cred_handle,
162                                                  context_handle,
163                                                  sname,
164                                                  ISC_REQ_MUTUAL_AUTH |
165                                                  ISC_REQ_ALLOCATE_MEMORY |
166                                                  ISC_REQ_CONFIDENTIALITY |
167                                                  ISC_REQ_REPLAY_DETECT,
168                                                  0,
169                                                  SECURITY_NATIVE_DREP,
170                                                  &input_desc,
171                                                  0,
172                                                  &sspi_context,
173                                                  &output_desc,
174                                                  &sspi_ret_flags,
175                                                  &expiry);
176
177     Curl_unicodefree(sname);
178
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;
183     }
184
185     if(check_sspi_err(conn, status, "InitializeSecurityContext")) {
186       free(service_name);
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;
193     }
194
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));
200
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.");
204         free(service_name);
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;
212       }
213
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.");
218         free(service_name);
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;
226       }
227
228     }
229
230     if(sspi_send_token.pvBuffer) {
231       s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
232       sspi_send_token.pvBuffer = NULL;
233     }
234     sspi_send_token.cbBuffer = 0;
235
236     if(sspi_recv_token.pvBuffer) {
237       s_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
238       sspi_recv_token.pvBuffer = NULL;
239     }
240     sspi_recv_token.cbBuffer = 0;
241
242     if(status != SEC_I_CONTINUE_NEEDED)
243       break;
244
245     /* analyse response */
246
247     /*   GSS-API response looks like
248      * +----+------+-----+----------------+
249      * |VER | MTYP | LEN |     TOKEN      |
250      * +----+------+----------------------+
251      * | 1  |  1   |  2  | up to 2^16 - 1 |
252      * +----+------+-----+----------------+
253      */
254
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.");
258       free(service_name);
259       s_pSecFn->FreeCredentialsHandle(&cred_handle);
260       s_pSecFn->DeleteSecurityContext(&sspi_context);
261       return CURLE_COULDNT_CONNECT;
262     }
263
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]);
268       free(service_name);
269       s_pSecFn->FreeCredentialsHandle(&cred_handle);
270       s_pSecFn->DeleteSecurityContext(&sspi_context);
271       return CURLE_COULDNT_CONNECT;
272     }
273
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]);
277       free(service_name);
278       s_pSecFn->FreeCredentialsHandle(&cred_handle);
279       s_pSecFn->DeleteSecurityContext(&sspi_context);
280       return CURLE_COULDNT_CONNECT;
281     }
282
283     memcpy(&us_length, socksreq+2, sizeof(short));
284     us_length = ntohs(us_length);
285
286     sspi_recv_token.cbBuffer = us_length;
287     sspi_recv_token.pvBuffer = malloc(us_length);
288
289     if(!sspi_recv_token.pvBuffer) {
290       free(service_name);
291       s_pSecFn->FreeCredentialsHandle(&cred_handle);
292       s_pSecFn->DeleteSecurityContext(&sspi_context);
293       return CURLE_OUT_OF_MEMORY;
294     }
295     result = Curl_blockread_all(conn, sock, (char *)sspi_recv_token.pvBuffer,
296                                 sspi_recv_token.cbBuffer, &actualread);
297
298     if(result || (actualread != us_length)) {
299       failf(data, "Failed to receive SSPI authentication token.");
300       free(service_name);
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;
306     }
307
308     context_handle = &sspi_context;
309   }
310
311   free(service_name);
312
313   /* Everything is good so far, user was authenticated! */
314   status = s_pSecFn->QueryCredentialsAttributes(&cred_handle,
315                                                 SECPKG_CRED_ATTR_NAMES,
316                                                 &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;
323   }
324   infof(data, "SOCKS5 server authencticated user %s with GSS-API.\n",
325         names.sUserName);
326   s_pSecFn->FreeContextBuffer(names.sUserName);
327
328   /* Do encryption */
329   socksreq[0] = 1;    /* GSS-API subnegotiation version */
330   socksreq[1] = 2;    /* encryption message type */
331
332   gss_enc = 0; /* no data protection */
333   /* do confidentiality protection if supported */
334   if(sspi_ret_flags & ISC_REQ_CONFIDENTIALITY)
335     gss_enc = 2;
336   /* else do integrity protection */
337   else if(sspi_ret_flags & ISC_REQ_INTEGRITY)
338     gss_enc = 1;
339
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 */
343   gss_enc = 0;
344   /*
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
348    * therefore at fault
349    *
350    *  +------+------+------+.......................+
351    *  + ver  | mtyp | len  |   token               |
352    *  +------+------+------+.......................+
353    *  + 0x01 | 0x02 | 0x02 | up to 2^16 - 1 octets |
354    *  +------+------+------+.......................+
355    *
356    *   Where:
357    *
358    *  - "ver" is the protocol version number, here 1 to represent the
359    *    first version of the SOCKS/GSS-API protocol
360    *
361    *  - "mtyp" is the message type, here 2 to represent a protection
362    *    -level negotiation message
363    *
364    *  - "len" is the length of the "token" field in octets
365    *
366    *  - "token" is the GSS-API encapsulated protection level
367    *
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()/
371    * gss_unwrap().
372    *
373    */
374
375   if(data->set.socks5_gssapi_nec) {
376     us_length = htons((short)1);
377     memcpy(socksreq+2, &us_length, sizeof(short));
378   }
379   else {
380     status = s_pSecFn->QueryContextAttributes(&sspi_context,
381                                               SECPKG_ATTR_SIZES,
382                                               &sspi_sizes);
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;
387     }
388
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);
392
393     if(!sspi_w_token[0].pvBuffer) {
394       s_pSecFn->DeleteSecurityContext(&sspi_context);
395       return CURLE_OUT_OF_MEMORY;
396     }
397
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;
404     }
405
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;
415     }
416     status = s_pSecFn->EncryptMessage(&sspi_context,
417                                       KERB_WRAP_NO_ENCRYPT,
418                                       &wrap_desc,
419                                       0);
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;
427     }
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;
438     }
439
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);
448
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;
458
459     us_length = htons((short)sspi_send_token.cbBuffer);
460     memcpy(socksreq+2, &us_length, sizeof(short));
461   }
462
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;
470   }
471
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;
479     }
480   }
481   else {
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;
490     }
491     if(sspi_send_token.pvBuffer)
492       s_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
493   }
494
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;
500   }
501
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;
508   }
509
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;
515   }
516
517   memcpy(&us_length, socksreq+2, sizeof(short));
518   us_length = ntohs(us_length);
519
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;
525   }
526
527   result = Curl_blockread_all(conn, sock, (char *)sspi_w_token[0].pvBuffer,
528                               sspi_w_token[0].cbBuffer, &actualread);
529
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;
535   }
536
537
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;
544
545     status = s_pSecFn->DecryptMessage(&sspi_context,
546                                       &wrap_desc,
547                                       0,
548                                       &qop);
549
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;
558     }
559
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;
569     }
570
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);
574   }
575   else {
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;
582     }
583     memcpy(socksreq, sspi_w_token[0].pvBuffer, sspi_w_token[0].cbBuffer);
584     s_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
585   }
586
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"));
590
591   /* For later use if encryption is required
592      conn->socks5_gssapi_enctype = socksreq[0];
593      if(socksreq[0] != 0)
594        conn->socks5_sspi_context = sspi_context;
595      else {
596        s_pSecFn->DeleteSecurityContext(&sspi_context);
597        conn->socks5_sspi_context = sspi_context;
598      }
599   */
600   return CURLE_OK;
601 }
602 #endif