2012-12-24 05:20:41 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Schannel Security Package (OpenSSL)
|
|
|
|
*
|
2014-06-06 06:10:08 +04:00
|
|
|
* Copyright 2012-2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2014-08-18 19:22:22 +04:00
|
|
|
*
|
2012-12-24 05:20:41 +04:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/sspi.h>
|
2014-07-28 23:55:57 +04:00
|
|
|
#include <winpr/ssl.h>
|
2012-12-24 05:20:41 +04:00
|
|
|
#include <winpr/print.h>
|
|
|
|
|
|
|
|
#include "schannel_openssl.h"
|
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#include "../../log.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#define TAG WINPR_TAG("sspi.schannel")
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
char* openssl_get_ssl_error_string(int ssl_error)
|
2012-12-24 05:20:41 +04:00
|
|
|
{
|
|
|
|
switch (ssl_error)
|
|
|
|
{
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
|
|
|
return "SSL_ERROR_ZERO_RETURN";
|
2014-08-18 20:57:08 +04:00
|
|
|
|
2012-12-24 05:20:41 +04:00
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
return "SSL_ERROR_WANT_READ";
|
2014-08-18 20:57:08 +04:00
|
|
|
|
2012-12-24 05:20:41 +04:00
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
return "SSL_ERROR_WANT_WRITE";
|
2014-08-18 20:57:08 +04:00
|
|
|
|
2012-12-24 05:20:41 +04:00
|
|
|
case SSL_ERROR_SYSCALL:
|
|
|
|
return "SSL_ERROR_SYSCALL";
|
2014-08-18 20:57:08 +04:00
|
|
|
|
2012-12-24 05:20:41 +04:00
|
|
|
case SSL_ERROR_SSL:
|
|
|
|
return "SSL_ERROR_SSL";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "SSL_ERROR_UNKNOWN";
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int schannel_openssl_client_init(SCHANNEL_OPENSSL* context)
|
2012-12-24 05:20:41 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
long options = 0;
|
|
|
|
context->ctx = SSL_CTX_new(TLSv1_client_method());
|
|
|
|
|
|
|
|
if (!context->ctx)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "SSL_CTX_new failed");
|
2012-12-24 05:20:41 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SSL_OP_NO_COMPRESSION:
|
|
|
|
*
|
|
|
|
* The Microsoft RDP server does not advertise support
|
|
|
|
* for TLS compression, but alternative servers may support it.
|
|
|
|
* This was observed between early versions of the FreeRDP server
|
|
|
|
* and the FreeRDP client, and caused major performance issues,
|
|
|
|
* which is why we're disabling it.
|
|
|
|
*/
|
|
|
|
#ifdef SSL_OP_NO_COMPRESSION
|
|
|
|
options |= SSL_OP_NO_COMPRESSION;
|
|
|
|
#endif
|
|
|
|
/**
|
|
|
|
* SSL_OP_TLS_BLOCK_PADDING_BUG:
|
|
|
|
*
|
|
|
|
* The Microsoft RDP server does *not* support TLS padding.
|
|
|
|
* It absolutely needs to be disabled otherwise it won't work.
|
|
|
|
*/
|
|
|
|
options |= SSL_OP_TLS_BLOCK_PADDING_BUG;
|
|
|
|
/**
|
|
|
|
* SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS:
|
|
|
|
*
|
|
|
|
* Just like TLS padding, the Microsoft RDP server does not
|
|
|
|
* support empty fragments. This needs to be disabled.
|
|
|
|
*/
|
|
|
|
options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
|
|
|
|
SSL_CTX_set_options(context->ctx, options);
|
|
|
|
context->ssl = SSL_new(context->ctx);
|
|
|
|
|
|
|
|
if (!context->ssl)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "SSL_new failed");
|
2015-04-03 17:21:01 +03:00
|
|
|
goto out_ssl_new_failed;
|
2012-12-24 05:20:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
context->bioRead = BIO_new(BIO_s_mem());
|
|
|
|
|
|
|
|
if (!context->bioRead)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "BIO_new failed");
|
2015-04-03 17:21:01 +03:00
|
|
|
goto out_bio_read_failed;
|
2012-12-24 05:20:41 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = BIO_set_write_buf_size(context->bioRead, SCHANNEL_CB_MAX_TOKEN);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (status != 1)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "BIO_set_write_buf_size on bioRead failed");
|
|
|
|
goto out_set_write_buf_read;
|
|
|
|
}
|
|
|
|
|
2012-12-24 05:20:41 +04:00
|
|
|
context->bioWrite = BIO_new(BIO_s_mem());
|
|
|
|
|
|
|
|
if (!context->bioWrite)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "BIO_new failed");
|
2015-04-03 17:21:01 +03:00
|
|
|
goto out_bio_write_failed;
|
2012-12-24 05:20:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
status = BIO_set_write_buf_size(context->bioWrite, SCHANNEL_CB_MAX_TOKEN);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (status != 1)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "BIO_set_write_buf_size on bioWrite failed");
|
|
|
|
goto out_set_write_buf_write;
|
|
|
|
}
|
2012-12-24 05:20:41 +04:00
|
|
|
status = BIO_make_bio_pair(context->bioRead, context->bioWrite);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (status != 1)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "BIO_make_bio_pair failed");
|
|
|
|
goto out_bio_pair;
|
|
|
|
}
|
2012-12-24 05:20:41 +04:00
|
|
|
SSL_set_bio(context->ssl, context->bioRead, context->bioWrite);
|
2014-08-18 21:34:47 +04:00
|
|
|
context->ReadBuffer = (BYTE*) malloc(SCHANNEL_CB_MAX_TOKEN);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!context->ReadBuffer)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Failed to allocate ReadBuffer");
|
|
|
|
goto out_read_alloc;
|
|
|
|
}
|
2014-08-18 21:34:47 +04:00
|
|
|
context->WriteBuffer = (BYTE*) malloc(SCHANNEL_CB_MAX_TOKEN);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!context->WriteBuffer)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Failed to allocate ReadBuffer");
|
|
|
|
goto out_write_alloc;
|
|
|
|
}
|
2013-01-03 22:36:19 +04:00
|
|
|
return 0;
|
2015-04-03 17:21:01 +03:00
|
|
|
|
|
|
|
out_write_alloc:
|
|
|
|
free(context->ReadBuffer);
|
|
|
|
out_read_alloc:
|
|
|
|
out_bio_pair:
|
|
|
|
out_set_write_buf_write:
|
|
|
|
BIO_free(context->bioWrite);
|
|
|
|
out_bio_write_failed:
|
|
|
|
out_set_write_buf_read:
|
|
|
|
BIO_free(context->bioRead);
|
|
|
|
out_bio_read_failed:
|
|
|
|
SSL_free(context->ssl);
|
|
|
|
out_ssl_new_failed:
|
|
|
|
SSL_CTX_free(context->ctx);
|
|
|
|
return -1;
|
2013-01-03 22:36:19 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int schannel_openssl_server_init(SCHANNEL_OPENSSL* context)
|
2013-01-03 22:36:19 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
long options = 0;
|
2013-01-10 21:19:07 +04:00
|
|
|
//context->ctx = SSL_CTX_new(SSLv23_server_method());
|
|
|
|
context->ctx = SSL_CTX_new(TLSv1_server_method());
|
2012-12-24 05:20:41 +04:00
|
|
|
|
2013-01-03 22:36:19 +04:00
|
|
|
if (!context->ctx)
|
2012-12-24 05:20:41 +04:00
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "SSL_CTX_new failed");
|
2013-01-03 22:36:19 +04:00
|
|
|
return -1;
|
2012-12-24 05:20:41 +04:00
|
|
|
}
|
|
|
|
|
2013-01-03 22:36:19 +04:00
|
|
|
/*
|
|
|
|
* SSL_OP_NO_SSLv2:
|
|
|
|
*
|
|
|
|
* We only want SSLv3 and TLSv1, so disable SSLv2.
|
|
|
|
* SSLv3 is used by, eg. Microsoft RDC for Mac OS X.
|
|
|
|
*/
|
|
|
|
options |= SSL_OP_NO_SSLv2;
|
|
|
|
/**
|
|
|
|
* SSL_OP_NO_COMPRESSION:
|
|
|
|
*
|
|
|
|
* The Microsoft RDP server does not advertise support
|
|
|
|
* for TLS compression, but alternative servers may support it.
|
|
|
|
* This was observed between early versions of the FreeRDP server
|
|
|
|
* and the FreeRDP client, and caused major performance issues,
|
|
|
|
* which is why we're disabling it.
|
|
|
|
*/
|
|
|
|
#ifdef SSL_OP_NO_COMPRESSION
|
|
|
|
options |= SSL_OP_NO_COMPRESSION;
|
|
|
|
#endif
|
|
|
|
/**
|
|
|
|
* SSL_OP_TLS_BLOCK_PADDING_BUG:
|
|
|
|
*
|
|
|
|
* The Microsoft RDP server does *not* support TLS padding.
|
|
|
|
* It absolutely needs to be disabled otherwise it won't work.
|
|
|
|
*/
|
|
|
|
options |= SSL_OP_TLS_BLOCK_PADDING_BUG;
|
|
|
|
/**
|
|
|
|
* SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS:
|
|
|
|
*
|
|
|
|
* Just like TLS padding, the Microsoft RDP server does not
|
|
|
|
* support empty fragments. This needs to be disabled.
|
|
|
|
*/
|
|
|
|
options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
|
|
|
|
SSL_CTX_set_options(context->ctx, options);
|
|
|
|
|
2013-01-10 21:19:07 +04:00
|
|
|
if (SSL_CTX_use_RSAPrivateKey_file(context->ctx, "/tmp/localhost.key", SSL_FILETYPE_PEM) <= 0)
|
2013-01-03 22:36:19 +04:00
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "SSL_CTX_use_RSAPrivateKey_file failed");
|
2015-04-03 17:21:01 +03:00
|
|
|
goto out_rsa_key;
|
2013-01-03 22:36:19 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 21:19:07 +04:00
|
|
|
context->ssl = SSL_new(context->ctx);
|
|
|
|
|
|
|
|
if (!context->ssl)
|
2013-01-03 22:36:19 +04:00
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "SSL_new failed");
|
2015-04-03 17:21:01 +03:00
|
|
|
goto out_ssl_new;
|
2013-01-03 22:36:19 +04:00
|
|
|
}
|
|
|
|
|
2013-01-05 21:35:24 +04:00
|
|
|
if (SSL_use_certificate_file(context->ssl, "/tmp/localhost.crt", SSL_FILETYPE_PEM) <= 0)
|
2012-12-24 05:20:41 +04:00
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "SSL_use_certificate_file failed");
|
2015-04-03 17:21:01 +03:00
|
|
|
goto out_use_certificate;
|
2012-12-24 05:20:41 +04:00
|
|
|
}
|
2013-01-03 22:36:19 +04:00
|
|
|
|
|
|
|
context->bioRead = BIO_new(BIO_s_mem());
|
2012-12-24 05:20:41 +04:00
|
|
|
|
2013-01-03 22:36:19 +04:00
|
|
|
if (!context->bioRead)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "BIO_new failed");
|
2015-04-03 17:21:01 +03:00
|
|
|
goto out_bio_read;
|
2013-01-03 22:36:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
status = BIO_set_write_buf_size(context->bioRead, SCHANNEL_CB_MAX_TOKEN);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (status != 1)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "BIO_set_write_buf_size failed for bioRead");
|
|
|
|
goto out_set_write_buf_read;
|
|
|
|
}
|
2013-01-03 22:36:19 +04:00
|
|
|
context->bioWrite = BIO_new(BIO_s_mem());
|
|
|
|
|
|
|
|
if (!context->bioWrite)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "BIO_new failed");
|
2015-04-03 17:21:01 +03:00
|
|
|
goto out_bio_write;
|
2013-01-03 22:36:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
status = BIO_set_write_buf_size(context->bioWrite, SCHANNEL_CB_MAX_TOKEN);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (status != 1)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "BIO_set_write_buf_size failed for bioWrite");
|
|
|
|
goto out_set_write_buf_write;
|
|
|
|
}
|
2013-01-03 22:36:19 +04:00
|
|
|
status = BIO_make_bio_pair(context->bioRead, context->bioWrite);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (status != 1)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "BIO_make_bio_pair failed");
|
|
|
|
goto out_bio_pair;
|
|
|
|
}
|
2013-01-03 22:36:19 +04:00
|
|
|
SSL_set_bio(context->ssl, context->bioRead, context->bioWrite);
|
2014-08-18 21:34:47 +04:00
|
|
|
context->ReadBuffer = (BYTE*) malloc(SCHANNEL_CB_MAX_TOKEN);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!context->ReadBuffer)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Failed to allocate memory for ReadBuffer");
|
|
|
|
goto out_read_buffer;
|
|
|
|
}
|
2014-08-18 21:34:47 +04:00
|
|
|
context->WriteBuffer = (BYTE*) malloc(SCHANNEL_CB_MAX_TOKEN);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!context->WriteBuffer)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Failed to allocate memory for WriteBuffer");
|
|
|
|
goto out_write_buffer;
|
|
|
|
}
|
2012-12-24 05:20:41 +04:00
|
|
|
return 0;
|
2015-04-03 17:21:01 +03:00
|
|
|
|
|
|
|
out_write_buffer:
|
|
|
|
free(context->ReadBuffer);
|
|
|
|
out_read_buffer:
|
|
|
|
out_bio_pair:
|
|
|
|
out_set_write_buf_write:
|
|
|
|
BIO_free(context->bioWrite);
|
|
|
|
out_bio_write:
|
|
|
|
out_set_write_buf_read:
|
|
|
|
BIO_free(context->bioRead);
|
|
|
|
out_bio_read:
|
|
|
|
out_use_certificate:
|
|
|
|
SSL_free(context->ssl);
|
|
|
|
out_ssl_new:
|
|
|
|
out_rsa_key:
|
|
|
|
SSL_CTX_free(context->ctx);
|
|
|
|
return -1;
|
2012-12-24 05:20:41 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
SECURITY_STATUS schannel_openssl_client_process_tokens(SCHANNEL_OPENSSL* context, PSecBufferDesc pInput, PSecBufferDesc pOutput)
|
2013-01-03 22:36:19 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
int ssl_error;
|
|
|
|
PSecBuffer pBuffer;
|
|
|
|
|
|
|
|
if (!context->connected)
|
|
|
|
{
|
|
|
|
if (pInput)
|
|
|
|
{
|
|
|
|
if (pInput->cBuffers < 1)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2013-01-11 23:23:54 +04:00
|
|
|
pBuffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
|
2013-01-03 22:36:19 +04:00
|
|
|
|
2013-01-11 23:23:54 +04:00
|
|
|
if (!pBuffer)
|
2013-01-03 22:36:19 +04:00
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
|
|
|
status = BIO_write(context->bioRead, pBuffer->pvBuffer, pBuffer->cbBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
status = SSL_connect(context->ssl);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
ssl_error = SSL_get_error(context->ssl, status);
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "SSL_connect error: %s", openssl_get_ssl_error_string(ssl_error));
|
2013-01-03 22:36:19 +04:00
|
|
|
}
|
|
|
|
|
2013-02-19 06:50:33 +04:00
|
|
|
if (status == 1)
|
|
|
|
context->connected = TRUE;
|
|
|
|
|
2013-01-03 22:36:19 +04:00
|
|
|
status = BIO_read(context->bioWrite, context->ReadBuffer, SCHANNEL_CB_MAX_TOKEN);
|
|
|
|
|
|
|
|
if (pOutput->cBuffers < 1)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2013-01-11 23:23:54 +04:00
|
|
|
pBuffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
|
2013-01-03 22:36:19 +04:00
|
|
|
|
2013-01-11 23:23:54 +04:00
|
|
|
if (!pBuffer)
|
2013-01-03 22:36:19 +04:00
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2013-01-11 02:12:27 +04:00
|
|
|
if (status > 0)
|
|
|
|
{
|
2014-06-06 06:10:08 +04:00
|
|
|
if (pBuffer->cbBuffer < (unsigned long) status)
|
2013-01-11 02:12:27 +04:00
|
|
|
return SEC_E_INSUFFICIENT_MEMORY;
|
2013-01-03 22:36:19 +04:00
|
|
|
|
2013-01-11 02:12:27 +04:00
|
|
|
CopyMemory(pBuffer->pvBuffer, context->ReadBuffer, status);
|
|
|
|
pBuffer->cbBuffer = status;
|
2013-02-19 06:50:33 +04:00
|
|
|
return (context->connected) ? SEC_E_OK : SEC_I_CONTINUE_NEEDED;
|
2013-01-11 02:12:27 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pBuffer->cbBuffer = 0;
|
2013-02-19 06:50:33 +04:00
|
|
|
return (context->connected) ? SEC_E_OK : SEC_I_CONTINUE_NEEDED;
|
2013-01-11 02:12:27 +04:00
|
|
|
}
|
2013-01-03 22:36:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
SECURITY_STATUS schannel_openssl_server_process_tokens(SCHANNEL_OPENSSL* context, PSecBufferDesc pInput, PSecBufferDesc pOutput)
|
2013-01-03 22:36:19 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
int ssl_error;
|
|
|
|
PSecBuffer pBuffer;
|
|
|
|
|
|
|
|
if (!context->connected)
|
|
|
|
{
|
|
|
|
if (pInput->cBuffers < 1)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2013-01-11 23:23:54 +04:00
|
|
|
pBuffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
|
2013-01-03 22:36:19 +04:00
|
|
|
|
2013-01-11 23:23:54 +04:00
|
|
|
if (!pBuffer)
|
2013-01-03 22:36:19 +04:00
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
|
|
|
status = BIO_write(context->bioRead, pBuffer->pvBuffer, pBuffer->cbBuffer);
|
|
|
|
status = SSL_accept(context->ssl);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
ssl_error = SSL_get_error(context->ssl, status);
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "SSL_accept error: %s", openssl_get_ssl_error_string(ssl_error));
|
2013-01-03 22:36:19 +04:00
|
|
|
}
|
|
|
|
|
2013-02-19 05:07:29 +04:00
|
|
|
if (status == 1)
|
|
|
|
context->connected = TRUE;
|
|
|
|
|
2013-01-03 22:36:19 +04:00
|
|
|
status = BIO_read(context->bioWrite, context->ReadBuffer, SCHANNEL_CB_MAX_TOKEN);
|
|
|
|
|
|
|
|
if (pOutput->cBuffers < 1)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2013-01-11 23:23:54 +04:00
|
|
|
pBuffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
|
2013-01-03 22:36:19 +04:00
|
|
|
|
2013-01-11 23:23:54 +04:00
|
|
|
if (!pBuffer)
|
2013-01-03 22:36:19 +04:00
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2013-01-11 02:12:27 +04:00
|
|
|
if (status > 0)
|
|
|
|
{
|
2014-06-06 06:10:08 +04:00
|
|
|
if (pBuffer->cbBuffer < (unsigned long) status)
|
2013-01-11 02:12:27 +04:00
|
|
|
return SEC_E_INSUFFICIENT_MEMORY;
|
2013-01-03 22:36:19 +04:00
|
|
|
|
2013-01-11 02:12:27 +04:00
|
|
|
CopyMemory(pBuffer->pvBuffer, context->ReadBuffer, status);
|
|
|
|
pBuffer->cbBuffer = status;
|
2013-02-19 05:07:29 +04:00
|
|
|
return (context->connected) ? SEC_E_OK : SEC_I_CONTINUE_NEEDED;
|
2013-01-11 02:12:27 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pBuffer->cbBuffer = 0;
|
2013-02-19 05:07:29 +04:00
|
|
|
return (context->connected) ? SEC_E_OK : SEC_I_CONTINUE_NEEDED;
|
2013-01-11 02:12:27 +04:00
|
|
|
}
|
2013-01-03 22:36:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
SECURITY_STATUS schannel_openssl_encrypt_message(SCHANNEL_OPENSSL* context, PSecBufferDesc pMessage)
|
2013-01-11 23:23:54 +04:00
|
|
|
{
|
|
|
|
int status;
|
2013-02-19 06:50:33 +04:00
|
|
|
int length;
|
|
|
|
int offset;
|
2013-01-11 23:23:54 +04:00
|
|
|
int ssl_error;
|
|
|
|
PSecBuffer pStreamBodyBuffer;
|
|
|
|
PSecBuffer pStreamHeaderBuffer;
|
|
|
|
PSecBuffer pStreamTrailerBuffer;
|
|
|
|
pStreamHeaderBuffer = sspi_FindSecBuffer(pMessage, SECBUFFER_STREAM_HEADER);
|
|
|
|
pStreamBodyBuffer = sspi_FindSecBuffer(pMessage, SECBUFFER_DATA);
|
|
|
|
pStreamTrailerBuffer = sspi_FindSecBuffer(pMessage, SECBUFFER_STREAM_TRAILER);
|
|
|
|
|
|
|
|
if ((!pStreamHeaderBuffer) || (!pStreamBodyBuffer) || (!pStreamTrailerBuffer))
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
|
|
|
status = SSL_write(context->ssl, pStreamBodyBuffer->pvBuffer, pStreamBodyBuffer->cbBuffer);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
ssl_error = SSL_get_error(context->ssl, status);
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "SSL_write: %s", openssl_get_ssl_error_string(ssl_error));
|
2013-01-11 23:23:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
status = BIO_read(context->bioWrite, context->ReadBuffer, SCHANNEL_CB_MAX_TOKEN);
|
|
|
|
|
2013-02-19 06:50:33 +04:00
|
|
|
if (status > 0)
|
|
|
|
{
|
|
|
|
offset = 0;
|
2014-06-06 06:10:08 +04:00
|
|
|
length = (pStreamHeaderBuffer->cbBuffer > (unsigned long) status) ? status : pStreamHeaderBuffer->cbBuffer;
|
2013-02-19 06:50:33 +04:00
|
|
|
CopyMemory(pStreamHeaderBuffer->pvBuffer, &context->ReadBuffer[offset], length);
|
|
|
|
status -= length;
|
|
|
|
offset += length;
|
2014-06-06 06:10:08 +04:00
|
|
|
length = (pStreamBodyBuffer->cbBuffer > (unsigned long) status) ? status : pStreamBodyBuffer->cbBuffer;
|
2013-02-19 06:50:33 +04:00
|
|
|
CopyMemory(pStreamBodyBuffer->pvBuffer, &context->ReadBuffer[offset], length);
|
|
|
|
status -= length;
|
|
|
|
offset += length;
|
2014-06-06 06:10:08 +04:00
|
|
|
length = (pStreamTrailerBuffer->cbBuffer > (unsigned long) status) ? status : pStreamTrailerBuffer->cbBuffer;
|
2013-02-19 06:50:33 +04:00
|
|
|
CopyMemory(pStreamTrailerBuffer->pvBuffer, &context->ReadBuffer[offset], length);
|
|
|
|
status -= length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
2013-01-11 23:23:54 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
SECURITY_STATUS schannel_openssl_decrypt_message(SCHANNEL_OPENSSL* context, PSecBufferDesc pMessage)
|
2013-01-11 23:23:54 +04:00
|
|
|
{
|
2013-02-19 06:50:33 +04:00
|
|
|
int status;
|
|
|
|
int length;
|
2014-08-18 21:34:47 +04:00
|
|
|
BYTE* buffer;
|
2013-02-19 06:50:33 +04:00
|
|
|
int ssl_error;
|
2013-01-11 23:23:54 +04:00
|
|
|
PSecBuffer pBuffer;
|
|
|
|
pBuffer = sspi_FindSecBuffer(pMessage, SECBUFFER_DATA);
|
|
|
|
|
|
|
|
if (!pBuffer)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
2013-02-19 06:50:33 +04:00
|
|
|
status = BIO_write(context->bioRead, pBuffer->pvBuffer, pBuffer->cbBuffer);
|
|
|
|
status = SSL_read(context->ssl, pBuffer->pvBuffer, pBuffer->cbBuffer);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
ssl_error = SSL_get_error(context->ssl, status);
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "SSL_read: %s", openssl_get_ssl_error_string(ssl_error));
|
2013-02-19 06:50:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
length = status;
|
|
|
|
buffer = pBuffer->pvBuffer;
|
|
|
|
pMessage->pBuffers[0].BufferType = SECBUFFER_STREAM_HEADER;
|
|
|
|
pMessage->pBuffers[0].cbBuffer = 5;
|
|
|
|
pMessage->pBuffers[1].BufferType = SECBUFFER_DATA;
|
|
|
|
pMessage->pBuffers[1].pvBuffer = buffer;
|
|
|
|
pMessage->pBuffers[1].cbBuffer = length;
|
|
|
|
pMessage->pBuffers[2].BufferType = SECBUFFER_STREAM_TRAILER;
|
|
|
|
pMessage->pBuffers[2].cbBuffer = 36;
|
|
|
|
pMessage->pBuffers[3].BufferType = SECBUFFER_EMPTY;
|
|
|
|
pMessage->pBuffers[3].cbBuffer = 0;
|
|
|
|
return SEC_E_OK;
|
2013-01-11 23:23:54 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
SCHANNEL_OPENSSL* schannel_openssl_new()
|
2012-12-24 05:20:41 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
SCHANNEL_OPENSSL* context;
|
2015-04-03 17:21:01 +03:00
|
|
|
context = (SCHANNEL_OPENSSL*) calloc(1, sizeof(SCHANNEL_OPENSSL));
|
2012-12-24 05:20:41 +04:00
|
|
|
|
|
|
|
if (context != NULL)
|
|
|
|
{
|
2014-07-28 23:55:57 +04:00
|
|
|
winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
|
2013-01-03 22:36:19 +04:00
|
|
|
context->connected = FALSE;
|
2012-12-24 05:20:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void schannel_openssl_free(SCHANNEL_OPENSSL* context)
|
2012-12-24 05:20:41 +04:00
|
|
|
{
|
2013-01-03 22:36:19 +04:00
|
|
|
if (context)
|
|
|
|
{
|
|
|
|
free(context->ReadBuffer);
|
|
|
|
free(context->WriteBuffer);
|
|
|
|
free(context);
|
|
|
|
}
|
2012-12-24 05:20:41 +04:00
|
|
|
}
|