FreeRDP/channels/smartcard/client/smartcard_operations.c

2636 lines
64 KiB
C
Raw Normal View History

/**
2012-10-09 05:00:07 +04:00
* FreeRDP: A Remote Desktop Protocol Implementation
* Smartcard Device Service Virtual Channel
*
* Copyright (C) Alexi Volkov <alexi@myrealbox.com> 2006
* Copyright 2011 O.S. Systems Software Ltda.
* Copyright 2011 Anthony Tong <atong@trustedcs.com>
*
* 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
2011-10-15 19:30:10 +04:00
#include "config.h"
#endif
2011-10-15 19:30:10 +04:00
2013-08-30 18:30:27 +04:00
#include <assert.h>
2011-10-15 19:30:10 +04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
2012-10-02 17:57:32 +04:00
#define BOOL PCSC_BOOL
2011-10-15 19:30:10 +04:00
#include <PCSC/pcsclite.h>
#include <PCSC/reader.h>
#include <PCSC/winscard.h>
2012-10-02 17:57:32 +04:00
#undef BOOL
2011-10-15 19:30:10 +04:00
#include <winpr/crt.h>
#include <winpr/print.h>
#include <winpr/stream.h>
2013-03-22 01:58:18 +04:00
#include <freerdp/freerdp.h>
2012-10-09 05:00:07 +04:00
#include <freerdp/channels/rdpdr.h>
2013-03-22 01:58:18 +04:00
#include <freerdp/utils/svc_plugin.h>
2011-10-15 19:30:10 +04:00
#include "smartcard_main.h"
2011-10-15 19:30:10 +04:00
/* [MS-RDPESC] 3.1.4 */
2012-10-09 05:00:07 +04:00
#define SCARD_IOCTL_ESTABLISH_CONTEXT 0x00090014 /* EstablishContext */
#define SCARD_IOCTL_RELEASE_CONTEXT 0x00090018 /* ReleaseContext */
#define SCARD_IOCTL_IS_VALID_CONTEXT 0x0009001C /* IsValidContext */
#define SCARD_IOCTL_LIST_READER_GROUPS 0x00090020 /* ListReaderGroups */
#define SCARD_IOCTL_LIST_READERS 0x00090028 /* ListReadersA */
#define SCARD_IOCTL_INTRODUCE_READER_GROUP 0x00090050 /* IntroduceReaderGroup */
#define SCARD_IOCTL_FORGET_READER_GROUP 0x00090058 /* ForgetReader */
#define SCARD_IOCTL_INTRODUCE_READER 0x00090060 /* IntroduceReader */
#define SCARD_IOCTL_FORGET_READER 0x00090068 /* IntroduceReader */
#define SCARD_IOCTL_ADD_READER_TO_GROUP 0x00090070 /* AddReaderToGroup */
#define SCARD_IOCTL_REMOVE_READER_FROM_GROUP 0x00090078 /* RemoveReaderFromGroup */
#define SCARD_IOCTL_GET_STATUS_CHANGE 0x000900A0 /* GetStatusChangeA */
#define SCARD_IOCTL_CANCEL 0x000900A8 /* Cancel */
#define SCARD_IOCTL_CONNECT 0x000900AC /* ConnectA */
#define SCARD_IOCTL_RECONNECT 0x000900B4 /* Reconnect */
#define SCARD_IOCTL_DISCONNECT 0x000900B8 /* Disconnect */
#define SCARD_IOCTL_BEGIN_TRANSACTION 0x000900BC /* BeginTransaction */
#define SCARD_IOCTL_END_TRANSACTION 0x000900C0 /* EndTransaction */
#define SCARD_IOCTL_STATE 0x000900C4 /* State */
#define SCARD_IOCTL_STATUS 0x000900C8 /* StatusA */
#define SCARD_IOCTL_TRANSMIT 0x000900D0 /* Transmit */
#define SCARD_IOCTL_CONTROL 0x000900D4 /* Control */
#define SCARD_IOCTL_GETATTRIB 0x000900D8 /* GetAttrib */
#define SCARD_IOCTL_SETATTRIB 0x000900DC /* SetAttrib */
#define SCARD_IOCTL_ACCESS_STARTED_EVENT 0x000900E0 /* SCardAccessStartedEvent */
#define SCARD_IOCTL_LOCATE_CARDS_BY_ATR 0x000900E8 /* LocateCardsByATR */
/* Decode Win CTL_CODE values */
2012-10-09 05:00:07 +04:00
#define WIN_CTL_FUNCTION(ctl_code) ((ctl_code & 0x3FFC) >> 2)
#define WIN_CTL_DEVICE_TYPE(ctl_code) (ctl_code >> 16)
2012-10-09 05:00:07 +04:00
#define WIN_FILE_DEVICE_SMARTCARD 0x00000031
#if defined(DEBUG_SCARD) && (DEBUG_SCARD)
static char *ioctl_to_string(DWORD code)
{
static char res[100];
char *rc;
switch(code)
{
case SCARD_IOCTL_ESTABLISH_CONTEXT:
rc = "SCARD_IOCTL_ESTABLISH_CONTEXT";
break;
case SCARD_IOCTL_IS_VALID_CONTEXT:
rc = "SCARD_IOCTL_IS_VALID_CONTEXT";
break;
case SCARD_IOCTL_RELEASE_CONTEXT:
rc = "SCARD_IOCTL_RELEASE_CONTEXT";
break;
case SCARD_IOCTL_LIST_READERS:
rc = "SCARD_IOCTL_LIST_READERS";
break;
case SCARD_IOCTL_LIST_READERS + 4:
rc = "SCARD_IOCTL_LIST_READERS + 4";
break;
case SCARD_IOCTL_LIST_READER_GROUPS:
rc = "SCARD_IOCTL_LIST_READER_GROUPS";
break;
case SCARD_IOCTL_LIST_READER_GROUPS + 4:
rc = "SCARD_IOCTL_LIST_READER_GROUPS + 4";
break;
case SCARD_IOCTL_GET_STATUS_CHANGE:
rc = "SCARD_IOCTL_GET_STATUS_CHANGE";
break;
case SCARD_IOCTL_GET_STATUS_CHANGE + 4:
rc = "SCARD_IOCTL_GET_STATUS_CHANGE + 4";
break;
case SCARD_IOCTL_CANCEL:
rc = "SCARD_IOCTL_CANCEL";
break;
case SCARD_IOCTL_CONNECT:
rc = "SCARD_IOCTL_CONNECT";
break;
case SCARD_IOCTL_CONNECT + 4:
rc = "SCARD_IOCTL_CONNECT + 4";
break;
case SCARD_IOCTL_RECONNECT:
rc = "SCARD_IOCTL_RECONNECT";
break;
case SCARD_IOCTL_DISCONNECT:
rc = "SCARD_IOCTL_DISCONNECT";
break;
case SCARD_IOCTL_BEGIN_TRANSACTION:
rc = "SCARD_IOCTL_BEGIN_TRANSACTION";
break;
case SCARD_IOCTL_END_TRANSACTION:
rc = "SCARD_IOCTL_END_TRANSACTION";
break;
case SCARD_IOCTL_STATE:
rc = "SCARD_IOCTL_STATE";
break;
case SCARD_IOCTL_STATUS:
rc = "SCARD_IOCTL_STATUS";
break;
case SCARD_IOCTL_STATUS + 4:
rc = "SCARD_IOCTL_STATUS + 4";
break;
case SCARD_IOCTL_TRANSMIT:
rc = "SCARD_IOCTL_TRANSMIT";
break;
case SCARD_IOCTL_CONTROL:
rc = "SCARD_IOCTL_CONTROL";
break;
case SCARD_IOCTL_GETATTRIB:
rc = "SCARD_IOCTL_GETATTRIB";
break;
case SCARD_IOCTL_ACCESS_STARTED_EVENT:
rc = "SCARD_IOCTL_ACCESS_STARTED_EVENT";
break;
case SCARD_IOCTL_LOCATE_CARDS_BY_ATR:
rc = "SCARD_IOCTL_LOCATE_CARDS_BY_ATR";
break;
case SCARD_IOCTL_LOCATE_CARDS_BY_ATR + 4:
rc = "SCARD_IOCTL_LOCATE_CARDS_BY_ATR + 4";
break;
default:
rc = "UNKNOWN";
break;
}
memset(res, 0, sizeof(res));
strncpy(res, rc, strlen(rc));
return res;
}
#endif
static UINT32 handle_CommonTypeHeader(SMARTCARD_DEVICE* scard, IRP* irp)
{
UINT8 version;
UINT8 endianess;
UINT16 header_length;
assert(scard);
assert(irp);
assert(irp->input);
if (Stream_GetRemainingLength(irp->input) < 8)
{
DEBUG_WARN("length violation %d [%d]", 8,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
/* Process CommonTypeHeader */
Stream_Read_UINT8(irp->input, version);
Stream_Read_UINT8(irp->input, endianess);
Stream_Read_UINT16(irp->input, header_length);
Stream_Seek(irp->input, 4);
if (0x01 != version)
{
DEBUG_WARN("unsupported header version %d", version);
return SCARD_F_INTERNAL_ERROR;
}
if (0x10 != endianess)
{
DEBUG_WARN("unsupported endianess %d", endianess);
return SCARD_F_INTERNAL_ERROR;
}
if (0x08 != header_length)
{
DEBUG_WARN("unsupported header length %d", header_length);
return SCARD_F_INTERNAL_ERROR;
}
return 0;
}
static UINT32 handle_PrivateTypeHeader(SMARTCARD_DEVICE* scard, IRP* irp)
{
UINT32 len;
assert(scard);
assert(irp);
assert(irp->input);
if (Stream_GetRemainingLength(irp->input) < 8)
{
DEBUG_WARN("length violation %d [%d]", 8,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
/* Process PrivateTypeHeader */
Stream_Read_UINT32(irp->input, len);
Stream_Seek_UINT32(irp->input);
/* Assure the remaining length is as expected. */
if (len < Stream_GetRemainingLength(irp->input))
{
DEBUG_WARN("missing payload %d [%d]",
len, Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
return 0;
}
static UINT32 handle_Context(SMARTCARD_DEVICE* scard, IRP* irp, int *redirect)
{
UINT32 len;
assert(scard);
assert(irp);
assert(irp->input);
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
/* Process PrivateTypeHeader */
Stream_Read_UINT32(irp->input, len);
if (Stream_GetRemainingLength(irp->input) < len)
{
DEBUG_WARN("length violation %d [%d]", len,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
Stream_Seek(irp->input, len);
if (!len)
{
DEBUG_WARN("Context handle is NULL, using from cache.");
*redirect |= 0x01;
}
if (len > Stream_GetRemainingLength(irp->input))
{
DEBUG_WARN("length violation %d [%d]", len,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
return 0;
}
static UINT32 handle_CardHandle(SMARTCARD_DEVICE* scard, IRP* irp, int *redirect)
{
UINT32 status;
UINT32 len;
assert(scard);
assert(irp);
assert(irp->input);
assert(redirect);
status = handle_Context(scard, irp, redirect);
if (status)
return status;
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
Stream_Read_UINT32(irp->input, len);
if (Stream_GetRemainingLength(irp->input) < len)
{
DEBUG_WARN("length violation %d [%d]", len,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
Stream_Seek(irp->input, len);
if (!len)
{
DEBUG_WARN("Card handle is NULL, using from cache.");
*redirect |= 0x02;
}
return 0;
}
static UINT32 handle_RedirContextRef(SMARTCARD_DEVICE* scard, IRP* irp,
int redirect, SCARDCONTEXT* hContext)
{
UINT32 len;
assert(scard);
assert(irp);
assert(irp->input);
assert(hContext);
/* No context provided, use stored. */
if (redirect & 0x01)
{
DEBUG_WARN("No context provided, using stored context.");
*hContext = scard->hContext;
return 0;
}
/* Extract context handle. */
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
Stream_Read_UINT32(irp->input, len);
if (len > Stream_GetRemainingLength(irp->input))
{
DEBUG_WARN("length violation %d [%d]", len,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
memset(hContext, 0, sizeof(SCARDCONTEXT));
Stream_Read(irp->input, hContext, len);
DEBUG_SCARD("hContext=%p", *hContext);
return 0;
}
static UINT32 handle_RedirHandleRef(SMARTCARD_DEVICE* scard, IRP* irp,
int redirect, SCARDCONTEXT* hContext, SCARDHANDLE *hHandle)
{
UINT32 len, status;
status = handle_RedirContextRef(scard, irp, redirect, hContext);
if (status)
return status;
/* Use stored card handle. */
if (redirect & 0x02)
{
DEBUG_WARN("No card handle provided, using stored handle.");
*hHandle = scard->hCard;
return 0;
}
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
Stream_Read_UINT32(irp->input, len);
if (len != 4)
{
DEBUG_WARN("length violation %d [%d]", len, 4);
return SCARD_F_INTERNAL_ERROR;
}
if (Stream_GetRemainingLength(irp->input) < len)
{
DEBUG_WARN("length violation %d [%d]", len,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
memset(hHandle, 0, sizeof(SCARDHANDLE));
Stream_Read_UINT32(irp->input, *hHandle);
DEBUG_SCARD("hCard=%p", *hHandle);
return 0;
}
static BOOL check_reader_is_forwarded(SMARTCARD_DEVICE *scard, const char *readerName)
{
BOOL rc = TRUE;
char *name = _strdup(readerName);
char *str, *strpos=NULL, *strstatus=NULL;
long pos, cpos, ret;
assert(scard);
assert(readerName);
/* Extract the name, position and status from the data provided. */
str = strtok(name, " ");
while(str)
{
strpos = strstatus;
strstatus = str;
str = strtok(NULL, " ");
}
2013-09-13 11:47:18 +04:00
if (!strpos)
goto finally;
pos = strtol(strpos, NULL, 10);
if ( strpos && strstatus )
{
/* Check, if the name of the reader matches. */
if (scard->name && strncmp(scard->name, readerName, strlen(scard->name)))
rc = FALSE;
/* Check, if the position matches. */
if (scard->path)
{
ret = sscanf(scard->path, "%ld", &cpos);
if ((1 == ret) && (cpos != pos))
rc = FALSE;
}
}
else
DEBUG_WARN("unknown reader format '%s'", readerName);
2013-09-13 11:47:18 +04:00
finally:
free(name);
if (!rc)
DEBUG_WARN("reader '%s' not forwarded", readerName);
return rc;
}
static BOOL check_handle_is_forwarded(SMARTCARD_DEVICE *scard,
SCARDHANDLE hCard, SCARDCONTEXT hContext)
{
BOOL rc = FALSE;
LONG status;
DWORD state = 0, protocol = 0;
DWORD readerLen;
DWORD atrLen = MAX_ATR_SIZE;
char* readerName = NULL;
BYTE pbAtr[MAX_ATR_SIZE];
assert(scard);
assert(hCard);
#ifdef SCARD_AUTOALLOCATE
readerLen = SCARD_AUTOALLOCATE;
#else
readerLen = 256;
readerName = malloc(readerLen);
#endif
status = SCardStatus(hCard, (LPSTR) &readerName, &readerLen, &state, &protocol, pbAtr, &atrLen);
if (status == SCARD_S_SUCCESS)
{
rc = check_reader_is_forwarded(scard, readerName);
if (!rc)
DEBUG_WARN("Reader '%s' not forwarded!", readerName);
}
#ifdef SCARD_AUTOALLOCATE
SCardFreeMemory(hContext, readerName);
#else
free(readerName);
#endif
return rc;
}
static UINT32 smartcard_output_string(IRP* irp, char* src, BOOL wide)
2011-10-15 19:30:10 +04:00
{
BYTE* p;
2012-10-09 11:26:39 +04:00
UINT32 len;
2011-10-15 19:30:10 +04:00
p = Stream_Pointer(irp->output);
2011-10-15 19:30:10 +04:00
len = strlen(src) + 1;
if (wide)
{
int i;
2011-10-15 19:30:10 +04:00
for (i = 0; i < len; i++ )
{
p[2 * i] = src[i] < 0 ? '?' : src[i];
p[2 * i + 1] = '\0';
}
len *= 2;
}
else
{
memcpy(p, src, len);
}
Stream_Seek(irp->output, len);
2011-10-15 19:30:10 +04:00
return len;
}
static void smartcard_output_alignment(IRP* irp, UINT32 seed)
2011-10-15 19:30:10 +04:00
{
2012-10-09 11:26:39 +04:00
const UINT32 field_lengths = 20;/* Remove the lengths of the fields
* RDPDR_HEADER, DeviceID,
* CompletionID, and IoStatus
* of Section 2.2.1.5.5 of MS-RDPEFS.
*/
UINT32 size = Stream_GetPosition(irp->output) - field_lengths;
2012-10-09 11:26:39 +04:00
UINT32 add = (seed - (size % seed)) % seed;
2011-10-15 19:30:10 +04:00
if (add > 0)
2013-05-09 00:27:21 +04:00
Stream_Zero(irp->output, add);
2011-10-15 19:30:10 +04:00
}
static void smartcard_output_repos(IRP* irp, UINT32 written)
2011-10-15 19:30:10 +04:00
{
2012-10-09 11:26:39 +04:00
UINT32 add = (4 - (written % 4)) % 4;
2011-10-15 19:30:10 +04:00
if (add > 0)
2013-05-09 00:27:21 +04:00
Stream_Zero(irp->output, add);
2011-10-15 19:30:10 +04:00
}
static UINT32 smartcard_output_return(IRP* irp, UINT32 status)
2011-10-15 19:30:10 +04:00
{
2013-05-09 00:27:21 +04:00
Stream_Zero(irp->output, 256);
return status;
2011-10-15 19:30:10 +04:00
}
static void smartcard_output_buffer_limit(IRP* irp, char* buffer, unsigned int length, unsigned int highLimit)
2011-10-15 19:30:10 +04:00
{
int header = (length < 0) ? (0) : ((length > highLimit) ? (highLimit) : (length));
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, header);
2011-10-15 19:30:10 +04:00
if (length <= 0)
{
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, 0);
2011-10-15 19:30:10 +04:00
}
else
{
2013-08-30 18:30:27 +04:00
assert(NULL != buffer);
2011-10-15 19:30:10 +04:00
if (header < length)
length = header;
2013-05-09 00:09:16 +04:00
Stream_Write(irp->output, buffer, length);
smartcard_output_repos(irp, length);
2011-10-15 19:30:10 +04:00
}
}
static void smartcard_output_buffer(IRP* irp, char* buffer, unsigned int length)
2011-10-15 19:30:10 +04:00
{
smartcard_output_buffer_limit(irp, buffer, length, 0x7FFFFFFF);
2011-10-15 19:30:10 +04:00
}
static void smartcard_output_buffer_start_limit(IRP* irp, int length, int highLimit)
2011-10-15 19:30:10 +04:00
{
int header = (length < 0) ? (0) : ((length > highLimit) ? (highLimit) : (length));
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, header);
Stream_Write_UINT32(irp->output, 0x00000001); /* Magic DWORD - any non zero */
2011-10-15 19:30:10 +04:00
}
static void smartcard_output_buffer_start(IRP* irp, int length)
2011-10-15 19:30:10 +04:00
{
smartcard_output_buffer_start_limit(irp, length, 0x7FFFFFFF);
2011-10-15 19:30:10 +04:00
}
static UINT32 smartcard_input_string(IRP* irp, char** dest, UINT32 dataLength, BOOL wide)
2011-10-15 19:30:10 +04:00
{
2012-10-09 05:00:07 +04:00
char* buffer;
2011-10-15 19:30:10 +04:00
int bufferSize;
bufferSize = wide ? (2 * dataLength) : dataLength;
buffer = malloc(bufferSize + 2); /* reserve 2 bytes for the '\0' */
2011-10-15 19:30:10 +04:00
2013-05-09 00:09:16 +04:00
Stream_Read(irp->input, buffer, bufferSize);
2012-10-09 05:00:07 +04:00
2011-10-15 19:30:10 +04:00
if (wide)
{
int i;
for (i = 0; i < dataLength; i++)
{
if ((buffer[2 * i] < 0) || (buffer[2 * i + 1] != 0))
buffer[i] = '?';
else
buffer[i] = buffer[2 * i];
}
}
buffer[dataLength] = '\0';
*dest = buffer;
return bufferSize;
}
static void smartcard_input_repos(IRP* irp, UINT32 read)
2011-10-15 19:30:10 +04:00
{
2012-10-09 11:26:39 +04:00
UINT32 add = 4 - (read % 4);
2011-10-15 19:30:10 +04:00
if (add < 4 && add > 0)
Stream_Seek(irp->input, add);
2011-10-15 19:30:10 +04:00
}
static UINT32 smartcard_input_reader_name(IRP* irp, char** dest, BOOL wide)
2011-10-15 19:30:10 +04:00
{
2012-10-09 11:26:39 +04:00
UINT32 dataLength;
2011-10-15 19:30:10 +04:00
assert(irp);
assert(dest);
if (Stream_GetRemainingLength(irp->input) < 12)
{
DEBUG_WARN("length violation %d [%d]", 12,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
Stream_Seek(irp->input, 8);
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, dataLength);
2011-10-15 19:30:10 +04:00
if (Stream_GetRemainingLength(irp->input) < dataLength)
{
DEBUG_WARN("length violation %d [%d]", dataLength,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
2011-10-15 19:30:10 +04:00
DEBUG_SCARD("datalength %d", dataLength);
smartcard_input_repos(irp, smartcard_input_string(irp, dest, dataLength, wide));
return 0;
2011-10-15 19:30:10 +04:00
}
static UINT32 smartcard_map_state(UINT32 state)
2011-10-15 19:30:10 +04:00
{
/* is this mapping still needed? */
2011-10-15 19:30:10 +04:00
if (state & SCARD_SPECIFIC)
state = 0x00000006;
else if (state & SCARD_NEGOTIABLE)
state = 0x00000006;
2011-10-15 19:30:10 +04:00
else if (state & SCARD_POWERED)
state = 0x00000004;
else if (state & SCARD_SWALLOWED)
state = 0x00000003;
else if (state & SCARD_PRESENT)
state = 0x00000002;
else if (state & SCARD_ABSENT)
state = 0x00000001;
else
state = 0x00000000;
return state;
}
static UINT32 handle_EstablishContext(SMARTCARD_DEVICE* scard, IRP* irp)
2011-10-15 19:30:10 +04:00
{
UINT32 status;
2012-10-09 11:26:39 +04:00
UINT32 scope;
2011-10-15 19:30:10 +04:00
SCARDCONTEXT hContext = -1;
status = handle_CommonTypeHeader(scard, irp);
if (status)
return status;
status = handle_PrivateTypeHeader(scard, irp);
if (status)
return status;
/* Ensure, that the capacity expected is actually available. */
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
2011-10-15 19:30:10 +04:00
return SCARD_F_INTERNAL_ERROR;
}
2011-10-15 19:30:10 +04:00
/* Read the scope from the stream. */
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, scope);
2011-10-15 19:30:10 +04:00
status = SCardEstablishContext(scope, NULL, NULL, &hContext);
2011-10-15 19:30:10 +04:00
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, 4); // cbContext
Stream_Write_UINT32(irp->output, -1); // ReferentID
2011-10-15 19:30:10 +04:00
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, 4);
Stream_Write_UINT32(irp->output, hContext);
2011-10-15 19:30:10 +04:00
/* store hContext in allowed context list */
scard->hContext = hContext;
2011-10-15 19:30:10 +04:00
smartcard_output_alignment(irp, 8);
2013-09-13 11:47:18 +04:00
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_ReleaseContext(SMARTCARD_DEVICE* scard, IRP* irp)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
UINT32 status;
2011-10-15 19:30:10 +04:00
SCARDCONTEXT hContext = -1;
status = handle_CommonTypeHeader(scard, irp);
if (status)
return status;
2011-10-15 19:30:10 +04:00
status = handle_PrivateTypeHeader(scard, irp);
if (status)
return status;
status = handle_Context(scard, irp, &redirect);
if (status)
return status;
status = handle_RedirContextRef(scard, irp, redirect, &hContext);
if (status)
return status;
2011-10-15 19:30:10 +04:00
status = SCardReleaseContext(hContext);
memset(&scard->hContext, 0, sizeof(scard->hContext));
if (status)
DEBUG_SCARD("%s (0x%08x)", pcsc_stringify_error(status), (unsigned) status);
2011-10-15 19:30:10 +04:00
else
DEBUG_SCARD("success 0x%08lx", hContext);
smartcard_output_alignment(irp, 8);
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_IsValidContext(SMARTCARD_DEVICE* scard, IRP* irp)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
UINT32 status;
2011-10-15 19:30:10 +04:00
SCARDCONTEXT hContext;
status = handle_CommonTypeHeader(scard, irp);
if (status)
return status;
status = handle_PrivateTypeHeader(scard, irp);
if (status)
return status;
status = handle_Context(scard, irp, &redirect);
if (status)
return status;
status = handle_RedirContextRef(scard, irp, redirect, &hContext);
if (status)
return status;
2011-10-15 19:30:10 +04:00
status = SCardIsValidContext(hContext);
2011-10-15 19:30:10 +04:00
if (status)
DEBUG_SCARD("Failure: %s (0x%08x)", pcsc_stringify_error(status), (unsigned) status);
2011-10-15 19:30:10 +04:00
else
DEBUG_SCARD("Success context: 0x%08x", (unsigned) hContext);
smartcard_output_alignment(irp, 8);
2011-10-15 19:30:10 +04:00
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_ListReaders(SMARTCARD_DEVICE* scard, IRP* irp, BOOL wide)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
UINT32 status;
2011-10-15 19:30:10 +04:00
SCARDCONTEXT hContext;
DWORD dwReaders;
UINT32 cBytes;
INT32 fmszReadersIsNULL;
UINT32 cchReaders;
LPTSTR mszGroups = NULL;
2011-10-15 19:30:10 +04:00
char *readerList = NULL, *walker;
int elemLength, dataLength;
int pos, poslen1, poslen2, allowed_pos;
2011-10-15 19:30:10 +04:00
status = handle_CommonTypeHeader(scard, irp);
if (status)
goto finish;
2011-10-15 19:30:10 +04:00
status = handle_PrivateTypeHeader(scard, irp);
if (status)
goto finish;
2011-10-15 19:30:10 +04:00
status = handle_Context(scard, irp, &redirect);
if (status)
goto finish;
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
Stream_Read_UINT32(irp->input, cBytes);
/* Ensure, that the capacity expected is actually available. */
if (Stream_GetRemainingLength(irp->input) < cBytes)
{
DEBUG_WARN("length violation %d [%d]", cBytes,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
if (cBytes)
{
mszGroups = malloc(cBytes);
Stream_Read(irp->input, mszGroups, cBytes);
}
2011-10-15 19:30:10 +04:00
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
Stream_Read_UINT32(irp->input, fmszReadersIsNULL);
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
Stream_Read_UINT32(irp->input, cchReaders);
Stream_Seek(irp->input, 4);
/* Read RedirScardcontextRef */
status = handle_RedirContextRef(scard, irp, redirect, &hContext);
if (status)
goto finish;
2011-10-15 19:30:10 +04:00
/* ignore rest of [MS-RDPESC] 2.2.2.4 ListReaders_Call */
#ifdef SCARD_AUTOALLOCATE
dwReaders = SCARD_AUTOALLOCATE;
status = SCardListReaders(hContext, mszGroups, (LPSTR) &readerList, &dwReaders);
2011-10-15 19:30:10 +04:00
#else
status = SCardListReaders(hContext, mszGroups, NULL, &dwReaders);
2011-10-15 19:30:10 +04:00
readerList = malloc(dwReaders);
status = SCardListReaders(hContext, mszGroups, readerList, &dwReaders);
2011-10-15 19:30:10 +04:00
#endif
if (status != SCARD_S_SUCCESS)
2011-10-15 19:30:10 +04:00
{
DEBUG_SCARD("Failure: %s (0x%08x)", pcsc_stringify_error(status), (unsigned) status);
goto finish;
2011-10-15 19:30:10 +04:00
}
/* DEBUG_SCARD("Success 0x%08x %d %d", (unsigned) hContext, (unsigned) cchReaders, (int) strlen(readerList));*/
2011-10-15 19:30:10 +04:00
poslen1 = Stream_GetPosition(irp->output);
Stream_Seek_UINT32(irp->output);
2011-10-15 19:30:10 +04:00
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, 0x01760650);
2011-10-15 19:30:10 +04:00
poslen2 = Stream_GetPosition(irp->output);
Stream_Seek_UINT32(irp->output);
2011-10-15 19:30:10 +04:00
walker = readerList;
dataLength = 0;
/* Smartcards can be forwarded by position and name. */
allowed_pos = -1;
if (scard->path)
if (1 != sscanf(scard->path, "%d", &allowed_pos))
allowed_pos = -1;
pos = 0;
2011-10-15 19:30:10 +04:00
while (1)
{
elemLength = strlen(walker);
if (elemLength == 0)
break;
/* Ignore readers not forwarded. */
if ((allowed_pos < 0) || (pos == allowed_pos))
{
if (!scard->name || strstr(walker, scard->name))
dataLength += smartcard_output_string(irp, walker, wide);
}
2011-10-15 19:30:10 +04:00
walker += elemLength + 1;
pos ++;
2011-10-15 19:30:10 +04:00
}
dataLength += smartcard_output_string(irp, "\0", wide);
2011-10-15 19:30:10 +04:00
pos = Stream_GetPosition(irp->output);
2011-10-15 19:30:10 +04:00
Stream_SetPosition(irp->output, poslen1);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, dataLength);
Stream_SetPosition(irp->output, poslen2);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, dataLength);
2011-10-15 19:30:10 +04:00
Stream_SetPosition(irp->output, pos);
2011-10-15 19:30:10 +04:00
smartcard_output_repos(irp, dataLength);
smartcard_output_alignment(irp, 8);
2011-10-15 19:30:10 +04:00
finish:
if (readerList)
{
2011-10-15 19:30:10 +04:00
#ifdef SCARD_AUTOALLOCATE
SCardFreeMemory(hContext, readerList);
2011-10-15 19:30:10 +04:00
#else
free(readerList);
2011-10-15 19:30:10 +04:00
#endif
}
2011-10-15 19:30:10 +04:00
if (mszGroups)
free(mszGroups);
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_GetStatusChange(SMARTCARD_DEVICE* scard, IRP* irp, BOOL wide)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
int i;
LONG status;
2011-10-15 19:30:10 +04:00
SCARDCONTEXT hContext;
DWORD dwTimeout = 0;
DWORD readerCount = 0;
SCARD_READERSTATE *readerStates = NULL, *cur;
2011-10-15 19:30:10 +04:00
status = handle_CommonTypeHeader(scard, irp);
if (status)
goto finish;
status = handle_PrivateTypeHeader(scard, irp);
if (status)
goto finish;
status = handle_Context(scard, irp, &redirect);
if (status)
goto finish;
/* Ensure, that the capacity expected is actually available. */
if (Stream_GetRemainingLength(irp->input) < 12)
{
DEBUG_WARN("length violation %d [%d]", 12,
Stream_GetRemainingLength(irp->input));
status =SCARD_F_INTERNAL_ERROR;
goto finish;
}
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, dwTimeout);
Stream_Read_UINT32(irp->input, readerCount);
2011-10-15 19:30:10 +04:00
/* Skip reader state */
Stream_Seek(irp->input, 4);
2011-10-15 19:30:10 +04:00
/* Get context */
status = handle_RedirContextRef(scard, irp, redirect, &hContext);
if (status)
goto finish;
2011-10-15 19:30:10 +04:00
/* Skip ReaderStateConformant */
if (Stream_GetRemainingLength(irp->input) < 4 )
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
Stream_Seek(irp->input, 4);
2011-10-15 19:30:10 +04:00
DEBUG_SCARD("context: 0x%08x, timeout: 0x%08x, count: %d",
(unsigned) hContext, (unsigned) dwTimeout, (int) readerCount);
2011-10-15 19:30:10 +04:00
if (readerCount > 0)
{
readerStates = malloc(readerCount * sizeof(SCARD_READERSTATE));
ZeroMemory(readerStates, readerCount * sizeof(SCARD_READERSTATE));
2011-10-15 19:30:10 +04:00
for (i = 0; i < readerCount; i++)
{
cur = &readerStates[i];
if (Stream_GetRemainingLength(irp->input) < 52 )
{
DEBUG_WARN("length violation %d [%d]", 52,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
Stream_Seek(irp->input, 4);
2011-10-15 19:30:10 +04:00
/*
* TODO: on-wire is little endian; need to either
* convert to host endian or fix the headers to
* request the order we want
*/
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, cur->dwCurrentState);
Stream_Read_UINT32(irp->input, cur->dwEventState);
Stream_Read_UINT32(irp->input, cur->cbAtr);
Stream_Read(irp->input, cur->rgbAtr, 32);
2011-10-15 19:30:10 +04:00
Stream_Seek(irp->input, 4);
2011-10-15 19:30:10 +04:00
/* reset high bytes? */
cur->dwCurrentState &= 0x0000FFFF;
cur->dwEventState = 0;
}
for (i = 0; i < readerCount; i++)
{
cur = &readerStates[i];
2012-10-09 11:26:39 +04:00
UINT32 dataLength;
2011-10-15 19:30:10 +04:00
if (Stream_GetRemainingLength(irp->input) < 12 )
{
DEBUG_WARN("length violation %d [%d]", 12,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
Stream_Seek(irp->input, 8);
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, dataLength);
if (Stream_GetRemainingLength(irp->input) < dataLength )
{
DEBUG_WARN("length violation %d [%d]", dataLength,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
smartcard_input_repos(irp, smartcard_input_string(irp,
(char **) &cur->szReader, dataLength, wide));
2011-10-15 19:30:10 +04:00
DEBUG_SCARD(" \"%s\"", cur->szReader ? cur->szReader : "NULL");
DEBUG_SCARD(" user: 0x%08x, state: 0x%08x, event: 0x%08x",
(unsigned) cur->pvUserData, (unsigned) cur->dwCurrentState,
(unsigned) cur->dwEventState);
2013-08-30 18:30:27 +04:00
if (!cur->szReader)
{
DEBUG_WARN("cur->szReader=%p", cur->szReader);
continue;
}
2011-10-15 19:30:10 +04:00
if (strcmp(cur->szReader, "\\\\?PnP?\\Notification") == 0)
cur->dwCurrentState |= SCARD_STATE_IGNORE;
}
}
else
{
readerStates = NULL;
}
status = SCardGetStatusChange(hContext, (DWORD) dwTimeout, readerStates, (DWORD) readerCount);
2011-10-15 19:30:10 +04:00
if (status != SCARD_S_SUCCESS)
DEBUG_SCARD("Failure: %s (0x%08x)", pcsc_stringify_error(status), (unsigned) status);
2011-10-15 19:30:10 +04:00
else
DEBUG_SCARD("Success");
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, readerCount);
Stream_Write_UINT32(irp->output, 0x00084dd8);
Stream_Write_UINT32(irp->output, readerCount);
2011-10-15 19:30:10 +04:00
for (i = 0; i < readerCount; i++)
{
cur = &readerStates[i];
DEBUG_SCARD(" \"%s\"", cur->szReader ? cur->szReader : "NULL");
DEBUG_SCARD(" user: 0x%08x, state: 0x%08x, event: 0x%08x",
2011-10-15 19:30:10 +04:00
(unsigned) cur->pvUserData, (unsigned) cur->dwCurrentState,
(unsigned) cur->dwEventState);
/* TODO: do byte conversions if necessary */
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, cur->dwCurrentState);
Stream_Write_UINT32(irp->output, cur->dwEventState);
Stream_Write_UINT32(irp->output, cur->cbAtr);
Stream_Write(irp->output, cur->rgbAtr, 32);
2011-10-15 19:30:10 +04:00
2013-05-09 00:27:21 +04:00
Stream_Zero(irp->output, 4);
2011-10-15 19:30:10 +04:00
free((void *)cur->szReader);
2011-10-15 19:30:10 +04:00
}
smartcard_output_alignment(irp, 8);
2011-10-15 19:30:10 +04:00
finish:
if (readerStates)
free(readerStates);
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_Cancel(SMARTCARD_DEVICE *scard, IRP* irp)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
LONG status;
2011-10-15 19:30:10 +04:00
SCARDCONTEXT hContext;
status = handle_CommonTypeHeader(scard, irp);
if (status)
return status;
status = handle_PrivateTypeHeader(scard, irp);
if (status)
return status;
status = handle_Context(scard, irp, &redirect);
if (status)
return status;
status = handle_RedirContextRef(scard, irp, redirect, &hContext);
if (status)
return status;
2011-10-15 19:30:10 +04:00
status = SCardCancel(hContext);
2011-10-15 19:30:10 +04:00
if (status != SCARD_S_SUCCESS)
DEBUG_SCARD("Failure: %s (0x%08x)", pcsc_stringify_error(status), (unsigned) status);
2011-10-15 19:30:10 +04:00
else
DEBUG_SCARD("Success context: 0x%08x %s", (unsigned) hContext, pcsc_stringify_error(status));
2011-10-15 19:30:10 +04:00
smartcard_output_alignment(irp, 8);
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_Connect(SMARTCARD_DEVICE* scard, IRP* irp, BOOL wide)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
LONG status;
2011-10-15 19:30:10 +04:00
SCARDCONTEXT hContext;
char* readerName = NULL;
2011-10-15 19:30:10 +04:00
DWORD dwShareMode = 0;
DWORD dwPreferredProtocol = 0;
DWORD dwActiveProtocol = 0;
SCARDHANDLE hCard;
status = handle_CommonTypeHeader(scard, irp);
if (status)
goto finish;
2011-10-15 19:30:10 +04:00
status = handle_PrivateTypeHeader(scard, irp);
if (status)
goto finish;
2011-10-15 19:30:10 +04:00
/* Skip ptrReader */
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("Length violadion %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
Stream_Seek(irp->input, 4);
/* Read common data */
status = handle_Context(scard, irp, &redirect);
if (status)
goto finish;
if (Stream_GetRemainingLength(irp->input) < 8)
{
DEBUG_WARN("Length violadion %d [%d]", 8,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
Stream_Read_UINT32(irp->input, dwShareMode);
Stream_Read_UINT32(irp->input, dwPreferredProtocol);
status = smartcard_input_reader_name(irp, &readerName, wide);
if (status)
goto finish;
status = handle_RedirContextRef(scard, irp, redirect, &hContext);
if (status)
goto finish;
2011-10-15 19:30:10 +04:00
DEBUG_SCARD("(context: 0x%08x, share: 0x%08x, proto: 0x%08x, reader: \"%s\")",
(unsigned) hContext, (unsigned) dwShareMode,
(unsigned) dwPreferredProtocol, readerName ? readerName : "NULL");
if (!check_reader_is_forwarded(scard, readerName))
{
DEBUG_WARN("Reader '%s' not forwarded!", readerName);
status = SCARD_E_INVALID_TARGET;
goto finish;
}
status = SCardConnect(hContext, readerName, (DWORD) dwShareMode,
2011-10-15 19:30:10 +04:00
(DWORD) dwPreferredProtocol, &hCard, (DWORD *) &dwActiveProtocol);
if (status != SCARD_S_SUCCESS)
DEBUG_WARN("Failure: %s 0x%08x", pcsc_stringify_error(status), (unsigned) status);
2011-10-15 19:30:10 +04:00
else
DEBUG_SCARD("Success 0x%08x", (unsigned) hCard);
scard->hCard = hCard;
2011-10-15 19:30:10 +04:00
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, 0x00000000);
Stream_Write_UINT32(irp->output, 0x00000000);
Stream_Write_UINT32(irp->output, 0x00000004);
Stream_Write_UINT32(irp->output, 0x016Cff34);
Stream_Write_UINT32(irp->output, dwActiveProtocol);
Stream_Write_UINT32(irp->output, 0x00000004);
Stream_Write_UINT32(irp->output, hCard);
2011-10-15 19:30:10 +04:00
smartcard_output_alignment(irp, 8);
2011-10-15 19:30:10 +04:00
finish:
if (readerName)
free(readerName);
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_Reconnect(SMARTCARD_DEVICE* scard, IRP* irp)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
LONG status;
2011-10-15 19:30:10 +04:00
SCARDCONTEXT hContext;
SCARDHANDLE hCard;
DWORD dwShareMode = 0;
DWORD dwPreferredProtocol = 0;
DWORD dwInitialization = 0;
DWORD dwActiveProtocol = 0;
status = handle_CommonTypeHeader(scard, irp);
if (status)
return status;
status = handle_PrivateTypeHeader(scard, irp);
if (status)
return status;
status = handle_CardHandle(scard, irp, &redirect);
if (status)
return status;
if (Stream_GetRemainingLength(irp->input) < 12)
{
DEBUG_WARN("length violation %d [%d]", 12,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, dwShareMode);
Stream_Read_UINT32(irp->input, dwPreferredProtocol);
Stream_Read_UINT32(irp->input, dwInitialization);
2011-10-15 19:30:10 +04:00
status = handle_RedirHandleRef(scard, irp, redirect, &hContext, &hCard);
if (status)
return status;
2011-10-15 19:30:10 +04:00
DEBUG_SCARD("(context: 0x%08x, hcard: 0x%08x, share: 0x%08x, proto: 0x%08x, init: 0x%08x)",
(unsigned) hContext, (unsigned) hCard,
(unsigned) dwShareMode, (unsigned) dwPreferredProtocol, (unsigned) dwInitialization);
if (!check_handle_is_forwarded(scard, hCard, hContext))
{
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
return SCARD_E_INVALID_TARGET;
}
status = SCardReconnect(hCard, (DWORD) dwShareMode, (DWORD) dwPreferredProtocol,
2011-10-15 19:30:10 +04:00
(DWORD) dwInitialization, (LPDWORD) &dwActiveProtocol);
if (status != SCARD_S_SUCCESS)
DEBUG_SCARD("Failure: %s (0x%08x)", pcsc_stringify_error(status), (unsigned) status);
2011-10-15 19:30:10 +04:00
else
DEBUG_SCARD("Success (proto: 0x%08x)", (unsigned) dwActiveProtocol);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, dwActiveProtocol);
smartcard_output_alignment(irp, 8);
2011-10-15 19:30:10 +04:00
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_Disconnect(SMARTCARD_DEVICE* scard, IRP* irp)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
LONG status;
2011-10-15 19:30:10 +04:00
SCARDCONTEXT hContext;
SCARDHANDLE hCard;
DWORD dwDisposition = 0;
status = handle_CommonTypeHeader(scard, irp);
if (status)
return status;
status = handle_PrivateTypeHeader(scard, irp);
if (status)
return status;
status = handle_CardHandle(scard, irp, &redirect);
if (status)
return status;
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, dwDisposition);
status = handle_RedirHandleRef(scard, irp, redirect, &hContext, &hCard);
if (status)
return status;
2011-10-15 19:30:10 +04:00
DEBUG_SCARD("(context: 0x%08x, hcard: 0x%08x, disposition: 0x%08x)",
(unsigned) hContext, (unsigned) hCard, (unsigned) dwDisposition);
if (!check_handle_is_forwarded(scard, hCard, hContext))
{
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
return SCARD_E_INVALID_TARGET;
}
status = SCardDisconnect(hCard, (DWORD) dwDisposition);
2011-10-15 19:30:10 +04:00
if (status != SCARD_S_SUCCESS)
DEBUG_SCARD("Failure: %s (0x%08x)", pcsc_stringify_error(status), (unsigned) status);
2011-10-15 19:30:10 +04:00
else
DEBUG_SCARD("Success");
memset(&scard->hCard, 0, sizeof(scard->hCard));
2011-10-15 19:30:10 +04:00
smartcard_output_alignment(irp, 8);
2011-10-15 19:30:10 +04:00
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_BeginTransaction(SMARTCARD_DEVICE* scard, IRP* irp)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
LONG status;
SCARDHANDLE hCard;
SCARDCONTEXT hContext;
status = handle_CommonTypeHeader(scard, irp);
if (status)
return status;
status = handle_PrivateTypeHeader(scard, irp);
if (status)
return status;
2011-10-15 19:30:10 +04:00
status = handle_CardHandle(scard, irp, &redirect);
if (status)
return status;
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
Stream_Seek(irp->input, 4);
status = handle_RedirHandleRef(scard, irp, redirect, &hContext, &hCard);
if (status)
return status;
if (!check_handle_is_forwarded(scard, hCard, hContext))
{
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
return SCARD_E_INVALID_TARGET;
}
2011-10-15 19:30:10 +04:00
status = SCardBeginTransaction(hCard);
2011-10-15 19:30:10 +04:00
if (status != SCARD_S_SUCCESS)
DEBUG_SCARD("Failure: %s (0x%08x)", pcsc_stringify_error(status), (unsigned) status);
2011-10-15 19:30:10 +04:00
else
DEBUG_SCARD("Success hcard: 0x%08x", (unsigned) hCard);
smartcard_output_alignment(irp, 8);
2011-10-15 19:30:10 +04:00
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_EndTransaction(SMARTCARD_DEVICE* scard, IRP* irp)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
LONG status;
SCARDHANDLE hCard;
SCARDCONTEXT hContext;
2011-10-15 19:30:10 +04:00
DWORD dwDisposition = 0;
status = handle_CommonTypeHeader(scard, irp);
if (status)
return status;
status = handle_PrivateTypeHeader(scard, irp);
if (status)
return status;
status = handle_CardHandle(scard, irp, &redirect);
if (status)
return status;
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, dwDisposition);
2011-10-15 19:30:10 +04:00
status = handle_RedirHandleRef(scard, irp, redirect, &hContext, &hCard);
if (status)
return status;
if (!check_handle_is_forwarded(scard, hCard, hContext))
{
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
return SCARD_E_INVALID_TARGET;
}
2011-10-15 19:30:10 +04:00
status = SCardEndTransaction(hCard, dwDisposition);
if (status != SCARD_S_SUCCESS)
DEBUG_SCARD("Failure: %s (0x%08x)", pcsc_stringify_error(status), (unsigned) status);
2011-10-15 19:30:10 +04:00
else
DEBUG_SCARD("Success hcard: 0x%08x", (unsigned) hCard);
smartcard_output_alignment(irp, 8);
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_State(SMARTCARD_DEVICE* scard, IRP* irp)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
LONG status;
2011-10-15 19:30:10 +04:00
SCARDHANDLE hCard;
SCARDCONTEXT hContext;
2011-10-15 19:30:10 +04:00
DWORD state = 0, protocol = 0;
DWORD readerLen;
DWORD atrLen = MAX_ATR_SIZE;
2013-09-13 11:47:18 +04:00
char* readerName = NULL;
2011-10-15 19:30:10 +04:00
BYTE pbAtr[MAX_ATR_SIZE];
#ifdef WITH_DEBUG_SCARD
int i;
#endif
status = handle_CommonTypeHeader(scard, irp);
if (status)
goto finish;
status = handle_PrivateTypeHeader(scard, irp);
if (status)
goto finish;
status = handle_CardHandle(scard, irp, &redirect);
if (status)
goto finish;
if (Stream_GetRemainingLength(irp->input) < 8)
{
DEBUG_WARN("length violation %d [%d]", 8,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
Stream_Seek(irp->input, 4);
Stream_Seek_UINT32(irp->input); /* atrLen */
status = handle_RedirHandleRef(scard, irp, redirect, &hContext, &hCard);
if (status)
goto finish;
2011-10-15 19:30:10 +04:00
if (!check_handle_is_forwarded(scard, hCard, hContext))
{
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
status = SCARD_E_INVALID_TARGET;
goto finish;
}
2011-10-15 19:30:10 +04:00
#ifdef SCARD_AUTOALLOCATE
readerLen = SCARD_AUTOALLOCATE;
status = SCardStatus(hCard, (LPSTR) &readerName, &readerLen, &state, &protocol, pbAtr, &atrLen);
2011-10-15 19:30:10 +04:00
#else
readerLen = 256;
readerName = malloc(readerLen);
2011-10-15 19:30:10 +04:00
status = SCardStatus(hCard, (LPSTR) readerName, &readerLen, &state, &protocol, pbAtr, &atrLen);
2011-10-15 19:30:10 +04:00
#endif
if (status != SCARD_S_SUCCESS)
2011-10-15 19:30:10 +04:00
{
DEBUG_SCARD("Failure: %s (0x%08x)", pcsc_stringify_error(status), (unsigned) status);
status = smartcard_output_return(irp, status);
goto finish;
2011-10-15 19:30:10 +04:00
}
DEBUG_SCARD("Success (hcard: 0x%08x len: %d state: 0x%08x, proto: 0x%08x)",
(unsigned) hCard, (int) atrLen, (unsigned) state, (unsigned) protocol);
#ifdef WITH_DEBUG_SCARD
fprintf(stderr, " ATR: ");
2011-10-15 19:30:10 +04:00
for (i = 0; i < atrLen; i++)
fprintf(stderr, "%02x%c", pbAtr[i], (i == atrLen - 1) ? ' ' : ':');
fprintf(stderr, "\n");
2011-10-15 19:30:10 +04:00
#endif
state = smartcard_map_state(state);
2011-10-15 19:30:10 +04:00
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, state);
Stream_Write_UINT32(irp->output, protocol);
Stream_Write_UINT32(irp->output, atrLen);
Stream_Write_UINT32(irp->output, 0x00000001);
Stream_Write_UINT32(irp->output, atrLen);
Stream_Write(irp->output, pbAtr, atrLen);
2011-10-15 19:30:10 +04:00
smartcard_output_repos(irp, atrLen);
smartcard_output_alignment(irp, 8);
2011-10-15 19:30:10 +04:00
finish:
if (readerName)
{
2011-10-15 19:30:10 +04:00
#ifdef SCARD_AUTOALLOCATE
SCardFreeMemory(hContext, readerName);
2011-10-15 19:30:10 +04:00
#else
free(readerName);
2011-10-15 19:30:10 +04:00
#endif
}
2011-10-15 19:30:10 +04:00
return status;
2011-10-15 19:30:10 +04:00
}
static DWORD handle_Status(SMARTCARD_DEVICE *scard, IRP* irp, BOOL wide)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
LONG status;
2011-10-15 19:30:10 +04:00
SCARDHANDLE hCard;
SCARDCONTEXT hContext;
2011-10-15 19:30:10 +04:00
DWORD state, protocol;
DWORD readerLen = 0;
2013-09-13 11:17:06 +04:00
DWORD atrLen = MAX_ATR_SIZE;
2013-09-13 11:47:18 +04:00
char* readerName = NULL;
BYTE *pbAtr = NULL;
UINT32 dataLength = 0;
2011-10-15 19:30:10 +04:00
int pos, poslen1, poslen2;
#ifdef WITH_DEBUG_SCARD
int i;
#endif
status = handle_CommonTypeHeader(scard, irp);
if (status)
goto finish;
status = handle_PrivateTypeHeader(scard, irp);
if (status)
goto finish;
status = handle_CardHandle(scard, irp, &redirect);
if (status)
goto finish;
if (Stream_GetRemainingLength(irp->input) < 12)
{
DEBUG_WARN("length violation %d [%d]", 12,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
Stream_Seek(irp->input, 4);
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, readerLen);
Stream_Read_UINT32(irp->input, atrLen);
status = handle_RedirHandleRef(scard, irp, redirect, &hContext, &hCard);
if (status)
goto finish;
2011-10-15 19:30:10 +04:00
if (!check_handle_is_forwarded(scard, hCard, hContext))
{
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
status = SCARD_E_INVALID_TARGET;
goto finish;
}
2013-09-13 11:47:18 +04:00
pbAtr = malloc(sizeof(BYTE) * atrLen);
2011-10-15 19:30:10 +04:00
#ifdef SCARD_AUTOALLOCATE
readerLen = SCARD_AUTOALLOCATE;
status = SCardStatus(hCard, (LPSTR) &readerName, &readerLen, &state, &protocol, pbAtr, &atrLen);
2011-10-15 19:30:10 +04:00
#else
readerLen = 256;
readerName = malloc(readerLen);
2011-10-15 19:30:10 +04:00
status = SCardStatus(hCard, (LPSTR) readerName, &readerLen, &state, &protocol, pbAtr, &atrLen);
2011-10-15 19:30:10 +04:00
#endif
if (status != SCARD_S_SUCCESS)
2011-10-15 19:30:10 +04:00
{
DEBUG_SCARD("Failure: %s (0x%08x)", pcsc_stringify_error(status), (unsigned) status);
status = smartcard_output_return(irp, status);
goto finish;
2011-10-15 19:30:10 +04:00
}
DEBUG_SCARD("Success (state: 0x%08x, proto: 0x%08x)", (unsigned) state, (unsigned) protocol);
DEBUG_SCARD(" Reader: \"%s\"", readerName ? readerName : "NULL");
#ifdef WITH_DEBUG_SCARD
2013-09-13 11:17:06 +04:00
fprintf(stderr, " ATR: ");
2011-10-15 19:30:10 +04:00
for (i = 0; i < atrLen; i++)
fprintf(stderr, "%02x%c", pbAtr[i], (i == atrLen - 1) ? ' ' : ':');
fprintf(stderr, "\n");
2011-10-15 19:30:10 +04:00
#endif
state = smartcard_map_state(state);
2011-10-15 19:30:10 +04:00
poslen1 = Stream_GetPosition(irp->output);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, readerLen);
Stream_Write_UINT32(irp->output, 0x00020000);
Stream_Write_UINT32(irp->output, state);
Stream_Write_UINT32(irp->output, protocol);
Stream_Write(irp->output, pbAtr, atrLen);
2011-10-15 19:30:10 +04:00
if (atrLen < 32)
2013-05-09 00:27:21 +04:00
Stream_Zero(irp->output, 32 - atrLen);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, atrLen);
2011-10-15 19:30:10 +04:00
poslen2 = Stream_GetPosition(irp->output);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, readerLen);
2011-10-15 19:30:10 +04:00
2013-09-13 11:47:18 +04:00
if (readerName)
dataLength += smartcard_output_string(irp, readerName, wide);
dataLength += smartcard_output_string(irp, "\0", wide);
smartcard_output_repos(irp, dataLength);
2011-10-15 19:30:10 +04:00
pos = Stream_GetPosition(irp->output);
Stream_SetPosition(irp->output, poslen1);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output,dataLength);
Stream_SetPosition(irp->output, poslen2);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output,dataLength);
Stream_SetPosition(irp->output, pos);
2011-10-15 19:30:10 +04:00
smartcard_output_alignment(irp, 8);
2011-10-15 19:30:10 +04:00
finish:
if (readerName)
{
2011-10-15 19:30:10 +04:00
#ifdef SCARD_AUTOALLOCATE
SCardFreeMemory(hContext, readerName);
2011-10-15 19:30:10 +04:00
#else
free(readerName);
2011-10-15 19:30:10 +04:00
#endif
}
2011-10-15 19:30:10 +04:00
2013-09-13 11:47:18 +04:00
if (pbAtr)
free(pbAtr);
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_Transmit(SMARTCARD_DEVICE* scard, IRP* irp)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
LONG status;
SCARDHANDLE hCard;
SCARDCONTEXT hContext;
UINT32 pioSendPciBufferPtr;
UINT32 ptrSendBuffer;
UINT32 ptrIoRecvPciBuffer;
UINT32 recvBufferIsNULL;
UINT32 linkedLen;
2013-09-13 11:47:18 +04:00
void *tmp;
union
{
SCARD_IO_REQUEST *rq;
UINT32 *u32;
void *v;
} ioSendPci, ioRecvPci;
2013-09-13 11:47:18 +04:00
SCARD_IO_REQUEST *pPioRecvPci = NULL;
2011-10-15 19:30:10 +04:00
DWORD cbSendLength = 0, cbRecvLength = 0;
BYTE *sendBuf = NULL, *recvBuf = NULL;
2011-10-15 19:30:10 +04:00
2013-09-13 11:47:18 +04:00
ioSendPci.v = NULL;
ioRecvPci.v = NULL;
status = handle_CommonTypeHeader(scard, irp);
if (status)
goto finish;
status = handle_PrivateTypeHeader(scard, irp);
if (status)
goto finish;
status = handle_CardHandle(scard, irp, &redirect);
if (status)
goto finish;
if (Stream_GetRemainingLength(irp->input) < 32)
{
DEBUG_WARN("length violation %d [%d]", 32,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
2011-10-15 19:30:10 +04:00
ioSendPci.v = malloc(sizeof(SCARD_IO_REQUEST));
ioRecvPci.v = malloc(sizeof(SCARD_IO_REQUEST));
Stream_Read_UINT32(irp->input, ioSendPci.rq->dwProtocol);
Stream_Read_UINT32(irp->input, ioSendPci.rq->cbPciLength);
Stream_Read_UINT32(irp->input, pioSendPciBufferPtr);
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, cbSendLength);
Stream_Read_UINT32(irp->input, ptrSendBuffer);
Stream_Read_UINT32(irp->input, ptrIoRecvPciBuffer);
Stream_Read_UINT32(irp->input, recvBufferIsNULL);
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, cbRecvLength);
2011-10-15 19:30:10 +04:00
status = handle_RedirHandleRef(scard, irp, redirect, &hContext, &hCard);
if (status)
goto finish;
2013-09-23 11:11:23 +04:00
/* Check, if there is data available from the ipSendPci element */
if (pioSendPciBufferPtr)
2011-10-15 19:30:10 +04:00
{
if (Stream_GetRemainingLength(irp->input) < 8)
{
DEBUG_WARN("length violation %d [%d]", 8,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, linkedLen);
if (Stream_GetRemainingLength(irp->input) < ioSendPci.rq->cbPciLength)
{
DEBUG_WARN("length violation %d [%d]", ioSendPci.rq->cbPciLength,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
/* For details see 2.2.1.8 SCardIO_Request in MS-RDPESC and
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa379807%28v=vs.85%29.aspx
*/
if (linkedLen < ioSendPci.rq->cbPciLength - sizeof(SCARD_IO_REQUEST))
{
DEBUG_WARN("SCARD_IO_REQUEST with invalid extra byte length %d [%d]",
ioSendPci.rq->cbPciLength - sizeof(SCARD_IO_REQUEST), linkedLen);
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
2013-09-13 11:47:18 +04:00
tmp = realloc(ioSendPci.v, ioSendPci.rq->cbPciLength);
if (!tmp)
goto finish;
ioSendPci.v = tmp;
Stream_Read(irp->input, &ioSendPci.rq[1], ioSendPci.rq->cbPciLength);
2011-10-15 19:30:10 +04:00
}
else
ioSendPci.rq->cbPciLength = sizeof(SCARD_IO_REQUEST);
2011-10-15 19:30:10 +04:00
/* Check, if there is data available from the SendBufferPointer */
if (ptrSendBuffer)
2011-10-15 19:30:10 +04:00
{
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, linkedLen);
2011-10-15 19:30:10 +04:00
/* Just check for too few bytes, there may be more actual
* data than is used due to padding. */
if (linkedLen < cbSendLength)
{
DEBUG_WARN("SendBuffer invalid byte length %d [%d]",
cbSendLength, linkedLen);
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
if (Stream_GetRemainingLength(irp->input) < cbSendLength)
{
DEBUG_WARN("length violation %d [%d]", cbSendLength,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
sendBuf = malloc(cbSendLength);
Stream_Read(irp->input, sendBuf, cbSendLength);
2011-10-15 19:30:10 +04:00
}
/* Check, if a response is desired. */
if (cbRecvLength && !recvBufferIsNULL)
recvBuf = malloc(cbRecvLength);
else
cbRecvLength = 0;
2011-10-15 19:30:10 +04:00
if (ptrIoRecvPciBuffer)
2011-10-15 19:30:10 +04:00
{
if (Stream_GetRemainingLength(irp->input) < 8)
{
DEBUG_WARN("length violation %d [%d]", 8,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
2011-10-15 19:30:10 +04:00
/* recvPci */
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, linkedLen);
Stream_Read_UINT16(irp->input, ioRecvPci.rq->dwProtocol);
Stream_Read_UINT16(irp->input, ioRecvPci.rq->cbPciLength);
2013-09-23 11:11:23 +04:00
/* Just check for too few bytes, there may be more actual
* data than is used due to padding. */
if (linkedLen < ioSendPci.rq->cbPciLength)
{
DEBUG_WARN("SCARD_IO_REQUEST with invalid extra byte length %d [%d]",
ioSendPci.rq->cbPciLength - sizeof(SCARD_IO_REQUEST), linkedLen);
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
if (Stream_GetRemainingLength(irp->input) < ioRecvPci.rq->cbPciLength)
{
DEBUG_WARN("length violation %d [%d]", ioRecvPci.rq->cbPciLength,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
/* Read data, see
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa379807%28v=vs.85%29.aspx
*/
2013-09-13 11:47:18 +04:00
tmp = realloc(ioRecvPci.v, ioRecvPci.rq->cbPciLength);
if (!tmp)
goto finish;
ioRecvPci.v = tmp;
2013-09-23 11:11:23 +04:00
Stream_Read(irp->input, &ioRecvPci.rq[1], ioRecvPci.rq->cbPciLength);
pPioRecvPci = ioRecvPci.rq;
2011-10-15 19:30:10 +04:00
}
else
pPioRecvPci = NULL;
DEBUG_SCARD("SCardTransmit(hcard: 0x%08lx, send: %d bytes, recv: %d bytes)",
(long unsigned) hCard, (int) cbSendLength, (int) cbRecvLength);
if (!check_handle_is_forwarded(scard, hCard, hContext))
{
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
status = SCARD_E_INVALID_TARGET;
goto finish;
}
status = SCardTransmit(hCard, ioSendPci.rq, sendBuf, cbSendLength,
2011-10-15 19:30:10 +04:00
pPioRecvPci, recvBuf, &cbRecvLength);
if (status != SCARD_S_SUCCESS)
2011-10-15 19:30:10 +04:00
{
DEBUG_SCARD("Failure: %s (0x%08x)", pcsc_stringify_error(status), (unsigned) status);
2011-10-15 19:30:10 +04:00
}
else
{
DEBUG_SCARD("Success (%d bytes)", (int) cbRecvLength);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, 0); /* pioRecvPci 0x00; */
2011-10-15 19:30:10 +04:00
2013-08-30 18:30:27 +04:00
if (recvBuf)
{
smartcard_output_buffer_start(irp, cbRecvLength); /* start of recvBuf output */
smartcard_output_buffer(irp, (char*) recvBuf, cbRecvLength);
}
2011-10-15 19:30:10 +04:00
}
smartcard_output_alignment(irp, 8);
2011-10-15 19:30:10 +04:00
finish:
2013-08-30 18:30:27 +04:00
if (sendBuf)
free(sendBuf);
if (recvBuf)
free(recvBuf);
if (ioSendPci.v)
free(ioSendPci.v);
if (ioRecvPci.v)
free(ioRecvPci.v);
2011-10-15 19:30:10 +04:00
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_Control(SMARTCARD_DEVICE* scard, IRP* irp)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
LONG status;
2011-10-15 19:30:10 +04:00
SCARDCONTEXT hContext;
SCARDHANDLE hCard;
UINT32 pvInBuffer, fpvOutBufferIsNULL;
2012-10-09 11:26:39 +04:00
UINT32 controlCode;
UINT32 controlFunction;
BYTE* recvBuffer = NULL;
BYTE* sendBuffer = NULL;
2012-10-09 11:26:39 +04:00
UINT32 recvLength;
2011-10-15 19:30:10 +04:00
DWORD nBytesReturned;
DWORD outBufferSize;
status = handle_CommonTypeHeader(scard, irp);
if (status)
goto finish;
status = handle_PrivateTypeHeader(scard, irp);
if (status)
goto finish;
status = handle_CardHandle(scard, irp, &redirect);
if (status)
goto finish;
if (Stream_GetRemainingLength(irp->input) < 20)
{
DEBUG_WARN("length violation %d [%d]", 20,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, controlCode);
Stream_Read_UINT32(irp->input, recvLength);
Stream_Read_UINT32(irp->input, pvInBuffer);
Stream_Read_UINT32(irp->input, fpvOutBufferIsNULL);
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, outBufferSize);
status = handle_RedirHandleRef(scard, irp, redirect, &hContext, &hCard);
if (status)
goto finish;
2011-10-15 19:30:10 +04:00
/* Translate Windows SCARD_CTL_CODE's to corresponding local code */
if (WIN_CTL_DEVICE_TYPE(controlCode) == WIN_FILE_DEVICE_SMARTCARD)
{
controlFunction = WIN_CTL_FUNCTION(controlCode);
controlCode = SCARD_CTL_CODE(controlFunction);
}
DEBUG_SCARD("controlCode: 0x%08x", (unsigned) controlCode);
if (pvInBuffer)
2011-10-15 19:30:10 +04:00
{
/* Get the size of the linked data. */
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, recvLength);
2011-10-15 19:30:10 +04:00
/* Check, if there is actually enough data... */
if (Stream_GetRemainingLength(irp->input) < recvLength)
{
DEBUG_WARN("length violation %d [%d]", recvLength,
Stream_GetRemainingLength(irp->input));
status = SCARD_F_INTERNAL_ERROR;
goto finish;
}
recvBuffer = malloc(recvLength);
2013-05-09 00:09:16 +04:00
Stream_Read(irp->input, recvBuffer, recvLength);
2011-10-15 19:30:10 +04:00
}
nBytesReturned = outBufferSize;
sendBuffer = malloc(outBufferSize);
if (!check_handle_is_forwarded(scard, hCard, hContext))
{
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
status = SCARD_E_INVALID_TARGET;
goto finish;
}
status = SCardControl(hCard, (DWORD) controlCode, recvBuffer, (DWORD) recvLength,
2011-10-15 19:30:10 +04:00
sendBuffer, (DWORD) outBufferSize, &nBytesReturned);
if (status != SCARD_S_SUCCESS)
DEBUG_SCARD("Failure: %s (0x%08x)", pcsc_stringify_error(status), (unsigned) status);
2011-10-15 19:30:10 +04:00
else
DEBUG_SCARD("Success (out: %u bytes)", (unsigned) nBytesReturned);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, (UINT32) nBytesReturned);
Stream_Write_UINT32(irp->output, 0x00000004);
Stream_Write_UINT32(irp->output, nBytesReturned);
2011-10-15 19:30:10 +04:00
if (nBytesReturned > 0)
{
2013-05-09 00:09:16 +04:00
Stream_Write(irp->output, sendBuffer, nBytesReturned);
smartcard_output_repos(irp, nBytesReturned);
2011-10-15 19:30:10 +04:00
}
smartcard_output_alignment(irp, 8);
2011-10-15 19:30:10 +04:00
finish:
if (recvBuffer)
free(recvBuffer);
if (sendBuffer)
free(sendBuffer);
2011-10-15 19:30:10 +04:00
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_GetAttrib(SMARTCARD_DEVICE* scard, IRP* irp)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
LONG status;
2011-10-15 19:30:10 +04:00
SCARDHANDLE hCard;
SCARDCONTEXT hContext;
DWORD dwAttrId = 0;
DWORD dwAttrLen = 0;
2011-10-15 19:30:10 +04:00
DWORD attrLen = 0;
BYTE* pbAttr = NULL;
2011-10-15 19:30:10 +04:00
status = handle_CommonTypeHeader(scard, irp);
if (status)
return status;
status = handle_PrivateTypeHeader(scard, irp);
if (status)
return status;
status = handle_CardHandle(scard, irp, &redirect);
if (status)
return status;
if (Stream_GetRemainingLength(irp->input) < 12)
{
DEBUG_WARN("length violation %d [%d]", 12,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, dwAttrId);
Stream_Seek(irp->input, 0x4);
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, dwAttrLen);
status = handle_RedirHandleRef(scard, irp, redirect, &hContext, &hCard);
if (status)
return status;
2011-10-15 19:30:10 +04:00
DEBUG_SCARD("hcard: 0x%08x, attrib: 0x%08x (%d bytes)",
2011-10-15 19:30:10 +04:00
(unsigned) hCard, (unsigned) dwAttrId, (int) dwAttrLen);
if (!check_handle_is_forwarded(scard, hCard, hContext))
{
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
return SCARD_E_INVALID_TARGET;
}
2011-10-15 19:30:10 +04:00
#ifdef SCARD_AUTOALLOCATE
if (dwAttrLen == 0)
2011-10-15 19:30:10 +04:00
{
attrLen = 0;
}
else
{
attrLen = SCARD_AUTOALLOCATE;
}
#endif
status = SCardGetAttrib(hCard, dwAttrId, attrLen == 0 ? NULL : (BYTE*) &pbAttr, &attrLen);
if (status != SCARD_S_SUCCESS)
{
#ifdef SCARD_AUTOALLOCATE
if (dwAttrLen == 0)
attrLen = 0;
else
attrLen = SCARD_AUTOALLOCATE;
#endif
}
2011-10-15 19:30:10 +04:00
if (dwAttrId == SCARD_ATTR_DEVICE_FRIENDLY_NAME_A && status == SCARD_E_UNSUPPORTED_FEATURE)
2011-10-15 19:30:10 +04:00
{
status = SCardGetAttrib(hCard, SCARD_ATTR_DEVICE_FRIENDLY_NAME_W,
attrLen == 0 ? NULL : (BYTE*) &pbAttr, &attrLen);
if (status != SCARD_S_SUCCESS)
{
#ifdef SCARD_AUTOALLOCATE
if (dwAttrLen == 0)
attrLen = 0;
else
attrLen = SCARD_AUTOALLOCATE;
#endif
}
2011-10-15 19:30:10 +04:00
}
if (dwAttrId == SCARD_ATTR_DEVICE_FRIENDLY_NAME_W && status == SCARD_E_UNSUPPORTED_FEATURE)
2011-10-15 19:30:10 +04:00
{
status = SCardGetAttrib(hCard, SCARD_ATTR_DEVICE_FRIENDLY_NAME_A,
attrLen == 0 ? NULL : (BYTE*) &pbAttr, &attrLen);
if (status != SCARD_S_SUCCESS)
{
#ifdef SCARD_AUTOALLOCATE
if (dwAttrLen == 0)
attrLen = 0;
else
attrLen = SCARD_AUTOALLOCATE;
#endif
}
2011-10-15 19:30:10 +04:00
}
if (attrLen > dwAttrLen && pbAttr != NULL)
2011-10-15 19:30:10 +04:00
{
status = SCARD_E_INSUFFICIENT_BUFFER;
2011-10-15 19:30:10 +04:00
}
dwAttrLen = attrLen;
if (status != SCARD_S_SUCCESS)
2011-10-15 19:30:10 +04:00
{
DEBUG_SCARD("Failure: %s (0x%08x)", pcsc_stringify_error(status), (unsigned int) status);
2011-10-15 19:30:10 +04:00
free(pbAttr);
return smartcard_output_return(irp, status);
2011-10-15 19:30:10 +04:00
}
else
{
DEBUG_SCARD("Success (%d bytes)", (int) dwAttrLen);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, dwAttrLen);
Stream_Write_UINT32(irp->output, 0x00000200);
Stream_Write_UINT32(irp->output, dwAttrLen);
2011-10-15 19:30:10 +04:00
if (!pbAttr)
{
2013-05-09 00:27:21 +04:00
Stream_Zero(irp->output, dwAttrLen);
2011-10-15 19:30:10 +04:00
}
else
{
2013-05-09 00:09:16 +04:00
Stream_Write(irp->output, pbAttr, dwAttrLen);
2011-10-15 19:30:10 +04:00
}
smartcard_output_repos(irp, dwAttrLen);
/* align to multiple of 4 */
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, 0);
2011-10-15 19:30:10 +04:00
}
smartcard_output_alignment(irp, 8);
2011-10-15 19:30:10 +04:00
#ifdef SCARD_AUTOALLOCATE
SCardFreeMemory(hContext, pbAttr);
#else
free(pbAttr);
#endif
2011-10-15 19:30:10 +04:00
return status;
2011-10-15 19:30:10 +04:00
}
static UINT32 handle_AccessStartedEvent(SMARTCARD_DEVICE* scard, IRP* irp)
2011-10-15 19:30:10 +04:00
{
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
Stream_Seek(irp->input, 4);
smartcard_output_alignment(irp, 8);
2011-10-15 19:30:10 +04:00
return SCARD_S_SUCCESS;
}
void scard_error(SMARTCARD_DEVICE* scard, IRP* irp, UINT32 ntstatus)
2011-10-15 19:30:10 +04:00
{
/* [MS-RDPESC] 3.1.4.4 */
DEBUG_WARN("scard processing error %x", ntstatus);
2011-10-15 19:30:10 +04:00
Stream_SetPosition(irp->output, 0); /* CHECKME */
2011-10-15 19:30:10 +04:00
irp->IoStatus = ntstatus;
irp->Complete(irp);
}
/* http://msdn.microsoft.com/en-gb/library/ms938473.aspx */
typedef struct _SERVER_SCARD_ATRMASK
{
2012-10-09 11:26:39 +04:00
UINT32 cbAtr;
BYTE rgbAtr[36];
BYTE rgbMask[36];
2011-10-15 19:30:10 +04:00
}
SERVER_SCARD_ATRMASK;
static UINT32 handle_LocateCardsByATR(SMARTCARD_DEVICE* scard, IRP* irp, BOOL wide)
2011-10-15 19:30:10 +04:00
{
int redirect = 0;
LONG status;
int i, j, k;
2011-10-15 19:30:10 +04:00
SCARDCONTEXT hContext;
2012-10-09 11:26:39 +04:00
UINT32 atrMaskCount = 0;
UINT32 readerCount = 0;
SCARD_READERSTATE* cur = NULL;
SCARD_READERSTATE* rsCur = NULL;
SCARD_READERSTATE* readerStates = NULL;
SERVER_SCARD_ATRMASK* curAtr = NULL;
SERVER_SCARD_ATRMASK* pAtrMasks = NULL;
2011-10-15 19:30:10 +04:00
status = handle_CommonTypeHeader(scard, irp);
if (status)
return status;
status = handle_PrivateTypeHeader(scard, irp);
if (status)
return status;
status = handle_Context(scard, irp, &redirect);
if (status)
return status;
if (Stream_GetRemainingLength(irp->input) < 4)
{
DEBUG_WARN("length violation %d [%d]", 4,
Stream_GetRemainingLength(irp->input));
return SCARD_F_INTERNAL_ERROR;
}
Stream_Seek(irp->input, 4);
status = handle_RedirContextRef(scard, irp, redirect, &hContext);
if (status)
return status;
Stream_Seek(irp->input, 0x2C);
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, hContext);
Stream_Read_UINT32(irp->input, atrMaskCount);
2011-10-15 19:30:10 +04:00
pAtrMasks = malloc(atrMaskCount * sizeof(SERVER_SCARD_ATRMASK));
2011-10-15 19:30:10 +04:00
if (!pAtrMasks)
return smartcard_output_return(irp, SCARD_E_NO_MEMORY);
2011-10-15 19:30:10 +04:00
for (i = 0; i < atrMaskCount; i++)
{
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, pAtrMasks[i].cbAtr);
Stream_Read(irp->input, pAtrMasks[i].rgbAtr, 36);
Stream_Read(irp->input, pAtrMasks[i].rgbMask, 36);
2011-10-15 19:30:10 +04:00
}
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, readerCount);
2011-10-15 19:30:10 +04:00
readerStates = malloc(readerCount * sizeof(SCARD_READERSTATE));
ZeroMemory(readerStates, readerCount * sizeof(SCARD_READERSTATE));
2011-10-15 19:30:10 +04:00
if (!readerStates)
{
free(pAtrMasks);
return smartcard_output_return(irp, SCARD_E_NO_MEMORY);
}
2011-10-15 19:30:10 +04:00
for (i = 0; i < readerCount; i++)
{
cur = &readerStates[i];
Stream_Seek(irp->input, 4);
2011-10-15 19:30:10 +04:00
/*
* TODO: on-wire is little endian; need to either
* convert to host endian or fix the headers to
* request the order we want
*/
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, cur->dwCurrentState);
Stream_Read_UINT32(irp->input, cur->dwEventState);
Stream_Read_UINT32(irp->input, cur->cbAtr);
Stream_Read(irp->input, cur->rgbAtr, 32);
2011-10-15 19:30:10 +04:00
Stream_Seek(irp->input, 4);
2011-10-15 19:30:10 +04:00
/* reset high bytes? */
cur->dwCurrentState &= 0x0000FFFF;
cur->dwEventState &= 0x0000FFFF;
cur->dwEventState = 0;
}
for (i = 0; i < readerCount; i++)
{
cur = &readerStates[i];
2012-10-09 11:26:39 +04:00
UINT32 dataLength;
2011-10-15 19:30:10 +04:00
Stream_Seek(irp->input, 8);
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, dataLength);
smartcard_input_repos(irp, smartcard_input_string(irp, (char **) &cur->szReader, dataLength, wide));
2011-10-15 19:30:10 +04:00
DEBUG_SCARD(" \"%s\"", cur->szReader ? cur->szReader : "NULL");
DEBUG_SCARD(" user: 0x%08x, state: 0x%08x, event: 0x%08x",
(unsigned) cur->pvUserData, (unsigned) cur->dwCurrentState,
(unsigned) cur->dwEventState);
2013-08-30 18:30:27 +04:00
if (!cur->szReader)
{
DEBUG_WARN("cur->szReader=%p", cur->szReader);
continue;
}
2011-10-15 19:30:10 +04:00
if (strcmp(cur->szReader, "\\\\?PnP?\\Notification") == 0)
cur->dwCurrentState |= SCARD_STATE_IGNORE;
}
status = SCardGetStatusChange(hContext, 0x00000001, readerStates, readerCount);
if (status != SCARD_S_SUCCESS)
2011-10-15 19:30:10 +04:00
{
DEBUG_SCARD("Failure: %s (0x%08x)",
pcsc_stringify_error(status), (unsigned) status);
2011-10-15 19:30:10 +04:00
free(readerStates);
free(pAtrMasks);
return smartcard_output_return(irp, status);
2011-10-15 19:30:10 +04:00
}
DEBUG_SCARD("Success");
for (i = 0, curAtr = pAtrMasks; i < atrMaskCount; i++, curAtr++)
{
for (j = 0, rsCur = readerStates; j < readerCount; j++, rsCur++)
{
BOOL equal = 1;
2011-10-15 19:30:10 +04:00
for (k = 0; k < cur->cbAtr; k++)
{
if ((curAtr->rgbAtr[k] & curAtr->rgbMask[k]) !=
(rsCur->rgbAtr[k] & curAtr->rgbMask[k]))
{
equal = 0;
break;
}
}
if (equal)
{
rsCur->dwEventState |= 0x00000040; /* SCARD_STATE_ATRMATCH 0x00000040 */
}
}
}
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, readerCount);
Stream_Write_UINT32(irp->output, 0x00084dd8);
Stream_Write_UINT32(irp->output, readerCount);
2011-10-15 19:30:10 +04:00
for (i = 0, cur = readerStates; i < readerCount; i++, cur++)
2011-10-15 19:30:10 +04:00
{
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, cur->dwCurrentState);
Stream_Write_UINT32(irp->output, cur->dwEventState);
Stream_Write_UINT32(irp->output, cur->cbAtr);
Stream_Write(irp->output, cur->rgbAtr, 32);
2011-10-15 19:30:10 +04:00
2013-05-09 00:27:21 +04:00
Stream_Zero(irp->output, 4);
2011-10-15 19:30:10 +04:00
free((void*) cur->szReader);
2011-10-15 19:30:10 +04:00
}
smartcard_output_alignment(irp, 8);
2011-10-15 19:30:10 +04:00
free(readerStates);
free(pAtrMasks);
2011-10-15 19:30:10 +04:00
return status;
2011-10-15 19:30:10 +04:00
}
BOOL smartcard_async_op(IRP* irp)
2011-10-15 19:30:10 +04:00
{
2012-10-09 11:26:39 +04:00
UINT32 ioctl_code;
2011-10-15 19:30:10 +04:00
/* peek ahead */
Stream_Seek(irp->input, 8);
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, ioctl_code);
Stream_Rewind(irp->input, 12);
2011-10-15 19:30:10 +04:00
switch (ioctl_code)
{
/* non-blocking events */
case SCARD_IOCTL_ACCESS_STARTED_EVENT:
case SCARD_IOCTL_ESTABLISH_CONTEXT:
case SCARD_IOCTL_RELEASE_CONTEXT:
case SCARD_IOCTL_IS_VALID_CONTEXT:
return FALSE;
2011-10-15 19:30:10 +04:00
break;
/* async events */
case SCARD_IOCTL_GET_STATUS_CHANGE:
case SCARD_IOCTL_GET_STATUS_CHANGE + 4:
case SCARD_IOCTL_TRANSMIT:
case SCARD_IOCTL_STATUS:
case SCARD_IOCTL_STATUS + 4:
return TRUE;
2011-10-15 19:30:10 +04:00
break;
default:
break;
}
/* default to async */
return TRUE;
2011-10-15 19:30:10 +04:00
}
void smartcard_device_control(SMARTCARD_DEVICE* scard, IRP* irp)
2011-10-15 19:30:10 +04:00
{
2012-10-09 11:26:39 +04:00
UINT32 pos;
UINT32 result;
UINT32 result_pos;
UINT32 output_len;
UINT32 input_len;
UINT32 ioctl_code;
UINT32 stream_len;
UINT32 irp_result_pos;
UINT32 output_len_pos;
const UINT32 header_lengths = 16;
/* MS-RPCE, Sections 2.2.6.1 and 2.2.6.2. */
if (Stream_GetRemainingLength(irp->input) < 32)
{
DEBUG_WARN("Invalid IRP of length %d received, ignoring.",
Stream_GetRemainingLength(irp->input));
return;
}
2011-10-15 19:30:10 +04:00
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, output_len);
Stream_Read_UINT32(irp->input, input_len);
Stream_Read_UINT32(irp->input, ioctl_code);
2011-10-15 19:30:10 +04:00
Stream_Seek(irp->input, 20); /* padding */
2011-10-15 19:30:10 +04:00
// Stream_Seek(irp->input, 4); /* TODO: parse len, le, v1 */
// Stream_Seek(irp->input, 4); /* 0xcccccccc */
// Stream_Seek(irp->input, 4); /* rpce len */
2011-10-15 19:30:10 +04:00
/* [MS-RDPESC] 3.2.5.1 Sending Outgoing Messages */
2013-05-02 02:15:55 +04:00
Stream_EnsureRemainingCapacity(irp->output, 2048);
2011-10-15 19:30:10 +04:00
irp_result_pos = Stream_GetPosition(irp->output);
2011-10-15 19:30:10 +04:00
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, 0x00000000); /* MS-RDPEFS
* OutputBufferLength
* will be updated
* later in this
* function.
*/
2011-10-15 19:30:10 +04:00
/* [MS-RPCE] 2.2.6.1 */
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, 0x00081001); /* len 8, LE, v1 */
Stream_Write_UINT32(irp->output, 0xcccccccc); /* filler */
2011-10-15 19:30:10 +04:00
output_len_pos = Stream_GetPosition(irp->output);
Stream_Seek(irp->output, 4); /* size */
2011-10-15 19:30:10 +04:00
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, 0x0); /* filler */
2011-10-15 19:30:10 +04:00
result_pos = Stream_GetPosition(irp->output);
Stream_Seek(irp->output, 4); /* result */
2011-10-15 19:30:10 +04:00
/* Ensure, that this package is fully available. */
if (Stream_GetRemainingLength(irp->input) < input_len)
{
DEBUG_WARN("Invalid IRP of length %d received, expected %d, ignoring.",
Stream_GetRemainingLength(irp->input), input_len);
return;
}
/* body. input_len contains the length of the remaining data
* that can be read from the current position of irp->input,
* so pass it on ;) */
DEBUG_SCARD("ioctl '%s' [%08X], length=%d [%d]",
ioctl_to_string(ioctl_code), ioctl_code, input_len,
Stream_GetRemainingLength(irp->input));
2011-10-15 19:30:10 +04:00
switch (ioctl_code)
{
case SCARD_IOCTL_ESTABLISH_CONTEXT:
result = handle_EstablishContext(scard, irp);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_IS_VALID_CONTEXT:
result = handle_IsValidContext(scard, irp);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_RELEASE_CONTEXT:
result = handle_ReleaseContext(scard, irp);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_LIST_READERS:
result = handle_ListReaders(scard, irp, 0);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_LIST_READERS + 4:
result = handle_ListReaders(scard, irp, 1);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_LIST_READER_GROUPS:
case SCARD_IOCTL_LIST_READER_GROUPS + 4:
/* typically not used unless list_readers fail */
2011-10-15 19:30:10 +04:00
result = SCARD_F_INTERNAL_ERROR;
break;
case SCARD_IOCTL_GET_STATUS_CHANGE:
result = handle_GetStatusChange(scard, irp, 0);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_GET_STATUS_CHANGE + 4:
result = handle_GetStatusChange(scard, irp, 1);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_CANCEL:
result = handle_Cancel(scard, irp);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_CONNECT:
result = handle_Connect(scard, irp, 0);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_CONNECT + 4:
result = handle_Connect(scard, irp, 1);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_RECONNECT:
result = handle_Reconnect(scard, irp);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_DISCONNECT:
result = handle_Disconnect(scard, irp);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_BEGIN_TRANSACTION:
result = handle_BeginTransaction(scard, irp);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_END_TRANSACTION:
result = handle_EndTransaction(scard, irp);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_STATE:
result = handle_State(scard, irp);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_STATUS:
result = handle_Status(scard, irp, 0);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_STATUS + 4:
result = handle_Status(scard, irp, 1);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_TRANSMIT:
result = handle_Transmit(scard, irp);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_CONTROL:
result = handle_Control(scard, irp);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_GETATTRIB:
result = handle_GetAttrib(scard, irp);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_ACCESS_STARTED_EVENT:
result = handle_AccessStartedEvent(scard, irp);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_LOCATE_CARDS_BY_ATR:
result = handle_LocateCardsByATR(scard, irp, 0);
2011-10-15 19:30:10 +04:00
break;
case SCARD_IOCTL_LOCATE_CARDS_BY_ATR + 4:
result = handle_LocateCardsByATR(scard, irp, 1);
2011-10-15 19:30:10 +04:00
break;
default:
result = 0xc0000001;
DEBUG_WARN("scard unknown ioctl 0x%x [%d]\n",
ioctl_code, input_len);
2011-10-15 19:30:10 +04:00
break;
}
/* look for NTSTATUS errors */
if ((result & 0xc0000000) == 0xc0000000)
return scard_error(scard, irp, result);
/* per Ludovic Rousseau, map different usage of this particular
* error code between pcsc-lite & windows */
if (result == 0x8010001F)
result = 0x80100022;
/* handle response packet */
pos = Stream_GetPosition(irp->output);
stream_len = pos - irp_result_pos - 4; /* Value of OutputBufferLength */
Stream_SetPosition(irp->output, irp_result_pos);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, stream_len);
2011-10-15 19:30:10 +04:00
Stream_SetPosition(irp->output, output_len_pos);
/* Remove the effect of the MS-RPCE Common Type Header and Private
* Header (Sections 2.2.6.1 and 2.2.6.2).
*/
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, stream_len - header_lengths);
2011-10-15 19:30:10 +04:00
Stream_SetPosition(irp->output, result_pos);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, result);
2011-10-15 19:30:10 +04:00
Stream_SetPosition(irp->output, pos);
2011-10-15 19:30:10 +04:00
irp->IoStatus = 0;
irp->Complete(irp);
}