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
23 #include "wine/port.h"
28 #ifdef SONAME_LIBGNUTLS
29 #include <gnutls/gnutls.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_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);
72 #define SCHAN_INVALID_HANDLE ~0UL
74 enum schan_handle_type
84 enum schan_handle_type type;
87 struct schan_credentials
90 gnutls_certificate_credentials credentials;
95 gnutls_session_t session;
99 struct schan_transport;
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 *);
110 struct schan_transport
112 struct schan_context *ctx;
113 struct schan_buffers in;
114 struct schan_buffers out;
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;
122 static ULONG_PTR schan_alloc_handle(void *object, enum schan_handle_type type)
124 struct schan_handle *handle;
126 if (schan_free_handles)
128 /* Use a free handle */
129 handle = schan_free_handles;
130 if (handle->type != SCHAN_HANDLE_FREE)
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;
135 schan_free_handles = handle->object;
136 handle->object = object;
139 return handle - schan_handle_table;
141 if (!(schan_handle_count < schan_handle_table_size))
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));
148 ERR("Failed to grow the handle table\n");
149 return SCHAN_INVALID_HANDLE;
151 schan_handle_table = new_table;
152 schan_handle_table_size = new_size;
155 handle = &schan_handle_table[schan_handle_count++];
156 handle->object = object;
159 return handle - schan_handle_table;
162 static void *schan_free_handle(ULONG_PTR handle_idx, enum schan_handle_type type)
164 struct schan_handle *handle;
167 if (handle_idx == SCHAN_INVALID_HANDLE) return NULL;
168 handle = &schan_handle_table[handle_idx];
169 if (handle->type != type)
171 ERR("Handle %ld(%p) is not of type %#x\n", handle_idx, handle, type);
175 object = handle->object;
176 handle->object = schan_free_handles;
177 handle->type = SCHAN_HANDLE_FREE;
178 schan_free_handles = handle;
183 static void *schan_get_object(ULONG_PTR handle_idx, enum schan_handle_type type)
185 struct schan_handle *handle;
187 if (handle_idx == SCHAN_INVALID_HANDLE) return NULL;
188 handle = &schan_handle_table[handle_idx];
189 if (handle->type != type)
191 ERR("Handle %ld(%p) is not of type %#x\n", handle_idx, handle, type);
195 return handle->object;
198 static SECURITY_STATUS schan_QueryCredentialsAttributes(
199 PCredHandle phCredential, ULONG ulAttribute, VOID *pBuffer)
205 case SECPKG_ATTR_SUPPORTED_ALGS:
208 /* FIXME: get from CryptoAPI */
209 FIXME("SECPKG_ATTR_SUPPORTED_ALGS: stub\n");
210 ret = SEC_E_UNSUPPORTED_FUNCTION;
213 ret = SEC_E_INTERNAL_ERROR;
215 case SECPKG_ATTR_CIPHER_STRENGTHS:
218 SecPkgCred_CipherStrengths *r = pBuffer;
220 /* FIXME: get from CryptoAPI */
221 FIXME("SECPKG_ATTR_CIPHER_STRENGTHS: semi-stub\n");
222 r->dwMinimumCipherStrength = 40;
223 r->dwMaximumCipherStrength = 168;
227 ret = SEC_E_INTERNAL_ERROR;
229 case SECPKG_ATTR_SUPPORTED_PROTOCOLS:
232 /* FIXME: get from OpenSSL? */
233 FIXME("SECPKG_ATTR_SUPPORTED_PROTOCOLS: stub\n");
234 ret = SEC_E_UNSUPPORTED_FUNCTION;
237 ret = SEC_E_INTERNAL_ERROR;
240 ret = SEC_E_UNSUPPORTED_FUNCTION;
245 static SECURITY_STATUS SEC_ENTRY schan_QueryCredentialsAttributesA(
246 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
250 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
254 case SECPKG_CRED_ATTR_NAMES:
255 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
256 ret = SEC_E_UNSUPPORTED_FUNCTION;
259 ret = schan_QueryCredentialsAttributes(phCredential, ulAttribute,
265 static SECURITY_STATUS SEC_ENTRY schan_QueryCredentialsAttributesW(
266 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
270 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
274 case SECPKG_CRED_ATTR_NAMES:
275 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
276 ret = SEC_E_UNSUPPORTED_FUNCTION;
279 ret = schan_QueryCredentialsAttributes(phCredential, ulAttribute,
285 static SECURITY_STATUS schan_CheckCreds(const SCHANNEL_CRED *schanCred)
289 switch (schanCred->dwVersion)
292 case SCHANNEL_CRED_VERSION:
295 return SEC_E_INTERNAL_ERROR;
298 if (schanCred->cCreds == 0)
299 st = SEC_E_NO_CREDENTIALS;
300 else if (schanCred->cCreds > 1)
301 st = SEC_E_UNKNOWN_CREDENTIALS;
308 ret = CryptAcquireCertificatePrivateKey(schanCred->paCred[0],
309 0, /* FIXME: what flags to use? */ NULL,
310 &csp, &keySpec, &freeCSP);
315 CryptReleaseContext(csp, 0);
318 st = SEC_E_UNKNOWN_CREDENTIALS;
323 static SECURITY_STATUS schan_AcquireClientCredentials(const SCHANNEL_CRED *schanCred,
324 PCredHandle phCredential, PTimeStamp ptsExpiry)
326 struct schan_credentials *creds;
327 SECURITY_STATUS st = SEC_E_OK;
329 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred, phCredential, ptsExpiry);
333 st = schan_CheckCreds(schanCred);
334 if (st == SEC_E_NO_CREDENTIALS)
338 /* For now, the only thing I'm interested in is the direction of the
339 * connection, so just store it.
346 creds = HeapAlloc(GetProcessHeap(), 0, sizeof(*creds));
347 if (!creds) return SEC_E_INSUFFICIENT_MEMORY;
349 handle = schan_alloc_handle(creds, SCHAN_HANDLE_CRED);
350 if (handle == SCHAN_INVALID_HANDLE) goto fail;
352 creds->credential_use = SECPKG_CRED_OUTBOUND;
353 ret = pgnutls_certificate_allocate_credentials(&creds->credentials);
354 if (ret != GNUTLS_E_SUCCESS)
357 schan_free_handle(handle, SCHAN_HANDLE_CRED);
361 phCredential->dwLower = handle;
362 phCredential->dwUpper = 0;
364 /* Outbound credentials have no expiry */
367 ptsExpiry->LowPart = 0;
368 ptsExpiry->HighPart = 0;
374 HeapFree(GetProcessHeap(), 0, creds);
375 return SEC_E_INTERNAL_ERROR;
378 static SECURITY_STATUS schan_AcquireServerCredentials(const SCHANNEL_CRED *schanCred,
379 PCredHandle phCredential, PTimeStamp ptsExpiry)
383 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred, phCredential, ptsExpiry);
385 if (!schanCred) return SEC_E_NO_CREDENTIALS;
387 st = schan_CheckCreds(schanCred);
391 struct schan_credentials *creds;
393 creds = HeapAlloc(GetProcessHeap(), 0, sizeof(*creds));
394 if (!creds) return SEC_E_INSUFFICIENT_MEMORY;
395 creds->credential_use = SECPKG_CRED_INBOUND;
397 handle = schan_alloc_handle(creds, SCHAN_HANDLE_CRED);
398 if (handle == SCHAN_INVALID_HANDLE)
400 HeapFree(GetProcessHeap(), 0, creds);
401 return SEC_E_INTERNAL_ERROR;
404 phCredential->dwLower = handle;
405 phCredential->dwUpper = 0;
407 /* FIXME: get expiry from cert */
412 static SECURITY_STATUS schan_AcquireCredentialsHandle(ULONG fCredentialUse,
413 const SCHANNEL_CRED *schanCred, PCredHandle phCredential, PTimeStamp ptsExpiry)
417 if (fCredentialUse == SECPKG_CRED_OUTBOUND)
418 ret = schan_AcquireClientCredentials(schanCred, phCredential,
421 ret = schan_AcquireServerCredentials(schanCred, phCredential,
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)
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);
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)
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);
450 static SECURITY_STATUS SEC_ENTRY schan_FreeCredentialsHandle(
451 PCredHandle phCredential)
453 struct schan_credentials *creds;
455 TRACE("phCredential %p\n", phCredential);
457 if (!phCredential) return SEC_E_INVALID_HANDLE;
459 creds = schan_free_handle(phCredential->dwLower, SCHAN_HANDLE_CRED);
460 if (!creds) return SEC_E_INVALID_HANDLE;
462 if (creds->credential_use == SECPKG_CRED_OUTBOUND)
463 pgnutls_certificate_free_credentials(creds->credentials);
464 HeapFree(GetProcessHeap(), 0, creds);
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 *))
474 s->current_buffer_idx = -1;
475 s->allow_buffer_resize = FALSE;
476 s->get_next_buffer = get_next_buffer;
479 static int schan_find_sec_buffer_idx(const SecBufferDesc *desc, unsigned int start_idx, ULONG buffer_type)
484 for (i = start_idx; i < desc->cBuffers; ++i)
486 buffer = &desc->pBuffers[i];
487 if (buffer->BufferType == buffer_type) return i;
493 static void schan_resize_current_buffer(const struct schan_buffers *s, SIZE_T min_size)
495 SecBuffer *b = &s->desc->pBuffers[s->current_buffer_idx];
496 SIZE_T new_size = b->cbBuffer ? b->cbBuffer * 2 : 128;
499 if (b->cbBuffer >= min_size || !s->allow_buffer_resize || min_size > UINT_MAX / 2) return;
501 while (new_size < min_size) new_size *= 2;
504 new_data = HeapReAlloc(GetProcessHeap(), 0, b->pvBuffer, new_size);
506 new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
510 TRACE("Failed to resize %p from %d to %ld\n", b->pvBuffer, b->cbBuffer, new_size);
514 b->cbBuffer = new_size;
515 b->pvBuffer = new_data;
518 static char *schan_get_buffer(const struct schan_transport *t, struct schan_buffers *s, size_t *count)
529 if (s->current_buffer_idx == -1)
532 int buffer_idx = s->get_next_buffer(t, s);
533 if (buffer_idx == -1)
535 TRACE("No next buffer\n");
538 s->current_buffer_idx = buffer_idx;
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);
544 schan_resize_current_buffer(s, s->offset + *count);
545 max_count = buffer->cbBuffer - s->offset;
550 s->allow_buffer_resize = FALSE;
551 buffer_idx = s->get_next_buffer(t, s);
552 if (buffer_idx == -1)
554 TRACE("No next buffer\n");
557 s->current_buffer_idx = buffer_idx;
559 return schan_get_buffer(t, s, count);
562 if (*count > max_count) *count = max_count;
563 return (char *)buffer->pvBuffer + s->offset;
566 static ssize_t schan_pull(gnutls_transport_ptr_t transport, void *buff, size_t buff_len)
568 struct schan_transport *t = transport;
571 TRACE("Pull %zu bytes\n", buff_len);
573 b = schan_get_buffer(t, &t->in, &buff_len);
576 pgnutls_transport_set_errno(t->ctx->session, EAGAIN);
580 memcpy(buff, b, buff_len);
581 t->in.offset += buff_len;
583 TRACE("Read %zu bytes\n", buff_len);
588 static ssize_t schan_push(gnutls_transport_ptr_t transport, const void *buff, size_t buff_len)
590 struct schan_transport *t = transport;
593 TRACE("Push %zu bytes\n", buff_len);
595 b = schan_get_buffer(t, &t->out, &buff_len);
598 pgnutls_transport_set_errno(t->ctx->session, EAGAIN);
602 memcpy(b, buff, buff_len);
603 t->out.offset += buff_len;
605 TRACE("Wrote %zu bytes\n", buff_len);
610 static int schan_init_sec_ctx_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
612 if (s->current_buffer_idx == -1)
614 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
615 if (t->ctx->req_ctx_attr & ISC_REQ_ALLOCATE_MEMORY)
619 idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_EMPTY);
620 if (idx != -1) s->desc->pBuffers[idx].BufferType = SECBUFFER_TOKEN;
622 if (idx != -1 && !s->desc->pBuffers[idx].pvBuffer)
624 s->desc->pBuffers[idx].cbBuffer = 0;
625 s->allow_buffer_resize = TRUE;
634 static void dump_buffer_desc(SecBufferDesc *desc)
639 TRACE("Buffer desc %p:\n", desc);
640 for (i = 0; i < desc->cBuffers; ++i)
642 SecBuffer *b = &desc->pBuffers[i];
643 TRACE("\tbuffer %u: cbBuffer %d, BufferType %#x pvBuffer %p\n", i, b->cbBuffer, b->BufferType, b->pvBuffer);
647 /***********************************************************************
648 * InitializeSecurityContextW
650 static SECURITY_STATUS SEC_ENTRY schan_InitializeSecurityContextW(
651 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
652 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
653 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
654 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
656 struct schan_context *ctx;
657 struct schan_buffers *out_buffers;
658 struct schan_credentials *cred;
659 struct schan_transport transport;
662 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
663 debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
664 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
666 dump_buffer_desc(pInput);
667 dump_buffer_desc(pOutput);
673 if (!phCredential) return SEC_E_INVALID_HANDLE;
675 cred = schan_get_object(phCredential->dwLower, SCHAN_HANDLE_CRED);
676 if (!cred) return SEC_E_INVALID_HANDLE;
678 if (!(cred->credential_use & SECPKG_CRED_OUTBOUND))
680 WARN("Invalid credential use %#x\n", cred->credential_use);
681 return SEC_E_INVALID_HANDLE;
684 ctx = HeapAlloc(GetProcessHeap(), 0, sizeof(*ctx));
685 if (!ctx) return SEC_E_INSUFFICIENT_MEMORY;
687 handle = schan_alloc_handle(ctx, SCHAN_HANDLE_CTX);
688 if (handle == SCHAN_INVALID_HANDLE)
690 HeapFree(GetProcessHeap(), 0, ctx);
691 return SEC_E_INTERNAL_ERROR;
694 err = pgnutls_init(&ctx->session, GNUTLS_CLIENT);
695 if (err != GNUTLS_E_SUCCESS)
698 schan_free_handle(handle, SCHAN_HANDLE_CTX);
699 HeapFree(GetProcessHeap(), 0, ctx);
700 return SEC_E_INTERNAL_ERROR;
703 /* FIXME: We should be using the information from the credentials here. */
704 FIXME("Using hardcoded \"NORMAL\" priority\n");
705 err = pgnutls_set_default_priority(ctx->session);
706 if (err != GNUTLS_E_SUCCESS)
709 pgnutls_deinit(ctx->session);
710 schan_free_handle(handle, SCHAN_HANDLE_CTX);
711 HeapFree(GetProcessHeap(), 0, ctx);
714 err = pgnutls_credentials_set(ctx->session, GNUTLS_CRD_CERTIFICATE, cred->credentials);
715 if (err != GNUTLS_E_SUCCESS)
718 pgnutls_deinit(ctx->session);
719 schan_free_handle(handle, SCHAN_HANDLE_CTX);
720 HeapFree(GetProcessHeap(), 0, ctx);
723 pgnutls_transport_set_pull_function(ctx->session, schan_pull);
724 pgnutls_transport_set_push_function(ctx->session, schan_push);
726 phNewContext->dwLower = handle;
727 phNewContext->dwUpper = 0;
731 ctx = schan_get_object(phContext->dwLower, SCHAN_HANDLE_CTX);
734 ctx->req_ctx_attr = fContextReq;
737 init_schan_buffers(&transport.in, pInput, schan_init_sec_ctx_get_next_buffer);
738 init_schan_buffers(&transport.out, pOutput, schan_init_sec_ctx_get_next_buffer);
739 pgnutls_transport_set_ptr(ctx->session, &transport);
741 /* Perform the TLS handshake */
742 err = pgnutls_handshake(ctx->session);
744 out_buffers = &transport.out;
745 if (out_buffers->current_buffer_idx != -1)
747 SecBuffer *buffer = &out_buffers->desc->pBuffers[out_buffers->current_buffer_idx];
748 buffer->cbBuffer = out_buffers->offset;
752 if (ctx->req_ctx_attr & ISC_REQ_ALLOCATE_MEMORY)
753 *pfContextAttr |= ISC_RET_ALLOCATED_MEMORY;
757 case GNUTLS_E_SUCCESS:
758 TRACE("Handshake completed\n");
762 TRACE("Continue...\n");
763 return SEC_I_CONTINUE_NEEDED;
765 case GNUTLS_E_WARNING_ALERT_RECEIVED:
766 case GNUTLS_E_FATAL_ALERT_RECEIVED:
768 gnutls_alert_description_t alert = pgnutls_alert_get(ctx->session);
769 const char *alert_name = pgnutls_alert_get_name(alert);
770 WARN("ALERT: %d %s\n", alert, alert_name);
771 return SEC_E_INTERNAL_ERROR;
776 return SEC_E_INTERNAL_ERROR;
780 /***********************************************************************
781 * InitializeSecurityContextA
783 static SECURITY_STATUS SEC_ENTRY schan_InitializeSecurityContextA(
784 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
785 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
786 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
787 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
790 SEC_WCHAR *target_name = NULL;
792 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
793 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
794 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
798 INT len = MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, NULL, 0);
799 target_name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*target_name));
800 MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, target_name, len);
803 ret = schan_InitializeSecurityContextW(phCredential, phContext, target_name,
804 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
805 phNewContext, pOutput, pfContextAttr, ptsExpiry);
807 HeapFree(GetProcessHeap(), 0, target_name);
812 static unsigned int schannel_get_cipher_block_size(gnutls_cipher_algorithm_t cipher)
816 gnutls_cipher_algorithm_t cipher;
817 unsigned int block_size;
821 {GNUTLS_CIPHER_3DES_CBC, 8},
822 {GNUTLS_CIPHER_AES_128_CBC, 16},
823 {GNUTLS_CIPHER_AES_256_CBC, 16},
824 {GNUTLS_CIPHER_ARCFOUR_128, 1},
825 {GNUTLS_CIPHER_ARCFOUR_40, 1},
826 {GNUTLS_CIPHER_DES_CBC, 8},
827 {GNUTLS_CIPHER_NULL, 1},
828 {GNUTLS_CIPHER_RC2_40_CBC, 8},
832 for (i = 0; i < sizeof(algorithms) / sizeof(*algorithms); ++i)
834 if (algorithms[i].cipher == cipher)
835 return algorithms[i].block_size;
838 FIXME("Unknown cipher %#x, returning 1\n", cipher);
843 static SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesW(
844 PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
846 struct schan_context *ctx;
848 TRACE("context_handle %p, attribute %#x, buffer %p\n",
849 context_handle, attribute, buffer);
851 if (!context_handle) return SEC_E_INVALID_HANDLE;
852 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
856 case SECPKG_ATTR_STREAM_SIZES:
858 SecPkgContext_StreamSizes *stream_sizes = buffer;
859 gnutls_mac_algorithm_t mac = pgnutls_mac_get(ctx->session);
860 size_t mac_size = pgnutls_mac_get_key_size(mac);
861 gnutls_cipher_algorithm_t cipher = pgnutls_cipher_get(ctx->session);
862 unsigned int block_size = schannel_get_cipher_block_size(cipher);
864 TRACE("Using %zu mac bytes, block size %u\n", mac_size, block_size);
866 /* These are defined by the TLS RFC */
867 stream_sizes->cbHeader = 5;
868 stream_sizes->cbTrailer = mac_size + 256; /* Max 255 bytes padding + 1 for padding size */
869 stream_sizes->cbMaximumMessage = 1 << 14;
870 stream_sizes->cbBuffers = 4;
871 stream_sizes->cbBlockSize = block_size;
876 FIXME("Unhandled attribute %#x\n", attribute);
877 return SEC_E_UNSUPPORTED_FUNCTION;
881 static SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesA(
882 PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
884 TRACE("context_handle %p, attribute %#x, buffer %p\n",
885 context_handle, attribute, buffer);
889 case SECPKG_ATTR_STREAM_SIZES:
890 return schan_QueryContextAttributesW(context_handle, attribute, buffer);
893 FIXME("Unhandled attribute %#x\n", attribute);
894 return SEC_E_UNSUPPORTED_FUNCTION;
898 static int schan_encrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
902 if (s->current_buffer_idx == -1)
903 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_HEADER);
905 b = &s->desc->pBuffers[s->current_buffer_idx];
907 if (b->BufferType == SECBUFFER_STREAM_HEADER)
908 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
910 if (b->BufferType == SECBUFFER_DATA)
911 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_TRAILER);
916 static int schan_encrypt_message_get_next_buffer_token(const struct schan_transport *t, struct schan_buffers *s)
920 if (s->current_buffer_idx == -1)
921 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
923 b = &s->desc->pBuffers[s->current_buffer_idx];
925 if (b->BufferType == SECBUFFER_TOKEN)
927 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
928 if (idx != s->current_buffer_idx) return -1;
929 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
932 if (b->BufferType == SECBUFFER_DATA)
934 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
936 idx = schan_find_sec_buffer_idx(s->desc, idx + 1, SECBUFFER_TOKEN);
943 static SECURITY_STATUS SEC_ENTRY schan_EncryptMessage(PCtxtHandle context_handle,
944 ULONG quality, PSecBufferDesc message, ULONG message_seq_no)
946 struct schan_transport transport;
947 struct schan_context *ctx;
948 struct schan_buffers *b;
956 TRACE("context_handle %p, quality %d, message %p, message_seq_no %d\n",
957 context_handle, quality, message, message_seq_no);
959 if (!context_handle) return SEC_E_INVALID_HANDLE;
960 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
962 dump_buffer_desc(message);
964 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_DATA);
967 WARN("No data buffer passed\n");
968 return SEC_E_INTERNAL_ERROR;
970 buffer = &message->pBuffers[idx];
972 data_size = buffer->cbBuffer;
973 data = HeapAlloc(GetProcessHeap(), 0, data_size);
974 memcpy(data, buffer->pvBuffer, data_size);
977 init_schan_buffers(&transport.in, NULL, NULL);
978 if (schan_find_sec_buffer_idx(message, 0, SECBUFFER_STREAM_HEADER) != -1)
979 init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer);
981 init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer_token);
982 pgnutls_transport_set_ptr(ctx->session, &transport);
984 while (sent < data_size)
986 ret = pgnutls_record_send(ctx->session, data + sent, data_size - sent);
989 if (ret != GNUTLS_E_AGAIN)
992 HeapFree(GetProcessHeap(), 0, data);
993 ERR("Returning SEC_E_INTERNAL_ERROR\n");
994 return SEC_E_INTERNAL_ERROR;
1001 TRACE("Sent %zd bytes\n", sent);
1004 b->desc->pBuffers[b->current_buffer_idx].cbBuffer = b->offset;
1005 HeapFree(GetProcessHeap(), 0, data);
1010 static int schan_decrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
1012 if (s->current_buffer_idx == -1)
1013 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
1018 static SECURITY_STATUS SEC_ENTRY schan_DecryptMessage(PCtxtHandle context_handle,
1019 PSecBufferDesc message, ULONG message_seq_no, PULONG quality)
1021 struct schan_transport transport;
1022 struct schan_context *ctx;
1026 ssize_t received = 0;
1030 TRACE("context_handle %p, message %p, message_seq_no %d, quality %p\n",
1031 context_handle, message, message_seq_no, quality);
1033 if (!context_handle) return SEC_E_INVALID_HANDLE;
1034 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
1036 dump_buffer_desc(message);
1038 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_DATA);
1041 WARN("No data buffer passed\n");
1042 return SEC_E_INTERNAL_ERROR;
1044 buffer = &message->pBuffers[idx];
1046 data_size = buffer->cbBuffer;
1047 data = HeapAlloc(GetProcessHeap(), 0, data_size);
1049 transport.ctx = ctx;
1050 init_schan_buffers(&transport.in, message, schan_decrypt_message_get_next_buffer);
1051 init_schan_buffers(&transport.out, NULL, NULL);
1052 pgnutls_transport_set_ptr(ctx->session, (gnutls_transport_ptr_t)&transport);
1054 while (received < data_size)
1056 ret = pgnutls_record_recv(ctx->session, data + received, data_size - received);
1059 if (ret == GNUTLS_E_AGAIN)
1063 pgnutls_perror(ret);
1064 HeapFree(GetProcessHeap(), 0, data);
1065 TRACE("Returning SEC_E_INCOMPLETE_MESSAGE\n");
1066 return SEC_E_INCOMPLETE_MESSAGE;
1072 pgnutls_perror(ret);
1073 HeapFree(GetProcessHeap(), 0, data);
1074 ERR("Returning SEC_E_INTERNAL_ERROR\n");
1075 return SEC_E_INTERNAL_ERROR;
1081 TRACE("Received %zd bytes\n", received);
1083 memcpy(buffer->pvBuffer, data, received);
1084 buffer->cbBuffer = received;
1085 HeapFree(GetProcessHeap(), 0, data);
1090 static SECURITY_STATUS SEC_ENTRY schan_DeleteSecurityContext(PCtxtHandle context_handle)
1092 struct schan_context *ctx;
1094 TRACE("context_handle %p\n", context_handle);
1096 if (!context_handle) return SEC_E_INVALID_HANDLE;
1098 ctx = schan_free_handle(context_handle->dwLower, SCHAN_HANDLE_CTX);
1099 if (!ctx) return SEC_E_INVALID_HANDLE;
1101 pgnutls_deinit(ctx->session);
1102 HeapFree(GetProcessHeap(), 0, ctx);
1107 static void schan_gnutls_log(int level, const char *msg)
1109 TRACE("<%d> %s", level, msg);
1112 static const SecurityFunctionTableA schanTableA = {
1114 NULL, /* EnumerateSecurityPackagesA */
1115 schan_QueryCredentialsAttributesA,
1116 schan_AcquireCredentialsHandleA,
1117 schan_FreeCredentialsHandle,
1118 NULL, /* Reserved2 */
1119 schan_InitializeSecurityContextA,
1120 NULL, /* AcceptSecurityContext */
1121 NULL, /* CompleteAuthToken */
1122 schan_DeleteSecurityContext,
1123 NULL, /* ApplyControlToken */
1124 schan_QueryContextAttributesA,
1125 NULL, /* ImpersonateSecurityContext */
1126 NULL, /* RevertSecurityContext */
1127 NULL, /* MakeSignature */
1128 NULL, /* VerifySignature */
1130 NULL, /* QuerySecurityPackageInfoA */
1131 NULL, /* Reserved3 */
1132 NULL, /* Reserved4 */
1133 NULL, /* ExportSecurityContext */
1134 NULL, /* ImportSecurityContextA */
1135 NULL, /* AddCredentialsA */
1136 NULL, /* Reserved8 */
1137 NULL, /* QuerySecurityContextToken */
1138 schan_EncryptMessage,
1139 schan_DecryptMessage,
1140 NULL, /* SetContextAttributesA */
1143 static const SecurityFunctionTableW schanTableW = {
1145 NULL, /* EnumerateSecurityPackagesW */
1146 schan_QueryCredentialsAttributesW,
1147 schan_AcquireCredentialsHandleW,
1148 schan_FreeCredentialsHandle,
1149 NULL, /* Reserved2 */
1150 schan_InitializeSecurityContextW,
1151 NULL, /* AcceptSecurityContext */
1152 NULL, /* CompleteAuthToken */
1153 schan_DeleteSecurityContext,
1154 NULL, /* ApplyControlToken */
1155 schan_QueryContextAttributesW,
1156 NULL, /* ImpersonateSecurityContext */
1157 NULL, /* RevertSecurityContext */
1158 NULL, /* MakeSignature */
1159 NULL, /* VerifySignature */
1161 NULL, /* QuerySecurityPackageInfoW */
1162 NULL, /* Reserved3 */
1163 NULL, /* Reserved4 */
1164 NULL, /* ExportSecurityContext */
1165 NULL, /* ImportSecurityContextW */
1166 NULL, /* AddCredentialsW */
1167 NULL, /* Reserved8 */
1168 NULL, /* QuerySecurityContextToken */
1169 schan_EncryptMessage,
1170 schan_DecryptMessage,
1171 NULL, /* SetContextAttributesW */
1174 static const WCHAR schannelComment[] = { 'S','c','h','a','n','n','e','l',' ',
1175 'S','e','c','u','r','i','t','y',' ','P','a','c','k','a','g','e',0 };
1176 static const WCHAR schannelDllName[] = { 's','c','h','a','n','n','e','l','.','d','l','l',0 };
1178 void SECUR32_initSchannelSP(void)
1180 /* This is what Windows reports. This shouldn't break any applications
1181 * even though the functions are missing, because the wrapper will
1182 * return SEC_E_UNSUPPORTED_FUNCTION if our function is NULL.
1184 static const long caps =
1185 SECPKG_FLAG_INTEGRITY |
1186 SECPKG_FLAG_PRIVACY |
1187 SECPKG_FLAG_CONNECTION |
1188 SECPKG_FLAG_MULTI_REQUIRED |
1189 SECPKG_FLAG_EXTENDED_ERROR |
1190 SECPKG_FLAG_IMPERSONATION |
1191 SECPKG_FLAG_ACCEPT_WIN32_NAME |
1193 static const short version = 1;
1194 static const long maxToken = 16384;
1195 SEC_WCHAR *uniSPName = (SEC_WCHAR *)UNISP_NAME_W,
1196 *schannel = (SEC_WCHAR *)SCHANNEL_NAME_W;
1197 const SecPkgInfoW info[] = {
1198 { caps, version, UNISP_RPC_ID, maxToken, uniSPName, uniSPName },
1199 { caps, version, UNISP_RPC_ID, maxToken, schannel,
1200 (SEC_WCHAR *)schannelComment },
1202 SecureProvider *provider;
1205 libgnutls_handle = wine_dlopen(SONAME_LIBGNUTLS, RTLD_NOW, NULL, 0);
1206 if (!libgnutls_handle)
1208 WARN("Failed to load libgnutls.\n");
1212 #define LOAD_FUNCPTR(f) \
1213 if (!(p##f = wine_dlsym(libgnutls_handle, #f, NULL, 0))) \
1215 ERR("Failed to load %s\n", #f); \
1219 LOAD_FUNCPTR(gnutls_alert_get)
1220 LOAD_FUNCPTR(gnutls_alert_get_name)
1221 LOAD_FUNCPTR(gnutls_certificate_allocate_credentials)
1222 LOAD_FUNCPTR(gnutls_certificate_free_credentials)
1223 LOAD_FUNCPTR(gnutls_cipher_get)
1224 LOAD_FUNCPTR(gnutls_credentials_set)
1225 LOAD_FUNCPTR(gnutls_deinit)
1226 LOAD_FUNCPTR(gnutls_global_deinit)
1227 LOAD_FUNCPTR(gnutls_global_init)
1228 LOAD_FUNCPTR(gnutls_global_set_log_function)
1229 LOAD_FUNCPTR(gnutls_global_set_log_level)
1230 LOAD_FUNCPTR(gnutls_handshake)
1231 LOAD_FUNCPTR(gnutls_init)
1232 LOAD_FUNCPTR(gnutls_mac_get)
1233 LOAD_FUNCPTR(gnutls_mac_get_key_size)
1234 LOAD_FUNCPTR(gnutls_perror)
1235 LOAD_FUNCPTR(gnutls_set_default_priority)
1236 LOAD_FUNCPTR(gnutls_record_recv);
1237 LOAD_FUNCPTR(gnutls_record_send);
1238 LOAD_FUNCPTR(gnutls_transport_set_errno)
1239 LOAD_FUNCPTR(gnutls_transport_set_ptr)
1240 LOAD_FUNCPTR(gnutls_transport_set_pull_function)
1241 LOAD_FUNCPTR(gnutls_transport_set_push_function)
1244 ret = pgnutls_global_init();
1245 if (ret != GNUTLS_E_SUCCESS)
1247 pgnutls_perror(ret);
1251 if (TRACE_ON(secur32))
1253 pgnutls_global_set_log_level(4);
1254 pgnutls_global_set_log_function(schan_gnutls_log);
1257 schan_handle_table = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 64 * sizeof(*schan_handle_table));
1258 if (!schan_handle_table)
1260 ERR("Failed to allocate schannel handle table.\n");
1263 schan_handle_table_size = 64;
1265 provider = SECUR32_addProvider(&schanTableA, &schanTableW, schannelDllName);
1268 ERR("Failed to add schannel provider.\n");
1272 SECUR32_addPackages(provider, sizeof(info) / sizeof(info[0]), NULL, info);
1277 HeapFree(GetProcessHeap(), 0, schan_handle_table);
1278 schan_handle_table = NULL;
1279 wine_dlclose(libgnutls_handle, NULL, 0);
1280 libgnutls_handle = NULL;
1284 void SECUR32_deinitSchannelSP(void)
1286 if (!libgnutls_handle) return;
1288 pgnutls_global_deinit();
1289 wine_dlclose(libgnutls_handle, NULL, 0);
1292 #else /* SONAME_LIBGNUTLS */
1294 void SECUR32_initSchannelSP(void) {}
1295 void SECUR32_deinitSchannelSP(void) {}
1297 #endif /* SONAME_LIBGNUTLS */