wined3d: Use a separate STATE_VDECL state handler in the GLSL pipeline.
[wine/multimedia.git] / dlls / secur32 / schannel.c
blob5b86a7521f660f6e3a665af783d25c64789077b1
1 /* Copyright (C) 2005 Juan Lang
2 * Copyright 2008 Henri Verbeet
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.
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.
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
18 * This file implements the schannel provider, or, the SSL/TLS implementations.
20 #include "config.h"
21 #include "wine/port.h"
23 #include <stdarg.h>
24 #include <errno.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "sspi.h"
31 #include "schannel.h"
32 #include "secur32_priv.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
39 #if defined(SONAME_LIBGNUTLS) || defined (HAVE_SECURITY_SECURITY_H)
41 #define SCHAN_INVALID_HANDLE ~0UL
43 enum schan_handle_type
45 SCHAN_HANDLE_CRED,
46 SCHAN_HANDLE_CTX,
47 SCHAN_HANDLE_FREE
50 struct schan_handle
52 void *object;
53 enum schan_handle_type type;
56 struct schan_context
58 schan_imp_session session;
59 ULONG req_ctx_attr;
60 const CERT_CONTEXT *cert;
63 static struct schan_handle *schan_handle_table;
64 static struct schan_handle *schan_free_handles;
65 static SIZE_T schan_handle_table_size;
66 static SIZE_T schan_handle_count;
68 /* Protocols enabled, only those may be used for the connection. */
69 static DWORD config_enabled_protocols;
71 /* Protocols disabled by default. They are enabled for using, but disabled when caller asks for default settings. */
72 static DWORD config_default_disabled_protocols;
74 static ULONG_PTR schan_alloc_handle(void *object, enum schan_handle_type type)
76 struct schan_handle *handle;
78 if (schan_free_handles)
80 DWORD index = schan_free_handles - schan_handle_table;
81 /* Use a free handle */
82 handle = schan_free_handles;
83 if (handle->type != SCHAN_HANDLE_FREE)
85 ERR("Handle %d(%p) is in the free list, but has type %#x.\n", index, handle, handle->type);
86 return SCHAN_INVALID_HANDLE;
88 schan_free_handles = handle->object;
89 handle->object = object;
90 handle->type = type;
92 return index;
94 if (!(schan_handle_count < schan_handle_table_size))
96 /* Grow the table */
97 SIZE_T new_size = schan_handle_table_size + (schan_handle_table_size >> 1);
98 struct schan_handle *new_table = HeapReAlloc(GetProcessHeap(), 0, schan_handle_table, new_size * sizeof(*schan_handle_table));
99 if (!new_table)
101 ERR("Failed to grow the handle table\n");
102 return SCHAN_INVALID_HANDLE;
104 schan_handle_table = new_table;
105 schan_handle_table_size = new_size;
108 handle = &schan_handle_table[schan_handle_count++];
109 handle->object = object;
110 handle->type = type;
112 return handle - schan_handle_table;
115 static void *schan_free_handle(ULONG_PTR handle_idx, enum schan_handle_type type)
117 struct schan_handle *handle;
118 void *object;
120 if (handle_idx == SCHAN_INVALID_HANDLE) return NULL;
121 if (handle_idx >= schan_handle_count) return NULL;
122 handle = &schan_handle_table[handle_idx];
123 if (handle->type != type)
125 ERR("Handle %ld(%p) is not of type %#x\n", handle_idx, handle, type);
126 return NULL;
129 object = handle->object;
130 handle->object = schan_free_handles;
131 handle->type = SCHAN_HANDLE_FREE;
132 schan_free_handles = handle;
134 return object;
137 static void *schan_get_object(ULONG_PTR handle_idx, enum schan_handle_type type)
139 struct schan_handle *handle;
141 if (handle_idx == SCHAN_INVALID_HANDLE) return NULL;
142 if (handle_idx >= schan_handle_count) return NULL;
143 handle = &schan_handle_table[handle_idx];
144 if (handle->type != type)
146 ERR("Handle %ld(%p) is not of type %#x\n", handle_idx, handle, type);
147 return NULL;
150 return handle->object;
153 static void read_config(void)
155 DWORD enabled = 0, default_disabled = 0;
156 HKEY protocols_key, key;
157 WCHAR subkey_name[64];
158 unsigned i;
159 DWORD res;
161 static BOOL config_read = FALSE;
163 static const WCHAR protocol_config_key_name[] = {
164 'S','Y','S','T','E','M','\\',
165 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
166 'C','o','n','t','r','o','l','\\',
167 'S','e','c','u','r','i','t','y','P','r','o','v','i','d','e','r','s','\\',
168 'S','C','H','A','N','N','E','L','\\',
169 'P','r','o','t','o','c','o','l','s',0 };
171 static const WCHAR clientW[] = {'\\','C','l','i','e','n','t',0};
172 static const WCHAR enabledW[] = {'e','n','a','b','l','e','d',0};
173 static const WCHAR disabledbydefaultW[] = {'D','i','s','a','b','l','e','d','B','y','D','e','f','a','u','l','t',0};
175 static const struct {
176 WCHAR key_name[20];
177 DWORD prot_client_flag;
178 BOOL enabled; /* If no config is present, enable the protocol */
179 BOOL disabled_by_default; /* Disable if caller asks for default protocol set */
180 } protocol_config_keys[] = {
181 {{'S','S','L',' ','2','.','0',0}, SP_PROT_SSL2_CLIENT, FALSE, TRUE}, /* NOTE: TRUE, TRUE on Windows */
182 {{'S','S','L',' ','3','.','0',0}, SP_PROT_SSL3_CLIENT, TRUE, FALSE},
183 {{'T','L','S',' ','1','.','0',0}, SP_PROT_TLS1_0_CLIENT, TRUE, FALSE},
184 {{'T','L','S',' ','1','.','1',0}, SP_PROT_TLS1_1_CLIENT, TRUE, FALSE /* NOTE: not enabled by default on Windows */ },
185 {{'T','L','S',' ','1','.','2',0}, SP_PROT_TLS1_2_CLIENT, TRUE, FALSE /* NOTE: not enabled by default on Windows */ }
188 /* No need for thread safety */
189 if(config_read)
190 return;
192 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, protocol_config_key_name, 0, KEY_READ, &protocols_key);
193 if(res == ERROR_SUCCESS) {
194 DWORD type, size, value;
196 for(i=0; i < sizeof(protocol_config_keys)/sizeof(*protocol_config_keys); i++) {
197 strcpyW(subkey_name, protocol_config_keys[i].key_name);
198 strcatW(subkey_name, clientW);
199 res = RegOpenKeyExW(protocols_key, subkey_name, 0, KEY_READ, &key);
200 if(res != ERROR_SUCCESS) {
201 if(protocol_config_keys[i].enabled)
202 enabled |= protocol_config_keys[i].prot_client_flag;
203 if(protocol_config_keys[i].disabled_by_default)
204 default_disabled |= protocol_config_keys[i].prot_client_flag;
205 continue;
208 size = sizeof(value);
209 res = RegQueryValueExW(key, enabledW, NULL, &type, (BYTE*)&value, &size);
210 if(res == ERROR_SUCCESS) {
211 if(type == REG_DWORD && value)
212 enabled |= protocol_config_keys[i].prot_client_flag;
213 }else if(protocol_config_keys[i].enabled) {
214 enabled |= protocol_config_keys[i].prot_client_flag;
217 size = sizeof(value);
218 res = RegQueryValueExW(key, disabledbydefaultW, NULL, &type, (BYTE*)&value, &size);
219 if(res == ERROR_SUCCESS) {
220 if(type != REG_DWORD || value)
221 default_disabled |= protocol_config_keys[i].prot_client_flag;
222 }else if(protocol_config_keys[i].disabled_by_default) {
223 default_disabled |= protocol_config_keys[i].prot_client_flag;
226 RegCloseKey(key);
228 }else {
229 /* No config, enable all known protocols. */
230 for(i=0; i < sizeof(protocol_config_keys)/sizeof(*protocol_config_keys); i++) {
231 if(protocol_config_keys[i].enabled)
232 enabled |= protocol_config_keys[i].prot_client_flag;
233 if(protocol_config_keys[i].disabled_by_default)
234 default_disabled |= protocol_config_keys[i].prot_client_flag;
238 RegCloseKey(protocols_key);
240 config_enabled_protocols = enabled & schan_imp_enabled_protocols();
241 config_default_disabled_protocols = default_disabled;
242 config_read = TRUE;
244 TRACE("enabled %x, disabled by default %x\n", config_enabled_protocols, config_default_disabled_protocols);
247 static SECURITY_STATUS schan_QueryCredentialsAttributes(
248 PCredHandle phCredential, ULONG ulAttribute, VOID *pBuffer)
250 struct schan_credentials *cred;
251 SECURITY_STATUS ret;
253 cred = schan_get_object(phCredential->dwLower, SCHAN_HANDLE_CRED);
254 if(!cred)
255 return SEC_E_INVALID_HANDLE;
257 switch (ulAttribute)
259 case SECPKG_ATTR_SUPPORTED_ALGS:
260 if (pBuffer)
262 /* FIXME: get from CryptoAPI */
263 FIXME("SECPKG_ATTR_SUPPORTED_ALGS: stub\n");
264 ret = SEC_E_UNSUPPORTED_FUNCTION;
266 else
267 ret = SEC_E_INTERNAL_ERROR;
268 break;
269 case SECPKG_ATTR_CIPHER_STRENGTHS:
270 if (pBuffer)
272 SecPkgCred_CipherStrengths *r = pBuffer;
274 /* FIXME: get from CryptoAPI */
275 FIXME("SECPKG_ATTR_CIPHER_STRENGTHS: semi-stub\n");
276 r->dwMinimumCipherStrength = 40;
277 r->dwMaximumCipherStrength = 168;
278 ret = SEC_E_OK;
280 else
281 ret = SEC_E_INTERNAL_ERROR;
282 break;
283 case SECPKG_ATTR_SUPPORTED_PROTOCOLS:
284 if(pBuffer) {
285 /* Regardless of MSDN documentation, tests show that this attribute takes into account
286 * what protocols are enabled for given credential. */
287 ((SecPkgCred_SupportedProtocols*)pBuffer)->grbitProtocol = cred->enabled_protocols;
288 ret = SEC_E_OK;
289 }else {
290 ret = SEC_E_INTERNAL_ERROR;
292 break;
293 default:
294 ret = SEC_E_UNSUPPORTED_FUNCTION;
296 return ret;
299 static SECURITY_STATUS SEC_ENTRY schan_QueryCredentialsAttributesA(
300 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
302 SECURITY_STATUS ret;
304 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
306 switch (ulAttribute)
308 case SECPKG_CRED_ATTR_NAMES:
309 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
310 ret = SEC_E_UNSUPPORTED_FUNCTION;
311 break;
312 default:
313 ret = schan_QueryCredentialsAttributes(phCredential, ulAttribute,
314 pBuffer);
316 return ret;
319 static SECURITY_STATUS SEC_ENTRY schan_QueryCredentialsAttributesW(
320 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
322 SECURITY_STATUS ret;
324 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
326 switch (ulAttribute)
328 case SECPKG_CRED_ATTR_NAMES:
329 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
330 ret = SEC_E_UNSUPPORTED_FUNCTION;
331 break;
332 default:
333 ret = schan_QueryCredentialsAttributes(phCredential, ulAttribute,
334 pBuffer);
336 return ret;
339 static SECURITY_STATUS schan_CheckCreds(const SCHANNEL_CRED *schanCred)
341 SECURITY_STATUS st;
342 DWORD i;
344 TRACE("dwVersion = %d\n", schanCred->dwVersion);
345 TRACE("cCreds = %d\n", schanCred->cCreds);
346 TRACE("hRootStore = %p\n", schanCred->hRootStore);
347 TRACE("cMappers = %d\n", schanCred->cMappers);
348 TRACE("cSupportedAlgs = %d:\n", schanCred->cSupportedAlgs);
349 for (i = 0; i < schanCred->cSupportedAlgs; i++)
350 TRACE("%08x\n", schanCred->palgSupportedAlgs[i]);
351 TRACE("grbitEnabledProtocols = %08x\n", schanCred->grbitEnabledProtocols);
352 TRACE("dwMinimumCipherStrength = %d\n", schanCred->dwMinimumCipherStrength);
353 TRACE("dwMaximumCipherStrength = %d\n", schanCred->dwMaximumCipherStrength);
354 TRACE("dwSessionLifespan = %d\n", schanCred->dwSessionLifespan);
355 TRACE("dwFlags = %08x\n", schanCred->dwFlags);
356 TRACE("dwCredFormat = %d\n", schanCred->dwCredFormat);
358 switch (schanCred->dwVersion)
360 case SCH_CRED_V3:
361 case SCHANNEL_CRED_VERSION:
362 break;
363 default:
364 return SEC_E_INTERNAL_ERROR;
367 if (schanCred->cCreds == 0)
368 st = SEC_E_NO_CREDENTIALS;
369 else if (schanCred->cCreds > 1)
370 st = SEC_E_UNKNOWN_CREDENTIALS;
371 else
373 DWORD keySpec;
374 HCRYPTPROV csp;
375 BOOL ret, freeCSP;
377 ret = CryptAcquireCertificatePrivateKey(schanCred->paCred[0],
378 0, /* FIXME: what flags to use? */ NULL,
379 &csp, &keySpec, &freeCSP);
380 if (ret)
382 st = SEC_E_OK;
383 if (freeCSP)
384 CryptReleaseContext(csp, 0);
386 else
387 st = SEC_E_UNKNOWN_CREDENTIALS;
389 return st;
392 static SECURITY_STATUS schan_AcquireClientCredentials(const SCHANNEL_CRED *schanCred,
393 PCredHandle phCredential, PTimeStamp ptsExpiry)
395 struct schan_credentials *creds;
396 unsigned enabled_protocols;
397 ULONG_PTR handle;
398 SECURITY_STATUS st = SEC_E_OK;
400 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred, phCredential, ptsExpiry);
402 if (schanCred)
404 st = schan_CheckCreds(schanCred);
405 if (st != SEC_E_OK && st != SEC_E_NO_CREDENTIALS)
406 return st;
408 st = SEC_E_OK;
411 read_config();
412 if(schanCred && schanCred->grbitEnabledProtocols)
413 enabled_protocols = schanCred->grbitEnabledProtocols & config_enabled_protocols;
414 else
415 enabled_protocols = config_enabled_protocols & ~config_default_disabled_protocols;
416 if(!enabled_protocols) {
417 ERR("Could not find matching protocol\n");
418 return SEC_E_NO_AUTHENTICATING_AUTHORITY;
421 /* For now, the only thing I'm interested in is the direction of the
422 * connection, so just store it.
424 creds = HeapAlloc(GetProcessHeap(), 0, sizeof(*creds));
425 if (!creds) return SEC_E_INSUFFICIENT_MEMORY;
427 handle = schan_alloc_handle(creds, SCHAN_HANDLE_CRED);
428 if (handle == SCHAN_INVALID_HANDLE) goto fail;
430 creds->credential_use = SECPKG_CRED_OUTBOUND;
431 if (!schan_imp_allocate_certificate_credentials(creds))
433 schan_free_handle(handle, SCHAN_HANDLE_CRED);
434 goto fail;
437 creds->enabled_protocols = enabled_protocols;
438 phCredential->dwLower = handle;
439 phCredential->dwUpper = 0;
441 /* Outbound credentials have no expiry */
442 if (ptsExpiry)
444 ptsExpiry->LowPart = 0;
445 ptsExpiry->HighPart = 0;
448 return st;
450 fail:
451 HeapFree(GetProcessHeap(), 0, creds);
452 return SEC_E_INTERNAL_ERROR;
455 static SECURITY_STATUS schan_AcquireServerCredentials(const SCHANNEL_CRED *schanCred,
456 PCredHandle phCredential, PTimeStamp ptsExpiry)
458 SECURITY_STATUS st;
460 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred, phCredential, ptsExpiry);
462 if (!schanCred) return SEC_E_NO_CREDENTIALS;
464 st = schan_CheckCreds(schanCred);
465 if (st == SEC_E_OK)
467 ULONG_PTR handle;
468 struct schan_credentials *creds;
470 creds = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*creds));
471 if (!creds) return SEC_E_INSUFFICIENT_MEMORY;
472 creds->credential_use = SECPKG_CRED_INBOUND;
474 handle = schan_alloc_handle(creds, SCHAN_HANDLE_CRED);
475 if (handle == SCHAN_INVALID_HANDLE)
477 HeapFree(GetProcessHeap(), 0, creds);
478 return SEC_E_INTERNAL_ERROR;
481 phCredential->dwLower = handle;
482 phCredential->dwUpper = 0;
484 /* FIXME: get expiry from cert */
486 return st;
489 static SECURITY_STATUS schan_AcquireCredentialsHandle(ULONG fCredentialUse,
490 const SCHANNEL_CRED *schanCred, PCredHandle phCredential, PTimeStamp ptsExpiry)
492 SECURITY_STATUS ret;
494 if (fCredentialUse == SECPKG_CRED_OUTBOUND)
495 ret = schan_AcquireClientCredentials(schanCred, phCredential,
496 ptsExpiry);
497 else
498 ret = schan_AcquireServerCredentials(schanCred, phCredential,
499 ptsExpiry);
500 return ret;
503 static SECURITY_STATUS SEC_ENTRY schan_AcquireCredentialsHandleA(
504 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
505 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
506 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
508 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
509 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
510 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
511 return schan_AcquireCredentialsHandle(fCredentialUse,
512 pAuthData, phCredential, ptsExpiry);
515 static SECURITY_STATUS SEC_ENTRY schan_AcquireCredentialsHandleW(
516 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
517 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
518 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
520 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
521 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
522 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
523 return schan_AcquireCredentialsHandle(fCredentialUse,
524 pAuthData, phCredential, ptsExpiry);
527 static SECURITY_STATUS SEC_ENTRY schan_FreeCredentialsHandle(
528 PCredHandle phCredential)
530 struct schan_credentials *creds;
532 TRACE("phCredential %p\n", phCredential);
534 if (!phCredential) return SEC_E_INVALID_HANDLE;
536 creds = schan_free_handle(phCredential->dwLower, SCHAN_HANDLE_CRED);
537 if (!creds) return SEC_E_INVALID_HANDLE;
539 if (creds->credential_use == SECPKG_CRED_OUTBOUND)
540 schan_imp_free_certificate_credentials(creds);
541 HeapFree(GetProcessHeap(), 0, creds);
543 return SEC_E_OK;
546 static void init_schan_buffers(struct schan_buffers *s, const PSecBufferDesc desc,
547 int (*get_next_buffer)(const struct schan_transport *, struct schan_buffers *))
549 s->offset = 0;
550 s->limit = ~0UL;
551 s->desc = desc;
552 s->current_buffer_idx = -1;
553 s->allow_buffer_resize = FALSE;
554 s->get_next_buffer = get_next_buffer;
557 static int schan_find_sec_buffer_idx(const SecBufferDesc *desc, unsigned int start_idx, ULONG buffer_type)
559 unsigned int i;
560 PSecBuffer buffer;
562 for (i = start_idx; i < desc->cBuffers; ++i)
564 buffer = &desc->pBuffers[i];
565 if (buffer->BufferType == buffer_type) return i;
568 return -1;
571 static void schan_resize_current_buffer(const struct schan_buffers *s, SIZE_T min_size)
573 SecBuffer *b = &s->desc->pBuffers[s->current_buffer_idx];
574 SIZE_T new_size = b->cbBuffer ? b->cbBuffer * 2 : 128;
575 void *new_data;
577 if (b->cbBuffer >= min_size || !s->allow_buffer_resize || min_size > UINT_MAX / 2) return;
579 while (new_size < min_size) new_size *= 2;
581 if (b->pvBuffer)
582 new_data = HeapReAlloc(GetProcessHeap(), 0, b->pvBuffer, new_size);
583 else
584 new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
586 if (!new_data)
588 TRACE("Failed to resize %p from %d to %ld\n", b->pvBuffer, b->cbBuffer, new_size);
589 return;
592 b->cbBuffer = new_size;
593 b->pvBuffer = new_data;
596 char *schan_get_buffer(const struct schan_transport *t, struct schan_buffers *s, SIZE_T *count)
598 SIZE_T max_count;
599 PSecBuffer buffer;
601 if (!s->desc)
603 TRACE("No desc\n");
604 return NULL;
607 if (s->current_buffer_idx == -1)
609 /* Initial buffer */
610 int buffer_idx = s->get_next_buffer(t, s);
611 if (buffer_idx == -1)
613 TRACE("No next buffer\n");
614 return NULL;
616 s->current_buffer_idx = buffer_idx;
619 buffer = &s->desc->pBuffers[s->current_buffer_idx];
620 TRACE("Using buffer %d: cbBuffer %d, BufferType %#x, pvBuffer %p\n", s->current_buffer_idx, buffer->cbBuffer, buffer->BufferType, buffer->pvBuffer);
622 schan_resize_current_buffer(s, s->offset + *count);
623 max_count = buffer->cbBuffer - s->offset;
624 if (s->limit != ~0UL && s->limit < max_count)
625 max_count = s->limit;
626 if (!max_count)
628 int buffer_idx;
630 s->allow_buffer_resize = FALSE;
631 buffer_idx = s->get_next_buffer(t, s);
632 if (buffer_idx == -1)
634 TRACE("No next buffer\n");
635 return NULL;
637 s->current_buffer_idx = buffer_idx;
638 s->offset = 0;
639 return schan_get_buffer(t, s, count);
642 if (*count > max_count)
643 *count = max_count;
644 if (s->limit != ~0UL)
645 s->limit -= *count;
647 return (char *)buffer->pvBuffer + s->offset;
650 /* schan_pull
651 * Read data from the transport input buffer.
653 * t - The session transport object.
654 * buff - The buffer into which to store the read data. Must be at least
655 * *buff_len bytes in length.
656 * buff_len - On input, *buff_len is the desired length to read. On successful
657 * return, *buff_len is the number of bytes actually read.
659 * Returns:
660 * 0 on success, in which case:
661 * *buff_len == 0 indicates end of file.
662 * *buff_len > 0 indicates that some data was read. May be less than
663 * what was requested, in which case the caller should call again if/
664 * when they want more.
665 * EAGAIN when no data could be read without blocking
666 * another errno-style error value on failure
669 int schan_pull(struct schan_transport *t, void *buff, size_t *buff_len)
671 char *b;
672 SIZE_T local_len = *buff_len;
674 TRACE("Pull %lu bytes\n", local_len);
676 *buff_len = 0;
678 b = schan_get_buffer(t, &t->in, &local_len);
679 if (!b)
680 return EAGAIN;
682 memcpy(buff, b, local_len);
683 t->in.offset += local_len;
685 TRACE("Read %lu bytes\n", local_len);
687 *buff_len = local_len;
688 return 0;
691 /* schan_push
692 * Write data to the transport output buffer.
694 * t - The session transport object.
695 * buff - The buffer of data to write. Must be at least *buff_len bytes in length.
696 * buff_len - On input, *buff_len is the desired length to write. On successful
697 * return, *buff_len is the number of bytes actually written.
699 * Returns:
700 * 0 on success
701 * *buff_len will be > 0 indicating how much data was written. May be less
702 * than what was requested, in which case the caller should call again
703 if/when they want to write more.
704 * EAGAIN when no data could be written without blocking
705 * another errno-style error value on failure
708 int schan_push(struct schan_transport *t, const void *buff, size_t *buff_len)
710 char *b;
711 SIZE_T local_len = *buff_len;
713 TRACE("Push %lu bytes\n", local_len);
715 *buff_len = 0;
717 b = schan_get_buffer(t, &t->out, &local_len);
718 if (!b)
719 return EAGAIN;
721 memcpy(b, buff, local_len);
722 t->out.offset += local_len;
724 TRACE("Wrote %lu bytes\n", local_len);
726 *buff_len = local_len;
727 return 0;
730 schan_imp_session schan_session_for_transport(struct schan_transport* t)
732 return t->ctx->session;
735 static int schan_init_sec_ctx_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
737 if (s->current_buffer_idx == -1)
739 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
740 if (t->ctx->req_ctx_attr & ISC_REQ_ALLOCATE_MEMORY)
742 if (idx == -1)
744 idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_EMPTY);
745 if (idx != -1) s->desc->pBuffers[idx].BufferType = SECBUFFER_TOKEN;
747 if (idx != -1 && !s->desc->pBuffers[idx].pvBuffer)
749 s->desc->pBuffers[idx].cbBuffer = 0;
750 s->allow_buffer_resize = TRUE;
753 return idx;
756 return -1;
759 static void dump_buffer_desc(SecBufferDesc *desc)
761 unsigned int i;
763 if (!desc) return;
764 TRACE("Buffer desc %p:\n", desc);
765 for (i = 0; i < desc->cBuffers; ++i)
767 SecBuffer *b = &desc->pBuffers[i];
768 TRACE("\tbuffer %u: cbBuffer %d, BufferType %#x pvBuffer %p\n", i, b->cbBuffer, b->BufferType, b->pvBuffer);
772 /***********************************************************************
773 * InitializeSecurityContextW
775 static SECURITY_STATUS SEC_ENTRY schan_InitializeSecurityContextW(
776 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
777 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
778 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
779 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
781 struct schan_context *ctx;
782 struct schan_buffers *out_buffers;
783 struct schan_credentials *cred;
784 struct schan_transport transport;
785 SIZE_T expected_size = ~0UL;
786 SECURITY_STATUS ret;
788 TRACE("%p %p %s 0x%08x %d %d %p %d %p %p %p %p\n", phCredential, phContext,
789 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
790 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
792 dump_buffer_desc(pInput);
793 dump_buffer_desc(pOutput);
795 if (!phContext)
797 ULONG_PTR handle;
799 if (!phCredential) return SEC_E_INVALID_HANDLE;
801 cred = schan_get_object(phCredential->dwLower, SCHAN_HANDLE_CRED);
802 if (!cred) return SEC_E_INVALID_HANDLE;
804 if (!(cred->credential_use & SECPKG_CRED_OUTBOUND))
806 WARN("Invalid credential use %#x\n", cred->credential_use);
807 return SEC_E_INVALID_HANDLE;
810 ctx = HeapAlloc(GetProcessHeap(), 0, sizeof(*ctx));
811 if (!ctx) return SEC_E_INSUFFICIENT_MEMORY;
813 ctx->cert = NULL;
814 handle = schan_alloc_handle(ctx, SCHAN_HANDLE_CTX);
815 if (handle == SCHAN_INVALID_HANDLE)
817 HeapFree(GetProcessHeap(), 0, ctx);
818 return SEC_E_INTERNAL_ERROR;
821 if (!schan_imp_create_session(&ctx->session, cred))
823 schan_free_handle(handle, SCHAN_HANDLE_CTX);
824 HeapFree(GetProcessHeap(), 0, ctx);
825 return SEC_E_INTERNAL_ERROR;
828 if (pszTargetName)
830 UINT len = WideCharToMultiByte( CP_UNIXCP, 0, pszTargetName, -1, NULL, 0, NULL, NULL );
831 char *target = HeapAlloc( GetProcessHeap(), 0, len );
833 if (target)
835 WideCharToMultiByte( CP_UNIXCP, 0, pszTargetName, -1, target, len, NULL, NULL );
836 schan_imp_set_session_target( ctx->session, target );
837 HeapFree( GetProcessHeap(), 0, target );
840 phNewContext->dwLower = handle;
841 phNewContext->dwUpper = 0;
843 else
845 SIZE_T record_size = 0;
846 unsigned char *ptr;
847 SecBuffer *buffer;
848 int idx;
850 if (!pInput)
851 return SEC_E_INCOMPLETE_MESSAGE;
853 idx = schan_find_sec_buffer_idx(pInput, 0, SECBUFFER_TOKEN);
854 if (idx == -1)
855 return SEC_E_INCOMPLETE_MESSAGE;
857 buffer = &pInput->pBuffers[idx];
858 ptr = buffer->pvBuffer;
859 expected_size = 0;
861 while (buffer->cbBuffer > expected_size + 5)
863 record_size = 5 + ((ptr[3] << 8) | ptr[4]);
865 if (buffer->cbBuffer < expected_size + record_size)
866 break;
868 expected_size += record_size;
869 ptr += record_size;
872 if (!expected_size)
874 TRACE("Expected at least %lu bytes, but buffer only contains %u bytes.\n",
875 max(6, record_size), buffer->cbBuffer);
876 return SEC_E_INCOMPLETE_MESSAGE;
879 TRACE("Using expected_size %lu.\n", expected_size);
881 ctx = schan_get_object(phContext->dwLower, SCHAN_HANDLE_CTX);
884 ctx->req_ctx_attr = fContextReq;
886 transport.ctx = ctx;
887 init_schan_buffers(&transport.in, pInput, schan_init_sec_ctx_get_next_buffer);
888 transport.in.limit = expected_size;
889 init_schan_buffers(&transport.out, pOutput, schan_init_sec_ctx_get_next_buffer);
890 schan_imp_set_session_transport(ctx->session, &transport);
892 /* Perform the TLS handshake */
893 ret = schan_imp_handshake(ctx->session);
895 if(transport.in.offset && transport.in.offset != pInput->pBuffers[0].cbBuffer) {
896 if(pInput->cBuffers<2 || pInput->pBuffers[1].BufferType!=SECBUFFER_EMPTY)
897 return SEC_E_INVALID_TOKEN;
899 pInput->pBuffers[1].BufferType = SECBUFFER_EXTRA;
900 pInput->pBuffers[1].cbBuffer = pInput->pBuffers[0].cbBuffer-transport.in.offset;
903 out_buffers = &transport.out;
904 if (out_buffers->current_buffer_idx != -1)
906 SecBuffer *buffer = &out_buffers->desc->pBuffers[out_buffers->current_buffer_idx];
907 buffer->cbBuffer = out_buffers->offset;
910 *pfContextAttr = 0;
911 if (ctx->req_ctx_attr & ISC_REQ_ALLOCATE_MEMORY)
912 *pfContextAttr |= ISC_RET_ALLOCATED_MEMORY;
914 return ret;
917 /***********************************************************************
918 * InitializeSecurityContextA
920 static SECURITY_STATUS SEC_ENTRY schan_InitializeSecurityContextA(
921 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
922 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
923 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
924 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
926 SECURITY_STATUS ret;
927 SEC_WCHAR *target_name = NULL;
929 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
930 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
931 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
933 if (pszTargetName)
935 INT len = MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, NULL, 0);
936 target_name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*target_name));
937 MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, target_name, len);
940 ret = schan_InitializeSecurityContextW(phCredential, phContext, target_name,
941 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
942 phNewContext, pOutput, pfContextAttr, ptsExpiry);
944 HeapFree(GetProcessHeap(), 0, target_name);
946 return ret;
949 static SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesW(
950 PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
952 struct schan_context *ctx;
954 TRACE("context_handle %p, attribute %#x, buffer %p\n",
955 context_handle, attribute, buffer);
957 if (!context_handle) return SEC_E_INVALID_HANDLE;
958 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
960 switch(attribute)
962 case SECPKG_ATTR_STREAM_SIZES:
964 SecPkgContext_ConnectionInfo info;
965 SECURITY_STATUS status = schan_imp_get_connection_info(ctx->session, &info);
966 if (status == SEC_E_OK)
968 SecPkgContext_StreamSizes *stream_sizes = buffer;
969 SIZE_T mac_size = info.dwHashStrength;
970 unsigned int block_size = schan_imp_get_session_cipher_block_size(ctx->session);
971 unsigned int message_size = schan_imp_get_max_message_size(ctx->session);
973 TRACE("Using %lu mac bytes, message size %u, block size %u\n",
974 mac_size, message_size, block_size);
976 /* These are defined by the TLS RFC */
977 stream_sizes->cbHeader = 5;
978 stream_sizes->cbTrailer = mac_size + 256; /* Max 255 bytes padding + 1 for padding size */
979 stream_sizes->cbMaximumMessage = message_size;
980 stream_sizes->cbBuffers = 4;
981 stream_sizes->cbBlockSize = block_size;
984 return status;
986 case SECPKG_ATTR_REMOTE_CERT_CONTEXT:
988 PCCERT_CONTEXT *cert = buffer;
990 if (!ctx->cert) {
991 HCERTSTORE cert_store;
992 SECURITY_STATUS status;
994 cert_store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, CERT_STORE_CREATE_NEW_FLAG, NULL);
995 if(!cert_store)
996 return GetLastError();
998 status = schan_imp_get_session_peer_certificate(ctx->session, cert_store, &ctx->cert);
999 CertCloseStore(cert_store, 0);
1000 if(status != SEC_E_OK)
1001 return status;
1004 *cert = CertDuplicateCertificateContext(ctx->cert);
1005 return SEC_E_OK;
1007 case SECPKG_ATTR_CONNECTION_INFO:
1009 SecPkgContext_ConnectionInfo *info = buffer;
1010 return schan_imp_get_connection_info(ctx->session, info);
1013 default:
1014 FIXME("Unhandled attribute %#x\n", attribute);
1015 return SEC_E_UNSUPPORTED_FUNCTION;
1019 static SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesA(
1020 PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
1022 TRACE("context_handle %p, attribute %#x, buffer %p\n",
1023 context_handle, attribute, buffer);
1025 switch(attribute)
1027 case SECPKG_ATTR_STREAM_SIZES:
1028 return schan_QueryContextAttributesW(context_handle, attribute, buffer);
1029 case SECPKG_ATTR_REMOTE_CERT_CONTEXT:
1030 return schan_QueryContextAttributesW(context_handle, attribute, buffer);
1031 case SECPKG_ATTR_CONNECTION_INFO:
1032 return schan_QueryContextAttributesW(context_handle, attribute, buffer);
1034 default:
1035 FIXME("Unhandled attribute %#x\n", attribute);
1036 return SEC_E_UNSUPPORTED_FUNCTION;
1040 static int schan_encrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
1042 SecBuffer *b;
1044 if (s->current_buffer_idx == -1)
1045 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_HEADER);
1047 b = &s->desc->pBuffers[s->current_buffer_idx];
1049 if (b->BufferType == SECBUFFER_STREAM_HEADER)
1050 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
1052 if (b->BufferType == SECBUFFER_DATA)
1053 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_TRAILER);
1055 return -1;
1058 static int schan_encrypt_message_get_next_buffer_token(const struct schan_transport *t, struct schan_buffers *s)
1060 SecBuffer *b;
1062 if (s->current_buffer_idx == -1)
1063 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
1065 b = &s->desc->pBuffers[s->current_buffer_idx];
1067 if (b->BufferType == SECBUFFER_TOKEN)
1069 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
1070 if (idx != s->current_buffer_idx) return -1;
1071 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
1074 if (b->BufferType == SECBUFFER_DATA)
1076 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
1077 if (idx != -1)
1078 idx = schan_find_sec_buffer_idx(s->desc, idx + 1, SECBUFFER_TOKEN);
1079 return idx;
1082 return -1;
1085 static SECURITY_STATUS SEC_ENTRY schan_EncryptMessage(PCtxtHandle context_handle,
1086 ULONG quality, PSecBufferDesc message, ULONG message_seq_no)
1088 struct schan_transport transport;
1089 struct schan_context *ctx;
1090 struct schan_buffers *b;
1091 SECURITY_STATUS status;
1092 SecBuffer *buffer;
1093 SIZE_T data_size;
1094 SIZE_T length;
1095 char *data;
1096 int idx;
1098 TRACE("context_handle %p, quality %d, message %p, message_seq_no %d\n",
1099 context_handle, quality, message, message_seq_no);
1101 if (!context_handle) return SEC_E_INVALID_HANDLE;
1102 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
1104 dump_buffer_desc(message);
1106 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_DATA);
1107 if (idx == -1)
1109 WARN("No data buffer passed\n");
1110 return SEC_E_INTERNAL_ERROR;
1112 buffer = &message->pBuffers[idx];
1114 data_size = buffer->cbBuffer;
1115 data = HeapAlloc(GetProcessHeap(), 0, data_size);
1116 memcpy(data, buffer->pvBuffer, data_size);
1118 transport.ctx = ctx;
1119 init_schan_buffers(&transport.in, NULL, NULL);
1120 if (schan_find_sec_buffer_idx(message, 0, SECBUFFER_STREAM_HEADER) != -1)
1121 init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer);
1122 else
1123 init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer_token);
1124 schan_imp_set_session_transport(ctx->session, &transport);
1126 length = data_size;
1127 status = schan_imp_send(ctx->session, data, &length);
1129 TRACE("Sent %ld bytes.\n", length);
1131 if (length != data_size)
1132 status = SEC_E_INTERNAL_ERROR;
1134 b = &transport.out;
1135 b->desc->pBuffers[b->current_buffer_idx].cbBuffer = b->offset;
1136 HeapFree(GetProcessHeap(), 0, data);
1138 TRACE("Returning %#x.\n", status);
1140 return status;
1143 static int schan_decrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
1145 if (s->current_buffer_idx == -1)
1146 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
1148 return -1;
1151 static int schan_validate_decrypt_buffer_desc(PSecBufferDesc message)
1153 int data_idx = -1;
1154 unsigned int empty_count = 0;
1155 unsigned int i;
1157 if (message->cBuffers < 4)
1159 WARN("Less than four buffers passed\n");
1160 return -1;
1163 for (i = 0; i < message->cBuffers; ++i)
1165 SecBuffer *b = &message->pBuffers[i];
1166 if (b->BufferType == SECBUFFER_DATA)
1168 if (data_idx != -1)
1170 WARN("More than one data buffer passed\n");
1171 return -1;
1173 data_idx = i;
1175 else if (b->BufferType == SECBUFFER_EMPTY)
1176 ++empty_count;
1179 if (data_idx == -1)
1181 WARN("No data buffer passed\n");
1182 return -1;
1185 if (empty_count < 3)
1187 WARN("Less than three empty buffers passed\n");
1188 return -1;
1191 return data_idx;
1194 static void schan_decrypt_fill_buffer(PSecBufferDesc message, ULONG buffer_type, void *data, ULONG size)
1196 int idx;
1197 SecBuffer *buffer;
1199 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_EMPTY);
1200 buffer = &message->pBuffers[idx];
1202 buffer->BufferType = buffer_type;
1203 buffer->pvBuffer = data;
1204 buffer->cbBuffer = size;
1207 static SECURITY_STATUS SEC_ENTRY schan_DecryptMessage(PCtxtHandle context_handle,
1208 PSecBufferDesc message, ULONG message_seq_no, PULONG quality)
1210 struct schan_transport transport;
1211 struct schan_context *ctx;
1212 SecBuffer *buffer;
1213 SIZE_T data_size;
1214 char *data;
1215 unsigned expected_size;
1216 SSIZE_T received = 0;
1217 int idx;
1218 unsigned char *buf_ptr;
1220 TRACE("context_handle %p, message %p, message_seq_no %d, quality %p\n",
1221 context_handle, message, message_seq_no, quality);
1223 if (!context_handle) return SEC_E_INVALID_HANDLE;
1224 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
1226 dump_buffer_desc(message);
1228 idx = schan_validate_decrypt_buffer_desc(message);
1229 if (idx == -1)
1230 return SEC_E_INVALID_TOKEN;
1231 buffer = &message->pBuffers[idx];
1232 buf_ptr = buffer->pvBuffer;
1234 expected_size = 5 + ((buf_ptr[3] << 8) | buf_ptr[4]);
1235 if(buffer->cbBuffer < expected_size)
1237 TRACE("Expected %u bytes, but buffer only contains %u bytes\n", expected_size, buffer->cbBuffer);
1238 buffer->BufferType = SECBUFFER_MISSING;
1239 buffer->cbBuffer = expected_size - buffer->cbBuffer;
1241 /* This is a bit weird, but windows does it too */
1242 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_EMPTY);
1243 buffer = &message->pBuffers[idx];
1244 buffer->BufferType = SECBUFFER_MISSING;
1245 buffer->cbBuffer = expected_size - buffer->cbBuffer;
1247 TRACE("Returning SEC_E_INCOMPLETE_MESSAGE\n");
1248 return SEC_E_INCOMPLETE_MESSAGE;
1251 data_size = expected_size - 5;
1252 data = HeapAlloc(GetProcessHeap(), 0, data_size);
1254 transport.ctx = ctx;
1255 init_schan_buffers(&transport.in, message, schan_decrypt_message_get_next_buffer);
1256 transport.in.limit = expected_size;
1257 init_schan_buffers(&transport.out, NULL, NULL);
1258 schan_imp_set_session_transport(ctx->session, &transport);
1260 while (received < data_size)
1262 SIZE_T length = data_size - received;
1263 SECURITY_STATUS status = schan_imp_recv(ctx->session, data + received, &length);
1265 if (status == SEC_I_CONTINUE_NEEDED)
1266 break;
1268 if (status != SEC_E_OK)
1270 HeapFree(GetProcessHeap(), 0, data);
1271 ERR("Returning %x\n", status);
1272 return status;
1275 if (!length)
1276 break;
1278 received += length;
1281 TRACE("Received %ld bytes\n", received);
1283 memcpy(buf_ptr + 5, data, received);
1284 HeapFree(GetProcessHeap(), 0, data);
1286 schan_decrypt_fill_buffer(message, SECBUFFER_DATA,
1287 buf_ptr + 5, received);
1289 schan_decrypt_fill_buffer(message, SECBUFFER_STREAM_TRAILER,
1290 buf_ptr + 5 + received, buffer->cbBuffer - 5 - received);
1292 if(buffer->cbBuffer > expected_size)
1293 schan_decrypt_fill_buffer(message, SECBUFFER_EXTRA,
1294 buf_ptr + expected_size, buffer->cbBuffer - expected_size);
1296 buffer->BufferType = SECBUFFER_STREAM_HEADER;
1297 buffer->cbBuffer = 5;
1299 return SEC_E_OK;
1302 static SECURITY_STATUS SEC_ENTRY schan_DeleteSecurityContext(PCtxtHandle context_handle)
1304 struct schan_context *ctx;
1306 TRACE("context_handle %p\n", context_handle);
1308 if (!context_handle) return SEC_E_INVALID_HANDLE;
1310 ctx = schan_free_handle(context_handle->dwLower, SCHAN_HANDLE_CTX);
1311 if (!ctx) return SEC_E_INVALID_HANDLE;
1313 if (ctx->cert)
1314 CertFreeCertificateContext(ctx->cert);
1315 schan_imp_dispose_session(ctx->session);
1316 HeapFree(GetProcessHeap(), 0, ctx);
1318 return SEC_E_OK;
1321 static const SecurityFunctionTableA schanTableA = {
1323 NULL, /* EnumerateSecurityPackagesA */
1324 schan_QueryCredentialsAttributesA,
1325 schan_AcquireCredentialsHandleA,
1326 schan_FreeCredentialsHandle,
1327 NULL, /* Reserved2 */
1328 schan_InitializeSecurityContextA,
1329 NULL, /* AcceptSecurityContext */
1330 NULL, /* CompleteAuthToken */
1331 schan_DeleteSecurityContext,
1332 NULL, /* ApplyControlToken */
1333 schan_QueryContextAttributesA,
1334 NULL, /* ImpersonateSecurityContext */
1335 NULL, /* RevertSecurityContext */
1336 NULL, /* MakeSignature */
1337 NULL, /* VerifySignature */
1338 FreeContextBuffer,
1339 NULL, /* QuerySecurityPackageInfoA */
1340 NULL, /* Reserved3 */
1341 NULL, /* Reserved4 */
1342 NULL, /* ExportSecurityContext */
1343 NULL, /* ImportSecurityContextA */
1344 NULL, /* AddCredentialsA */
1345 NULL, /* Reserved8 */
1346 NULL, /* QuerySecurityContextToken */
1347 schan_EncryptMessage,
1348 schan_DecryptMessage,
1349 NULL, /* SetContextAttributesA */
1352 static const SecurityFunctionTableW schanTableW = {
1354 NULL, /* EnumerateSecurityPackagesW */
1355 schan_QueryCredentialsAttributesW,
1356 schan_AcquireCredentialsHandleW,
1357 schan_FreeCredentialsHandle,
1358 NULL, /* Reserved2 */
1359 schan_InitializeSecurityContextW,
1360 NULL, /* AcceptSecurityContext */
1361 NULL, /* CompleteAuthToken */
1362 schan_DeleteSecurityContext,
1363 NULL, /* ApplyControlToken */
1364 schan_QueryContextAttributesW,
1365 NULL, /* ImpersonateSecurityContext */
1366 NULL, /* RevertSecurityContext */
1367 NULL, /* MakeSignature */
1368 NULL, /* VerifySignature */
1369 FreeContextBuffer,
1370 NULL, /* QuerySecurityPackageInfoW */
1371 NULL, /* Reserved3 */
1372 NULL, /* Reserved4 */
1373 NULL, /* ExportSecurityContext */
1374 NULL, /* ImportSecurityContextW */
1375 NULL, /* AddCredentialsW */
1376 NULL, /* Reserved8 */
1377 NULL, /* QuerySecurityContextToken */
1378 schan_EncryptMessage,
1379 schan_DecryptMessage,
1380 NULL, /* SetContextAttributesW */
1383 static const WCHAR schannelComment[] = { 'S','c','h','a','n','n','e','l',' ',
1384 'S','e','c','u','r','i','t','y',' ','P','a','c','k','a','g','e',0 };
1385 static const WCHAR schannelDllName[] = { 's','c','h','a','n','n','e','l','.','d','l','l',0 };
1387 void SECUR32_initSchannelSP(void)
1389 /* This is what Windows reports. This shouldn't break any applications
1390 * even though the functions are missing, because the wrapper will
1391 * return SEC_E_UNSUPPORTED_FUNCTION if our function is NULL.
1393 static const LONG caps =
1394 SECPKG_FLAG_INTEGRITY |
1395 SECPKG_FLAG_PRIVACY |
1396 SECPKG_FLAG_CONNECTION |
1397 SECPKG_FLAG_MULTI_REQUIRED |
1398 SECPKG_FLAG_EXTENDED_ERROR |
1399 SECPKG_FLAG_IMPERSONATION |
1400 SECPKG_FLAG_ACCEPT_WIN32_NAME |
1401 SECPKG_FLAG_STREAM;
1402 static const short version = 1;
1403 static const LONG maxToken = 16384;
1404 SEC_WCHAR *uniSPName = (SEC_WCHAR *)UNISP_NAME_W,
1405 *schannel = (SEC_WCHAR *)SCHANNEL_NAME_W;
1406 const SecPkgInfoW info[] = {
1407 { caps, version, UNISP_RPC_ID, maxToken, uniSPName, uniSPName },
1408 { caps, version, UNISP_RPC_ID, maxToken, schannel,
1409 (SEC_WCHAR *)schannelComment },
1411 SecureProvider *provider;
1413 if (!schan_imp_init())
1414 return;
1416 schan_handle_table = HeapAlloc(GetProcessHeap(), 0, 64 * sizeof(*schan_handle_table));
1417 if (!schan_handle_table)
1419 ERR("Failed to allocate schannel handle table.\n");
1420 goto fail;
1422 schan_handle_table_size = 64;
1424 provider = SECUR32_addProvider(&schanTableA, &schanTableW, schannelDllName);
1425 if (!provider)
1427 ERR("Failed to add schannel provider.\n");
1428 goto fail;
1431 SECUR32_addPackages(provider, sizeof(info) / sizeof(info[0]), NULL, info);
1433 return;
1435 fail:
1436 HeapFree(GetProcessHeap(), 0, schan_handle_table);
1437 schan_handle_table = NULL;
1438 schan_imp_deinit();
1439 return;
1442 void SECUR32_deinitSchannelSP(void)
1444 SIZE_T i = schan_handle_count;
1446 if (!schan_handle_table) return;
1448 /* deinitialized sessions first because a pointer to the credentials
1449 * may be stored for the session. */
1450 while (i--)
1452 if (schan_handle_table[i].type == SCHAN_HANDLE_CTX)
1454 struct schan_context *ctx = schan_free_handle(i, SCHAN_HANDLE_CTX);
1455 schan_imp_dispose_session(ctx->session);
1456 HeapFree(GetProcessHeap(), 0, ctx);
1459 i = schan_handle_count;
1460 while (i--)
1462 if (schan_handle_table[i].type != SCHAN_HANDLE_FREE)
1464 struct schan_credentials *cred;
1465 cred = schan_free_handle(i, SCHAN_HANDLE_CRED);
1466 schan_imp_free_certificate_credentials(cred);
1467 HeapFree(GetProcessHeap(), 0, cred);
1470 HeapFree(GetProcessHeap(), 0, schan_handle_table);
1471 schan_imp_deinit();
1474 #else /* SONAME_LIBGNUTLS || HAVE_SECURITY_SECURITY_H */
1476 void SECUR32_initSchannelSP(void)
1478 ERR("TLS library not found, SSL connections will fail\n");
1481 void SECUR32_deinitSchannelSP(void) {}
1483 #endif /* SONAME_LIBGNUTLS || HAVE_SECURITY_SECURITY_H */