secur32: Disable schannel InitializeSecurityContextW.
[wine/hacks.git] / dlls / secur32 / schannel.c
blobc981bf2015d3bf6eefb95ed161fc05d06a89171d
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_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
72 #define SCHAN_INVALID_HANDLE ~0UL
74 enum schan_handle_type
76 SCHAN_HANDLE_CRED,
77 SCHAN_HANDLE_CTX,
78 SCHAN_HANDLE_FREE
81 struct schan_handle
83 void *object;
84 enum schan_handle_type type;
87 struct schan_credentials
89 ULONG credential_use;
90 gnutls_certificate_credentials credentials;
93 struct schan_context
95 gnutls_session_t session;
96 ULONG req_ctx_attr;
99 struct schan_transport;
101 struct schan_buffers
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 *);
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;
137 handle->type = type;
139 return handle - schan_handle_table;
141 if (!(schan_handle_count < schan_handle_table_size))
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)
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;
157 handle->type = type;
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;
165 void *object;
167 if (handle_idx == SCHAN_INVALID_HANDLE) return NULL;
168 if (handle_idx >= schan_handle_count)
169 ERR("Handle %ld is not in range [0,%ld]\n", handle_idx, schan_handle_count-1);
171 handle = &schan_handle_table[handle_idx];
172 if (handle->type != type)
174 ERR("Handle %ld(%p) is not of type %#x\n", handle_idx, handle, type);
175 return NULL;
178 object = handle->object;
179 handle->object = schan_free_handles;
180 handle->type = SCHAN_HANDLE_FREE;
181 schan_free_handles = handle;
183 return object;
186 static void *schan_get_object(ULONG_PTR handle_idx, enum schan_handle_type type)
188 struct schan_handle *handle;
190 if (handle_idx == SCHAN_INVALID_HANDLE) return NULL;
191 handle = &schan_handle_table[handle_idx];
192 if (handle->type != type)
194 ERR("Handle %ld(%p) is not of type %#x\n", handle_idx, handle, type);
195 return NULL;
198 return handle->object;
201 static SECURITY_STATUS schan_QueryCredentialsAttributes(
202 PCredHandle phCredential, ULONG ulAttribute, VOID *pBuffer)
204 SECURITY_STATUS ret;
206 switch (ulAttribute)
208 case SECPKG_ATTR_SUPPORTED_ALGS:
209 if (pBuffer)
211 /* FIXME: get from CryptoAPI */
212 FIXME("SECPKG_ATTR_SUPPORTED_ALGS: stub\n");
213 ret = SEC_E_UNSUPPORTED_FUNCTION;
215 else
216 ret = SEC_E_INTERNAL_ERROR;
217 break;
218 case SECPKG_ATTR_CIPHER_STRENGTHS:
219 if (pBuffer)
221 SecPkgCred_CipherStrengths *r = pBuffer;
223 /* FIXME: get from CryptoAPI */
224 FIXME("SECPKG_ATTR_CIPHER_STRENGTHS: semi-stub\n");
225 r->dwMinimumCipherStrength = 40;
226 r->dwMaximumCipherStrength = 168;
227 ret = SEC_E_OK;
229 else
230 ret = SEC_E_INTERNAL_ERROR;
231 break;
232 case SECPKG_ATTR_SUPPORTED_PROTOCOLS:
233 if (pBuffer)
235 /* FIXME: get from OpenSSL? */
236 FIXME("SECPKG_ATTR_SUPPORTED_PROTOCOLS: stub\n");
237 ret = SEC_E_UNSUPPORTED_FUNCTION;
239 else
240 ret = SEC_E_INTERNAL_ERROR;
241 break;
242 default:
243 ret = SEC_E_UNSUPPORTED_FUNCTION;
245 return ret;
248 static SECURITY_STATUS SEC_ENTRY schan_QueryCredentialsAttributesA(
249 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
251 SECURITY_STATUS ret;
253 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
255 switch (ulAttribute)
257 case SECPKG_CRED_ATTR_NAMES:
258 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
259 ret = SEC_E_UNSUPPORTED_FUNCTION;
260 break;
261 default:
262 ret = schan_QueryCredentialsAttributes(phCredential, ulAttribute,
263 pBuffer);
265 return ret;
268 static SECURITY_STATUS SEC_ENTRY schan_QueryCredentialsAttributesW(
269 PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
271 SECURITY_STATUS ret;
273 TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
275 switch (ulAttribute)
277 case SECPKG_CRED_ATTR_NAMES:
278 FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
279 ret = SEC_E_UNSUPPORTED_FUNCTION;
280 break;
281 default:
282 ret = schan_QueryCredentialsAttributes(phCredential, ulAttribute,
283 pBuffer);
285 return ret;
288 static SECURITY_STATUS schan_CheckCreds(const SCHANNEL_CRED *schanCred)
290 SECURITY_STATUS st;
292 switch (schanCred->dwVersion)
294 case SCH_CRED_V3:
295 case SCHANNEL_CRED_VERSION:
296 break;
297 default:
298 return SEC_E_INTERNAL_ERROR;
301 if (schanCred->cCreds == 0)
302 st = SEC_E_NO_CREDENTIALS;
303 else if (schanCred->cCreds > 1)
304 st = SEC_E_UNKNOWN_CREDENTIALS;
305 else
307 DWORD keySpec;
308 HCRYPTPROV csp;
309 BOOL ret, freeCSP;
311 ret = CryptAcquireCertificatePrivateKey(schanCred->paCred[0],
312 0, /* FIXME: what flags to use? */ NULL,
313 &csp, &keySpec, &freeCSP);
314 if (ret)
316 st = SEC_E_OK;
317 if (freeCSP)
318 CryptReleaseContext(csp, 0);
320 else
321 st = SEC_E_UNKNOWN_CREDENTIALS;
323 return st;
326 static SECURITY_STATUS schan_AcquireClientCredentials(const SCHANNEL_CRED *schanCred,
327 PCredHandle phCredential, PTimeStamp ptsExpiry)
329 struct schan_credentials *creds;
330 SECURITY_STATUS st = SEC_E_OK;
332 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred, phCredential, ptsExpiry);
334 if (schanCred)
336 st = schan_CheckCreds(schanCred);
337 if (st == SEC_E_NO_CREDENTIALS)
338 st = SEC_E_OK;
341 /* For now, the only thing I'm interested in is the direction of the
342 * connection, so just store it.
344 if (st == SEC_E_OK)
346 ULONG_PTR handle;
347 int ret;
349 creds = HeapAlloc(GetProcessHeap(), 0, sizeof(*creds));
350 if (!creds) return SEC_E_INSUFFICIENT_MEMORY;
352 handle = schan_alloc_handle(creds, SCHAN_HANDLE_CRED);
353 if (handle == SCHAN_INVALID_HANDLE) goto fail;
355 creds->credential_use = SECPKG_CRED_OUTBOUND;
356 ret = pgnutls_certificate_allocate_credentials(&creds->credentials);
357 if (ret != GNUTLS_E_SUCCESS)
359 pgnutls_perror(ret);
360 schan_free_handle(handle, SCHAN_HANDLE_CRED);
361 goto fail;
364 phCredential->dwLower = handle;
365 phCredential->dwUpper = 0;
367 /* Outbound credentials have no expiry */
368 if (ptsExpiry)
370 ptsExpiry->LowPart = 0;
371 ptsExpiry->HighPart = 0;
374 return st;
376 fail:
377 HeapFree(GetProcessHeap(), 0, creds);
378 return SEC_E_INTERNAL_ERROR;
381 static SECURITY_STATUS schan_AcquireServerCredentials(const SCHANNEL_CRED *schanCred,
382 PCredHandle phCredential, PTimeStamp ptsExpiry)
384 SECURITY_STATUS st;
386 TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred, phCredential, ptsExpiry);
388 if (!schanCred) return SEC_E_NO_CREDENTIALS;
390 st = schan_CheckCreds(schanCred);
391 if (st == SEC_E_OK)
393 ULONG_PTR handle;
394 struct schan_credentials *creds;
396 creds = HeapAlloc(GetProcessHeap(), 0, sizeof(*creds));
397 if (!creds) return SEC_E_INSUFFICIENT_MEMORY;
398 creds->credential_use = SECPKG_CRED_INBOUND;
400 handle = schan_alloc_handle(creds, SCHAN_HANDLE_CRED);
401 if (handle == SCHAN_INVALID_HANDLE)
403 HeapFree(GetProcessHeap(), 0, creds);
404 return SEC_E_INTERNAL_ERROR;
407 phCredential->dwLower = handle;
408 phCredential->dwUpper = 0;
410 /* FIXME: get expiry from cert */
412 return st;
415 static SECURITY_STATUS schan_AcquireCredentialsHandle(ULONG fCredentialUse,
416 const SCHANNEL_CRED *schanCred, PCredHandle phCredential, PTimeStamp ptsExpiry)
418 SECURITY_STATUS ret;
420 if (fCredentialUse == SECPKG_CRED_OUTBOUND)
421 ret = schan_AcquireClientCredentials(schanCred, phCredential,
422 ptsExpiry);
423 else
424 ret = schan_AcquireServerCredentials(schanCred, phCredential,
425 ptsExpiry);
426 return ret;
429 static SECURITY_STATUS SEC_ENTRY schan_AcquireCredentialsHandleA(
430 SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
431 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
432 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
434 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
435 debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
436 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
437 return schan_AcquireCredentialsHandle(fCredentialUse,
438 pAuthData, phCredential, ptsExpiry);
441 static SECURITY_STATUS SEC_ENTRY schan_AcquireCredentialsHandleW(
442 SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
443 PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
444 PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
446 TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
447 debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
448 pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
449 return schan_AcquireCredentialsHandle(fCredentialUse,
450 pAuthData, phCredential, ptsExpiry);
453 static SECURITY_STATUS SEC_ENTRY schan_FreeCredentialsHandle(
454 PCredHandle phCredential)
456 struct schan_credentials *creds;
458 TRACE("phCredential %p\n", phCredential);
460 if (!phCredential) return SEC_E_INVALID_HANDLE;
462 creds = schan_free_handle(phCredential->dwLower, SCHAN_HANDLE_CRED);
463 if (!creds) return SEC_E_INVALID_HANDLE;
465 if (creds->credential_use == SECPKG_CRED_OUTBOUND)
466 pgnutls_certificate_free_credentials(creds->credentials);
467 HeapFree(GetProcessHeap(), 0, creds);
469 return SEC_E_OK;
472 static void init_schan_buffers(struct schan_buffers *s, const PSecBufferDesc desc,
473 int (*get_next_buffer)(const struct schan_transport *, struct schan_buffers *))
475 s->offset = 0;
476 s->desc = desc;
477 s->current_buffer_idx = -1;
478 s->allow_buffer_resize = FALSE;
479 s->get_next_buffer = get_next_buffer;
482 static int schan_find_sec_buffer_idx(const SecBufferDesc *desc, unsigned int start_idx, ULONG buffer_type)
484 unsigned int i;
485 PSecBuffer buffer;
487 for (i = start_idx; i < desc->cBuffers; ++i)
489 buffer = &desc->pBuffers[i];
490 if (buffer->BufferType == buffer_type) return i;
493 return -1;
496 static void schan_resize_current_buffer(const struct schan_buffers *s, SIZE_T min_size)
498 SecBuffer *b = &s->desc->pBuffers[s->current_buffer_idx];
499 SIZE_T new_size = b->cbBuffer ? b->cbBuffer * 2 : 128;
500 void *new_data;
502 if (b->cbBuffer >= min_size || !s->allow_buffer_resize || min_size > UINT_MAX / 2) return;
504 while (new_size < min_size) new_size *= 2;
506 if (b->pvBuffer)
507 new_data = HeapReAlloc(GetProcessHeap(), 0, b->pvBuffer, new_size);
508 else
509 new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
511 if (!new_data)
513 TRACE("Failed to resize %p from %d to %ld\n", b->pvBuffer, b->cbBuffer, new_size);
514 return;
517 b->cbBuffer = new_size;
518 b->pvBuffer = new_data;
521 static char *schan_get_buffer(const struct schan_transport *t, struct schan_buffers *s, size_t *count)
523 SIZE_T max_count;
524 PSecBuffer buffer;
526 if (!s->desc)
528 TRACE("No desc\n");
529 return NULL;
532 if (s->current_buffer_idx == -1)
534 /* Initial buffer */
535 int buffer_idx = s->get_next_buffer(t, s);
536 if (buffer_idx == -1)
538 TRACE("No next buffer\n");
539 return NULL;
541 s->current_buffer_idx = buffer_idx;
544 buffer = &s->desc->pBuffers[s->current_buffer_idx];
545 TRACE("Using buffer %d: cbBuffer %d, BufferType %#x, pvBuffer %p\n", s->current_buffer_idx, buffer->cbBuffer, buffer->BufferType, buffer->pvBuffer);
547 schan_resize_current_buffer(s, s->offset + *count);
548 max_count = buffer->cbBuffer - s->offset;
549 if (!max_count)
551 int buffer_idx;
553 s->allow_buffer_resize = FALSE;
554 buffer_idx = s->get_next_buffer(t, s);
555 if (buffer_idx == -1)
557 TRACE("No next buffer\n");
558 return NULL;
560 s->current_buffer_idx = buffer_idx;
561 s->offset = 0;
562 return schan_get_buffer(t, s, count);
565 if (*count > max_count) *count = max_count;
566 return (char *)buffer->pvBuffer + s->offset;
569 static ssize_t schan_pull(gnutls_transport_ptr_t transport, void *buff, size_t buff_len)
571 struct schan_transport *t = transport;
572 char *b;
574 TRACE("Pull %zu bytes\n", buff_len);
576 b = schan_get_buffer(t, &t->in, &buff_len);
577 if (!b)
579 pgnutls_transport_set_errno(t->ctx->session, EAGAIN);
580 return -1;
583 memcpy(buff, b, buff_len);
584 t->in.offset += buff_len;
586 TRACE("Read %zu bytes\n", buff_len);
588 return buff_len;
591 static ssize_t schan_push(gnutls_transport_ptr_t transport, const void *buff, size_t buff_len)
593 struct schan_transport *t = transport;
594 char *b;
596 TRACE("Push %zu bytes\n", buff_len);
598 b = schan_get_buffer(t, &t->out, &buff_len);
599 if (!b)
601 pgnutls_transport_set_errno(t->ctx->session, EAGAIN);
602 return -1;
605 memcpy(b, buff, buff_len);
606 t->out.offset += buff_len;
608 TRACE("Wrote %zu bytes\n", buff_len);
610 return buff_len;
613 static int schan_init_sec_ctx_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
615 if (s->current_buffer_idx == -1)
617 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
618 if (t->ctx->req_ctx_attr & ISC_REQ_ALLOCATE_MEMORY)
620 if (idx == -1)
622 idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_EMPTY);
623 if (idx != -1) s->desc->pBuffers[idx].BufferType = SECBUFFER_TOKEN;
625 if (idx != -1 && !s->desc->pBuffers[idx].pvBuffer)
627 s->desc->pBuffers[idx].cbBuffer = 0;
628 s->allow_buffer_resize = TRUE;
631 return idx;
634 return -1;
637 static void dump_buffer_desc(SecBufferDesc *desc)
639 unsigned int i;
641 if (!desc) return;
642 TRACE("Buffer desc %p:\n", desc);
643 for (i = 0; i < desc->cBuffers; ++i)
645 SecBuffer *b = &desc->pBuffers[i];
646 TRACE("\tbuffer %u: cbBuffer %d, BufferType %#x pvBuffer %p\n", i, b->cbBuffer, b->BufferType, b->pvBuffer);
650 /***********************************************************************
651 * InitializeSecurityContextW
653 static SECURITY_STATUS SEC_ENTRY schan_InitializeSecurityContextW(
654 PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
655 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
656 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
657 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
659 SECURITY_STATUS ret;
660 if (phCredential)
662 FIXME("stub\n");
663 ret = SEC_E_UNSUPPORTED_FUNCTION;
665 else
667 ret = SEC_E_INVALID_HANDLE;
669 return ret;
672 /***********************************************************************
673 * InitializeSecurityContextA
675 static SECURITY_STATUS SEC_ENTRY schan_InitializeSecurityContextA(
676 PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
677 ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
678 PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
679 PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
681 SECURITY_STATUS ret;
682 SEC_WCHAR *target_name = NULL;
684 TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
685 debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
686 Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
688 if (pszTargetName)
690 INT len = MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, NULL, 0);
691 target_name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*target_name));
692 MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, target_name, len);
695 ret = schan_InitializeSecurityContextW(phCredential, phContext, target_name,
696 fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
697 phNewContext, pOutput, pfContextAttr, ptsExpiry);
699 HeapFree(GetProcessHeap(), 0, target_name);
701 return ret;
704 static unsigned int schannel_get_cipher_block_size(gnutls_cipher_algorithm_t cipher)
706 const struct
708 gnutls_cipher_algorithm_t cipher;
709 unsigned int block_size;
711 algorithms[] =
713 {GNUTLS_CIPHER_3DES_CBC, 8},
714 {GNUTLS_CIPHER_AES_128_CBC, 16},
715 {GNUTLS_CIPHER_AES_256_CBC, 16},
716 {GNUTLS_CIPHER_ARCFOUR_128, 1},
717 {GNUTLS_CIPHER_ARCFOUR_40, 1},
718 {GNUTLS_CIPHER_DES_CBC, 8},
719 {GNUTLS_CIPHER_NULL, 1},
720 {GNUTLS_CIPHER_RC2_40_CBC, 8},
722 unsigned int i;
724 for (i = 0; i < sizeof(algorithms) / sizeof(*algorithms); ++i)
726 if (algorithms[i].cipher == cipher)
727 return algorithms[i].block_size;
730 FIXME("Unknown cipher %#x, returning 1\n", cipher);
732 return 1;
735 static SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesW(
736 PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
738 struct schan_context *ctx;
740 TRACE("context_handle %p, attribute %#x, buffer %p\n",
741 context_handle, attribute, buffer);
743 if (!context_handle) return SEC_E_INVALID_HANDLE;
744 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
746 switch(attribute)
748 case SECPKG_ATTR_STREAM_SIZES:
750 SecPkgContext_StreamSizes *stream_sizes = buffer;
751 gnutls_mac_algorithm_t mac = pgnutls_mac_get(ctx->session);
752 size_t mac_size = pgnutls_mac_get_key_size(mac);
753 gnutls_cipher_algorithm_t cipher = pgnutls_cipher_get(ctx->session);
754 unsigned int block_size = schannel_get_cipher_block_size(cipher);
756 TRACE("Using %zu mac bytes, block size %u\n", mac_size, block_size);
758 /* These are defined by the TLS RFC */
759 stream_sizes->cbHeader = 5;
760 stream_sizes->cbTrailer = mac_size + 256; /* Max 255 bytes padding + 1 for padding size */
761 stream_sizes->cbMaximumMessage = 1 << 14;
762 stream_sizes->cbBuffers = 4;
763 stream_sizes->cbBlockSize = block_size;
764 return SEC_E_OK;
767 default:
768 FIXME("Unhandled attribute %#x\n", attribute);
769 return SEC_E_UNSUPPORTED_FUNCTION;
773 static SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesA(
774 PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
776 TRACE("context_handle %p, attribute %#x, buffer %p\n",
777 context_handle, attribute, buffer);
779 switch(attribute)
781 case SECPKG_ATTR_STREAM_SIZES:
782 return schan_QueryContextAttributesW(context_handle, attribute, buffer);
784 default:
785 FIXME("Unhandled attribute %#x\n", attribute);
786 return SEC_E_UNSUPPORTED_FUNCTION;
790 static int schan_encrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
792 SecBuffer *b;
794 if (s->current_buffer_idx == -1)
795 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_HEADER);
797 b = &s->desc->pBuffers[s->current_buffer_idx];
799 if (b->BufferType == SECBUFFER_STREAM_HEADER)
800 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
802 if (b->BufferType == SECBUFFER_DATA)
803 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_TRAILER);
805 return -1;
808 static int schan_encrypt_message_get_next_buffer_token(const struct schan_transport *t, struct schan_buffers *s)
810 SecBuffer *b;
812 if (s->current_buffer_idx == -1)
813 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
815 b = &s->desc->pBuffers[s->current_buffer_idx];
817 if (b->BufferType == SECBUFFER_TOKEN)
819 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
820 if (idx != s->current_buffer_idx) return -1;
821 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
824 if (b->BufferType == SECBUFFER_DATA)
826 int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
827 if (idx != -1)
828 idx = schan_find_sec_buffer_idx(s->desc, idx + 1, SECBUFFER_TOKEN);
829 return idx;
832 return -1;
835 static SECURITY_STATUS SEC_ENTRY schan_EncryptMessage(PCtxtHandle context_handle,
836 ULONG quality, PSecBufferDesc message, ULONG message_seq_no)
838 struct schan_transport transport;
839 struct schan_context *ctx;
840 struct schan_buffers *b;
841 SecBuffer *buffer;
842 SIZE_T data_size;
843 char *data;
844 ssize_t sent = 0;
845 ssize_t ret;
846 int idx;
848 TRACE("context_handle %p, quality %d, message %p, message_seq_no %d\n",
849 context_handle, quality, message, message_seq_no);
851 if (!context_handle) return SEC_E_INVALID_HANDLE;
852 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
854 dump_buffer_desc(message);
856 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_DATA);
857 if (idx == -1)
859 WARN("No data buffer passed\n");
860 return SEC_E_INTERNAL_ERROR;
862 buffer = &message->pBuffers[idx];
864 data_size = buffer->cbBuffer;
865 data = HeapAlloc(GetProcessHeap(), 0, data_size);
866 memcpy(data, buffer->pvBuffer, data_size);
868 transport.ctx = ctx;
869 init_schan_buffers(&transport.in, NULL, NULL);
870 if (schan_find_sec_buffer_idx(message, 0, SECBUFFER_STREAM_HEADER) != -1)
871 init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer);
872 else
873 init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer_token);
874 pgnutls_transport_set_ptr(ctx->session, &transport);
876 while (sent < data_size)
878 ret = pgnutls_record_send(ctx->session, data + sent, data_size - sent);
879 if (ret < 0)
881 if (ret != GNUTLS_E_AGAIN)
883 pgnutls_perror(ret);
884 HeapFree(GetProcessHeap(), 0, data);
885 ERR("Returning SEC_E_INTERNAL_ERROR\n");
886 return SEC_E_INTERNAL_ERROR;
888 else break;
890 sent += ret;
893 TRACE("Sent %zd bytes\n", sent);
895 b = &transport.out;
896 b->desc->pBuffers[b->current_buffer_idx].cbBuffer = b->offset;
897 HeapFree(GetProcessHeap(), 0, data);
899 return SEC_E_OK;
902 static int schan_decrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
904 if (s->current_buffer_idx == -1)
905 return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
907 return -1;
910 static SECURITY_STATUS SEC_ENTRY schan_DecryptMessage(PCtxtHandle context_handle,
911 PSecBufferDesc message, ULONG message_seq_no, PULONG quality)
913 struct schan_transport transport;
914 struct schan_context *ctx;
915 SecBuffer *buffer;
916 SIZE_T data_size;
917 char *data;
918 ssize_t received = 0;
919 ssize_t ret;
920 int idx;
922 TRACE("context_handle %p, message %p, message_seq_no %d, quality %p\n",
923 context_handle, message, message_seq_no, quality);
925 if (!context_handle) return SEC_E_INVALID_HANDLE;
926 ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
928 dump_buffer_desc(message);
930 idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_DATA);
931 if (idx == -1)
933 WARN("No data buffer passed\n");
934 return SEC_E_INTERNAL_ERROR;
936 buffer = &message->pBuffers[idx];
938 data_size = buffer->cbBuffer;
939 data = HeapAlloc(GetProcessHeap(), 0, data_size);
941 transport.ctx = ctx;
942 init_schan_buffers(&transport.in, message, schan_decrypt_message_get_next_buffer);
943 init_schan_buffers(&transport.out, NULL, NULL);
944 pgnutls_transport_set_ptr(ctx->session, (gnutls_transport_ptr_t)&transport);
946 while (received < data_size)
948 ret = pgnutls_record_recv(ctx->session, data + received, data_size - received);
949 if (ret < 0)
951 if (ret == GNUTLS_E_AGAIN)
953 if (!received)
955 pgnutls_perror(ret);
956 HeapFree(GetProcessHeap(), 0, data);
957 TRACE("Returning SEC_E_INCOMPLETE_MESSAGE\n");
958 return SEC_E_INCOMPLETE_MESSAGE;
960 break;
962 else
964 pgnutls_perror(ret);
965 HeapFree(GetProcessHeap(), 0, data);
966 ERR("Returning SEC_E_INTERNAL_ERROR\n");
967 return SEC_E_INTERNAL_ERROR;
970 received += ret;
973 TRACE("Received %zd bytes\n", received);
975 memcpy(buffer->pvBuffer, data, received);
976 buffer->cbBuffer = received;
977 HeapFree(GetProcessHeap(), 0, data);
979 return SEC_E_OK;
982 static SECURITY_STATUS SEC_ENTRY schan_DeleteSecurityContext(PCtxtHandle context_handle)
984 struct schan_context *ctx;
986 TRACE("context_handle %p\n", context_handle);
988 if (!context_handle) return SEC_E_INVALID_HANDLE;
990 ctx = schan_free_handle(context_handle->dwLower, SCHAN_HANDLE_CTX);
991 if (!ctx) return SEC_E_INVALID_HANDLE;
993 pgnutls_deinit(ctx->session);
994 HeapFree(GetProcessHeap(), 0, ctx);
996 return SEC_E_OK;
999 static void schan_gnutls_log(int level, const char *msg)
1001 TRACE("<%d> %s", level, msg);
1004 static const SecurityFunctionTableA schanTableA = {
1006 NULL, /* EnumerateSecurityPackagesA */
1007 schan_QueryCredentialsAttributesA,
1008 schan_AcquireCredentialsHandleA,
1009 schan_FreeCredentialsHandle,
1010 NULL, /* Reserved2 */
1011 schan_InitializeSecurityContextA,
1012 NULL, /* AcceptSecurityContext */
1013 NULL, /* CompleteAuthToken */
1014 schan_DeleteSecurityContext,
1015 NULL, /* ApplyControlToken */
1016 schan_QueryContextAttributesA,
1017 NULL, /* ImpersonateSecurityContext */
1018 NULL, /* RevertSecurityContext */
1019 NULL, /* MakeSignature */
1020 NULL, /* VerifySignature */
1021 FreeContextBuffer,
1022 NULL, /* QuerySecurityPackageInfoA */
1023 NULL, /* Reserved3 */
1024 NULL, /* Reserved4 */
1025 NULL, /* ExportSecurityContext */
1026 NULL, /* ImportSecurityContextA */
1027 NULL, /* AddCredentialsA */
1028 NULL, /* Reserved8 */
1029 NULL, /* QuerySecurityContextToken */
1030 schan_EncryptMessage,
1031 schan_DecryptMessage,
1032 NULL, /* SetContextAttributesA */
1035 static const SecurityFunctionTableW schanTableW = {
1037 NULL, /* EnumerateSecurityPackagesW */
1038 schan_QueryCredentialsAttributesW,
1039 schan_AcquireCredentialsHandleW,
1040 schan_FreeCredentialsHandle,
1041 NULL, /* Reserved2 */
1042 schan_InitializeSecurityContextW,
1043 NULL, /* AcceptSecurityContext */
1044 NULL, /* CompleteAuthToken */
1045 schan_DeleteSecurityContext,
1046 NULL, /* ApplyControlToken */
1047 schan_QueryContextAttributesW,
1048 NULL, /* ImpersonateSecurityContext */
1049 NULL, /* RevertSecurityContext */
1050 NULL, /* MakeSignature */
1051 NULL, /* VerifySignature */
1052 FreeContextBuffer,
1053 NULL, /* QuerySecurityPackageInfoW */
1054 NULL, /* Reserved3 */
1055 NULL, /* Reserved4 */
1056 NULL, /* ExportSecurityContext */
1057 NULL, /* ImportSecurityContextW */
1058 NULL, /* AddCredentialsW */
1059 NULL, /* Reserved8 */
1060 NULL, /* QuerySecurityContextToken */
1061 schan_EncryptMessage,
1062 schan_DecryptMessage,
1063 NULL, /* SetContextAttributesW */
1066 static const WCHAR schannelComment[] = { 'S','c','h','a','n','n','e','l',' ',
1067 'S','e','c','u','r','i','t','y',' ','P','a','c','k','a','g','e',0 };
1068 static const WCHAR schannelDllName[] = { 's','c','h','a','n','n','e','l','.','d','l','l',0 };
1070 void SECUR32_initSchannelSP(void)
1072 /* This is what Windows reports. This shouldn't break any applications
1073 * even though the functions are missing, because the wrapper will
1074 * return SEC_E_UNSUPPORTED_FUNCTION if our function is NULL.
1076 static const long caps =
1077 SECPKG_FLAG_INTEGRITY |
1078 SECPKG_FLAG_PRIVACY |
1079 SECPKG_FLAG_CONNECTION |
1080 SECPKG_FLAG_MULTI_REQUIRED |
1081 SECPKG_FLAG_EXTENDED_ERROR |
1082 SECPKG_FLAG_IMPERSONATION |
1083 SECPKG_FLAG_ACCEPT_WIN32_NAME |
1084 SECPKG_FLAG_STREAM;
1085 static const short version = 1;
1086 static const long maxToken = 16384;
1087 SEC_WCHAR *uniSPName = (SEC_WCHAR *)UNISP_NAME_W,
1088 *schannel = (SEC_WCHAR *)SCHANNEL_NAME_W;
1089 const SecPkgInfoW info[] = {
1090 { caps, version, UNISP_RPC_ID, maxToken, uniSPName, uniSPName },
1091 { caps, version, UNISP_RPC_ID, maxToken, schannel,
1092 (SEC_WCHAR *)schannelComment },
1094 SecureProvider *provider;
1095 int ret;
1097 libgnutls_handle = wine_dlopen(SONAME_LIBGNUTLS, RTLD_NOW, NULL, 0);
1098 if (!libgnutls_handle)
1100 WARN("Failed to load libgnutls.\n");
1101 return;
1104 #define LOAD_FUNCPTR(f) \
1105 if (!(p##f = wine_dlsym(libgnutls_handle, #f, NULL, 0))) \
1107 ERR("Failed to load %s\n", #f); \
1108 goto fail; \
1111 LOAD_FUNCPTR(gnutls_alert_get)
1112 LOAD_FUNCPTR(gnutls_alert_get_name)
1113 LOAD_FUNCPTR(gnutls_certificate_allocate_credentials)
1114 LOAD_FUNCPTR(gnutls_certificate_free_credentials)
1115 LOAD_FUNCPTR(gnutls_cipher_get)
1116 LOAD_FUNCPTR(gnutls_credentials_set)
1117 LOAD_FUNCPTR(gnutls_deinit)
1118 LOAD_FUNCPTR(gnutls_global_deinit)
1119 LOAD_FUNCPTR(gnutls_global_init)
1120 LOAD_FUNCPTR(gnutls_global_set_log_function)
1121 LOAD_FUNCPTR(gnutls_global_set_log_level)
1122 LOAD_FUNCPTR(gnutls_handshake)
1123 LOAD_FUNCPTR(gnutls_init)
1124 LOAD_FUNCPTR(gnutls_mac_get)
1125 LOAD_FUNCPTR(gnutls_mac_get_key_size)
1126 LOAD_FUNCPTR(gnutls_perror)
1127 LOAD_FUNCPTR(gnutls_set_default_priority)
1128 LOAD_FUNCPTR(gnutls_record_recv);
1129 LOAD_FUNCPTR(gnutls_record_send);
1130 LOAD_FUNCPTR(gnutls_transport_set_errno)
1131 LOAD_FUNCPTR(gnutls_transport_set_ptr)
1132 LOAD_FUNCPTR(gnutls_transport_set_pull_function)
1133 LOAD_FUNCPTR(gnutls_transport_set_push_function)
1134 #undef LOAD_FUNCPTR
1136 ret = pgnutls_global_init();
1137 if (ret != GNUTLS_E_SUCCESS)
1139 pgnutls_perror(ret);
1140 goto fail;
1143 if (TRACE_ON(secur32))
1145 pgnutls_global_set_log_level(4);
1146 pgnutls_global_set_log_function(schan_gnutls_log);
1149 schan_handle_table = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 64 * sizeof(*schan_handle_table));
1150 if (!schan_handle_table)
1152 ERR("Failed to allocate schannel handle table.\n");
1153 goto fail;
1155 schan_handle_table_size = 64;
1157 provider = SECUR32_addProvider(&schanTableA, &schanTableW, schannelDllName);
1158 if (!provider)
1160 ERR("Failed to add schannel provider.\n");
1161 goto fail;
1164 SECUR32_addPackages(provider, sizeof(info) / sizeof(info[0]), NULL, info);
1166 return;
1168 fail:
1169 HeapFree(GetProcessHeap(), 0, schan_handle_table);
1170 schan_handle_table = NULL;
1171 wine_dlclose(libgnutls_handle, NULL, 0);
1172 libgnutls_handle = NULL;
1173 return;
1176 void SECUR32_deinitSchannelSP(void)
1178 if (!libgnutls_handle) return;
1180 pgnutls_global_deinit();
1181 wine_dlclose(libgnutls_handle, NULL, 0);
1184 #else /* SONAME_LIBGNUTLS */
1186 void SECUR32_initSchannelSP(void) {}
1187 void SECUR32_deinitSchannelSP(void) {}
1189 #endif /* SONAME_LIBGNUTLS */