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.
21 #include "wine/port.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
53 enum schan_handle_type type
;
58 schan_imp_session session
;
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
;
94 if (!(schan_handle_count
< schan_handle_table_size
))
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
));
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
;
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
;
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
);
129 object
= handle
->object
;
130 handle
->object
= schan_free_handles
;
131 handle
->type
= SCHAN_HANDLE_FREE
;
132 schan_free_handles
= handle
;
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
);
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];
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 {
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 */
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
;
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
;
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
;
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
;
253 cred
= schan_get_object(phCredential
->dwLower
, SCHAN_HANDLE_CRED
);
255 return SEC_E_INVALID_HANDLE
;
259 case SECPKG_ATTR_SUPPORTED_ALGS
:
262 /* FIXME: get from CryptoAPI */
263 FIXME("SECPKG_ATTR_SUPPORTED_ALGS: stub\n");
264 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
267 ret
= SEC_E_INTERNAL_ERROR
;
269 case SECPKG_ATTR_CIPHER_STRENGTHS
:
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;
281 ret
= SEC_E_INTERNAL_ERROR
;
283 case SECPKG_ATTR_SUPPORTED_PROTOCOLS
:
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
;
290 ret
= SEC_E_INTERNAL_ERROR
;
294 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
299 static SECURITY_STATUS SEC_ENTRY
schan_QueryCredentialsAttributesA(
300 PCredHandle phCredential
, ULONG ulAttribute
, PVOID pBuffer
)
304 TRACE("(%p, %d, %p)\n", phCredential
, ulAttribute
, pBuffer
);
308 case SECPKG_CRED_ATTR_NAMES
:
309 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
310 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
313 ret
= schan_QueryCredentialsAttributes(phCredential
, ulAttribute
,
319 static SECURITY_STATUS SEC_ENTRY
schan_QueryCredentialsAttributesW(
320 PCredHandle phCredential
, ULONG ulAttribute
, PVOID pBuffer
)
324 TRACE("(%p, %d, %p)\n", phCredential
, ulAttribute
, pBuffer
);
328 case SECPKG_CRED_ATTR_NAMES
:
329 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
330 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
333 ret
= schan_QueryCredentialsAttributes(phCredential
, ulAttribute
,
339 static SECURITY_STATUS
schan_CheckCreds(const SCHANNEL_CRED
*schanCred
)
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
)
361 case SCHANNEL_CRED_VERSION
:
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
;
377 ret
= CryptAcquireCertificatePrivateKey(schanCred
->paCred
[0],
378 0, /* FIXME: what flags to use? */ NULL
,
379 &csp
, &keySpec
, &freeCSP
);
384 CryptReleaseContext(csp
, 0);
387 st
= SEC_E_UNKNOWN_CREDENTIALS
;
392 static SECURITY_STATUS
schan_AcquireClientCredentials(const SCHANNEL_CRED
*schanCred
,
393 PCredHandle phCredential
, PTimeStamp ptsExpiry
)
395 struct schan_credentials
*creds
;
396 unsigned enabled_protocols
;
398 SECURITY_STATUS st
= SEC_E_OK
;
400 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred
, phCredential
, ptsExpiry
);
404 st
= schan_CheckCreds(schanCred
);
405 if (st
!= SEC_E_OK
&& st
!= SEC_E_NO_CREDENTIALS
)
412 if(schanCred
&& schanCred
->grbitEnabledProtocols
)
413 enabled_protocols
= schanCred
->grbitEnabledProtocols
& config_enabled_protocols
;
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
);
437 creds
->enabled_protocols
= enabled_protocols
;
438 phCredential
->dwLower
= handle
;
439 phCredential
->dwUpper
= 0;
441 /* Outbound credentials have no expiry */
444 ptsExpiry
->LowPart
= 0;
445 ptsExpiry
->HighPart
= 0;
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
)
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
);
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 */
489 static SECURITY_STATUS
schan_AcquireCredentialsHandle(ULONG fCredentialUse
,
490 const SCHANNEL_CRED
*schanCred
, PCredHandle phCredential
, PTimeStamp ptsExpiry
)
494 if (fCredentialUse
== SECPKG_CRED_OUTBOUND
)
495 ret
= schan_AcquireClientCredentials(schanCred
, phCredential
,
498 ret
= schan_AcquireServerCredentials(schanCred
, phCredential
,
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
);
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
*))
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
)
562 for (i
= start_idx
; i
< desc
->cBuffers
; ++i
)
564 buffer
= &desc
->pBuffers
[i
];
565 if (buffer
->BufferType
== buffer_type
) return i
;
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;
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;
582 new_data
= HeapReAlloc(GetProcessHeap(), 0, b
->pvBuffer
, new_size
);
584 new_data
= HeapAlloc(GetProcessHeap(), 0, new_size
);
588 TRACE("Failed to resize %p from %d to %ld\n", b
->pvBuffer
, b
->cbBuffer
, new_size
);
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
)
607 if (s
->current_buffer_idx
== -1)
610 int buffer_idx
= s
->get_next_buffer(t
, s
);
611 if (buffer_idx
== -1)
613 TRACE("No next buffer\n");
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
;
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");
637 s
->current_buffer_idx
= buffer_idx
;
639 return schan_get_buffer(t
, s
, count
);
642 if (*count
> max_count
)
644 if (s
->limit
!= ~0UL)
647 return (char *)buffer
->pvBuffer
+ s
->offset
;
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.
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
)
672 SIZE_T local_len
= *buff_len
;
674 TRACE("Pull %lu bytes\n", local_len
);
678 b
= schan_get_buffer(t
, &t
->in
, &local_len
);
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
;
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.
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
)
711 SIZE_T local_len
= *buff_len
;
713 TRACE("Push %lu bytes\n", local_len
);
717 b
= schan_get_buffer(t
, &t
->out
, &local_len
);
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
;
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
)
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
;
759 static void dump_buffer_desc(SecBufferDesc
*desc
)
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;
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
);
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
;
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
&& *pszTargetName
)
830 UINT len
= WideCharToMultiByte( CP_UNIXCP
, 0, pszTargetName
, -1, NULL
, 0, NULL
, NULL
);
831 char *target
= HeapAlloc( GetProcessHeap(), 0, len
);
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;
845 SIZE_T record_size
= 0;
851 return SEC_E_INCOMPLETE_MESSAGE
;
853 idx
= schan_find_sec_buffer_idx(pInput
, 0, SECBUFFER_TOKEN
);
855 return SEC_E_INCOMPLETE_MESSAGE
;
857 buffer
= &pInput
->pBuffers
[idx
];
858 ptr
= buffer
->pvBuffer
;
861 while (buffer
->cbBuffer
> expected_size
+ 5)
863 record_size
= 5 + ((ptr
[3] << 8) | ptr
[4]);
865 if (buffer
->cbBuffer
< expected_size
+ record_size
)
868 expected_size
+= record_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
;
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
;
911 if (ctx
->req_ctx_attr
& ISC_REQ_REPLAY_DETECT
)
912 *pfContextAttr
|= ISC_RET_REPLAY_DETECT
;
913 if (ctx
->req_ctx_attr
& ISC_REQ_SEQUENCE_DETECT
)
914 *pfContextAttr
|= ISC_RET_SEQUENCE_DETECT
;
915 if (ctx
->req_ctx_attr
& ISC_REQ_CONFIDENTIALITY
)
916 *pfContextAttr
|= ISC_RET_CONFIDENTIALITY
;
917 if (ctx
->req_ctx_attr
& ISC_REQ_ALLOCATE_MEMORY
)
918 *pfContextAttr
|= ISC_RET_ALLOCATED_MEMORY
;
919 if (ctx
->req_ctx_attr
& ISC_REQ_STREAM
)
920 *pfContextAttr
|= ISC_RET_STREAM
;
925 /***********************************************************************
926 * InitializeSecurityContextA
928 static SECURITY_STATUS SEC_ENTRY
schan_InitializeSecurityContextA(
929 PCredHandle phCredential
, PCtxtHandle phContext
, SEC_CHAR
*pszTargetName
,
930 ULONG fContextReq
, ULONG Reserved1
, ULONG TargetDataRep
,
931 PSecBufferDesc pInput
, ULONG Reserved2
, PCtxtHandle phNewContext
,
932 PSecBufferDesc pOutput
, ULONG
*pfContextAttr
, PTimeStamp ptsExpiry
)
935 SEC_WCHAR
*target_name
= NULL
;
937 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential
, phContext
,
938 debugstr_a(pszTargetName
), fContextReq
, Reserved1
, TargetDataRep
, pInput
,
939 Reserved1
, phNewContext
, pOutput
, pfContextAttr
, ptsExpiry
);
943 INT len
= MultiByteToWideChar(CP_ACP
, 0, pszTargetName
, -1, NULL
, 0);
944 target_name
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(*target_name
));
945 MultiByteToWideChar(CP_ACP
, 0, pszTargetName
, -1, target_name
, len
);
948 ret
= schan_InitializeSecurityContextW(phCredential
, phContext
, target_name
,
949 fContextReq
, Reserved1
, TargetDataRep
, pInput
, Reserved2
,
950 phNewContext
, pOutput
, pfContextAttr
, ptsExpiry
);
952 HeapFree(GetProcessHeap(), 0, target_name
);
957 static SECURITY_STATUS SEC_ENTRY
schan_QueryContextAttributesW(
958 PCtxtHandle context_handle
, ULONG attribute
, PVOID buffer
)
960 struct schan_context
*ctx
;
962 TRACE("context_handle %p, attribute %#x, buffer %p\n",
963 context_handle
, attribute
, buffer
);
965 if (!context_handle
) return SEC_E_INVALID_HANDLE
;
966 ctx
= schan_get_object(context_handle
->dwLower
, SCHAN_HANDLE_CTX
);
970 case SECPKG_ATTR_STREAM_SIZES
:
972 SecPkgContext_ConnectionInfo info
;
973 SECURITY_STATUS status
= schan_imp_get_connection_info(ctx
->session
, &info
);
974 if (status
== SEC_E_OK
)
976 SecPkgContext_StreamSizes
*stream_sizes
= buffer
;
977 SIZE_T mac_size
= info
.dwHashStrength
;
978 unsigned int block_size
= schan_imp_get_session_cipher_block_size(ctx
->session
);
979 unsigned int message_size
= schan_imp_get_max_message_size(ctx
->session
);
981 TRACE("Using %lu mac bytes, message size %u, block size %u\n",
982 mac_size
, message_size
, block_size
);
984 /* These are defined by the TLS RFC */
985 stream_sizes
->cbHeader
= 5;
986 stream_sizes
->cbTrailer
= mac_size
+ 256; /* Max 255 bytes padding + 1 for padding size */
987 stream_sizes
->cbMaximumMessage
= message_size
;
988 stream_sizes
->cbBuffers
= 4;
989 stream_sizes
->cbBlockSize
= block_size
;
994 case SECPKG_ATTR_REMOTE_CERT_CONTEXT
:
996 PCCERT_CONTEXT
*cert
= buffer
;
999 HCERTSTORE cert_store
;
1000 SECURITY_STATUS status
;
1002 cert_store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0, CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1004 return GetLastError();
1006 status
= schan_imp_get_session_peer_certificate(ctx
->session
, cert_store
, &ctx
->cert
);
1007 CertCloseStore(cert_store
, 0);
1008 if(status
!= SEC_E_OK
)
1012 *cert
= CertDuplicateCertificateContext(ctx
->cert
);
1015 case SECPKG_ATTR_CONNECTION_INFO
:
1017 SecPkgContext_ConnectionInfo
*info
= buffer
;
1018 return schan_imp_get_connection_info(ctx
->session
, info
);
1022 FIXME("Unhandled attribute %#x\n", attribute
);
1023 return SEC_E_UNSUPPORTED_FUNCTION
;
1027 static SECURITY_STATUS SEC_ENTRY
schan_QueryContextAttributesA(
1028 PCtxtHandle context_handle
, ULONG attribute
, PVOID buffer
)
1030 TRACE("context_handle %p, attribute %#x, buffer %p\n",
1031 context_handle
, attribute
, buffer
);
1035 case SECPKG_ATTR_STREAM_SIZES
:
1036 return schan_QueryContextAttributesW(context_handle
, attribute
, buffer
);
1037 case SECPKG_ATTR_REMOTE_CERT_CONTEXT
:
1038 return schan_QueryContextAttributesW(context_handle
, attribute
, buffer
);
1039 case SECPKG_ATTR_CONNECTION_INFO
:
1040 return schan_QueryContextAttributesW(context_handle
, attribute
, buffer
);
1043 FIXME("Unhandled attribute %#x\n", attribute
);
1044 return SEC_E_UNSUPPORTED_FUNCTION
;
1048 static int schan_encrypt_message_get_next_buffer(const struct schan_transport
*t
, struct schan_buffers
*s
)
1052 if (s
->current_buffer_idx
== -1)
1053 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_STREAM_HEADER
);
1055 b
= &s
->desc
->pBuffers
[s
->current_buffer_idx
];
1057 if (b
->BufferType
== SECBUFFER_STREAM_HEADER
)
1058 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_DATA
);
1060 if (b
->BufferType
== SECBUFFER_DATA
)
1061 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_STREAM_TRAILER
);
1066 static int schan_encrypt_message_get_next_buffer_token(const struct schan_transport
*t
, struct schan_buffers
*s
)
1070 if (s
->current_buffer_idx
== -1)
1071 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_TOKEN
);
1073 b
= &s
->desc
->pBuffers
[s
->current_buffer_idx
];
1075 if (b
->BufferType
== SECBUFFER_TOKEN
)
1077 int idx
= schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_TOKEN
);
1078 if (idx
!= s
->current_buffer_idx
) return -1;
1079 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_DATA
);
1082 if (b
->BufferType
== SECBUFFER_DATA
)
1084 int idx
= schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_TOKEN
);
1086 idx
= schan_find_sec_buffer_idx(s
->desc
, idx
+ 1, SECBUFFER_TOKEN
);
1093 static SECURITY_STATUS SEC_ENTRY
schan_EncryptMessage(PCtxtHandle context_handle
,
1094 ULONG quality
, PSecBufferDesc message
, ULONG message_seq_no
)
1096 struct schan_transport transport
;
1097 struct schan_context
*ctx
;
1098 struct schan_buffers
*b
;
1099 SECURITY_STATUS status
;
1106 TRACE("context_handle %p, quality %d, message %p, message_seq_no %d\n",
1107 context_handle
, quality
, message
, message_seq_no
);
1109 if (!context_handle
) return SEC_E_INVALID_HANDLE
;
1110 ctx
= schan_get_object(context_handle
->dwLower
, SCHAN_HANDLE_CTX
);
1112 dump_buffer_desc(message
);
1114 idx
= schan_find_sec_buffer_idx(message
, 0, SECBUFFER_DATA
);
1117 WARN("No data buffer passed\n");
1118 return SEC_E_INTERNAL_ERROR
;
1120 buffer
= &message
->pBuffers
[idx
];
1122 data_size
= buffer
->cbBuffer
;
1123 data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
1124 memcpy(data
, buffer
->pvBuffer
, data_size
);
1126 transport
.ctx
= ctx
;
1127 init_schan_buffers(&transport
.in
, NULL
, NULL
);
1128 if (schan_find_sec_buffer_idx(message
, 0, SECBUFFER_STREAM_HEADER
) != -1)
1129 init_schan_buffers(&transport
.out
, message
, schan_encrypt_message_get_next_buffer
);
1131 init_schan_buffers(&transport
.out
, message
, schan_encrypt_message_get_next_buffer_token
);
1132 schan_imp_set_session_transport(ctx
->session
, &transport
);
1135 status
= schan_imp_send(ctx
->session
, data
, &length
);
1137 TRACE("Sent %ld bytes.\n", length
);
1139 if (length
!= data_size
)
1140 status
= SEC_E_INTERNAL_ERROR
;
1143 b
->desc
->pBuffers
[b
->current_buffer_idx
].cbBuffer
= b
->offset
;
1144 HeapFree(GetProcessHeap(), 0, data
);
1146 TRACE("Returning %#x.\n", status
);
1151 static int schan_decrypt_message_get_next_buffer(const struct schan_transport
*t
, struct schan_buffers
*s
)
1153 if (s
->current_buffer_idx
== -1)
1154 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_DATA
);
1159 static int schan_validate_decrypt_buffer_desc(PSecBufferDesc message
)
1162 unsigned int empty_count
= 0;
1165 if (message
->cBuffers
< 4)
1167 WARN("Less than four buffers passed\n");
1171 for (i
= 0; i
< message
->cBuffers
; ++i
)
1173 SecBuffer
*b
= &message
->pBuffers
[i
];
1174 if (b
->BufferType
== SECBUFFER_DATA
)
1178 WARN("More than one data buffer passed\n");
1183 else if (b
->BufferType
== SECBUFFER_EMPTY
)
1189 WARN("No data buffer passed\n");
1193 if (empty_count
< 3)
1195 WARN("Less than three empty buffers passed\n");
1202 static void schan_decrypt_fill_buffer(PSecBufferDesc message
, ULONG buffer_type
, void *data
, ULONG size
)
1207 idx
= schan_find_sec_buffer_idx(message
, 0, SECBUFFER_EMPTY
);
1208 buffer
= &message
->pBuffers
[idx
];
1210 buffer
->BufferType
= buffer_type
;
1211 buffer
->pvBuffer
= data
;
1212 buffer
->cbBuffer
= size
;
1215 static SECURITY_STATUS SEC_ENTRY
schan_DecryptMessage(PCtxtHandle context_handle
,
1216 PSecBufferDesc message
, ULONG message_seq_no
, PULONG quality
)
1218 struct schan_transport transport
;
1219 struct schan_context
*ctx
;
1223 unsigned expected_size
;
1224 SSIZE_T received
= 0;
1226 unsigned char *buf_ptr
;
1228 TRACE("context_handle %p, message %p, message_seq_no %d, quality %p\n",
1229 context_handle
, message
, message_seq_no
, quality
);
1231 if (!context_handle
) return SEC_E_INVALID_HANDLE
;
1232 ctx
= schan_get_object(context_handle
->dwLower
, SCHAN_HANDLE_CTX
);
1234 dump_buffer_desc(message
);
1236 idx
= schan_validate_decrypt_buffer_desc(message
);
1238 return SEC_E_INVALID_TOKEN
;
1239 buffer
= &message
->pBuffers
[idx
];
1240 buf_ptr
= buffer
->pvBuffer
;
1242 expected_size
= 5 + ((buf_ptr
[3] << 8) | buf_ptr
[4]);
1243 if(buffer
->cbBuffer
< expected_size
)
1245 TRACE("Expected %u bytes, but buffer only contains %u bytes\n", expected_size
, buffer
->cbBuffer
);
1246 buffer
->BufferType
= SECBUFFER_MISSING
;
1247 buffer
->cbBuffer
= expected_size
- buffer
->cbBuffer
;
1249 /* This is a bit weird, but windows does it too */
1250 idx
= schan_find_sec_buffer_idx(message
, 0, SECBUFFER_EMPTY
);
1251 buffer
= &message
->pBuffers
[idx
];
1252 buffer
->BufferType
= SECBUFFER_MISSING
;
1253 buffer
->cbBuffer
= expected_size
- buffer
->cbBuffer
;
1255 TRACE("Returning SEC_E_INCOMPLETE_MESSAGE\n");
1256 return SEC_E_INCOMPLETE_MESSAGE
;
1259 data_size
= expected_size
- 5;
1260 data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
1262 transport
.ctx
= ctx
;
1263 init_schan_buffers(&transport
.in
, message
, schan_decrypt_message_get_next_buffer
);
1264 transport
.in
.limit
= expected_size
;
1265 init_schan_buffers(&transport
.out
, NULL
, NULL
);
1266 schan_imp_set_session_transport(ctx
->session
, &transport
);
1268 while (received
< data_size
)
1270 SIZE_T length
= data_size
- received
;
1271 SECURITY_STATUS status
= schan_imp_recv(ctx
->session
, data
+ received
, &length
);
1273 if (status
== SEC_I_CONTINUE_NEEDED
)
1276 if (status
!= SEC_E_OK
)
1278 HeapFree(GetProcessHeap(), 0, data
);
1279 ERR("Returning %x\n", status
);
1289 TRACE("Received %ld bytes\n", received
);
1291 memcpy(buf_ptr
+ 5, data
, received
);
1292 HeapFree(GetProcessHeap(), 0, data
);
1294 schan_decrypt_fill_buffer(message
, SECBUFFER_DATA
,
1295 buf_ptr
+ 5, received
);
1297 schan_decrypt_fill_buffer(message
, SECBUFFER_STREAM_TRAILER
,
1298 buf_ptr
+ 5 + received
, buffer
->cbBuffer
- 5 - received
);
1300 if(buffer
->cbBuffer
> expected_size
)
1301 schan_decrypt_fill_buffer(message
, SECBUFFER_EXTRA
,
1302 buf_ptr
+ expected_size
, buffer
->cbBuffer
- expected_size
);
1304 buffer
->BufferType
= SECBUFFER_STREAM_HEADER
;
1305 buffer
->cbBuffer
= 5;
1310 static SECURITY_STATUS SEC_ENTRY
schan_DeleteSecurityContext(PCtxtHandle context_handle
)
1312 struct schan_context
*ctx
;
1314 TRACE("context_handle %p\n", context_handle
);
1316 if (!context_handle
) return SEC_E_INVALID_HANDLE
;
1318 ctx
= schan_free_handle(context_handle
->dwLower
, SCHAN_HANDLE_CTX
);
1319 if (!ctx
) return SEC_E_INVALID_HANDLE
;
1322 CertFreeCertificateContext(ctx
->cert
);
1323 schan_imp_dispose_session(ctx
->session
);
1324 HeapFree(GetProcessHeap(), 0, ctx
);
1329 static const SecurityFunctionTableA schanTableA
= {
1331 NULL
, /* EnumerateSecurityPackagesA */
1332 schan_QueryCredentialsAttributesA
,
1333 schan_AcquireCredentialsHandleA
,
1334 schan_FreeCredentialsHandle
,
1335 NULL
, /* Reserved2 */
1336 schan_InitializeSecurityContextA
,
1337 NULL
, /* AcceptSecurityContext */
1338 NULL
, /* CompleteAuthToken */
1339 schan_DeleteSecurityContext
,
1340 NULL
, /* ApplyControlToken */
1341 schan_QueryContextAttributesA
,
1342 NULL
, /* ImpersonateSecurityContext */
1343 NULL
, /* RevertSecurityContext */
1344 NULL
, /* MakeSignature */
1345 NULL
, /* VerifySignature */
1347 NULL
, /* QuerySecurityPackageInfoA */
1348 NULL
, /* Reserved3 */
1349 NULL
, /* Reserved4 */
1350 NULL
, /* ExportSecurityContext */
1351 NULL
, /* ImportSecurityContextA */
1352 NULL
, /* AddCredentialsA */
1353 NULL
, /* Reserved8 */
1354 NULL
, /* QuerySecurityContextToken */
1355 schan_EncryptMessage
,
1356 schan_DecryptMessage
,
1357 NULL
, /* SetContextAttributesA */
1360 static const SecurityFunctionTableW schanTableW
= {
1362 NULL
, /* EnumerateSecurityPackagesW */
1363 schan_QueryCredentialsAttributesW
,
1364 schan_AcquireCredentialsHandleW
,
1365 schan_FreeCredentialsHandle
,
1366 NULL
, /* Reserved2 */
1367 schan_InitializeSecurityContextW
,
1368 NULL
, /* AcceptSecurityContext */
1369 NULL
, /* CompleteAuthToken */
1370 schan_DeleteSecurityContext
,
1371 NULL
, /* ApplyControlToken */
1372 schan_QueryContextAttributesW
,
1373 NULL
, /* ImpersonateSecurityContext */
1374 NULL
, /* RevertSecurityContext */
1375 NULL
, /* MakeSignature */
1376 NULL
, /* VerifySignature */
1378 NULL
, /* QuerySecurityPackageInfoW */
1379 NULL
, /* Reserved3 */
1380 NULL
, /* Reserved4 */
1381 NULL
, /* ExportSecurityContext */
1382 NULL
, /* ImportSecurityContextW */
1383 NULL
, /* AddCredentialsW */
1384 NULL
, /* Reserved8 */
1385 NULL
, /* QuerySecurityContextToken */
1386 schan_EncryptMessage
,
1387 schan_DecryptMessage
,
1388 NULL
, /* SetContextAttributesW */
1391 static const WCHAR schannelComment
[] = { 'S','c','h','a','n','n','e','l',' ',
1392 'S','e','c','u','r','i','t','y',' ','P','a','c','k','a','g','e',0 };
1393 static const WCHAR schannelDllName
[] = { 's','c','h','a','n','n','e','l','.','d','l','l',0 };
1395 void SECUR32_initSchannelSP(void)
1397 /* This is what Windows reports. This shouldn't break any applications
1398 * even though the functions are missing, because the wrapper will
1399 * return SEC_E_UNSUPPORTED_FUNCTION if our function is NULL.
1401 static const LONG caps
=
1402 SECPKG_FLAG_INTEGRITY
|
1403 SECPKG_FLAG_PRIVACY
|
1404 SECPKG_FLAG_CONNECTION
|
1405 SECPKG_FLAG_MULTI_REQUIRED
|
1406 SECPKG_FLAG_EXTENDED_ERROR
|
1407 SECPKG_FLAG_IMPERSONATION
|
1408 SECPKG_FLAG_ACCEPT_WIN32_NAME
|
1410 static const short version
= 1;
1411 static const LONG maxToken
= 16384;
1412 SEC_WCHAR
*uniSPName
= (SEC_WCHAR
*)UNISP_NAME_W
,
1413 *schannel
= (SEC_WCHAR
*)SCHANNEL_NAME_W
;
1414 const SecPkgInfoW info
[] = {
1415 { caps
, version
, UNISP_RPC_ID
, maxToken
, uniSPName
, uniSPName
},
1416 { caps
, version
, UNISP_RPC_ID
, maxToken
, schannel
,
1417 (SEC_WCHAR
*)schannelComment
},
1419 SecureProvider
*provider
;
1421 if (!schan_imp_init())
1424 schan_handle_table
= HeapAlloc(GetProcessHeap(), 0, 64 * sizeof(*schan_handle_table
));
1425 if (!schan_handle_table
)
1427 ERR("Failed to allocate schannel handle table.\n");
1430 schan_handle_table_size
= 64;
1432 provider
= SECUR32_addProvider(&schanTableA
, &schanTableW
, schannelDllName
);
1435 ERR("Failed to add schannel provider.\n");
1439 SECUR32_addPackages(provider
, sizeof(info
) / sizeof(info
[0]), NULL
, info
);
1444 HeapFree(GetProcessHeap(), 0, schan_handle_table
);
1445 schan_handle_table
= NULL
;
1450 void SECUR32_deinitSchannelSP(void)
1452 SIZE_T i
= schan_handle_count
;
1454 if (!schan_handle_table
) return;
1456 /* deinitialized sessions first because a pointer to the credentials
1457 * may be stored for the session. */
1460 if (schan_handle_table
[i
].type
== SCHAN_HANDLE_CTX
)
1462 struct schan_context
*ctx
= schan_free_handle(i
, SCHAN_HANDLE_CTX
);
1463 schan_imp_dispose_session(ctx
->session
);
1464 HeapFree(GetProcessHeap(), 0, ctx
);
1467 i
= schan_handle_count
;
1470 if (schan_handle_table
[i
].type
!= SCHAN_HANDLE_FREE
)
1472 struct schan_credentials
*cred
;
1473 cred
= schan_free_handle(i
, SCHAN_HANDLE_CRED
);
1474 schan_imp_free_certificate_credentials(cred
);
1475 HeapFree(GetProcessHeap(), 0, cred
);
1478 HeapFree(GetProcessHeap(), 0, schan_handle_table
);
1482 #else /* SONAME_LIBGNUTLS || HAVE_SECURITY_SECURITY_H */
1484 void SECUR32_initSchannelSP(void)
1486 ERR("TLS library not found, SSL connections will fail\n");
1489 void SECUR32_deinitSchannelSP(void) {}
1491 #endif /* SONAME_LIBGNUTLS || HAVE_SECURITY_SECURITY_H */