[SECUR32]
[reactos.git] / reactos / dll / win32 / schannel / schannel_wine.c
1 /* Copyright (C) 2005 Juan Lang
2 * Copyright 2008 Henri Verbeet
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 *
18 * This file implements the schannel provider, or, the SSL/TLS implementations.
19 */
20
21 #include "precomp.h"
22
23 #include <wine/config.h>
24
25 #if defined(SONAME_LIBGNUTLS) || defined (HAVE_SECURITY_SECURITY_H)
26
27 #define SCHAN_INVALID_HANDLE ~0UL
28
29 enum schan_handle_type
30 {
31 SCHAN_HANDLE_CRED,
32 SCHAN_HANDLE_CTX,
33 SCHAN_HANDLE_FREE
34 };
35
36 struct schan_handle
37 {
38 void *object;
39 enum schan_handle_type type;
40 };
41
42 struct schan_context
43 {
44 schan_imp_session session;
45 ULONG req_ctx_attr;
46 const CERT_CONTEXT *cert;
47 };
48
49 static struct schan_handle *schan_handle_table;
50 static struct schan_handle *schan_free_handles;
51 static SIZE_T schan_handle_table_size;
52 static SIZE_T schan_handle_count;
53
54 /* Protocols enabled, only those may be used for the connection. */
55 static DWORD config_enabled_protocols;
56
57 /* Protocols disabled by default. They are enabled for using, but disabled when caller asks for default settings. */
58 static DWORD config_default_disabled_protocols;
59
60 static ULONG_PTR schan_alloc_handle(void *object, enum schan_handle_type type)
61 {
62 struct schan_handle *handle;
63
64 if (schan_free_handles)
65 {
66 DWORD index = schan_free_handles - schan_handle_table;
67 /* Use a free handle */
68 handle = schan_free_handles;
69 if (handle->type != SCHAN_HANDLE_FREE)
70 {
71 ERR("Handle %d(%p) is in the free list, but has type %#x.\n", index, handle, handle->type);
72 return SCHAN_INVALID_HANDLE;
73 }
74 schan_free_handles = handle->object;
75 handle->object = object;
76 handle->type = type;
77
78 return index;
79 }
80 if (!(schan_handle_count < schan_handle_table_size))
81 {
82 /* Grow the table */
83 SIZE_T new_size = schan_handle_table_size + (schan_handle_table_size >> 1);
84 struct schan_handle *new_table = HeapReAlloc(GetProcessHeap(), 0, schan_handle_table, new_size * sizeof(*schan_handle_table));
85 if (!new_table)
86 {
87 ERR("Failed to grow the handle table\n");
88 return SCHAN_INVALID_HANDLE;
89 }
90 schan_handle_table = new_table;
91 schan_handle_table_size = new_size;
92 }
93
94 handle = &schan_handle_table[schan_handle_count++];
95 handle->object = object;
96 handle->type = type;
97
98 return handle - schan_handle_table;
99 }
100
101 static void *schan_free_handle(ULONG_PTR handle_idx, enum schan_handle_type type)
102 {
103 struct schan_handle *handle;
104 void *object;
105
106 if (handle_idx == SCHAN_INVALID_HANDLE) return NULL;
107 if (handle_idx >= schan_handle_count) return NULL;
108 handle = &schan_handle_table[handle_idx];
109 if (handle->type != type)
110 {
111 ERR("Handle %ld(%p) is not of type %#x\n", handle_idx, handle, type);
112 return NULL;
113 }
114
115 object = handle->object;
116 handle->object = schan_free_handles;
117 handle->type = SCHAN_HANDLE_FREE;
118 schan_free_handles = handle;
119
120 return object;
121 }
122
123 static void *schan_get_object(ULONG_PTR handle_idx, enum schan_handle_type type)
124 {
125 struct schan_handle *handle;
126
127 if (handle_idx == SCHAN_INVALID_HANDLE) return NULL;
128 if (handle_idx >= schan_handle_count) return NULL;
129 handle = &schan_handle_table[handle_idx];
130 if (handle->type != type)
131 {
132 ERR("Handle %ld(%p) is not of type %#x\n", handle_idx, handle, type);
133 return NULL;
134 }
135
136 return handle->object;
137 }
138
139 static void read_config(void)
140 {
141 DWORD enabled = 0, default_disabled = 0;
142 HKEY protocols_key, key;
143 WCHAR subkey_name[64];
144 unsigned i;
145 DWORD res;
146
147 static BOOL config_read = FALSE;
148
149 static const WCHAR protocol_config_key_name[] = {
150 'S','Y','S','T','E','M','\\',
151 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
152 'C','o','n','t','r','o','l','\\',
153 'S','e','c','u','r','i','t','y','P','r','o','v','i','d','e','r','s','\\',
154 'S','C','H','A','N','N','E','L','\\',
155 'P','r','o','t','o','c','o','l','s',0 };
156
157 static const WCHAR clientW[] = {'\\','C','l','i','e','n','t',0};
158 static const WCHAR enabledW[] = {'e','n','a','b','l','e','d',0};
159 static const WCHAR disabledbydefaultW[] = {'D','i','s','a','b','l','e','d','B','y','D','e','f','a','u','l','t',0};
160
161 static const struct {
162 WCHAR key_name[20];
163 DWORD prot_client_flag;
164 BOOL enabled; /* If no config is present, enable the protocol */
165 BOOL disabled_by_default; /* Disable if caller asks for default protocol set */
166 } protocol_config_keys[] = {
167 {{'S','S','L',' ','2','.','0',0}, SP_PROT_SSL2_CLIENT, FALSE, TRUE}, /* NOTE: TRUE, TRUE on Windows */
168 {{'S','S','L',' ','3','.','0',0}, SP_PROT_SSL3_CLIENT, TRUE, FALSE},
169 {{'T','L','S',' ','1','.','0',0}, SP_PROT_TLS1_0_CLIENT, TRUE, FALSE},
170 {{'T','L','S',' ','1','.','1',0}, SP_PROT_TLS1_1_CLIENT, TRUE, FALSE /* NOTE: not enabled by default on Windows */ },
171 {{'T','L','S',' ','1','.','2',0}, SP_PROT_TLS1_2_CLIENT, TRUE, FALSE /* NOTE: not enabled by default on Windows */ }
172 };
173
174 /* No need for thread safety */
175 if(config_read)
176 return;
177
178 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, protocol_config_key_name, 0, KEY_READ, &protocols_key);
179 if(res == ERROR_SUCCESS) {
180 DWORD type, size, value;
181
182 for(i=0; i < sizeof(protocol_config_keys)/sizeof(*protocol_config_keys); i++) {
183 strcpyW(subkey_name, protocol_config_keys[i].key_name);
184 strcatW(subkey_name, clientW);
185 res = RegOpenKeyExW(protocols_key, subkey_name, 0, KEY_READ, &key);
186 if(res != ERROR_SUCCESS) {
187 if(protocol_config_keys[i].enabled)
188 enabled |= protocol_config_keys[i].prot_client_flag;
189 if(protocol_config_keys[i].disabled_by_default)
190 default_disabled |= protocol_config_keys[i].prot_client_flag;
191 continue;
192 }
193
194 size = sizeof(value);
195 res = RegQueryValueExW(key, enabledW, NULL, &type, (BYTE*)&value, &size);
196 if(res == ERROR_SUCCESS) {
197 if(type == REG_DWORD && value)
198 enabled |= protocol_config_keys[i].prot_client_flag;
199 }else if(protocol_config_keys[i].enabled) {
200 enabled |= protocol_config_keys[i].prot_client_flag;
201 }
202
203 size = sizeof(value);
204 res = RegQueryValueExW(key, disabledbydefaultW, NULL, &type, (BYTE*)&value, &size);
205 if(res == ERROR_SUCCESS) {
206 if(type != REG_DWORD || value)
207 default_disabled |= protocol_config_keys[i].prot_client_flag;
208 }else if(protocol_config_keys[i].disabled_by_default) {
209 default_disabled |= protocol_config_keys[i].prot_client_flag;
210 }
211
212 RegCloseKey(key);
213 }
214 }else {
215 /* No config, enable all known protocols. */
216 for(i=0; i < sizeof(protocol_config_keys)/sizeof(*protocol_config_keys); i++) {
217 if(protocol_config_keys[i].enabled)
218 enabled |= protocol_config_keys[i].prot_client_flag;
219 if(protocol_config_keys[i].disabled_by_default)
220 default_disabled |= protocol_config_keys[i].prot_client_flag;
221 }
222 }
223
224 RegCloseKey(protocols_key);
225
226 config_enabled_protocols = enabled & schan_imp_enabled_protocols();
227 config_default_disabled_protocols = default_disabled;
228 config_read = TRUE;
229
230 TRACE("enabled %x, disabled by default %x\n", config_enabled_protocols, config_default_disabled_protocols);
231 }
232
233 static SECURITY_STATUS schan_QueryCredentialsAttributes(
234 PCredHandle phCredential, ULONG ulAttribute, VOID *pBuffer)
235 {
236 struct schan_credentials *cred;
237 SECURITY_STATUS ret;
238
239 cred = schan_get_object(phCredential->dwLower, SCHAN_HANDLE_CRED);
240 if(!cred)
241 return SEC_E_INVALID_HANDLE;
242
243 switch (ulAttribute)
244 {
245 case SECPKG_ATTR_SUPPORTED_ALGS:
246 if (pBuffer)
247 {
248 /* FIXME: get from CryptoAPI */
249 FIXME("SECPKG_ATTR_SUPPORTED_ALGS: stub\n");
250 ret = SEC_E_UNSUPPORTED_FUNCTION;
251 }
252 else
253 ret = SEC_E_INTERNAL_ERROR;
254 break;
255 case SECPKG_ATTR_CIPHER_STRENGTHS:
256 if (pBuffer)
257 {
258 SecPkgCred_CipherStrengths *r = pBuffer;
259
260 /* FIXME: get from CryptoAPI */
261 FIXME("SECPKG_ATTR_CIPHER_STRENGTHS: semi-stub\n");
262 r->dwMinimumCipherStrength = 40;
263 r->dwMaximumCipherStrength = 168;
264 ret = SEC_E_OK;
265 }
266 else
267 ret = SEC_E_INTERNAL_ERROR;
268 break;
269 case SECPKG_ATTR_SUPPORTED_PROTOCOLS:
270 if(pBuffer) {
271 /* Regardless of MSDN documentation, tests show that this attribute takes into account
272 * what protocols are enabled for given credential. */
273 ((SecPkgCred_SupportedProtocols*)pBuffer)->grbitProtocol = cred->enabled_protocols;
274 ret = SEC_E_OK;
275 }else {
276 ret = SEC_E_INTERNAL_ERROR;
277 }
278 break;
279 default:
280 ret = SEC_E_UNSUPPORTED_FUNCTION;
281 }
282 return ret;
283 }
284
285 static SECURITY_STATUS SEC_ENTRY schan_QueryCredentialsAttributesA(
286 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
287 {
288 SECURITY_STATUS ret;
289
290 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
291
292 switch (ulAttribute)
293 {
294 case SECPKG_CRED_ATTR_NAMES:
295 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
296 ret = SEC_E_UNSUPPORTED_FUNCTION;
297 break;
298 default:
299 ret = schan_QueryCredentialsAttributes(phCredential, ulAttribute,
300 pBuffer);
301 }
302 return ret;
303 }
304
305 SECURITY_STATUS SEC_ENTRY schan_QueryCredentialsAttributesW(
306 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
307 {
308 SECURITY_STATUS ret;
309
310 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
311
312 switch (ulAttribute)
313 {
314 case SECPKG_CRED_ATTR_NAMES:
315 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
316 ret = SEC_E_UNSUPPORTED_FUNCTION;
317 break;
318 default:
319 ret = schan_QueryCredentialsAttributes(phCredential, ulAttribute,
320 pBuffer);
321 }
322 return ret;
323 }
324
325 static SECURITY_STATUS schan_CheckCreds(const SCHANNEL_CRED *schanCred)
326 {
327 SECURITY_STATUS st;
328 DWORD i;
329
330 TRACE("dwVersion = %d\n", schanCred->dwVersion);
331 TRACE("cCreds = %d\n", schanCred->cCreds);
332 TRACE("hRootStore = %p\n", schanCred->hRootStore);
333 TRACE("cMappers = %d\n", schanCred->cMappers);
334 TRACE("cSupportedAlgs = %d:\n", schanCred->cSupportedAlgs);
335 for (i = 0; i < schanCred->cSupportedAlgs; i++)
336 TRACE("%08x\n", schanCred->palgSupportedAlgs[i]);
337 TRACE("grbitEnabledProtocols = %08x\n", schanCred->grbitEnabledProtocols);
338 TRACE("dwMinimumCipherStrength = %d\n", schanCred->dwMinimumCipherStrength);
339 TRACE("dwMaximumCipherStrength = %d\n", schanCred->dwMaximumCipherStrength);
340 TRACE("dwSessionLifespan = %d\n", schanCred->dwSessionLifespan);
341 TRACE("dwFlags = %08x\n", schanCred->dwFlags);
342 TRACE("dwCredFormat = %d\n", schanCred->dwCredFormat);
343
344 switch (schanCred->dwVersion)
345 {
346 case SCH_CRED_V3:
347 case SCHANNEL_CRED_VERSION:
348 break;
349 default:
350 return SEC_E_INTERNAL_ERROR;
351 }
352
353 if (schanCred->cCreds == 0)
354 st = SEC_E_NO_CREDENTIALS;
355 else if (schanCred->cCreds > 1)
356 st = SEC_E_UNKNOWN_CREDENTIALS;
357 else
358 {
359 DWORD keySpec;
360 HCRYPTPROV csp;
361 BOOL ret, freeCSP;
362
363 ret = CryptAcquireCertificatePrivateKey(schanCred->paCred[0],
364 0, /* FIXME: what flags to use? */ NULL,
365 &csp, &keySpec, &freeCSP);
366 if (ret)
367 {
368 st = SEC_E_OK;
369 if (freeCSP)
370 CryptReleaseContext(csp, 0);
371 }
372 else
373 st = SEC_E_UNKNOWN_CREDENTIALS;
374 }
375 return st;
376 }
377
378 static SECURITY_STATUS schan_AcquireClientCredentials(const SCHANNEL_CRED *schanCred,
379 PCredHandle phCredential, PTimeStamp ptsExpiry)
380 {
381 struct schan_credentials *creds;
382 unsigned enabled_protocols;
383 ULONG_PTR handle;
384 SECURITY_STATUS st = SEC_E_OK;
385
386 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred, phCredential, ptsExpiry);
387
388 if (schanCred)
389 {
390 st = schan_CheckCreds(schanCred);
391 if (st != SEC_E_OK && st != SEC_E_NO_CREDENTIALS)
392 return st;
393
394 st = SEC_E_OK;
395 }
396
397 read_config();
398 if(schanCred && schanCred->grbitEnabledProtocols)
399 enabled_protocols = schanCred->grbitEnabledProtocols & config_enabled_protocols;
400 else
401 enabled_protocols = config_enabled_protocols & ~config_default_disabled_protocols;
402 if(!enabled_protocols) {
403 ERR("Could not find matching protocol\n");
404 return SEC_E_NO_AUTHENTICATING_AUTHORITY;
405 }
406
407 /* For now, the only thing I'm interested in is the direction of the
408 * connection, so just store it.
409 */
410 creds = HeapAlloc(GetProcessHeap(), 0, sizeof(*creds));
411 if (!creds) return SEC_E_INSUFFICIENT_MEMORY;
412
413 handle = schan_alloc_handle(creds, SCHAN_HANDLE_CRED);
414 if (handle == SCHAN_INVALID_HANDLE) goto fail;
415
416 creds->credential_use = SECPKG_CRED_OUTBOUND;
417 if (!schan_imp_allocate_certificate_credentials(creds))
418 {
419 schan_free_handle(handle, SCHAN_HANDLE_CRED);
420 goto fail;
421 }
422
423 creds->enabled_protocols = enabled_protocols;
424 phCredential->dwLower = handle;
425 phCredential->dwUpper = 0;
426
427 /* Outbound credentials have no expiry */
428 if (ptsExpiry)
429 {
430 ptsExpiry->LowPart = 0;
431 ptsExpiry->HighPart = 0;
432 }
433
434 return st;
435
436 fail:
437 HeapFree(GetProcessHeap(), 0, creds);
438 return SEC_E_INTERNAL_ERROR;
439 }
440
441 static SECURITY_STATUS schan_AcquireServerCredentials(const SCHANNEL_CRED *schanCred,
442 PCredHandle phCredential, PTimeStamp ptsExpiry)
443 {
444 SECURITY_STATUS st;
445
446 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred, phCredential, ptsExpiry);
447
448 if (!schanCred) return SEC_E_NO_CREDENTIALS;
449
450 st = schan_CheckCreds(schanCred);
451 if (st == SEC_E_OK)
452 {
453 ULONG_PTR handle;
454 struct schan_credentials *creds;
455
456 creds = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*creds));
457 if (!creds) return SEC_E_INSUFFICIENT_MEMORY;
458 creds->credential_use = SECPKG_CRED_INBOUND;
459
460 handle = schan_alloc_handle(creds, SCHAN_HANDLE_CRED);
461 if (handle == SCHAN_INVALID_HANDLE)
462 {
463 HeapFree(GetProcessHeap(), 0, creds);
464 return SEC_E_INTERNAL_ERROR;
465 }
466
467 phCredential->dwLower = handle;
468 phCredential->dwUpper = 0;
469
470 /* FIXME: get expiry from cert */
471 }
472 return st;
473 }
474
475 static SECURITY_STATUS schan_AcquireCredentialsHandle(ULONG fCredentialUse,
476 const SCHANNEL_CRED *schanCred, PCredHandle phCredential, PTimeStamp ptsExpiry)
477 {
478 SECURITY_STATUS ret;
479
480 if (fCredentialUse == SECPKG_CRED_OUTBOUND)
481 ret = schan_AcquireClientCredentials(schanCred, phCredential,
482 ptsExpiry);
483 else
484 ret = schan_AcquireServerCredentials(schanCred, phCredential,
485 ptsExpiry);
486 return ret;
487 }
488
489 SECURITY_STATUS SEC_ENTRY schan_AcquireCredentialsHandleA(
490 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
491 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
492 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
493 {
494 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
495 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
496 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
497 return schan_AcquireCredentialsHandle(fCredentialUse,
498 pAuthData, phCredential, ptsExpiry);
499 }
500
501 SECURITY_STATUS SEC_ENTRY schan_AcquireCredentialsHandleW(
502 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
503 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
504 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
505 {
506 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
507 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
508 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
509 return schan_AcquireCredentialsHandle(fCredentialUse,
510 pAuthData, phCredential, ptsExpiry);
511 }
512
513 SECURITY_STATUS SEC_ENTRY schan_FreeCredentialsHandle(
514 PCredHandle phCredential)
515 {
516 struct schan_credentials *creds;
517
518 TRACE("phCredential %p\n", phCredential);
519
520 if (!phCredential) return SEC_E_INVALID_HANDLE;
521
522 creds = schan_free_handle(phCredential->dwLower, SCHAN_HANDLE_CRED);
523 if (!creds) return SEC_E_INVALID_HANDLE;
524
525 if (creds->credential_use == SECPKG_CRED_OUTBOUND)
526 schan_imp_free_certificate_credentials(creds);
527 HeapFree(GetProcessHeap(), 0, creds);
528
529 return SEC_E_OK;
530 }
531
532 static void init_schan_buffers(struct schan_buffers *s, const PSecBufferDesc desc,
533 int (*get_next_buffer)(const struct schan_transport *, struct schan_buffers *))
534 {
535 s->offset = 0;
536 s->limit = ~0UL;
537 s->desc = desc;
538 s->current_buffer_idx = -1;
539 s->allow_buffer_resize = FALSE;
540 s->get_next_buffer = get_next_buffer;
541 }
542
543 static int schan_find_sec_buffer_idx(const SecBufferDesc *desc, unsigned int start_idx, ULONG buffer_type)
544 {
545 unsigned int i;
546 PSecBuffer buffer;
547
548 for (i = start_idx; i < desc->cBuffers; ++i)
549 {
550 buffer = &desc->pBuffers[i];
551 if (buffer->BufferType == buffer_type) return i;
552 }
553
554 return -1;
555 }
556
557 static void schan_resize_current_buffer(const struct schan_buffers *s, SIZE_T min_size)
558 {
559 SecBuffer *b = &s->desc->pBuffers[s->current_buffer_idx];
560 SIZE_T new_size = b->cbBuffer ? b->cbBuffer * 2 : 128;
561 void *new_data;
562
563 if (b->cbBuffer >= min_size || !s->allow_buffer_resize || min_size > UINT_MAX / 2) return;
564
565 while (new_size < min_size) new_size *= 2;
566
567 if (b->pvBuffer)
568 new_data = HeapReAlloc(GetProcessHeap(), 0, b->pvBuffer, new_size);
569 else
570 new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
571
572 if (!new_data)
573 {
574 TRACE("Failed to resize %p from %d to %ld\n", b->pvBuffer, b->cbBuffer, new_size);
575 return;
576 }
577
578 b->cbBuffer = new_size;
579 b->pvBuffer = new_data;
580 }
581
582 char *schan_get_buffer(const struct schan_transport *t, struct schan_buffers *s, SIZE_T *count)
583 {
584 SIZE_T max_count;
585 PSecBuffer buffer;
586
587 if (!s->desc)
588 {
589 TRACE("No desc\n");
590 return NULL;
591 }
592
593 if (s->current_buffer_idx == -1)
594 {
595 /* Initial buffer */
596 int buffer_idx = s->get_next_buffer(t, s);
597 if (buffer_idx == -1)
598 {
599 TRACE("No next buffer\n");
600 return NULL;
601 }
602 s->current_buffer_idx = buffer_idx;
603 }
604
605 buffer = &s->desc->pBuffers[s->current_buffer_idx];
606 TRACE("Using buffer %d: cbBuffer %d, BufferType %#x, pvBuffer %p\n", s->current_buffer_idx, buffer->cbBuffer, buffer->BufferType, buffer->pvBuffer);
607
608 schan_resize_current_buffer(s, s->offset + *count);
609 max_count = buffer->cbBuffer - s->offset;
610 if (s->limit != ~0UL && s->limit < max_count)
611 max_count = s->limit;
612 if (!max_count)
613 {
614 int buffer_idx;
615
616 s->allow_buffer_resize = FALSE;
617 buffer_idx = s->get_next_buffer(t, s);
618 if (buffer_idx == -1)
619 {
620 TRACE("No next buffer\n");
621 return NULL;
622 }
623 s->current_buffer_idx = buffer_idx;
624 s->offset = 0;
625 return schan_get_buffer(t, s, count);
626 }
627
628 if (*count > max_count)
629 *count = max_count;
630 if (s->limit != ~0UL)
631 s->limit -= *count;
632
633 return (char *)buffer->pvBuffer + s->offset;
634 }
635
636 /* schan_pull
637 * Read data from the transport input buffer.
638 *
639 * t - The session transport object.
640 * buff - The buffer into which to store the read data. Must be at least
641 * *buff_len bytes in length.
642 * buff_len - On input, *buff_len is the desired length to read. On successful
643 * return, *buff_len is the number of bytes actually read.
644 *
645 * Returns:
646 * 0 on success, in which case:
647 * *buff_len == 0 indicates end of file.
648 * *buff_len > 0 indicates that some data was read. May be less than
649 * what was requested, in which case the caller should call again if/
650 * when they want more.
651 * EAGAIN when no data could be read without blocking
652 * another errno-style error value on failure
653 *
654 */
655 int schan_pull(struct schan_transport *t, void *buff, size_t *buff_len)
656 {
657 char *b;
658 SIZE_T local_len = *buff_len;
659
660 TRACE("Pull %lu bytes\n", local_len);
661
662 *buff_len = 0;
663
664 b = schan_get_buffer(t, &t->in, &local_len);
665 if (!b)
666 return EAGAIN;
667
668 memcpy(buff, b, local_len);
669 t->in.offset += local_len;
670
671 TRACE("Read %lu bytes\n", local_len);
672
673 *buff_len = local_len;
674 return 0;
675 }
676
677 /* schan_push
678 * Write data to the transport output buffer.
679 *
680 * t - The session transport object.
681 * buff - The buffer of data to write. Must be at least *buff_len bytes in length.
682 * buff_len - On input, *buff_len is the desired length to write. On successful
683 * return, *buff_len is the number of bytes actually written.
684 *
685 * Returns:
686 * 0 on success
687 * *buff_len will be > 0 indicating how much data was written. May be less
688 * than what was requested, in which case the caller should call again
689 if/when they want to write more.
690 * EAGAIN when no data could be written without blocking
691 * another errno-style error value on failure
692 *
693 */
694 int schan_push(struct schan_transport *t, const void *buff, size_t *buff_len)
695 {
696 char *b;
697 SIZE_T local_len = *buff_len;
698
699 TRACE("Push %lu bytes\n", local_len);
700
701 *buff_len = 0;
702
703 b = schan_get_buffer(t, &t->out, &local_len);
704 if (!b)
705 return EAGAIN;
706
707 memcpy(b, buff, local_len);
708 t->out.offset += local_len;
709
710 TRACE("Wrote %lu bytes\n", local_len);
711
712 *buff_len = local_len;
713 return 0;
714 }
715
716 schan_imp_session schan_session_for_transport(struct schan_transport* t)
717 {
718 return t->ctx->session;
719 }
720
721 static int schan_init_sec_ctx_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
722 {
723 if (s->current_buffer_idx == -1)
724 {
725 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
726 if (t->ctx->req_ctx_attr & ISC_REQ_ALLOCATE_MEMORY)
727 {
728 if (idx == -1)
729 {
730 idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_EMPTY);
731 if (idx != -1) s->desc->pBuffers[idx].BufferType = SECBUFFER_TOKEN;
732 }
733 if (idx != -1 && !s->desc->pBuffers[idx].pvBuffer)
734 {
735 s->desc->pBuffers[idx].cbBuffer = 0;
736 s->allow_buffer_resize = TRUE;
737 }
738 }
739 return idx;
740 }
741
742 return -1;
743 }
744
745 static void dump_buffer_desc(SecBufferDesc *desc)
746 {
747 unsigned int i;
748
749 if (!desc) return;
750 TRACE("Buffer desc %p:\n", desc);
751 for (i = 0; i < desc->cBuffers; ++i)
752 {
753 SecBuffer *b = &desc->pBuffers[i];
754 TRACE("\tbuffer %u: cbBuffer %d, BufferType %#x pvBuffer %p\n", i, b->cbBuffer, b->BufferType, b->pvBuffer);
755 }
756 }
757
758 /***********************************************************************
759 * InitializeSecurityContextW
760 */
761 SECURITY_STATUS SEC_ENTRY schan_InitializeSecurityContextW(
762 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
763 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
764 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
765 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
766 {
767 struct schan_context *ctx;
768 struct schan_buffers *out_buffers;
769 struct schan_credentials *cred;
770 struct schan_transport transport;
771 SIZE_T expected_size = ~0UL;
772 SECURITY_STATUS ret;
773
774 TRACE("%p %p %s 0x%08x %d %d %p %d %p %p %p %p\n", phCredential, phContext,
775 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
776 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
777
778 dump_buffer_desc(pInput);
779 dump_buffer_desc(pOutput);
780
781 if (!phContext)
782 {
783 ULONG_PTR handle;
784
785 if (!phCredential) return SEC_E_INVALID_HANDLE;
786
787 cred = schan_get_object(phCredential->dwLower, SCHAN_HANDLE_CRED);
788 if (!cred) return SEC_E_INVALID_HANDLE;
789
790 if (!(cred->credential_use & SECPKG_CRED_OUTBOUND))
791 {
792 WARN("Invalid credential use %#x\n", cred->credential_use);
793 return SEC_E_INVALID_HANDLE;
794 }
795
796 ctx = HeapAlloc(GetProcessHeap(), 0, sizeof(*ctx));
797 if (!ctx) return SEC_E_INSUFFICIENT_MEMORY;
798
799 ctx->cert = NULL;
800 handle = schan_alloc_handle(ctx, SCHAN_HANDLE_CTX);
801 if (handle == SCHAN_INVALID_HANDLE)
802 {
803 HeapFree(GetProcessHeap(), 0, ctx);
804 return SEC_E_INTERNAL_ERROR;
805 }
806
807 if (!schan_imp_create_session(&ctx->session, cred))
808 {
809 schan_free_handle(handle, SCHAN_HANDLE_CTX);
810 HeapFree(GetProcessHeap(), 0, ctx);
811 return SEC_E_INTERNAL_ERROR;
812 }
813
814 if (pszTargetName)
815 {
816 UINT len = WideCharToMultiByte( CP_UNIXCP, 0, pszTargetName, -1, NULL, 0, NULL, NULL );
817 char *target = HeapAlloc( GetProcessHeap(), 0, len );
818
819 if (target)
820 {
821 WideCharToMultiByte( CP_UNIXCP, 0, pszTargetName, -1, target, len, NULL, NULL );
822 schan_imp_set_session_target( ctx->session, target );
823 HeapFree( GetProcessHeap(), 0, target );
824 }
825 }
826 phNewContext->dwLower = handle;
827 phNewContext->dwUpper = 0;
828 }
829 else
830 {
831 SIZE_T record_size = 0;
832 unsigned char *ptr;
833 SecBuffer *buffer;
834 int idx;
835
836 if (!pInput)
837 return SEC_E_INCOMPLETE_MESSAGE;
838
839 idx = schan_find_sec_buffer_idx(pInput, 0, SECBUFFER_TOKEN);
840 if (idx == -1)
841 return SEC_E_INCOMPLETE_MESSAGE;
842
843 buffer = &pInput->pBuffers[idx];
844 ptr = buffer->pvBuffer;
845 expected_size = 0;
846
847 while (buffer->cbBuffer > expected_size + 5)
848 {
849 record_size = 5 + ((ptr[3] << 8) | ptr[4]);
850
851 if (buffer->cbBuffer < expected_size + record_size)
852 break;
853
854 expected_size += record_size;
855 ptr += record_size;
856 }
857
858 if (!expected_size)
859 {
860 TRACE("Expected at least %lu bytes, but buffer only contains %u bytes.\n",
861 max(6, record_size), buffer->cbBuffer);
862 return SEC_E_INCOMPLETE_MESSAGE;
863 }
864
865 TRACE("Using expected_size %lu.\n", expected_size);
866
867 ctx = schan_get_object(phContext->dwLower, SCHAN_HANDLE_CTX);
868 }
869
870 ctx->req_ctx_attr = fContextReq;
871
872 transport.ctx = ctx;
873 init_schan_buffers(&transport.in, pInput, schan_init_sec_ctx_get_next_buffer);
874 transport.in.limit = expected_size;
875 init_schan_buffers(&transport.out, pOutput, schan_init_sec_ctx_get_next_buffer);
876 schan_imp_set_session_transport(ctx->session, &transport);
877
878 /* Perform the TLS handshake */
879 ret = schan_imp_handshake(ctx->session);
880
881 if(transport.in.offset && transport.in.offset != pInput->pBuffers[0].cbBuffer) {
882 if(pInput->cBuffers<2 || pInput->pBuffers[1].BufferType!=SECBUFFER_EMPTY)
883 return SEC_E_INVALID_TOKEN;
884
885 pInput->pBuffers[1].BufferType = SECBUFFER_EXTRA;
886 pInput->pBuffers[1].cbBuffer = pInput->pBuffers[0].cbBuffer-transport.in.offset;
887 }
888
889 out_buffers = &transport.out;
890 if (out_buffers->current_buffer_idx != -1)
891 {
892 SecBuffer *buffer = &out_buffers->desc->pBuffers[out_buffers->current_buffer_idx];
893 buffer->cbBuffer = out_buffers->offset;
894 }
895
896 *pfContextAttr = 0;
897 if (ctx->req_ctx_attr & ISC_REQ_ALLOCATE_MEMORY)
898 *pfContextAttr |= ISC_RET_ALLOCATED_MEMORY;
899
900 return ret;
901 }
902
903 /***********************************************************************
904 * InitializeSecurityContextA
905 */
906 SECURITY_STATUS SEC_ENTRY schan_InitializeSecurityContextA(
907 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
908 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
909 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
910 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
911 {
912 SECURITY_STATUS ret;
913 SEC_WCHAR *target_name = NULL;
914
915 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
916 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
917 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
918
919 if (pszTargetName)
920 {
921 INT len = MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, NULL, 0);
922 target_name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*target_name));
923 MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, target_name, len);
924 }
925
926 ret = schan_InitializeSecurityContextW(phCredential, phContext, target_name,
927 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
928 phNewContext, pOutput, pfContextAttr, ptsExpiry);
929
930 HeapFree(GetProcessHeap(), 0, target_name);
931
932 return ret;
933 }
934
935 SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesW(
936 PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
937 {
938 struct schan_context *ctx;
939
940 TRACE("context_handle %p, attribute %#x, buffer %p\n",
941 context_handle, attribute, buffer);
942
943 if (!context_handle) return SEC_E_INVALID_HANDLE;
944 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
945
946 switch(attribute)
947 {
948 case SECPKG_ATTR_STREAM_SIZES:
949 {
950 SecPkgContext_ConnectionInfo info;
951 SECURITY_STATUS status = schan_imp_get_connection_info(ctx->session, &info);
952 if (status == SEC_E_OK)
953 {
954 SecPkgContext_StreamSizes *stream_sizes = buffer;
955 SIZE_T mac_size = info.dwHashStrength;
956 unsigned int block_size = schan_imp_get_session_cipher_block_size(ctx->session);
957 unsigned int message_size = schan_imp_get_max_message_size(ctx->session);
958
959 TRACE("Using %lu mac bytes, message size %u, block size %u\n",
960 mac_size, message_size, block_size);
961
962 /* These are defined by the TLS RFC */
963 stream_sizes->cbHeader = 5;
964 stream_sizes->cbTrailer = mac_size + 256; /* Max 255 bytes padding + 1 for padding size */
965 stream_sizes->cbMaximumMessage = message_size;
966 stream_sizes->cbBuffers = 4;
967 stream_sizes->cbBlockSize = block_size;
968 }
969
970 return status;
971 }
972 case SECPKG_ATTR_REMOTE_CERT_CONTEXT:
973 {
974 PCCERT_CONTEXT *cert = buffer;
975
976 if (!ctx->cert) {
977 HCERTSTORE cert_store;
978 SECURITY_STATUS status;
979
980 cert_store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, CERT_STORE_CREATE_NEW_FLAG, NULL);
981 if(!cert_store)
982 return GetLastError();
983
984 status = schan_imp_get_session_peer_certificate(ctx->session, cert_store, &ctx->cert);
985 CertCloseStore(cert_store, 0);
986 if(status != SEC_E_OK)
987 return status;
988 }
989
990 *cert = CertDuplicateCertificateContext(ctx->cert);
991 return SEC_E_OK;
992 }
993 case SECPKG_ATTR_CONNECTION_INFO:
994 {
995 SecPkgContext_ConnectionInfo *info = buffer;
996 return schan_imp_get_connection_info(ctx->session, info);
997 }
998
999 default:
1000 FIXME("Unhandled attribute %#x\n", attribute);
1001 return SEC_E_UNSUPPORTED_FUNCTION;
1002 }
1003 }
1004
1005 SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesA(
1006 PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
1007 {
1008 TRACE("context_handle %p, attribute %#x, buffer %p\n",
1009 context_handle, attribute, buffer);
1010
1011 switch(attribute)
1012 {
1013 case SECPKG_ATTR_STREAM_SIZES:
1014 return schan_QueryContextAttributesW(context_handle, attribute, buffer);
1015 case SECPKG_ATTR_REMOTE_CERT_CONTEXT:
1016 return schan_QueryContextAttributesW(context_handle, attribute, buffer);
1017 case SECPKG_ATTR_CONNECTION_INFO:
1018 return schan_QueryContextAttributesW(context_handle, attribute, buffer);
1019
1020 default:
1021 FIXME("Unhandled attribute %#x\n", attribute);
1022 return SEC_E_UNSUPPORTED_FUNCTION;
1023 }
1024 }
1025
1026 static int schan_encrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
1027 {
1028 SecBuffer *b;
1029
1030 if (s->current_buffer_idx == -1)
1031 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_HEADER);
1032
1033 b = &s->desc->pBuffers[s->current_buffer_idx];
1034
1035 if (b->BufferType == SECBUFFER_STREAM_HEADER)
1036 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
1037
1038 if (b->BufferType == SECBUFFER_DATA)
1039 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_TRAILER);
1040
1041 return -1;
1042 }
1043
1044 static int schan_encrypt_message_get_next_buffer_token(const struct schan_transport *t, struct schan_buffers *s)
1045 {
1046 SecBuffer *b;
1047
1048 if (s->current_buffer_idx == -1)
1049 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
1050
1051 b = &s->desc->pBuffers[s->current_buffer_idx];
1052
1053 if (b->BufferType == SECBUFFER_TOKEN)
1054 {
1055 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
1056 if (idx != s->current_buffer_idx) return -1;
1057 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
1058 }
1059
1060 if (b->BufferType == SECBUFFER_DATA)
1061 {
1062 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
1063 if (idx != -1)
1064 idx = schan_find_sec_buffer_idx(s->desc, idx + 1, SECBUFFER_TOKEN);
1065 return idx;
1066 }
1067
1068 return -1;
1069 }
1070
1071 SECURITY_STATUS SEC_ENTRY schan_EncryptMessage(PCtxtHandle context_handle,
1072 ULONG quality, PSecBufferDesc message, ULONG message_seq_no)
1073 {
1074 struct schan_transport transport;
1075 struct schan_context *ctx;
1076 struct schan_buffers *b;
1077 SECURITY_STATUS status;
1078 SecBuffer *buffer;
1079 SIZE_T data_size;
1080 SIZE_T length;
1081 char *data;
1082 int idx;
1083
1084 TRACE("context_handle %p, quality %d, message %p, message_seq_no %d\n",
1085 context_handle, quality, message, message_seq_no);
1086
1087 if (!context_handle) return SEC_E_INVALID_HANDLE;
1088 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
1089
1090 dump_buffer_desc(message);
1091
1092 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_DATA);
1093 if (idx == -1)
1094 {
1095 WARN("No data buffer passed\n");
1096 return SEC_E_INTERNAL_ERROR;
1097 }
1098 buffer = &message->pBuffers[idx];
1099
1100 data_size = buffer->cbBuffer;
1101 data = HeapAlloc(GetProcessHeap(), 0, data_size);
1102 memcpy(data, buffer->pvBuffer, data_size);
1103
1104 transport.ctx = ctx;
1105 init_schan_buffers(&transport.in, NULL, NULL);
1106 if (schan_find_sec_buffer_idx(message, 0, SECBUFFER_STREAM_HEADER) != -1)
1107 init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer);
1108 else
1109 init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer_token);
1110 schan_imp_set_session_transport(ctx->session, &transport);
1111
1112 length = data_size;
1113 status = schan_imp_send(ctx->session, data, &length);
1114
1115 TRACE("Sent %ld bytes.\n", length);
1116
1117 if (length != data_size)
1118 status = SEC_E_INTERNAL_ERROR;
1119
1120 b = &transport.out;
1121 b->desc->pBuffers[b->current_buffer_idx].cbBuffer = b->offset;
1122 HeapFree(GetProcessHeap(), 0, data);
1123
1124 TRACE("Returning %#x.\n", status);
1125
1126 return status;
1127 }
1128
1129 static int schan_decrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
1130 {
1131 if (s->current_buffer_idx == -1)
1132 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
1133
1134 return -1;
1135 }
1136
1137 static int schan_validate_decrypt_buffer_desc(PSecBufferDesc message)
1138 {
1139 int data_idx = -1;
1140 unsigned int empty_count = 0;
1141 unsigned int i;
1142
1143 if (message->cBuffers < 4)
1144 {
1145 WARN("Less than four buffers passed\n");
1146 return -1;
1147 }
1148
1149 for (i = 0; i < message->cBuffers; ++i)
1150 {
1151 SecBuffer *b = &message->pBuffers[i];
1152 if (b->BufferType == SECBUFFER_DATA)
1153 {
1154 if (data_idx != -1)
1155 {
1156 WARN("More than one data buffer passed\n");
1157 return -1;
1158 }
1159 data_idx = i;
1160 }
1161 else if (b->BufferType == SECBUFFER_EMPTY)
1162 ++empty_count;
1163 }
1164
1165 if (data_idx == -1)
1166 {
1167 WARN("No data buffer passed\n");
1168 return -1;
1169 }
1170
1171 if (empty_count < 3)
1172 {
1173 WARN("Less than three empty buffers passed\n");
1174 return -1;
1175 }
1176
1177 return data_idx;
1178 }
1179
1180 static void schan_decrypt_fill_buffer(PSecBufferDesc message, ULONG buffer_type, void *data, ULONG size)
1181 {
1182 int idx;
1183 SecBuffer *buffer;
1184
1185 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_EMPTY);
1186 buffer = &message->pBuffers[idx];
1187
1188 buffer->BufferType = buffer_type;
1189 buffer->pvBuffer = data;
1190 buffer->cbBuffer = size;
1191 }
1192
1193 SECURITY_STATUS SEC_ENTRY schan_DecryptMessage(PCtxtHandle context_handle,
1194 PSecBufferDesc message, ULONG message_seq_no, PULONG quality)
1195 {
1196 struct schan_transport transport;
1197 struct schan_context *ctx;
1198 SecBuffer *buffer;
1199 SIZE_T data_size;
1200 char *data;
1201 unsigned expected_size;
1202 SSIZE_T received = 0;
1203 int idx;
1204 unsigned char *buf_ptr;
1205
1206 TRACE("context_handle %p, message %p, message_seq_no %d, quality %p\n",
1207 context_handle, message, message_seq_no, quality);
1208
1209 if (!context_handle) return SEC_E_INVALID_HANDLE;
1210 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
1211
1212 dump_buffer_desc(message);
1213
1214 idx = schan_validate_decrypt_buffer_desc(message);
1215 if (idx == -1)
1216 return SEC_E_INVALID_TOKEN;
1217 buffer = &message->pBuffers[idx];
1218 buf_ptr = buffer->pvBuffer;
1219
1220 expected_size = 5 + ((buf_ptr[3] << 8) | buf_ptr[4]);
1221 if(buffer->cbBuffer < expected_size)
1222 {
1223 TRACE("Expected %u bytes, but buffer only contains %u bytes\n", expected_size, buffer->cbBuffer);
1224 buffer->BufferType = SECBUFFER_MISSING;
1225 buffer->cbBuffer = expected_size - buffer->cbBuffer;
1226
1227 /* This is a bit weird, but windows does it too */
1228 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_EMPTY);
1229 buffer = &message->pBuffers[idx];
1230 buffer->BufferType = SECBUFFER_MISSING;
1231 buffer->cbBuffer = expected_size - buffer->cbBuffer;
1232
1233 TRACE("Returning SEC_E_INCOMPLETE_MESSAGE\n");
1234 return SEC_E_INCOMPLETE_MESSAGE;
1235 }
1236
1237 data_size = expected_size - 5;
1238 data = HeapAlloc(GetProcessHeap(), 0, data_size);
1239
1240 transport.ctx = ctx;
1241 init_schan_buffers(&transport.in, message, schan_decrypt_message_get_next_buffer);
1242 transport.in.limit = expected_size;
1243 init_schan_buffers(&transport.out, NULL, NULL);
1244 schan_imp_set_session_transport(ctx->session, &transport);
1245
1246 while (received < data_size)
1247 {
1248 SIZE_T length = data_size - received;
1249 SECURITY_STATUS status = schan_imp_recv(ctx->session, data + received, &length);
1250
1251 if (status == SEC_I_CONTINUE_NEEDED)
1252 break;
1253
1254 if (status != SEC_E_OK)
1255 {
1256 HeapFree(GetProcessHeap(), 0, data);
1257 ERR("Returning %x\n", status);
1258 return status;
1259 }
1260
1261 if (!length)
1262 break;
1263
1264 received += length;
1265 }
1266
1267 TRACE("Received %ld bytes\n", received);
1268
1269 memcpy(buf_ptr + 5, data, received);
1270 HeapFree(GetProcessHeap(), 0, data);
1271
1272 schan_decrypt_fill_buffer(message, SECBUFFER_DATA,
1273 buf_ptr + 5, received);
1274
1275 schan_decrypt_fill_buffer(message, SECBUFFER_STREAM_TRAILER,
1276 buf_ptr + 5 + received, buffer->cbBuffer - 5 - received);
1277
1278 if(buffer->cbBuffer > expected_size)
1279 schan_decrypt_fill_buffer(message, SECBUFFER_EXTRA,
1280 buf_ptr + expected_size, buffer->cbBuffer - expected_size);
1281
1282 buffer->BufferType = SECBUFFER_STREAM_HEADER;
1283 buffer->cbBuffer = 5;
1284
1285 return SEC_E_OK;
1286 }
1287
1288 SECURITY_STATUS SEC_ENTRY schan_DeleteSecurityContext(PCtxtHandle context_handle)
1289 {
1290 struct schan_context *ctx;
1291
1292 TRACE("context_handle %p\n", context_handle);
1293
1294 if (!context_handle) return SEC_E_INVALID_HANDLE;
1295
1296 ctx = schan_free_handle(context_handle->dwLower, SCHAN_HANDLE_CTX);
1297 if (!ctx) return SEC_E_INVALID_HANDLE;
1298
1299 if (ctx->cert)
1300 CertFreeCertificateContext(ctx->cert);
1301 schan_imp_dispose_session(ctx->session);
1302 HeapFree(GetProcessHeap(), 0, ctx);
1303
1304 return SEC_E_OK;
1305 }
1306
1307 SecurityFunctionTableA schanTableA = {
1308 1,
1309 schan_EnumerateSecurityPackagesA,
1310 schan_QueryCredentialsAttributesA,
1311 schan_AcquireCredentialsHandleA,
1312 schan_FreeCredentialsHandle,
1313 NULL, /* Reserved2 */
1314 schan_InitializeSecurityContextA,
1315 NULL, /* AcceptSecurityContext */
1316 NULL, /* CompleteAuthToken */
1317 schan_DeleteSecurityContext,
1318 NULL, /* ApplyControlToken */
1319 schan_QueryContextAttributesA,
1320 NULL, /* ImpersonateSecurityContext */
1321 NULL, /* RevertSecurityContext */
1322 NULL, /* MakeSignature */
1323 NULL, /* VerifySignature */
1324 schan_FreeContextBuffer,
1325 NULL, /* QuerySecurityPackageInfoA */
1326 NULL, /* Reserved3 */
1327 NULL, /* Reserved4 */
1328 NULL, /* ExportSecurityContext */
1329 NULL, /* ImportSecurityContextA */
1330 NULL, /* AddCredentialsA */
1331 NULL, /* Reserved8 */
1332 NULL, /* QuerySecurityContextToken */
1333 schan_EncryptMessage,
1334 schan_DecryptMessage,
1335 NULL, /* SetContextAttributesA */
1336 };
1337
1338 SecurityFunctionTableW schanTableW = {
1339 1,
1340 schan_EnumerateSecurityPackagesW,
1341 schan_QueryCredentialsAttributesW,
1342 schan_AcquireCredentialsHandleW,
1343 schan_FreeCredentialsHandle,
1344 NULL, /* Reserved2 */
1345 schan_InitializeSecurityContextW,
1346 NULL, /* AcceptSecurityContext */
1347 NULL, /* CompleteAuthToken */
1348 schan_DeleteSecurityContext,
1349 NULL, /* ApplyControlToken */
1350 schan_QueryContextAttributesW,
1351 NULL, /* ImpersonateSecurityContext */
1352 NULL, /* RevertSecurityContext */
1353 NULL, /* MakeSignature */
1354 NULL, /* VerifySignature */
1355 schan_FreeContextBuffer,
1356 NULL, /* QuerySecurityPackageInfoW */
1357 NULL, /* Reserved3 */
1358 NULL, /* Reserved4 */
1359 NULL, /* ExportSecurityContext */
1360 NULL, /* ImportSecurityContextW */
1361 NULL, /* AddCredentialsW */
1362 NULL, /* Reserved8 */
1363 NULL, /* QuerySecurityContextToken */
1364 schan_EncryptMessage,
1365 schan_DecryptMessage,
1366 NULL, /* SetContextAttributesW */
1367 };
1368
1369 static const WCHAR schannelComment[] = { 'S','c','h','a','n','n','e','l',' ',
1370 'S','e','c','u','r','i','t','y',' ','P','a','c','k','a','g','e',0 };
1371 static const WCHAR schannelDllName[] = { 's','c','h','a','n','n','e','l','.','d','l','l',0 };
1372
1373 void SECUR32_initSchannelSP(void)
1374 {
1375 /* This is what Windows reports. This shouldn't break any applications
1376 * even though the functions are missing, because the wrapper will
1377 * return SEC_E_UNSUPPORTED_FUNCTION if our function is NULL.
1378 */
1379 static const LONG caps =
1380 SECPKG_FLAG_INTEGRITY |
1381 SECPKG_FLAG_PRIVACY |
1382 SECPKG_FLAG_CONNECTION |
1383 SECPKG_FLAG_MULTI_REQUIRED |
1384 SECPKG_FLAG_EXTENDED_ERROR |
1385 SECPKG_FLAG_IMPERSONATION |
1386 SECPKG_FLAG_ACCEPT_WIN32_NAME |
1387 SECPKG_FLAG_STREAM;
1388 static const short version = 1;
1389 static const LONG maxToken = 16384;
1390 SEC_WCHAR *uniSPName = (SEC_WCHAR *)UNISP_NAME_W,
1391 *schannel = (SEC_WCHAR *)SCHANNEL_NAME_W;
1392 const SecPkgInfoW info[] = {
1393 { caps, version, UNISP_RPC_ID, maxToken, uniSPName, uniSPName },
1394 { caps, version, UNISP_RPC_ID, maxToken, schannel,
1395 (SEC_WCHAR *)schannelComment },
1396 };
1397 SecureProvider *provider;
1398
1399 if (!schan_imp_init())
1400 return;
1401
1402 schan_handle_table = HeapAlloc(GetProcessHeap(), 0, 64 * sizeof(*schan_handle_table));
1403 if (!schan_handle_table)
1404 {
1405 ERR("Failed to allocate schannel handle table.\n");
1406 goto fail;
1407 }
1408 schan_handle_table_size = 64;
1409
1410 provider = SECUR32_addProvider(&schanTableA, &schanTableW, schannelDllName);
1411 if (!provider)
1412 {
1413 ERR("Failed to add schannel provider.\n");
1414 goto fail;
1415 }
1416
1417 SECUR32_addPackages(provider, sizeof(info) / sizeof(info[0]), NULL, info);
1418
1419 return;
1420
1421 fail:
1422 HeapFree(GetProcessHeap(), 0, schan_handle_table);
1423 schan_handle_table = NULL;
1424 schan_imp_deinit();
1425 return;
1426 }
1427
1428 void SECUR32_deinitSchannelSP(void)
1429 {
1430 SIZE_T i = schan_handle_count;
1431
1432 if (!schan_handle_table) return;
1433
1434 /* deinitialized sessions first because a pointer to the credentials
1435 * may be stored for the session. */
1436 while (i--)
1437 {
1438 if (schan_handle_table[i].type == SCHAN_HANDLE_CTX)
1439 {
1440 struct schan_context *ctx = schan_free_handle(i, SCHAN_HANDLE_CTX);
1441 schan_imp_dispose_session(ctx->session);
1442 HeapFree(GetProcessHeap(), 0, ctx);
1443 }
1444 }
1445 i = schan_handle_count;
1446 while (i--)
1447 {
1448 if (schan_handle_table[i].type != SCHAN_HANDLE_FREE)
1449 {
1450 struct schan_credentials *cred;
1451 cred = schan_free_handle(i, SCHAN_HANDLE_CRED);
1452 schan_imp_free_certificate_credentials(cred);
1453 HeapFree(GetProcessHeap(), 0, cred);
1454 }
1455 }
1456 HeapFree(GetProcessHeap(), 0, schan_handle_table);
1457 schan_imp_deinit();
1458 }
1459
1460 #else /* SONAME_LIBGNUTLS || HAVE_SECURITY_SECURITY_H */
1461
1462 void SECUR32_initSchannelSP(void)
1463 {
1464 ERR("TLS library not found, SSL connections will fail\n");
1465 }
1466
1467 void SECUR32_deinitSchannelSP(void) {}
1468
1469 #endif /* SONAME_LIBGNUTLS || HAVE_SECURITY_SECURITY_H */