2012-04-09 15:40:06 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012, Marc Hoersken, <info@marc-hoersken.de>, et al.
|
2012-06-19 04:15:02 +02:00
|
|
|
* Copyright (C) 2012, Mark Salisbury, <mark.salisbury@hp.com>
|
2013-01-03 03:53:06 +01:00
|
|
|
* Copyright (C) 2012 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2012-04-09 15:40:06 +02:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Source file for all SChannel-specific code for the TLS/SSL layer. No code
|
2013-01-03 03:53:06 +01:00
|
|
|
* but curl_sslgen.c should ever call or use these functions.
|
2012-04-09 15:40:06 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO list for TLS/SSL implementation:
|
2012-04-10 21:21:31 +02:00
|
|
|
* - implement client certificate authentication
|
|
|
|
* - implement custom server certificate validation
|
|
|
|
* - implement cipher/algorithm option
|
|
|
|
*
|
|
|
|
* Related articles on MSDN:
|
|
|
|
* - Getting a Certificate for Schannel
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa375447.aspx
|
|
|
|
* - Specifying Schannel Ciphers and Cipher Strengths
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa380161.aspx
|
2012-04-09 15:40:06 +02:00
|
|
|
*/
|
|
|
|
|
2012-12-28 12:40:20 +01:00
|
|
|
#include "curl_setup.h"
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
#ifdef USE_SCHANNEL
|
|
|
|
|
2012-06-13 15:53:51 +02:00
|
|
|
#ifndef USE_WINDOWS_SSPI
|
|
|
|
# error "Can't compile SCHANNEL support without SSPI."
|
|
|
|
#endif
|
|
|
|
|
2012-04-09 15:40:06 +02:00
|
|
|
#include "curl_sspi.h"
|
|
|
|
#include "curl_schannel.h"
|
2012-12-28 12:40:20 +01:00
|
|
|
#include "curl_sslgen.h"
|
|
|
|
#include "curl_sendf.h"
|
|
|
|
#include "curl_connect.h" /* for the connect timeout */
|
|
|
|
#include "curl_strerror.h"
|
|
|
|
#include "curl_select.h" /* for the socket readyness */
|
|
|
|
#include "curl_inet_pton.h" /* for IP addr SNI check */
|
2012-06-15 18:05:11 +02:00
|
|
|
#include "curl_multibyte.h"
|
2012-12-28 12:40:20 +01:00
|
|
|
#include "curl_warnless.h"
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
2012-06-15 18:05:11 +02:00
|
|
|
|
2012-04-09 15:40:06 +02:00
|
|
|
#include "curl_memory.h"
|
|
|
|
/* The last #include file should be: */
|
2012-12-28 12:40:20 +01:00
|
|
|
#include "curl_memdebug.h"
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
/* Uncomment to force verbose output
|
|
|
|
* #define infof(x, y, ...) printf(y, __VA_ARGS__)
|
|
|
|
* #define failf(x, y, ...) printf(y, __VA_ARGS__)
|
|
|
|
*/
|
|
|
|
|
|
|
|
static Curl_recv schannel_recv;
|
|
|
|
static Curl_send schannel_send;
|
|
|
|
|
2012-06-19 04:15:02 +02:00
|
|
|
#ifdef _WIN32_WCE
|
|
|
|
static CURLcode verify_certificate(struct connectdata *conn, int sockindex);
|
|
|
|
#endif
|
|
|
|
|
2012-06-19 04:15:01 +02:00
|
|
|
static void InitSecBuffer(SecBuffer *buffer, unsigned long BufType,
|
|
|
|
void *BufDataPtr, unsigned long BufByteSize)
|
|
|
|
{
|
|
|
|
buffer->cbBuffer = BufByteSize;
|
|
|
|
buffer->BufferType = BufType;
|
|
|
|
buffer->pvBuffer = BufDataPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void InitSecBufferDesc(SecBufferDesc *desc, SecBuffer *BufArr,
|
|
|
|
unsigned long NumArrElem)
|
|
|
|
{
|
|
|
|
desc->ulVersion = SECBUFFER_VERSION;
|
|
|
|
desc->pBuffers = BufArr;
|
|
|
|
desc->cBuffers = NumArrElem;
|
|
|
|
}
|
|
|
|
|
2012-04-09 15:40:06 +02:00
|
|
|
static CURLcode
|
2012-04-15 04:12:26 +02:00
|
|
|
schannel_connect_step1(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
2012-06-14 11:53:46 +02:00
|
|
|
ssize_t written = -1;
|
2012-04-09 15:40:06 +02:00
|
|
|
struct SessionHandle *data = conn->data;
|
2012-04-15 04:12:26 +02:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2012-04-09 15:40:06 +02:00
|
|
|
SecBuffer outbuf;
|
|
|
|
SecBufferDesc outbuf_desc;
|
|
|
|
SCHANNEL_CRED schannel_cred;
|
|
|
|
SECURITY_STATUS sspi_status = SEC_E_OK;
|
2012-04-15 04:12:26 +02:00
|
|
|
struct curl_schannel_cred *old_cred = NULL;
|
2012-04-09 15:40:06 +02:00
|
|
|
struct in_addr addr;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct in6_addr addr6;
|
|
|
|
#endif
|
2012-06-16 19:20:50 +02:00
|
|
|
TCHAR *host_name;
|
2012-06-19 20:28:22 +02:00
|
|
|
CURLcode code;
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-06-19 20:28:21 +02:00
|
|
|
infof(data, "schannel: SSL/TLS connection with %s port %hu (step 1/3)\n",
|
2012-04-09 15:40:06 +02:00
|
|
|
conn->host.name, conn->remote_port);
|
|
|
|
|
2012-04-09 23:24:55 +02:00
|
|
|
/* check for an existing re-usable credential handle */
|
2012-04-13 13:05:26 +02:00
|
|
|
if(!Curl_ssl_getsessionid(conn, (void**)&old_cred, NULL)) {
|
2012-04-09 23:24:55 +02:00
|
|
|
connssl->cred = old_cred;
|
|
|
|
infof(data, "schannel: re-using existing credential handle\n");
|
2012-04-09 18:35:00 +02:00
|
|
|
}
|
|
|
|
else {
|
2012-04-09 23:24:55 +02:00
|
|
|
/* setup Schannel API options */
|
|
|
|
memset(&schannel_cred, 0, sizeof(schannel_cred));
|
|
|
|
schannel_cred.dwVersion = SCHANNEL_CRED_VERSION;
|
|
|
|
|
|
|
|
if(data->set.ssl.verifypeer) {
|
2012-06-19 04:15:02 +02:00
|
|
|
#ifdef _WIN32_WCE
|
|
|
|
/* certificate validation on CE doesn't seem to work right; we'll
|
|
|
|
do it following a more manual process. */
|
|
|
|
schannel_cred.dwFlags = SCH_CRED_MANUAL_CRED_VALIDATION |
|
|
|
|
SCH_CRED_IGNORE_NO_REVOCATION_CHECK |
|
|
|
|
SCH_CRED_IGNORE_REVOCATION_OFFLINE;
|
|
|
|
#else
|
2012-04-09 23:24:55 +02:00
|
|
|
schannel_cred.dwFlags = SCH_CRED_AUTO_CRED_VALIDATION |
|
|
|
|
SCH_CRED_REVOCATION_CHECK_CHAIN;
|
2012-06-19 04:15:02 +02:00
|
|
|
#endif
|
2012-04-15 04:12:26 +02:00
|
|
|
infof(data, "schannel: checking server certificate revocation\n");
|
2012-04-09 23:24:55 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
schannel_cred.dwFlags = SCH_CRED_MANUAL_CRED_VALIDATION |
|
|
|
|
SCH_CRED_IGNORE_NO_REVOCATION_CHECK |
|
|
|
|
SCH_CRED_IGNORE_REVOCATION_OFFLINE;
|
2012-04-15 04:12:26 +02:00
|
|
|
infof(data, "schannel: disable server certificate revocation checks\n");
|
2012-04-09 23:24:55 +02:00
|
|
|
}
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-11-03 02:06:51 +01:00
|
|
|
if(Curl_inet_pton(AF_INET, conn->host.name, &addr)
|
2012-04-09 15:40:06 +02:00
|
|
|
#ifdef ENABLE_IPV6
|
2012-11-03 02:06:51 +01:00
|
|
|
|| Curl_inet_pton(AF_INET6, conn->host.name, &addr6)
|
2012-04-09 15:40:06 +02:00
|
|
|
#endif
|
2012-11-03 02:06:51 +01:00
|
|
|
) {
|
2012-04-09 23:24:55 +02:00
|
|
|
schannel_cred.dwFlags |= SCH_CRED_NO_SERVERNAME_CHECK;
|
2012-11-03 02:06:51 +01:00
|
|
|
infof(data, "schannel: using IP address, SNI is being disabled by "
|
|
|
|
"disabling the servername check against the "
|
|
|
|
"subject names in server certificates.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!data->set.ssl.verifyhost) {
|
|
|
|
schannel_cred.dwFlags |= SCH_CRED_NO_SERVERNAME_CHECK;
|
|
|
|
infof(data, "schannel: verifyhost setting prevents Schannel from "
|
|
|
|
"comparing the supplied target name with the subject "
|
|
|
|
"names in server certificates. Also disables SNI.\n");
|
2012-04-09 23:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(data->set.ssl.version) {
|
|
|
|
case CURL_SSLVERSION_TLSv1:
|
|
|
|
schannel_cred.grbitEnabledProtocols = SP_PROT_TLS1_0_CLIENT |
|
|
|
|
SP_PROT_TLS1_1_CLIENT |
|
|
|
|
SP_PROT_TLS1_2_CLIENT;
|
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_SSLv3:
|
|
|
|
schannel_cred.grbitEnabledProtocols = SP_PROT_SSL3_CLIENT;
|
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_SSLv2:
|
|
|
|
schannel_cred.grbitEnabledProtocols = SP_PROT_SSL2_CLIENT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate memory for the re-usable credential handle */
|
2012-04-15 04:12:26 +02:00
|
|
|
connssl->cred = malloc(sizeof(struct curl_schannel_cred));
|
|
|
|
if(!connssl->cred) {
|
2012-04-09 23:24:55 +02:00
|
|
|
failf(data, "schannel: unable to allocate memory");
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-04-15 04:12:26 +02:00
|
|
|
memset(connssl->cred, 0, sizeof(struct curl_schannel_cred));
|
2012-04-09 23:24:55 +02:00
|
|
|
|
|
|
|
/* http://msdn.microsoft.com/en-us/library/windows/desktop/aa374716.aspx */
|
2012-07-05 22:16:15 +02:00
|
|
|
sspi_status = s_pSecFn->AcquireCredentialsHandle(NULL, (TCHAR *)UNISP_NAME,
|
2012-06-14 12:05:48 +02:00
|
|
|
SECPKG_CRED_OUTBOUND, NULL, &schannel_cred, NULL, NULL,
|
2012-04-09 23:24:55 +02:00
|
|
|
&connssl->cred->cred_handle, &connssl->cred->time_stamp);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-04-09 23:24:55 +02:00
|
|
|
if(sspi_status != SEC_E_OK) {
|
|
|
|
if(sspi_status == SEC_E_WRONG_PRINCIPAL)
|
2012-06-14 13:32:05 +02:00
|
|
|
failf(data, "schannel: SNI or certificate check failed: %s",
|
2012-06-12 01:06:48 +02:00
|
|
|
Curl_sspi_strerror(conn, sspi_status));
|
2012-04-09 23:24:55 +02:00
|
|
|
else
|
2012-06-15 18:05:11 +02:00
|
|
|
failf(data, "schannel: AcquireCredentialsHandle failed: %s",
|
2012-06-12 01:06:48 +02:00
|
|
|
Curl_sspi_strerror(conn, sspi_status));
|
2012-06-25 07:16:26 +02:00
|
|
|
Curl_safefree(connssl->cred);
|
2012-04-09 23:24:55 +02:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
/* setup output buffer */
|
2012-06-19 04:15:01 +02:00
|
|
|
InitSecBuffer(&outbuf, SECBUFFER_EMPTY, NULL, 0);
|
|
|
|
InitSecBufferDesc(&outbuf_desc, &outbuf, 1);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
/* setup request flags */
|
|
|
|
connssl->req_flags = ISC_REQ_SEQUENCE_DETECT | ISC_REQ_REPLAY_DETECT |
|
2012-04-09 22:33:58 +02:00
|
|
|
ISC_REQ_CONFIDENTIALITY | ISC_REQ_EXTENDED_ERROR |
|
|
|
|
ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_STREAM;
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-04-09 23:24:55 +02:00
|
|
|
/* allocate memory for the security context handle */
|
2012-04-15 04:12:26 +02:00
|
|
|
connssl->ctxt = malloc(sizeof(struct curl_schannel_ctxt));
|
|
|
|
if(!connssl->ctxt) {
|
2012-04-09 23:24:55 +02:00
|
|
|
failf(data, "schannel: unable to allocate memory");
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-04-15 04:12:26 +02:00
|
|
|
memset(connssl->ctxt, 0, sizeof(struct curl_schannel_ctxt));
|
2012-04-09 23:24:55 +02:00
|
|
|
|
2012-07-05 22:16:15 +02:00
|
|
|
host_name = Curl_convert_UTF8_to_tchar(conn->host.name);
|
2012-06-15 18:05:11 +02:00
|
|
|
if(!host_name)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2012-04-09 15:40:06 +02:00
|
|
|
/* http://msdn.microsoft.com/en-us/library/windows/desktop/aa375924.aspx */
|
2012-06-15 18:05:11 +02:00
|
|
|
|
2012-04-13 13:10:09 +02:00
|
|
|
sspi_status = s_pSecFn->InitializeSecurityContext(
|
2012-06-15 18:05:11 +02:00
|
|
|
&connssl->cred->cred_handle, NULL, host_name,
|
2012-04-09 23:24:55 +02:00
|
|
|
connssl->req_flags, 0, 0, NULL, 0, &connssl->ctxt->ctxt_handle,
|
|
|
|
&outbuf_desc, &connssl->ret_flags, &connssl->ctxt->time_stamp);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-07-05 22:16:15 +02:00
|
|
|
Curl_unicodefree(host_name);
|
2012-06-15 18:05:11 +02:00
|
|
|
|
2012-04-09 15:40:06 +02:00
|
|
|
if(sspi_status != SEC_I_CONTINUE_NEEDED) {
|
|
|
|
if(sspi_status == SEC_E_WRONG_PRINCIPAL)
|
2012-06-14 13:32:05 +02:00
|
|
|
failf(data, "schannel: SNI or certificate check failed: %s",
|
2012-06-12 01:06:48 +02:00
|
|
|
Curl_sspi_strerror(conn, sspi_status));
|
2012-04-09 15:40:06 +02:00
|
|
|
else
|
2012-06-15 18:05:11 +02:00
|
|
|
failf(data, "schannel: initial InitializeSecurityContext failed: %s",
|
2012-06-12 01:06:48 +02:00
|
|
|
Curl_sspi_strerror(conn, sspi_status));
|
2012-06-25 07:16:26 +02:00
|
|
|
Curl_safefree(connssl->ctxt);
|
2012-04-09 15:40:06 +02:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-06-14 16:55:26 +02:00
|
|
|
infof(data, "schannel: sending initial handshake data: "
|
|
|
|
"sending %lu bytes...\n", outbuf.cbBuffer);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
/* send initial handshake data which is now stored in output buffer */
|
2012-06-19 20:28:22 +02:00
|
|
|
code = Curl_write_plain(conn, conn->sock[sockindex], outbuf.pvBuffer,
|
|
|
|
outbuf.cbBuffer, &written);
|
2012-04-09 15:40:06 +02:00
|
|
|
s_pSecFn->FreeContextBuffer(outbuf.pvBuffer);
|
2012-06-19 20:28:22 +02:00
|
|
|
if((code != CURLE_OK) || (outbuf.cbBuffer != (size_t)written)) {
|
2012-06-14 16:55:26 +02:00
|
|
|
failf(data, "schannel: failed to send initial handshake data: "
|
|
|
|
"sent %zd of %lu bytes", written, outbuf.cbBuffer);
|
2012-04-09 15:40:06 +02:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-06-14 16:55:26 +02:00
|
|
|
infof(data, "schannel: sent initial handshake data: "
|
|
|
|
"sent %zd bytes\n", written);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
/* continue to second handshake step */
|
|
|
|
connssl->connecting_state = ssl_connect_2;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode
|
2012-04-15 04:12:26 +02:00
|
|
|
schannel_connect_step2(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
2012-04-09 15:40:06 +02:00
|
|
|
int i;
|
2012-06-14 11:53:46 +02:00
|
|
|
ssize_t nread = -1, written = -1;
|
2012-04-09 15:40:06 +02:00
|
|
|
struct SessionHandle *data = conn->data;
|
2012-04-15 04:12:26 +02:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2012-04-09 15:40:06 +02:00
|
|
|
SecBuffer outbuf[2];
|
|
|
|
SecBufferDesc outbuf_desc;
|
|
|
|
SecBuffer inbuf[2];
|
|
|
|
SecBufferDesc inbuf_desc;
|
|
|
|
SECURITY_STATUS sspi_status = SEC_E_OK;
|
2012-06-16 19:20:50 +02:00
|
|
|
TCHAR *host_name;
|
2012-06-19 20:28:22 +02:00
|
|
|
CURLcode code;
|
2012-06-20 22:26:51 +02:00
|
|
|
bool doread;
|
|
|
|
|
2012-10-06 13:59:28 +02:00
|
|
|
doread = (connssl->connecting_state != ssl_connect_2_writing) ? TRUE : FALSE;
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-06-19 20:28:21 +02:00
|
|
|
infof(data, "schannel: SSL/TLS connection with %s port %hu (step 2/3)\n",
|
2012-04-09 15:40:06 +02:00
|
|
|
conn->host.name, conn->remote_port);
|
|
|
|
|
|
|
|
/* buffer to store previously received and encrypted data */
|
|
|
|
if(connssl->encdata_buffer == NULL) {
|
|
|
|
connssl->encdata_offset = 0;
|
2012-04-15 04:12:26 +02:00
|
|
|
connssl->encdata_length = CURL_SCHANNEL_BUFFER_INIT_SIZE;
|
2012-04-09 15:40:06 +02:00
|
|
|
connssl->encdata_buffer = malloc(connssl->encdata_length);
|
|
|
|
if(connssl->encdata_buffer == NULL) {
|
|
|
|
failf(data, "schannel: unable to allocate memory");
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
/* if we need a bigger buffer to read a full message, increase buffer now */
|
2012-06-20 07:49:34 +02:00
|
|
|
if(connssl->encdata_length - connssl->encdata_offset <
|
2012-10-06 13:59:28 +02:00
|
|
|
CURL_SCHANNEL_BUFFER_FREE_SIZE) {
|
2012-06-20 00:51:03 +02:00
|
|
|
/* increase internal encrypted data buffer */
|
2012-06-20 07:49:34 +02:00
|
|
|
connssl->encdata_length *= CURL_SCHANNEL_BUFFER_STEP_FACTOR;
|
2012-06-20 00:51:03 +02:00
|
|
|
connssl->encdata_buffer = realloc(connssl->encdata_buffer,
|
|
|
|
connssl->encdata_length);
|
2012-06-20 07:49:34 +02:00
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
if(connssl->encdata_buffer == NULL) {
|
|
|
|
failf(data, "schannel: unable to re-allocate memory");
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
for(;;) {
|
2012-06-20 00:51:03 +02:00
|
|
|
if(doread) {
|
|
|
|
/* read encrypted handshake data from socket */
|
|
|
|
code = Curl_read_plain(conn->sock[sockindex],
|
2012-06-19 20:28:22 +02:00
|
|
|
(char *) (connssl->encdata_buffer + connssl->encdata_offset),
|
|
|
|
connssl->encdata_length - connssl->encdata_offset,
|
|
|
|
&nread);
|
2012-06-20 00:51:03 +02:00
|
|
|
if(code == CURLE_AGAIN) {
|
|
|
|
if(connssl->connecting_state != ssl_connect_2_writing)
|
|
|
|
connssl->connecting_state = ssl_connect_2_reading;
|
|
|
|
infof(data, "schannel: failed to receive handshake, "
|
|
|
|
"need more data\n");
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
else if((code != CURLE_OK) || (nread == 0)) {
|
|
|
|
failf(data, "schannel: failed to receive handshake, "
|
|
|
|
"SSL/TLS connection failed");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
/* increase encrypted data buffer offset */
|
|
|
|
connssl->encdata_offset += nread;
|
|
|
|
}
|
2012-06-19 20:28:22 +02:00
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
infof(data, "schannel: encrypted data buffer: offset %zu length %zu\n",
|
2012-06-14 16:55:26 +02:00
|
|
|
connssl->encdata_offset, connssl->encdata_length);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
/* setup input buffers */
|
|
|
|
InitSecBuffer(&inbuf[0], SECBUFFER_TOKEN, malloc(connssl->encdata_offset),
|
|
|
|
curlx_uztoul(connssl->encdata_offset));
|
|
|
|
InitSecBuffer(&inbuf[1], SECBUFFER_EMPTY, NULL, 0);
|
|
|
|
InitSecBufferDesc(&inbuf_desc, inbuf, 2);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
/* setup output buffers */
|
|
|
|
InitSecBuffer(&outbuf[0], SECBUFFER_TOKEN, NULL, 0);
|
|
|
|
InitSecBuffer(&outbuf[1], SECBUFFER_ALERT, NULL, 0);
|
|
|
|
InitSecBufferDesc(&outbuf_desc, outbuf, 2);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
if(inbuf[0].pvBuffer == NULL) {
|
|
|
|
failf(data, "schannel: unable to allocate memory");
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
/* copy received handshake data into input buffer */
|
|
|
|
memcpy(inbuf[0].pvBuffer, connssl->encdata_buffer,
|
|
|
|
connssl->encdata_offset);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-07-05 22:16:15 +02:00
|
|
|
host_name = Curl_convert_UTF8_to_tchar(conn->host.name);
|
2012-06-20 00:51:03 +02:00
|
|
|
if(!host_name)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2012-06-15 18:05:11 +02:00
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
/* http://msdn.microsoft.com/en-us/library/windows/desktop/aa375924.aspx */
|
2012-06-15 18:05:11 +02:00
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
sspi_status = s_pSecFn->InitializeSecurityContext(
|
|
|
|
&connssl->cred->cred_handle, &connssl->ctxt->ctxt_handle,
|
|
|
|
host_name, connssl->req_flags, 0, 0, &inbuf_desc, 0, NULL,
|
|
|
|
&outbuf_desc, &connssl->ret_flags, &connssl->ctxt->time_stamp);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-07-05 22:16:15 +02:00
|
|
|
Curl_unicodefree(host_name);
|
2012-06-15 18:05:11 +02:00
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
/* free buffer for received handshake data */
|
2012-06-25 07:16:26 +02:00
|
|
|
Curl_safefree(inbuf[0].pvBuffer);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
/* check if the handshake was incomplete */
|
|
|
|
if(sspi_status == SEC_E_INCOMPLETE_MESSAGE) {
|
|
|
|
connssl->connecting_state = ssl_connect_2_reading;
|
|
|
|
infof(data, "schannel: received incomplete message, need more data\n");
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
/* check if the handshake needs to be continued */
|
|
|
|
if(sspi_status == SEC_I_CONTINUE_NEEDED || sspi_status == SEC_E_OK) {
|
|
|
|
for(i = 0; i < 2; i++) {
|
|
|
|
/* search for handshake tokens that need to be send */
|
|
|
|
if(outbuf[i].BufferType == SECBUFFER_TOKEN && outbuf[i].cbBuffer > 0) {
|
|
|
|
infof(data, "schannel: sending next handshake data: "
|
|
|
|
"sending %lu bytes...\n", outbuf[i].cbBuffer);
|
|
|
|
|
|
|
|
/* send handshake token to server */
|
|
|
|
code = Curl_write_plain(conn, conn->sock[sockindex],
|
|
|
|
outbuf[i].pvBuffer, outbuf[i].cbBuffer,
|
|
|
|
&written);
|
|
|
|
if((code != CURLE_OK) || (outbuf[i].cbBuffer != (size_t)written)) {
|
|
|
|
failf(data, "schannel: failed to send next handshake data: "
|
|
|
|
"sent %zd of %lu bytes", written, outbuf[i].cbBuffer);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
2012-04-09 15:40:06 +02:00
|
|
|
}
|
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
/* free obsolete buffer */
|
|
|
|
if(outbuf[i].pvBuffer != NULL) {
|
|
|
|
s_pSecFn->FreeContextBuffer(outbuf[i].pvBuffer);
|
|
|
|
}
|
2012-04-09 15:40:06 +02:00
|
|
|
}
|
|
|
|
}
|
2012-06-20 00:51:03 +02:00
|
|
|
else {
|
|
|
|
if(sspi_status == SEC_E_WRONG_PRINCIPAL)
|
|
|
|
failf(data, "schannel: SNI or certificate check failed: %s",
|
|
|
|
Curl_sspi_strerror(conn, sspi_status));
|
|
|
|
else
|
|
|
|
failf(data, "schannel: next InitializeSecurityContext failed: %s",
|
|
|
|
Curl_sspi_strerror(conn, sspi_status));
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-06-20 00:51:03 +02:00
|
|
|
/* check if there was additional remaining encrypted data */
|
|
|
|
if(inbuf[1].BufferType == SECBUFFER_EXTRA && inbuf[1].cbBuffer > 0) {
|
|
|
|
infof(data, "schannel: encrypted data length: %lu\n", inbuf[1].cbBuffer);
|
|
|
|
/*
|
|
|
|
There are two cases where we could be getting extra data here:
|
|
|
|
1) If we're renegotiating a connection and the handshake is already
|
|
|
|
complete (from the server perspective), it can encrypted app data
|
|
|
|
(not handshake data) in an extra buffer at this point.
|
|
|
|
2) (sspi_status == SEC_I_CONTINUE_NEEDED) We are negotiating a
|
|
|
|
connection and this extra data is part of the handshake.
|
|
|
|
We should process the data immediately; waiting for the socket to
|
|
|
|
be ready may fail since the server is done sending handshake data.
|
|
|
|
*/
|
|
|
|
/* check if the remaining data is less than the total amount
|
|
|
|
and therefore begins after the already processed data */
|
|
|
|
if(connssl->encdata_offset > inbuf[1].cbBuffer) {
|
|
|
|
memmove(connssl->encdata_buffer,
|
|
|
|
(connssl->encdata_buffer + connssl->encdata_offset) -
|
|
|
|
inbuf[1].cbBuffer, inbuf[1].cbBuffer);
|
|
|
|
connssl->encdata_offset = inbuf[1].cbBuffer;
|
|
|
|
if(sspi_status == SEC_I_CONTINUE_NEEDED) {
|
|
|
|
doread = FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-04-09 15:40:06 +02:00
|
|
|
}
|
2012-06-20 00:51:03 +02:00
|
|
|
else {
|
|
|
|
connssl->encdata_offset = 0;
|
|
|
|
}
|
|
|
|
break;
|
2012-04-09 15:40:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check if the handshake needs to be continued */
|
|
|
|
if(sspi_status == SEC_I_CONTINUE_NEEDED) {
|
|
|
|
connssl->connecting_state = ssl_connect_2_reading;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if the handshake is complete */
|
|
|
|
if(sspi_status == SEC_E_OK) {
|
|
|
|
connssl->connecting_state = ssl_connect_3;
|
2012-06-19 20:28:21 +02:00
|
|
|
infof(data, "schannel: SSL/TLS handshake complete\n");
|
2012-04-09 15:40:06 +02:00
|
|
|
}
|
|
|
|
|
2012-06-19 04:15:02 +02:00
|
|
|
#ifdef _WIN32_WCE
|
|
|
|
/* Windows CE doesn't do any server certificate validation.
|
|
|
|
We have to do it manually. */
|
|
|
|
if(data->set.ssl.verifypeer)
|
|
|
|
return verify_certificate(conn, sockindex);
|
|
|
|
#endif
|
|
|
|
|
2012-04-09 15:40:06 +02:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode
|
2012-04-15 04:12:26 +02:00
|
|
|
schannel_connect_step3(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
2012-04-09 23:24:55 +02:00
|
|
|
CURLcode retcode = CURLE_OK;
|
2012-04-09 22:33:58 +02:00
|
|
|
struct SessionHandle *data = conn->data;
|
2012-04-09 15:40:06 +02:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2012-04-15 04:12:26 +02:00
|
|
|
struct curl_schannel_cred *old_cred = NULL;
|
2012-04-09 23:24:55 +02:00
|
|
|
int incache;
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
|
|
|
|
|
2012-06-19 20:28:21 +02:00
|
|
|
infof(data, "schannel: SSL/TLS connection with %s port %hu (step 3/3)\n",
|
2012-04-10 21:21:31 +02:00
|
|
|
conn->host.name, conn->remote_port);
|
|
|
|
|
2012-04-09 23:24:55 +02:00
|
|
|
/* check if the required context attributes are met */
|
2012-04-09 22:43:48 +02:00
|
|
|
if(connssl->ret_flags != connssl->req_flags) {
|
2012-04-09 22:33:58 +02:00
|
|
|
if(!(connssl->ret_flags & ISC_RET_SEQUENCE_DETECT))
|
2012-06-14 13:32:05 +02:00
|
|
|
failf(data, "schannel: failed to setup sequence detection");
|
2012-04-09 22:33:58 +02:00
|
|
|
if(!(connssl->ret_flags & ISC_RET_REPLAY_DETECT))
|
2012-06-14 13:32:05 +02:00
|
|
|
failf(data, "schannel: failed to setup replay detection");
|
2012-04-09 22:33:58 +02:00
|
|
|
if(!(connssl->ret_flags & ISC_RET_CONFIDENTIALITY))
|
2012-06-14 13:32:05 +02:00
|
|
|
failf(data, "schannel: failed to setup confidentiality");
|
2012-04-09 22:33:58 +02:00
|
|
|
if(!(connssl->ret_flags & ISC_RET_EXTENDED_ERROR))
|
2012-06-14 13:32:05 +02:00
|
|
|
failf(data, "schannel: failed to setup extended errors");
|
2012-04-09 22:33:58 +02:00
|
|
|
if(!(connssl->ret_flags & ISC_RET_ALLOCATED_MEMORY))
|
2012-06-14 13:32:05 +02:00
|
|
|
failf(data, "schannel: failed to setup memory allocation");
|
2012-04-09 22:33:58 +02:00
|
|
|
if(!(connssl->ret_flags & ISC_RET_STREAM))
|
2012-06-14 13:32:05 +02:00
|
|
|
failf(data, "schannel: failed to setup stream orientation");
|
2012-04-09 22:33:58 +02:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-09-09 12:36:54 +02:00
|
|
|
/* increment the reference counter of the credential/session handle */
|
|
|
|
if(connssl->cred && connssl->ctxt) {
|
|
|
|
connssl->cred->refcount++;
|
|
|
|
infof(data, "schannel: incremented credential handle refcount = %d\n",
|
|
|
|
connssl->cred->refcount);
|
|
|
|
}
|
|
|
|
|
2012-04-09 23:24:55 +02:00
|
|
|
/* save the current session data for possible re-use */
|
2012-04-13 13:05:26 +02:00
|
|
|
incache = !(Curl_ssl_getsessionid(conn, (void**)&old_cred, NULL));
|
2012-04-09 23:24:55 +02:00
|
|
|
if(incache) {
|
|
|
|
if(old_cred != connssl->cred) {
|
|
|
|
infof(data, "schannel: old credential handle is stale, removing\n");
|
2012-04-13 13:05:26 +02:00
|
|
|
Curl_ssl_delsessionid(conn, (void*)old_cred);
|
2012-04-09 23:24:55 +02:00
|
|
|
incache = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!incache) {
|
2012-04-13 13:05:26 +02:00
|
|
|
retcode = Curl_ssl_addsessionid(conn, (void*)connssl->cred,
|
2012-04-15 04:12:26 +02:00
|
|
|
sizeof(struct curl_schannel_cred));
|
2012-04-09 23:24:55 +02:00
|
|
|
if(retcode) {
|
2012-06-14 13:32:05 +02:00
|
|
|
failf(data, "schannel: failed to store credential handle");
|
2012-04-09 23:24:55 +02:00
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
else {
|
2012-09-09 12:36:54 +02:00
|
|
|
infof(data, "schannel: stored credential handle in session cache\n");
|
2012-04-09 23:24:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-09 15:40:06 +02:00
|
|
|
connssl->connecting_state = ssl_connect_done;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode
|
|
|
|
schannel_connect_common(struct connectdata *conn, int sockindex,
|
2012-04-15 04:12:26 +02:00
|
|
|
bool nonblocking, bool *done)
|
|
|
|
{
|
2012-04-09 15:40:06 +02:00
|
|
|
CURLcode retcode;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
curl_socket_t sockfd = conn->sock[sockindex];
|
|
|
|
long timeout_ms;
|
|
|
|
int what;
|
|
|
|
|
|
|
|
/* check if the connection has already been established */
|
|
|
|
if(ssl_connection_complete == connssl->state) {
|
|
|
|
*done = TRUE;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ssl_connect_1 == connssl->connecting_state) {
|
|
|
|
/* check out how much more time we're allowed */
|
|
|
|
timeout_ms = Curl_timeleft(data, NULL, TRUE);
|
|
|
|
|
|
|
|
if(timeout_ms < 0) {
|
|
|
|
/* no need to continue if time already is up */
|
2012-06-19 20:28:21 +02:00
|
|
|
failf(data, "SSL/TLS connection timeout");
|
2012-04-09 15:40:06 +02:00
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
retcode = schannel_connect_step1(conn, sockindex);
|
|
|
|
if(retcode)
|
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(ssl_connect_2 == connssl->connecting_state ||
|
|
|
|
ssl_connect_2_reading == connssl->connecting_state ||
|
|
|
|
ssl_connect_2_writing == connssl->connecting_state) {
|
|
|
|
|
|
|
|
/* check out how much more time we're allowed */
|
|
|
|
timeout_ms = Curl_timeleft(data, NULL, TRUE);
|
|
|
|
|
|
|
|
if(timeout_ms < 0) {
|
|
|
|
/* no need to continue if time already is up */
|
2012-06-19 20:28:21 +02:00
|
|
|
failf(data, "SSL/TLS connection timeout");
|
2012-04-09 15:40:06 +02:00
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if ssl is expecting something, check if it's available. */
|
|
|
|
if(connssl->connecting_state == ssl_connect_2_reading
|
|
|
|
|| connssl->connecting_state == ssl_connect_2_writing) {
|
|
|
|
|
|
|
|
curl_socket_t writefd = ssl_connect_2_writing ==
|
|
|
|
connssl->connecting_state ? sockfd : CURL_SOCKET_BAD;
|
|
|
|
curl_socket_t readfd = ssl_connect_2_reading ==
|
|
|
|
connssl->connecting_state ? sockfd : CURL_SOCKET_BAD;
|
|
|
|
|
|
|
|
what = Curl_socket_ready(readfd, writefd, nonblocking ? 0 : timeout_ms);
|
|
|
|
if(what < 0) {
|
|
|
|
/* fatal error */
|
2012-06-19 20:28:21 +02:00
|
|
|
failf(data, "select/poll on SSL/TLS socket, errno: %d", SOCKERRNO);
|
2012-04-09 15:40:06 +02:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
else if(0 == what) {
|
|
|
|
if(nonblocking) {
|
|
|
|
*done = FALSE;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* timeout */
|
2012-06-19 20:28:21 +02:00
|
|
|
failf(data, "SSL/TLS connection timeout");
|
2012-04-09 15:40:06 +02:00
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* socket is readable or writable */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run transaction, and return to the caller if it failed or if
|
|
|
|
* this connection is part of a multi handle and this loop would
|
|
|
|
* execute again. This permits the owner of a multi handle to
|
|
|
|
* abort a connection attempt before step2 has completed while
|
|
|
|
* ensuring that a client using select() or epoll() will always
|
|
|
|
* have a valid fdset to wait on.
|
|
|
|
*/
|
|
|
|
retcode = schannel_connect_step2(conn, sockindex);
|
|
|
|
if(retcode || (nonblocking &&
|
|
|
|
(ssl_connect_2 == connssl->connecting_state ||
|
|
|
|
ssl_connect_2_reading == connssl->connecting_state ||
|
|
|
|
ssl_connect_2_writing == connssl->connecting_state)))
|
|
|
|
return retcode;
|
|
|
|
|
|
|
|
} /* repeat step2 until all transactions are done. */
|
|
|
|
|
|
|
|
if(ssl_connect_3 == connssl->connecting_state) {
|
|
|
|
retcode = schannel_connect_step3(conn, sockindex);
|
|
|
|
if(retcode)
|
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ssl_connect_done == connssl->connecting_state) {
|
|
|
|
connssl->state = ssl_connection_complete;
|
|
|
|
conn->recv[sockindex] = schannel_recv;
|
|
|
|
conn->send[sockindex] = schannel_send;
|
|
|
|
*done = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*done = FALSE;
|
|
|
|
|
|
|
|
/* reset our connection state machine */
|
|
|
|
connssl->connecting_state = ssl_connect_1;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
schannel_send(struct connectdata *conn, int sockindex,
|
2012-04-15 04:12:26 +02:00
|
|
|
const void *buf, size_t len, CURLcode *err)
|
|
|
|
{
|
2012-06-14 11:53:46 +02:00
|
|
|
ssize_t written = -1;
|
2012-04-09 15:40:06 +02:00
|
|
|
size_t data_len = 0;
|
|
|
|
unsigned char *data = NULL;
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
SecBuffer outbuf[4];
|
|
|
|
SecBufferDesc outbuf_desc;
|
|
|
|
SECURITY_STATUS sspi_status = SEC_E_OK;
|
2012-06-19 20:28:22 +02:00
|
|
|
CURLcode code;
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
/* check if the maximum stream sizes were queried */
|
|
|
|
if(connssl->stream_sizes.cbMaximumMessage == 0) {
|
2012-04-13 13:10:09 +02:00
|
|
|
sspi_status = s_pSecFn->QueryContextAttributes(
|
2012-04-09 23:24:55 +02:00
|
|
|
&connssl->ctxt->ctxt_handle,
|
2012-06-11 19:08:46 +02:00
|
|
|
SECPKG_ATTR_STREAM_SIZES,
|
|
|
|
&connssl->stream_sizes);
|
2012-04-09 15:40:06 +02:00
|
|
|
if(sspi_status != SEC_E_OK) {
|
|
|
|
*err = CURLE_SEND_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if the buffer is longer than the maximum message length */
|
|
|
|
if(len > connssl->stream_sizes.cbMaximumMessage) {
|
|
|
|
*err = CURLE_SEND_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate the complete message length and allocate a buffer for it */
|
|
|
|
data_len = connssl->stream_sizes.cbHeader + len +
|
|
|
|
connssl->stream_sizes.cbTrailer;
|
|
|
|
data = (unsigned char*) malloc(data_len);
|
|
|
|
if(data == NULL) {
|
|
|
|
*err = CURLE_OUT_OF_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup output buffers (header, data, trailer, empty) */
|
2012-06-19 04:15:01 +02:00
|
|
|
InitSecBuffer(&outbuf[0], SECBUFFER_STREAM_HEADER,
|
|
|
|
data, connssl->stream_sizes.cbHeader);
|
|
|
|
InitSecBuffer(&outbuf[1], SECBUFFER_DATA,
|
2012-10-04 19:55:17 +02:00
|
|
|
data + connssl->stream_sizes.cbHeader, curlx_uztoul(len));
|
2012-06-19 04:15:01 +02:00
|
|
|
InitSecBuffer(&outbuf[2], SECBUFFER_STREAM_TRAILER,
|
|
|
|
data + connssl->stream_sizes.cbHeader + len,
|
|
|
|
connssl->stream_sizes.cbTrailer);
|
|
|
|
InitSecBuffer(&outbuf[3], SECBUFFER_EMPTY, NULL, 0);
|
|
|
|
InitSecBufferDesc(&outbuf_desc, outbuf, 4);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
/* copy data into output buffer */
|
|
|
|
memcpy(outbuf[1].pvBuffer, buf, len);
|
|
|
|
|
|
|
|
/* http://msdn.microsoft.com/en-us/library/windows/desktop/aa375390.aspx */
|
2012-04-09 23:24:55 +02:00
|
|
|
sspi_status = s_pSecFn->EncryptMessage(&connssl->ctxt->ctxt_handle, 0,
|
2012-04-09 15:40:06 +02:00
|
|
|
&outbuf_desc, 0);
|
|
|
|
|
|
|
|
/* check if the message was encrypted */
|
|
|
|
if(sspi_status == SEC_E_OK) {
|
2012-06-20 00:14:17 +02:00
|
|
|
written = 0;
|
|
|
|
|
2012-04-09 15:40:06 +02:00
|
|
|
/* send the encrypted message including header, data and trailer */
|
|
|
|
len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer;
|
2012-06-20 00:14:17 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
It's important to send the full message which includes the header,
|
|
|
|
encrypted payload, and trailer. Until the client receives all the
|
|
|
|
data a coherent message has not been delivered and the client
|
|
|
|
can't read any of it.
|
|
|
|
|
|
|
|
If we wanted to buffer the unwritten encrypted bytes, we would
|
|
|
|
tell the client that all data it has requested to be sent has been
|
|
|
|
sent. The unwritten encrypted bytes would be the first bytes to
|
|
|
|
send on the next invocation.
|
|
|
|
Here's the catch with this - if we tell the client that all the
|
|
|
|
bytes have been sent, will the client call this method again to
|
|
|
|
send the buffered data? Looking at who calls this function, it
|
|
|
|
seems the answer is NO.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* send entire message or fail */
|
|
|
|
while(len > (size_t)written) {
|
|
|
|
ssize_t this_write;
|
|
|
|
long timeleft;
|
|
|
|
int what;
|
|
|
|
|
|
|
|
this_write = 0;
|
|
|
|
|
|
|
|
timeleft = Curl_timeleft(conn->data, NULL, TRUE);
|
|
|
|
if(timeleft < 0) {
|
|
|
|
/* we already got the timeout */
|
|
|
|
failf(conn->data, "schannel: timed out sending data "
|
|
|
|
"(bytes sent: %zd)", written);
|
|
|
|
*err = CURLE_OPERATION_TIMEDOUT;
|
|
|
|
written = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
what = Curl_socket_ready(CURL_SOCKET_BAD, conn->sock[sockindex],
|
|
|
|
timeleft);
|
|
|
|
if(what < 0) {
|
|
|
|
/* fatal error */
|
|
|
|
failf(conn->data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
|
|
|
|
*err = CURLE_SEND_ERROR;
|
|
|
|
written = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(0 == what) {
|
|
|
|
failf(conn->data, "schannel: timed out sending data "
|
|
|
|
"(bytes sent: %zd)", written);
|
|
|
|
*err = CURLE_OPERATION_TIMEDOUT;
|
|
|
|
written = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* socket is writable */
|
|
|
|
|
|
|
|
code = Curl_write_plain(conn, conn->sock[sockindex], data + written,
|
|
|
|
len - written, &this_write);
|
|
|
|
if(code == CURLE_AGAIN)
|
|
|
|
continue;
|
|
|
|
else if(code != CURLE_OK) {
|
|
|
|
*err = code;
|
|
|
|
written = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
written += this_write;
|
|
|
|
}
|
2012-04-09 15:40:06 +02:00
|
|
|
}
|
|
|
|
else if(sspi_status == SEC_E_INSUFFICIENT_MEMORY) {
|
|
|
|
*err = CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
*err = CURLE_SEND_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-06-25 07:16:26 +02:00
|
|
|
Curl_safefree(data);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-06-20 00:14:17 +02:00
|
|
|
if(len == (size_t)written)
|
|
|
|
/* Encrypted message including header, data and trailer entirely sent.
|
|
|
|
The return value is the number of unencrypted bytes that were sent. */
|
|
|
|
written = outbuf[1].cbBuffer;
|
|
|
|
|
2012-06-14 11:53:46 +02:00
|
|
|
return written;
|
2012-04-09 15:40:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
schannel_recv(struct connectdata *conn, int sockindex,
|
2012-04-15 04:12:26 +02:00
|
|
|
char *buf, size_t len, CURLcode *err)
|
|
|
|
{
|
2012-04-09 15:40:06 +02:00
|
|
|
size_t size = 0;
|
2012-06-14 11:53:46 +02:00
|
|
|
ssize_t nread = 0, ret = -1;
|
2012-04-09 15:40:06 +02:00
|
|
|
CURLcode retcode;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
bool done = FALSE;
|
|
|
|
SecBuffer inbuf[4];
|
|
|
|
SecBufferDesc inbuf_desc;
|
|
|
|
SECURITY_STATUS sspi_status = SEC_E_OK;
|
|
|
|
|
2012-06-14 16:55:26 +02:00
|
|
|
infof(data, "schannel: client wants to read %zu bytes\n", len);
|
2012-04-09 15:40:06 +02:00
|
|
|
*err = CURLE_OK;
|
|
|
|
|
|
|
|
/* buffer to store previously received and decrypted data */
|
|
|
|
if(connssl->decdata_buffer == NULL) {
|
|
|
|
connssl->decdata_offset = 0;
|
2012-04-15 04:12:26 +02:00
|
|
|
connssl->decdata_length = CURL_SCHANNEL_BUFFER_INIT_SIZE;
|
2012-04-09 15:40:06 +02:00
|
|
|
connssl->decdata_buffer = malloc(connssl->decdata_length);
|
|
|
|
if(connssl->decdata_buffer == NULL) {
|
|
|
|
failf(data, "schannel: unable to allocate memory");
|
2012-06-20 07:49:34 +02:00
|
|
|
*err = CURLE_OUT_OF_MEMORY;
|
|
|
|
return -1;
|
2012-04-09 15:40:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-10 21:49:35 +02:00
|
|
|
/* increase buffer in order to fit the requested amount of data */
|
2012-04-15 04:12:26 +02:00
|
|
|
while(connssl->encdata_length - connssl->encdata_offset <
|
2012-10-06 13:59:28 +02:00
|
|
|
CURL_SCHANNEL_BUFFER_FREE_SIZE || connssl->encdata_length < len) {
|
2012-04-09 15:40:06 +02:00
|
|
|
/* increase internal encrypted data buffer */
|
2012-06-20 07:49:34 +02:00
|
|
|
connssl->encdata_length *= CURL_SCHANNEL_BUFFER_STEP_FACTOR;
|
2012-04-09 15:40:06 +02:00
|
|
|
connssl->encdata_buffer = realloc(connssl->encdata_buffer,
|
|
|
|
connssl->encdata_length);
|
2012-06-20 07:49:34 +02:00
|
|
|
|
2012-04-09 15:40:06 +02:00
|
|
|
if(connssl->encdata_buffer == NULL) {
|
|
|
|
failf(data, "schannel: unable to re-allocate memory");
|
|
|
|
*err = CURLE_OUT_OF_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read encrypted data from socket */
|
2012-06-14 16:55:26 +02:00
|
|
|
infof(data, "schannel: encrypted data buffer: offset %zu length %zu\n",
|
2012-04-09 15:40:06 +02:00
|
|
|
connssl->encdata_offset, connssl->encdata_length);
|
|
|
|
size = connssl->encdata_length - connssl->encdata_offset;
|
|
|
|
if(size > 0) {
|
2012-06-19 20:28:22 +02:00
|
|
|
*err = Curl_read_plain(conn->sock[sockindex],
|
|
|
|
(char *) (connssl->encdata_buffer + connssl->encdata_offset),
|
|
|
|
size, &nread);
|
2012-04-09 15:40:06 +02:00
|
|
|
/* check for received data */
|
2012-06-19 20:28:22 +02:00
|
|
|
if(*err != CURLE_OK)
|
|
|
|
ret = -1;
|
|
|
|
else {
|
|
|
|
if(nread > 0)
|
|
|
|
/* increase encrypted data buffer offset */
|
|
|
|
connssl->encdata_offset += nread;
|
|
|
|
ret = nread;
|
2012-04-10 21:21:31 +02:00
|
|
|
}
|
2012-06-19 20:28:22 +02:00
|
|
|
infof(data, "schannel: encrypted data got %zd\n", ret);
|
2012-04-09 15:40:06 +02:00
|
|
|
}
|
|
|
|
|
2012-06-14 16:55:26 +02:00
|
|
|
infof(data, "schannel: encrypted data buffer: offset %zu length %zu\n",
|
|
|
|
connssl->encdata_offset, connssl->encdata_length);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
/* check if we still have some data in our buffers */
|
2012-10-06 15:47:14 +02:00
|
|
|
while(connssl->encdata_offset > 0 && sspi_status == SEC_E_OK &&
|
|
|
|
connssl->decdata_offset < len) {
|
2012-04-09 15:40:06 +02:00
|
|
|
/* prepare data buffer for DecryptMessage call */
|
2012-06-19 04:15:01 +02:00
|
|
|
InitSecBuffer(&inbuf[0], SECBUFFER_DATA, connssl->encdata_buffer,
|
|
|
|
curlx_uztoul(connssl->encdata_offset));
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
/* we need 3 more empty input buffers for possible output */
|
2012-06-19 04:15:01 +02:00
|
|
|
InitSecBuffer(&inbuf[1], SECBUFFER_EMPTY, NULL, 0);
|
|
|
|
InitSecBuffer(&inbuf[2], SECBUFFER_EMPTY, NULL, 0);
|
|
|
|
InitSecBuffer(&inbuf[3], SECBUFFER_EMPTY, NULL, 0);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-06-19 04:15:01 +02:00
|
|
|
InitSecBufferDesc(&inbuf_desc, inbuf, 4);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
/* http://msdn.microsoft.com/en-us/library/windows/desktop/aa375348.aspx */
|
2012-04-09 23:24:55 +02:00
|
|
|
sspi_status = s_pSecFn->DecryptMessage(&connssl->ctxt->ctxt_handle,
|
2012-04-09 15:40:06 +02:00
|
|
|
&inbuf_desc, 0, NULL);
|
2012-04-10 21:49:35 +02:00
|
|
|
|
|
|
|
/* check if we need more data */
|
|
|
|
if(sspi_status == SEC_E_INCOMPLETE_MESSAGE) {
|
|
|
|
infof(data, "schannel: failed to decrypt data, need more data\n");
|
|
|
|
*err = CURLE_AGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-06-11 19:08:46 +02:00
|
|
|
/* check if everything went fine (server may want to renegotiate
|
|
|
|
context) */
|
2012-04-10 21:49:35 +02:00
|
|
|
if(sspi_status == SEC_E_OK || sspi_status == SEC_I_RENEGOTIATE ||
|
|
|
|
sspi_status == SEC_I_CONTEXT_EXPIRED) {
|
2012-04-09 15:40:06 +02:00
|
|
|
/* check for successfully decrypted data */
|
|
|
|
if(inbuf[1].BufferType == SECBUFFER_DATA) {
|
2012-06-14 16:55:26 +02:00
|
|
|
infof(data, "schannel: decrypted data length: %lu\n",
|
2012-06-11 19:08:46 +02:00
|
|
|
inbuf[1].cbBuffer);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
2012-04-10 21:49:35 +02:00
|
|
|
/* increase buffer in order to fit the received amount of data */
|
2012-10-06 13:59:28 +02:00
|
|
|
size = inbuf[1].cbBuffer > CURL_SCHANNEL_BUFFER_FREE_SIZE ?
|
|
|
|
inbuf[1].cbBuffer : CURL_SCHANNEL_BUFFER_FREE_SIZE;
|
2012-04-10 21:49:35 +02:00
|
|
|
while(connssl->decdata_length - connssl->decdata_offset < size ||
|
|
|
|
connssl->decdata_length < len) {
|
|
|
|
/* increase internal decrypted data buffer */
|
2012-06-20 07:49:34 +02:00
|
|
|
connssl->decdata_length *= CURL_SCHANNEL_BUFFER_STEP_FACTOR;
|
2012-04-10 21:49:35 +02:00
|
|
|
connssl->decdata_buffer = realloc(connssl->decdata_buffer,
|
|
|
|
connssl->decdata_length);
|
2012-06-20 07:49:34 +02:00
|
|
|
|
2012-04-10 21:49:35 +02:00
|
|
|
if(connssl->decdata_buffer == NULL) {
|
|
|
|
failf(data, "schannel: unable to re-allocate memory");
|
|
|
|
*err = CURLE_OUT_OF_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-09 15:40:06 +02:00
|
|
|
/* copy decrypted data to internal buffer */
|
2012-04-10 21:49:35 +02:00
|
|
|
size = inbuf[1].cbBuffer;
|
2012-04-09 15:40:06 +02:00
|
|
|
if(size > 0) {
|
|
|
|
memcpy(connssl->decdata_buffer + connssl->decdata_offset,
|
|
|
|
inbuf[1].pvBuffer, size);
|
|
|
|
connssl->decdata_offset += size;
|
|
|
|
}
|
|
|
|
|
2012-06-14 16:55:26 +02:00
|
|
|
infof(data, "schannel: decrypted data added: %zu\n", size);
|
|
|
|
infof(data, "schannel: decrypted data cached: offset %zu length %zu\n",
|
2012-04-09 15:40:06 +02:00
|
|
|
connssl->decdata_offset, connssl->decdata_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for remaining encrypted data */
|
2012-04-13 13:02:59 +02:00
|
|
|
if(inbuf[3].BufferType == SECBUFFER_EXTRA && inbuf[3].cbBuffer > 0) {
|
2012-06-14 16:55:26 +02:00
|
|
|
infof(data, "schannel: encrypted data length: %lu\n",
|
2012-06-11 19:08:46 +02:00
|
|
|
inbuf[3].cbBuffer);
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
/* check if the remaining data is less than the total amount
|
|
|
|
* and therefore begins after the already processed data
|
|
|
|
*/
|
|
|
|
if(connssl->encdata_offset > inbuf[3].cbBuffer) {
|
2012-06-11 19:08:46 +02:00
|
|
|
/* move remaining encrypted data forward to the beginning of
|
|
|
|
buffer */
|
2012-04-09 15:40:06 +02:00
|
|
|
memmove(connssl->encdata_buffer,
|
|
|
|
(connssl->encdata_buffer + connssl->encdata_offset) -
|
|
|
|
inbuf[3].cbBuffer, inbuf[3].cbBuffer);
|
|
|
|
connssl->encdata_offset = inbuf[3].cbBuffer;
|
|
|
|
}
|
|
|
|
|
2012-06-14 16:55:26 +02:00
|
|
|
infof(data, "schannel: encrypted data cached: offset %zu length %zu\n",
|
2012-04-09 15:40:06 +02:00
|
|
|
connssl->encdata_offset, connssl->encdata_length);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
/* reset encrypted buffer offset, because there is no data remaining */
|
|
|
|
connssl->encdata_offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if server wants to renegotiate the connection context */
|
|
|
|
if(sspi_status == SEC_I_RENEGOTIATE) {
|
2012-06-19 20:28:21 +02:00
|
|
|
infof(data, "schannel: remote party requests SSL/TLS renegotiation\n");
|
2012-04-09 15:40:06 +02:00
|
|
|
|
|
|
|
/* begin renegotiation */
|
2012-06-19 20:28:21 +02:00
|
|
|
infof(data, "schannel: renegotiating SSL/TLS connection\n");
|
2012-04-09 15:40:06 +02:00
|
|
|
connssl->state = ssl_connection_negotiating;
|
2012-04-10 21:21:31 +02:00
|
|
|
connssl->connecting_state = ssl_connect_2_writing;
|
2012-04-09 15:40:06 +02:00
|
|
|
retcode = schannel_connect_common(conn, sockindex, FALSE, &done);
|
|
|
|
if(retcode)
|
|
|
|
*err = retcode;
|
2012-06-19 20:28:21 +02:00
|
|
|
else {
|
|
|
|
infof(data, "schannel: SSL/TLS connection renegotiated\n");
|
|
|
|
/* now retry receiving data */
|
2012-04-10 21:21:31 +02:00
|
|
|
return schannel_recv(conn, sockindex, buf, len, err);
|
2012-06-19 20:28:21 +02:00
|
|
|
}
|
2012-04-09 15:40:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-06 15:47:14 +02:00
|
|
|
infof(data, "schannel: decrypted data buffer: offset %zu length %zu\n",
|
|
|
|
connssl->decdata_offset, connssl->decdata_length);
|
|
|
|
|
2012-04-09 15:40:06 +02:00
|
|
|
/* copy requested decrypted data to supplied buffer */
|
|
|
|
size = len < connssl->decdata_offset ? len : connssl->decdata_offset;
|
|
|
|
if(size > 0) {
|
|
|
|
memcpy(buf, connssl->decdata_buffer, size);
|
|
|
|
ret = size;
|
|
|
|
|
|
|
|
/* move remaining decrypted data forward to the beginning of buffer */
|
|
|
|
memmove(connssl->decdata_buffer, connssl->decdata_buffer + size,
|
|
|
|
connssl->decdata_offset - size);
|
|
|
|
connssl->decdata_offset -= size;
|
2012-10-06 15:47:14 +02:00
|
|
|
|
|
|
|
infof(data, "schannel: decrypted data returned %zd\n", size);
|
|
|
|
infof(data, "schannel: decrypted data buffer: offset %zu length %zu\n",
|
|
|
|
connssl->decdata_offset, connssl->decdata_length);
|
2012-04-09 15:40:06 +02:00
|
|
|
}
|
|
|
|
|
2012-04-10 21:21:31 +02:00
|
|
|
/* check if the server closed the connection */
|
2012-04-10 21:49:35 +02:00
|
|
|
if(ret <= 0 && ( /* special check for Windows 2000 Professional */
|
|
|
|
sspi_status == SEC_I_CONTEXT_EXPIRED || (sspi_status == SEC_E_OK &&
|
|
|
|
connssl->encdata_offset > 0 && connssl->encdata_buffer[0] == 0x15))) {
|
2012-04-10 21:21:31 +02:00
|
|
|
infof(data, "schannel: server closed the connection\n");
|
|
|
|
*err = CURLE_OK;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-09 15:40:06 +02:00
|
|
|
/* check if something went wrong and we need to return an error */
|
2012-04-10 21:49:35 +02:00
|
|
|
if(ret < 0 && sspi_status != SEC_E_OK) {
|
2012-06-12 01:06:48 +02:00
|
|
|
infof(data, "schannel: failed to read data from server: %s\n",
|
|
|
|
Curl_sspi_strerror(conn, sspi_status));
|
2012-04-10 21:49:35 +02:00
|
|
|
*err = CURLE_RECV_ERROR;
|
2012-04-09 15:40:06 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode
|
|
|
|
Curl_schannel_connect_nonblocking(struct connectdata *conn, int sockindex,
|
2012-04-15 04:12:26 +02:00
|
|
|
bool *done)
|
|
|
|
{
|
2012-04-09 15:40:06 +02:00
|
|
|
return schannel_connect_common(conn, sockindex, TRUE, done);
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode
|
2012-04-15 04:12:26 +02:00
|
|
|
Curl_schannel_connect(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
2012-04-09 15:40:06 +02:00
|
|
|
CURLcode retcode;
|
|
|
|
bool done = FALSE;
|
|
|
|
|
|
|
|
retcode = schannel_connect_common(conn, sockindex, FALSE, &done);
|
|
|
|
if(retcode)
|
|
|
|
return retcode;
|
|
|
|
|
|
|
|
DEBUGASSERT(done);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-15 04:12:26 +02:00
|
|
|
bool Curl_schannel_data_pending(const struct connectdata *conn, int sockindex)
|
|
|
|
{
|
2012-04-09 15:40:06 +02:00
|
|
|
const struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
|
2012-06-19 20:28:21 +02:00
|
|
|
if(connssl->use) /* SSL/TLS is in use */
|
2012-04-09 15:40:06 +02:00
|
|
|
return (connssl->encdata_offset > 0 ||
|
|
|
|
connssl->decdata_offset > 0 ) ? TRUE : FALSE;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-04-15 04:12:26 +02:00
|
|
|
void Curl_schannel_close(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
2012-06-19 04:15:03 +02:00
|
|
|
if(conn->ssl[sockindex].use)
|
2012-06-19 20:28:21 +02:00
|
|
|
/* if the SSL/TLS channel hasn't been shut down yet, do that now. */
|
2012-06-19 04:15:03 +02:00
|
|
|
Curl_ssl_shutdown(conn, sockindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Curl_schannel_shutdown(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
2012-06-25 07:16:26 +02:00
|
|
|
/* See http://msdn.microsoft.com/en-us/library/windows/desktop/aa380138.aspx
|
|
|
|
* Shutting Down an Schannel Connection
|
|
|
|
*/
|
2012-04-09 15:40:06 +02:00
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
|
2012-06-19 20:28:21 +02:00
|
|
|
infof(data, "schannel: shutting down SSL/TLS connection with %s port %hu\n",
|
2012-04-09 15:40:06 +02:00
|
|
|
conn->host.name, conn->remote_port);
|
|
|
|
|
2012-09-09 12:36:54 +02:00
|
|
|
if(connssl->cred && connssl->ctxt) {
|
2012-06-19 04:15:03 +02:00
|
|
|
SecBufferDesc BuffDesc;
|
|
|
|
SecBuffer Buffer;
|
|
|
|
SECURITY_STATUS sspi_status;
|
|
|
|
SecBuffer outbuf;
|
|
|
|
SecBufferDesc outbuf_desc;
|
|
|
|
CURLcode code;
|
|
|
|
TCHAR *host_name;
|
|
|
|
DWORD dwshut = SCHANNEL_SHUTDOWN;
|
|
|
|
|
|
|
|
InitSecBuffer(&Buffer, SECBUFFER_TOKEN, &dwshut, sizeof(dwshut));
|
|
|
|
InitSecBufferDesc(&BuffDesc, &Buffer, 1);
|
|
|
|
|
|
|
|
sspi_status = s_pSecFn->ApplyControlToken(&connssl->ctxt->ctxt_handle,
|
|
|
|
&BuffDesc);
|
|
|
|
|
|
|
|
if(sspi_status != SEC_E_OK)
|
|
|
|
failf(data, "schannel: ApplyControlToken failure: %s",
|
|
|
|
Curl_sspi_strerror(conn, sspi_status));
|
|
|
|
|
2012-07-05 22:16:15 +02:00
|
|
|
host_name = Curl_convert_UTF8_to_tchar(conn->host.name);
|
2012-06-19 04:15:03 +02:00
|
|
|
if(!host_name)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* setup output buffer */
|
|
|
|
InitSecBuffer(&outbuf, SECBUFFER_EMPTY, NULL, 0);
|
|
|
|
InitSecBufferDesc(&outbuf_desc, &outbuf, 1);
|
|
|
|
|
|
|
|
sspi_status = s_pSecFn->InitializeSecurityContext(
|
|
|
|
&connssl->cred->cred_handle,
|
|
|
|
&connssl->ctxt->ctxt_handle,
|
|
|
|
host_name,
|
|
|
|
connssl->req_flags,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
&connssl->ctxt->ctxt_handle,
|
|
|
|
&outbuf_desc,
|
|
|
|
&connssl->ret_flags,
|
|
|
|
&connssl->ctxt->time_stamp);
|
|
|
|
|
2012-07-05 22:16:15 +02:00
|
|
|
Curl_unicodefree(host_name);
|
2012-06-19 04:15:03 +02:00
|
|
|
|
|
|
|
if((sspi_status == SEC_E_OK) || (sspi_status == SEC_I_CONTEXT_EXPIRED)) {
|
|
|
|
/* send close message which is in output buffer */
|
|
|
|
ssize_t written;
|
|
|
|
code = Curl_write_plain(conn, conn->sock[sockindex], outbuf.pvBuffer,
|
|
|
|
outbuf.cbBuffer, &written);
|
|
|
|
|
|
|
|
s_pSecFn->FreeContextBuffer(outbuf.pvBuffer);
|
|
|
|
if((code != CURLE_OK) || (outbuf.cbBuffer != (size_t)written)) {
|
|
|
|
infof(data, "schannel: failed to send close msg: %s"
|
|
|
|
" (bytes written: %zd)\n", curl_easy_strerror(code), written);
|
|
|
|
}
|
|
|
|
}
|
2012-06-25 07:16:26 +02:00
|
|
|
|
2012-06-19 04:15:03 +02:00
|
|
|
/* free SSPI Schannel API security context handle */
|
|
|
|
if(connssl->ctxt) {
|
|
|
|
s_pSecFn->DeleteSecurityContext(&connssl->ctxt->ctxt_handle);
|
2012-06-25 07:16:26 +02:00
|
|
|
Curl_safefree(connssl->ctxt);
|
2012-06-19 04:15:03 +02:00
|
|
|
}
|
2012-09-09 12:36:54 +02:00
|
|
|
|
|
|
|
/* decrement the reference counter of the credential/session handle */
|
|
|
|
if(connssl->cred && connssl->cred->refcount > 0) {
|
|
|
|
connssl->cred->refcount--;
|
|
|
|
infof(data, "schannel: decremented credential handle refcount = %d\n",
|
|
|
|
connssl->cred->refcount);
|
|
|
|
}
|
2012-04-09 15:40:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* free internal buffer for received encrypted data */
|
|
|
|
if(connssl->encdata_buffer != NULL) {
|
2012-06-25 07:16:26 +02:00
|
|
|
Curl_safefree(connssl->encdata_buffer);
|
2012-04-09 15:40:06 +02:00
|
|
|
connssl->encdata_length = 0;
|
|
|
|
connssl->encdata_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free internal buffer for received decrypted data */
|
|
|
|
if(connssl->decdata_buffer != NULL) {
|
2012-06-25 07:16:26 +02:00
|
|
|
Curl_safefree(connssl->decdata_buffer);
|
2012-04-09 15:40:06 +02:00
|
|
|
connssl->decdata_length = 0;
|
|
|
|
connssl->decdata_offset = 0;
|
|
|
|
}
|
|
|
|
|
2012-06-19 04:15:03 +02:00
|
|
|
return CURLE_OK;
|
2012-04-09 15:40:06 +02:00
|
|
|
}
|
|
|
|
|
2012-04-15 04:12:26 +02:00
|
|
|
void Curl_schannel_session_free(void *ptr)
|
|
|
|
{
|
|
|
|
struct curl_schannel_cred *cred = ptr;
|
2012-04-09 23:24:55 +02:00
|
|
|
|
2012-09-09 12:36:54 +02:00
|
|
|
if(cred && cred->refcount == 0) {
|
2012-04-09 23:24:55 +02:00
|
|
|
s_pSecFn->FreeCredentialsHandle(&cred->cred_handle);
|
2012-06-25 07:16:26 +02:00
|
|
|
Curl_safefree(cred);
|
2012-04-09 23:24:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-14 11:34:12 +02:00
|
|
|
int Curl_schannel_init(void)
|
2012-04-15 04:12:26 +02:00
|
|
|
{
|
2012-04-09 15:40:06 +02:00
|
|
|
return (Curl_sspi_global_init() == CURLE_OK ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2012-06-14 11:34:12 +02:00
|
|
|
void Curl_schannel_cleanup(void)
|
2012-04-15 04:12:26 +02:00
|
|
|
{
|
2012-04-09 15:40:06 +02:00
|
|
|
Curl_sspi_global_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Curl_schannel_version(char *buffer, size_t size)
|
|
|
|
{
|
2012-07-09 15:23:49 +02:00
|
|
|
size = snprintf(buffer, size, "WinSSL");
|
2012-06-10 13:07:45 +02:00
|
|
|
|
2012-04-14 15:00:33 +02:00
|
|
|
return size;
|
2012-04-09 15:40:06 +02:00
|
|
|
}
|
|
|
|
|
2012-06-19 04:15:02 +02:00
|
|
|
#ifdef _WIN32_WCE
|
|
|
|
static CURLcode verify_certificate(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
CERT_CONTEXT *pCertContextServer = NULL;
|
2012-06-19 20:28:21 +02:00
|
|
|
const CERT_CHAIN_CONTEXT *pChainContext = NULL;
|
2012-06-19 04:15:02 +02:00
|
|
|
|
|
|
|
status = s_pSecFn->QueryContextAttributes(&connssl->ctxt->ctxt_handle,
|
|
|
|
SECPKG_ATTR_REMOTE_CERT_CONTEXT,
|
|
|
|
&pCertContextServer);
|
|
|
|
|
|
|
|
if((status != SEC_E_OK) || (pCertContextServer == NULL)) {
|
|
|
|
failf(data, "schannel: Failed to read remote certificate context: %s",
|
|
|
|
Curl_sspi_strerror(conn, status));
|
|
|
|
result = CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result == CURLE_OK) {
|
|
|
|
CERT_CHAIN_PARA ChainPara;
|
|
|
|
memset(&ChainPara, 0, sizeof(ChainPara));
|
|
|
|
ChainPara.cbSize = sizeof(ChainPara);
|
|
|
|
|
|
|
|
if(!CertGetCertificateChain(NULL,
|
|
|
|
pCertContextServer,
|
|
|
|
NULL,
|
|
|
|
pCertContextServer->hCertStore,
|
|
|
|
&ChainPara,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&pChainContext)) {
|
|
|
|
failf(data, "schannel: CertGetCertificateChain failed: %s",
|
|
|
|
Curl_sspi_strerror(conn, GetLastError()));
|
|
|
|
pChainContext = NULL;
|
|
|
|
result = CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result == CURLE_OK) {
|
|
|
|
CERT_SIMPLE_CHAIN *pSimpleChain = pChainContext->rgpChain[0];
|
2012-07-05 22:16:15 +02:00
|
|
|
DWORD dwTrustErrorMask = ~(DWORD)(CERT_TRUST_IS_NOT_TIME_NESTED|
|
2012-06-19 04:15:02 +02:00
|
|
|
CERT_TRUST_REVOCATION_STATUS_UNKNOWN);
|
|
|
|
dwTrustErrorMask &= pSimpleChain->TrustStatus.dwErrorStatus;
|
|
|
|
if(dwTrustErrorMask) {
|
|
|
|
if(dwTrustErrorMask & CERT_TRUST_IS_PARTIAL_CHAIN)
|
|
|
|
failf(data, "schannel: CertGetCertificateChain trust error"
|
|
|
|
" CERT_TRUST_IS_PARTIAL_CHAIN");
|
|
|
|
if(dwTrustErrorMask & CERT_TRUST_IS_UNTRUSTED_ROOT)
|
|
|
|
failf(data, "schannel: CertGetCertificateChain trust error"
|
|
|
|
" CERT_TRUST_IS_UNTRUSTED_ROOT");
|
|
|
|
if(dwTrustErrorMask & CERT_TRUST_IS_NOT_TIME_VALID)
|
|
|
|
failf(data, "schannel: CertGetCertificateChain trust error"
|
|
|
|
" CERT_TRUST_IS_NOT_TIME_VALID");
|
|
|
|
failf(data, "schannel: CertGetCertificateChain error mask: 0x%08x",
|
|
|
|
dwTrustErrorMask);
|
|
|
|
result = CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result == CURLE_OK) {
|
2012-10-27 12:31:39 +02:00
|
|
|
if(data->set.ssl.verifyhost) {
|
2012-07-05 22:16:15 +02:00
|
|
|
TCHAR cert_hostname_buff[128];
|
|
|
|
xcharp_u hostname;
|
|
|
|
xcharp_u cert_hostname;
|
2012-06-19 04:15:02 +02:00
|
|
|
DWORD len;
|
|
|
|
|
2012-07-05 22:16:15 +02:00
|
|
|
cert_hostname.const_tchar_ptr = cert_hostname_buff;
|
|
|
|
hostname.tchar_ptr = Curl_convert_UTF8_to_tchar(conn->host.name);
|
|
|
|
|
|
|
|
len = CertGetNameString(pCertContextServer,
|
|
|
|
CERT_NAME_DNS_TYPE,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
cert_hostname.tchar_ptr,
|
|
|
|
128);
|
|
|
|
if(len > 0 && *cert_hostname.tchar_ptr == '*') {
|
2012-06-19 04:15:02 +02:00
|
|
|
/* this is a wildcard cert. try matching the last len - 1 chars */
|
|
|
|
int hostname_len = strlen(conn->host.name);
|
2012-07-05 22:16:15 +02:00
|
|
|
cert_hostname.tchar_ptr++;
|
|
|
|
if(_tcsicmp(cert_hostname.const_tchar_ptr,
|
|
|
|
hostname.const_tchar_ptr + hostname_len - len + 2) != 0)
|
2012-06-19 04:15:02 +02:00
|
|
|
result = CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
}
|
2012-07-05 22:16:15 +02:00
|
|
|
else if(len == 0 || _tcsicmp(hostname.const_tchar_ptr,
|
|
|
|
cert_hostname.const_tchar_ptr) != 0) {
|
2012-06-19 04:15:02 +02:00
|
|
|
result = CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
}
|
|
|
|
if(result == CURLE_PEER_FAILED_VERIFICATION) {
|
2012-07-05 22:16:15 +02:00
|
|
|
char *_cert_hostname;
|
|
|
|
_cert_hostname = Curl_convert_tchar_to_UTF8(cert_hostname.tchar_ptr);
|
2012-06-19 04:15:02 +02:00
|
|
|
failf(data, "schannel: CertGetNameString() certificate hostname "
|
|
|
|
"(%s) did not match connection (%s)",
|
|
|
|
_cert_hostname, conn->host.name);
|
2012-07-05 23:41:47 +02:00
|
|
|
Curl_unicodefree(_cert_hostname);
|
2012-06-19 04:15:02 +02:00
|
|
|
}
|
2012-07-05 22:16:15 +02:00
|
|
|
Curl_unicodefree(hostname.tchar_ptr);
|
2012-06-19 04:15:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pChainContext)
|
|
|
|
CertFreeCertificateChain(pChainContext);
|
|
|
|
|
|
|
|
if(pCertContextServer)
|
|
|
|
CertFreeCertificateContext(pCertContextServer);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif /* _WIN32_WCE */
|
|
|
|
|
2012-04-09 15:40:06 +02:00
|
|
|
#endif /* USE_SCHANNEL */
|