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 #if defined(SONAME_LIBGNUTLS) || defined (HAVE_SECURITY_SECURITY_H)
27 #define SCHAN_INVALID_HANDLE ~0UL
29 enum schan_handle_type
39 enum schan_handle_type type
;
44 schan_imp_session session
;
46 const CERT_CONTEXT
*cert
;
49 static struct schan_handle
*schan_handle_table
;
50 static struct schan_handle
*schan_free_handles
;
51 static SIZE_T schan_handle_table_size
;
52 static SIZE_T schan_handle_count
;
54 /* Protocols enabled, only those may be used for the connection. */
55 static DWORD config_enabled_protocols
;
57 /* Protocols disabled by default. They are enabled for using, but disabled when caller asks for default settings. */
58 static DWORD config_default_disabled_protocols
;
60 static ULONG_PTR
schan_alloc_handle(void *object
, enum schan_handle_type type
)
62 struct schan_handle
*handle
;
64 if (schan_free_handles
)
66 DWORD index
= schan_free_handles
- schan_handle_table
;
67 /* Use a free handle */
68 handle
= schan_free_handles
;
69 if (handle
->type
!= SCHAN_HANDLE_FREE
)
71 ERR("Handle %d(%p) is in the free list, but has type %#x.\n", index
, handle
, handle
->type
);
72 return SCHAN_INVALID_HANDLE
;
74 schan_free_handles
= handle
->object
;
75 handle
->object
= object
;
80 if (!(schan_handle_count
< schan_handle_table_size
))
83 SIZE_T new_size
= schan_handle_table_size
+ (schan_handle_table_size
>> 1);
84 struct schan_handle
*new_table
= HeapReAlloc(GetProcessHeap(), 0, schan_handle_table
, new_size
* sizeof(*schan_handle_table
));
87 ERR("Failed to grow the handle table\n");
88 return SCHAN_INVALID_HANDLE
;
90 schan_handle_table
= new_table
;
91 schan_handle_table_size
= new_size
;
94 handle
= &schan_handle_table
[schan_handle_count
++];
95 handle
->object
= object
;
98 return handle
- schan_handle_table
;
101 static void *schan_free_handle(ULONG_PTR handle_idx
, enum schan_handle_type type
)
103 struct schan_handle
*handle
;
106 if (handle_idx
== SCHAN_INVALID_HANDLE
) return NULL
;
107 if (handle_idx
>= schan_handle_count
) return NULL
;
108 handle
= &schan_handle_table
[handle_idx
];
109 if (handle
->type
!= type
)
111 ERR("Handle %ld(%p) is not of type %#x\n", handle_idx
, handle
, type
);
115 object
= handle
->object
;
116 handle
->object
= schan_free_handles
;
117 handle
->type
= SCHAN_HANDLE_FREE
;
118 schan_free_handles
= handle
;
123 static void *schan_get_object(ULONG_PTR handle_idx
, enum schan_handle_type type
)
125 struct schan_handle
*handle
;
127 if (handle_idx
== SCHAN_INVALID_HANDLE
) return NULL
;
128 if (handle_idx
>= schan_handle_count
) return NULL
;
129 handle
= &schan_handle_table
[handle_idx
];
130 if (handle
->type
!= type
)
132 ERR("Handle %ld(%p) is not of type %#x\n", handle_idx
, handle
, type
);
136 return handle
->object
;
139 static void read_config(void)
141 DWORD enabled
= 0, default_disabled
= 0;
142 HKEY protocols_key
, key
;
143 WCHAR subkey_name
[64];
147 static BOOL config_read
= FALSE
;
149 static const WCHAR protocol_config_key_name
[] = {
150 'S','Y','S','T','E','M','\\',
151 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
152 'C','o','n','t','r','o','l','\\',
153 'S','e','c','u','r','i','t','y','P','r','o','v','i','d','e','r','s','\\',
154 'S','C','H','A','N','N','E','L','\\',
155 'P','r','o','t','o','c','o','l','s',0 };
157 static const WCHAR clientW
[] = {'\\','C','l','i','e','n','t',0};
158 static const WCHAR enabledW
[] = {'e','n','a','b','l','e','d',0};
159 static const WCHAR disabledbydefaultW
[] = {'D','i','s','a','b','l','e','d','B','y','D','e','f','a','u','l','t',0};
161 static const struct {
163 DWORD prot_client_flag
;
164 BOOL enabled
; /* If no config is present, enable the protocol */
165 BOOL disabled_by_default
; /* Disable if caller asks for default protocol set */
166 } protocol_config_keys
[] = {
167 {{'S','S','L',' ','2','.','0',0}, SP_PROT_SSL2_CLIENT
, FALSE
, TRUE
}, /* NOTE: TRUE, TRUE on Windows */
168 {{'S','S','L',' ','3','.','0',0}, SP_PROT_SSL3_CLIENT
, TRUE
, FALSE
},
169 {{'T','L','S',' ','1','.','0',0}, SP_PROT_TLS1_0_CLIENT
, TRUE
, FALSE
},
170 {{'T','L','S',' ','1','.','1',0}, SP_PROT_TLS1_1_CLIENT
, TRUE
, FALSE
/* NOTE: not enabled by default on Windows */ },
171 {{'T','L','S',' ','1','.','2',0}, SP_PROT_TLS1_2_CLIENT
, TRUE
, FALSE
/* NOTE: not enabled by default on Windows */ }
174 /* No need for thread safety */
178 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, protocol_config_key_name
, 0, KEY_READ
, &protocols_key
);
179 if(res
== ERROR_SUCCESS
) {
180 DWORD type
, size
, value
;
182 for(i
=0; i
< sizeof(protocol_config_keys
)/sizeof(*protocol_config_keys
); i
++) {
183 strcpyW(subkey_name
, protocol_config_keys
[i
].key_name
);
184 strcatW(subkey_name
, clientW
);
185 res
= RegOpenKeyExW(protocols_key
, subkey_name
, 0, KEY_READ
, &key
);
186 if(res
!= ERROR_SUCCESS
) {
187 if(protocol_config_keys
[i
].enabled
)
188 enabled
|= protocol_config_keys
[i
].prot_client_flag
;
189 if(protocol_config_keys
[i
].disabled_by_default
)
190 default_disabled
|= protocol_config_keys
[i
].prot_client_flag
;
194 size
= sizeof(value
);
195 res
= RegQueryValueExW(key
, enabledW
, NULL
, &type
, (BYTE
*)&value
, &size
);
196 if(res
== ERROR_SUCCESS
) {
197 if(type
== REG_DWORD
&& value
)
198 enabled
|= protocol_config_keys
[i
].prot_client_flag
;
199 }else if(protocol_config_keys
[i
].enabled
) {
200 enabled
|= protocol_config_keys
[i
].prot_client_flag
;
203 size
= sizeof(value
);
204 res
= RegQueryValueExW(key
, disabledbydefaultW
, NULL
, &type
, (BYTE
*)&value
, &size
);
205 if(res
== ERROR_SUCCESS
) {
206 if(type
!= REG_DWORD
|| value
)
207 default_disabled
|= protocol_config_keys
[i
].prot_client_flag
;
208 }else if(protocol_config_keys
[i
].disabled_by_default
) {
209 default_disabled
|= protocol_config_keys
[i
].prot_client_flag
;
215 /* No config, enable all known protocols. */
216 for(i
=0; i
< sizeof(protocol_config_keys
)/sizeof(*protocol_config_keys
); i
++) {
217 if(protocol_config_keys
[i
].enabled
)
218 enabled
|= protocol_config_keys
[i
].prot_client_flag
;
219 if(protocol_config_keys
[i
].disabled_by_default
)
220 default_disabled
|= protocol_config_keys
[i
].prot_client_flag
;
224 RegCloseKey(protocols_key
);
226 config_enabled_protocols
= enabled
& schan_imp_enabled_protocols();
227 config_default_disabled_protocols
= default_disabled
;
230 TRACE("enabled %x, disabled by default %x\n", config_enabled_protocols
, config_default_disabled_protocols
);
233 static SECURITY_STATUS
schan_QueryCredentialsAttributes(
234 PCredHandle phCredential
, ULONG ulAttribute
, VOID
*pBuffer
)
236 struct schan_credentials
*cred
;
239 cred
= schan_get_object(phCredential
->dwLower
, SCHAN_HANDLE_CRED
);
241 return SEC_E_INVALID_HANDLE
;
245 case SECPKG_ATTR_SUPPORTED_ALGS
:
248 /* FIXME: get from CryptoAPI */
249 FIXME("SECPKG_ATTR_SUPPORTED_ALGS: stub\n");
250 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
253 ret
= SEC_E_INTERNAL_ERROR
;
255 case SECPKG_ATTR_CIPHER_STRENGTHS
:
258 SecPkgCred_CipherStrengths
*r
= pBuffer
;
260 /* FIXME: get from CryptoAPI */
261 FIXME("SECPKG_ATTR_CIPHER_STRENGTHS: semi-stub\n");
262 r
->dwMinimumCipherStrength
= 40;
263 r
->dwMaximumCipherStrength
= 168;
267 ret
= SEC_E_INTERNAL_ERROR
;
269 case SECPKG_ATTR_SUPPORTED_PROTOCOLS
:
271 /* Regardless of MSDN documentation, tests show that this attribute takes into account
272 * what protocols are enabled for given credential. */
273 ((SecPkgCred_SupportedProtocols
*)pBuffer
)->grbitProtocol
= cred
->enabled_protocols
;
276 ret
= SEC_E_INTERNAL_ERROR
;
280 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
285 static SECURITY_STATUS SEC_ENTRY
schan_QueryCredentialsAttributesA(
286 PCredHandle phCredential
, ULONG ulAttribute
, PVOID pBuffer
)
290 TRACE("(%p, %d, %p)\n", phCredential
, ulAttribute
, pBuffer
);
294 case SECPKG_CRED_ATTR_NAMES
:
295 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
296 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
299 ret
= schan_QueryCredentialsAttributes(phCredential
, ulAttribute
,
305 SECURITY_STATUS SEC_ENTRY
schan_QueryCredentialsAttributesW(
306 PCredHandle phCredential
, ULONG ulAttribute
, PVOID pBuffer
)
310 TRACE("(%p, %d, %p)\n", phCredential
, ulAttribute
, pBuffer
);
314 case SECPKG_CRED_ATTR_NAMES
:
315 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
316 ret
= SEC_E_UNSUPPORTED_FUNCTION
;
319 ret
= schan_QueryCredentialsAttributes(phCredential
, ulAttribute
,
325 static SECURITY_STATUS
schan_CheckCreds(const SCHANNEL_CRED
*schanCred
)
330 TRACE("dwVersion = %d\n", schanCred
->dwVersion
);
331 TRACE("cCreds = %d\n", schanCred
->cCreds
);
332 TRACE("hRootStore = %p\n", schanCred
->hRootStore
);
333 TRACE("cMappers = %d\n", schanCred
->cMappers
);
334 TRACE("cSupportedAlgs = %d:\n", schanCred
->cSupportedAlgs
);
335 for (i
= 0; i
< schanCred
->cSupportedAlgs
; i
++)
336 TRACE("%08x\n", schanCred
->palgSupportedAlgs
[i
]);
337 TRACE("grbitEnabledProtocols = %08x\n", schanCred
->grbitEnabledProtocols
);
338 TRACE("dwMinimumCipherStrength = %d\n", schanCred
->dwMinimumCipherStrength
);
339 TRACE("dwMaximumCipherStrength = %d\n", schanCred
->dwMaximumCipherStrength
);
340 TRACE("dwSessionLifespan = %d\n", schanCred
->dwSessionLifespan
);
341 TRACE("dwFlags = %08x\n", schanCred
->dwFlags
);
342 TRACE("dwCredFormat = %d\n", schanCred
->dwCredFormat
);
344 switch (schanCred
->dwVersion
)
347 case SCHANNEL_CRED_VERSION
:
350 return SEC_E_INTERNAL_ERROR
;
353 if (schanCred
->cCreds
== 0)
354 st
= SEC_E_NO_CREDENTIALS
;
355 else if (schanCred
->cCreds
> 1)
356 st
= SEC_E_UNKNOWN_CREDENTIALS
;
363 ret
= CryptAcquireCertificatePrivateKey(schanCred
->paCred
[0],
364 0, /* FIXME: what flags to use? */ NULL
,
365 &csp
, &keySpec
, &freeCSP
);
370 CryptReleaseContext(csp
, 0);
373 st
= SEC_E_UNKNOWN_CREDENTIALS
;
378 static SECURITY_STATUS
schan_AcquireClientCredentials(const SCHANNEL_CRED
*schanCred
,
379 PCredHandle phCredential
, PTimeStamp ptsExpiry
)
381 struct schan_credentials
*creds
;
382 unsigned enabled_protocols
;
384 SECURITY_STATUS st
= SEC_E_OK
;
386 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred
, phCredential
, ptsExpiry
);
390 st
= schan_CheckCreds(schanCred
);
391 if (st
!= SEC_E_OK
&& st
!= SEC_E_NO_CREDENTIALS
)
398 if(schanCred
&& schanCred
->grbitEnabledProtocols
)
399 enabled_protocols
= schanCred
->grbitEnabledProtocols
& config_enabled_protocols
;
401 enabled_protocols
= config_enabled_protocols
& ~config_default_disabled_protocols
;
402 if(!enabled_protocols
) {
403 ERR("Could not find matching protocol\n");
404 return SEC_E_NO_AUTHENTICATING_AUTHORITY
;
407 /* For now, the only thing I'm interested in is the direction of the
408 * connection, so just store it.
410 creds
= HeapAlloc(GetProcessHeap(), 0, sizeof(*creds
));
411 if (!creds
) return SEC_E_INSUFFICIENT_MEMORY
;
413 handle
= schan_alloc_handle(creds
, SCHAN_HANDLE_CRED
);
414 if (handle
== SCHAN_INVALID_HANDLE
) goto fail
;
416 creds
->credential_use
= SECPKG_CRED_OUTBOUND
;
417 if (!schan_imp_allocate_certificate_credentials(creds
))
419 schan_free_handle(handle
, SCHAN_HANDLE_CRED
);
423 creds
->enabled_protocols
= enabled_protocols
;
424 phCredential
->dwLower
= handle
;
425 phCredential
->dwUpper
= 0;
427 /* Outbound credentials have no expiry */
430 ptsExpiry
->LowPart
= 0;
431 ptsExpiry
->HighPart
= 0;
437 HeapFree(GetProcessHeap(), 0, creds
);
438 return SEC_E_INTERNAL_ERROR
;
441 static SECURITY_STATUS
schan_AcquireServerCredentials(const SCHANNEL_CRED
*schanCred
,
442 PCredHandle phCredential
, PTimeStamp ptsExpiry
)
446 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred
, phCredential
, ptsExpiry
);
448 if (!schanCred
) return SEC_E_NO_CREDENTIALS
;
450 st
= schan_CheckCreds(schanCred
);
454 struct schan_credentials
*creds
;
456 creds
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*creds
));
457 if (!creds
) return SEC_E_INSUFFICIENT_MEMORY
;
458 creds
->credential_use
= SECPKG_CRED_INBOUND
;
460 handle
= schan_alloc_handle(creds
, SCHAN_HANDLE_CRED
);
461 if (handle
== SCHAN_INVALID_HANDLE
)
463 HeapFree(GetProcessHeap(), 0, creds
);
464 return SEC_E_INTERNAL_ERROR
;
467 phCredential
->dwLower
= handle
;
468 phCredential
->dwUpper
= 0;
470 /* FIXME: get expiry from cert */
475 static SECURITY_STATUS
schan_AcquireCredentialsHandle(ULONG fCredentialUse
,
476 const SCHANNEL_CRED
*schanCred
, PCredHandle phCredential
, PTimeStamp ptsExpiry
)
480 if (fCredentialUse
== SECPKG_CRED_OUTBOUND
)
481 ret
= schan_AcquireClientCredentials(schanCred
, phCredential
,
484 ret
= schan_AcquireServerCredentials(schanCred
, phCredential
,
489 SECURITY_STATUS SEC_ENTRY
schan_AcquireCredentialsHandleA(
490 SEC_CHAR
*pszPrincipal
, SEC_CHAR
*pszPackage
, ULONG fCredentialUse
,
491 PLUID pLogonID
, PVOID pAuthData
, SEC_GET_KEY_FN pGetKeyFn
,
492 PVOID pGetKeyArgument
, PCredHandle phCredential
, PTimeStamp ptsExpiry
)
494 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
495 debugstr_a(pszPrincipal
), debugstr_a(pszPackage
), fCredentialUse
,
496 pLogonID
, pAuthData
, pGetKeyFn
, pGetKeyArgument
, phCredential
, ptsExpiry
);
497 return schan_AcquireCredentialsHandle(fCredentialUse
,
498 pAuthData
, phCredential
, ptsExpiry
);
501 SECURITY_STATUS SEC_ENTRY
schan_AcquireCredentialsHandleW(
502 SEC_WCHAR
*pszPrincipal
, SEC_WCHAR
*pszPackage
, ULONG fCredentialUse
,
503 PLUID pLogonID
, PVOID pAuthData
, SEC_GET_KEY_FN pGetKeyFn
,
504 PVOID pGetKeyArgument
, PCredHandle phCredential
, PTimeStamp ptsExpiry
)
506 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
507 debugstr_w(pszPrincipal
), debugstr_w(pszPackage
), fCredentialUse
,
508 pLogonID
, pAuthData
, pGetKeyFn
, pGetKeyArgument
, phCredential
, ptsExpiry
);
509 return schan_AcquireCredentialsHandle(fCredentialUse
,
510 pAuthData
, phCredential
, ptsExpiry
);
513 SECURITY_STATUS SEC_ENTRY
schan_FreeCredentialsHandle(
514 PCredHandle phCredential
)
516 struct schan_credentials
*creds
;
518 TRACE("phCredential %p\n", phCredential
);
520 if (!phCredential
) return SEC_E_INVALID_HANDLE
;
522 creds
= schan_free_handle(phCredential
->dwLower
, SCHAN_HANDLE_CRED
);
523 if (!creds
) return SEC_E_INVALID_HANDLE
;
525 if (creds
->credential_use
== SECPKG_CRED_OUTBOUND
)
526 schan_imp_free_certificate_credentials(creds
);
527 HeapFree(GetProcessHeap(), 0, creds
);
532 static void init_schan_buffers(struct schan_buffers
*s
, const PSecBufferDesc desc
,
533 int (*get_next_buffer
)(const struct schan_transport
*, struct schan_buffers
*))
538 s
->current_buffer_idx
= -1;
539 s
->allow_buffer_resize
= FALSE
;
540 s
->get_next_buffer
= get_next_buffer
;
543 static int schan_find_sec_buffer_idx(const SecBufferDesc
*desc
, unsigned int start_idx
, ULONG buffer_type
)
548 for (i
= start_idx
; i
< desc
->cBuffers
; ++i
)
550 buffer
= &desc
->pBuffers
[i
];
551 if (buffer
->BufferType
== buffer_type
) return i
;
557 static void schan_resize_current_buffer(const struct schan_buffers
*s
, SIZE_T min_size
)
559 SecBuffer
*b
= &s
->desc
->pBuffers
[s
->current_buffer_idx
];
560 SIZE_T new_size
= b
->cbBuffer
? b
->cbBuffer
* 2 : 128;
563 if (b
->cbBuffer
>= min_size
|| !s
->allow_buffer_resize
|| min_size
> UINT_MAX
/ 2) return;
565 while (new_size
< min_size
) new_size
*= 2;
568 new_data
= HeapReAlloc(GetProcessHeap(), 0, b
->pvBuffer
, new_size
);
570 new_data
= HeapAlloc(GetProcessHeap(), 0, new_size
);
574 TRACE("Failed to resize %p from %d to %ld\n", b
->pvBuffer
, b
->cbBuffer
, new_size
);
578 b
->cbBuffer
= new_size
;
579 b
->pvBuffer
= new_data
;
582 char *schan_get_buffer(const struct schan_transport
*t
, struct schan_buffers
*s
, SIZE_T
*count
)
593 if (s
->current_buffer_idx
== -1)
596 int buffer_idx
= s
->get_next_buffer(t
, s
);
597 if (buffer_idx
== -1)
599 TRACE("No next buffer\n");
602 s
->current_buffer_idx
= buffer_idx
;
605 buffer
= &s
->desc
->pBuffers
[s
->current_buffer_idx
];
606 TRACE("Using buffer %d: cbBuffer %d, BufferType %#x, pvBuffer %p\n", s
->current_buffer_idx
, buffer
->cbBuffer
, buffer
->BufferType
, buffer
->pvBuffer
);
608 schan_resize_current_buffer(s
, s
->offset
+ *count
);
609 max_count
= buffer
->cbBuffer
- s
->offset
;
610 if (s
->limit
!= ~0UL && s
->limit
< max_count
)
611 max_count
= s
->limit
;
616 s
->allow_buffer_resize
= FALSE
;
617 buffer_idx
= s
->get_next_buffer(t
, s
);
618 if (buffer_idx
== -1)
620 TRACE("No next buffer\n");
623 s
->current_buffer_idx
= buffer_idx
;
625 return schan_get_buffer(t
, s
, count
);
628 if (*count
> max_count
)
630 if (s
->limit
!= ~0UL)
633 return (char *)buffer
->pvBuffer
+ s
->offset
;
637 * Read data from the transport input buffer.
639 * t - The session transport object.
640 * buff - The buffer into which to store the read data. Must be at least
641 * *buff_len bytes in length.
642 * buff_len - On input, *buff_len is the desired length to read. On successful
643 * return, *buff_len is the number of bytes actually read.
646 * 0 on success, in which case:
647 * *buff_len == 0 indicates end of file.
648 * *buff_len > 0 indicates that some data was read. May be less than
649 * what was requested, in which case the caller should call again if/
650 * when they want more.
651 * EAGAIN when no data could be read without blocking
652 * another errno-style error value on failure
655 int schan_pull(struct schan_transport
*t
, void *buff
, size_t *buff_len
)
658 SIZE_T local_len
= *buff_len
;
660 TRACE("Pull %lu bytes\n", local_len
);
664 b
= schan_get_buffer(t
, &t
->in
, &local_len
);
668 memcpy(buff
, b
, local_len
);
669 t
->in
.offset
+= local_len
;
671 TRACE("Read %lu bytes\n", local_len
);
673 *buff_len
= local_len
;
678 * Write data to the transport output buffer.
680 * t - The session transport object.
681 * buff - The buffer of data to write. Must be at least *buff_len bytes in length.
682 * buff_len - On input, *buff_len is the desired length to write. On successful
683 * return, *buff_len is the number of bytes actually written.
687 * *buff_len will be > 0 indicating how much data was written. May be less
688 * than what was requested, in which case the caller should call again
689 if/when they want to write more.
690 * EAGAIN when no data could be written without blocking
691 * another errno-style error value on failure
694 int schan_push(struct schan_transport
*t
, const void *buff
, size_t *buff_len
)
697 SIZE_T local_len
= *buff_len
;
699 TRACE("Push %lu bytes\n", local_len
);
703 b
= schan_get_buffer(t
, &t
->out
, &local_len
);
707 memcpy(b
, buff
, local_len
);
708 t
->out
.offset
+= local_len
;
710 TRACE("Wrote %lu bytes\n", local_len
);
712 *buff_len
= local_len
;
716 schan_imp_session
schan_session_for_transport(struct schan_transport
* t
)
718 return t
->ctx
->session
;
721 static int schan_init_sec_ctx_get_next_buffer(const struct schan_transport
*t
, struct schan_buffers
*s
)
723 if (s
->current_buffer_idx
== -1)
725 int idx
= schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_TOKEN
);
726 if (t
->ctx
->req_ctx_attr
& ISC_REQ_ALLOCATE_MEMORY
)
730 idx
= schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_EMPTY
);
731 if (idx
!= -1) s
->desc
->pBuffers
[idx
].BufferType
= SECBUFFER_TOKEN
;
733 if (idx
!= -1 && !s
->desc
->pBuffers
[idx
].pvBuffer
)
735 s
->desc
->pBuffers
[idx
].cbBuffer
= 0;
736 s
->allow_buffer_resize
= TRUE
;
745 static void dump_buffer_desc(SecBufferDesc
*desc
)
750 TRACE("Buffer desc %p:\n", desc
);
751 for (i
= 0; i
< desc
->cBuffers
; ++i
)
753 SecBuffer
*b
= &desc
->pBuffers
[i
];
754 TRACE("\tbuffer %u: cbBuffer %d, BufferType %#x pvBuffer %p\n", i
, b
->cbBuffer
, b
->BufferType
, b
->pvBuffer
);
758 /***********************************************************************
759 * InitializeSecurityContextW
761 SECURITY_STATUS SEC_ENTRY
schan_InitializeSecurityContextW(
762 PCredHandle phCredential
, PCtxtHandle phContext
, SEC_WCHAR
*pszTargetName
,
763 ULONG fContextReq
, ULONG Reserved1
, ULONG TargetDataRep
,
764 PSecBufferDesc pInput
, ULONG Reserved2
, PCtxtHandle phNewContext
,
765 PSecBufferDesc pOutput
, ULONG
*pfContextAttr
, PTimeStamp ptsExpiry
)
767 struct schan_context
*ctx
;
768 struct schan_buffers
*out_buffers
;
769 struct schan_credentials
*cred
;
770 struct schan_transport transport
;
771 SIZE_T expected_size
= ~0UL;
774 TRACE("%p %p %s 0x%08x %d %d %p %d %p %p %p %p\n", phCredential
, phContext
,
775 debugstr_w(pszTargetName
), fContextReq
, Reserved1
, TargetDataRep
, pInput
,
776 Reserved1
, phNewContext
, pOutput
, pfContextAttr
, ptsExpiry
);
778 dump_buffer_desc(pInput
);
779 dump_buffer_desc(pOutput
);
785 if (!phCredential
) return SEC_E_INVALID_HANDLE
;
787 cred
= schan_get_object(phCredential
->dwLower
, SCHAN_HANDLE_CRED
);
788 if (!cred
) return SEC_E_INVALID_HANDLE
;
790 if (!(cred
->credential_use
& SECPKG_CRED_OUTBOUND
))
792 WARN("Invalid credential use %#x\n", cred
->credential_use
);
793 return SEC_E_INVALID_HANDLE
;
796 ctx
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ctx
));
797 if (!ctx
) return SEC_E_INSUFFICIENT_MEMORY
;
800 handle
= schan_alloc_handle(ctx
, SCHAN_HANDLE_CTX
);
801 if (handle
== SCHAN_INVALID_HANDLE
)
803 HeapFree(GetProcessHeap(), 0, ctx
);
804 return SEC_E_INTERNAL_ERROR
;
807 if (!schan_imp_create_session(&ctx
->session
, cred
))
809 schan_free_handle(handle
, SCHAN_HANDLE_CTX
);
810 HeapFree(GetProcessHeap(), 0, ctx
);
811 return SEC_E_INTERNAL_ERROR
;
816 UINT len
= WideCharToMultiByte( CP_UNIXCP
, 0, pszTargetName
, -1, NULL
, 0, NULL
, NULL
);
817 char *target
= HeapAlloc( GetProcessHeap(), 0, len
);
821 WideCharToMultiByte( CP_UNIXCP
, 0, pszTargetName
, -1, target
, len
, NULL
, NULL
);
822 schan_imp_set_session_target( ctx
->session
, target
);
823 HeapFree( GetProcessHeap(), 0, target
);
826 phNewContext
->dwLower
= handle
;
827 phNewContext
->dwUpper
= 0;
831 SIZE_T record_size
= 0;
837 return SEC_E_INCOMPLETE_MESSAGE
;
839 idx
= schan_find_sec_buffer_idx(pInput
, 0, SECBUFFER_TOKEN
);
841 return SEC_E_INCOMPLETE_MESSAGE
;
843 buffer
= &pInput
->pBuffers
[idx
];
844 ptr
= buffer
->pvBuffer
;
847 while (buffer
->cbBuffer
> expected_size
+ 5)
849 record_size
= 5 + ((ptr
[3] << 8) | ptr
[4]);
851 if (buffer
->cbBuffer
< expected_size
+ record_size
)
854 expected_size
+= record_size
;
860 TRACE("Expected at least %lu bytes, but buffer only contains %u bytes.\n",
861 max(6, record_size
), buffer
->cbBuffer
);
862 return SEC_E_INCOMPLETE_MESSAGE
;
865 TRACE("Using expected_size %lu.\n", expected_size
);
867 ctx
= schan_get_object(phContext
->dwLower
, SCHAN_HANDLE_CTX
);
870 ctx
->req_ctx_attr
= fContextReq
;
873 init_schan_buffers(&transport
.in
, pInput
, schan_init_sec_ctx_get_next_buffer
);
874 transport
.in
.limit
= expected_size
;
875 init_schan_buffers(&transport
.out
, pOutput
, schan_init_sec_ctx_get_next_buffer
);
876 schan_imp_set_session_transport(ctx
->session
, &transport
);
878 /* Perform the TLS handshake */
879 ret
= schan_imp_handshake(ctx
->session
);
881 if(transport
.in
.offset
&& transport
.in
.offset
!= pInput
->pBuffers
[0].cbBuffer
) {
882 if(pInput
->cBuffers
<2 || pInput
->pBuffers
[1].BufferType
!=SECBUFFER_EMPTY
)
883 return SEC_E_INVALID_TOKEN
;
885 pInput
->pBuffers
[1].BufferType
= SECBUFFER_EXTRA
;
886 pInput
->pBuffers
[1].cbBuffer
= pInput
->pBuffers
[0].cbBuffer
-transport
.in
.offset
;
889 out_buffers
= &transport
.out
;
890 if (out_buffers
->current_buffer_idx
!= -1)
892 SecBuffer
*buffer
= &out_buffers
->desc
->pBuffers
[out_buffers
->current_buffer_idx
];
893 buffer
->cbBuffer
= out_buffers
->offset
;
897 if (ctx
->req_ctx_attr
& ISC_REQ_ALLOCATE_MEMORY
)
898 *pfContextAttr
|= ISC_RET_ALLOCATED_MEMORY
;
903 /***********************************************************************
904 * InitializeSecurityContextA
906 SECURITY_STATUS SEC_ENTRY
schan_InitializeSecurityContextA(
907 PCredHandle phCredential
, PCtxtHandle phContext
, SEC_CHAR
*pszTargetName
,
908 ULONG fContextReq
, ULONG Reserved1
, ULONG TargetDataRep
,
909 PSecBufferDesc pInput
, ULONG Reserved2
, PCtxtHandle phNewContext
,
910 PSecBufferDesc pOutput
, ULONG
*pfContextAttr
, PTimeStamp ptsExpiry
)
913 SEC_WCHAR
*target_name
= NULL
;
915 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential
, phContext
,
916 debugstr_a(pszTargetName
), fContextReq
, Reserved1
, TargetDataRep
, pInput
,
917 Reserved1
, phNewContext
, pOutput
, pfContextAttr
, ptsExpiry
);
921 INT len
= MultiByteToWideChar(CP_ACP
, 0, pszTargetName
, -1, NULL
, 0);
922 target_name
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(*target_name
));
923 MultiByteToWideChar(CP_ACP
, 0, pszTargetName
, -1, target_name
, len
);
926 ret
= schan_InitializeSecurityContextW(phCredential
, phContext
, target_name
,
927 fContextReq
, Reserved1
, TargetDataRep
, pInput
, Reserved2
,
928 phNewContext
, pOutput
, pfContextAttr
, ptsExpiry
);
930 HeapFree(GetProcessHeap(), 0, target_name
);
935 SECURITY_STATUS SEC_ENTRY
schan_QueryContextAttributesW(
936 PCtxtHandle context_handle
, ULONG attribute
, PVOID buffer
)
938 struct schan_context
*ctx
;
940 TRACE("context_handle %p, attribute %#x, buffer %p\n",
941 context_handle
, attribute
, buffer
);
943 if (!context_handle
) return SEC_E_INVALID_HANDLE
;
944 ctx
= schan_get_object(context_handle
->dwLower
, SCHAN_HANDLE_CTX
);
948 case SECPKG_ATTR_STREAM_SIZES
:
950 SecPkgContext_ConnectionInfo info
;
951 SECURITY_STATUS status
= schan_imp_get_connection_info(ctx
->session
, &info
);
952 if (status
== SEC_E_OK
)
954 SecPkgContext_StreamSizes
*stream_sizes
= buffer
;
955 SIZE_T mac_size
= info
.dwHashStrength
;
956 unsigned int block_size
= schan_imp_get_session_cipher_block_size(ctx
->session
);
957 unsigned int message_size
= schan_imp_get_max_message_size(ctx
->session
);
959 TRACE("Using %lu mac bytes, message size %u, block size %u\n",
960 mac_size
, message_size
, block_size
);
962 /* These are defined by the TLS RFC */
963 stream_sizes
->cbHeader
= 5;
964 stream_sizes
->cbTrailer
= mac_size
+ 256; /* Max 255 bytes padding + 1 for padding size */
965 stream_sizes
->cbMaximumMessage
= message_size
;
966 stream_sizes
->cbBuffers
= 4;
967 stream_sizes
->cbBlockSize
= block_size
;
972 case SECPKG_ATTR_REMOTE_CERT_CONTEXT
:
974 PCCERT_CONTEXT
*cert
= buffer
;
977 HCERTSTORE cert_store
;
978 SECURITY_STATUS status
;
980 cert_store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0, CERT_STORE_CREATE_NEW_FLAG
, NULL
);
982 return GetLastError();
984 status
= schan_imp_get_session_peer_certificate(ctx
->session
, cert_store
, &ctx
->cert
);
985 CertCloseStore(cert_store
, 0);
986 if(status
!= SEC_E_OK
)
990 *cert
= CertDuplicateCertificateContext(ctx
->cert
);
993 case SECPKG_ATTR_CONNECTION_INFO
:
995 SecPkgContext_ConnectionInfo
*info
= buffer
;
996 return schan_imp_get_connection_info(ctx
->session
, info
);
1000 FIXME("Unhandled attribute %#x\n", attribute
);
1001 return SEC_E_UNSUPPORTED_FUNCTION
;
1005 SECURITY_STATUS SEC_ENTRY
schan_QueryContextAttributesA(
1006 PCtxtHandle context_handle
, ULONG attribute
, PVOID buffer
)
1008 TRACE("context_handle %p, attribute %#x, buffer %p\n",
1009 context_handle
, attribute
, buffer
);
1013 case SECPKG_ATTR_STREAM_SIZES
:
1014 return schan_QueryContextAttributesW(context_handle
, attribute
, buffer
);
1015 case SECPKG_ATTR_REMOTE_CERT_CONTEXT
:
1016 return schan_QueryContextAttributesW(context_handle
, attribute
, buffer
);
1017 case SECPKG_ATTR_CONNECTION_INFO
:
1018 return schan_QueryContextAttributesW(context_handle
, attribute
, buffer
);
1021 FIXME("Unhandled attribute %#x\n", attribute
);
1022 return SEC_E_UNSUPPORTED_FUNCTION
;
1026 static int schan_encrypt_message_get_next_buffer(const struct schan_transport
*t
, struct schan_buffers
*s
)
1030 if (s
->current_buffer_idx
== -1)
1031 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_STREAM_HEADER
);
1033 b
= &s
->desc
->pBuffers
[s
->current_buffer_idx
];
1035 if (b
->BufferType
== SECBUFFER_STREAM_HEADER
)
1036 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_DATA
);
1038 if (b
->BufferType
== SECBUFFER_DATA
)
1039 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_STREAM_TRAILER
);
1044 static int schan_encrypt_message_get_next_buffer_token(const struct schan_transport
*t
, struct schan_buffers
*s
)
1048 if (s
->current_buffer_idx
== -1)
1049 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_TOKEN
);
1051 b
= &s
->desc
->pBuffers
[s
->current_buffer_idx
];
1053 if (b
->BufferType
== SECBUFFER_TOKEN
)
1055 int idx
= schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_TOKEN
);
1056 if (idx
!= s
->current_buffer_idx
) return -1;
1057 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_DATA
);
1060 if (b
->BufferType
== SECBUFFER_DATA
)
1062 int idx
= schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_TOKEN
);
1064 idx
= schan_find_sec_buffer_idx(s
->desc
, idx
+ 1, SECBUFFER_TOKEN
);
1071 static SECURITY_STATUS SEC_ENTRY
schan_EncryptMessage(PCtxtHandle context_handle
,
1072 ULONG quality
, PSecBufferDesc message
, ULONG message_seq_no
)
1074 struct schan_transport transport
;
1075 struct schan_context
*ctx
;
1076 struct schan_buffers
*b
;
1077 SECURITY_STATUS status
;
1084 TRACE("context_handle %p, quality %d, message %p, message_seq_no %d\n",
1085 context_handle
, quality
, message
, message_seq_no
);
1087 if (!context_handle
) return SEC_E_INVALID_HANDLE
;
1088 ctx
= schan_get_object(context_handle
->dwLower
, SCHAN_HANDLE_CTX
);
1090 dump_buffer_desc(message
);
1092 idx
= schan_find_sec_buffer_idx(message
, 0, SECBUFFER_DATA
);
1095 WARN("No data buffer passed\n");
1096 return SEC_E_INTERNAL_ERROR
;
1098 buffer
= &message
->pBuffers
[idx
];
1100 data_size
= buffer
->cbBuffer
;
1101 data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
1102 memcpy(data
, buffer
->pvBuffer
, data_size
);
1104 transport
.ctx
= ctx
;
1105 init_schan_buffers(&transport
.in
, NULL
, NULL
);
1106 if (schan_find_sec_buffer_idx(message
, 0, SECBUFFER_STREAM_HEADER
) != -1)
1107 init_schan_buffers(&transport
.out
, message
, schan_encrypt_message_get_next_buffer
);
1109 init_schan_buffers(&transport
.out
, message
, schan_encrypt_message_get_next_buffer_token
);
1110 schan_imp_set_session_transport(ctx
->session
, &transport
);
1113 status
= schan_imp_send(ctx
->session
, data
, &length
);
1115 TRACE("Sent %ld bytes.\n", length
);
1117 if (length
!= data_size
)
1118 status
= SEC_E_INTERNAL_ERROR
;
1121 b
->desc
->pBuffers
[b
->current_buffer_idx
].cbBuffer
= b
->offset
;
1122 HeapFree(GetProcessHeap(), 0, data
);
1124 TRACE("Returning %#x.\n", status
);
1129 static int schan_decrypt_message_get_next_buffer(const struct schan_transport
*t
, struct schan_buffers
*s
)
1131 if (s
->current_buffer_idx
== -1)
1132 return schan_find_sec_buffer_idx(s
->desc
, 0, SECBUFFER_DATA
);
1137 static int schan_validate_decrypt_buffer_desc(PSecBufferDesc message
)
1140 unsigned int empty_count
= 0;
1143 if (message
->cBuffers
< 4)
1145 WARN("Less than four buffers passed\n");
1149 for (i
= 0; i
< message
->cBuffers
; ++i
)
1151 SecBuffer
*b
= &message
->pBuffers
[i
];
1152 if (b
->BufferType
== SECBUFFER_DATA
)
1156 WARN("More than one data buffer passed\n");
1161 else if (b
->BufferType
== SECBUFFER_EMPTY
)
1167 WARN("No data buffer passed\n");
1171 if (empty_count
< 3)
1173 WARN("Less than three empty buffers passed\n");
1180 static void schan_decrypt_fill_buffer(PSecBufferDesc message
, ULONG buffer_type
, void *data
, ULONG size
)
1185 idx
= schan_find_sec_buffer_idx(message
, 0, SECBUFFER_EMPTY
);
1186 buffer
= &message
->pBuffers
[idx
];
1188 buffer
->BufferType
= buffer_type
;
1189 buffer
->pvBuffer
= data
;
1190 buffer
->cbBuffer
= size
;
1193 static SECURITY_STATUS SEC_ENTRY
schan_DecryptMessage(PCtxtHandle context_handle
,
1194 PSecBufferDesc message
, ULONG message_seq_no
, PULONG quality
)
1196 struct schan_transport transport
;
1197 struct schan_context
*ctx
;
1201 unsigned expected_size
;
1202 SSIZE_T received
= 0;
1204 unsigned char *buf_ptr
;
1206 TRACE("context_handle %p, message %p, message_seq_no %d, quality %p\n",
1207 context_handle
, message
, message_seq_no
, quality
);
1209 if (!context_handle
) return SEC_E_INVALID_HANDLE
;
1210 ctx
= schan_get_object(context_handle
->dwLower
, SCHAN_HANDLE_CTX
);
1212 dump_buffer_desc(message
);
1214 idx
= schan_validate_decrypt_buffer_desc(message
);
1216 return SEC_E_INVALID_TOKEN
;
1217 buffer
= &message
->pBuffers
[idx
];
1218 buf_ptr
= buffer
->pvBuffer
;
1220 expected_size
= 5 + ((buf_ptr
[3] << 8) | buf_ptr
[4]);
1221 if(buffer
->cbBuffer
< expected_size
)
1223 TRACE("Expected %u bytes, but buffer only contains %u bytes\n", expected_size
, buffer
->cbBuffer
);
1224 buffer
->BufferType
= SECBUFFER_MISSING
;
1225 buffer
->cbBuffer
= expected_size
- buffer
->cbBuffer
;
1227 /* This is a bit weird, but windows does it too */
1228 idx
= schan_find_sec_buffer_idx(message
, 0, SECBUFFER_EMPTY
);
1229 buffer
= &message
->pBuffers
[idx
];
1230 buffer
->BufferType
= SECBUFFER_MISSING
;
1231 buffer
->cbBuffer
= expected_size
- buffer
->cbBuffer
;
1233 TRACE("Returning SEC_E_INCOMPLETE_MESSAGE\n");
1234 return SEC_E_INCOMPLETE_MESSAGE
;
1237 data_size
= expected_size
- 5;
1238 data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
1240 transport
.ctx
= ctx
;
1241 init_schan_buffers(&transport
.in
, message
, schan_decrypt_message_get_next_buffer
);
1242 transport
.in
.limit
= expected_size
;
1243 init_schan_buffers(&transport
.out
, NULL
, NULL
);
1244 schan_imp_set_session_transport(ctx
->session
, &transport
);
1246 while (received
< data_size
)
1248 SIZE_T length
= data_size
- received
;
1249 SECURITY_STATUS status
= schan_imp_recv(ctx
->session
, data
+ received
, &length
);
1251 if (status
== SEC_I_CONTINUE_NEEDED
)
1254 if (status
!= SEC_E_OK
)
1256 HeapFree(GetProcessHeap(), 0, data
);
1257 ERR("Returning %x\n", status
);
1267 TRACE("Received %ld bytes\n", received
);
1269 memcpy(buf_ptr
+ 5, data
, received
);
1270 HeapFree(GetProcessHeap(), 0, data
);
1272 schan_decrypt_fill_buffer(message
, SECBUFFER_DATA
,
1273 buf_ptr
+ 5, received
);
1275 schan_decrypt_fill_buffer(message
, SECBUFFER_STREAM_TRAILER
,
1276 buf_ptr
+ 5 + received
, buffer
->cbBuffer
- 5 - received
);
1278 if(buffer
->cbBuffer
> expected_size
)
1279 schan_decrypt_fill_buffer(message
, SECBUFFER_EXTRA
,
1280 buf_ptr
+ expected_size
, buffer
->cbBuffer
- expected_size
);
1282 buffer
->BufferType
= SECBUFFER_STREAM_HEADER
;
1283 buffer
->cbBuffer
= 5;
1288 SECURITY_STATUS SEC_ENTRY
schan_DeleteSecurityContext(PCtxtHandle context_handle
)
1290 struct schan_context
*ctx
;
1292 TRACE("context_handle %p\n", context_handle
);
1294 if (!context_handle
) return SEC_E_INVALID_HANDLE
;
1296 ctx
= schan_free_handle(context_handle
->dwLower
, SCHAN_HANDLE_CTX
);
1297 if (!ctx
) return SEC_E_INVALID_HANDLE
;
1300 CertFreeCertificateContext(ctx
->cert
);
1301 schan_imp_dispose_session(ctx
->session
);
1302 HeapFree(GetProcessHeap(), 0, ctx
);
1307 SecurityFunctionTableA schanTableA
= {
1309 schan_EnumerateSecurityPackagesA
,
1310 schan_QueryCredentialsAttributesA
,
1311 schan_AcquireCredentialsHandleA
,
1312 schan_FreeCredentialsHandle
,
1313 NULL
, /* Reserved2 */
1314 schan_InitializeSecurityContextA
,
1315 NULL
, /* AcceptSecurityContext */
1316 NULL
, /* CompleteAuthToken */
1317 schan_DeleteSecurityContext
,
1318 NULL
, /* ApplyControlToken */
1319 schan_QueryContextAttributesA
,
1320 NULL
, /* ImpersonateSecurityContext */
1321 NULL
, /* RevertSecurityContext */
1322 NULL
, /* MakeSignature */
1323 NULL
, /* VerifySignature */
1324 schan_FreeContextBuffer
,
1325 NULL
, /* QuerySecurityPackageInfoA */
1326 NULL
, /* Reserved3 */
1327 NULL
, /* Reserved4 */
1328 NULL
, /* ExportSecurityContext */
1329 NULL
, /* ImportSecurityContextA */
1330 NULL
, /* AddCredentialsA */
1331 NULL
, /* Reserved8 */
1332 NULL
, /* QuerySecurityContextToken */
1333 schan_EncryptMessage
,
1334 schan_DecryptMessage
,
1335 NULL
, /* SetContextAttributesA */
1338 SecurityFunctionTableW schanTableW
= {
1340 schan_EnumerateSecurityPackagesW
,
1341 schan_QueryCredentialsAttributesW
,
1342 schan_AcquireCredentialsHandleW
,
1343 schan_FreeCredentialsHandle
,
1344 NULL
, /* Reserved2 */
1345 schan_InitializeSecurityContextW
,
1346 NULL
, /* AcceptSecurityContext */
1347 NULL
, /* CompleteAuthToken */
1348 schan_DeleteSecurityContext
,
1349 NULL
, /* ApplyControlToken */
1350 schan_QueryContextAttributesW
,
1351 NULL
, /* ImpersonateSecurityContext */
1352 NULL
, /* RevertSecurityContext */
1353 NULL
, /* MakeSignature */
1354 NULL
, /* VerifySignature */
1355 schan_FreeContextBuffer
,
1356 NULL
, /* QuerySecurityPackageInfoW */
1357 NULL
, /* Reserved3 */
1358 NULL
, /* Reserved4 */
1359 NULL
, /* ExportSecurityContext */
1360 NULL
, /* ImportSecurityContextW */
1361 NULL
, /* AddCredentialsW */
1362 NULL
, /* Reserved8 */
1363 NULL
, /* QuerySecurityContextToken */
1364 schan_EncryptMessage
,
1365 schan_DecryptMessage
,
1366 NULL
, /* SetContextAttributesW */
1369 static const WCHAR schannelComment
[] = { 'S','c','h','a','n','n','e','l',' ',
1370 'S','e','c','u','r','i','t','y',' ','P','a','c','k','a','g','e',0 };
1371 static const WCHAR schannelDllName
[] = { 's','c','h','a','n','n','e','l','.','d','l','l',0 };
1373 void SECUR32_initSchannelSP(void)
1375 /* This is what Windows reports. This shouldn't break any applications
1376 * even though the functions are missing, because the wrapper will
1377 * return SEC_E_UNSUPPORTED_FUNCTION if our function is NULL.
1379 static const LONG caps
=
1380 SECPKG_FLAG_INTEGRITY
|
1381 SECPKG_FLAG_PRIVACY
|
1382 SECPKG_FLAG_CONNECTION
|
1383 SECPKG_FLAG_MULTI_REQUIRED
|
1384 SECPKG_FLAG_EXTENDED_ERROR
|
1385 SECPKG_FLAG_IMPERSONATION
|
1386 SECPKG_FLAG_ACCEPT_WIN32_NAME
|
1388 static const short version
= 1;
1389 static const LONG maxToken
= 16384;
1390 SEC_WCHAR
*uniSPName
= (SEC_WCHAR
*)UNISP_NAME_W
,
1391 *schannel
= (SEC_WCHAR
*)SCHANNEL_NAME_W
;
1392 const SecPkgInfoW info
[] = {
1393 { caps
, version
, UNISP_RPC_ID
, maxToken
, uniSPName
, uniSPName
},
1394 { caps
, version
, UNISP_RPC_ID
, maxToken
, schannel
,
1395 (SEC_WCHAR
*)schannelComment
},
1397 SecureProvider
*provider
;
1399 if (!schan_imp_init())
1402 schan_handle_table
= HeapAlloc(GetProcessHeap(), 0, 64 * sizeof(*schan_handle_table
));
1403 if (!schan_handle_table
)
1405 ERR("Failed to allocate schannel handle table.\n");
1408 schan_handle_table_size
= 64;
1410 provider
= SECUR32_addProvider(&schanTableA
, &schanTableW
, schannelDllName
);
1413 ERR("Failed to add schannel provider.\n");
1417 SECUR32_addPackages(provider
, sizeof(info
) / sizeof(info
[0]), NULL
, info
);
1422 HeapFree(GetProcessHeap(), 0, schan_handle_table
);
1423 schan_handle_table
= NULL
;
1428 void SECUR32_deinitSchannelSP(void)
1430 SIZE_T i
= schan_handle_count
;
1432 if (!schan_handle_table
) return;
1434 /* deinitialized sessions first because a pointer to the credentials
1435 * may be stored for the session. */
1438 if (schan_handle_table
[i
].type
== SCHAN_HANDLE_CTX
)
1440 struct schan_context
*ctx
= schan_free_handle(i
, SCHAN_HANDLE_CTX
);
1441 schan_imp_dispose_session(ctx
->session
);
1442 HeapFree(GetProcessHeap(), 0, ctx
);
1445 i
= schan_handle_count
;
1448 if (schan_handle_table
[i
].type
!= SCHAN_HANDLE_FREE
)
1450 struct schan_credentials
*cred
;
1451 cred
= schan_free_handle(i
, SCHAN_HANDLE_CRED
);
1452 schan_imp_free_certificate_credentials(cred
);
1453 HeapFree(GetProcessHeap(), 0, cred
);
1456 HeapFree(GetProcessHeap(), 0, schan_handle_table
);
1460 #else /* SONAME_LIBGNUTLS || HAVE_SECURITY_SECURITY_H */
1462 void SECUR32_initSchannelSP(void)
1464 ERR("TLS library not found, SSL connections will fail\n");
1467 void SECUR32_deinitSchannelSP(void) {}
1469 #endif /* SONAME_LIBGNUTLS || HAVE_SECURITY_SECURITY_H */