2005-05-13 19:44:47 +02:00
|
|
|
/* Copyright (C) 2005 Juan Lang
|
2008-10-20 09:26:45 +02:00
|
|
|
* Copyright 2008 Henri Verbeet
|
2005-05-13 19:44:47 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2005-05-13 19:44:47 +02:00
|
|
|
*
|
|
|
|
* This file implements the schannel provider, or, the SSL/TLS implementations.
|
|
|
|
* FIXME: It should be rather obvious that this file is empty of any
|
|
|
|
* implementation.
|
|
|
|
*/
|
2008-09-22 22:13:19 +02:00
|
|
|
#include "config.h"
|
2008-09-22 22:14:04 +02:00
|
|
|
#include "wine/port.h"
|
2008-09-22 22:13:19 +02:00
|
|
|
|
2005-05-13 19:44:47 +02:00
|
|
|
#include <stdarg.h>
|
2008-10-20 09:26:45 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2008-09-22 22:14:04 +02:00
|
|
|
#ifdef SONAME_LIBGNUTLS
|
|
|
|
#include <gnutls/gnutls.h>
|
|
|
|
#endif
|
|
|
|
|
2005-05-13 19:44:47 +02:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
2008-10-06 19:08:47 +02:00
|
|
|
#include "winnls.h"
|
2005-05-13 19:44:47 +02:00
|
|
|
#include "sspi.h"
|
|
|
|
#include "schannel.h"
|
|
|
|
#include "secur32_priv.h"
|
|
|
|
#include "wine/debug.h"
|
2008-09-22 22:14:04 +02:00
|
|
|
#include "wine/library.h"
|
2005-05-13 19:44:47 +02:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(secur32);
|
|
|
|
|
2008-09-22 22:13:19 +02:00
|
|
|
#ifdef SONAME_LIBGNUTLS
|
|
|
|
|
2008-09-22 22:14:04 +02:00
|
|
|
static void *libgnutls_handle;
|
|
|
|
#define MAKE_FUNCPTR(f) static typeof(f) * p##f
|
2008-10-20 09:26:45 +02:00
|
|
|
MAKE_FUNCPTR(gnutls_alert_get);
|
|
|
|
MAKE_FUNCPTR(gnutls_alert_get_name);
|
2008-09-22 22:14:04 +02:00
|
|
|
MAKE_FUNCPTR(gnutls_certificate_allocate_credentials);
|
|
|
|
MAKE_FUNCPTR(gnutls_certificate_free_credentials);
|
2009-08-05 19:43:57 +02:00
|
|
|
MAKE_FUNCPTR(gnutls_certificate_get_peers);
|
2008-12-28 22:21:35 +01:00
|
|
|
MAKE_FUNCPTR(gnutls_cipher_get);
|
2009-08-07 17:38:17 +02:00
|
|
|
MAKE_FUNCPTR(gnutls_cipher_get_key_size);
|
2008-10-20 09:26:45 +02:00
|
|
|
MAKE_FUNCPTR(gnutls_credentials_set);
|
|
|
|
MAKE_FUNCPTR(gnutls_deinit);
|
2008-09-22 22:14:04 +02:00
|
|
|
MAKE_FUNCPTR(gnutls_global_deinit);
|
|
|
|
MAKE_FUNCPTR(gnutls_global_init);
|
2008-09-22 22:14:04 +02:00
|
|
|
MAKE_FUNCPTR(gnutls_global_set_log_function);
|
|
|
|
MAKE_FUNCPTR(gnutls_global_set_log_level);
|
2008-10-20 09:26:45 +02:00
|
|
|
MAKE_FUNCPTR(gnutls_handshake);
|
|
|
|
MAKE_FUNCPTR(gnutls_init);
|
2009-08-07 17:38:17 +02:00
|
|
|
MAKE_FUNCPTR(gnutls_kx_get);
|
2008-12-28 22:21:35 +01:00
|
|
|
MAKE_FUNCPTR(gnutls_mac_get);
|
|
|
|
MAKE_FUNCPTR(gnutls_mac_get_key_size);
|
2008-10-06 19:08:47 +02:00
|
|
|
MAKE_FUNCPTR(gnutls_perror);
|
2009-08-07 17:38:17 +02:00
|
|
|
MAKE_FUNCPTR(gnutls_protocol_get_version);
|
2008-10-20 09:26:45 +02:00
|
|
|
MAKE_FUNCPTR(gnutls_set_default_priority);
|
2008-12-28 22:21:36 +01:00
|
|
|
MAKE_FUNCPTR(gnutls_record_recv);
|
2008-12-28 22:21:35 +01:00
|
|
|
MAKE_FUNCPTR(gnutls_record_send);
|
2008-10-20 09:26:45 +02:00
|
|
|
MAKE_FUNCPTR(gnutls_transport_set_errno);
|
|
|
|
MAKE_FUNCPTR(gnutls_transport_set_ptr);
|
|
|
|
MAKE_FUNCPTR(gnutls_transport_set_pull_function);
|
|
|
|
MAKE_FUNCPTR(gnutls_transport_set_push_function);
|
2008-09-22 22:14:04 +02:00
|
|
|
#undef MAKE_FUNCPTR
|
|
|
|
|
2008-10-06 19:08:36 +02:00
|
|
|
#define SCHAN_INVALID_HANDLE ~0UL
|
|
|
|
|
2008-09-22 22:14:04 +02:00
|
|
|
enum schan_handle_type
|
|
|
|
{
|
|
|
|
SCHAN_HANDLE_CRED,
|
2008-10-20 09:26:45 +02:00
|
|
|
SCHAN_HANDLE_CTX,
|
2008-09-22 22:14:04 +02:00
|
|
|
SCHAN_HANDLE_FREE
|
|
|
|
};
|
|
|
|
|
|
|
|
struct schan_handle
|
|
|
|
{
|
|
|
|
void *object;
|
|
|
|
enum schan_handle_type type;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct schan_credentials
|
|
|
|
{
|
|
|
|
ULONG credential_use;
|
2008-09-24 12:24:01 +02:00
|
|
|
gnutls_certificate_credentials credentials;
|
2008-09-22 22:14:04 +02:00
|
|
|
};
|
|
|
|
|
2008-10-20 09:26:45 +02:00
|
|
|
struct schan_context
|
|
|
|
{
|
|
|
|
gnutls_session_t session;
|
|
|
|
ULONG req_ctx_attr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct schan_transport;
|
|
|
|
|
|
|
|
struct schan_buffers
|
|
|
|
{
|
|
|
|
SIZE_T offset;
|
2010-09-15 19:30:09 +02:00
|
|
|
SIZE_T limit;
|
2008-10-20 09:26:45 +02:00
|
|
|
const SecBufferDesc *desc;
|
|
|
|
int current_buffer_idx;
|
|
|
|
BOOL allow_buffer_resize;
|
|
|
|
int (*get_next_buffer)(const struct schan_transport *, struct schan_buffers *);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct schan_transport
|
|
|
|
{
|
|
|
|
struct schan_context *ctx;
|
|
|
|
struct schan_buffers in;
|
|
|
|
struct schan_buffers out;
|
|
|
|
};
|
|
|
|
|
2008-09-22 22:14:04 +02:00
|
|
|
static struct schan_handle *schan_handle_table;
|
|
|
|
static struct schan_handle *schan_free_handles;
|
|
|
|
static SIZE_T schan_handle_table_size;
|
|
|
|
static SIZE_T schan_handle_count;
|
|
|
|
|
|
|
|
static ULONG_PTR schan_alloc_handle(void *object, enum schan_handle_type type)
|
|
|
|
{
|
|
|
|
struct schan_handle *handle;
|
|
|
|
|
|
|
|
if (schan_free_handles)
|
|
|
|
{
|
2010-04-09 16:14:07 +02:00
|
|
|
DWORD index = schan_free_handles - schan_handle_table;
|
2008-09-22 22:14:04 +02:00
|
|
|
/* Use a free handle */
|
|
|
|
handle = schan_free_handles;
|
|
|
|
if (handle->type != SCHAN_HANDLE_FREE)
|
|
|
|
{
|
2010-04-09 16:14:07 +02:00
|
|
|
ERR("Handle %d(%p) is in the free list, but has type %#x.\n", index, handle, handle->type);
|
2008-10-06 19:08:36 +02:00
|
|
|
return SCHAN_INVALID_HANDLE;
|
2008-09-22 22:14:04 +02:00
|
|
|
}
|
2009-01-22 09:52:28 +01:00
|
|
|
schan_free_handles = handle->object;
|
2008-09-22 22:14:04 +02:00
|
|
|
handle->object = object;
|
|
|
|
handle->type = type;
|
|
|
|
|
2010-04-09 16:14:07 +02:00
|
|
|
return index;
|
2008-09-22 22:14:04 +02:00
|
|
|
}
|
|
|
|
if (!(schan_handle_count < schan_handle_table_size))
|
|
|
|
{
|
|
|
|
/* Grow the table */
|
|
|
|
SIZE_T new_size = schan_handle_table_size + (schan_handle_table_size >> 1);
|
|
|
|
struct schan_handle *new_table = HeapReAlloc(GetProcessHeap(), 0, schan_handle_table, new_size * sizeof(*schan_handle_table));
|
|
|
|
if (!new_table)
|
|
|
|
{
|
|
|
|
ERR("Failed to grow the handle table\n");
|
2008-10-06 19:08:36 +02:00
|
|
|
return SCHAN_INVALID_HANDLE;
|
2008-09-22 22:14:04 +02:00
|
|
|
}
|
|
|
|
schan_handle_table = new_table;
|
|
|
|
schan_handle_table_size = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle = &schan_handle_table[schan_handle_count++];
|
|
|
|
handle->object = object;
|
|
|
|
handle->type = type;
|
|
|
|
|
|
|
|
return handle - schan_handle_table;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *schan_free_handle(ULONG_PTR handle_idx, enum schan_handle_type type)
|
|
|
|
{
|
|
|
|
struct schan_handle *handle;
|
|
|
|
void *object;
|
|
|
|
|
2008-10-06 19:08:36 +02:00
|
|
|
if (handle_idx == SCHAN_INVALID_HANDLE) return NULL;
|
2009-08-04 19:44:06 +02:00
|
|
|
if (handle_idx >= schan_handle_count) return NULL;
|
2008-09-22 22:14:04 +02:00
|
|
|
handle = &schan_handle_table[handle_idx];
|
|
|
|
if (handle->type != type)
|
|
|
|
{
|
|
|
|
ERR("Handle %ld(%p) is not of type %#x\n", handle_idx, handle, type);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
object = handle->object;
|
|
|
|
handle->object = schan_free_handles;
|
|
|
|
handle->type = SCHAN_HANDLE_FREE;
|
|
|
|
schan_free_handles = handle;
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2008-10-20 09:26:45 +02:00
|
|
|
static void *schan_get_object(ULONG_PTR handle_idx, enum schan_handle_type type)
|
|
|
|
{
|
|
|
|
struct schan_handle *handle;
|
|
|
|
|
|
|
|
if (handle_idx == SCHAN_INVALID_HANDLE) return NULL;
|
2009-08-04 19:44:06 +02:00
|
|
|
if (handle_idx >= schan_handle_count) return NULL;
|
2008-10-20 09:26:45 +02:00
|
|
|
handle = &schan_handle_table[handle_idx];
|
|
|
|
if (handle->type != type)
|
|
|
|
{
|
|
|
|
ERR("Handle %ld(%p) is not of type %#x\n", handle_idx, handle, type);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return handle->object;
|
|
|
|
}
|
|
|
|
|
2005-05-13 19:44:47 +02:00
|
|
|
static SECURITY_STATUS schan_QueryCredentialsAttributes(
|
2008-10-20 00:15:12 +02:00
|
|
|
PCredHandle phCredential, ULONG ulAttribute, VOID *pBuffer)
|
2005-05-13 19:44:47 +02:00
|
|
|
{
|
|
|
|
SECURITY_STATUS ret;
|
|
|
|
|
|
|
|
switch (ulAttribute)
|
|
|
|
{
|
|
|
|
case SECPKG_ATTR_SUPPORTED_ALGS:
|
|
|
|
if (pBuffer)
|
|
|
|
{
|
|
|
|
/* FIXME: get from CryptoAPI */
|
2008-03-12 18:35:10 +01:00
|
|
|
FIXME("SECPKG_ATTR_SUPPORTED_ALGS: stub\n");
|
2005-05-13 19:44:47 +02:00
|
|
|
ret = SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = SEC_E_INTERNAL_ERROR;
|
|
|
|
break;
|
|
|
|
case SECPKG_ATTR_CIPHER_STRENGTHS:
|
|
|
|
if (pBuffer)
|
|
|
|
{
|
2009-01-22 09:52:28 +01:00
|
|
|
SecPkgCred_CipherStrengths *r = pBuffer;
|
2008-03-12 18:35:10 +01:00
|
|
|
|
2005-05-13 19:44:47 +02:00
|
|
|
/* FIXME: get from CryptoAPI */
|
2008-03-12 18:35:10 +01:00
|
|
|
FIXME("SECPKG_ATTR_CIPHER_STRENGTHS: semi-stub\n");
|
|
|
|
r->dwMinimumCipherStrength = 40;
|
|
|
|
r->dwMaximumCipherStrength = 168;
|
|
|
|
ret = SEC_E_OK;
|
2005-05-13 19:44:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = SEC_E_INTERNAL_ERROR;
|
|
|
|
break;
|
|
|
|
case SECPKG_ATTR_SUPPORTED_PROTOCOLS:
|
|
|
|
if (pBuffer)
|
|
|
|
{
|
|
|
|
/* FIXME: get from OpenSSL? */
|
2008-03-12 18:35:10 +01:00
|
|
|
FIXME("SECPKG_ATTR_SUPPORTED_PROTOCOLS: stub\n");
|
2005-05-13 19:44:47 +02:00
|
|
|
ret = SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = SEC_E_INTERNAL_ERROR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SECURITY_STATUS SEC_ENTRY schan_QueryCredentialsAttributesA(
|
|
|
|
PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS ret;
|
|
|
|
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
|
2005-05-13 19:44:47 +02:00
|
|
|
|
|
|
|
switch (ulAttribute)
|
|
|
|
{
|
|
|
|
case SECPKG_CRED_ATTR_NAMES:
|
|
|
|
FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
|
|
|
|
ret = SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = schan_QueryCredentialsAttributes(phCredential, ulAttribute,
|
|
|
|
pBuffer);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SECURITY_STATUS SEC_ENTRY schan_QueryCredentialsAttributesW(
|
|
|
|
PCredHandle phCredential, ULONG ulAttribute, PVOID pBuffer)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS ret;
|
|
|
|
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("(%p, %d, %p)\n", phCredential, ulAttribute, pBuffer);
|
2005-05-13 19:44:47 +02:00
|
|
|
|
|
|
|
switch (ulAttribute)
|
|
|
|
{
|
|
|
|
case SECPKG_CRED_ATTR_NAMES:
|
|
|
|
FIXME("SECPKG_CRED_ATTR_NAMES: stub\n");
|
|
|
|
ret = SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = schan_QueryCredentialsAttributes(phCredential, ulAttribute,
|
|
|
|
pBuffer);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-08-25 23:21:07 +02:00
|
|
|
static SECURITY_STATUS schan_CheckCreds(const SCHANNEL_CRED *schanCred)
|
2005-05-13 19:44:47 +02:00
|
|
|
{
|
2006-07-28 08:39:56 +02:00
|
|
|
SECURITY_STATUS st;
|
2009-08-06 00:13:33 +02:00
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
TRACE("dwVersion = %d\n", schanCred->dwVersion);
|
|
|
|
TRACE("cCreds = %d\n", schanCred->cCreds);
|
|
|
|
TRACE("hRootStore = %p\n", schanCred->hRootStore);
|
|
|
|
TRACE("cMappers = %d\n", schanCred->cMappers);
|
|
|
|
TRACE("cSupportedAlgs = %d:\n", schanCred->cSupportedAlgs);
|
|
|
|
for (i = 0; i < schanCred->cSupportedAlgs; i++)
|
|
|
|
TRACE("%08x\n", schanCred->palgSupportedAlgs[i]);
|
|
|
|
TRACE("grbitEnabledProtocols = %08x\n", schanCred->grbitEnabledProtocols);
|
|
|
|
TRACE("dwMinimumCipherStrength = %d\n", schanCred->dwMinimumCipherStrength);
|
|
|
|
TRACE("dwMaximumCipherStrength = %d\n", schanCred->dwMaximumCipherStrength);
|
|
|
|
TRACE("dwSessionLifespan = %d\n", schanCred->dwSessionLifespan);
|
|
|
|
TRACE("dwFlags = %08x\n", schanCred->dwFlags);
|
|
|
|
TRACE("dwCredFormat = %d\n", schanCred->dwCredFormat);
|
2005-05-13 19:44:47 +02:00
|
|
|
|
2006-07-28 08:39:56 +02:00
|
|
|
switch (schanCred->dwVersion)
|
|
|
|
{
|
|
|
|
case SCH_CRED_V3:
|
|
|
|
case SCHANNEL_CRED_VERSION:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (schanCred->cCreds == 0)
|
|
|
|
st = SEC_E_NO_CREDENTIALS;
|
|
|
|
else if (schanCred->cCreds > 1)
|
|
|
|
st = SEC_E_UNKNOWN_CREDENTIALS;
|
2005-05-13 19:44:47 +02:00
|
|
|
else
|
|
|
|
{
|
2006-07-28 08:39:56 +02:00
|
|
|
DWORD keySpec;
|
|
|
|
HCRYPTPROV csp;
|
|
|
|
BOOL ret, freeCSP;
|
|
|
|
|
|
|
|
ret = CryptAcquireCertificatePrivateKey(schanCred->paCred[0],
|
|
|
|
0, /* FIXME: what flags to use? */ NULL,
|
|
|
|
&csp, &keySpec, &freeCSP);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
st = SEC_E_OK;
|
|
|
|
if (freeCSP)
|
|
|
|
CryptReleaseContext(csp, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
st = SEC_E_UNKNOWN_CREDENTIALS;
|
|
|
|
}
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2007-08-25 23:21:07 +02:00
|
|
|
static SECURITY_STATUS schan_AcquireClientCredentials(const SCHANNEL_CRED *schanCred,
|
2006-07-28 08:39:56 +02:00
|
|
|
PCredHandle phCredential, PTimeStamp ptsExpiry)
|
|
|
|
{
|
2008-10-06 19:08:47 +02:00
|
|
|
struct schan_credentials *creds;
|
2006-07-28 08:39:56 +02:00
|
|
|
SECURITY_STATUS st = SEC_E_OK;
|
|
|
|
|
2008-09-22 22:14:04 +02:00
|
|
|
TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred, phCredential, ptsExpiry);
|
|
|
|
|
2006-07-28 08:39:56 +02:00
|
|
|
if (schanCred)
|
|
|
|
{
|
|
|
|
st = schan_CheckCreds(schanCred);
|
|
|
|
if (st == SEC_E_NO_CREDENTIALS)
|
|
|
|
st = SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For now, the only thing I'm interested in is the direction of the
|
|
|
|
* connection, so just store it.
|
|
|
|
*/
|
|
|
|
if (st == SEC_E_OK)
|
|
|
|
{
|
2008-09-22 22:14:04 +02:00
|
|
|
ULONG_PTR handle;
|
2008-10-06 19:08:47 +02:00
|
|
|
int ret;
|
2008-09-22 22:14:04 +02:00
|
|
|
|
|
|
|
creds = HeapAlloc(GetProcessHeap(), 0, sizeof(*creds));
|
|
|
|
if (!creds) return SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
|
|
|
|
handle = schan_alloc_handle(creds, SCHAN_HANDLE_CRED);
|
2008-10-06 19:08:47 +02:00
|
|
|
if (handle == SCHAN_INVALID_HANDLE) goto fail;
|
2008-09-22 22:14:04 +02:00
|
|
|
|
|
|
|
creds->credential_use = SECPKG_CRED_OUTBOUND;
|
2008-10-06 19:08:47 +02:00
|
|
|
ret = pgnutls_certificate_allocate_credentials(&creds->credentials);
|
|
|
|
if (ret != GNUTLS_E_SUCCESS)
|
|
|
|
{
|
|
|
|
pgnutls_perror(ret);
|
2008-10-20 00:15:13 +02:00
|
|
|
schan_free_handle(handle, SCHAN_HANDLE_CRED);
|
2008-10-06 19:08:47 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2008-09-22 22:14:04 +02:00
|
|
|
|
|
|
|
phCredential->dwLower = handle;
|
|
|
|
phCredential->dwUpper = 0;
|
|
|
|
|
2006-07-28 08:39:56 +02:00
|
|
|
/* Outbound credentials have no expiry */
|
2005-05-13 19:44:47 +02:00
|
|
|
if (ptsExpiry)
|
2006-06-19 12:09:56 +02:00
|
|
|
{
|
|
|
|
ptsExpiry->LowPart = 0;
|
|
|
|
ptsExpiry->HighPart = 0;
|
|
|
|
}
|
2005-05-13 19:44:47 +02:00
|
|
|
}
|
2006-07-28 08:39:56 +02:00
|
|
|
return st;
|
2008-10-06 19:08:47 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
HeapFree(GetProcessHeap(), 0, creds);
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
2006-07-28 08:39:56 +02:00
|
|
|
}
|
|
|
|
|
2007-08-25 23:21:07 +02:00
|
|
|
static SECURITY_STATUS schan_AcquireServerCredentials(const SCHANNEL_CRED *schanCred,
|
2006-07-28 08:39:56 +02:00
|
|
|
PCredHandle phCredential, PTimeStamp ptsExpiry)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS st;
|
|
|
|
|
2008-09-22 22:14:04 +02:00
|
|
|
TRACE("schanCred %p, phCredential %p, ptsExpiry %p\n", schanCred, phCredential, ptsExpiry);
|
|
|
|
|
2006-07-28 08:39:56 +02:00
|
|
|
if (!schanCred) return SEC_E_NO_CREDENTIALS;
|
|
|
|
|
|
|
|
st = schan_CheckCreds(schanCred);
|
|
|
|
if (st == SEC_E_OK)
|
|
|
|
{
|
2008-09-22 22:14:04 +02:00
|
|
|
ULONG_PTR handle;
|
|
|
|
struct schan_credentials *creds;
|
|
|
|
|
|
|
|
creds = HeapAlloc(GetProcessHeap(), 0, sizeof(*creds));
|
|
|
|
if (!creds) return SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
creds->credential_use = SECPKG_CRED_INBOUND;
|
|
|
|
|
|
|
|
handle = schan_alloc_handle(creds, SCHAN_HANDLE_CRED);
|
2008-10-06 19:08:36 +02:00
|
|
|
if (handle == SCHAN_INVALID_HANDLE)
|
2008-09-22 22:14:04 +02:00
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, creds);
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
phCredential->dwLower = handle;
|
|
|
|
phCredential->dwUpper = 0;
|
|
|
|
|
2006-07-28 08:39:56 +02:00
|
|
|
/* FIXME: get expiry from cert */
|
|
|
|
}
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SECURITY_STATUS schan_AcquireCredentialsHandle(ULONG fCredentialUse,
|
2007-08-25 23:21:07 +02:00
|
|
|
const SCHANNEL_CRED *schanCred, PCredHandle phCredential, PTimeStamp ptsExpiry)
|
2006-07-28 08:39:56 +02:00
|
|
|
{
|
|
|
|
SECURITY_STATUS ret;
|
|
|
|
|
|
|
|
if (fCredentialUse == SECPKG_CRED_OUTBOUND)
|
|
|
|
ret = schan_AcquireClientCredentials(schanCred, phCredential,
|
|
|
|
ptsExpiry);
|
|
|
|
else
|
|
|
|
ret = schan_AcquireServerCredentials(schanCred, phCredential,
|
|
|
|
ptsExpiry);
|
2005-05-13 19:44:47 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SECURITY_STATUS SEC_ENTRY schan_AcquireCredentialsHandleA(
|
|
|
|
SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
|
|
|
|
PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
|
|
|
|
PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
|
|
|
|
{
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
|
2005-05-13 19:44:47 +02:00
|
|
|
debugstr_a(pszPrincipal), debugstr_a(pszPackage), fCredentialUse,
|
|
|
|
pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
|
2006-07-28 08:39:56 +02:00
|
|
|
return schan_AcquireCredentialsHandle(fCredentialUse,
|
2009-01-22 09:52:28 +01:00
|
|
|
pAuthData, phCredential, ptsExpiry);
|
2005-05-13 19:44:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static SECURITY_STATUS SEC_ENTRY schan_AcquireCredentialsHandleW(
|
|
|
|
SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
|
|
|
|
PLUID pLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
|
|
|
|
PVOID pGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
|
|
|
|
{
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("(%s, %s, 0x%08x, %p, %p, %p, %p, %p, %p)\n",
|
2005-05-13 19:44:47 +02:00
|
|
|
debugstr_w(pszPrincipal), debugstr_w(pszPackage), fCredentialUse,
|
|
|
|
pLogonID, pAuthData, pGetKeyFn, pGetKeyArgument, phCredential, ptsExpiry);
|
2006-07-28 08:39:56 +02:00
|
|
|
return schan_AcquireCredentialsHandle(fCredentialUse,
|
2009-01-22 09:52:28 +01:00
|
|
|
pAuthData, phCredential, ptsExpiry);
|
2006-07-28 08:39:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static SECURITY_STATUS SEC_ENTRY schan_FreeCredentialsHandle(
|
|
|
|
PCredHandle phCredential)
|
|
|
|
{
|
2008-09-22 22:14:04 +02:00
|
|
|
struct schan_credentials *creds;
|
|
|
|
|
|
|
|
TRACE("phCredential %p\n", phCredential);
|
|
|
|
|
|
|
|
if (!phCredential) return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
creds = schan_free_handle(phCredential->dwLower, SCHAN_HANDLE_CRED);
|
|
|
|
if (!creds) return SEC_E_INVALID_HANDLE;
|
|
|
|
|
2008-09-22 22:14:04 +02:00
|
|
|
if (creds->credential_use == SECPKG_CRED_OUTBOUND)
|
|
|
|
pgnutls_certificate_free_credentials(creds->credentials);
|
2008-09-22 22:14:04 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, creds);
|
|
|
|
|
2006-07-28 08:39:56 +02:00
|
|
|
return SEC_E_OK;
|
2005-05-13 19:44:47 +02:00
|
|
|
}
|
|
|
|
|
2008-10-20 09:26:45 +02:00
|
|
|
static void init_schan_buffers(struct schan_buffers *s, const PSecBufferDesc desc,
|
|
|
|
int (*get_next_buffer)(const struct schan_transport *, struct schan_buffers *))
|
|
|
|
{
|
|
|
|
s->offset = 0;
|
2010-09-15 19:30:09 +02:00
|
|
|
s->limit = 0;
|
2008-10-20 09:26:45 +02:00
|
|
|
s->desc = desc;
|
|
|
|
s->current_buffer_idx = -1;
|
|
|
|
s->allow_buffer_resize = FALSE;
|
|
|
|
s->get_next_buffer = get_next_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int schan_find_sec_buffer_idx(const SecBufferDesc *desc, unsigned int start_idx, ULONG buffer_type)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
PSecBuffer buffer;
|
|
|
|
|
|
|
|
for (i = start_idx; i < desc->cBuffers; ++i)
|
|
|
|
{
|
|
|
|
buffer = &desc->pBuffers[i];
|
|
|
|
if (buffer->BufferType == buffer_type) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void schan_resize_current_buffer(const struct schan_buffers *s, SIZE_T min_size)
|
|
|
|
{
|
|
|
|
SecBuffer *b = &s->desc->pBuffers[s->current_buffer_idx];
|
|
|
|
SIZE_T new_size = b->cbBuffer ? b->cbBuffer * 2 : 128;
|
|
|
|
void *new_data;
|
|
|
|
|
|
|
|
if (b->cbBuffer >= min_size || !s->allow_buffer_resize || min_size > UINT_MAX / 2) return;
|
|
|
|
|
|
|
|
while (new_size < min_size) new_size *= 2;
|
|
|
|
|
|
|
|
if (b->pvBuffer)
|
|
|
|
new_data = HeapReAlloc(GetProcessHeap(), 0, b->pvBuffer, new_size);
|
|
|
|
else
|
|
|
|
new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
|
|
|
|
|
|
|
|
if (!new_data)
|
|
|
|
{
|
2009-01-06 00:06:04 +01:00
|
|
|
TRACE("Failed to resize %p from %d to %ld\n", b->pvBuffer, b->cbBuffer, new_size);
|
2008-10-20 09:26:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
b->cbBuffer = new_size;
|
|
|
|
b->pvBuffer = new_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *schan_get_buffer(const struct schan_transport *t, struct schan_buffers *s, size_t *count)
|
|
|
|
{
|
|
|
|
SIZE_T max_count;
|
|
|
|
PSecBuffer buffer;
|
|
|
|
|
|
|
|
if (!s->desc)
|
|
|
|
{
|
|
|
|
TRACE("No desc\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->current_buffer_idx == -1)
|
|
|
|
{
|
|
|
|
/* Initial buffer */
|
|
|
|
int buffer_idx = s->get_next_buffer(t, s);
|
|
|
|
if (buffer_idx == -1)
|
|
|
|
{
|
|
|
|
TRACE("No next buffer\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
s->current_buffer_idx = buffer_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = &s->desc->pBuffers[s->current_buffer_idx];
|
2009-01-06 00:06:04 +01:00
|
|
|
TRACE("Using buffer %d: cbBuffer %d, BufferType %#x, pvBuffer %p\n", s->current_buffer_idx, buffer->cbBuffer, buffer->BufferType, buffer->pvBuffer);
|
2008-10-20 09:26:45 +02:00
|
|
|
|
|
|
|
schan_resize_current_buffer(s, s->offset + *count);
|
|
|
|
max_count = buffer->cbBuffer - s->offset;
|
|
|
|
if (!max_count)
|
|
|
|
{
|
|
|
|
int buffer_idx;
|
|
|
|
|
|
|
|
s->allow_buffer_resize = FALSE;
|
|
|
|
buffer_idx = s->get_next_buffer(t, s);
|
|
|
|
if (buffer_idx == -1)
|
|
|
|
{
|
|
|
|
TRACE("No next buffer\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
s->current_buffer_idx = buffer_idx;
|
|
|
|
s->offset = 0;
|
|
|
|
return schan_get_buffer(t, s, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*count > max_count) *count = max_count;
|
|
|
|
return (char *)buffer->pvBuffer + s->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t schan_pull(gnutls_transport_ptr_t transport, void *buff, size_t buff_len)
|
|
|
|
{
|
2009-01-22 09:52:28 +01:00
|
|
|
struct schan_transport *t = transport;
|
2008-10-20 09:26:45 +02:00
|
|
|
char *b;
|
|
|
|
|
|
|
|
TRACE("Pull %zu bytes\n", buff_len);
|
|
|
|
|
|
|
|
b = schan_get_buffer(t, &t->in, &buff_len);
|
|
|
|
if (!b)
|
|
|
|
{
|
|
|
|
pgnutls_transport_set_errno(t->ctx->session, EAGAIN);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-15 19:30:09 +02:00
|
|
|
if (t->in.limit != 0 && t->in.offset + buff_len >= t->in.limit)
|
|
|
|
{
|
|
|
|
buff_len = t->in.limit - t->in.offset;
|
|
|
|
if (buff_len == 0)
|
|
|
|
{
|
|
|
|
pgnutls_transport_set_errno(t->ctx->session, EAGAIN);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-20 09:26:45 +02:00
|
|
|
memcpy(buff, b, buff_len);
|
|
|
|
t->in.offset += buff_len;
|
|
|
|
|
|
|
|
TRACE("Read %zu bytes\n", buff_len);
|
|
|
|
|
|
|
|
return buff_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t schan_push(gnutls_transport_ptr_t transport, const void *buff, size_t buff_len)
|
|
|
|
{
|
2009-01-22 09:52:28 +01:00
|
|
|
struct schan_transport *t = transport;
|
2008-10-20 09:26:45 +02:00
|
|
|
char *b;
|
|
|
|
|
|
|
|
TRACE("Push %zu bytes\n", buff_len);
|
|
|
|
|
|
|
|
b = schan_get_buffer(t, &t->out, &buff_len);
|
|
|
|
if (!b)
|
|
|
|
{
|
|
|
|
pgnutls_transport_set_errno(t->ctx->session, EAGAIN);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(b, buff, buff_len);
|
|
|
|
t->out.offset += buff_len;
|
|
|
|
|
|
|
|
TRACE("Wrote %zu bytes\n", buff_len);
|
|
|
|
|
|
|
|
return buff_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int schan_init_sec_ctx_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
|
|
|
|
{
|
|
|
|
if (s->current_buffer_idx == -1)
|
|
|
|
{
|
|
|
|
int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
|
2008-12-28 22:21:35 +01:00
|
|
|
if (t->ctx->req_ctx_attr & ISC_REQ_ALLOCATE_MEMORY)
|
|
|
|
{
|
|
|
|
if (idx == -1)
|
|
|
|
{
|
|
|
|
idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_EMPTY);
|
|
|
|
if (idx != -1) s->desc->pBuffers[idx].BufferType = SECBUFFER_TOKEN;
|
|
|
|
}
|
2009-05-25 09:17:21 +02:00
|
|
|
if (idx != -1 && !s->desc->pBuffers[idx].pvBuffer)
|
|
|
|
{
|
|
|
|
s->desc->pBuffers[idx].cbBuffer = 0;
|
|
|
|
s->allow_buffer_resize = TRUE;
|
|
|
|
}
|
2008-12-28 22:21:35 +01:00
|
|
|
}
|
2008-10-20 09:26:45 +02:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-12-28 22:21:35 +01:00
|
|
|
static void dump_buffer_desc(SecBufferDesc *desc)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!desc) return;
|
|
|
|
TRACE("Buffer desc %p:\n", desc);
|
|
|
|
for (i = 0; i < desc->cBuffers; ++i)
|
|
|
|
{
|
|
|
|
SecBuffer *b = &desc->pBuffers[i];
|
2009-01-06 00:06:04 +01:00
|
|
|
TRACE("\tbuffer %u: cbBuffer %d, BufferType %#x pvBuffer %p\n", i, b->cbBuffer, b->BufferType, b->pvBuffer);
|
2008-12-28 22:21:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-14 17:37:36 +01:00
|
|
|
/***********************************************************************
|
2008-10-06 19:08:47 +02:00
|
|
|
* InitializeSecurityContextW
|
2006-02-14 17:37:36 +01:00
|
|
|
*/
|
2008-10-06 19:08:47 +02:00
|
|
|
static SECURITY_STATUS SEC_ENTRY schan_InitializeSecurityContextW(
|
|
|
|
PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR *pszTargetName,
|
2006-02-14 17:37:36 +01:00
|
|
|
ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
|
|
|
|
PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
|
|
|
|
PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
|
|
|
|
{
|
2008-10-20 09:26:45 +02:00
|
|
|
struct schan_context *ctx;
|
|
|
|
struct schan_buffers *out_buffers;
|
|
|
|
struct schan_credentials *cred;
|
|
|
|
struct schan_transport transport;
|
|
|
|
int err;
|
2006-02-14 17:37:36 +01:00
|
|
|
|
2010-10-22 01:37:53 +02:00
|
|
|
TRACE("%p %p %s 0x%08x %d %d %p %d %p %p %p %p\n", phCredential, phContext,
|
2008-10-06 19:08:47 +02:00
|
|
|
debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
|
2006-02-14 17:37:36 +01:00
|
|
|
Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
|
2008-10-20 09:26:45 +02:00
|
|
|
|
2008-12-28 22:21:35 +01:00
|
|
|
dump_buffer_desc(pInput);
|
|
|
|
dump_buffer_desc(pOutput);
|
|
|
|
|
2008-10-20 09:26:45 +02:00
|
|
|
if (!phContext)
|
2006-07-28 08:39:56 +02:00
|
|
|
{
|
2008-10-20 09:26:45 +02:00
|
|
|
ULONG_PTR handle;
|
|
|
|
|
|
|
|
if (!phCredential) return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
cred = schan_get_object(phCredential->dwLower, SCHAN_HANDLE_CRED);
|
|
|
|
if (!cred) return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
if (!(cred->credential_use & SECPKG_CRED_OUTBOUND))
|
|
|
|
{
|
|
|
|
WARN("Invalid credential use %#x\n", cred->credential_use);
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = HeapAlloc(GetProcessHeap(), 0, sizeof(*ctx));
|
|
|
|
if (!ctx) return SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
|
|
|
|
handle = schan_alloc_handle(ctx, SCHAN_HANDLE_CTX);
|
|
|
|
if (handle == SCHAN_INVALID_HANDLE)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, ctx);
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgnutls_init(&ctx->session, GNUTLS_CLIENT);
|
|
|
|
if (err != GNUTLS_E_SUCCESS)
|
|
|
|
{
|
|
|
|
pgnutls_perror(err);
|
|
|
|
schan_free_handle(handle, SCHAN_HANDLE_CTX);
|
|
|
|
HeapFree(GetProcessHeap(), 0, ctx);
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: We should be using the information from the credentials here. */
|
|
|
|
FIXME("Using hardcoded \"NORMAL\" priority\n");
|
|
|
|
err = pgnutls_set_default_priority(ctx->session);
|
|
|
|
if (err != GNUTLS_E_SUCCESS)
|
|
|
|
{
|
|
|
|
pgnutls_perror(err);
|
|
|
|
pgnutls_deinit(ctx->session);
|
|
|
|
schan_free_handle(handle, SCHAN_HANDLE_CTX);
|
|
|
|
HeapFree(GetProcessHeap(), 0, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgnutls_credentials_set(ctx->session, GNUTLS_CRD_CERTIFICATE, cred->credentials);
|
|
|
|
if (err != GNUTLS_E_SUCCESS)
|
|
|
|
{
|
|
|
|
pgnutls_perror(err);
|
|
|
|
pgnutls_deinit(ctx->session);
|
|
|
|
schan_free_handle(handle, SCHAN_HANDLE_CTX);
|
|
|
|
HeapFree(GetProcessHeap(), 0, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
pgnutls_transport_set_pull_function(ctx->session, schan_pull);
|
|
|
|
pgnutls_transport_set_push_function(ctx->session, schan_push);
|
|
|
|
|
|
|
|
phNewContext->dwLower = handle;
|
|
|
|
phNewContext->dwUpper = 0;
|
2006-02-14 17:37:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-10-20 09:26:45 +02:00
|
|
|
ctx = schan_get_object(phContext->dwLower, SCHAN_HANDLE_CTX);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->req_ctx_attr = fContextReq;
|
|
|
|
|
|
|
|
transport.ctx = ctx;
|
|
|
|
init_schan_buffers(&transport.in, pInput, schan_init_sec_ctx_get_next_buffer);
|
|
|
|
init_schan_buffers(&transport.out, pOutput, schan_init_sec_ctx_get_next_buffer);
|
|
|
|
pgnutls_transport_set_ptr(ctx->session, &transport);
|
|
|
|
|
|
|
|
/* Perform the TLS handshake */
|
|
|
|
err = pgnutls_handshake(ctx->session);
|
|
|
|
|
2010-09-17 10:18:41 +02:00
|
|
|
if(transport.in.offset && transport.in.offset != pInput->pBuffers[0].cbBuffer) {
|
|
|
|
if(pInput->cBuffers<2 || pInput->pBuffers[1].BufferType!=SECBUFFER_EMPTY)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
|
|
|
pInput->pBuffers[1].BufferType = SECBUFFER_EXTRA;
|
|
|
|
pInput->pBuffers[1].cbBuffer = pInput->pBuffers[0].cbBuffer-transport.in.offset;
|
|
|
|
}
|
|
|
|
|
2008-10-20 09:26:45 +02:00
|
|
|
out_buffers = &transport.out;
|
|
|
|
if (out_buffers->current_buffer_idx != -1)
|
|
|
|
{
|
|
|
|
SecBuffer *buffer = &out_buffers->desc->pBuffers[out_buffers->current_buffer_idx];
|
|
|
|
buffer->cbBuffer = out_buffers->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pfContextAttr = 0;
|
|
|
|
if (ctx->req_ctx_attr & ISC_REQ_ALLOCATE_MEMORY)
|
|
|
|
*pfContextAttr |= ISC_RET_ALLOCATED_MEMORY;
|
|
|
|
|
|
|
|
switch(err)
|
|
|
|
{
|
|
|
|
case GNUTLS_E_SUCCESS:
|
|
|
|
TRACE("Handshake completed\n");
|
|
|
|
return SEC_E_OK;
|
|
|
|
|
|
|
|
case GNUTLS_E_AGAIN:
|
|
|
|
TRACE("Continue...\n");
|
|
|
|
return SEC_I_CONTINUE_NEEDED;
|
|
|
|
|
|
|
|
case GNUTLS_E_WARNING_ALERT_RECEIVED:
|
|
|
|
case GNUTLS_E_FATAL_ALERT_RECEIVED:
|
|
|
|
{
|
|
|
|
gnutls_alert_description_t alert = pgnutls_alert_get(ctx->session);
|
|
|
|
const char *alert_name = pgnutls_alert_get_name(alert);
|
|
|
|
WARN("ALERT: %d %s\n", alert, alert_name);
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
pgnutls_perror(err);
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
2006-02-14 17:37:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2008-10-06 19:08:47 +02:00
|
|
|
* InitializeSecurityContextA
|
2006-02-14 17:37:36 +01:00
|
|
|
*/
|
2008-10-06 19:08:47 +02:00
|
|
|
static SECURITY_STATUS SEC_ENTRY schan_InitializeSecurityContextA(
|
|
|
|
PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR *pszTargetName,
|
2006-02-14 17:37:36 +01:00
|
|
|
ULONG fContextReq, ULONG Reserved1, ULONG TargetDataRep,
|
2008-10-06 19:08:47 +02:00
|
|
|
PSecBufferDesc pInput, ULONG Reserved2, PCtxtHandle phNewContext,
|
2006-02-14 17:37:36 +01:00
|
|
|
PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS ret;
|
2008-10-06 19:08:47 +02:00
|
|
|
SEC_WCHAR *target_name = NULL;
|
2006-02-14 17:37:36 +01:00
|
|
|
|
2006-10-15 14:51:52 +02:00
|
|
|
TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
|
2008-10-06 19:08:47 +02:00
|
|
|
debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
|
2006-02-14 17:37:36 +01:00
|
|
|
Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
|
2008-10-06 19:08:47 +02:00
|
|
|
|
|
|
|
if (pszTargetName)
|
2006-02-14 17:37:36 +01:00
|
|
|
{
|
2008-10-06 19:08:47 +02:00
|
|
|
INT len = MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, NULL, 0);
|
|
|
|
target_name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*target_name));
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, pszTargetName, -1, target_name, len);
|
2006-02-14 17:37:36 +01:00
|
|
|
}
|
2008-10-06 19:08:47 +02:00
|
|
|
|
|
|
|
ret = schan_InitializeSecurityContextW(phCredential, phContext, target_name,
|
|
|
|
fContextReq, Reserved1, TargetDataRep, pInput, Reserved2,
|
|
|
|
phNewContext, pOutput, pfContextAttr, ptsExpiry);
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, target_name);
|
|
|
|
|
2006-02-14 17:37:36 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-12-28 22:21:35 +01:00
|
|
|
static unsigned int schannel_get_cipher_block_size(gnutls_cipher_algorithm_t cipher)
|
|
|
|
{
|
|
|
|
const struct
|
|
|
|
{
|
|
|
|
gnutls_cipher_algorithm_t cipher;
|
|
|
|
unsigned int block_size;
|
|
|
|
}
|
|
|
|
algorithms[] =
|
|
|
|
{
|
|
|
|
{GNUTLS_CIPHER_3DES_CBC, 8},
|
|
|
|
{GNUTLS_CIPHER_AES_128_CBC, 16},
|
|
|
|
{GNUTLS_CIPHER_AES_256_CBC, 16},
|
|
|
|
{GNUTLS_CIPHER_ARCFOUR_128, 1},
|
|
|
|
{GNUTLS_CIPHER_ARCFOUR_40, 1},
|
|
|
|
{GNUTLS_CIPHER_DES_CBC, 8},
|
|
|
|
{GNUTLS_CIPHER_NULL, 1},
|
|
|
|
{GNUTLS_CIPHER_RC2_40_CBC, 8},
|
|
|
|
};
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(algorithms) / sizeof(*algorithms); ++i)
|
|
|
|
{
|
|
|
|
if (algorithms[i].cipher == cipher)
|
|
|
|
return algorithms[i].block_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
FIXME("Unknown cipher %#x, returning 1\n", cipher);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-08-07 17:38:17 +02:00
|
|
|
static DWORD schannel_get_protocol(gnutls_protocol_t proto)
|
|
|
|
{
|
|
|
|
/* FIXME: currently schannel only implements client connections, but
|
|
|
|
* there's no reason it couldn't be used for servers as well. The
|
|
|
|
* context doesn't tell us which it is, so assume client for now.
|
|
|
|
*/
|
|
|
|
switch (proto)
|
|
|
|
{
|
|
|
|
case GNUTLS_SSL3: return SP_PROT_SSL3_CLIENT;
|
|
|
|
case GNUTLS_TLS1_0: return SP_PROT_TLS1_CLIENT;
|
|
|
|
default:
|
|
|
|
FIXME("unknown protocol %d\n", proto);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ALG_ID schannel_get_cipher_algid(gnutls_cipher_algorithm_t cipher)
|
|
|
|
{
|
|
|
|
switch (cipher)
|
|
|
|
{
|
|
|
|
case GNUTLS_CIPHER_UNKNOWN:
|
|
|
|
case GNUTLS_CIPHER_NULL: return 0;
|
2009-10-15 10:36:43 +02:00
|
|
|
case GNUTLS_CIPHER_ARCFOUR_40:
|
|
|
|
case GNUTLS_CIPHER_ARCFOUR_128: return CALG_RC4;
|
2009-08-07 17:38:17 +02:00
|
|
|
case GNUTLS_CIPHER_DES_CBC:
|
|
|
|
case GNUTLS_CIPHER_3DES_CBC: return CALG_DES;
|
|
|
|
case GNUTLS_CIPHER_AES_128_CBC:
|
|
|
|
case GNUTLS_CIPHER_AES_256_CBC: return CALG_AES;
|
|
|
|
case GNUTLS_CIPHER_RC2_40_CBC: return CALG_RC2;
|
|
|
|
default:
|
|
|
|
FIXME("unknown algorithm %d\n", cipher);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ALG_ID schannel_get_mac_algid(gnutls_mac_algorithm_t mac)
|
|
|
|
{
|
|
|
|
switch (mac)
|
|
|
|
{
|
|
|
|
case GNUTLS_MAC_UNKNOWN:
|
|
|
|
case GNUTLS_MAC_NULL: return 0;
|
|
|
|
case GNUTLS_MAC_MD5: return CALG_MD5;
|
|
|
|
case GNUTLS_MAC_SHA1:
|
|
|
|
case GNUTLS_MAC_SHA256:
|
|
|
|
case GNUTLS_MAC_SHA384:
|
|
|
|
case GNUTLS_MAC_SHA512: return CALG_SHA;
|
|
|
|
default:
|
|
|
|
FIXME("unknown algorithm %d\n", mac);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ALG_ID schannel_get_kx_algid(gnutls_kx_algorithm_t kx)
|
|
|
|
{
|
|
|
|
switch (kx)
|
|
|
|
{
|
|
|
|
case GNUTLS_KX_RSA: return CALG_RSA_KEYX;
|
|
|
|
case GNUTLS_KX_DHE_DSS:
|
|
|
|
case GNUTLS_KX_DHE_RSA: return CALG_DH_EPHEM;
|
|
|
|
default:
|
|
|
|
FIXME("unknown algorithm %d\n", kx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-28 22:21:35 +01:00
|
|
|
static SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesW(
|
|
|
|
PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
|
|
|
|
{
|
2008-12-28 22:21:35 +01:00
|
|
|
struct schan_context *ctx;
|
|
|
|
|
2008-12-28 22:21:35 +01:00
|
|
|
TRACE("context_handle %p, attribute %#x, buffer %p\n",
|
|
|
|
context_handle, attribute, buffer);
|
|
|
|
|
2008-12-28 22:21:35 +01:00
|
|
|
if (!context_handle) return SEC_E_INVALID_HANDLE;
|
|
|
|
ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
|
|
|
|
|
2008-12-28 22:21:35 +01:00
|
|
|
switch(attribute)
|
|
|
|
{
|
2008-12-28 22:21:35 +01:00
|
|
|
case SECPKG_ATTR_STREAM_SIZES:
|
|
|
|
{
|
2009-01-22 09:52:28 +01:00
|
|
|
SecPkgContext_StreamSizes *stream_sizes = buffer;
|
2008-12-28 22:21:35 +01:00
|
|
|
gnutls_mac_algorithm_t mac = pgnutls_mac_get(ctx->session);
|
|
|
|
size_t mac_size = pgnutls_mac_get_key_size(mac);
|
|
|
|
gnutls_cipher_algorithm_t cipher = pgnutls_cipher_get(ctx->session);
|
|
|
|
unsigned int block_size = schannel_get_cipher_block_size(cipher);
|
|
|
|
|
|
|
|
TRACE("Using %zu mac bytes, block size %u\n", mac_size, block_size);
|
|
|
|
|
|
|
|
/* These are defined by the TLS RFC */
|
|
|
|
stream_sizes->cbHeader = 5;
|
|
|
|
stream_sizes->cbTrailer = mac_size + 256; /* Max 255 bytes padding + 1 for padding size */
|
|
|
|
stream_sizes->cbMaximumMessage = 1 << 14;
|
|
|
|
stream_sizes->cbBuffers = 4;
|
|
|
|
stream_sizes->cbBlockSize = block_size;
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
2009-08-05 19:43:57 +02:00
|
|
|
case SECPKG_ATTR_REMOTE_CERT_CONTEXT:
|
|
|
|
{
|
|
|
|
unsigned int list_size;
|
2009-09-28 09:48:32 +02:00
|
|
|
const gnutls_datum_t *datum;
|
2009-08-05 19:43:57 +02:00
|
|
|
|
|
|
|
datum = pgnutls_certificate_get_peers(ctx->session, &list_size);
|
|
|
|
if (datum)
|
|
|
|
{
|
|
|
|
PCCERT_CONTEXT *cert = buffer;
|
|
|
|
|
|
|
|
*cert = CertCreateCertificateContext(X509_ASN_ENCODING,
|
|
|
|
datum->data, datum->size);
|
|
|
|
if (!*cert)
|
|
|
|
return GetLastError();
|
|
|
|
else
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
}
|
2009-08-07 17:38:17 +02:00
|
|
|
case SECPKG_ATTR_CONNECTION_INFO:
|
|
|
|
{
|
|
|
|
SecPkgContext_ConnectionInfo *info = buffer;
|
|
|
|
gnutls_protocol_t proto = pgnutls_protocol_get_version(ctx->session);
|
|
|
|
gnutls_cipher_algorithm_t alg = pgnutls_cipher_get(ctx->session);
|
|
|
|
gnutls_mac_algorithm_t mac = pgnutls_mac_get(ctx->session);
|
|
|
|
gnutls_kx_algorithm_t kx = pgnutls_kx_get(ctx->session);
|
|
|
|
|
|
|
|
info->dwProtocol = schannel_get_protocol(proto);
|
|
|
|
info->aiCipher = schannel_get_cipher_algid(alg);
|
|
|
|
info->dwCipherStrength = pgnutls_cipher_get_key_size(alg);
|
|
|
|
info->aiHash = schannel_get_mac_algid(mac);
|
|
|
|
info->dwHashStrength = pgnutls_mac_get_key_size(mac);
|
|
|
|
info->aiExch = schannel_get_kx_algid(kx);
|
|
|
|
/* FIXME: info->dwExchStrength? */
|
|
|
|
info->dwExchStrength = 0;
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
2008-12-28 22:21:35 +01:00
|
|
|
|
2008-12-28 22:21:35 +01:00
|
|
|
default:
|
|
|
|
FIXME("Unhandled attribute %#x\n", attribute);
|
|
|
|
return SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static SECURITY_STATUS SEC_ENTRY schan_QueryContextAttributesA(
|
|
|
|
PCtxtHandle context_handle, ULONG attribute, PVOID buffer)
|
|
|
|
{
|
|
|
|
TRACE("context_handle %p, attribute %#x, buffer %p\n",
|
|
|
|
context_handle, attribute, buffer);
|
|
|
|
|
|
|
|
switch(attribute)
|
|
|
|
{
|
2008-12-28 22:21:35 +01:00
|
|
|
case SECPKG_ATTR_STREAM_SIZES:
|
|
|
|
return schan_QueryContextAttributesW(context_handle, attribute, buffer);
|
2009-08-05 19:43:57 +02:00
|
|
|
case SECPKG_ATTR_REMOTE_CERT_CONTEXT:
|
|
|
|
return schan_QueryContextAttributesW(context_handle, attribute, buffer);
|
2009-08-07 17:38:17 +02:00
|
|
|
case SECPKG_ATTR_CONNECTION_INFO:
|
|
|
|
return schan_QueryContextAttributesW(context_handle, attribute, buffer);
|
2008-12-28 22:21:35 +01:00
|
|
|
|
2008-12-28 22:21:35 +01:00
|
|
|
default:
|
|
|
|
FIXME("Unhandled attribute %#x\n", attribute);
|
|
|
|
return SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-28 22:21:35 +01:00
|
|
|
static int schan_encrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
|
|
|
|
{
|
|
|
|
SecBuffer *b;
|
|
|
|
|
|
|
|
if (s->current_buffer_idx == -1)
|
|
|
|
return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_HEADER);
|
|
|
|
|
|
|
|
b = &s->desc->pBuffers[s->current_buffer_idx];
|
|
|
|
|
|
|
|
if (b->BufferType == SECBUFFER_STREAM_HEADER)
|
|
|
|
return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
|
|
|
|
|
|
|
|
if (b->BufferType == SECBUFFER_DATA)
|
|
|
|
return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_STREAM_TRAILER);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int schan_encrypt_message_get_next_buffer_token(const struct schan_transport *t, struct schan_buffers *s)
|
|
|
|
{
|
|
|
|
SecBuffer *b;
|
|
|
|
|
|
|
|
if (s->current_buffer_idx == -1)
|
|
|
|
return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
|
|
|
|
|
|
|
|
b = &s->desc->pBuffers[s->current_buffer_idx];
|
|
|
|
|
|
|
|
if (b->BufferType == SECBUFFER_TOKEN)
|
|
|
|
{
|
|
|
|
int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
|
|
|
|
if (idx != s->current_buffer_idx) return -1;
|
|
|
|
return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b->BufferType == SECBUFFER_DATA)
|
|
|
|
{
|
|
|
|
int idx = schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_TOKEN);
|
|
|
|
if (idx != -1)
|
|
|
|
idx = schan_find_sec_buffer_idx(s->desc, idx + 1, SECBUFFER_TOKEN);
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SECURITY_STATUS SEC_ENTRY schan_EncryptMessage(PCtxtHandle context_handle,
|
|
|
|
ULONG quality, PSecBufferDesc message, ULONG message_seq_no)
|
|
|
|
{
|
|
|
|
struct schan_transport transport;
|
|
|
|
struct schan_context *ctx;
|
|
|
|
struct schan_buffers *b;
|
|
|
|
SecBuffer *buffer;
|
|
|
|
SIZE_T data_size;
|
|
|
|
char *data;
|
|
|
|
ssize_t sent = 0;
|
|
|
|
ssize_t ret;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
TRACE("context_handle %p, quality %d, message %p, message_seq_no %d\n",
|
|
|
|
context_handle, quality, message, message_seq_no);
|
|
|
|
|
|
|
|
if (!context_handle) return SEC_E_INVALID_HANDLE;
|
|
|
|
ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
|
|
|
|
|
|
|
|
dump_buffer_desc(message);
|
|
|
|
|
|
|
|
idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_DATA);
|
|
|
|
if (idx == -1)
|
|
|
|
{
|
|
|
|
WARN("No data buffer passed\n");
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
buffer = &message->pBuffers[idx];
|
|
|
|
|
|
|
|
data_size = buffer->cbBuffer;
|
|
|
|
data = HeapAlloc(GetProcessHeap(), 0, data_size);
|
|
|
|
memcpy(data, buffer->pvBuffer, data_size);
|
|
|
|
|
|
|
|
transport.ctx = ctx;
|
|
|
|
init_schan_buffers(&transport.in, NULL, NULL);
|
|
|
|
if (schan_find_sec_buffer_idx(message, 0, SECBUFFER_STREAM_HEADER) != -1)
|
|
|
|
init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer);
|
|
|
|
else
|
|
|
|
init_schan_buffers(&transport.out, message, schan_encrypt_message_get_next_buffer_token);
|
|
|
|
pgnutls_transport_set_ptr(ctx->session, &transport);
|
|
|
|
|
|
|
|
while (sent < data_size)
|
|
|
|
{
|
|
|
|
ret = pgnutls_record_send(ctx->session, data + sent, data_size - sent);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
if (ret != GNUTLS_E_AGAIN)
|
|
|
|
{
|
|
|
|
pgnutls_perror(ret);
|
|
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
|
|
ERR("Returning SEC_E_INTERNAL_ERROR\n");
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
sent += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("Sent %zd bytes\n", sent);
|
|
|
|
|
|
|
|
b = &transport.out;
|
|
|
|
b->desc->pBuffers[b->current_buffer_idx].cbBuffer = b->offset;
|
|
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2008-12-28 22:21:36 +01:00
|
|
|
static int schan_decrypt_message_get_next_buffer(const struct schan_transport *t, struct schan_buffers *s)
|
|
|
|
{
|
|
|
|
if (s->current_buffer_idx == -1)
|
|
|
|
return schan_find_sec_buffer_idx(s->desc, 0, SECBUFFER_DATA);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-15 19:30:07 +02:00
|
|
|
static int schan_validate_decrypt_buffer_desc(PSecBufferDesc message)
|
|
|
|
{
|
|
|
|
int data_idx = -1;
|
|
|
|
unsigned int empty_count = 0;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (message->cBuffers < 4)
|
|
|
|
{
|
|
|
|
WARN("Less than four buffers passed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < message->cBuffers; ++i)
|
|
|
|
{
|
|
|
|
SecBuffer *b = &message->pBuffers[i];
|
|
|
|
if (b->BufferType == SECBUFFER_DATA)
|
|
|
|
{
|
|
|
|
if (data_idx != -1)
|
|
|
|
{
|
|
|
|
WARN("More than one data buffer passed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
data_idx = i;
|
|
|
|
}
|
|
|
|
else if (b->BufferType == SECBUFFER_EMPTY)
|
|
|
|
++empty_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data_idx == -1)
|
|
|
|
{
|
|
|
|
WARN("No data buffer passed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty_count < 3)
|
|
|
|
{
|
|
|
|
WARN("Less than three empty buffers passed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data_idx;
|
|
|
|
}
|
|
|
|
|
2010-09-15 19:30:08 +02:00
|
|
|
static void schan_decrypt_fill_buffer(PSecBufferDesc message, ULONG buffer_type, void *data, ULONG size)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
SecBuffer *buffer;
|
|
|
|
|
|
|
|
idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_EMPTY);
|
|
|
|
buffer = &message->pBuffers[idx];
|
|
|
|
|
|
|
|
buffer->BufferType = buffer_type;
|
|
|
|
buffer->pvBuffer = data;
|
|
|
|
buffer->cbBuffer = size;
|
|
|
|
}
|
|
|
|
|
2008-12-28 22:21:36 +01:00
|
|
|
static SECURITY_STATUS SEC_ENTRY schan_DecryptMessage(PCtxtHandle context_handle,
|
|
|
|
PSecBufferDesc message, ULONG message_seq_no, PULONG quality)
|
|
|
|
{
|
|
|
|
struct schan_transport transport;
|
|
|
|
struct schan_context *ctx;
|
|
|
|
SecBuffer *buffer;
|
|
|
|
SIZE_T data_size;
|
|
|
|
char *data;
|
2010-09-15 19:30:09 +02:00
|
|
|
unsigned expected_size;
|
2008-12-28 22:21:36 +01:00
|
|
|
ssize_t received = 0;
|
|
|
|
ssize_t ret;
|
|
|
|
int idx;
|
2010-09-15 19:30:09 +02:00
|
|
|
unsigned char *buf_ptr;
|
2008-12-28 22:21:36 +01:00
|
|
|
|
|
|
|
TRACE("context_handle %p, message %p, message_seq_no %d, quality %p\n",
|
|
|
|
context_handle, message, message_seq_no, quality);
|
|
|
|
|
|
|
|
if (!context_handle) return SEC_E_INVALID_HANDLE;
|
|
|
|
ctx = schan_get_object(context_handle->dwLower, SCHAN_HANDLE_CTX);
|
|
|
|
|
|
|
|
dump_buffer_desc(message);
|
|
|
|
|
2010-09-15 19:30:07 +02:00
|
|
|
idx = schan_validate_decrypt_buffer_desc(message);
|
2008-12-28 22:21:36 +01:00
|
|
|
if (idx == -1)
|
2010-09-15 19:30:07 +02:00
|
|
|
return SEC_E_INVALID_TOKEN;
|
2008-12-28 22:21:36 +01:00
|
|
|
buffer = &message->pBuffers[idx];
|
2010-09-15 19:30:09 +02:00
|
|
|
buf_ptr = buffer->pvBuffer;
|
|
|
|
|
|
|
|
expected_size = 5 + ((buf_ptr[3] << 8) | buf_ptr[4]);
|
|
|
|
if(buffer->cbBuffer < expected_size)
|
|
|
|
{
|
|
|
|
TRACE("Expected %u bytes, but buffer only contains %u bytes\n", expected_size, buffer->cbBuffer);
|
2010-09-15 19:30:10 +02:00
|
|
|
buffer->BufferType = SECBUFFER_MISSING;
|
|
|
|
buffer->cbBuffer = expected_size - buffer->cbBuffer;
|
|
|
|
|
|
|
|
/* This is a bit weird, but windows does it too */
|
|
|
|
idx = schan_find_sec_buffer_idx(message, 0, SECBUFFER_EMPTY);
|
|
|
|
buffer = &message->pBuffers[idx];
|
|
|
|
buffer->BufferType = SECBUFFER_MISSING;
|
|
|
|
buffer->cbBuffer = expected_size - buffer->cbBuffer;
|
|
|
|
|
2010-09-15 19:30:09 +02:00
|
|
|
TRACE("Returning SEC_E_INCOMPLETE_MESSAGE\n");
|
|
|
|
return SEC_E_INCOMPLETE_MESSAGE;
|
|
|
|
}
|
2008-12-28 22:21:36 +01:00
|
|
|
|
|
|
|
data_size = buffer->cbBuffer;
|
|
|
|
data = HeapAlloc(GetProcessHeap(), 0, data_size);
|
|
|
|
|
|
|
|
transport.ctx = ctx;
|
|
|
|
init_schan_buffers(&transport.in, message, schan_decrypt_message_get_next_buffer);
|
2010-09-15 19:30:09 +02:00
|
|
|
transport.in.limit = expected_size;
|
2008-12-28 22:21:36 +01:00
|
|
|
init_schan_buffers(&transport.out, NULL, NULL);
|
|
|
|
pgnutls_transport_set_ptr(ctx->session, (gnutls_transport_ptr_t)&transport);
|
|
|
|
|
|
|
|
while (received < data_size)
|
|
|
|
{
|
|
|
|
ret = pgnutls_record_recv(ctx->session, data + received, data_size - received);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
if (ret == GNUTLS_E_AGAIN)
|
|
|
|
{
|
|
|
|
if (!received)
|
|
|
|
{
|
|
|
|
pgnutls_perror(ret);
|
|
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
|
|
TRACE("Returning SEC_E_INCOMPLETE_MESSAGE\n");
|
|
|
|
return SEC_E_INCOMPLETE_MESSAGE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pgnutls_perror(ret);
|
|
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
|
|
ERR("Returning SEC_E_INTERNAL_ERROR\n");
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2009-11-19 09:30:33 +01:00
|
|
|
else if (!ret)
|
|
|
|
break;
|
|
|
|
|
2008-12-28 22:21:36 +01:00
|
|
|
received += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("Received %zd bytes\n", received);
|
|
|
|
|
2010-09-15 19:30:08 +02:00
|
|
|
memcpy(buf_ptr + 5, data, received);
|
2008-12-28 22:21:36 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
|
|
|
2010-09-15 19:30:08 +02:00
|
|
|
schan_decrypt_fill_buffer(message, SECBUFFER_DATA,
|
|
|
|
buf_ptr + 5, received);
|
|
|
|
|
|
|
|
schan_decrypt_fill_buffer(message, SECBUFFER_STREAM_TRAILER,
|
|
|
|
buf_ptr + 5 + received, buffer->cbBuffer - 5 - received);
|
|
|
|
|
2010-09-15 19:30:09 +02:00
|
|
|
if(buffer->cbBuffer > expected_size)
|
|
|
|
schan_decrypt_fill_buffer(message, SECBUFFER_EXTRA,
|
|
|
|
buf_ptr + expected_size, buffer->cbBuffer - expected_size);
|
|
|
|
|
2010-09-15 19:30:08 +02:00
|
|
|
buffer->BufferType = SECBUFFER_STREAM_HEADER;
|
|
|
|
buffer->cbBuffer = 5;
|
|
|
|
|
2008-12-28 22:21:36 +01:00
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2008-10-20 09:26:45 +02:00
|
|
|
static SECURITY_STATUS SEC_ENTRY schan_DeleteSecurityContext(PCtxtHandle context_handle)
|
|
|
|
{
|
|
|
|
struct schan_context *ctx;
|
|
|
|
|
|
|
|
TRACE("context_handle %p\n", context_handle);
|
|
|
|
|
|
|
|
if (!context_handle) return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
ctx = schan_free_handle(context_handle->dwLower, SCHAN_HANDLE_CTX);
|
|
|
|
if (!ctx) return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
pgnutls_deinit(ctx->session);
|
|
|
|
HeapFree(GetProcessHeap(), 0, ctx);
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-22 22:14:04 +02:00
|
|
|
static void schan_gnutls_log(int level, const char *msg)
|
|
|
|
{
|
|
|
|
TRACE("<%d> %s", level, msg);
|
|
|
|
}
|
|
|
|
|
2006-12-14 15:47:50 +01:00
|
|
|
static const SecurityFunctionTableA schanTableA = {
|
2005-05-13 19:44:47 +02:00
|
|
|
1,
|
|
|
|
NULL, /* EnumerateSecurityPackagesA */
|
|
|
|
schan_QueryCredentialsAttributesA,
|
|
|
|
schan_AcquireCredentialsHandleA,
|
2006-07-28 08:39:56 +02:00
|
|
|
schan_FreeCredentialsHandle,
|
2005-05-13 19:44:47 +02:00
|
|
|
NULL, /* Reserved2 */
|
2006-02-14 17:37:36 +01:00
|
|
|
schan_InitializeSecurityContextA,
|
2005-05-13 19:44:47 +02:00
|
|
|
NULL, /* AcceptSecurityContext */
|
|
|
|
NULL, /* CompleteAuthToken */
|
2008-10-20 09:26:45 +02:00
|
|
|
schan_DeleteSecurityContext,
|
2005-05-13 19:44:47 +02:00
|
|
|
NULL, /* ApplyControlToken */
|
2008-12-28 22:21:35 +01:00
|
|
|
schan_QueryContextAttributesA,
|
2005-05-13 19:44:47 +02:00
|
|
|
NULL, /* ImpersonateSecurityContext */
|
|
|
|
NULL, /* RevertSecurityContext */
|
|
|
|
NULL, /* MakeSignature */
|
|
|
|
NULL, /* VerifySignature */
|
|
|
|
FreeContextBuffer,
|
|
|
|
NULL, /* QuerySecurityPackageInfoA */
|
|
|
|
NULL, /* Reserved3 */
|
|
|
|
NULL, /* Reserved4 */
|
|
|
|
NULL, /* ExportSecurityContext */
|
|
|
|
NULL, /* ImportSecurityContextA */
|
|
|
|
NULL, /* AddCredentialsA */
|
|
|
|
NULL, /* Reserved8 */
|
|
|
|
NULL, /* QuerySecurityContextToken */
|
2008-12-28 22:21:35 +01:00
|
|
|
schan_EncryptMessage,
|
2008-12-28 22:21:36 +01:00
|
|
|
schan_DecryptMessage,
|
2005-05-13 19:44:47 +02:00
|
|
|
NULL, /* SetContextAttributesA */
|
|
|
|
};
|
|
|
|
|
2006-12-14 15:47:50 +01:00
|
|
|
static const SecurityFunctionTableW schanTableW = {
|
2005-05-13 19:44:47 +02:00
|
|
|
1,
|
|
|
|
NULL, /* EnumerateSecurityPackagesW */
|
|
|
|
schan_QueryCredentialsAttributesW,
|
|
|
|
schan_AcquireCredentialsHandleW,
|
2006-07-28 08:39:56 +02:00
|
|
|
schan_FreeCredentialsHandle,
|
2005-05-13 19:44:47 +02:00
|
|
|
NULL, /* Reserved2 */
|
2006-02-14 17:37:36 +01:00
|
|
|
schan_InitializeSecurityContextW,
|
2005-05-13 19:44:47 +02:00
|
|
|
NULL, /* AcceptSecurityContext */
|
|
|
|
NULL, /* CompleteAuthToken */
|
2008-10-20 09:26:45 +02:00
|
|
|
schan_DeleteSecurityContext,
|
2005-05-13 19:44:47 +02:00
|
|
|
NULL, /* ApplyControlToken */
|
2008-12-28 22:21:35 +01:00
|
|
|
schan_QueryContextAttributesW,
|
2005-05-13 19:44:47 +02:00
|
|
|
NULL, /* ImpersonateSecurityContext */
|
|
|
|
NULL, /* RevertSecurityContext */
|
|
|
|
NULL, /* MakeSignature */
|
|
|
|
NULL, /* VerifySignature */
|
|
|
|
FreeContextBuffer,
|
|
|
|
NULL, /* QuerySecurityPackageInfoW */
|
|
|
|
NULL, /* Reserved3 */
|
|
|
|
NULL, /* Reserved4 */
|
|
|
|
NULL, /* ExportSecurityContext */
|
|
|
|
NULL, /* ImportSecurityContextW */
|
|
|
|
NULL, /* AddCredentialsW */
|
|
|
|
NULL, /* Reserved8 */
|
|
|
|
NULL, /* QuerySecurityContextToken */
|
2008-12-28 22:21:35 +01:00
|
|
|
schan_EncryptMessage,
|
2008-12-28 22:21:36 +01:00
|
|
|
schan_DecryptMessage,
|
2005-05-13 19:44:47 +02:00
|
|
|
NULL, /* SetContextAttributesW */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const WCHAR schannelComment[] = { 'S','c','h','a','n','n','e','l',' ',
|
|
|
|
'S','e','c','u','r','i','t','y',' ','P','a','c','k','a','g','e',0 };
|
2008-07-02 17:18:49 +02:00
|
|
|
static const WCHAR schannelDllName[] = { 's','c','h','a','n','n','e','l','.','d','l','l',0 };
|
2005-05-13 19:44:47 +02:00
|
|
|
|
|
|
|
void SECUR32_initSchannelSP(void)
|
|
|
|
{
|
2008-10-20 00:15:13 +02:00
|
|
|
/* This is what Windows reports. This shouldn't break any applications
|
|
|
|
* even though the functions are missing, because the wrapper will
|
|
|
|
* return SEC_E_UNSUPPORTED_FUNCTION if our function is NULL.
|
|
|
|
*/
|
2010-05-11 21:28:42 +02:00
|
|
|
static const LONG caps =
|
2008-10-20 00:15:13 +02:00
|
|
|
SECPKG_FLAG_INTEGRITY |
|
|
|
|
SECPKG_FLAG_PRIVACY |
|
|
|
|
SECPKG_FLAG_CONNECTION |
|
|
|
|
SECPKG_FLAG_MULTI_REQUIRED |
|
|
|
|
SECPKG_FLAG_EXTENDED_ERROR |
|
|
|
|
SECPKG_FLAG_IMPERSONATION |
|
|
|
|
SECPKG_FLAG_ACCEPT_WIN32_NAME |
|
|
|
|
SECPKG_FLAG_STREAM;
|
|
|
|
static const short version = 1;
|
2010-05-11 21:28:42 +02:00
|
|
|
static const LONG maxToken = 16384;
|
2008-10-20 00:15:13 +02:00
|
|
|
SEC_WCHAR *uniSPName = (SEC_WCHAR *)UNISP_NAME_W,
|
|
|
|
*schannel = (SEC_WCHAR *)SCHANNEL_NAME_W;
|
|
|
|
const SecPkgInfoW info[] = {
|
|
|
|
{ caps, version, UNISP_RPC_ID, maxToken, uniSPName, uniSPName },
|
|
|
|
{ caps, version, UNISP_RPC_ID, maxToken, schannel,
|
|
|
|
(SEC_WCHAR *)schannelComment },
|
|
|
|
};
|
2008-09-22 22:14:04 +02:00
|
|
|
SecureProvider *provider;
|
2008-10-20 00:15:13 +02:00
|
|
|
int ret;
|
2008-09-22 22:14:04 +02:00
|
|
|
|
|
|
|
libgnutls_handle = wine_dlopen(SONAME_LIBGNUTLS, RTLD_NOW, NULL, 0);
|
|
|
|
if (!libgnutls_handle)
|
|
|
|
{
|
|
|
|
WARN("Failed to load libgnutls.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LOAD_FUNCPTR(f) \
|
|
|
|
if (!(p##f = wine_dlsym(libgnutls_handle, #f, NULL, 0))) \
|
|
|
|
{ \
|
|
|
|
ERR("Failed to load %s\n", #f); \
|
2008-10-20 00:15:13 +02:00
|
|
|
goto fail; \
|
2008-09-22 22:14:04 +02:00
|
|
|
}
|
|
|
|
|
2008-10-20 09:26:45 +02:00
|
|
|
LOAD_FUNCPTR(gnutls_alert_get)
|
|
|
|
LOAD_FUNCPTR(gnutls_alert_get_name)
|
2008-09-22 22:14:04 +02:00
|
|
|
LOAD_FUNCPTR(gnutls_certificate_allocate_credentials)
|
|
|
|
LOAD_FUNCPTR(gnutls_certificate_free_credentials)
|
2009-08-05 19:43:57 +02:00
|
|
|
LOAD_FUNCPTR(gnutls_certificate_get_peers)
|
2008-12-28 22:21:35 +01:00
|
|
|
LOAD_FUNCPTR(gnutls_cipher_get)
|
2009-08-07 17:38:17 +02:00
|
|
|
LOAD_FUNCPTR(gnutls_cipher_get_key_size)
|
2008-10-20 09:26:45 +02:00
|
|
|
LOAD_FUNCPTR(gnutls_credentials_set)
|
|
|
|
LOAD_FUNCPTR(gnutls_deinit)
|
2008-09-22 22:14:04 +02:00
|
|
|
LOAD_FUNCPTR(gnutls_global_deinit)
|
|
|
|
LOAD_FUNCPTR(gnutls_global_init)
|
2008-09-22 22:14:04 +02:00
|
|
|
LOAD_FUNCPTR(gnutls_global_set_log_function)
|
|
|
|
LOAD_FUNCPTR(gnutls_global_set_log_level)
|
2008-10-20 09:26:45 +02:00
|
|
|
LOAD_FUNCPTR(gnutls_handshake)
|
|
|
|
LOAD_FUNCPTR(gnutls_init)
|
2009-08-07 17:38:17 +02:00
|
|
|
LOAD_FUNCPTR(gnutls_kx_get)
|
2008-12-28 22:21:35 +01:00
|
|
|
LOAD_FUNCPTR(gnutls_mac_get)
|
|
|
|
LOAD_FUNCPTR(gnutls_mac_get_key_size)
|
2008-10-06 19:08:47 +02:00
|
|
|
LOAD_FUNCPTR(gnutls_perror)
|
2009-08-07 17:38:17 +02:00
|
|
|
LOAD_FUNCPTR(gnutls_protocol_get_version)
|
2008-10-20 09:26:45 +02:00
|
|
|
LOAD_FUNCPTR(gnutls_set_default_priority)
|
2008-12-28 22:21:36 +01:00
|
|
|
LOAD_FUNCPTR(gnutls_record_recv);
|
2008-12-28 22:21:35 +01:00
|
|
|
LOAD_FUNCPTR(gnutls_record_send);
|
2008-10-20 09:26:45 +02:00
|
|
|
LOAD_FUNCPTR(gnutls_transport_set_errno)
|
|
|
|
LOAD_FUNCPTR(gnutls_transport_set_ptr)
|
|
|
|
LOAD_FUNCPTR(gnutls_transport_set_pull_function)
|
|
|
|
LOAD_FUNCPTR(gnutls_transport_set_push_function)
|
2008-09-22 22:14:04 +02:00
|
|
|
#undef LOAD_FUNCPTR
|
|
|
|
|
2008-10-20 00:15:13 +02:00
|
|
|
ret = pgnutls_global_init();
|
|
|
|
if (ret != GNUTLS_E_SUCCESS)
|
|
|
|
{
|
|
|
|
pgnutls_perror(ret);
|
|
|
|
goto fail;
|
|
|
|
}
|
2005-05-13 19:44:47 +02:00
|
|
|
|
2008-10-20 00:15:13 +02:00
|
|
|
if (TRACE_ON(secur32))
|
2005-05-13 19:44:47 +02:00
|
|
|
{
|
2008-10-20 00:15:13 +02:00
|
|
|
pgnutls_global_set_log_level(4);
|
|
|
|
pgnutls_global_set_log_function(schan_gnutls_log);
|
|
|
|
}
|
|
|
|
|
2009-10-15 10:36:45 +02:00
|
|
|
schan_handle_table = HeapAlloc(GetProcessHeap(), 0, 64 * sizeof(*schan_handle_table));
|
2008-10-20 00:15:13 +02:00
|
|
|
if (!schan_handle_table)
|
|
|
|
{
|
|
|
|
ERR("Failed to allocate schannel handle table.\n");
|
|
|
|
goto fail;
|
2005-05-13 19:44:47 +02:00
|
|
|
}
|
2008-10-20 00:15:13 +02:00
|
|
|
schan_handle_table_size = 64;
|
|
|
|
|
|
|
|
provider = SECUR32_addProvider(&schanTableA, &schanTableW, schannelDllName);
|
|
|
|
if (!provider)
|
|
|
|
{
|
|
|
|
ERR("Failed to add schannel provider.\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECUR32_addPackages(provider, sizeof(info) / sizeof(info[0]), NULL, info);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
HeapFree(GetProcessHeap(), 0, schan_handle_table);
|
|
|
|
schan_handle_table = NULL;
|
|
|
|
wine_dlclose(libgnutls_handle, NULL, 0);
|
|
|
|
libgnutls_handle = NULL;
|
|
|
|
return;
|
2005-05-13 19:44:47 +02:00
|
|
|
}
|
2008-09-22 22:13:19 +02:00
|
|
|
|
2008-09-22 22:14:04 +02:00
|
|
|
void SECUR32_deinitSchannelSP(void)
|
|
|
|
{
|
2009-10-15 10:36:44 +02:00
|
|
|
SIZE_T i = schan_handle_count;
|
2009-09-28 09:48:38 +02:00
|
|
|
|
2008-10-20 00:15:13 +02:00
|
|
|
if (!libgnutls_handle) return;
|
|
|
|
|
2009-09-28 09:48:38 +02:00
|
|
|
/* deinitialized sessions first because a pointer to the credentials
|
|
|
|
* are stored for the session by calling gnutls_credentials_set. */
|
2009-10-15 10:36:44 +02:00
|
|
|
while (i--)
|
2009-09-28 09:48:38 +02:00
|
|
|
{
|
|
|
|
if (schan_handle_table[i].type == SCHAN_HANDLE_CTX)
|
|
|
|
{
|
|
|
|
struct schan_context *ctx = schan_free_handle(i, SCHAN_HANDLE_CTX);
|
|
|
|
pgnutls_deinit(ctx->session);
|
|
|
|
HeapFree(GetProcessHeap(), 0, ctx);
|
|
|
|
}
|
|
|
|
}
|
2009-10-15 10:36:44 +02:00
|
|
|
i = schan_handle_count;
|
|
|
|
while (i--)
|
2009-09-28 09:48:38 +02:00
|
|
|
{
|
|
|
|
if (schan_handle_table[i].type != SCHAN_HANDLE_FREE)
|
|
|
|
{
|
|
|
|
struct schan_credentials *cred;
|
|
|
|
cred = schan_free_handle(i, SCHAN_HANDLE_CRED);
|
|
|
|
pgnutls_certificate_free_credentials(cred->credentials);
|
|
|
|
HeapFree(GetProcessHeap(), 0, cred);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HeapFree(GetProcessHeap(), 0, schan_handle_table);
|
2008-09-22 22:14:04 +02:00
|
|
|
pgnutls_global_deinit();
|
2008-10-20 00:15:13 +02:00
|
|
|
wine_dlclose(libgnutls_handle, NULL, 0);
|
2008-09-22 22:14:04 +02:00
|
|
|
}
|
|
|
|
|
2008-09-22 22:13:19 +02:00
|
|
|
#else /* SONAME_LIBGNUTLS */
|
|
|
|
|
2010-01-11 19:43:34 +01:00
|
|
|
void SECUR32_initSchannelSP(void)
|
|
|
|
{
|
|
|
|
ERR("libgnutls not found, SSL connections will fail\n");
|
|
|
|
}
|
|
|
|
|
2008-09-22 22:14:04 +02:00
|
|
|
void SECUR32_deinitSchannelSP(void) {}
|
2008-09-22 22:13:19 +02:00
|
|
|
|
|
|
|
#endif /* SONAME_LIBGNUTLS */
|