secur32: Set the SSL server name to enable the SNI extension.
[wine.git] / dlls / secur32 / schannel.c
blob240062fea98463e0222760370d5d76de071899d9
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 HCERTSTORE cert_store;
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_store = 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_store) {
991 ctx->cert_store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, CERT_STORE_CREATE_NEW_FLAG, NULL);
992 if(!ctx->cert_store)
993 return GetLastError();
996 return schan_imp_get_session_peer_certificate(ctx->session, ctx->cert_store, cert);
998 case SECPKG_ATTR_CONNECTION_INFO:
1000 SecPkgContext_ConnectionInfo *info = buffer;
1001 return schan_imp_get_connection_info(ctx->session, info);
1004 default:
1005 FIXME("Unhandled attribute %#x\n", attribute);
1006 return SEC_E_UNSUPPORTED_FUNCTION;
1010 static SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesA(
1011 PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
1013 TRACE("context_handle %p, attribute %#x, buffer %p\n",
1014 context_handle, attribute, buffer);
1016 switch(attribute)
1018 case SECPKG_ATTR_STREAM_SIZES:
1019 return schan_QueryContextAttributesW(context_handle, attribute, buffer);
1020 case SECPKG_ATTR_REMOTE_CERT_CONTEXT:
1021 return schan_QueryContextAttributesW(context_handle, attribute, buffer);
1022 case SECPKG_ATTR_CONNECTION_INFO:
1023 return schan_QueryContextAttributesW(context_handle, attribute, buffer);
1025 default:
1026 FIXME("Unhandled attribute %#x\n", attribute);
1027 return SEC_E_UNSUPPORTED_FUNCTION;
1031 static int schan_encrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
1033 SecBuffer *b;
1035 if (s->current_buffer_idx == -1)
1036 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_HEADER);
1038 b = &s->desc->pBuffers[s->current_buffer_idx];
1040 if (b->BufferType == SECBUFFER_STREAM_HEADER)
1041 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
1043 if (b->BufferType == SECBUFFER_DATA)
1044 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_TRAILER);
1046 return -1;
1049 static int schan_encrypt_message_get_next_buffer_token(const struct schan_transport *t, struct schan_buffers *s)
1051 SecBuffer *b;
1053 if (s->current_buffer_idx == -1)
1054 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
1056 b = &s->desc->pBuffers[s->current_buffer_idx];
1058 if (b->BufferType == SECBUFFER_TOKEN)
1060 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
1061 if (idx != s->current_buffer_idx) return -1;
1062 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
1065 if (b->BufferType == SECBUFFER_DATA)
1067 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
1068 if (idx != -1)
1069 idx = schan_find_sec_buffer_idx(s->desc, idx + 1, SECBUFFER_TOKEN);
1070 return idx;
1073 return -1;
1076 static SECURITY_STATUS SEC_ENTRY schan_EncryptMessage(PCtxtHandle context_handle,
1077 ULONG quality, PSecBufferDesc message, ULONG message_seq_no)
1079 struct schan_transport transport;
1080 struct schan_context *ctx;
1081 struct schan_buffers *b;
1082 SECURITY_STATUS status;
1083 SecBuffer *buffer;
1084 SIZE_T data_size;
1085 SIZE_T length;
1086 char *data;
1087 int idx;
1089 TRACE("context_handle %p, quality %d, message %p, message_seq_no %d\n",
1090 context_handle, quality, message, message_seq_no);
1092 if (!context_handle) return SEC_E_INVALID_HANDLE;
1093 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
1095 dump_buffer_desc(message);
1097 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_DATA);
1098 if (idx == -1)
1100 WARN("No data buffer passed\n");
1101 return SEC_E_INTERNAL_ERROR;
1103 buffer = &message->pBuffers[idx];
1105 data_size = buffer->cbBuffer;
1106 data = HeapAlloc(GetProcessHeap(), 0, data_size);
1107 memcpy(data, buffer->pvBuffer, data_size);
1109 transport.ctx = ctx;
1110 init_schan_buffers(&transport.in, NULL, NULL);
1111 if (schan_find_sec_buffer_idx(message, 0, SECBUFFER_STREAM_HEADER) != -1)
1112 init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer);
1113 else
1114 init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer_token);
1115 schan_imp_set_session_transport(ctx->session, &transport);
1117 length = data_size;
1118 status = schan_imp_send(ctx->session, data, &length);
1120 TRACE("Sent %ld bytes.\n", length);
1122 if (length != data_size)
1123 status = SEC_E_INTERNAL_ERROR;
1125 b = &transport.out;
1126 b->desc->pBuffers[b->current_buffer_idx].cbBuffer = b->offset;
1127 HeapFree(GetProcessHeap(), 0, data);
1129 TRACE("Returning %#x.\n", status);
1131 return status;
1134 static int schan_decrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
1136 if (s->current_buffer_idx == -1)
1137 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
1139 return -1;
1142 static int schan_validate_decrypt_buffer_desc(PSecBufferDesc message)
1144 int data_idx = -1;
1145 unsigned int empty_count = 0;
1146 unsigned int i;
1148 if (message->cBuffers < 4)
1150 WARN("Less than four buffers passed\n");
1151 return -1;
1154 for (i = 0; i < message->cBuffers; ++i)
1156 SecBuffer *b = &message->pBuffers[i];
1157 if (b->BufferType == SECBUFFER_DATA)
1159 if (data_idx != -1)
1161 WARN("More than one data buffer passed\n");
1162 return -1;
1164 data_idx = i;
1166 else if (b->BufferType == SECBUFFER_EMPTY)
1167 ++empty_count;
1170 if (data_idx == -1)
1172 WARN("No data buffer passed\n");
1173 return -1;
1176 if (empty_count < 3)
1178 WARN("Less than three empty buffers passed\n");
1179 return -1;
1182 return data_idx;
1185 static void schan_decrypt_fill_buffer(PSecBufferDesc message, ULONG buffer_type, void *data, ULONG size)
1187 int idx;
1188 SecBuffer *buffer;
1190 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_EMPTY);
1191 buffer = &message->pBuffers[idx];
1193 buffer->BufferType = buffer_type;
1194 buffer->pvBuffer = data;
1195 buffer->cbBuffer = size;
1198 static SECURITY_STATUS SEC_ENTRY schan_DecryptMessage(PCtxtHandle context_handle,
1199 PSecBufferDesc message, ULONG message_seq_no, PULONG quality)
1201 struct schan_transport transport;
1202 struct schan_context *ctx;
1203 SecBuffer *buffer;
1204 SIZE_T data_size;
1205 char *data;
1206 unsigned expected_size;
1207 SSIZE_T received = 0;
1208 int idx;
1209 unsigned char *buf_ptr;
1211 TRACE("context_handle %p, message %p, message_seq_no %d, quality %p\n",
1212 context_handle, message, message_seq_no, quality);
1214 if (!context_handle) return SEC_E_INVALID_HANDLE;
1215 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
1217 dump_buffer_desc(message);
1219 idx = schan_validate_decrypt_buffer_desc(message);
1220 if (idx == -1)
1221 return SEC_E_INVALID_TOKEN;
1222 buffer = &message->pBuffers[idx];
1223 buf_ptr = buffer->pvBuffer;
1225 expected_size = 5 + ((buf_ptr[3] << 8) | buf_ptr[4]);
1226 if(buffer->cbBuffer < expected_size)
1228 TRACE("Expected %u bytes, but buffer only contains %u bytes\n", expected_size, buffer->cbBuffer);
1229 buffer->BufferType = SECBUFFER_MISSING;
1230 buffer->cbBuffer = expected_size - buffer->cbBuffer;
1232 /* This is a bit weird, but windows does it too */
1233 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_EMPTY);
1234 buffer = &message->pBuffers[idx];
1235 buffer->BufferType = SECBUFFER_MISSING;
1236 buffer->cbBuffer = expected_size - buffer->cbBuffer;
1238 TRACE("Returning SEC_E_INCOMPLETE_MESSAGE\n");
1239 return SEC_E_INCOMPLETE_MESSAGE;
1242 data_size = expected_size - 5;
1243 data = HeapAlloc(GetProcessHeap(), 0, data_size);
1245 transport.ctx = ctx;
1246 init_schan_buffers(&transport.in, message, schan_decrypt_message_get_next_buffer);
1247 transport.in.limit = expected_size;
1248 init_schan_buffers(&transport.out, NULL, NULL);
1249 schan_imp_set_session_transport(ctx->session, &transport);
1251 while (received < data_size)
1253 SIZE_T length = data_size - received;
1254 SECURITY_STATUS status = schan_imp_recv(ctx->session, data + received, &length);
1256 if (status == SEC_I_CONTINUE_NEEDED)
1257 break;
1259 if (status != SEC_E_OK)
1261 HeapFree(GetProcessHeap(), 0, data);
1262 ERR("Returning %x\n", status);
1263 return status;
1266 if (!length)
1267 break;
1269 received += length;
1272 TRACE("Received %ld bytes\n", received);
1274 memcpy(buf_ptr + 5, data, received);
1275 HeapFree(GetProcessHeap(), 0, data);
1277 schan_decrypt_fill_buffer(message, SECBUFFER_DATA,
1278 buf_ptr + 5, received);
1280 schan_decrypt_fill_buffer(message, SECBUFFER_STREAM_TRAILER,
1281 buf_ptr + 5 + received, buffer->cbBuffer - 5 - received);
1283 if(buffer->cbBuffer > expected_size)
1284 schan_decrypt_fill_buffer(message, SECBUFFER_EXTRA,
1285 buf_ptr + expected_size, buffer->cbBuffer - expected_size);
1287 buffer->BufferType = SECBUFFER_STREAM_HEADER;
1288 buffer->cbBuffer = 5;
1290 return SEC_E_OK;
1293 static SECURITY_STATUS SEC_ENTRY schan_DeleteSecurityContext(PCtxtHandle context_handle)
1295 struct schan_context *ctx;
1297 TRACE("context_handle %p\n", context_handle);
1299 if (!context_handle) return SEC_E_INVALID_HANDLE;
1301 ctx = schan_free_handle(context_handle->dwLower, SCHAN_HANDLE_CTX);
1302 if (!ctx) return SEC_E_INVALID_HANDLE;
1304 if (ctx->cert_store)
1305 CertCloseStore(ctx->cert_store, 0);
1306 schan_imp_dispose_session(ctx->session);
1307 HeapFree(GetProcessHeap(), 0, ctx);
1309 return SEC_E_OK;
1312 static const SecurityFunctionTableA schanTableA = {
1314 NULL, /* EnumerateSecurityPackagesA */
1315 schan_QueryCredentialsAttributesA,
1316 schan_AcquireCredentialsHandleA,
1317 schan_FreeCredentialsHandle,
1318 NULL, /* Reserved2 */
1319 schan_InitializeSecurityContextA,
1320 NULL, /* AcceptSecurityContext */
1321 NULL, /* CompleteAuthToken */
1322 schan_DeleteSecurityContext,
1323 NULL, /* ApplyControlToken */
1324 schan_QueryContextAttributesA,
1325 NULL, /* ImpersonateSecurityContext */
1326 NULL, /* RevertSecurityContext */
1327 NULL, /* MakeSignature */
1328 NULL, /* VerifySignature */
1329 FreeContextBuffer,
1330 NULL, /* QuerySecurityPackageInfoA */
1331 NULL, /* Reserved3 */
1332 NULL, /* Reserved4 */
1333 NULL, /* ExportSecurityContext */
1334 NULL, /* ImportSecurityContextA */
1335 NULL, /* AddCredentialsA */
1336 NULL, /* Reserved8 */
1337 NULL, /* QuerySecurityContextToken */
1338 schan_EncryptMessage,
1339 schan_DecryptMessage,
1340 NULL, /* SetContextAttributesA */
1343 static const SecurityFunctionTableW schanTableW = {
1345 NULL, /* EnumerateSecurityPackagesW */
1346 schan_QueryCredentialsAttributesW,
1347 schan_AcquireCredentialsHandleW,
1348 schan_FreeCredentialsHandle,
1349 NULL, /* Reserved2 */
1350 schan_InitializeSecurityContextW,
1351 NULL, /* AcceptSecurityContext */
1352 NULL, /* CompleteAuthToken */
1353 schan_DeleteSecurityContext,
1354 NULL, /* ApplyControlToken */
1355 schan_QueryContextAttributesW,
1356 NULL, /* ImpersonateSecurityContext */
1357 NULL, /* RevertSecurityContext */
1358 NULL, /* MakeSignature */
1359 NULL, /* VerifySignature */
1360 FreeContextBuffer,
1361 NULL, /* QuerySecurityPackageInfoW */
1362 NULL, /* Reserved3 */
1363 NULL, /* Reserved4 */
1364 NULL, /* ExportSecurityContext */
1365 NULL, /* ImportSecurityContextW */
1366 NULL, /* AddCredentialsW */
1367 NULL, /* Reserved8 */
1368 NULL, /* QuerySecurityContextToken */
1369 schan_EncryptMessage,
1370 schan_DecryptMessage,
1371 NULL, /* SetContextAttributesW */
1374 static const WCHAR schannelComment[] = { 'S','c','h','a','n','n','e','l',' ',
1375 'S','e','c','u','r','i','t','y',' ','P','a','c','k','a','g','e',0 };
1376 static const WCHAR schannelDllName[] = { 's','c','h','a','n','n','e','l','.','d','l','l',0 };
1378 void SECUR32_initSchannelSP(void)
1380 /* This is what Windows reports. This shouldn't break any applications
1381 * even though the functions are missing, because the wrapper will
1382 * return SEC_E_UNSUPPORTED_FUNCTION if our function is NULL.
1384 static const LONG caps =
1385 SECPKG_FLAG_INTEGRITY |
1386 SECPKG_FLAG_PRIVACY |
1387 SECPKG_FLAG_CONNECTION |
1388 SECPKG_FLAG_MULTI_REQUIRED |
1389 SECPKG_FLAG_EXTENDED_ERROR |
1390 SECPKG_FLAG_IMPERSONATION |
1391 SECPKG_FLAG_ACCEPT_WIN32_NAME |
1392 SECPKG_FLAG_STREAM;
1393 static const short version = 1;
1394 static const LONG maxToken = 16384;
1395 SEC_WCHAR *uniSPName = (SEC_WCHAR *)UNISP_NAME_W,
1396 *schannel = (SEC_WCHAR *)SCHANNEL_NAME_W;
1397 const SecPkgInfoW info[] = {
1398 { caps, version, UNISP_RPC_ID, maxToken, uniSPName, uniSPName },
1399 { caps, version, UNISP_RPC_ID, maxToken, schannel,
1400 (SEC_WCHAR *)schannelComment },
1402 SecureProvider *provider;
1404 if (!schan_imp_init())
1405 return;
1407 schan_handle_table = HeapAlloc(GetProcessHeap(), 0, 64 * sizeof(*schan_handle_table));
1408 if (!schan_handle_table)
1410 ERR("Failed to allocate schannel handle table.\n");
1411 goto fail;
1413 schan_handle_table_size = 64;
1415 provider = SECUR32_addProvider(&schanTableA, &schanTableW, schannelDllName);
1416 if (!provider)
1418 ERR("Failed to add schannel provider.\n");
1419 goto fail;
1422 SECUR32_addPackages(provider, sizeof(info) / sizeof(info[0]), NULL, info);
1424 return;
1426 fail:
1427 HeapFree(GetProcessHeap(), 0, schan_handle_table);
1428 schan_handle_table = NULL;
1429 schan_imp_deinit();
1430 return;
1433 void SECUR32_deinitSchannelSP(void)
1435 SIZE_T i = schan_handle_count;
1437 if (!schan_handle_table) return;
1439 /* deinitialized sessions first because a pointer to the credentials
1440 * may be stored for the session. */
1441 while (i--)
1443 if (schan_handle_table[i].type == SCHAN_HANDLE_CTX)
1445 struct schan_context *ctx = schan_free_handle(i, SCHAN_HANDLE_CTX);
1446 schan_imp_dispose_session(ctx->session);
1447 HeapFree(GetProcessHeap(), 0, ctx);
1450 i = schan_handle_count;
1451 while (i--)
1453 if (schan_handle_table[i].type != SCHAN_HANDLE_FREE)
1455 struct schan_credentials *cred;
1456 cred = schan_free_handle(i, SCHAN_HANDLE_CRED);
1457 schan_imp_free_certificate_credentials(cred);
1458 HeapFree(GetProcessHeap(), 0, cred);
1461 HeapFree(GetProcessHeap(), 0, schan_handle_table);
1462 schan_imp_deinit();
1465 #else /* SONAME_LIBGNUTLS || HAVE_SECURITY_SECURITY_H */
1467 void SECUR32_initSchannelSP(void)
1469 ERR("TLS library not found, SSL connections will fail\n");
1472 void SECUR32_deinitSchannelSP(void) {}
1474 #endif /* SONAME_LIBGNUTLS || HAVE_SECURITY_SECURITY_H */