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.
23 #include <wine/config.h>
25 WINE_DEFAULT_DEBUG_CHANNEL(schannel
);
27 #if defined(SONAME_LIBGNUTLS) || defined (HAVE_SECURITY_SECURITY_H) || defined (SONAME_LIBMBEDTLS)
29 #define SCHAN_INVALID_HANDLE ~0UL
31 enum schan_handle_type
41 enum schan_handle_type type
;
46 schan_imp_session session
;
48 const CERT_CONTEXT
*cert
;
51 static struct schan_handle
*schan_handle_table
;
52 static struct schan_handle
*schan_free_handles
;
53 static SIZE_T schan_handle_table_size
;
54 static SIZE_T schan_handle_count
;
56 /* Protocols enabled, only those may be used for the connection. */
57 static DWORD config_enabled_protocols
;
59 /* Protocols disabled by default. They are enabled for using, but disabled when caller asks for default settings. */
60 static DWORD config_default_disabled_protocols
;
62 static ULONG_PTR
schan_alloc_handle(void *object
, enum schan_handle_type type
)
64 struct schan_handle
*handle
;
66 if (schan_free_handles
)
68 DWORD index
= schan_free_handles
- schan_handle_table
;
69 /* Use a free handle */
70 handle
= schan_free_handles
;
71 if (handle
->type
!= SCHAN_HANDLE_FREE
)
73 ERR("Handle %d(%p) is in the free list, but has type %#x.\n", index
, handle
, handle
->type
);
74 return SCHAN_INVALID_HANDLE
;
76 schan_free_handles
= handle
->object
;
77 handle
->object
= object
;
82 if (!(schan_handle_count
< schan_handle_table_size
))
85 SIZE_T new_size
= schan_handle_table_size
+ (schan_handle_table_size
>> 1);
86 struct schan_handle
*new_table
= HeapReAlloc(GetProcessHeap(), 0, schan_handle_table
, new_size
* sizeof(*schan_handle_table
));
89 ERR("Failed to grow the handle table\n");
90 return SCHAN_INVALID_HANDLE
;
92 schan_handle_table
= new_table
;
93 schan_handle_table_size
= new_size
;
96 handle
= &schan_handle_table
[schan_handle_count
++];
97 handle
->object
= object
;
100 return handle
- schan_handle_table
;
103 static void *schan_free_handle(ULONG_PTR handle_idx
, enum schan_handle_type type
)
105 struct schan_handle
*handle
;
108 if (handle_idx
== SCHAN_INVALID_HANDLE
) return NULL
;
109 if (handle_idx
>= schan_handle_count
) return NULL
;
110 handle
= &schan_handle_table
[handle_idx
];
111 if (handle
->type
!= type
)
113 ERR("Handle %ld(%p) is not of type %#x\n", handle_idx
, handle
, type
);
117 object
= handle
->object
;
118 handle
->object
= schan_free_handles
;
119 handle
->type
= SCHAN_HANDLE_FREE
;
120 schan_free_handles
= handle
;
125 static void *schan_get_object(ULONG_PTR handle_idx
, enum schan_handle_type type
)
127 struct schan_handle
*handle
;
129 if (handle_idx
== SCHAN_INVALID_HANDLE
) return NULL
;
130 if (handle_idx
>= schan_handle_count
) return NULL
;
131 handle
= &schan_handle_table
[handle_idx
];
132 if (handle
->type
!= type
)
134 ERR("Handle %ld(%p) is not of type %#x\n", handle_idx
, handle
, type
);
138 return handle
->object
;
141 static void read_config(void)
143 DWORD enabled
= 0, default_disabled
= 0;
144 HKEY protocols_key
, key
;
145 WCHAR subkey_name
[64];
149 static BOOL config_read
= FALSE
;
151 static const WCHAR protocol_config_key_name
[] = {
152 'S','Y','S','T','E','M','\\',
153 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
154 'C','o','n','t','r','o','l','\\',
155 'S','e','c','u','r','i','t','y','P','r','o','v','i','d','e','r','s','\\',
156 'S','C','H','A','N','N','E','L','\\',
157 'P','r','o','t','o','c','o','l','s',0 };
159 static const WCHAR clientW
[] = {'\\','C','l','i','e','n','t',0};
160 static const WCHAR enabledW
[] = {'e','n','a','b','l','e','d',0};
161 static const WCHAR disabledbydefaultW
[] = {'D','i','s','a','b','l','e','d','B','y','D','e','f','a','u','l','t',0};
163 static const struct {
165 DWORD prot_client_flag
;
166 BOOL enabled
; /* If no config is present, enable the protocol */
167 BOOL disabled_by_default
; /* Disable if caller asks for default protocol set */
168 } protocol_config_keys
[] = {
169 {{'S','S','L',' ','2','.','0',0}, SP_PROT_SSL2_CLIENT
, FALSE
, TRUE
}, /* NOTE: TRUE, TRUE on Windows */
170 {{'S','S','L',' ','3','.','0',0}, SP_PROT_SSL3_CLIENT
, TRUE
, FALSE
},
171 {{'T','L','S',' ','1','.','0',0}, SP_PROT_TLS1_0_CLIENT
, TRUE
, FALSE
},
172 {{'T','L','S',' ','1','.','1',0}, SP_PROT_TLS1_1_CLIENT
, TRUE
, FALSE
/* NOTE: not enabled by default on Windows */ },
173 {{'T','L','S',' ','1','.','2',0}, SP_PROT_TLS1_2_CLIENT
, TRUE
, FALSE
/* NOTE: not enabled by default on Windows */ }
176 /* No need for thread safety */
180 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, protocol_config_key_name
, 0, KEY_READ
, &protocols_key
);
181 if(res
== ERROR_SUCCESS
) {
182 DWORD type
, size
, value
;
184 for(i
=0; i
< sizeof(protocol_config_keys
)/sizeof(*protocol_config_keys
); i
++) {
185 strcpyW(subkey_name
, protocol_config_keys
[i
].key_name
);
186 strcatW(subkey_name
, clientW
);
187 res
= RegOpenKeyExW(protocols_key
, subkey_name
, 0, KEY_READ
, &key
);
188 if(res
!= ERROR_SUCCESS
) {
189 if(protocol_config_keys
[i
].enabled
)
190 enabled
|= protocol_config_keys
[i
].prot_client_flag
;
191 if(protocol_config_keys
[i
].disabled_by_default
)
192 default_disabled
|= protocol_config_keys
[i
].prot_client_flag
;
196 size
= sizeof(value
);
197 res
= RegQueryValueExW(key
, enabledW
, NULL
, &type
, (BYTE
*)&value
, &size
);
198 if(res
== ERROR_SUCCESS
) {
199 if(type
== REG_DWORD
&& value
)
200 enabled
|= protocol_config_keys
[i
].prot_client_flag
;
201 }else if(protocol_config_keys
[i
].enabled
) {
202 enabled
|= protocol_config_keys
[i
].prot_client_flag
;
205 size
= sizeof(value
);
206 res
= RegQueryValueExW(key
, disabledbydefaultW
, NULL
, &type
, (BYTE
*)&value
, &size
);
207 if(res
== ERROR_SUCCESS
) {
208 if(type
!= REG_DWORD
|| value
)
209 default_disabled
|= protocol_config_keys
[i
].prot_client_flag
;
210 }else if(protocol_config_keys
[i
].disabled_by_default
) {
211 default_disabled
|= protocol_config_keys
[i
].prot_client_flag
;
217 /* No config, enable all known protocols. */
218 for(i
=0; i
< sizeof(protocol_config_keys
)/sizeof(*protocol_config_keys
); i
++) {
219 if(protocol_config_keys
[i
].enabled
)
220 enabled
|= protocol_config_keys
[i
].prot_client_flag
;
221 if(protocol_config_keys
[i
].disabled_by_default
)
222 default_disabled
|= protocol_config_keys
[i
].prot_client_flag
;
226 RegCloseKey(protocols_key
);
228 config_enabled_protocols
= enabled
& schan_imp_enabled_protocols();
229 config_default_disabled_protocols
= default_disabled
;
232 TRACE("enabled %x, disabled by default %x\n", config_enabled_protocols
, config_default_disabled_protocols
);
235 static SECURITY_STATUS
schan_QueryCredentialsAttributes(
236 PCredHandle phCredential
, ULONG ulAttribute
, VOID
*pBuffer
)
238 struct schan_credentials
*cred
;
241 cred
= schan_get_object(phCredential
->dwLower
, SCHAN_HANDLE_CRED
);
243 return SEC_E_INVALID_HANDLE
;
247 case SECPKG_ATTR_SUPPORTED_ALGS
:
250 /* FIXME: get from CryptoAPI */
251 FIXME("SECPKG_ATTR_SUPPORTED_ALGS: stub\n");
252 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
255 ret
= SEC_E_INTERNAL_ERROR
;
257 case SECPKG_ATTR_CIPHER_STRENGTHS
:
260 SecPkgCred_CipherStrengths
*r
= pBuffer
;
262 /* FIXME: get from CryptoAPI */
263 FIXME("SECPKG_ATTR_CIPHER_STRENGTHS: semi-stub\n");
264 r
->dwMinimumCipherStrength
= 40;
265 r
->dwMaximumCipherStrength
= 168;
269 ret
= SEC_E_INTERNAL_ERROR
;
271 case SECPKG_ATTR_SUPPORTED_PROTOCOLS
:
273 /* Regardless of MSDN documentation, tests show that this attribute takes into account
274 * what protocols are enabled for given credential. */
275 ((SecPkgCred_SupportedProtocols
*)pBuffer
)->grbitProtocol
= cred
->enabled_protocols
;
278 ret
= SEC_E_INTERNAL_ERROR
;
282 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
287 static SECURITY_STATUS SEC_ENTRY
schan_QueryCredentialsAttributesA(
288 PCredHandle phCredential
, ULONG ulAttribute
, PVOID pBuffer
)
292 TRACE("(%p, %d, %p)\n", phCredential
, ulAttribute
, pBuffer
);
296 case SECPKG_CRED_ATTR_NAMES
:
297 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
298 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
301 ret
= schan_QueryCredentialsAttributes(phCredential
, ulAttribute
,
307 SECURITY_STATUS SEC_ENTRY
schan_QueryCredentialsAttributesW(
308 PCredHandle phCredential
, ULONG ulAttribute
, PVOID pBuffer
)
312 TRACE("(%p, %d, %p)\n", phCredential
, ulAttribute
, pBuffer
);
316 case SECPKG_CRED_ATTR_NAMES
:
317 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
318 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
321 ret
= schan_QueryCredentialsAttributes(phCredential
, ulAttribute
,
327 static SECURITY_STATUS
schan_CheckCreds(const SCHANNEL_CRED
*schanCred
)
332 TRACE("dwVersion = %d\n", schanCred
->dwVersion
);
333 TRACE("cCreds = %d\n", schanCred
->cCreds
);
334 TRACE("hRootStore = %p\n", schanCred
->hRootStore
);
335 TRACE("cMappers = %d\n", schanCred
->cMappers
);
336 TRACE("cSupportedAlgs = %d:\n", schanCred
->cSupportedAlgs
);
337 for (i
= 0; i
< schanCred
->cSupportedAlgs
; i
++)
338 TRACE("%08x\n", schanCred
->palgSupportedAlgs
[i
]);
339 TRACE("grbitEnabledProtocols = %08x\n", schanCred
->grbitEnabledProtocols
);
340 TRACE("dwMinimumCipherStrength = %d\n", schanCred
->dwMinimumCipherStrength
);
341 TRACE("dwMaximumCipherStrength = %d\n", schanCred
->dwMaximumCipherStrength
);
342 TRACE("dwSessionLifespan = %d\n", schanCred
->dwSessionLifespan
);
343 TRACE("dwFlags = %08x\n", schanCred
->dwFlags
);
344 TRACE("dwCredFormat = %d\n", schanCred
->dwCredFormat
);
346 switch (schanCred
->dwVersion
)
349 case SCHANNEL_CRED_VERSION
:
352 return SEC_E_INTERNAL_ERROR
;
355 if (schanCred
->cCreds
== 0)
356 st
= SEC_E_NO_CREDENTIALS
;
357 else if (schanCred
->cCreds
> 1)
358 st
= SEC_E_UNKNOWN_CREDENTIALS
;
365 ret
= CryptAcquireCertificatePrivateKey(schanCred
->paCred
[0],
366 0, /* FIXME: what flags to use? */ NULL
,
367 &csp
, &keySpec
, &freeCSP
);
372 CryptReleaseContext(csp
, 0);
375 st
= SEC_E_UNKNOWN_CREDENTIALS
;
380 static SECURITY_STATUS
schan_AcquireClientCredentials(const SCHANNEL_CRED
*schanCred
,
381 PCredHandle phCredential
, PTimeStamp ptsExpiry
)
383 struct schan_credentials
*creds
;
384 unsigned enabled_protocols
;
386 SECURITY_STATUS st
= SEC_E_OK
;
388 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred
, phCredential
, ptsExpiry
);
392 st
= schan_CheckCreds(schanCred
);
393 if (st
!= SEC_E_OK
&& st
!= SEC_E_NO_CREDENTIALS
)
400 if(schanCred
&& schanCred
->grbitEnabledProtocols
)
401 enabled_protocols
= schanCred
->grbitEnabledProtocols
& config_enabled_protocols
;
403 enabled_protocols
= config_enabled_protocols
& ~config_default_disabled_protocols
;
404 if(!enabled_protocols
) {
405 ERR("Could not find matching protocol\n");
406 return SEC_E_NO_AUTHENTICATING_AUTHORITY
;
409 /* For now, the only thing I'm interested in is the direction of the
410 * connection, so just store it.
412 creds
= HeapAlloc(GetProcessHeap(), 0, sizeof(*creds
));
413 if (!creds
) return SEC_E_INSUFFICIENT_MEMORY
;
415 handle
= schan_alloc_handle(creds
, SCHAN_HANDLE_CRED
);
416 if (handle
== SCHAN_INVALID_HANDLE
) goto fail
;
418 creds
->credential_use
= SECPKG_CRED_OUTBOUND
;
419 if (!schan_imp_allocate_certificate_credentials(creds
))
421 schan_free_handle(handle
, SCHAN_HANDLE_CRED
);
425 creds
->enabled_protocols
= enabled_protocols
;
426 phCredential
->dwLower
= handle
;
427 phCredential
->dwUpper
= 0;
429 /* Outbound credentials have no expiry */
432 ptsExpiry
->LowPart
= 0;
433 ptsExpiry
->HighPart
= 0;
439 HeapFree(GetProcessHeap(), 0, creds
);
440 return SEC_E_INTERNAL_ERROR
;
443 static SECURITY_STATUS
schan_AcquireServerCredentials(const SCHANNEL_CRED
*schanCred
,
444 PCredHandle phCredential
, PTimeStamp ptsExpiry
)
448 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred
, phCredential
, ptsExpiry
);
450 if (!schanCred
) return SEC_E_NO_CREDENTIALS
;
452 st
= schan_CheckCreds(schanCred
);
456 struct schan_credentials
*creds
;
458 creds
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*creds
));
459 if (!creds
) return SEC_E_INSUFFICIENT_MEMORY
;
460 creds
->credential_use
= SECPKG_CRED_INBOUND
;
462 handle
= schan_alloc_handle(creds
, SCHAN_HANDLE_CRED
);
463 if (handle
== SCHAN_INVALID_HANDLE
)
465 HeapFree(GetProcessHeap(), 0, creds
);
466 return SEC_E_INTERNAL_ERROR
;
469 phCredential
->dwLower
= handle
;
470 phCredential
->dwUpper
= 0;
472 /* FIXME: get expiry from cert */
477 static SECURITY_STATUS
schan_AcquireCredentialsHandle(ULONG fCredentialUse
,
478 const SCHANNEL_CRED
*schanCred
, PCredHandle phCredential
, PTimeStamp ptsExpiry
)
482 if (fCredentialUse
== SECPKG_CRED_OUTBOUND
)
483 ret
= schan_AcquireClientCredentials(schanCred
, phCredential
,
486 ret
= schan_AcquireServerCredentials(schanCred
, phCredential
,
491 SECURITY_STATUS SEC_ENTRY
schan_AcquireCredentialsHandleA(
492 SEC_CHAR
*pszPrincipal
, SEC_CHAR
*pszPackage
, ULONG fCredentialUse
,
493 PLUID pLogonID
, PVOID pAuthData
, SEC_GET_KEY_FN pGetKeyFn
,
494 PVOID pGetKeyArgument
, PCredHandle phCredential
, PTimeStamp ptsExpiry
)
496 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
497 debugstr_a(pszPrincipal
), debugstr_a(pszPackage
), fCredentialUse
,
498 pLogonID
, pAuthData
, pGetKeyFn
, pGetKeyArgument
, phCredential
, ptsExpiry
);
499 return schan_AcquireCredentialsHandle(fCredentialUse
,
500 pAuthData
, phCredential
, ptsExpiry
);
503 SECURITY_STATUS SEC_ENTRY
schan_AcquireCredentialsHandleW(
504 SEC_WCHAR
*pszPrincipal
, SEC_WCHAR
*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_w(pszPrincipal
), debugstr_w(pszPackage
), fCredentialUse
,
510 pLogonID
, pAuthData
, pGetKeyFn
, pGetKeyArgument
, phCredential
, ptsExpiry
);
511 return schan_AcquireCredentialsHandle(fCredentialUse
,
512 pAuthData
, phCredential
, ptsExpiry
);
515 SECURITY_STATUS SEC_ENTRY
schan_FreeCredentialsHandle(
516 PCredHandle phCredential
)
518 struct schan_credentials
*creds
;
520 TRACE("phCredential %p\n", phCredential
);
522 if (!phCredential
) return SEC_E_INVALID_HANDLE
;
524 creds
= schan_free_handle(phCredential
->dwLower
, SCHAN_HANDLE_CRED
);
525 if (!creds
) return SEC_E_INVALID_HANDLE
;
527 if (creds
->credential_use
== SECPKG_CRED_OUTBOUND
)
528 schan_imp_free_certificate_credentials(creds
);
529 HeapFree(GetProcessHeap(), 0, creds
);
534 static void init_schan_buffers(struct schan_buffers
*s
, const PSecBufferDesc desc
,
535 int (*get_next_buffer
)(const struct schan_transport
*, struct schan_buffers
*))
540 s
->current_buffer_idx
= -1;
541 s
->allow_buffer_resize
= FALSE
;
542 s
->get_next_buffer
= get_next_buffer
;
545 static int schan_find_sec_buffer_idx(const SecBufferDesc
*desc
, unsigned int start_idx
, ULONG buffer_type
)
550 for (i
= start_idx
; i
< desc
->cBuffers
; ++i
)
552 buffer
= &desc
->pBuffers
[i
];
553 if (buffer
->BufferType
== buffer_type
) return i
;
559 static void schan_resize_current_buffer(const struct schan_buffers
*s
, SIZE_T min_size
)
561 SecBuffer
*b
= &s
->desc
->pBuffers
[s
->current_buffer_idx
];
562 SIZE_T new_size
= b
->cbBuffer
? b
->cbBuffer
* 2 : 128;
565 if (b
->cbBuffer
>= min_size
|| !s
->allow_buffer_resize
|| min_size
> UINT_MAX
/ 2) return;
567 while (new_size
< min_size
) new_size
*= 2;
570 new_data
= HeapReAlloc(GetProcessHeap(), 0, b
->pvBuffer
, new_size
);
572 new_data
= HeapAlloc(GetProcessHeap(), 0, new_size
);
576 TRACE("Failed to resize %p from %d to %ld\n", b
->pvBuffer
, b
->cbBuffer
, new_size
);
580 b
->cbBuffer
= new_size
;
581 b
->pvBuffer
= new_data
;
584 char *schan_get_buffer(const struct schan_transport
*t
, struct schan_buffers
*s
, SIZE_T
*count
)
595 if (s
->current_buffer_idx
== -1)
598 int buffer_idx
= s
->get_next_buffer(t
, s
);
599 if (buffer_idx
== -1)
601 TRACE("No next buffer\n");
604 s
->current_buffer_idx
= buffer_idx
;
607 buffer
= &s
->desc
->pBuffers
[s
->current_buffer_idx
];
608 TRACE("Using buffer %d: cbBuffer %d, BufferType %#x, pvBuffer %p\n", s
->current_buffer_idx
, buffer
->cbBuffer
, buffer
->BufferType
, buffer
->pvBuffer
);
610 schan_resize_current_buffer(s
, s
->offset
+ *count
);
611 max_count
= buffer
->cbBuffer
- s
->offset
;
612 if (s
->limit
!= ~0UL && s
->limit
< max_count
)
613 max_count
= s
->limit
;
618 s
->allow_buffer_resize
= FALSE
;
619 buffer_idx
= s
->get_next_buffer(t
, s
);
620 if (buffer_idx
== -1)
622 TRACE("No next buffer\n");
625 s
->current_buffer_idx
= buffer_idx
;
627 return schan_get_buffer(t
, s
, count
);
630 if (*count
> max_count
)
632 if (s
->limit
!= ~0UL)
635 return (char *)buffer
->pvBuffer
+ s
->offset
;
639 * Read data from the transport input buffer.
641 * t - The session transport object.
642 * buff - The buffer into which to store the read data. Must be at least
643 * *buff_len bytes in length.
644 * buff_len - On input, *buff_len is the desired length to read. On successful
645 * return, *buff_len is the number of bytes actually read.
648 * 0 on success, in which case:
649 * *buff_len == 0 indicates end of file.
650 * *buff_len > 0 indicates that some data was read. May be less than
651 * what was requested, in which case the caller should call again if/
652 * when they want more.
653 * EAGAIN when no data could be read without blocking
654 * another errno-style error value on failure
657 int schan_pull(struct schan_transport
*t
, void *buff
, size_t *buff_len
)
660 SIZE_T local_len
= *buff_len
;
662 TRACE("Pull %lu bytes\n", local_len
);
666 b
= schan_get_buffer(t
, &t
->in
, &local_len
);
670 memcpy(buff
, b
, local_len
);
671 t
->in
.offset
+= local_len
;
673 TRACE("Read %lu bytes\n", local_len
);
675 *buff_len
= local_len
;
680 * Write data to the transport output buffer.
682 * t - The session transport object.
683 * buff - The buffer of data to write. Must be at least *buff_len bytes in length.
684 * buff_len - On input, *buff_len is the desired length to write. On successful
685 * return, *buff_len is the number of bytes actually written.
689 * *buff_len will be > 0 indicating how much data was written. May be less
690 * than what was requested, in which case the caller should call again
691 if/when they want to write more.
692 * EAGAIN when no data could be written without blocking
693 * another errno-style error value on failure
696 int schan_push(struct schan_transport
*t
, const void *buff
, size_t *buff_len
)
699 SIZE_T local_len
= *buff_len
;
701 TRACE("Push %lu bytes\n", local_len
);
705 b
= schan_get_buffer(t
, &t
->out
, &local_len
);
709 memcpy(b
, buff
, local_len
);
710 t
->out
.offset
+= local_len
;
712 TRACE("Wrote %lu bytes\n", local_len
);
714 *buff_len
= local_len
;
718 schan_imp_session
schan_session_for_transport(struct schan_transport
* t
)
720 return t
->ctx
->session
;
723 static int schan_init_sec_ctx_get_next_input_buffer(const struct schan_transport
*t
, struct schan_buffers
*s
)
725 if (s
->current_buffer_idx
!= -1)
727 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_TOKEN
);
730 static int schan_init_sec_ctx_get_next_output_buffer(const struct schan_transport
*t
, struct schan_buffers
*s
)
732 if (s
->current_buffer_idx
== -1)
734 int idx
= schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_TOKEN
);
735 if (t
->ctx
->req_ctx_attr
& ISC_REQ_ALLOCATE_MEMORY
)
739 idx
= schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_EMPTY
);
740 if (idx
!= -1) s
->desc
->pBuffers
[idx
].BufferType
= SECBUFFER_TOKEN
;
742 if (idx
!= -1 && !s
->desc
->pBuffers
[idx
].pvBuffer
)
744 s
->desc
->pBuffers
[idx
].cbBuffer
= 0;
745 s
->allow_buffer_resize
= TRUE
;
754 static void dump_buffer_desc(SecBufferDesc
*desc
)
759 TRACE("Buffer desc %p:\n", desc
);
760 for (i
= 0; i
< desc
->cBuffers
; ++i
)
762 SecBuffer
*b
= &desc
->pBuffers
[i
];
763 TRACE("\tbuffer %u: cbBuffer %d, BufferType %#x pvBuffer %p\n", i
, b
->cbBuffer
, b
->BufferType
, b
->pvBuffer
);
767 /***********************************************************************
768 * InitializeSecurityContextW
770 SECURITY_STATUS SEC_ENTRY
schan_InitializeSecurityContextW(
771 PCredHandle phCredential
, PCtxtHandle phContext
, SEC_WCHAR
*pszTargetName
,
772 ULONG fContextReq
, ULONG Reserved1
, ULONG TargetDataRep
,
773 PSecBufferDesc pInput
, ULONG Reserved2
, PCtxtHandle phNewContext
,
774 PSecBufferDesc pOutput
, ULONG
*pfContextAttr
, PTimeStamp ptsExpiry
)
776 struct schan_context
*ctx
;
777 struct schan_buffers
*out_buffers
;
778 struct schan_credentials
*cred
;
779 struct schan_transport transport
;
780 SIZE_T expected_size
= ~0UL;
783 TRACE("%p %p %s 0x%08x %d %d %p %d %p %p %p %p\n", phCredential
, phContext
,
784 debugstr_w(pszTargetName
), fContextReq
, Reserved1
, TargetDataRep
, pInput
,
785 Reserved1
, phNewContext
, pOutput
, pfContextAttr
, ptsExpiry
);
787 dump_buffer_desc(pInput
);
788 dump_buffer_desc(pOutput
);
794 if (!phCredential
) return SEC_E_INVALID_HANDLE
;
796 cred
= schan_get_object(phCredential
->dwLower
, SCHAN_HANDLE_CRED
);
797 if (!cred
) return SEC_E_INVALID_HANDLE
;
799 if (!(cred
->credential_use
& SECPKG_CRED_OUTBOUND
))
801 WARN("Invalid credential use %#x\n", cred
->credential_use
);
802 return SEC_E_INVALID_HANDLE
;
805 ctx
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ctx
));
806 if (!ctx
) return SEC_E_INSUFFICIENT_MEMORY
;
809 handle
= schan_alloc_handle(ctx
, SCHAN_HANDLE_CTX
);
810 if (handle
== SCHAN_INVALID_HANDLE
)
812 HeapFree(GetProcessHeap(), 0, ctx
);
813 return SEC_E_INTERNAL_ERROR
;
816 if (!schan_imp_create_session(&ctx
->session
, cred
))
818 schan_free_handle(handle
, SCHAN_HANDLE_CTX
);
819 HeapFree(GetProcessHeap(), 0, ctx
);
820 return SEC_E_INTERNAL_ERROR
;
825 UINT len
= WideCharToMultiByte( CP_UNIXCP
, 0, pszTargetName
, -1, NULL
, 0, NULL
, NULL
);
826 char *target
= HeapAlloc( GetProcessHeap(), 0, len
);
830 WideCharToMultiByte( CP_UNIXCP
, 0, pszTargetName
, -1, target
, len
, NULL
, NULL
);
831 schan_imp_set_session_target( ctx
->session
, target
);
832 HeapFree( GetProcessHeap(), 0, target
);
835 phNewContext
->dwLower
= handle
;
836 phNewContext
->dwUpper
= 0;
840 SIZE_T record_size
= 0;
846 return SEC_E_INCOMPLETE_MESSAGE
;
848 idx
= schan_find_sec_buffer_idx(pInput
, 0, SECBUFFER_TOKEN
);
850 return SEC_E_INCOMPLETE_MESSAGE
;
852 buffer
= &pInput
->pBuffers
[idx
];
853 ptr
= buffer
->pvBuffer
;
856 while (buffer
->cbBuffer
> expected_size
+ 5)
858 record_size
= 5 + ((ptr
[3] << 8) | ptr
[4]);
860 if (buffer
->cbBuffer
< expected_size
+ record_size
)
863 expected_size
+= record_size
;
869 TRACE("Expected at least %lu bytes, but buffer only contains %u bytes.\n",
870 max(6, record_size
), buffer
->cbBuffer
);
871 return SEC_E_INCOMPLETE_MESSAGE
;
874 TRACE("Using expected_size %lu.\n", expected_size
);
876 ctx
= schan_get_object(phContext
->dwLower
, SCHAN_HANDLE_CTX
);
879 ctx
->req_ctx_attr
= fContextReq
;
882 init_schan_buffers(&transport
.in
, pInput
, schan_init_sec_ctx_get_next_input_buffer
);
883 transport
.in
.limit
= expected_size
;
884 init_schan_buffers(&transport
.out
, pOutput
, schan_init_sec_ctx_get_next_output_buffer
);
885 schan_imp_set_session_transport(ctx
->session
, &transport
);
887 /* Perform the TLS handshake */
888 ret
= schan_imp_handshake(ctx
->session
);
890 if(transport
.in
.offset
&& transport
.in
.offset
!= pInput
->pBuffers
[0].cbBuffer
) {
891 if(pInput
->cBuffers
<2 || pInput
->pBuffers
[1].BufferType
!=SECBUFFER_EMPTY
)
892 return SEC_E_INVALID_TOKEN
;
894 pInput
->pBuffers
[1].BufferType
= SECBUFFER_EXTRA
;
895 pInput
->pBuffers
[1].cbBuffer
= pInput
->pBuffers
[0].cbBuffer
-transport
.in
.offset
;
898 out_buffers
= &transport
.out
;
899 if (out_buffers
->current_buffer_idx
!= -1)
901 SecBuffer
*buffer
= &out_buffers
->desc
->pBuffers
[out_buffers
->current_buffer_idx
];
902 buffer
->cbBuffer
= out_buffers
->offset
;
906 if (ctx
->req_ctx_attr
& ISC_REQ_ALLOCATE_MEMORY
)
907 *pfContextAttr
|= ISC_RET_ALLOCATED_MEMORY
;
912 /***********************************************************************
913 * InitializeSecurityContextA
915 SECURITY_STATUS SEC_ENTRY
schan_InitializeSecurityContextA(
916 PCredHandle phCredential
, PCtxtHandle phContext
, SEC_CHAR
*pszTargetName
,
917 ULONG fContextReq
, ULONG Reserved1
, ULONG TargetDataRep
,
918 PSecBufferDesc pInput
, ULONG Reserved2
, PCtxtHandle phNewContext
,
919 PSecBufferDesc pOutput
, ULONG
*pfContextAttr
, PTimeStamp ptsExpiry
)
922 SEC_WCHAR
*target_name
= NULL
;
924 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential
, phContext
,
925 debugstr_a(pszTargetName
), fContextReq
, Reserved1
, TargetDataRep
, pInput
,
926 Reserved1
, phNewContext
, pOutput
, pfContextAttr
, ptsExpiry
);
930 INT len
= MultiByteToWideChar(CP_ACP
, 0, pszTargetName
, -1, NULL
, 0);
931 target_name
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(*target_name
));
932 MultiByteToWideChar(CP_ACP
, 0, pszTargetName
, -1, target_name
, len
);
935 ret
= schan_InitializeSecurityContextW(phCredential
, phContext
, target_name
,
936 fContextReq
, Reserved1
, TargetDataRep
, pInput
, Reserved2
,
937 phNewContext
, pOutput
, pfContextAttr
, ptsExpiry
);
939 HeapFree(GetProcessHeap(), 0, target_name
);
945 SECURITY_STATUS SEC_ENTRY
schan_QueryContextAttributesW(
946 PCtxtHandle context_handle
, ULONG attribute
, PVOID buffer
)
948 struct schan_context
*ctx
;
950 TRACE("context_handle %p, attribute %#x, buffer %p\n",
951 context_handle
, attribute
, buffer
);
953 if (!context_handle
) return SEC_E_INVALID_HANDLE
;
954 ctx
= schan_get_object(context_handle
->dwLower
, SCHAN_HANDLE_CTX
);
958 case SECPKG_ATTR_STREAM_SIZES
:
960 SecPkgContext_ConnectionInfo info
;
961 SECURITY_STATUS status
= schan_imp_get_connection_info(ctx
->session
, &info
);
962 if (status
== SEC_E_OK
)
964 SecPkgContext_StreamSizes
*stream_sizes
= buffer
;
965 SIZE_T mac_size
= info
.dwHashStrength
;
966 unsigned int block_size
= schan_imp_get_session_cipher_block_size(ctx
->session
);
967 unsigned int message_size
= schan_imp_get_max_message_size(ctx
->session
);
969 TRACE("Using %lu mac bytes, message size %u, block size %u\n",
970 mac_size
, message_size
, block_size
);
972 /* These are defined by the TLS RFC */
973 stream_sizes
->cbHeader
= 5;
974 stream_sizes
->cbTrailer
= mac_size
+ 256; /* Max 255 bytes padding + 1 for padding size */
975 stream_sizes
->cbMaximumMessage
= message_size
;
976 stream_sizes
->cbBuffers
= 4;
977 stream_sizes
->cbBlockSize
= block_size
;
982 case SECPKG_ATTR_REMOTE_CERT_CONTEXT
:
984 PCCERT_CONTEXT
*cert
= buffer
;
987 HCERTSTORE cert_store
;
988 SECURITY_STATUS status
;
990 cert_store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0, CERT_STORE_CREATE_NEW_FLAG
, NULL
);
992 return GetLastError();
994 status
= schan_imp_get_session_peer_certificate(ctx
->session
, cert_store
, &ctx
->cert
);
995 CertCloseStore(cert_store
, 0);
996 if(status
!= SEC_E_OK
)
1000 *cert
= CertDuplicateCertificateContext(ctx
->cert
);
1003 case SECPKG_ATTR_CONNECTION_INFO
:
1005 SecPkgContext_ConnectionInfo
*info
= buffer
;
1006 return schan_imp_get_connection_info(ctx
->session
, info
);
1010 FIXME("Unhandled attribute %#x\n", attribute
);
1011 return SEC_E_UNSUPPORTED_FUNCTION
;
1016 SECURITY_STATUS SEC_ENTRY
schan_QueryContextAttributesA(
1017 PCtxtHandle context_handle
, ULONG attribute
, PVOID buffer
)
1019 TRACE("context_handle %p, attribute %#x, buffer %p\n",
1020 context_handle
, attribute
, buffer
);
1024 case SECPKG_ATTR_STREAM_SIZES
:
1025 return schan_QueryContextAttributesW(context_handle
, attribute
, buffer
);
1026 case SECPKG_ATTR_REMOTE_CERT_CONTEXT
:
1027 return schan_QueryContextAttributesW(context_handle
, attribute
, buffer
);
1028 case SECPKG_ATTR_CONNECTION_INFO
:
1029 return schan_QueryContextAttributesW(context_handle
, attribute
, buffer
);
1032 FIXME("Unhandled attribute %#x\n", attribute
);
1033 return SEC_E_UNSUPPORTED_FUNCTION
;
1037 static int schan_encrypt_message_get_next_buffer(const struct schan_transport
*t
, struct schan_buffers
*s
)
1041 if (s
->current_buffer_idx
== -1)
1042 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_STREAM_HEADER
);
1044 b
= &s
->desc
->pBuffers
[s
->current_buffer_idx
];
1046 if (b
->BufferType
== SECBUFFER_STREAM_HEADER
)
1047 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_DATA
);
1049 if (b
->BufferType
== SECBUFFER_DATA
)
1050 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_STREAM_TRAILER
);
1055 static int schan_encrypt_message_get_next_buffer_token(const struct schan_transport
*t
, struct schan_buffers
*s
)
1059 if (s
->current_buffer_idx
== -1)
1060 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_TOKEN
);
1062 b
= &s
->desc
->pBuffers
[s
->current_buffer_idx
];
1064 if (b
->BufferType
== SECBUFFER_TOKEN
)
1066 int idx
= schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_TOKEN
);
1067 if (idx
!= s
->current_buffer_idx
) return -1;
1068 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_DATA
);
1071 if (b
->BufferType
== SECBUFFER_DATA
)
1073 int idx
= schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_TOKEN
);
1075 idx
= schan_find_sec_buffer_idx(s
->desc
, idx
+ 1, SECBUFFER_TOKEN
);
1082 static SECURITY_STATUS SEC_ENTRY
schan_EncryptMessage(PCtxtHandle context_handle
,
1083 ULONG quality
, PSecBufferDesc message
, ULONG message_seq_no
)
1085 struct schan_transport transport
;
1086 struct schan_context
*ctx
;
1087 struct schan_buffers
*b
;
1088 SECURITY_STATUS status
;
1095 TRACE("context_handle %p, quality %d, message %p, message_seq_no %d\n",
1096 context_handle
, quality
, message
, message_seq_no
);
1098 if (!context_handle
) return SEC_E_INVALID_HANDLE
;
1099 ctx
= schan_get_object(context_handle
->dwLower
, SCHAN_HANDLE_CTX
);
1101 dump_buffer_desc(message
);
1103 idx
= schan_find_sec_buffer_idx(message
, 0, SECBUFFER_DATA
);
1106 WARN("No data buffer passed\n");
1107 return SEC_E_INTERNAL_ERROR
;
1109 buffer
= &message
->pBuffers
[idx
];
1111 data_size
= buffer
->cbBuffer
;
1112 data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
1113 memcpy(data
, buffer
->pvBuffer
, data_size
);
1115 transport
.ctx
= ctx
;
1116 init_schan_buffers(&transport
.in
, NULL
, NULL
);
1117 if (schan_find_sec_buffer_idx(message
, 0, SECBUFFER_STREAM_HEADER
) != -1)
1118 init_schan_buffers(&transport
.out
, message
, schan_encrypt_message_get_next_buffer
);
1120 init_schan_buffers(&transport
.out
, message
, schan_encrypt_message_get_next_buffer_token
);
1121 schan_imp_set_session_transport(ctx
->session
, &transport
);
1124 status
= schan_imp_send(ctx
->session
, data
, &length
);
1126 TRACE("Sent %ld bytes.\n", length
);
1128 if (length
!= data_size
)
1129 status
= SEC_E_INTERNAL_ERROR
;
1132 b
->desc
->pBuffers
[b
->current_buffer_idx
].cbBuffer
= b
->offset
;
1133 HeapFree(GetProcessHeap(), 0, data
);
1135 TRACE("Returning %#x.\n", status
);
1140 static int schan_decrypt_message_get_next_buffer(const struct schan_transport
*t
, struct schan_buffers
*s
)
1142 if (s
->current_buffer_idx
== -1)
1143 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_DATA
);
1148 static int schan_validate_decrypt_buffer_desc(PSecBufferDesc message
)
1151 unsigned int empty_count
= 0;
1154 if (message
->cBuffers
< 4)
1156 WARN("Less than four buffers passed\n");
1160 for (i
= 0; i
< message
->cBuffers
; ++i
)
1162 SecBuffer
*b
= &message
->pBuffers
[i
];
1163 if (b
->BufferType
== SECBUFFER_DATA
)
1167 WARN("More than one data buffer passed\n");
1172 else if (b
->BufferType
== SECBUFFER_EMPTY
)
1178 WARN("No data buffer passed\n");
1182 if (empty_count
< 3)
1184 WARN("Less than three empty buffers passed\n");
1191 static void schan_decrypt_fill_buffer(PSecBufferDesc message
, ULONG buffer_type
, void *data
, ULONG size
)
1196 idx
= schan_find_sec_buffer_idx(message
, 0, SECBUFFER_EMPTY
);
1197 buffer
= &message
->pBuffers
[idx
];
1199 buffer
->BufferType
= buffer_type
;
1200 buffer
->pvBuffer
= data
;
1201 buffer
->cbBuffer
= size
;
1204 static SECURITY_STATUS SEC_ENTRY
schan_DecryptMessage(PCtxtHandle context_handle
,
1205 PSecBufferDesc message
, ULONG message_seq_no
, PULONG quality
)
1207 struct schan_transport transport
;
1208 struct schan_context
*ctx
;
1212 unsigned expected_size
;
1213 SSIZE_T received
= 0;
1215 unsigned char *buf_ptr
;
1217 TRACE("context_handle %p, message %p, message_seq_no %d, quality %p\n",
1218 context_handle
, message
, message_seq_no
, quality
);
1220 if (!context_handle
) return SEC_E_INVALID_HANDLE
;
1221 ctx
= schan_get_object(context_handle
->dwLower
, SCHAN_HANDLE_CTX
);
1223 dump_buffer_desc(message
);
1225 idx
= schan_validate_decrypt_buffer_desc(message
);
1227 return SEC_E_INVALID_TOKEN
;
1228 buffer
= &message
->pBuffers
[idx
];
1229 buf_ptr
= buffer
->pvBuffer
;
1231 expected_size
= 5 + ((buf_ptr
[3] << 8) | buf_ptr
[4]);
1232 if(buffer
->cbBuffer
< expected_size
)
1234 TRACE("Expected %u bytes, but buffer only contains %u bytes\n", expected_size
, buffer
->cbBuffer
);
1235 buffer
->BufferType
= SECBUFFER_MISSING
;
1236 buffer
->cbBuffer
= expected_size
- buffer
->cbBuffer
;
1238 /* This is a bit weird, but windows does it too */
1239 idx
= schan_find_sec_buffer_idx(message
, 0, SECBUFFER_EMPTY
);
1240 buffer
= &message
->pBuffers
[idx
];
1241 buffer
->BufferType
= SECBUFFER_MISSING
;
1242 buffer
->cbBuffer
= expected_size
- buffer
->cbBuffer
;
1244 TRACE("Returning SEC_E_INCOMPLETE_MESSAGE\n");
1245 return SEC_E_INCOMPLETE_MESSAGE
;
1248 data_size
= expected_size
- 5;
1249 data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
1251 transport
.ctx
= ctx
;
1252 init_schan_buffers(&transport
.in
, message
, schan_decrypt_message_get_next_buffer
);
1253 transport
.in
.limit
= expected_size
;
1254 init_schan_buffers(&transport
.out
, NULL
, NULL
);
1255 schan_imp_set_session_transport(ctx
->session
, &transport
);
1257 while (received
< data_size
)
1259 SIZE_T length
= data_size
- received
;
1260 SECURITY_STATUS status
= schan_imp_recv(ctx
->session
, data
+ received
, &length
);
1262 if (status
== SEC_I_CONTINUE_NEEDED
)
1265 if (status
!= SEC_E_OK
)
1267 HeapFree(GetProcessHeap(), 0, data
);
1268 ERR("Returning %x\n", status
);
1278 TRACE("Received %ld bytes\n", received
);
1280 memcpy(buf_ptr
+ 5, data
, received
);
1281 HeapFree(GetProcessHeap(), 0, data
);
1283 schan_decrypt_fill_buffer(message
, SECBUFFER_DATA
,
1284 buf_ptr
+ 5, received
);
1286 schan_decrypt_fill_buffer(message
, SECBUFFER_STREAM_TRAILER
,
1287 buf_ptr
+ 5 + received
, buffer
->cbBuffer
- 5 - received
);
1289 if(buffer
->cbBuffer
> expected_size
)
1290 schan_decrypt_fill_buffer(message
, SECBUFFER_EXTRA
,
1291 buf_ptr
+ expected_size
, buffer
->cbBuffer
- expected_size
);
1293 buffer
->BufferType
= SECBUFFER_STREAM_HEADER
;
1294 buffer
->cbBuffer
= 5;
1299 SECURITY_STATUS SEC_ENTRY
schan_DeleteSecurityContext(PCtxtHandle context_handle
)
1301 struct schan_context
*ctx
;
1303 TRACE("context_handle %p\n", context_handle
);
1305 if (!context_handle
) return SEC_E_INVALID_HANDLE
;
1307 ctx
= schan_free_handle(context_handle
->dwLower
, SCHAN_HANDLE_CTX
);
1308 if (!ctx
) return SEC_E_INVALID_HANDLE
;
1311 CertFreeCertificateContext(ctx
->cert
);
1312 schan_imp_dispose_session(ctx
->session
);
1313 HeapFree(GetProcessHeap(), 0, ctx
);
1318 SecurityFunctionTableA schanTableA
= {
1320 schan_EnumerateSecurityPackagesA
,
1321 schan_QueryCredentialsAttributesA
,
1322 schan_AcquireCredentialsHandleA
,
1323 schan_FreeCredentialsHandle
,
1324 NULL
, /* Reserved2 */
1325 schan_InitializeSecurityContextA
,
1326 NULL
, /* AcceptSecurityContext */
1327 NULL
, /* CompleteAuthToken */
1328 schan_DeleteSecurityContext
,
1329 NULL
, /* ApplyControlToken */
1330 schan_QueryContextAttributesA
,
1331 NULL
, /* ImpersonateSecurityContext */
1332 NULL
, /* RevertSecurityContext */
1333 NULL
, /* MakeSignature */
1334 NULL
, /* VerifySignature */
1335 schan_FreeContextBuffer
,
1336 NULL
, /* QuerySecurityPackageInfoA */
1337 NULL
, /* Reserved3 */
1338 NULL
, /* Reserved4 */
1339 NULL
, /* ExportSecurityContext */
1340 NULL
, /* ImportSecurityContextA */
1341 NULL
, /* AddCredentialsA */
1342 NULL
, /* Reserved8 */
1343 NULL
, /* QuerySecurityContextToken */
1344 schan_EncryptMessage
,
1345 schan_DecryptMessage
,
1346 NULL
, /* SetContextAttributesA */
1349 SecurityFunctionTableW schanTableW
= {
1351 schan_EnumerateSecurityPackagesW
,
1352 schan_QueryCredentialsAttributesW
,
1353 schan_AcquireCredentialsHandleW
,
1354 schan_FreeCredentialsHandle
,
1355 NULL
, /* Reserved2 */
1356 schan_InitializeSecurityContextW
,
1357 NULL
, /* AcceptSecurityContext */
1358 NULL
, /* CompleteAuthToken */
1359 schan_DeleteSecurityContext
,
1360 NULL
, /* ApplyControlToken */
1361 schan_QueryContextAttributesW
,
1362 NULL
, /* ImpersonateSecurityContext */
1363 NULL
, /* RevertSecurityContext */
1364 NULL
, /* MakeSignature */
1365 NULL
, /* VerifySignature */
1366 schan_FreeContextBuffer
,
1367 NULL
, /* QuerySecurityPackageInfoW */
1368 NULL
, /* Reserved3 */
1369 NULL
, /* Reserved4 */
1370 NULL
, /* ExportSecurityContext */
1371 NULL
, /* ImportSecurityContextW */
1372 NULL
, /* AddCredentialsW */
1373 NULL
, /* Reserved8 */
1374 NULL
, /* QuerySecurityContextToken */
1375 schan_EncryptMessage
,
1376 schan_DecryptMessage
,
1377 NULL
, /* SetContextAttributesW */
1380 static const WCHAR schannelComment
[] = { 'S','c','h','a','n','n','e','l',' ',
1381 'S','e','c','u','r','i','t','y',' ','P','a','c','k','a','g','e',0 };
1382 static const WCHAR schannelDllName
[] = { 's','c','h','a','n','n','e','l','.','d','l','l',0 };
1384 void SECUR32_initSchannelSP(void)
1386 /* This is what Windows reports. This shouldn't break any applications
1387 * even though the functions are missing, because the wrapper will
1388 * return SEC_E_UNSUPPORTED_FUNCTION if our function is NULL.
1390 static const LONG caps
=
1391 SECPKG_FLAG_INTEGRITY
|
1392 SECPKG_FLAG_PRIVACY
|
1393 SECPKG_FLAG_CONNECTION
|
1394 SECPKG_FLAG_MULTI_REQUIRED
|
1395 SECPKG_FLAG_EXTENDED_ERROR
|
1396 SECPKG_FLAG_IMPERSONATION
|
1397 SECPKG_FLAG_ACCEPT_WIN32_NAME
|
1399 static const short version
= 1;
1400 static const LONG maxToken
= 16384;
1401 SEC_WCHAR
*uniSPName
= (SEC_WCHAR
*)UNISP_NAME_W
,
1402 *schannel
= (SEC_WCHAR
*)SCHANNEL_NAME_W
;
1403 const SecPkgInfoW info
[] = {
1404 { caps
, version
, UNISP_RPC_ID
, maxToken
, uniSPName
, uniSPName
},
1405 { caps
, version
, UNISP_RPC_ID
, maxToken
, schannel
,
1406 (SEC_WCHAR
*)schannelComment
},
1408 SecureProvider
*provider
;
1410 if (!schan_imp_init())
1413 schan_handle_table
= HeapAlloc(GetProcessHeap(), 0, 64 * sizeof(*schan_handle_table
));
1414 if (!schan_handle_table
)
1416 ERR("Failed to allocate schannel handle table.\n");
1419 schan_handle_table_size
= 64;
1421 provider
= SECUR32_addProvider(&schanTableA
, &schanTableW
, schannelDllName
);
1424 ERR("Failed to add schannel provider.\n");
1428 SECUR32_addPackages(provider
, sizeof(info
) / sizeof(info
[0]), NULL
, info
);
1433 HeapFree(GetProcessHeap(), 0, schan_handle_table
);
1434 schan_handle_table
= NULL
;
1439 void SECUR32_deinitSchannelSP(void)
1441 SIZE_T i
= schan_handle_count
;
1443 if (!schan_handle_table
) return;
1445 /* deinitialized sessions first because a pointer to the credentials
1446 * may be stored for the session. */
1449 if (schan_handle_table
[i
].type
== SCHAN_HANDLE_CTX
)
1451 struct schan_context
*ctx
= schan_free_handle(i
, SCHAN_HANDLE_CTX
);
1452 schan_imp_dispose_session(ctx
->session
);
1453 HeapFree(GetProcessHeap(), 0, ctx
);
1456 i
= schan_handle_count
;
1459 if (schan_handle_table
[i
].type
!= SCHAN_HANDLE_FREE
)
1461 struct schan_credentials
*cred
;
1462 cred
= schan_free_handle(i
, SCHAN_HANDLE_CRED
);
1463 schan_imp_free_certificate_credentials(cred
);
1464 HeapFree(GetProcessHeap(), 0, cred
);
1467 HeapFree(GetProcessHeap(), 0, schan_handle_table
);
1471 #else /* SONAME_LIBGNUTLS || HAVE_SECURITY_SECURITY_H || SONAME_LIBMBEDTLS */
1473 void SECUR32_initSchannelSP(void)
1475 ERR("TLS library not found, SSL connections will fail\n");
1478 void SECUR32_deinitSchannelSP(void) {}
1480 #endif /* SONAME_LIBGNUTLS || HAVE_SECURITY_SECURITY_H || SONAME_LIBMBEDTLS */