secur32: Remove superfluous pointer casts.
[wine] / dlls / secur32 / schannel.c
1 /* Copyright (C) 2005 Juan Lang
2  * Copyright 2008 Henri Verbeet
3  *
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.
8  *
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.
13  *
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
17  *
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.
21  */
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26 #include <errno.h>
27 #include <limits.h>
28 #ifdef SONAME_LIBGNUTLS
29 #include <gnutls/gnutls.h>
30 #endif
31
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"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
42
43 #ifdef SONAME_LIBGNUTLS
44
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_cipher_get);
52 MAKE_FUNCPTR(gnutls_credentials_set);
53 MAKE_FUNCPTR(gnutls_deinit);
54 MAKE_FUNCPTR(gnutls_global_deinit);
55 MAKE_FUNCPTR(gnutls_global_init);
56 MAKE_FUNCPTR(gnutls_global_set_log_function);
57 MAKE_FUNCPTR(gnutls_global_set_log_level);
58 MAKE_FUNCPTR(gnutls_handshake);
59 MAKE_FUNCPTR(gnutls_init);
60 MAKE_FUNCPTR(gnutls_mac_get);
61 MAKE_FUNCPTR(gnutls_mac_get_key_size);
62 MAKE_FUNCPTR(gnutls_perror);
63 MAKE_FUNCPTR(gnutls_set_default_priority);
64 MAKE_FUNCPTR(gnutls_record_recv);
65 MAKE_FUNCPTR(gnutls_record_send);
66 MAKE_FUNCPTR(gnutls_transport_set_errno);
67 MAKE_FUNCPTR(gnutls_transport_set_ptr);
68 MAKE_FUNCPTR(gnutls_transport_set_pull_function);
69 MAKE_FUNCPTR(gnutls_transport_set_push_function);
70 #undef MAKE_FUNCPTR
71
72 #define SCHAN_INVALID_HANDLE ~0UL
73
74 enum schan_handle_type
75 {
76     SCHAN_HANDLE_CRED,
77     SCHAN_HANDLE_CTX,
78     SCHAN_HANDLE_FREE
79 };
80
81 struct schan_handle
82 {
83     void *object;
84     enum schan_handle_type type;
85 };
86
87 struct schan_credentials
88 {
89     ULONG credential_use;
90     gnutls_certificate_credentials credentials;
91 };
92
93 struct schan_context
94 {
95     gnutls_session_t session;
96     ULONG req_ctx_attr;
97 };
98
99 struct schan_transport;
100
101 struct schan_buffers
102 {
103     SIZE_T offset;
104     const SecBufferDesc *desc;
105     int current_buffer_idx;
106     BOOL allow_buffer_resize;
107     int (*get_next_buffer)(const struct schan_transport *, struct schan_buffers *);
108 };
109
110 struct schan_transport
111 {
112     struct schan_context *ctx;
113     struct schan_buffers in;
114     struct schan_buffers out;
115 };
116
117 static struct schan_handle *schan_handle_table;
118 static struct schan_handle *schan_free_handles;
119 static SIZE_T schan_handle_table_size;
120 static SIZE_T schan_handle_count;
121
122 static ULONG_PTR schan_alloc_handle(void *object, enum schan_handle_type type)
123 {
124     struct schan_handle *handle;
125
126     if (schan_free_handles)
127     {
128         /* Use a free handle */
129         handle = schan_free_handles;
130         if (handle->type != SCHAN_HANDLE_FREE)
131         {
132             ERR("Handle %d(%p) is in the free list, but has type %#x.\n", (handle-schan_handle_table), handle, handle->type);
133             return SCHAN_INVALID_HANDLE;
134         }
135         schan_free_handles = handle->object;
136         handle->object = object;
137         handle->type = type;
138
139         return handle - schan_handle_table;
140     }
141     if (!(schan_handle_count < schan_handle_table_size))
142     {
143         /* Grow the table */
144         SIZE_T new_size = schan_handle_table_size + (schan_handle_table_size >> 1);
145         struct schan_handle *new_table = HeapReAlloc(GetProcessHeap(), 0, schan_handle_table, new_size * sizeof(*schan_handle_table));
146         if (!new_table)
147         {
148             ERR("Failed to grow the handle table\n");
149             return SCHAN_INVALID_HANDLE;
150         }
151         schan_handle_table = new_table;
152         schan_handle_table_size = new_size;
153     }
154
155     handle = &schan_handle_table[schan_handle_count++];
156     handle->object = object;
157     handle->type = type;
158
159     return handle - schan_handle_table;
160 }
161
162 static void *schan_free_handle(ULONG_PTR handle_idx, enum schan_handle_type type)
163 {
164     struct schan_handle *handle;
165     void *object;
166
167     if (handle_idx == SCHAN_INVALID_HANDLE) return NULL;
168     handle = &schan_handle_table[handle_idx];
169     if (handle->type != type)
170     {
171         ERR("Handle %ld(%p) is not of type %#x\n", handle_idx, handle, type);
172         return NULL;
173     }
174
175     object = handle->object;
176     handle->object = schan_free_handles;
177     handle->type = SCHAN_HANDLE_FREE;
178     schan_free_handles = handle;
179
180     return object;
181 }
182
183 static void *schan_get_object(ULONG_PTR handle_idx, enum schan_handle_type type)
184 {
185     struct schan_handle *handle;
186
187     if (handle_idx == SCHAN_INVALID_HANDLE) return NULL;
188     handle = &schan_handle_table[handle_idx];
189     if (handle->type != type)
190     {
191         ERR("Handle %ld(%p) is not of type %#x\n", handle_idx, handle, type);
192         return NULL;
193     }
194
195     return handle->object;
196 }
197
198 static SECURITY_STATUS schan_QueryCredentialsAttributes(
199  PCredHandle phCredential, ULONG ulAttribute, VOID *pBuffer)
200 {
201     SECURITY_STATUS ret;
202
203     switch (ulAttribute)
204     {
205     case SECPKG_ATTR_SUPPORTED_ALGS:
206         if (pBuffer)
207         {
208             /* FIXME: get from CryptoAPI */
209             FIXME("SECPKG_ATTR_SUPPORTED_ALGS: stub\n");
210             ret = SEC_E_UNSUPPORTED_FUNCTION;
211         }
212         else
213             ret = SEC_E_INTERNAL_ERROR;
214         break;
215     case SECPKG_ATTR_CIPHER_STRENGTHS:
216         if (pBuffer)
217         {
218             SecPkgCred_CipherStrengths *r = pBuffer;
219
220             /* FIXME: get from CryptoAPI */
221             FIXME("SECPKG_ATTR_CIPHER_STRENGTHS: semi-stub\n");
222             r->dwMinimumCipherStrength = 40;
223             r->dwMaximumCipherStrength = 168;
224             ret = SEC_E_OK;
225         }
226         else
227             ret = SEC_E_INTERNAL_ERROR;
228         break;
229     case SECPKG_ATTR_SUPPORTED_PROTOCOLS:
230         if (pBuffer)
231         {
232             /* FIXME: get from OpenSSL? */
233             FIXME("SECPKG_ATTR_SUPPORTED_PROTOCOLS: stub\n");
234             ret = SEC_E_UNSUPPORTED_FUNCTION;
235         }
236         else
237             ret = SEC_E_INTERNAL_ERROR;
238         break;
239     default:
240         ret = SEC_E_UNSUPPORTED_FUNCTION;
241     }
242     return ret;
243 }
244
245 static SECURITY_STATUS SEC_ENTRY schan_QueryCredentialsAttributesA(
246  PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
247 {
248     SECURITY_STATUS ret;
249
250     TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
251
252     switch (ulAttribute)
253     {
254     case SECPKG_CRED_ATTR_NAMES:
255         FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
256         ret = SEC_E_UNSUPPORTED_FUNCTION;
257         break;
258     default:
259         ret = schan_QueryCredentialsAttributes(phCredential, ulAttribute,
260          pBuffer);
261     }
262     return ret;
263 }
264
265 static SECURITY_STATUS SEC_ENTRY schan_QueryCredentialsAttributesW(
266  PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
267 {
268     SECURITY_STATUS ret;
269
270     TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
271
272     switch (ulAttribute)
273     {
274     case SECPKG_CRED_ATTR_NAMES:
275         FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
276         ret = SEC_E_UNSUPPORTED_FUNCTION;
277         break;
278     default:
279         ret = schan_QueryCredentialsAttributes(phCredential, ulAttribute,
280          pBuffer);
281     }
282     return ret;
283 }
284
285 static SECURITY_STATUS schan_CheckCreds(const SCHANNEL_CRED *schanCred)
286 {
287     SECURITY_STATUS st;
288
289     switch (schanCred->dwVersion)
290     {
291     case SCH_CRED_V3:
292     case SCHANNEL_CRED_VERSION:
293         break;
294     default:
295         return SEC_E_INTERNAL_ERROR;
296     }
297
298     if (schanCred->cCreds == 0)
299         st = SEC_E_NO_CREDENTIALS;
300     else if (schanCred->cCreds > 1)
301         st = SEC_E_UNKNOWN_CREDENTIALS;
302     else
303     {
304         DWORD keySpec;
305         HCRYPTPROV csp;
306         BOOL ret, freeCSP;
307
308         ret = CryptAcquireCertificatePrivateKey(schanCred->paCred[0],
309          0, /* FIXME: what flags to use? */ NULL,
310          &csp, &keySpec, &freeCSP);
311         if (ret)
312         {
313             st = SEC_E_OK;
314             if (freeCSP)
315                 CryptReleaseContext(csp, 0);
316         }
317         else
318             st = SEC_E_UNKNOWN_CREDENTIALS;
319     }
320     return st;
321 }
322
323 static SECURITY_STATUS schan_AcquireClientCredentials(const SCHANNEL_CRED *schanCred,
324  PCredHandle phCredential, PTimeStamp ptsExpiry)
325 {
326     struct schan_credentials *creds;
327     SECURITY_STATUS st = SEC_E_OK;
328
329     TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred, phCredential, ptsExpiry);
330
331     if (schanCred)
332     {
333         st = schan_CheckCreds(schanCred);
334         if (st == SEC_E_NO_CREDENTIALS)
335             st = SEC_E_OK;
336     }
337
338     /* For now, the only thing I'm interested in is the direction of the
339      * connection, so just store it.
340      */
341     if (st == SEC_E_OK)
342     {
343         ULONG_PTR handle;
344         int ret;
345
346         creds = HeapAlloc(GetProcessHeap(), 0, sizeof(*creds));
347         if (!creds) return SEC_E_INSUFFICIENT_MEMORY;
348
349         handle = schan_alloc_handle(creds, SCHAN_HANDLE_CRED);
350         if (handle == SCHAN_INVALID_HANDLE) goto fail;
351
352         creds->credential_use = SECPKG_CRED_OUTBOUND;
353         ret = pgnutls_certificate_allocate_credentials(&creds->credentials);
354         if (ret != GNUTLS_E_SUCCESS)
355         {
356             pgnutls_perror(ret);
357             schan_free_handle(handle, SCHAN_HANDLE_CRED);
358             goto fail;
359         }
360
361         phCredential->dwLower = handle;
362         phCredential->dwUpper = 0;
363
364         /* Outbound credentials have no expiry */
365         if (ptsExpiry)
366         {
367             ptsExpiry->LowPart = 0;
368             ptsExpiry->HighPart = 0;
369         }
370     }
371     return st;
372
373 fail:
374     HeapFree(GetProcessHeap(), 0, creds);
375     return SEC_E_INTERNAL_ERROR;
376 }
377
378 static SECURITY_STATUS schan_AcquireServerCredentials(const SCHANNEL_CRED *schanCred,
379  PCredHandle phCredential, PTimeStamp ptsExpiry)
380 {
381     SECURITY_STATUS st;
382
383     TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred, phCredential, ptsExpiry);
384
385     if (!schanCred) return SEC_E_NO_CREDENTIALS;
386
387     st = schan_CheckCreds(schanCred);
388     if (st == SEC_E_OK)
389     {
390         ULONG_PTR handle;
391         struct schan_credentials *creds;
392
393         creds = HeapAlloc(GetProcessHeap(), 0, sizeof(*creds));
394         if (!creds) return SEC_E_INSUFFICIENT_MEMORY;
395         creds->credential_use = SECPKG_CRED_INBOUND;
396
397         handle = schan_alloc_handle(creds, SCHAN_HANDLE_CRED);
398         if (handle == SCHAN_INVALID_HANDLE)
399         {
400             HeapFree(GetProcessHeap(), 0, creds);
401             return SEC_E_INTERNAL_ERROR;
402         }
403
404         phCredential->dwLower = handle;
405         phCredential->dwUpper = 0;
406
407         /* FIXME: get expiry from cert */
408     }
409     return st;
410 }
411
412 static SECURITY_STATUS schan_AcquireCredentialsHandle(ULONG fCredentialUse,
413  const SCHANNEL_CRED *schanCred, PCredHandle phCredential, PTimeStamp ptsExpiry)
414 {
415     SECURITY_STATUS ret;
416
417     if (fCredentialUse == SECPKG_CRED_OUTBOUND)
418         ret = schan_AcquireClientCredentials(schanCred, phCredential,
419          ptsExpiry);
420     else
421         ret = schan_AcquireServerCredentials(schanCred, phCredential,
422          ptsExpiry);
423     return ret;
424 }
425
426 static SECURITY_STATUS SEC_ENTRY schan_AcquireCredentialsHandleA(
427  SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
428  PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
429  PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
430 {
431     TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
432      debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
433      pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
434     return schan_AcquireCredentialsHandle(fCredentialUse,
435      pAuthData, phCredential, ptsExpiry);
436 }
437
438 static SECURITY_STATUS SEC_ENTRY schan_AcquireCredentialsHandleW(
439  SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
440  PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
441  PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
442 {
443     TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
444      debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
445      pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
446     return schan_AcquireCredentialsHandle(fCredentialUse,
447      pAuthData, phCredential, ptsExpiry);
448 }
449
450 static SECURITY_STATUS SEC_ENTRY schan_FreeCredentialsHandle(
451  PCredHandle phCredential)
452 {
453     struct schan_credentials *creds;
454
455     TRACE("phCredential %p\n", phCredential);
456
457     if (!phCredential) return SEC_E_INVALID_HANDLE;
458
459     creds = schan_free_handle(phCredential->dwLower, SCHAN_HANDLE_CRED);
460     if (!creds) return SEC_E_INVALID_HANDLE;
461
462     if (creds->credential_use == SECPKG_CRED_OUTBOUND)
463         pgnutls_certificate_free_credentials(creds->credentials);
464     HeapFree(GetProcessHeap(), 0, creds);
465
466     return SEC_E_OK;
467 }
468
469 static void init_schan_buffers(struct schan_buffers *s, const PSecBufferDesc desc,
470         int (*get_next_buffer)(const struct schan_transport *, struct schan_buffers *))
471 {
472     s->offset = 0;
473     s->desc = desc;
474     s->current_buffer_idx = -1;
475     s->allow_buffer_resize = FALSE;
476     s->get_next_buffer = get_next_buffer;
477 }
478
479 static int schan_find_sec_buffer_idx(const SecBufferDesc *desc, unsigned int start_idx, ULONG buffer_type)
480 {
481     unsigned int i;
482     PSecBuffer buffer;
483
484     for (i = start_idx; i < desc->cBuffers; ++i)
485     {
486         buffer = &desc->pBuffers[i];
487         if (buffer->BufferType == buffer_type) return i;
488     }
489
490     return -1;
491 }
492
493 static void schan_resize_current_buffer(const struct schan_buffers *s, SIZE_T min_size)
494 {
495     SecBuffer *b = &s->desc->pBuffers[s->current_buffer_idx];
496     SIZE_T new_size = b->cbBuffer ? b->cbBuffer * 2 : 128;
497     void *new_data;
498
499     if (b->cbBuffer >= min_size || !s->allow_buffer_resize || min_size > UINT_MAX / 2) return;
500
501     while (new_size < min_size) new_size *= 2;
502
503     if (b->pvBuffer)
504         new_data = HeapReAlloc(GetProcessHeap(), 0, b->pvBuffer, new_size);
505     else
506         new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
507
508     if (!new_data)
509     {
510         TRACE("Failed to resize %p from %d to %ld\n", b->pvBuffer, b->cbBuffer, new_size);
511         return;
512     }
513
514     b->cbBuffer = new_size;
515     b->pvBuffer = new_data;
516 }
517
518 static char *schan_get_buffer(const struct schan_transport *t, struct schan_buffers *s, size_t *count)
519 {
520     SIZE_T max_count;
521     PSecBuffer buffer;
522
523     if (!s->desc)
524     {
525         TRACE("No desc\n");
526         return NULL;
527     }
528
529     if (s->current_buffer_idx == -1)
530     {
531         /* Initial buffer */
532         int buffer_idx = s->get_next_buffer(t, s);
533         if (buffer_idx == -1)
534         {
535             TRACE("No next buffer\n");
536             return NULL;
537         }
538         s->current_buffer_idx = buffer_idx;
539     }
540
541     buffer = &s->desc->pBuffers[s->current_buffer_idx];
542     TRACE("Using buffer %d: cbBuffer %d, BufferType %#x, pvBuffer %p\n", s->current_buffer_idx, buffer->cbBuffer, buffer->BufferType, buffer->pvBuffer);
543
544     schan_resize_current_buffer(s, s->offset + *count);
545     max_count = buffer->cbBuffer - s->offset;
546     if (!max_count)
547     {
548         int buffer_idx;
549
550         s->allow_buffer_resize = FALSE;
551         buffer_idx = s->get_next_buffer(t, s);
552         if (buffer_idx == -1)
553         {
554             TRACE("No next buffer\n");
555             return NULL;
556         }
557         s->current_buffer_idx = buffer_idx;
558         s->offset = 0;
559         return schan_get_buffer(t, s, count);
560     }
561
562     if (*count > max_count) *count = max_count;
563     return (char *)buffer->pvBuffer + s->offset;
564 }
565
566 static ssize_t schan_pull(gnutls_transport_ptr_t transport, void *buff, size_t buff_len)
567 {
568     struct schan_transport *t = transport;
569     char *b;
570
571     TRACE("Pull %zu bytes\n", buff_len);
572
573     b = schan_get_buffer(t, &t->in, &buff_len);
574     if (!b)
575     {
576         pgnutls_transport_set_errno(t->ctx->session, EAGAIN);
577         return -1;
578     }
579
580     memcpy(buff, b, buff_len);
581     t->in.offset += buff_len;
582
583     TRACE("Read %zu bytes\n", buff_len);
584
585     return buff_len;
586 }
587
588 static ssize_t schan_push(gnutls_transport_ptr_t transport, const void *buff, size_t buff_len)
589 {
590     struct schan_transport *t = transport;
591     char *b;
592
593     TRACE("Push %zu bytes\n", buff_len);
594
595     b = schan_get_buffer(t, &t->out, &buff_len);
596     if (!b)
597     {
598         pgnutls_transport_set_errno(t->ctx->session, EAGAIN);
599         return -1;
600     }
601
602     memcpy(b, buff, buff_len);
603     t->out.offset += buff_len;
604
605     TRACE("Wrote %zu bytes\n", buff_len);
606
607     return buff_len;
608 }
609
610 static int schan_init_sec_ctx_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
611 {
612     if (s->current_buffer_idx == -1)
613     {
614         int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
615         if (t->ctx->req_ctx_attr & ISC_REQ_ALLOCATE_MEMORY)
616         {
617             if (idx == -1)
618             {
619                 idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_EMPTY);
620                 if (idx != -1) s->desc->pBuffers[idx].BufferType = SECBUFFER_TOKEN;
621             }
622             if (idx != -1 && !s->desc->pBuffers[idx].pvBuffer) s->allow_buffer_resize = TRUE;
623         }
624         return idx;
625     }
626
627     return -1;
628 }
629
630 static void dump_buffer_desc(SecBufferDesc *desc)
631 {
632     unsigned int i;
633
634     if (!desc) return;
635     TRACE("Buffer desc %p:\n", desc);
636     for (i = 0; i < desc->cBuffers; ++i)
637     {
638         SecBuffer *b = &desc->pBuffers[i];
639         TRACE("\tbuffer %u: cbBuffer %d, BufferType %#x pvBuffer %p\n", i, b->cbBuffer, b->BufferType, b->pvBuffer);
640     }
641 }
642
643 /***********************************************************************
644  *              InitializeSecurityContextW
645  */
646 static SECURITY_STATUS SEC_ENTRY schan_InitializeSecurityContextW(
647  PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
648  ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
649  PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
650  PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
651 {
652     struct schan_context *ctx;
653     struct schan_buffers *out_buffers;
654     struct schan_credentials *cred;
655     struct schan_transport transport;
656     int err;
657
658     TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
659      debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
660      Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
661
662     dump_buffer_desc(pInput);
663     dump_buffer_desc(pOutput);
664
665     if (!phContext)
666     {
667         ULONG_PTR handle;
668
669         if (!phCredential) return SEC_E_INVALID_HANDLE;
670
671         cred = schan_get_object(phCredential->dwLower, SCHAN_HANDLE_CRED);
672         if (!cred) return SEC_E_INVALID_HANDLE;
673
674         if (!(cred->credential_use & SECPKG_CRED_OUTBOUND))
675         {
676             WARN("Invalid credential use %#x\n", cred->credential_use);
677             return SEC_E_INVALID_HANDLE;
678         }
679
680         ctx = HeapAlloc(GetProcessHeap(), 0, sizeof(*ctx));
681         if (!ctx) return SEC_E_INSUFFICIENT_MEMORY;
682
683         handle = schan_alloc_handle(ctx, SCHAN_HANDLE_CTX);
684         if (handle == SCHAN_INVALID_HANDLE)
685         {
686             HeapFree(GetProcessHeap(), 0, ctx);
687             return SEC_E_INTERNAL_ERROR;
688         }
689
690         err = pgnutls_init(&ctx->session, GNUTLS_CLIENT);
691         if (err != GNUTLS_E_SUCCESS)
692         {
693             pgnutls_perror(err);
694             schan_free_handle(handle, SCHAN_HANDLE_CTX);
695             HeapFree(GetProcessHeap(), 0, ctx);
696             return SEC_E_INTERNAL_ERROR;
697         }
698
699         /* FIXME: We should be using the information from the credentials here. */
700         FIXME("Using hardcoded \"NORMAL\" priority\n");
701         err = pgnutls_set_default_priority(ctx->session);
702         if (err != GNUTLS_E_SUCCESS)
703         {
704             pgnutls_perror(err);
705             pgnutls_deinit(ctx->session);
706             schan_free_handle(handle, SCHAN_HANDLE_CTX);
707             HeapFree(GetProcessHeap(), 0, ctx);
708         }
709
710         err = pgnutls_credentials_set(ctx->session, GNUTLS_CRD_CERTIFICATE, cred->credentials);
711         if (err != GNUTLS_E_SUCCESS)
712         {
713             pgnutls_perror(err);
714             pgnutls_deinit(ctx->session);
715             schan_free_handle(handle, SCHAN_HANDLE_CTX);
716             HeapFree(GetProcessHeap(), 0, ctx);
717         }
718
719         pgnutls_transport_set_pull_function(ctx->session, schan_pull);
720         pgnutls_transport_set_push_function(ctx->session, schan_push);
721
722         phNewContext->dwLower = handle;
723         phNewContext->dwUpper = 0;
724     }
725     else
726     {
727         ctx = schan_get_object(phContext->dwLower, SCHAN_HANDLE_CTX);
728     }
729
730     ctx->req_ctx_attr = fContextReq;
731
732     transport.ctx = ctx;
733     init_schan_buffers(&transport.in, pInput, schan_init_sec_ctx_get_next_buffer);
734     init_schan_buffers(&transport.out, pOutput, schan_init_sec_ctx_get_next_buffer);
735     pgnutls_transport_set_ptr(ctx->session, &transport);
736
737     /* Perform the TLS handshake */
738     err = pgnutls_handshake(ctx->session);
739
740     out_buffers = &transport.out;
741     if (out_buffers->current_buffer_idx != -1)
742     {
743         SecBuffer *buffer = &out_buffers->desc->pBuffers[out_buffers->current_buffer_idx];
744         buffer->cbBuffer = out_buffers->offset;
745     }
746
747     *pfContextAttr = 0;
748     if (ctx->req_ctx_attr & ISC_REQ_ALLOCATE_MEMORY)
749         *pfContextAttr |= ISC_RET_ALLOCATED_MEMORY;
750
751     switch(err)
752     {
753         case GNUTLS_E_SUCCESS:
754             TRACE("Handshake completed\n");
755             return SEC_E_OK;
756
757         case GNUTLS_E_AGAIN:
758             TRACE("Continue...\n");
759             return SEC_I_CONTINUE_NEEDED;
760
761         case GNUTLS_E_WARNING_ALERT_RECEIVED:
762         case GNUTLS_E_FATAL_ALERT_RECEIVED:
763         {
764             gnutls_alert_description_t alert = pgnutls_alert_get(ctx->session);
765             const char *alert_name = pgnutls_alert_get_name(alert);
766             WARN("ALERT: %d %s\n", alert, alert_name);
767             return SEC_E_INTERNAL_ERROR;
768         }
769
770         default:
771             pgnutls_perror(err);
772             return SEC_E_INTERNAL_ERROR;
773     }
774 }
775
776 /***********************************************************************
777  *              InitializeSecurityContextA
778  */
779 static SECURITY_STATUS SEC_ENTRY schan_InitializeSecurityContextA(
780  PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
781  ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
782  PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
783  PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
784 {
785     SECURITY_STATUS ret;
786     SEC_WCHAR *target_name = NULL;
787
788     TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
789      debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
790      Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
791
792     if (pszTargetName)
793     {
794         INT len = MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, NULL, 0);
795         target_name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*target_name));
796         MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, target_name, len);
797     }
798
799     ret = schan_InitializeSecurityContextW(phCredential, phContext, target_name,
800             fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
801             phNewContext, pOutput, pfContextAttr, ptsExpiry);
802
803     HeapFree(GetProcessHeap(), 0, target_name);
804
805     return ret;
806 }
807
808 static unsigned int schannel_get_cipher_block_size(gnutls_cipher_algorithm_t cipher)
809 {
810     const struct
811     {
812         gnutls_cipher_algorithm_t cipher;
813         unsigned int block_size;
814     }
815     algorithms[] =
816     {
817         {GNUTLS_CIPHER_3DES_CBC, 8},
818         {GNUTLS_CIPHER_AES_128_CBC, 16},
819         {GNUTLS_CIPHER_AES_256_CBC, 16},
820         {GNUTLS_CIPHER_ARCFOUR_128, 1},
821         {GNUTLS_CIPHER_ARCFOUR_40, 1},
822         {GNUTLS_CIPHER_DES_CBC, 8},
823         {GNUTLS_CIPHER_NULL, 1},
824         {GNUTLS_CIPHER_RC2_40_CBC, 8},
825     };
826     unsigned int i;
827
828     for (i = 0; i < sizeof(algorithms) / sizeof(*algorithms); ++i)
829     {
830         if (algorithms[i].cipher == cipher)
831             return algorithms[i].block_size;
832     }
833
834     FIXME("Unknown cipher %#x, returning 1\n", cipher);
835
836     return 1;
837 }
838
839 static SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesW(
840         PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
841 {
842     struct schan_context *ctx;
843
844     TRACE("context_handle %p, attribute %#x, buffer %p\n",
845             context_handle, attribute, buffer);
846
847     if (!context_handle) return SEC_E_INVALID_HANDLE;
848     ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
849
850     switch(attribute)
851     {
852         case SECPKG_ATTR_STREAM_SIZES:
853         {
854             SecPkgContext_StreamSizes *stream_sizes = buffer;
855             gnutls_mac_algorithm_t mac = pgnutls_mac_get(ctx->session);
856             size_t mac_size = pgnutls_mac_get_key_size(mac);
857             gnutls_cipher_algorithm_t cipher = pgnutls_cipher_get(ctx->session);
858             unsigned int block_size = schannel_get_cipher_block_size(cipher);
859
860             TRACE("Using %zu mac bytes, block size %u\n", mac_size, block_size);
861
862             /* These are defined by the TLS RFC */
863             stream_sizes->cbHeader = 5;
864             stream_sizes->cbTrailer = mac_size + 256; /* Max 255 bytes padding + 1 for padding size */
865             stream_sizes->cbMaximumMessage = 1 << 14;
866             stream_sizes->cbBuffers = 4;
867             stream_sizes->cbBlockSize = block_size;
868             return SEC_E_OK;
869         }
870
871         default:
872             FIXME("Unhandled attribute %#x\n", attribute);
873             return SEC_E_UNSUPPORTED_FUNCTION;
874     }
875 }
876
877 static SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesA(
878         PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
879 {
880     TRACE("context_handle %p, attribute %#x, buffer %p\n",
881             context_handle, attribute, buffer);
882
883     switch(attribute)
884     {
885         case SECPKG_ATTR_STREAM_SIZES:
886             return schan_QueryContextAttributesW(context_handle, attribute, buffer);
887
888         default:
889             FIXME("Unhandled attribute %#x\n", attribute);
890             return SEC_E_UNSUPPORTED_FUNCTION;
891     }
892 }
893
894 static int schan_encrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
895 {
896     SecBuffer *b;
897
898     if (s->current_buffer_idx == -1)
899         return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_HEADER);
900
901     b = &s->desc->pBuffers[s->current_buffer_idx];
902
903     if (b->BufferType == SECBUFFER_STREAM_HEADER)
904         return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
905
906     if (b->BufferType == SECBUFFER_DATA)
907         return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_TRAILER);
908
909     return -1;
910 }
911
912 static int schan_encrypt_message_get_next_buffer_token(const struct schan_transport *t, struct schan_buffers *s)
913 {
914     SecBuffer *b;
915
916     if (s->current_buffer_idx == -1)
917         return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
918
919     b = &s->desc->pBuffers[s->current_buffer_idx];
920
921     if (b->BufferType == SECBUFFER_TOKEN)
922     {
923         int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
924         if (idx != s->current_buffer_idx) return -1;
925         return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
926     }
927
928     if (b->BufferType == SECBUFFER_DATA)
929     {
930         int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
931         if (idx != -1)
932             idx = schan_find_sec_buffer_idx(s->desc, idx + 1, SECBUFFER_TOKEN);
933         return idx;
934     }
935
936     return -1;
937 }
938
939 static SECURITY_STATUS SEC_ENTRY schan_EncryptMessage(PCtxtHandle context_handle,
940         ULONG quality, PSecBufferDesc message, ULONG message_seq_no)
941 {
942     struct schan_transport transport;
943     struct schan_context *ctx;
944     struct schan_buffers *b;
945     SecBuffer *buffer;
946     SIZE_T data_size;
947     char *data;
948     ssize_t sent = 0;
949     ssize_t ret;
950     int idx;
951
952     TRACE("context_handle %p, quality %d, message %p, message_seq_no %d\n",
953             context_handle, quality, message, message_seq_no);
954
955     if (!context_handle) return SEC_E_INVALID_HANDLE;
956     ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
957
958     dump_buffer_desc(message);
959
960     idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_DATA);
961     if (idx == -1)
962     {
963         WARN("No data buffer passed\n");
964         return SEC_E_INTERNAL_ERROR;
965     }
966     buffer = &message->pBuffers[idx];
967
968     data_size = buffer->cbBuffer;
969     data = HeapAlloc(GetProcessHeap(), 0, data_size);
970     memcpy(data, buffer->pvBuffer, data_size);
971
972     transport.ctx = ctx;
973     init_schan_buffers(&transport.in, NULL, NULL);
974     if (schan_find_sec_buffer_idx(message, 0, SECBUFFER_STREAM_HEADER) != -1)
975         init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer);
976     else
977         init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer_token);
978     pgnutls_transport_set_ptr(ctx->session, &transport);
979
980     while (sent < data_size)
981     {
982         ret = pgnutls_record_send(ctx->session, data + sent, data_size - sent);
983         if (ret < 0)
984         {
985             if (ret != GNUTLS_E_AGAIN)
986             {
987                 pgnutls_perror(ret);
988                 HeapFree(GetProcessHeap(), 0, data);
989                 ERR("Returning SEC_E_INTERNAL_ERROR\n");
990                 return SEC_E_INTERNAL_ERROR;
991             }
992             else break;
993         }
994         sent += ret;
995     }
996
997     TRACE("Sent %zd bytes\n", sent);
998
999     b = &transport.out;
1000     b->desc->pBuffers[b->current_buffer_idx].cbBuffer = b->offset;
1001     HeapFree(GetProcessHeap(), 0, data);
1002
1003     return SEC_E_OK;
1004 }
1005
1006 static int schan_decrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
1007 {
1008     if (s->current_buffer_idx == -1)
1009         return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
1010
1011     return -1;
1012 }
1013
1014 static SECURITY_STATUS SEC_ENTRY schan_DecryptMessage(PCtxtHandle context_handle,
1015         PSecBufferDesc message, ULONG message_seq_no, PULONG quality)
1016 {
1017     struct schan_transport transport;
1018     struct schan_context *ctx;
1019     SecBuffer *buffer;
1020     SIZE_T data_size;
1021     char *data;
1022     ssize_t received = 0;
1023     ssize_t ret;
1024     int idx;
1025
1026     TRACE("context_handle %p, message %p, message_seq_no %d, quality %p\n",
1027             context_handle, message, message_seq_no, quality);
1028
1029     if (!context_handle) return SEC_E_INVALID_HANDLE;
1030     ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
1031
1032     dump_buffer_desc(message);
1033
1034     idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_DATA);
1035     if (idx == -1)
1036     {
1037         WARN("No data buffer passed\n");
1038         return SEC_E_INTERNAL_ERROR;
1039     }
1040     buffer = &message->pBuffers[idx];
1041
1042     data_size = buffer->cbBuffer;
1043     data = HeapAlloc(GetProcessHeap(), 0, data_size);
1044
1045     transport.ctx = ctx;
1046     init_schan_buffers(&transport.in, message, schan_decrypt_message_get_next_buffer);
1047     init_schan_buffers(&transport.out, NULL, NULL);
1048     pgnutls_transport_set_ptr(ctx->session, (gnutls_transport_ptr_t)&transport);
1049
1050     while (received < data_size)
1051     {
1052         ret = pgnutls_record_recv(ctx->session, data + received, data_size - received);
1053         if (ret < 0)
1054         {
1055             if (ret == GNUTLS_E_AGAIN)
1056             {
1057                 if (!received)
1058                 {
1059                     pgnutls_perror(ret);
1060                     HeapFree(GetProcessHeap(), 0, data);
1061                     TRACE("Returning SEC_E_INCOMPLETE_MESSAGE\n");
1062                     return SEC_E_INCOMPLETE_MESSAGE;
1063                 }
1064                 break;
1065             }
1066             else
1067             {
1068                 pgnutls_perror(ret);
1069                 HeapFree(GetProcessHeap(), 0, data);
1070                 ERR("Returning SEC_E_INTERNAL_ERROR\n");
1071                 return SEC_E_INTERNAL_ERROR;
1072             }
1073         }
1074         received += ret;
1075     }
1076
1077     TRACE("Received %zd bytes\n", received);
1078
1079     memcpy(buffer->pvBuffer, data, received);
1080     buffer->cbBuffer = received;
1081     HeapFree(GetProcessHeap(), 0, data);
1082
1083     return SEC_E_OK;
1084 }
1085
1086 static SECURITY_STATUS SEC_ENTRY schan_DeleteSecurityContext(PCtxtHandle context_handle)
1087 {
1088     struct schan_context *ctx;
1089
1090     TRACE("context_handle %p\n", context_handle);
1091
1092     if (!context_handle) return SEC_E_INVALID_HANDLE;
1093
1094     ctx = schan_free_handle(context_handle->dwLower, SCHAN_HANDLE_CTX);
1095     if (!ctx) return SEC_E_INVALID_HANDLE;
1096
1097     pgnutls_deinit(ctx->session);
1098     HeapFree(GetProcessHeap(), 0, ctx);
1099
1100     return SEC_E_OK;
1101 }
1102
1103 static void schan_gnutls_log(int level, const char *msg)
1104 {
1105     TRACE("<%d> %s", level, msg);
1106 }
1107
1108 static const SecurityFunctionTableA schanTableA = {
1109     1,
1110     NULL, /* EnumerateSecurityPackagesA */
1111     schan_QueryCredentialsAttributesA,
1112     schan_AcquireCredentialsHandleA,
1113     schan_FreeCredentialsHandle,
1114     NULL, /* Reserved2 */
1115     schan_InitializeSecurityContextA, 
1116     NULL, /* AcceptSecurityContext */
1117     NULL, /* CompleteAuthToken */
1118     schan_DeleteSecurityContext,
1119     NULL, /* ApplyControlToken */
1120     schan_QueryContextAttributesA,
1121     NULL, /* ImpersonateSecurityContext */
1122     NULL, /* RevertSecurityContext */
1123     NULL, /* MakeSignature */
1124     NULL, /* VerifySignature */
1125     FreeContextBuffer,
1126     NULL, /* QuerySecurityPackageInfoA */
1127     NULL, /* Reserved3 */
1128     NULL, /* Reserved4 */
1129     NULL, /* ExportSecurityContext */
1130     NULL, /* ImportSecurityContextA */
1131     NULL, /* AddCredentialsA */
1132     NULL, /* Reserved8 */
1133     NULL, /* QuerySecurityContextToken */
1134     schan_EncryptMessage,
1135     schan_DecryptMessage,
1136     NULL, /* SetContextAttributesA */
1137 };
1138
1139 static const SecurityFunctionTableW schanTableW = {
1140     1,
1141     NULL, /* EnumerateSecurityPackagesW */
1142     schan_QueryCredentialsAttributesW,
1143     schan_AcquireCredentialsHandleW,
1144     schan_FreeCredentialsHandle,
1145     NULL, /* Reserved2 */
1146     schan_InitializeSecurityContextW, 
1147     NULL, /* AcceptSecurityContext */
1148     NULL, /* CompleteAuthToken */
1149     schan_DeleteSecurityContext,
1150     NULL, /* ApplyControlToken */
1151     schan_QueryContextAttributesW,
1152     NULL, /* ImpersonateSecurityContext */
1153     NULL, /* RevertSecurityContext */
1154     NULL, /* MakeSignature */
1155     NULL, /* VerifySignature */
1156     FreeContextBuffer,
1157     NULL, /* QuerySecurityPackageInfoW */
1158     NULL, /* Reserved3 */
1159     NULL, /* Reserved4 */
1160     NULL, /* ExportSecurityContext */
1161     NULL, /* ImportSecurityContextW */
1162     NULL, /* AddCredentialsW */
1163     NULL, /* Reserved8 */
1164     NULL, /* QuerySecurityContextToken */
1165     schan_EncryptMessage,
1166     schan_DecryptMessage,
1167     NULL, /* SetContextAttributesW */
1168 };
1169
1170 static const WCHAR schannelComment[] = { 'S','c','h','a','n','n','e','l',' ',
1171  'S','e','c','u','r','i','t','y',' ','P','a','c','k','a','g','e',0 };
1172 static const WCHAR schannelDllName[] = { 's','c','h','a','n','n','e','l','.','d','l','l',0 };
1173
1174 void SECUR32_initSchannelSP(void)
1175 {
1176     /* This is what Windows reports.  This shouldn't break any applications
1177      * even though the functions are missing, because the wrapper will
1178      * return SEC_E_UNSUPPORTED_FUNCTION if our function is NULL.
1179      */
1180     static const long caps =
1181         SECPKG_FLAG_INTEGRITY |
1182         SECPKG_FLAG_PRIVACY |
1183         SECPKG_FLAG_CONNECTION |
1184         SECPKG_FLAG_MULTI_REQUIRED |
1185         SECPKG_FLAG_EXTENDED_ERROR |
1186         SECPKG_FLAG_IMPERSONATION |
1187         SECPKG_FLAG_ACCEPT_WIN32_NAME |
1188         SECPKG_FLAG_STREAM;
1189     static const short version = 1;
1190     static const long maxToken = 16384;
1191     SEC_WCHAR *uniSPName = (SEC_WCHAR *)UNISP_NAME_W,
1192               *schannel = (SEC_WCHAR *)SCHANNEL_NAME_W;
1193     const SecPkgInfoW info[] = {
1194         { caps, version, UNISP_RPC_ID, maxToken, uniSPName, uniSPName },
1195         { caps, version, UNISP_RPC_ID, maxToken, schannel,
1196             (SEC_WCHAR *)schannelComment },
1197     };
1198     SecureProvider *provider;
1199     int ret;
1200
1201     libgnutls_handle = wine_dlopen(SONAME_LIBGNUTLS, RTLD_NOW, NULL, 0);
1202     if (!libgnutls_handle)
1203     {
1204         WARN("Failed to load libgnutls.\n");
1205         return;
1206     }
1207
1208 #define LOAD_FUNCPTR(f) \
1209     if (!(p##f = wine_dlsym(libgnutls_handle, #f, NULL, 0))) \
1210     { \
1211         ERR("Failed to load %s\n", #f); \
1212         goto fail; \
1213     }
1214
1215     LOAD_FUNCPTR(gnutls_alert_get)
1216     LOAD_FUNCPTR(gnutls_alert_get_name)
1217     LOAD_FUNCPTR(gnutls_certificate_allocate_credentials)
1218     LOAD_FUNCPTR(gnutls_certificate_free_credentials)
1219     LOAD_FUNCPTR(gnutls_cipher_get)
1220     LOAD_FUNCPTR(gnutls_credentials_set)
1221     LOAD_FUNCPTR(gnutls_deinit)
1222     LOAD_FUNCPTR(gnutls_global_deinit)
1223     LOAD_FUNCPTR(gnutls_global_init)
1224     LOAD_FUNCPTR(gnutls_global_set_log_function)
1225     LOAD_FUNCPTR(gnutls_global_set_log_level)
1226     LOAD_FUNCPTR(gnutls_handshake)
1227     LOAD_FUNCPTR(gnutls_init)
1228     LOAD_FUNCPTR(gnutls_mac_get)
1229     LOAD_FUNCPTR(gnutls_mac_get_key_size)
1230     LOAD_FUNCPTR(gnutls_perror)
1231     LOAD_FUNCPTR(gnutls_set_default_priority)
1232     LOAD_FUNCPTR(gnutls_record_recv);
1233     LOAD_FUNCPTR(gnutls_record_send);
1234     LOAD_FUNCPTR(gnutls_transport_set_errno)
1235     LOAD_FUNCPTR(gnutls_transport_set_ptr)
1236     LOAD_FUNCPTR(gnutls_transport_set_pull_function)
1237     LOAD_FUNCPTR(gnutls_transport_set_push_function)
1238 #undef LOAD_FUNCPTR
1239
1240     ret = pgnutls_global_init();
1241     if (ret != GNUTLS_E_SUCCESS)
1242     {
1243         pgnutls_perror(ret);
1244         goto fail;
1245     }
1246
1247     if (TRACE_ON(secur32))
1248     {
1249         pgnutls_global_set_log_level(4);
1250         pgnutls_global_set_log_function(schan_gnutls_log);
1251     }
1252
1253     schan_handle_table = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 64 * sizeof(*schan_handle_table));
1254     if (!schan_handle_table)
1255     {
1256         ERR("Failed to allocate schannel handle table.\n");
1257         goto fail;
1258     }
1259     schan_handle_table_size = 64;
1260
1261     provider = SECUR32_addProvider(&schanTableA, &schanTableW, schannelDllName);
1262     if (!provider)
1263     {
1264         ERR("Failed to add schannel provider.\n");
1265         goto fail;
1266     }
1267
1268     SECUR32_addPackages(provider, sizeof(info) / sizeof(info[0]), NULL, info);
1269
1270     return;
1271
1272 fail:
1273     HeapFree(GetProcessHeap(), 0, schan_handle_table);
1274     schan_handle_table = NULL;
1275     wine_dlclose(libgnutls_handle, NULL, 0);
1276     libgnutls_handle = NULL;
1277     return;
1278 }
1279
1280 void SECUR32_deinitSchannelSP(void)
1281 {
1282     if (!libgnutls_handle) return;
1283
1284     pgnutls_global_deinit();
1285     wine_dlclose(libgnutls_handle, NULL, 0);
1286 }
1287
1288 #else /* SONAME_LIBGNUTLS */
1289
1290 void SECUR32_initSchannelSP(void) {}
1291 void SECUR32_deinitSchannelSP(void) {}
1292
1293 #endif /* SONAME_LIBGNUTLS */