secur32: Handle incomplete schannel records on decryption.
[wine/hacks.git] / dlls / secur32 / schannel.c
blob0a0626c241f80bdd0d64cd987ea8826e119241ed
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.
19 * FIXME: It should be rather obvious that this file is empty of any
20 * implementation.
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <errno.h>
27 #include <limits.h>
28 #ifdef SONAME_LIBGNUTLS
29 #include <gnutls/gnutls.h>
30 #endif
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winnls.h"
35 #include "sspi.h"
36 #include "schannel.h"
37 #include "secur32_priv.h"
38 #include "wine/debug.h"
39 #include "wine/library.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
43 #ifdef SONAME_LIBGNUTLS
45 static void *libgnutls_handle;
46 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
47 MAKE_FUNCPTR(gnutls_alert_get);
48 MAKE_FUNCPTR(gnutls_alert_get_name);
49 MAKE_FUNCPTR(gnutls_certificate_allocate_credentials);
50 MAKE_FUNCPTR(gnutls_certificate_free_credentials);
51 MAKE_FUNCPTR(gnutls_certificate_get_peers);
52 MAKE_FUNCPTR(gnutls_cipher_get);
53 MAKE_FUNCPTR(gnutls_cipher_get_key_size);
54 MAKE_FUNCPTR(gnutls_credentials_set);
55 MAKE_FUNCPTR(gnutls_deinit);
56 MAKE_FUNCPTR(gnutls_global_deinit);
57 MAKE_FUNCPTR(gnutls_global_init);
58 MAKE_FUNCPTR(gnutls_global_set_log_function);
59 MAKE_FUNCPTR(gnutls_global_set_log_level);
60 MAKE_FUNCPTR(gnutls_handshake);
61 MAKE_FUNCPTR(gnutls_init);
62 MAKE_FUNCPTR(gnutls_kx_get);
63 MAKE_FUNCPTR(gnutls_mac_get);
64 MAKE_FUNCPTR(gnutls_mac_get_key_size);
65 MAKE_FUNCPTR(gnutls_perror);
66 MAKE_FUNCPTR(gnutls_protocol_get_version);
67 MAKE_FUNCPTR(gnutls_set_default_priority);
68 MAKE_FUNCPTR(gnutls_record_recv);
69 MAKE_FUNCPTR(gnutls_record_send);
70 MAKE_FUNCPTR(gnutls_transport_set_errno);
71 MAKE_FUNCPTR(gnutls_transport_set_ptr);
72 MAKE_FUNCPTR(gnutls_transport_set_pull_function);
73 MAKE_FUNCPTR(gnutls_transport_set_push_function);
74 #undef MAKE_FUNCPTR
76 #define SCHAN_INVALID_HANDLE ~0UL
78 enum schan_handle_type
80 SCHAN_HANDLE_CRED,
81 SCHAN_HANDLE_CTX,
82 SCHAN_HANDLE_FREE
85 struct schan_handle
87 void *object;
88 enum schan_handle_type type;
91 struct schan_credentials
93 ULONG credential_use;
94 gnutls_certificate_credentials credentials;
97 struct schan_context
99 gnutls_session_t session;
100 ULONG req_ctx_attr;
103 struct schan_transport;
105 struct schan_buffers
107 SIZE_T offset;
108 const SecBufferDesc *desc;
109 int current_buffer_idx;
110 BOOL allow_buffer_resize;
111 int (*get_next_buffer)(const struct schan_transport *, struct schan_buffers *);
114 struct schan_transport
116 struct schan_context *ctx;
117 struct schan_buffers in;
118 struct schan_buffers out;
119 BOOL no_incomplete_pull;
122 static struct schan_handle *schan_handle_table;
123 static struct schan_handle *schan_free_handles;
124 static SIZE_T schan_handle_table_size;
125 static SIZE_T schan_handle_count;
127 static ULONG_PTR schan_alloc_handle(void *object, enum schan_handle_type type)
129 struct schan_handle *handle;
131 if (schan_free_handles)
133 DWORD index = schan_free_handles - schan_handle_table;
134 /* Use a free handle */
135 handle = schan_free_handles;
136 if (handle->type != SCHAN_HANDLE_FREE)
138 ERR("Handle %d(%p) is in the free list, but has type %#x.\n", index, handle, handle->type);
139 return SCHAN_INVALID_HANDLE;
141 schan_free_handles = handle->object;
142 handle->object = object;
143 handle->type = type;
145 return index;
147 if (!(schan_handle_count < schan_handle_table_size))
149 /* Grow the table */
150 SIZE_T new_size = schan_handle_table_size + (schan_handle_table_size >> 1);
151 struct schan_handle *new_table = HeapReAlloc(GetProcessHeap(), 0, schan_handle_table, new_size * sizeof(*schan_handle_table));
152 if (!new_table)
154 ERR("Failed to grow the handle table\n");
155 return SCHAN_INVALID_HANDLE;
157 schan_handle_table = new_table;
158 schan_handle_table_size = new_size;
161 handle = &schan_handle_table[schan_handle_count++];
162 handle->object = object;
163 handle->type = type;
165 return handle - schan_handle_table;
168 static void *schan_free_handle(ULONG_PTR handle_idx, enum schan_handle_type type)
170 struct schan_handle *handle;
171 void *object;
173 if (handle_idx == SCHAN_INVALID_HANDLE) return NULL;
174 if (handle_idx >= schan_handle_count) return NULL;
175 handle = &schan_handle_table[handle_idx];
176 if (handle->type != type)
178 ERR("Handle %ld(%p) is not of type %#x\n", handle_idx, handle, type);
179 return NULL;
182 object = handle->object;
183 handle->object = schan_free_handles;
184 handle->type = SCHAN_HANDLE_FREE;
185 schan_free_handles = handle;
187 return object;
190 static void *schan_get_object(ULONG_PTR handle_idx, enum schan_handle_type type)
192 struct schan_handle *handle;
194 if (handle_idx == SCHAN_INVALID_HANDLE) return NULL;
195 if (handle_idx >= schan_handle_count) return NULL;
196 handle = &schan_handle_table[handle_idx];
197 if (handle->type != type)
199 ERR("Handle %ld(%p) is not of type %#x\n", handle_idx, handle, type);
200 return NULL;
203 return handle->object;
206 static SECURITY_STATUS schan_QueryCredentialsAttributes(
207 PCredHandle phCredential, ULONG ulAttribute, VOID *pBuffer)
209 SECURITY_STATUS ret;
211 switch (ulAttribute)
213 case SECPKG_ATTR_SUPPORTED_ALGS:
214 if (pBuffer)
216 /* FIXME: get from CryptoAPI */
217 FIXME("SECPKG_ATTR_SUPPORTED_ALGS: stub\n");
218 ret = SEC_E_UNSUPPORTED_FUNCTION;
220 else
221 ret = SEC_E_INTERNAL_ERROR;
222 break;
223 case SECPKG_ATTR_CIPHER_STRENGTHS:
224 if (pBuffer)
226 SecPkgCred_CipherStrengths *r = pBuffer;
228 /* FIXME: get from CryptoAPI */
229 FIXME("SECPKG_ATTR_CIPHER_STRENGTHS: semi-stub\n");
230 r->dwMinimumCipherStrength = 40;
231 r->dwMaximumCipherStrength = 168;
232 ret = SEC_E_OK;
234 else
235 ret = SEC_E_INTERNAL_ERROR;
236 break;
237 case SECPKG_ATTR_SUPPORTED_PROTOCOLS:
238 if (pBuffer)
240 /* FIXME: get from OpenSSL? */
241 FIXME("SECPKG_ATTR_SUPPORTED_PROTOCOLS: stub\n");
242 ret = SEC_E_UNSUPPORTED_FUNCTION;
244 else
245 ret = SEC_E_INTERNAL_ERROR;
246 break;
247 default:
248 ret = SEC_E_UNSUPPORTED_FUNCTION;
250 return ret;
253 static SECURITY_STATUS SEC_ENTRY schan_QueryCredentialsAttributesA(
254 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
256 SECURITY_STATUS ret;
258 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
260 switch (ulAttribute)
262 case SECPKG_CRED_ATTR_NAMES:
263 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
264 ret = SEC_E_UNSUPPORTED_FUNCTION;
265 break;
266 default:
267 ret = schan_QueryCredentialsAttributes(phCredential, ulAttribute,
268 pBuffer);
270 return ret;
273 static SECURITY_STATUS SEC_ENTRY schan_QueryCredentialsAttributesW(
274 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
276 SECURITY_STATUS ret;
278 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
280 switch (ulAttribute)
282 case SECPKG_CRED_ATTR_NAMES:
283 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
284 ret = SEC_E_UNSUPPORTED_FUNCTION;
285 break;
286 default:
287 ret = schan_QueryCredentialsAttributes(phCredential, ulAttribute,
288 pBuffer);
290 return ret;
293 static SECURITY_STATUS schan_CheckCreds(const SCHANNEL_CRED *schanCred)
295 SECURITY_STATUS st;
296 DWORD i;
298 TRACE("dwVersion = %d\n", schanCred->dwVersion);
299 TRACE("cCreds = %d\n", schanCred->cCreds);
300 TRACE("hRootStore = %p\n", schanCred->hRootStore);
301 TRACE("cMappers = %d\n", schanCred->cMappers);
302 TRACE("cSupportedAlgs = %d:\n", schanCred->cSupportedAlgs);
303 for (i = 0; i < schanCred->cSupportedAlgs; i++)
304 TRACE("%08x\n", schanCred->palgSupportedAlgs[i]);
305 TRACE("grbitEnabledProtocols = %08x\n", schanCred->grbitEnabledProtocols);
306 TRACE("dwMinimumCipherStrength = %d\n", schanCred->dwMinimumCipherStrength);
307 TRACE("dwMaximumCipherStrength = %d\n", schanCred->dwMaximumCipherStrength);
308 TRACE("dwSessionLifespan = %d\n", schanCred->dwSessionLifespan);
309 TRACE("dwFlags = %08x\n", schanCred->dwFlags);
310 TRACE("dwCredFormat = %d\n", schanCred->dwCredFormat);
312 switch (schanCred->dwVersion)
314 case SCH_CRED_V3:
315 case SCHANNEL_CRED_VERSION:
316 break;
317 default:
318 return SEC_E_INTERNAL_ERROR;
321 if (schanCred->cCreds == 0)
322 st = SEC_E_NO_CREDENTIALS;
323 else if (schanCred->cCreds > 1)
324 st = SEC_E_UNKNOWN_CREDENTIALS;
325 else
327 DWORD keySpec;
328 HCRYPTPROV csp;
329 BOOL ret, freeCSP;
331 ret = CryptAcquireCertificatePrivateKey(schanCred->paCred[0],
332 0, /* FIXME: what flags to use? */ NULL,
333 &csp, &keySpec, &freeCSP);
334 if (ret)
336 st = SEC_E_OK;
337 if (freeCSP)
338 CryptReleaseContext(csp, 0);
340 else
341 st = SEC_E_UNKNOWN_CREDENTIALS;
343 return st;
346 static SECURITY_STATUS schan_AcquireClientCredentials(const SCHANNEL_CRED *schanCred,
347 PCredHandle phCredential, PTimeStamp ptsExpiry)
349 struct schan_credentials *creds;
350 SECURITY_STATUS st = SEC_E_OK;
352 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred, phCredential, ptsExpiry);
354 if (schanCred)
356 st = schan_CheckCreds(schanCred);
357 if (st == SEC_E_NO_CREDENTIALS)
358 st = SEC_E_OK;
361 /* For now, the only thing I'm interested in is the direction of the
362 * connection, so just store it.
364 if (st == SEC_E_OK)
366 ULONG_PTR handle;
367 int ret;
369 creds = HeapAlloc(GetProcessHeap(), 0, sizeof(*creds));
370 if (!creds) return SEC_E_INSUFFICIENT_MEMORY;
372 handle = schan_alloc_handle(creds, SCHAN_HANDLE_CRED);
373 if (handle == SCHAN_INVALID_HANDLE) goto fail;
375 creds->credential_use = SECPKG_CRED_OUTBOUND;
376 ret = pgnutls_certificate_allocate_credentials(&creds->credentials);
377 if (ret != GNUTLS_E_SUCCESS)
379 pgnutls_perror(ret);
380 schan_free_handle(handle, SCHAN_HANDLE_CRED);
381 goto fail;
384 phCredential->dwLower = handle;
385 phCredential->dwUpper = 0;
387 /* Outbound credentials have no expiry */
388 if (ptsExpiry)
390 ptsExpiry->LowPart = 0;
391 ptsExpiry->HighPart = 0;
394 return st;
396 fail:
397 HeapFree(GetProcessHeap(), 0, creds);
398 return SEC_E_INTERNAL_ERROR;
401 static SECURITY_STATUS schan_AcquireServerCredentials(const SCHANNEL_CRED *schanCred,
402 PCredHandle phCredential, PTimeStamp ptsExpiry)
404 SECURITY_STATUS st;
406 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred, phCredential, ptsExpiry);
408 if (!schanCred) return SEC_E_NO_CREDENTIALS;
410 st = schan_CheckCreds(schanCred);
411 if (st == SEC_E_OK)
413 ULONG_PTR handle;
414 struct schan_credentials *creds;
416 creds = HeapAlloc(GetProcessHeap(), 0, sizeof(*creds));
417 if (!creds) return SEC_E_INSUFFICIENT_MEMORY;
418 creds->credential_use = SECPKG_CRED_INBOUND;
420 handle = schan_alloc_handle(creds, SCHAN_HANDLE_CRED);
421 if (handle == SCHAN_INVALID_HANDLE)
423 HeapFree(GetProcessHeap(), 0, creds);
424 return SEC_E_INTERNAL_ERROR;
427 phCredential->dwLower = handle;
428 phCredential->dwUpper = 0;
430 /* FIXME: get expiry from cert */
432 return st;
435 static SECURITY_STATUS schan_AcquireCredentialsHandle(ULONG fCredentialUse,
436 const SCHANNEL_CRED *schanCred, PCredHandle phCredential, PTimeStamp ptsExpiry)
438 SECURITY_STATUS ret;
440 if (fCredentialUse == SECPKG_CRED_OUTBOUND)
441 ret = schan_AcquireClientCredentials(schanCred, phCredential,
442 ptsExpiry);
443 else
444 ret = schan_AcquireServerCredentials(schanCred, phCredential,
445 ptsExpiry);
446 return ret;
449 static SECURITY_STATUS SEC_ENTRY schan_AcquireCredentialsHandleA(
450 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
451 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
452 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
454 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
455 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
456 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
457 return schan_AcquireCredentialsHandle(fCredentialUse,
458 pAuthData, phCredential, ptsExpiry);
461 static SECURITY_STATUS SEC_ENTRY schan_AcquireCredentialsHandleW(
462 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
463 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
464 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
466 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
467 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
468 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
469 return schan_AcquireCredentialsHandle(fCredentialUse,
470 pAuthData, phCredential, ptsExpiry);
473 static SECURITY_STATUS SEC_ENTRY schan_FreeCredentialsHandle(
474 PCredHandle phCredential)
476 struct schan_credentials *creds;
478 TRACE("phCredential %p\n", phCredential);
480 if (!phCredential) return SEC_E_INVALID_HANDLE;
482 creds = schan_free_handle(phCredential->dwLower, SCHAN_HANDLE_CRED);
483 if (!creds) return SEC_E_INVALID_HANDLE;
485 if (creds->credential_use == SECPKG_CRED_OUTBOUND)
486 pgnutls_certificate_free_credentials(creds->credentials);
487 HeapFree(GetProcessHeap(), 0, creds);
489 return SEC_E_OK;
492 static void init_schan_buffers(struct schan_buffers *s, const PSecBufferDesc desc,
493 int (*get_next_buffer)(const struct schan_transport *, struct schan_buffers *))
495 s->offset = 0;
496 s->desc = desc;
497 s->current_buffer_idx = -1;
498 s->allow_buffer_resize = FALSE;
499 s->get_next_buffer = get_next_buffer;
502 static int schan_find_sec_buffer_idx(const SecBufferDesc *desc, unsigned int start_idx, ULONG buffer_type)
504 unsigned int i;
505 PSecBuffer buffer;
507 for (i = start_idx; i < desc->cBuffers; ++i)
509 buffer = &desc->pBuffers[i];
510 if (buffer->BufferType == buffer_type) return i;
513 return -1;
516 static void schan_resize_current_buffer(const struct schan_buffers *s, SIZE_T min_size)
518 SecBuffer *b = &s->desc->pBuffers[s->current_buffer_idx];
519 SIZE_T new_size = b->cbBuffer ? b->cbBuffer * 2 : 128;
520 void *new_data;
522 if (b->cbBuffer >= min_size || !s->allow_buffer_resize || min_size > UINT_MAX / 2) return;
524 while (new_size < min_size) new_size *= 2;
526 if (b->pvBuffer)
527 new_data = HeapReAlloc(GetProcessHeap(), 0, b->pvBuffer, new_size);
528 else
529 new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
531 if (!new_data)
533 TRACE("Failed to resize %p from %d to %ld\n", b->pvBuffer, b->cbBuffer, new_size);
534 return;
537 b->cbBuffer = new_size;
538 b->pvBuffer = new_data;
541 static char *schan_get_buffer(const struct schan_transport *t, struct schan_buffers *s, size_t *count)
543 SIZE_T max_count;
544 PSecBuffer buffer;
546 if (!s->desc)
548 TRACE("No desc\n");
549 return NULL;
552 if (s->current_buffer_idx == -1)
554 /* Initial buffer */
555 int buffer_idx = s->get_next_buffer(t, s);
556 if (buffer_idx == -1)
558 TRACE("No next buffer\n");
559 return NULL;
561 s->current_buffer_idx = buffer_idx;
564 buffer = &s->desc->pBuffers[s->current_buffer_idx];
565 TRACE("Using buffer %d: cbBuffer %d, BufferType %#x, pvBuffer %p\n", s->current_buffer_idx, buffer->cbBuffer, buffer->BufferType, buffer->pvBuffer);
567 schan_resize_current_buffer(s, s->offset + *count);
568 max_count = buffer->cbBuffer - s->offset;
569 if (!max_count)
571 int buffer_idx;
573 s->allow_buffer_resize = FALSE;
574 buffer_idx = s->get_next_buffer(t, s);
575 if (buffer_idx == -1)
577 TRACE("No next buffer\n");
578 return NULL;
580 s->current_buffer_idx = buffer_idx;
581 s->offset = 0;
582 return schan_get_buffer(t, s, count);
585 if (*count > max_count) *count = max_count;
586 return (char *)buffer->pvBuffer + s->offset;
589 static ssize_t schan_pull(gnutls_transport_ptr_t transport, void *buff, size_t buff_len)
591 struct schan_transport *t = transport;
592 char *b;
593 size_t req_len = buff_len;
595 TRACE("Pull %zu bytes\n", buff_len);
597 b = schan_get_buffer(t, &t->in, &buff_len);
598 if (!b || (t->no_incomplete_pull && req_len != buff_len))
600 pgnutls_transport_set_errno(t->ctx->session, EAGAIN);
601 return -1;
604 memcpy(buff, b, buff_len);
605 t->in.offset += buff_len;
607 TRACE("Read %zu bytes\n", buff_len);
609 return buff_len;
612 static ssize_t schan_push(gnutls_transport_ptr_t transport, const void *buff, size_t buff_len)
614 struct schan_transport *t = transport;
615 char *b;
617 TRACE("Push %zu bytes\n", buff_len);
619 b = schan_get_buffer(t, &t->out, &buff_len);
620 if (!b)
622 pgnutls_transport_set_errno(t->ctx->session, EAGAIN);
623 return -1;
626 memcpy(b, buff, buff_len);
627 t->out.offset += buff_len;
629 TRACE("Wrote %zu bytes\n", buff_len);
631 return buff_len;
634 static int schan_init_sec_ctx_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
636 if (s->current_buffer_idx == -1)
638 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
639 if (t->ctx->req_ctx_attr & ISC_REQ_ALLOCATE_MEMORY)
641 if (idx == -1)
643 idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_EMPTY);
644 if (idx != -1) s->desc->pBuffers[idx].BufferType = SECBUFFER_TOKEN;
646 if (idx != -1 && !s->desc->pBuffers[idx].pvBuffer)
648 s->desc->pBuffers[idx].cbBuffer = 0;
649 s->allow_buffer_resize = TRUE;
652 return idx;
655 return -1;
658 static void dump_buffer_desc(SecBufferDesc *desc)
660 unsigned int i;
662 if (!desc) return;
663 TRACE("Buffer desc %p:\n", desc);
664 for (i = 0; i < desc->cBuffers; ++i)
666 SecBuffer *b = &desc->pBuffers[i];
667 TRACE("\tbuffer %u: cbBuffer %d, BufferType %#x pvBuffer %p\n", i, b->cbBuffer, b->BufferType, b->pvBuffer);
671 /***********************************************************************
672 * InitializeSecurityContextW
674 static SECURITY_STATUS SEC_ENTRY schan_InitializeSecurityContextW(
675 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
676 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
677 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
678 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
680 struct schan_context *ctx;
681 struct schan_buffers *out_buffers;
682 struct schan_credentials *cred;
683 struct schan_transport transport;
684 int err;
686 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
687 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
688 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
690 dump_buffer_desc(pInput);
691 dump_buffer_desc(pOutput);
693 if (!phContext)
695 ULONG_PTR handle;
697 if (!phCredential) return SEC_E_INVALID_HANDLE;
699 cred = schan_get_object(phCredential->dwLower, SCHAN_HANDLE_CRED);
700 if (!cred) return SEC_E_INVALID_HANDLE;
702 if (!(cred->credential_use & SECPKG_CRED_OUTBOUND))
704 WARN("Invalid credential use %#x\n", cred->credential_use);
705 return SEC_E_INVALID_HANDLE;
708 ctx = HeapAlloc(GetProcessHeap(), 0, sizeof(*ctx));
709 if (!ctx) return SEC_E_INSUFFICIENT_MEMORY;
711 handle = schan_alloc_handle(ctx, SCHAN_HANDLE_CTX);
712 if (handle == SCHAN_INVALID_HANDLE)
714 HeapFree(GetProcessHeap(), 0, ctx);
715 return SEC_E_INTERNAL_ERROR;
718 err = pgnutls_init(&ctx->session, GNUTLS_CLIENT);
719 if (err != GNUTLS_E_SUCCESS)
721 pgnutls_perror(err);
722 schan_free_handle(handle, SCHAN_HANDLE_CTX);
723 HeapFree(GetProcessHeap(), 0, ctx);
724 return SEC_E_INTERNAL_ERROR;
727 /* FIXME: We should be using the information from the credentials here. */
728 FIXME("Using hardcoded \"NORMAL\" priority\n");
729 err = pgnutls_set_default_priority(ctx->session);
730 if (err != GNUTLS_E_SUCCESS)
732 pgnutls_perror(err);
733 pgnutls_deinit(ctx->session);
734 schan_free_handle(handle, SCHAN_HANDLE_CTX);
735 HeapFree(GetProcessHeap(), 0, ctx);
738 err = pgnutls_credentials_set(ctx->session, GNUTLS_CRD_CERTIFICATE, cred->credentials);
739 if (err != GNUTLS_E_SUCCESS)
741 pgnutls_perror(err);
742 pgnutls_deinit(ctx->session);
743 schan_free_handle(handle, SCHAN_HANDLE_CTX);
744 HeapFree(GetProcessHeap(), 0, ctx);
747 pgnutls_transport_set_pull_function(ctx->session, schan_pull);
748 pgnutls_transport_set_push_function(ctx->session, schan_push);
750 phNewContext->dwLower = handle;
751 phNewContext->dwUpper = 0;
753 else
755 ctx = schan_get_object(phContext->dwLower, SCHAN_HANDLE_CTX);
758 ctx->req_ctx_attr = fContextReq;
760 transport.ctx = ctx;
761 transport.no_incomplete_pull = FALSE;
762 init_schan_buffers(&transport.in, pInput, schan_init_sec_ctx_get_next_buffer);
763 init_schan_buffers(&transport.out, pOutput, schan_init_sec_ctx_get_next_buffer);
764 pgnutls_transport_set_ptr(ctx->session, &transport);
766 /* Perform the TLS handshake */
767 err = pgnutls_handshake(ctx->session);
769 out_buffers = &transport.out;
770 if (out_buffers->current_buffer_idx != -1)
772 SecBuffer *buffer = &out_buffers->desc->pBuffers[out_buffers->current_buffer_idx];
773 buffer->cbBuffer = out_buffers->offset;
776 *pfContextAttr = 0;
777 if (ctx->req_ctx_attr & ISC_REQ_ALLOCATE_MEMORY)
778 *pfContextAttr |= ISC_RET_ALLOCATED_MEMORY;
780 switch(err)
782 case GNUTLS_E_SUCCESS:
783 TRACE("Handshake completed\n");
784 return SEC_E_OK;
786 case GNUTLS_E_AGAIN:
787 TRACE("Continue...\n");
788 return SEC_I_CONTINUE_NEEDED;
790 case GNUTLS_E_WARNING_ALERT_RECEIVED:
791 case GNUTLS_E_FATAL_ALERT_RECEIVED:
793 gnutls_alert_description_t alert = pgnutls_alert_get(ctx->session);
794 const char *alert_name = pgnutls_alert_get_name(alert);
795 WARN("ALERT: %d %s\n", alert, alert_name);
796 return SEC_E_INTERNAL_ERROR;
799 default:
800 pgnutls_perror(err);
801 return SEC_E_INTERNAL_ERROR;
805 /***********************************************************************
806 * InitializeSecurityContextA
808 static SECURITY_STATUS SEC_ENTRY schan_InitializeSecurityContextA(
809 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
810 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
811 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
812 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
814 SECURITY_STATUS ret;
815 SEC_WCHAR *target_name = NULL;
817 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
818 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
819 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
821 if (pszTargetName)
823 INT len = MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, NULL, 0);
824 target_name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*target_name));
825 MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, target_name, len);
828 ret = schan_InitializeSecurityContextW(phCredential, phContext, target_name,
829 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
830 phNewContext, pOutput, pfContextAttr, ptsExpiry);
832 HeapFree(GetProcessHeap(), 0, target_name);
834 return ret;
837 static unsigned int schannel_get_cipher_block_size(gnutls_cipher_algorithm_t cipher)
839 const struct
841 gnutls_cipher_algorithm_t cipher;
842 unsigned int block_size;
844 algorithms[] =
846 {GNUTLS_CIPHER_3DES_CBC, 8},
847 {GNUTLS_CIPHER_AES_128_CBC, 16},
848 {GNUTLS_CIPHER_AES_256_CBC, 16},
849 {GNUTLS_CIPHER_ARCFOUR_128, 1},
850 {GNUTLS_CIPHER_ARCFOUR_40, 1},
851 {GNUTLS_CIPHER_DES_CBC, 8},
852 {GNUTLS_CIPHER_NULL, 1},
853 {GNUTLS_CIPHER_RC2_40_CBC, 8},
855 unsigned int i;
857 for (i = 0; i < sizeof(algorithms) / sizeof(*algorithms); ++i)
859 if (algorithms[i].cipher == cipher)
860 return algorithms[i].block_size;
863 FIXME("Unknown cipher %#x, returning 1\n", cipher);
865 return 1;
868 static DWORD schannel_get_protocol(gnutls_protocol_t proto)
870 /* FIXME: currently schannel only implements client connections, but
871 * there's no reason it couldn't be used for servers as well. The
872 * context doesn't tell us which it is, so assume client for now.
874 switch (proto)
876 case GNUTLS_SSL3: return SP_PROT_SSL3_CLIENT;
877 case GNUTLS_TLS1_0: return SP_PROT_TLS1_CLIENT;
878 default:
879 FIXME("unknown protocol %d\n", proto);
880 return 0;
884 static ALG_ID schannel_get_cipher_algid(gnutls_cipher_algorithm_t cipher)
886 switch (cipher)
888 case GNUTLS_CIPHER_UNKNOWN:
889 case GNUTLS_CIPHER_NULL: return 0;
890 case GNUTLS_CIPHER_ARCFOUR_40:
891 case GNUTLS_CIPHER_ARCFOUR_128: return CALG_RC4;
892 case GNUTLS_CIPHER_DES_CBC:
893 case GNUTLS_CIPHER_3DES_CBC: return CALG_DES;
894 case GNUTLS_CIPHER_AES_128_CBC:
895 case GNUTLS_CIPHER_AES_256_CBC: return CALG_AES;
896 case GNUTLS_CIPHER_RC2_40_CBC: return CALG_RC2;
897 default:
898 FIXME("unknown algorithm %d\n", cipher);
899 return 0;
903 static ALG_ID schannel_get_mac_algid(gnutls_mac_algorithm_t mac)
905 switch (mac)
907 case GNUTLS_MAC_UNKNOWN:
908 case GNUTLS_MAC_NULL: return 0;
909 case GNUTLS_MAC_MD5: return CALG_MD5;
910 case GNUTLS_MAC_SHA1:
911 case GNUTLS_MAC_SHA256:
912 case GNUTLS_MAC_SHA384:
913 case GNUTLS_MAC_SHA512: return CALG_SHA;
914 default:
915 FIXME("unknown algorithm %d\n", mac);
916 return 0;
920 static ALG_ID schannel_get_kx_algid(gnutls_kx_algorithm_t kx)
922 switch (kx)
924 case GNUTLS_KX_RSA: return CALG_RSA_KEYX;
925 case GNUTLS_KX_DHE_DSS:
926 case GNUTLS_KX_DHE_RSA: return CALG_DH_EPHEM;
927 default:
928 FIXME("unknown algorithm %d\n", kx);
929 return 0;
933 static SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesW(
934 PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
936 struct schan_context *ctx;
938 TRACE("context_handle %p, attribute %#x, buffer %p\n",
939 context_handle, attribute, buffer);
941 if (!context_handle) return SEC_E_INVALID_HANDLE;
942 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
944 switch(attribute)
946 case SECPKG_ATTR_STREAM_SIZES:
948 SecPkgContext_StreamSizes *stream_sizes = buffer;
949 gnutls_mac_algorithm_t mac = pgnutls_mac_get(ctx->session);
950 size_t mac_size = pgnutls_mac_get_key_size(mac);
951 gnutls_cipher_algorithm_t cipher = pgnutls_cipher_get(ctx->session);
952 unsigned int block_size = schannel_get_cipher_block_size(cipher);
954 TRACE("Using %zu mac bytes, block size %u\n", mac_size, block_size);
956 /* These are defined by the TLS RFC */
957 stream_sizes->cbHeader = 5;
958 stream_sizes->cbTrailer = mac_size + 256; /* Max 255 bytes padding + 1 for padding size */
959 stream_sizes->cbMaximumMessage = 1 << 14;
960 stream_sizes->cbBuffers = 4;
961 stream_sizes->cbBlockSize = block_size;
962 return SEC_E_OK;
964 case SECPKG_ATTR_REMOTE_CERT_CONTEXT:
966 unsigned int list_size;
967 const gnutls_datum_t *datum;
969 datum = pgnutls_certificate_get_peers(ctx->session, &list_size);
970 if (datum)
972 PCCERT_CONTEXT *cert = buffer;
974 *cert = CertCreateCertificateContext(X509_ASN_ENCODING,
975 datum->data, datum->size);
976 if (!*cert)
977 return GetLastError();
978 else
979 return SEC_E_OK;
981 else
982 return SEC_E_INTERNAL_ERROR;
984 case SECPKG_ATTR_CONNECTION_INFO:
986 SecPkgContext_ConnectionInfo *info = buffer;
987 gnutls_protocol_t proto = pgnutls_protocol_get_version(ctx->session);
988 gnutls_cipher_algorithm_t alg = pgnutls_cipher_get(ctx->session);
989 gnutls_mac_algorithm_t mac = pgnutls_mac_get(ctx->session);
990 gnutls_kx_algorithm_t kx = pgnutls_kx_get(ctx->session);
992 info->dwProtocol = schannel_get_protocol(proto);
993 info->aiCipher = schannel_get_cipher_algid(alg);
994 info->dwCipherStrength = pgnutls_cipher_get_key_size(alg);
995 info->aiHash = schannel_get_mac_algid(mac);
996 info->dwHashStrength = pgnutls_mac_get_key_size(mac);
997 info->aiExch = schannel_get_kx_algid(kx);
998 /* FIXME: info->dwExchStrength? */
999 info->dwExchStrength = 0;
1000 return SEC_E_OK;
1003 default:
1004 FIXME("Unhandled attribute %#x\n", attribute);
1005 return SEC_E_UNSUPPORTED_FUNCTION;
1009 static SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesA(
1010 PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
1012 TRACE("context_handle %p, attribute %#x, buffer %p\n",
1013 context_handle, attribute, buffer);
1015 switch(attribute)
1017 case SECPKG_ATTR_STREAM_SIZES:
1018 return schan_QueryContextAttributesW(context_handle, attribute, buffer);
1019 case SECPKG_ATTR_REMOTE_CERT_CONTEXT:
1020 return schan_QueryContextAttributesW(context_handle, attribute, buffer);
1021 case SECPKG_ATTR_CONNECTION_INFO:
1022 return schan_QueryContextAttributesW(context_handle, attribute, buffer);
1024 default:
1025 FIXME("Unhandled attribute %#x\n", attribute);
1026 return SEC_E_UNSUPPORTED_FUNCTION;
1030 static int schan_encrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
1032 SecBuffer *b;
1034 if (s->current_buffer_idx == -1)
1035 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_HEADER);
1037 b = &s->desc->pBuffers[s->current_buffer_idx];
1039 if (b->BufferType == SECBUFFER_STREAM_HEADER)
1040 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
1042 if (b->BufferType == SECBUFFER_DATA)
1043 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_TRAILER);
1045 return -1;
1048 static int schan_encrypt_message_get_next_buffer_token(const struct schan_transport *t, struct schan_buffers *s)
1050 SecBuffer *b;
1052 if (s->current_buffer_idx == -1)
1053 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
1055 b = &s->desc->pBuffers[s->current_buffer_idx];
1057 if (b->BufferType == SECBUFFER_TOKEN)
1059 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
1060 if (idx != s->current_buffer_idx) return -1;
1061 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
1064 if (b->BufferType == SECBUFFER_DATA)
1066 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
1067 if (idx != -1)
1068 idx = schan_find_sec_buffer_idx(s->desc, idx + 1, SECBUFFER_TOKEN);
1069 return idx;
1072 return -1;
1075 static SECURITY_STATUS SEC_ENTRY schan_EncryptMessage(PCtxtHandle context_handle,
1076 ULONG quality, PSecBufferDesc message, ULONG message_seq_no)
1078 struct schan_transport transport;
1079 struct schan_context *ctx;
1080 struct schan_buffers *b;
1081 SecBuffer *buffer;
1082 SIZE_T data_size;
1083 char *data;
1084 ssize_t sent = 0;
1085 ssize_t ret;
1086 int idx;
1088 TRACE("context_handle %p, quality %d, message %p, message_seq_no %d\n",
1089 context_handle, quality, message, message_seq_no);
1091 if (!context_handle) return SEC_E_INVALID_HANDLE;
1092 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
1094 dump_buffer_desc(message);
1096 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_DATA);
1097 if (idx == -1)
1099 WARN("No data buffer passed\n");
1100 return SEC_E_INTERNAL_ERROR;
1102 buffer = &message->pBuffers[idx];
1104 data_size = buffer->cbBuffer;
1105 data = HeapAlloc(GetProcessHeap(), 0, data_size);
1106 memcpy(data, buffer->pvBuffer, data_size);
1108 transport.ctx = ctx;
1109 transport.no_incomplete_pull = FALSE;
1110 init_schan_buffers(&transport.in, NULL, NULL);
1111 if (schan_find_sec_buffer_idx(message, 0, SECBUFFER_STREAM_HEADER) != -1)
1112 init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer);
1113 else
1114 init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer_token);
1115 pgnutls_transport_set_ptr(ctx->session, &transport);
1117 while (sent < data_size)
1119 ret = pgnutls_record_send(ctx->session, data + sent, data_size - sent);
1120 if (ret < 0)
1122 if (ret != GNUTLS_E_AGAIN)
1124 pgnutls_perror(ret);
1125 HeapFree(GetProcessHeap(), 0, data);
1126 ERR("Returning SEC_E_INTERNAL_ERROR\n");
1127 return SEC_E_INTERNAL_ERROR;
1129 else break;
1131 sent += ret;
1134 TRACE("Sent %zd bytes\n", sent);
1136 b = &transport.out;
1137 b->desc->pBuffers[b->current_buffer_idx].cbBuffer = b->offset;
1138 HeapFree(GetProcessHeap(), 0, data);
1140 return SEC_E_OK;
1143 static int schan_decrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
1145 if (s->current_buffer_idx == -1)
1146 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
1148 return -1;
1151 static SECURITY_STATUS SEC_ENTRY schan_DecryptMessage(PCtxtHandle context_handle,
1152 PSecBufferDesc message, ULONG message_seq_no, PULONG quality)
1154 struct schan_transport transport;
1155 struct schan_context *ctx;
1156 SecBuffer *buffer;
1157 SIZE_T data_size;
1158 char *data;
1159 ssize_t received = 0;
1160 ssize_t ret;
1161 int idx;
1163 TRACE("context_handle %p, message %p, message_seq_no %d, quality %p\n",
1164 context_handle, message, message_seq_no, quality);
1166 if (!context_handle) return SEC_E_INVALID_HANDLE;
1167 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
1169 dump_buffer_desc(message);
1171 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_DATA);
1172 if (idx == -1)
1174 WARN("No data buffer passed\n");
1175 return SEC_E_INTERNAL_ERROR;
1177 buffer = &message->pBuffers[idx];
1179 data_size = buffer->cbBuffer;
1180 data = HeapAlloc(GetProcessHeap(), 0, data_size);
1182 transport.ctx = ctx;
1183 transport.no_incomplete_pull = TRUE;
1184 init_schan_buffers(&transport.in, message, schan_decrypt_message_get_next_buffer);
1185 init_schan_buffers(&transport.out, NULL, NULL);
1186 pgnutls_transport_set_ptr(ctx->session, (gnutls_transport_ptr_t)&transport);
1188 while (received < data_size)
1190 ret = pgnutls_record_recv(ctx->session, data + received, data_size - received);
1191 if (ret < 0)
1193 if (ret == GNUTLS_E_AGAIN)
1195 if (!received)
1197 pgnutls_perror(ret);
1198 HeapFree(GetProcessHeap(), 0, data);
1199 TRACE("Returning SEC_E_INCOMPLETE_MESSAGE\n");
1200 return SEC_E_INCOMPLETE_MESSAGE;
1202 break;
1204 else
1206 pgnutls_perror(ret);
1207 HeapFree(GetProcessHeap(), 0, data);
1208 ERR("Returning SEC_E_INTERNAL_ERROR\n");
1209 return SEC_E_INTERNAL_ERROR;
1212 else if (!ret)
1213 break;
1215 received += ret;
1218 TRACE("Received %zd bytes\n", received);
1220 if (transport.in.offset < data_size)
1222 /* let the caller know about unprocessed data */
1223 SIZE_T extra = data_size - transport.in.offset;
1224 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_EMPTY);
1225 if (idx == -1)
1227 WARN("No empty buffer for extra data.\n");
1228 HeapFree(GetProcessHeap(), 0, data);
1229 return SEC_E_INTERNAL_ERROR;
1231 message->pBuffers[idx].BufferType = SECBUFFER_EXTRA;
1232 message->pBuffers[idx].cbBuffer = extra;
1233 TRACE("An extra %ld bytes remain to be processed.\n", extra);
1236 memcpy(buffer->pvBuffer, data, received);
1237 buffer->cbBuffer = received;
1238 HeapFree(GetProcessHeap(), 0, data);
1240 return SEC_E_OK;
1243 static SECURITY_STATUS SEC_ENTRY schan_DeleteSecurityContext(PCtxtHandle context_handle)
1245 struct schan_context *ctx;
1247 TRACE("context_handle %p\n", context_handle);
1249 if (!context_handle) return SEC_E_INVALID_HANDLE;
1251 ctx = schan_free_handle(context_handle->dwLower, SCHAN_HANDLE_CTX);
1252 if (!ctx) return SEC_E_INVALID_HANDLE;
1254 pgnutls_deinit(ctx->session);
1255 HeapFree(GetProcessHeap(), 0, ctx);
1257 return SEC_E_OK;
1260 static void schan_gnutls_log(int level, const char *msg)
1262 TRACE("<%d> %s", level, msg);
1265 static const SecurityFunctionTableA schanTableA = {
1267 NULL, /* EnumerateSecurityPackagesA */
1268 schan_QueryCredentialsAttributesA,
1269 schan_AcquireCredentialsHandleA,
1270 schan_FreeCredentialsHandle,
1271 NULL, /* Reserved2 */
1272 schan_InitializeSecurityContextA,
1273 NULL, /* AcceptSecurityContext */
1274 NULL, /* CompleteAuthToken */
1275 schan_DeleteSecurityContext,
1276 NULL, /* ApplyControlToken */
1277 schan_QueryContextAttributesA,
1278 NULL, /* ImpersonateSecurityContext */
1279 NULL, /* RevertSecurityContext */
1280 NULL, /* MakeSignature */
1281 NULL, /* VerifySignature */
1282 FreeContextBuffer,
1283 NULL, /* QuerySecurityPackageInfoA */
1284 NULL, /* Reserved3 */
1285 NULL, /* Reserved4 */
1286 NULL, /* ExportSecurityContext */
1287 NULL, /* ImportSecurityContextA */
1288 NULL, /* AddCredentialsA */
1289 NULL, /* Reserved8 */
1290 NULL, /* QuerySecurityContextToken */
1291 schan_EncryptMessage,
1292 schan_DecryptMessage,
1293 NULL, /* SetContextAttributesA */
1296 static const SecurityFunctionTableW schanTableW = {
1298 NULL, /* EnumerateSecurityPackagesW */
1299 schan_QueryCredentialsAttributesW,
1300 schan_AcquireCredentialsHandleW,
1301 schan_FreeCredentialsHandle,
1302 NULL, /* Reserved2 */
1303 schan_InitializeSecurityContextW,
1304 NULL, /* AcceptSecurityContext */
1305 NULL, /* CompleteAuthToken */
1306 schan_DeleteSecurityContext,
1307 NULL, /* ApplyControlToken */
1308 schan_QueryContextAttributesW,
1309 NULL, /* ImpersonateSecurityContext */
1310 NULL, /* RevertSecurityContext */
1311 NULL, /* MakeSignature */
1312 NULL, /* VerifySignature */
1313 FreeContextBuffer,
1314 NULL, /* QuerySecurityPackageInfoW */
1315 NULL, /* Reserved3 */
1316 NULL, /* Reserved4 */
1317 NULL, /* ExportSecurityContext */
1318 NULL, /* ImportSecurityContextW */
1319 NULL, /* AddCredentialsW */
1320 NULL, /* Reserved8 */
1321 NULL, /* QuerySecurityContextToken */
1322 schan_EncryptMessage,
1323 schan_DecryptMessage,
1324 NULL, /* SetContextAttributesW */
1327 static const WCHAR schannelComment[] = { 'S','c','h','a','n','n','e','l',' ',
1328 'S','e','c','u','r','i','t','y',' ','P','a','c','k','a','g','e',0 };
1329 static const WCHAR schannelDllName[] = { 's','c','h','a','n','n','e','l','.','d','l','l',0 };
1331 void SECUR32_initSchannelSP(void)
1333 /* This is what Windows reports. This shouldn't break any applications
1334 * even though the functions are missing, because the wrapper will
1335 * return SEC_E_UNSUPPORTED_FUNCTION if our function is NULL.
1337 static const LONG caps =
1338 SECPKG_FLAG_INTEGRITY |
1339 SECPKG_FLAG_PRIVACY |
1340 SECPKG_FLAG_CONNECTION |
1341 SECPKG_FLAG_MULTI_REQUIRED |
1342 SECPKG_FLAG_EXTENDED_ERROR |
1343 SECPKG_FLAG_IMPERSONATION |
1344 SECPKG_FLAG_ACCEPT_WIN32_NAME |
1345 SECPKG_FLAG_STREAM;
1346 static const short version = 1;
1347 static const LONG maxToken = 16384;
1348 SEC_WCHAR *uniSPName = (SEC_WCHAR *)UNISP_NAME_W,
1349 *schannel = (SEC_WCHAR *)SCHANNEL_NAME_W;
1350 const SecPkgInfoW info[] = {
1351 { caps, version, UNISP_RPC_ID, maxToken, uniSPName, uniSPName },
1352 { caps, version, UNISP_RPC_ID, maxToken, schannel,
1353 (SEC_WCHAR *)schannelComment },
1355 SecureProvider *provider;
1356 int ret;
1358 libgnutls_handle = wine_dlopen(SONAME_LIBGNUTLS, RTLD_NOW, NULL, 0);
1359 if (!libgnutls_handle)
1361 WARN("Failed to load libgnutls.\n");
1362 return;
1365 #define LOAD_FUNCPTR(f) \
1366 if (!(p##f = wine_dlsym(libgnutls_handle, #f, NULL, 0))) \
1368 ERR("Failed to load %s\n", #f); \
1369 goto fail; \
1372 LOAD_FUNCPTR(gnutls_alert_get)
1373 LOAD_FUNCPTR(gnutls_alert_get_name)
1374 LOAD_FUNCPTR(gnutls_certificate_allocate_credentials)
1375 LOAD_FUNCPTR(gnutls_certificate_free_credentials)
1376 LOAD_FUNCPTR(gnutls_certificate_get_peers)
1377 LOAD_FUNCPTR(gnutls_cipher_get)
1378 LOAD_FUNCPTR(gnutls_cipher_get_key_size)
1379 LOAD_FUNCPTR(gnutls_credentials_set)
1380 LOAD_FUNCPTR(gnutls_deinit)
1381 LOAD_FUNCPTR(gnutls_global_deinit)
1382 LOAD_FUNCPTR(gnutls_global_init)
1383 LOAD_FUNCPTR(gnutls_global_set_log_function)
1384 LOAD_FUNCPTR(gnutls_global_set_log_level)
1385 LOAD_FUNCPTR(gnutls_handshake)
1386 LOAD_FUNCPTR(gnutls_init)
1387 LOAD_FUNCPTR(gnutls_kx_get)
1388 LOAD_FUNCPTR(gnutls_mac_get)
1389 LOAD_FUNCPTR(gnutls_mac_get_key_size)
1390 LOAD_FUNCPTR(gnutls_perror)
1391 LOAD_FUNCPTR(gnutls_protocol_get_version)
1392 LOAD_FUNCPTR(gnutls_set_default_priority)
1393 LOAD_FUNCPTR(gnutls_record_recv);
1394 LOAD_FUNCPTR(gnutls_record_send);
1395 LOAD_FUNCPTR(gnutls_transport_set_errno)
1396 LOAD_FUNCPTR(gnutls_transport_set_ptr)
1397 LOAD_FUNCPTR(gnutls_transport_set_pull_function)
1398 LOAD_FUNCPTR(gnutls_transport_set_push_function)
1399 #undef LOAD_FUNCPTR
1401 ret = pgnutls_global_init();
1402 if (ret != GNUTLS_E_SUCCESS)
1404 pgnutls_perror(ret);
1405 goto fail;
1408 if (TRACE_ON(secur32))
1410 pgnutls_global_set_log_level(4);
1411 pgnutls_global_set_log_function(schan_gnutls_log);
1414 schan_handle_table = HeapAlloc(GetProcessHeap(), 0, 64 * sizeof(*schan_handle_table));
1415 if (!schan_handle_table)
1417 ERR("Failed to allocate schannel handle table.\n");
1418 goto fail;
1420 schan_handle_table_size = 64;
1422 provider = SECUR32_addProvider(&schanTableA, &schanTableW, schannelDllName);
1423 if (!provider)
1425 ERR("Failed to add schannel provider.\n");
1426 goto fail;
1429 SECUR32_addPackages(provider, sizeof(info) / sizeof(info[0]), NULL, info);
1431 return;
1433 fail:
1434 HeapFree(GetProcessHeap(), 0, schan_handle_table);
1435 schan_handle_table = NULL;
1436 wine_dlclose(libgnutls_handle, NULL, 0);
1437 libgnutls_handle = NULL;
1438 return;
1441 void SECUR32_deinitSchannelSP(void)
1443 SIZE_T i = schan_handle_count;
1445 if (!libgnutls_handle) return;
1447 /* deinitialized sessions first because a pointer to the credentials
1448 * are stored for the session by calling gnutls_credentials_set. */
1449 while (i--)
1451 if (schan_handle_table[i].type == SCHAN_HANDLE_CTX)
1453 struct schan_context *ctx = schan_free_handle(i, SCHAN_HANDLE_CTX);
1454 pgnutls_deinit(ctx->session);
1455 HeapFree(GetProcessHeap(), 0, ctx);
1458 i = schan_handle_count;
1459 while (i--)
1461 if (schan_handle_table[i].type != SCHAN_HANDLE_FREE)
1463 struct schan_credentials *cred;
1464 cred = schan_free_handle(i, SCHAN_HANDLE_CRED);
1465 pgnutls_certificate_free_credentials(cred->credentials);
1466 HeapFree(GetProcessHeap(), 0, cred);
1469 HeapFree(GetProcessHeap(), 0, schan_handle_table);
1470 pgnutls_global_deinit();
1471 wine_dlclose(libgnutls_handle, NULL, 0);
1474 #else /* SONAME_LIBGNUTLS */
1476 void SECUR32_initSchannelSP(void)
1478 ERR("libgnutls not found, SSL connections will fail\n");
1481 void SECUR32_deinitSchannelSP(void) {}
1483 #endif /* SONAME_LIBGNUTLS */