2012-08-15 01:09:01 +04:00
|
|
|
/**
|
2012-10-09 05:00:07 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2012-08-15 01:09:01 +04:00
|
|
|
* 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"
|
2012-08-15 01:09:01 +04:00
|
|
|
#endif
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-02-11 07:12:13 +04:00
|
|
|
#include <assert.h>
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-20 08:49:08 +04:00
|
|
|
#include <winpr/crt.h>
|
2012-12-14 08:54:54 +04:00
|
|
|
#include <winpr/print.h>
|
2013-03-22 00:45:25 +04:00
|
|
|
#include <winpr/stream.h>
|
2014-04-03 23:27:55 +04:00
|
|
|
#include <winpr/smartcard.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>
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
#include "smartcard_main.h"
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
const char* smartcard_get_ioctl_string(UINT32 ioControlCode)
|
|
|
|
{
|
|
|
|
switch (ioControlCode)
|
|
|
|
{
|
|
|
|
case SCARD_IOCTL_ESTABLISHCONTEXT:
|
|
|
|
return "SCARD_IOCTL_ESTABLISHCONTEXT";
|
|
|
|
case SCARD_IOCTL_RELEASECONTEXT:
|
|
|
|
return "SCARD_IOCTL_RELEASECONTEXT";
|
|
|
|
case SCARD_IOCTL_ISVALIDCONTEXT:
|
|
|
|
return "SCARD_IOCTL_ISVALIDCONTEXT";
|
|
|
|
case SCARD_IOCTL_LISTREADERGROUPSA:
|
|
|
|
return "SCARD_IOCTL_LISTREADERGROUPSA";
|
|
|
|
case SCARD_IOCTL_LISTREADERGROUPSW:
|
|
|
|
return "SCARD_IOCTL_LISTREADERGROUPSW";
|
|
|
|
case SCARD_IOCTL_LISTREADERSA:
|
|
|
|
return "SCARD_IOCTL_LISTREADERSA";
|
|
|
|
case SCARD_IOCTL_LISTREADERSW:
|
|
|
|
return "SCARD_IOCTL_LISTREADERSW";
|
|
|
|
case SCARD_IOCTL_INTRODUCEREADERGROUPA:
|
|
|
|
return "SCARD_IOCTL_INTRODUCEREADERGROUPA";
|
|
|
|
case SCARD_IOCTL_INTRODUCEREADERGROUPW:
|
|
|
|
return "SCARD_IOCTL_INTRODUCEREADERGROUPW";
|
|
|
|
case SCARD_IOCTL_FORGETREADERGROUPA:
|
|
|
|
return "SCARD_IOCTL_FORGETREADERGROUPA";
|
|
|
|
case SCARD_IOCTL_FORGETREADERGROUPW:
|
|
|
|
return "SCARD_IOCTL_FORGETREADERGROUPW";
|
|
|
|
case SCARD_IOCTL_INTRODUCEREADERA:
|
|
|
|
return "SCARD_IOCTL_INTRODUCEREADERA";
|
|
|
|
case SCARD_IOCTL_INTRODUCEREADERW:
|
|
|
|
return "SCARD_IOCTL_INTRODUCEREADERW";
|
|
|
|
case SCARD_IOCTL_FORGETREADERA:
|
|
|
|
return "SCARD_IOCTL_FORGETREADERA";
|
|
|
|
case SCARD_IOCTL_FORGETREADERW:
|
|
|
|
return "SCARD_IOCTL_FORGETREADERW";
|
|
|
|
case SCARD_IOCTL_ADDREADERTOGROUPA:
|
|
|
|
return "SCARD_IOCTL_ADDREADERTOGROUPA";
|
|
|
|
case SCARD_IOCTL_ADDREADERTOGROUPW:
|
|
|
|
return "SCARD_IOCTL_ADDREADERTOGROUPW";
|
|
|
|
case SCARD_IOCTL_REMOVEREADERFROMGROUPA:
|
|
|
|
return "SCARD_IOCTL_REMOVEREADERFROMGROUPA";
|
|
|
|
case SCARD_IOCTL_REMOVEREADERFROMGROUPW:
|
|
|
|
return "SCARD_IOCTL_REMOVEREADERFROMGROUPW";
|
|
|
|
case SCARD_IOCTL_LOCATECARDSA:
|
|
|
|
return "SCARD_IOCTL_LOCATECARDSA";
|
|
|
|
case SCARD_IOCTL_LOCATECARDSW:
|
|
|
|
return "SCARD_IOCTL_LOCATECARDSW";
|
|
|
|
case SCARD_IOCTL_GETSTATUSCHANGEA:
|
|
|
|
return "SCARD_IOCTL_GETSTATUSCHANGEA";
|
|
|
|
case SCARD_IOCTL_GETSTATUSCHANGEW:
|
|
|
|
return "SCARD_IOCTL_GETSTATUSCHANGEW";
|
|
|
|
case SCARD_IOCTL_CANCEL:
|
|
|
|
return "SCARD_IOCTL_CANCEL";
|
|
|
|
case SCARD_IOCTL_CONNECTA:
|
|
|
|
return "SCARD_IOCTL_CONNECTA";
|
|
|
|
case SCARD_IOCTL_CONNECTW:
|
|
|
|
return "SCARD_IOCTL_CONNECTW";
|
|
|
|
case SCARD_IOCTL_RECONNECT:
|
|
|
|
return "SCARD_IOCTL_RECONNECT";
|
|
|
|
case SCARD_IOCTL_DISCONNECT:
|
|
|
|
return "SCARD_IOCTL_DISCONNECT";
|
|
|
|
case SCARD_IOCTL_BEGINTRANSACTION:
|
|
|
|
return "SCARD_IOCTL_BEGINTRANSACTION";
|
|
|
|
case SCARD_IOCTL_ENDTRANSACTION:
|
|
|
|
return "SCARD_IOCTL_ENDTRANSACTION";
|
|
|
|
case SCARD_IOCTL_STATE:
|
|
|
|
return "SCARD_IOCTL_STATE";
|
|
|
|
case SCARD_IOCTL_STATUSA:
|
|
|
|
return "SCARD_IOCTL_STATUSA";
|
|
|
|
case SCARD_IOCTL_STATUSW:
|
|
|
|
return "SCARD_IOCTL_STATUSW";
|
|
|
|
case SCARD_IOCTL_TRANSMIT:
|
|
|
|
return "SCARD_IOCTL_TRANSMIT";
|
|
|
|
case SCARD_IOCTL_CONTROL:
|
|
|
|
return "SCARD_IOCTL_CONTROL";
|
|
|
|
case SCARD_IOCTL_GETATTRIB:
|
|
|
|
return "SCARD_IOCTL_GETATTRIB";
|
|
|
|
case SCARD_IOCTL_SETATTRIB:
|
|
|
|
return "SCARD_IOCTL_SETATTRIB";
|
|
|
|
case SCARD_IOCTL_ACCESSSTARTEDEVENT:
|
|
|
|
return "SCARD_IOCTL_ACCESSSTARTEDEVENT";
|
|
|
|
case SCARD_IOCTL_LOCATECARDSBYATRA:
|
|
|
|
return "SCARD_IOCTL_LOCATECARDSBYATRA";
|
|
|
|
case SCARD_IOCTL_LOCATECARDSBYATRW:
|
|
|
|
return "SCARD_IOCTL_LOCATECARDSBYATRW";
|
|
|
|
case SCARD_IOCTL_READCACHEA:
|
|
|
|
return "SCARD_IOCTL_READCACHEA";
|
|
|
|
case SCARD_IOCTL_READCACHEW:
|
|
|
|
return "SCARD_IOCTL_READCACHEW";
|
|
|
|
case SCARD_IOCTL_WRITECACHEA:
|
|
|
|
return "SCARD_IOCTL_WRITECACHEA";
|
|
|
|
case SCARD_IOCTL_WRITECACHEW:
|
|
|
|
return "SCARD_IOCTL_WRITECACHEW";
|
|
|
|
case SCARD_IOCTL_GETTRANSMITCOUNT:
|
|
|
|
return "SCARD_IOCTL_GETTRANSMITCOUNT";
|
|
|
|
case SCARD_IOCTL_RELEASETARTEDEVENT:
|
|
|
|
return "SCARD_IOCTL_RELEASETARTEDEVENT";
|
|
|
|
case SCARD_IOCTL_GETREADERICON:
|
|
|
|
return "SCARD_IOCTL_GETREADERICON";
|
|
|
|
case SCARD_IOCTL_GETDEVICETYPEID:
|
|
|
|
return "SCARD_IOCTL_GETDEVICETYPEID";
|
|
|
|
default:
|
|
|
|
return "SCARD_IOCTL_UNKNOWN";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "SCARD_IOCTL_UNKNOWN";
|
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_CommonTypeHeader(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
UINT8 version;
|
2014-04-04 08:56:24 +04:00
|
|
|
UINT32 filler;
|
|
|
|
UINT8 endianness;
|
|
|
|
UINT16 commonHeaderLength;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < 8)
|
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "CommonTypeHeader is too short: %d",
|
|
|
|
(int) Stream_GetRemainingLength(irp->input));
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process CommonTypeHeader */
|
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
Stream_Read_UINT8(irp->input, version); /* Version (1 byte) */
|
|
|
|
Stream_Read_UINT8(irp->input, endianness); /* Endianness (1 byte) */
|
|
|
|
Stream_Read_UINT16(irp->input, commonHeaderLength); /* CommonHeaderLength (2 bytes) */
|
|
|
|
Stream_Read_UINT32(irp->input, filler); /* Filler (4 bytes), should be 0xCCCCCCCC */
|
|
|
|
|
|
|
|
if (version != 1)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "Unsupported CommonTypeHeader Version %d", version);
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
2014-04-04 08:56:24 +04:00
|
|
|
|
|
|
|
if (endianness != 0x10)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "Unsupported CommonTypeHeader Endianness %d", endianness);
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
2014-04-04 08:56:24 +04:00
|
|
|
|
|
|
|
if (commonHeaderLength != 8)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "Unsupported CommonTypeHeader CommonHeaderLength %d", commonHeaderLength);
|
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filler != 0xCCCCCCCC)
|
|
|
|
{
|
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "Unexpected CommonTypeHeader Filler 0x%08X", filler);
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_PrivateTypeHeader(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
UINT32 filler;
|
|
|
|
UINT32 objectBufferLength;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < 8)
|
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "PrivateTypeHeader is too short: %d",
|
|
|
|
(int) Stream_GetRemainingLength(irp->input));
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
Stream_Read_UINT32(irp->input, objectBufferLength); /* ObjectBufferLength (4 bytes) */
|
|
|
|
Stream_Read_UINT32(irp->input, filler); /* Filler (4 bytes), should be 0x00000000 */
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
if (objectBufferLength != Stream_GetRemainingLength(irp->input))
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "PrivateTypeHeader ObjectBufferLength mismatch: Actual: %d, Expected: %d",
|
|
|
|
(int) objectBufferLength, Stream_GetRemainingLength(irp->input));
|
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filler != 0x00000000)
|
|
|
|
{
|
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "Unexpected PrivateTypeHeader Filler 0x%08X", filler);
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_Context(SMARTCARD_DEVICE* smartcard, IRP* irp, int *redirect)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
UINT32 length;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < 4)
|
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "Context is too short: %d",
|
|
|
|
(int) Stream_GetRemainingLength(irp->input));
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
Stream_Read_UINT32(irp->input, length); /* Length (4 bytes) */
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < length)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "Context is too short: Actual: %d, Expected: %d",
|
|
|
|
(int) Stream_GetRemainingLength(irp->input), length);
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
2014-04-04 08:56:24 +04:00
|
|
|
|
|
|
|
Stream_Seek(irp->input, length);
|
|
|
|
|
|
|
|
if (!length)
|
2013-09-30 16:49:19 +04:00
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "Context is null, using stored context");
|
2013-09-30 16:49:19 +04:00
|
|
|
*redirect |= 0x01;
|
|
|
|
}
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
if (length > Stream_GetRemainingLength(irp->input))
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "Context is too long: Actual: %d, Expected: %d",
|
|
|
|
(int) Stream_GetRemainingLength(irp->input), length);
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_CardHandle(SMARTCARD_DEVICE* smartcard, IRP* irp, int* redirect)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
UINT32 status;
|
2014-04-04 08:56:24 +04:00
|
|
|
UINT32 length;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_Context(smartcard, irp, redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < 4)
|
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "CardHandle is too short: %d",
|
|
|
|
(int) Stream_GetRemainingLength(irp->input));
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
Stream_Read_UINT32(irp->input, length); /* Length (4 bytes) */
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < length)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "CardHandle is too short: Actual: %d, Expected: %d",
|
|
|
|
(int) Stream_GetRemainingLength(irp->input), length);
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
Stream_Seek(irp->input, length); /* Length (4 bytes) */
|
|
|
|
|
|
|
|
if (!length)
|
2013-09-30 16:49:19 +04:00
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "CardHandle is null, using stored handle");
|
2013-09-30 16:49:19 +04:00
|
|
|
*redirect |= 0x02;
|
|
|
|
}
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_RedirContextRef(SMARTCARD_DEVICE* smartcard, IRP* irp,
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect, SCARDCONTEXT* hContext)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
UINT32 length;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2013-09-30 16:49:19 +04:00
|
|
|
/* No context provided, use stored. */
|
|
|
|
if (redirect & 0x01)
|
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "RedirContextRef no context provided, using stored context\n");
|
2014-04-04 01:29:12 +04:00
|
|
|
*hContext = smartcard->hContext;
|
2013-09-30 16:49:19 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (Stream_GetRemainingLength(irp->input) < 4)
|
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "RedirContextRef is too short: Actual: %d, Expected: %d\n",
|
|
|
|
(int) Stream_GetRemainingLength(irp->input), 4);
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
Stream_Read_UINT32(irp->input, length); /* Length (4 bytes) */
|
|
|
|
|
|
|
|
if ((length != 4) && (length != 8))
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "RedirContextRef length is not 4 or 8: %d\n", length);
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
if (Stream_GetRemainingLength(irp->input) < length)
|
|
|
|
{
|
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "RedirContextRef is too short: Actual: %d, Expected: %d\n",
|
|
|
|
(int) Stream_GetRemainingLength(irp->input), length);
|
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 4)
|
|
|
|
Stream_Read_UINT64(irp->input, *hContext);
|
|
|
|
else
|
|
|
|
Stream_Read_UINT32(irp->input, *hContext);
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_RedirHandleRef(SMARTCARD_DEVICE* smartcard, IRP* irp,
|
|
|
|
int redirect, SCARDCONTEXT* hContext, SCARDHANDLE* hHandle)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 08:56:24 +04:00
|
|
|
UINT32 length;
|
|
|
|
UINT32 status;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirContextRef(smartcard, irp, redirect, hContext);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2013-09-30 16:49:19 +04:00
|
|
|
/* Use stored card handle. */
|
|
|
|
if (redirect & 0x02)
|
|
|
|
{
|
2014-04-04 21:57:28 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "RedirHandleRef no handle provided, using stored handle\n");
|
2014-04-04 01:29:12 +04:00
|
|
|
*hHandle = smartcard->hCard;
|
2013-09-30 16:49:19 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (Stream_GetRemainingLength(irp->input) < 4)
|
|
|
|
{
|
2014-04-04 21:57:28 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "RedirHandleRef is too short: Actual: %d, Expected: %d\n",
|
|
|
|
(int) Stream_GetRemainingLength(irp->input), 4);
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:57:28 +04:00
|
|
|
Stream_Read_UINT32(irp->input, length); /* Length (4 bytes) */
|
|
|
|
|
|
|
|
if ((length != 4) && (length != 8))
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 21:57:28 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "RedirHandleRef length is not 4 or 8: %d\n", length);
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
if (Stream_GetRemainingLength(irp->input) < length)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 21:57:28 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "RedirHandleRef is too short: Actual: %d, Expected: %d\n",
|
|
|
|
(int) Stream_GetRemainingLength(irp->input), length);
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:57:28 +04:00
|
|
|
if (length > 4)
|
|
|
|
Stream_Read_UINT64(irp->input, *hHandle);
|
|
|
|
else
|
|
|
|
Stream_Read_UINT32(irp->input, *hHandle);
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static BOOL check_reader_is_forwarded(SMARTCARD_DEVICE* smartcard, const char* readerName)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
BOOL rc = TRUE;
|
|
|
|
char *name = _strdup(readerName);
|
|
|
|
char *str, *strpos=NULL, *strstatus=NULL;
|
2013-09-30 15:29:40 +04:00
|
|
|
long pos, cpos, ret;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
pos = strtol(strpos, NULL, 10);
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
if (strpos && strstatus)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
/* Check, if the name of the reader matches. */
|
2014-04-04 01:29:12 +04:00
|
|
|
if (smartcard->name && strncmp(smartcard->name, readerName, strlen(smartcard->name)))
|
2013-09-12 14:18:35 +04:00
|
|
|
rc = FALSE;
|
|
|
|
|
|
|
|
/* Check, if the position matches. */
|
2014-04-04 01:29:12 +04:00
|
|
|
if (smartcard->path)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2014-04-04 01:29:12 +04:00
|
|
|
ret = sscanf(smartcard->path, "%ld", &cpos);
|
2013-09-12 14:18:35 +04:00
|
|
|
if ((1 == ret) && (cpos != pos))
|
|
|
|
rc = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-04-04 01:29:12 +04:00
|
|
|
{
|
2013-09-12 14:18:35 +04:00
|
|
|
DEBUG_WARN("unknown reader format '%s'", readerName);
|
2014-04-04 01:29:12 +04:00
|
|
|
}
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2013-09-13 11:47:18 +04:00
|
|
|
finally:
|
2013-09-12 14:18:35 +04:00
|
|
|
free(name);
|
|
|
|
|
|
|
|
if (!rc)
|
|
|
|
DEBUG_WARN("reader '%s' not forwarded", readerName);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static BOOL check_handle_is_forwarded(SMARTCARD_DEVICE* smartcard, SCARDHANDLE hCard, SCARDCONTEXT hContext)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
LONG status;
|
|
|
|
DWORD state = 0, protocol = 0;
|
|
|
|
DWORD readerLen;
|
2014-04-03 23:27:55 +04:00
|
|
|
DWORD atrLen = SCARD_ATR_LENGTH;
|
2013-09-12 14:18:35 +04:00
|
|
|
char* readerName = NULL;
|
2014-04-03 23:27:55 +04:00
|
|
|
BYTE pbAtr[SCARD_ATR_LENGTH];
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
readerLen = SCARD_AUTOALLOCATE;
|
|
|
|
|
2014-04-04 21:57:28 +04:00
|
|
|
status = SCardStatusA(hCard, (LPSTR) &readerName, &readerLen, &state, &protocol, pbAtr, &atrLen);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status == SCARD_S_SUCCESS)
|
|
|
|
{
|
2014-04-04 01:29:12 +04:00
|
|
|
rc = check_reader_is_forwarded(smartcard, readerName);
|
2014-04-04 21:57:28 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (!rc)
|
|
|
|
DEBUG_WARN("Reader '%s' not forwarded!", readerName);
|
|
|
|
}
|
|
|
|
|
|
|
|
SCardFreeMemory(hContext, readerName);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2011-11-23 04:22:17 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
static UINT32 smartcard_output_string(IRP* irp, char* src, BOOL wide)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* p;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 len;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
p = Stream_Pointer(irp->output);
|
2011-10-15 19:30:10 +04:00
|
|
|
len = strlen(src) + 1;
|
|
|
|
|
|
|
|
if (wide)
|
|
|
|
{
|
2014-04-05 00:45:11 +04:00
|
|
|
UINT32 i;
|
2011-11-22 05:46:58 +04:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->output, len);
|
2011-10-15 19:30:10 +04:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
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
|
2012-08-02 02:05:45 +04:00
|
|
|
* RDPDR_HEADER, DeviceID,
|
|
|
|
* CompletionID, and IoStatus
|
|
|
|
* of Section 2.2.1.5.5 of MS-RDPEFS.
|
|
|
|
*/
|
2013-04-30 06:35:15 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +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
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +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);
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +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
|
|
|
{
|
2014-04-05 00:45:11 +04:00
|
|
|
UINT32 header = (length < 0) ? (0) : ((length > highLimit) ? (highLimit) : (length));
|
2011-10-15 19:30:10 +04:00
|
|
|
|
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
|
|
|
|
{
|
|
|
|
if (header < length)
|
|
|
|
length = header;
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write(irp->output, buffer, length);
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_repos(irp, length);
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
static void smartcard_output_buffer(IRP* irp, char* buffer, unsigned int length)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_buffer_limit(irp, buffer, length, 0x7FFFFFFF);
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +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
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
static void smartcard_output_buffer_start(IRP* irp, int length)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_buffer_start_limit(irp, length, 0x7FFFFFFF);
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +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;
|
2012-10-09 07:21:26 +04:00
|
|
|
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)
|
|
|
|
{
|
2014-04-05 00:45:11 +04:00
|
|
|
UINT32 i;
|
|
|
|
|
2011-10-15 19:30:10 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
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)
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->input, add);
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2013-09-12 14:18:35 +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
|
|
|
|
2013-09-12 14:18:35 +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;
|
|
|
|
}
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
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
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (Stream_GetRemainingLength(irp->input) < dataLength)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("length violation %d [%d]", dataLength,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_input_repos(irp, smartcard_input_string(irp, dest, dataLength, wide));
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
return 0;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_EstablishContext(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2012-11-21 20:56:40 +04:00
|
|
|
UINT32 status;
|
2014-04-04 21:57:28 +04:00
|
|
|
EstablishContext_Call call;
|
2011-10-15 19:30:10 +04:00
|
|
|
SCARDCONTEXT hContext = -1;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
2011-11-22 05:46:58 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 21:57:28 +04:00
|
|
|
status = smartcard_unpack_establish_context_call(smartcard, irp->input, &call);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 21:57:28 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 21:57:28 +04:00
|
|
|
status = SCardEstablishContext(call.dwScope, NULL, NULL, &hContext);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
Stream_Write_UINT32(irp->output, 4); /* cbContext (4 bytes) */
|
|
|
|
Stream_Write_UINT32(irp->output, -1); /* ReferentID (4 bytes) */
|
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
|
|
|
|
2013-09-30 15:29:40 +04:00
|
|
|
/* store hContext in allowed context list */
|
2014-04-04 01:29:12 +04:00
|
|
|
smartcard->hContext = hContext;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2014-04-04 01:29:12 +04:00
|
|
|
|
2013-09-13 11:47:18 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_ReleaseContext(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2013-09-12 14:18:35 +04:00
|
|
|
UINT32 status;
|
2011-10-15 19:30:10 +04:00
|
|
|
SCARDCONTEXT hContext = -1;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_Context(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirContextRef(smartcard, irp, redirect, &hContext);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
status = SCardReleaseContext(hContext);
|
2014-04-04 01:29:12 +04:00
|
|
|
ZeroMemory(&smartcard->hContext, sizeof(smartcard->hContext));
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
|
|
|
|
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_IsValidContext(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2012-11-21 20:56:40 +04:00
|
|
|
UINT32 status;
|
2011-10-15 19:30:10 +04:00
|
|
|
SCARDCONTEXT hContext;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_Context(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirContextRef(smartcard, irp, redirect, &hContext);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
status = SCardIsValidContext(hContext);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_ListReaders(SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL wide)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2013-09-12 14:18:35 +04:00
|
|
|
UINT32 status;
|
2011-10-15 19:30:10 +04:00
|
|
|
SCARDCONTEXT hContext;
|
|
|
|
DWORD dwReaders;
|
2014-04-04 21:57:28 +04:00
|
|
|
ListReaders_Call call;
|
2011-10-15 19:30:10 +04:00
|
|
|
char *readerList = NULL, *walker;
|
|
|
|
int elemLength, dataLength;
|
2013-09-12 14:18:35 +04:00
|
|
|
int pos, poslen1, poslen2, allowed_pos;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_Context(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 21:57:28 +04:00
|
|
|
status = smartcard_unpack_list_readers_call(smartcard, irp->input, &call);
|
2014-04-04 08:56:24 +04:00
|
|
|
|
2014-04-04 21:57:28 +04:00
|
|
|
if (status)
|
2013-09-30 15:29:40 +04:00
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
/* Read RedirScardcontextRef */
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirContextRef(smartcard, irp, redirect, &hContext);
|
2014-04-04 08:56:24 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
dwReaders = SCARD_AUTOALLOCATE;
|
2014-04-04 21:57:28 +04:00
|
|
|
status = SCardListReadersA(hContext, (LPCSTR) call.mszGroups, (LPSTR) &readerList, &dwReaders);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
if (status != SCARD_S_SUCCESS)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2013-04-30 06:35:15 +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
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
poslen2 = Stream_GetPosition(irp->output);
|
|
|
|
Stream_Seek_UINT32(irp->output);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
walker = readerList;
|
|
|
|
dataLength = 0;
|
2011-11-22 05:46:58 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
/* Smartcards can be forwarded by position and name. */
|
|
|
|
allowed_pos = -1;
|
2014-04-04 01:29:12 +04:00
|
|
|
if (smartcard->path)
|
|
|
|
{
|
|
|
|
if (1 != sscanf(smartcard->path, "%d", &allowed_pos))
|
2013-09-12 14:18:35 +04:00
|
|
|
allowed_pos = -1;
|
2014-04-04 01:29:12 +04:00
|
|
|
}
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
pos = 0;
|
2011-10-15 19:30:10 +04:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
elemLength = strlen(walker);
|
|
|
|
if (elemLength == 0)
|
|
|
|
break;
|
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
/* Ignore readers not forwarded. */
|
|
|
|
if ((allowed_pos < 0) || (pos == allowed_pos))
|
|
|
|
{
|
2014-04-04 01:29:12 +04:00
|
|
|
if (!smartcard->name || strstr(walker, smartcard->name))
|
2013-09-12 14:18:35 +04:00
|
|
|
dataLength += smartcard_output_string(irp, walker, wide);
|
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
walker += elemLength + 1;
|
2013-09-12 14:18:35 +04:00
|
|
|
pos ++;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
dataLength += smartcard_output_string(irp, "\0", wide);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
pos = Stream_GetPosition(irp->output);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(irp->output, poslen1);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, dataLength);
|
2013-04-30 06:35:15 +04:00
|
|
|
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
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(irp->output, pos);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_repos(irp, dataLength);
|
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 15:53:15 +04:00
|
|
|
finish:
|
|
|
|
if (readerList)
|
|
|
|
{
|
|
|
|
SCardFreeMemory(hContext, readerList);
|
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 21:57:28 +04:00
|
|
|
if (call.mszGroups)
|
|
|
|
free(call.mszGroups);
|
2013-09-30 15:29:40 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_GetStatusChange(SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL wide)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2014-04-05 00:45:11 +04:00
|
|
|
UINT32 i;
|
2012-11-21 20:56:40 +04:00
|
|
|
LONG status;
|
2014-04-04 01:29:12 +04:00
|
|
|
int redirect = 0;
|
2011-10-15 19:30:10 +04:00
|
|
|
SCARDCONTEXT hContext;
|
|
|
|
DWORD dwTimeout = 0;
|
|
|
|
DWORD readerCount = 0;
|
2014-04-05 00:45:11 +04:00
|
|
|
SCARD_READERSTATEA *readerStates = NULL, *cur;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_Context(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
/* Ensure, that the capacity expected is actually available. */
|
2013-09-12 15:53:15 +04:00
|
|
|
if (Stream_GetRemainingLength(irp->input) < 12)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
2013-09-12 15:53:15 +04:00
|
|
|
DEBUG_WARN("length violation %d [%d]", 12,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
2014-04-03 23:27:55 +04:00
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
/* Skip reader state */
|
|
|
|
Stream_Seek(irp->input, 4);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
/* Get context */
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirContextRef(smartcard, irp, redirect, &hContext);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
/* Skip ReaderStateConformant */
|
2013-09-12 15:53:15 +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-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->input, 4);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
if (readerCount > 0)
|
|
|
|
{
|
2012-11-20 08:49:08 +04:00
|
|
|
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];
|
|
|
|
|
2013-09-12 15:53:15 +04:00
|
|
|
if (Stream_GetRemainingLength(irp->input) < 52 )
|
|
|
|
{
|
|
|
|
DEBUG_WARN("length violation %d [%d]", 52,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
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
|
|
|
|
2013-04-30 06:35:15 +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++)
|
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 dataLength;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-05 00:45:11 +04:00
|
|
|
cur = &readerStates[i];
|
|
|
|
|
2013-09-12 15:53:15 +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;
|
|
|
|
}
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->input, 8);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, dataLength);
|
2013-09-12 15:53:15 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:57:28 +04:00
|
|
|
status = SCardGetStatusChangeA(hContext, (DWORD) dwTimeout, readerStates, (DWORD) readerCount);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
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];
|
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 15:53:15 +04:00
|
|
|
finish:
|
|
|
|
if (readerStates)
|
2013-10-01 15:09:52 +04:00
|
|
|
{
|
|
|
|
for (i = 0; i < readerCount; i++)
|
|
|
|
{
|
|
|
|
cur = &readerStates[i];
|
|
|
|
|
|
|
|
if (cur->szReader)
|
2014-04-03 23:27:55 +04:00
|
|
|
free((void*) cur->szReader);
|
2013-10-01 15:09:52 +04:00
|
|
|
cur->szReader = NULL;
|
|
|
|
}
|
2013-09-12 15:53:15 +04:00
|
|
|
free(readerStates);
|
2013-10-01 15:09:52 +04:00
|
|
|
}
|
2013-09-12 15:53:15 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_Cancel(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2012-11-21 20:56:40 +04:00
|
|
|
LONG status;
|
2011-10-15 19:30:10 +04:00
|
|
|
SCARDCONTEXT hContext;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_Context(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirContextRef(smartcard, irp, redirect, &hContext);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
status = SCardCancel(hContext);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-11-22 05:46:58 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_Connect(SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL wide)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2012-11-21 20:56:40 +04:00
|
|
|
LONG status;
|
2011-10-15 19:30:10 +04:00
|
|
|
SCARDCONTEXT hContext;
|
2012-11-21 20:56:40 +04:00
|
|
|
char* readerName = NULL;
|
2011-10-15 19:30:10 +04:00
|
|
|
DWORD dwShareMode = 0;
|
|
|
|
DWORD dwPreferredProtocol = 0;
|
|
|
|
DWORD dwActiveProtocol = 0;
|
|
|
|
SCARDHANDLE hCard;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
/* Skip ptrReader */
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < 4)
|
|
|
|
{
|
2014-04-04 01:29:12 +04:00
|
|
|
DEBUG_WARN("Length violation %d [%d]", 4,
|
2013-09-12 14:18:35 +04:00
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
}
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->input, 4);
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
/* Read common data */
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_Context(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < 8)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("Length violadion %d [%d]", 8,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Read_UINT32(irp->input, dwShareMode);
|
|
|
|
Stream_Read_UINT32(irp->input, dwPreferredProtocol);
|
|
|
|
|
|
|
|
status = smartcard_input_reader_name(irp, &readerName, wide);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirContextRef(smartcard, irp, redirect, &hContext);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
if (!check_reader_is_forwarded(smartcard, readerName))
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("Reader '%s' not forwarded!", readerName);
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_E_INVALID_TARGET;
|
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 21:57:28 +04:00
|
|
|
status = SCardConnectA(hContext, readerName, (DWORD) dwShareMode,
|
2011-10-15 19:30:10 +04:00
|
|
|
(DWORD) dwPreferredProtocol, &hCard, (DWORD *) &dwActiveProtocol);
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
smartcard->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
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 15:53:15 +04:00
|
|
|
finish:
|
|
|
|
if (readerName)
|
|
|
|
free(readerName);
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_Reconnect(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2012-11-21 20:56:40 +04:00
|
|
|
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;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CardHandle(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < 12)
|
2013-09-12 15:53:15 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("length violation %d [%d]", 12,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
2013-09-12 15:53:15 +04:00
|
|
|
}
|
2013-09-12 14:18:35 +04:00
|
|
|
|
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
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirHandleRef(smartcard, irp, redirect, &hContext, &hCard);
|
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
if (!check_handle_is_forwarded(smartcard, hCard, hContext))
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
|
|
|
|
return SCARD_E_INVALID_TARGET;
|
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
status = SCardReconnect(hCard, (DWORD) dwShareMode, (DWORD) dwPreferredProtocol,
|
2014-04-04 01:29:12 +04:00
|
|
|
(DWORD) dwInitialization, (LPDWORD) &dwActiveProtocol);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, dwActiveProtocol);
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_Disconnect(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2012-11-21 20:56:40 +04:00
|
|
|
LONG status;
|
2011-10-15 19:30:10 +04:00
|
|
|
SCARDCONTEXT hContext;
|
|
|
|
SCARDHANDLE hCard;
|
|
|
|
DWORD dwDisposition = 0;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CardHandle(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < 4)
|
2013-09-12 15:53:15 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("length violation %d [%d]", 4,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
2013-09-12 15:53:15 +04:00
|
|
|
}
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, dwDisposition);
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirHandleRef(smartcard, irp, redirect, &hContext, &hCard);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
if (!check_handle_is_forwarded(smartcard, hCard, hContext))
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
|
|
|
|
return SCARD_E_INVALID_TARGET;
|
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
status = SCardDisconnect(hCard, (DWORD) dwDisposition);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
ZeroMemory(&smartcard->hCard, sizeof(smartcard->hCard));
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_BeginTransaction(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2012-11-21 20:56:40 +04:00
|
|
|
LONG status;
|
2013-09-12 14:18:35 +04:00
|
|
|
SCARDHANDLE hCard;
|
|
|
|
SCARDCONTEXT hContext;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CardHandle(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < 4)
|
2013-09-12 15:53:15 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("length violation %d [%d]", 4,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
2013-09-12 15:53:15 +04:00
|
|
|
}
|
2013-09-12 14:18:35 +04:00
|
|
|
Stream_Seek(irp->input, 4);
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirHandleRef(smartcard, irp, redirect, &hContext, &hCard);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
if (!check_handle_is_forwarded(smartcard, hCard, hContext))
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
|
|
|
|
return SCARD_E_INVALID_TARGET;
|
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
status = SCardBeginTransaction(hCard);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_EndTransaction(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2012-11-21 20:56:40 +04:00
|
|
|
LONG status;
|
2013-09-12 14:18:35 +04:00
|
|
|
SCARDHANDLE hCard;
|
|
|
|
SCARDCONTEXT hContext;
|
2011-10-15 19:30:10 +04:00
|
|
|
DWORD dwDisposition = 0;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CardHandle(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < 4)
|
2013-09-12 15:53:15 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("length violation %d [%d]", 4,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
2013-09-12 15:53:15 +04:00
|
|
|
}
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, dwDisposition);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirHandleRef(smartcard, irp, redirect, &hContext, &hCard);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
if (!check_handle_is_forwarded(smartcard, hCard, hContext))
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
|
|
|
|
return SCARD_E_INVALID_TARGET;
|
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
status = SCardEndTransaction(hCard, dwDisposition);
|
2011-11-22 05:46:58 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-11-22 05:46:58 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_State(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2012-11-21 20:56:40 +04:00
|
|
|
LONG status;
|
2011-10-15 19:30:10 +04:00
|
|
|
SCARDHANDLE hCard;
|
2013-09-12 14:18:35 +04:00
|
|
|
SCARDCONTEXT hContext;
|
2011-10-15 19:30:10 +04:00
|
|
|
DWORD state = 0, protocol = 0;
|
|
|
|
DWORD readerLen;
|
2014-04-03 23:27:55 +04:00
|
|
|
DWORD atrLen = SCARD_ATR_LENGTH;
|
2013-09-13 11:47:18 +04:00
|
|
|
char* readerName = NULL;
|
2014-04-03 23:27:55 +04:00
|
|
|
BYTE pbAtr[SCARD_ATR_LENGTH];
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CardHandle(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < 8)
|
2013-09-12 15:53:15 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("length violation %d [%d]", 8,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
|
|
|
}
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
Stream_Seek(irp->input, 4);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek_UINT32(irp->input); /* atrLen */
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirHandleRef(smartcard, irp, redirect, &hContext, &hCard);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
if (!check_handle_is_forwarded(smartcard, hCard, hContext))
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_E_INVALID_TARGET;
|
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
readerLen = SCARD_AUTOALLOCATE;
|
2014-04-05 00:45:11 +04:00
|
|
|
status = SCardStatusA(hCard, (LPSTR) &readerName, &readerLen, &state, &protocol, pbAtr, &atrLen);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
if (status != SCARD_S_SUCCESS)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-12 15:53:15 +04:00
|
|
|
status = smartcard_output_return(irp, status);
|
|
|
|
goto finish;
|
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
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_repos(irp, atrLen);
|
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 15:53:15 +04:00
|
|
|
finish:
|
|
|
|
if (readerName)
|
|
|
|
{
|
|
|
|
SCardFreeMemory(hContext, readerName);
|
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static DWORD handle_Status(SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL wide)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2012-11-21 20:56:40 +04:00
|
|
|
LONG status;
|
2011-10-15 19:30:10 +04:00
|
|
|
SCARDHANDLE hCard;
|
2013-09-12 14:18:35 +04:00
|
|
|
SCARDCONTEXT hContext;
|
2011-10-15 19:30:10 +04:00
|
|
|
DWORD state, protocol;
|
|
|
|
DWORD readerLen = 0;
|
2014-04-03 23:27:55 +04:00
|
|
|
DWORD atrLen = SCARD_ATR_LENGTH;
|
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;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CardHandle(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < 12)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("length violation %d [%d]", 12,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
}
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
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);
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirHandleRef(smartcard, irp, redirect, &hContext, &hCard);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
if (!check_handle_is_forwarded(smartcard, hCard, hContext))
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_E_INVALID_TARGET;
|
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:47:18 +04:00
|
|
|
pbAtr = malloc(sizeof(BYTE) * atrLen);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
readerLen = SCARD_AUTOALLOCATE;
|
2014-04-04 21:57:28 +04:00
|
|
|
status = SCardStatusA(hCard, (LPSTR) &readerName, &readerLen, &state, &protocol, pbAtr, &atrLen);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
if (status != SCARD_S_SUCCESS)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-12 15:53:15 +04:00
|
|
|
status = smartcard_output_return(irp, status);
|
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2013-04-30 06:35:15 +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
|
|
|
|
2013-04-30 06:35:15 +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);
|
2012-11-21 20:56:40 +04:00
|
|
|
dataLength += smartcard_output_string(irp, "\0", wide);
|
|
|
|
smartcard_output_repos(irp, dataLength);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-04-30 06:35:15 +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);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(irp->output, poslen2);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output,dataLength);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(irp->output, pos);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 15:53:15 +04:00
|
|
|
finish:
|
|
|
|
if (readerName)
|
|
|
|
{
|
2014-04-04 01:29:12 +04:00
|
|
|
SCardFreeMemory(hContext, readerName);
|
2013-09-12 15:53:15 +04:00
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-13 11:47:18 +04:00
|
|
|
if (pbAtr)
|
|
|
|
free(pbAtr);
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_Transmit(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2012-11-21 20:56:40 +04:00
|
|
|
LONG status;
|
2013-09-12 14:18:35 +04:00
|
|
|
SCARDHANDLE hCard;
|
|
|
|
SCARDCONTEXT hContext;
|
2013-09-12 15:53:15 +04:00
|
|
|
UINT32 pioSendPciBufferPtr;
|
|
|
|
UINT32 ptrSendBuffer;
|
|
|
|
UINT32 ptrIoRecvPciBuffer;
|
|
|
|
UINT32 recvBufferIsNULL;
|
|
|
|
UINT32 linkedLen;
|
2013-09-13 11:47:18 +04:00
|
|
|
void *tmp;
|
2013-09-12 17:50:12 +04:00
|
|
|
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;
|
2013-09-12 16:10:44 +04:00
|
|
|
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;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CardHandle(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < 32)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("length violation %d [%d]", 32,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 17:50:12 +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);
|
2013-09-12 15:53:15 +04:00
|
|
|
Stream_Read_UINT32(irp->input, pioSendPciBufferPtr);
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, cbSendLength);
|
2013-09-12 15:53:15 +04:00
|
|
|
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
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirHandleRef(smartcard, irp, redirect, &hContext, &hCard);
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-23 11:11:23 +04:00
|
|
|
|
2013-09-12 15:53:15 +04:00
|
|
|
/* Check, if there is data available from the ipSendPci element */
|
|
|
|
if (pioSendPciBufferPtr)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-12 14:30:46 +04:00
|
|
|
if (Stream_GetRemainingLength(irp->input) < 8)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("length violation %d [%d]", 8,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
2013-09-12 14:30:46 +04:00
|
|
|
}
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, linkedLen);
|
2013-09-12 14:30:46 +04:00
|
|
|
|
2013-09-24 11:23:43 +04:00
|
|
|
if (Stream_GetRemainingLength(irp->input) < ioSendPci.rq->cbPciLength)
|
2013-09-12 14:30:46 +04:00
|
|
|
{
|
2013-09-24 11:23:43 +04:00
|
|
|
DEBUG_WARN("length violation %d [%d]", ioSendPci.rq->cbPciLength,
|
2013-09-12 14:30:46 +04:00
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
2013-09-12 14:30:46 +04:00
|
|
|
}
|
2013-09-12 17:50:12 +04:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
2013-09-24 11:23:43 +04:00
|
|
|
if (linkedLen < ioSendPci.rq->cbPciLength - sizeof(SCARD_IO_REQUEST))
|
2013-09-12 16:10:44 +04:00
|
|
|
{
|
2013-09-12 17:50:12 +04:00
|
|
|
DEBUG_WARN("SCARD_IO_REQUEST with invalid extra byte length %d [%d]",
|
|
|
|
ioSendPci.rq->cbPciLength - sizeof(SCARD_IO_REQUEST), linkedLen);
|
2013-09-12 16:10:44 +04:00
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
|
|
|
}
|
2013-09-13 11:47:18 +04:00
|
|
|
|
2013-10-01 16:41:59 +04:00
|
|
|
/* Invalid length received. */
|
|
|
|
if (!ioSendPci.rq->cbPciLength)
|
|
|
|
{
|
|
|
|
if (ioSendPci.v)
|
|
|
|
free(ioSendPci.v);
|
|
|
|
ioSendPci.v = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmp = realloc(ioSendPci.v, ioSendPci.rq->cbPciLength);
|
|
|
|
ioSendPci.v = tmp;
|
|
|
|
|
|
|
|
Stream_Read(irp->input, &ioSendPci.rq[1], ioSendPci.rq->cbPciLength);
|
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
2013-09-12 15:53:15 +04:00
|
|
|
else
|
2013-09-12 17:50:12 +04:00
|
|
|
ioSendPci.rq->cbPciLength = sizeof(SCARD_IO_REQUEST);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 15:53:15 +04:00
|
|
|
/* Check, if there is data available from the SendBufferPointer */
|
|
|
|
if (ptrSendBuffer)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-12 14:30:46 +04:00
|
|
|
if (Stream_GetRemainingLength(irp->input) < 4)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("length violation %d [%d]", 4,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
2013-09-12 14:30:46 +04:00
|
|
|
}
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, linkedLen);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-24 11:23:43 +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)
|
2013-09-12 14:30:46 +04:00
|
|
|
{
|
2013-09-24 11:23:43 +04:00
|
|
|
DEBUG_WARN("length violation %d [%d]", cbSendLength,
|
2013-09-12 14:30:46 +04:00
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
2013-09-12 14:30:46 +04:00
|
|
|
}
|
2013-09-24 11:23:43 +04:00
|
|
|
sendBuf = malloc(cbSendLength);
|
|
|
|
Stream_Read(irp->input, sendBuf, cbSendLength);
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2013-09-12 15:53:15 +04:00
|
|
|
/* Check, if a response is desired. */
|
|
|
|
if (cbRecvLength && !recvBufferIsNULL)
|
2012-10-09 07:21:26 +04:00
|
|
|
recvBuf = malloc(cbRecvLength);
|
2013-09-12 15:53:15 +04:00
|
|
|
else
|
|
|
|
cbRecvLength = 0;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 15:53:15 +04:00
|
|
|
if (ptrIoRecvPciBuffer)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-12 14:30:46 +04:00
|
|
|
if (Stream_GetRemainingLength(irp->input) < 8)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("length violation %d [%d]", 8,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
2013-09-12 14:30:46 +04:00
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
/* recvPci */
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, linkedLen);
|
2013-09-12 17:50:12 +04:00
|
|
|
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. */
|
2013-10-02 18:48:54 +04:00
|
|
|
if (linkedLen < ioRecvPci.rq->cbPciLength)
|
2013-09-12 17:50:12 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("SCARD_IO_REQUEST with invalid extra byte length %d [%d]",
|
2013-10-02 18:48:54 +04:00
|
|
|
ioRecvPci.rq->cbPciLength - sizeof(SCARD_IO_REQUEST), linkedLen);
|
2013-09-12 17:50:12 +04:00
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
|
|
|
}
|
2013-09-12 14:30:46 +04:00
|
|
|
|
2013-09-24 11:23:43 +04:00
|
|
|
if (Stream_GetRemainingLength(irp->input) < ioRecvPci.rq->cbPciLength)
|
2013-09-12 14:30:46 +04:00
|
|
|
{
|
2013-09-24 11:23:43 +04:00
|
|
|
DEBUG_WARN("length violation %d [%d]", ioRecvPci.rq->cbPciLength,
|
2013-09-12 14:30:46 +04:00
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
2013-09-12 14:30:46 +04:00
|
|
|
}
|
|
|
|
|
2013-09-12 17:50:12 +04:00
|
|
|
/* Read data, see
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa379807%28v=vs.85%29.aspx
|
|
|
|
*/
|
2013-10-01 16:41:59 +04:00
|
|
|
if (!ioRecvPci.rq->cbPciLength)
|
|
|
|
{
|
|
|
|
if (ioRecvPci.v)
|
|
|
|
free(ioRecvPci.v);
|
|
|
|
ioRecvPci.v = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmp = realloc(ioRecvPci.v, ioRecvPci.rq->cbPciLength);
|
|
|
|
ioRecvPci.v = tmp;
|
|
|
|
|
|
|
|
Stream_Read(irp->input, &ioRecvPci.rq[1], ioRecvPci.rq->cbPciLength);
|
|
|
|
}
|
2013-09-12 17:50:12 +04:00
|
|
|
|
|
|
|
pPioRecvPci = ioRecvPci.rq;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
else
|
2014-04-04 01:29:12 +04:00
|
|
|
{
|
2011-10-15 19:30:10 +04:00
|
|
|
pPioRecvPci = NULL;
|
2014-04-04 01:29:12 +04:00
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
if (!check_handle_is_forwarded(smartcard, hCard, hContext))
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_E_INVALID_TARGET;
|
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
}
|
|
|
|
|
2013-09-12 17:50:12 +04:00
|
|
|
status = SCardTransmit(hCard, ioSendPci.rq, sendBuf, cbSendLength,
|
2011-10-15 19:30:10 +04:00
|
|
|
pPioRecvPci, recvBuf, &cbRecvLength);
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
if (status != SCARD_S_SUCCESS)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2014-04-04 01:29:12 +04:00
|
|
|
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 15:53:15 +04:00
|
|
|
finish:
|
2013-08-30 18:30:27 +04:00
|
|
|
if (sendBuf)
|
|
|
|
free(sendBuf);
|
|
|
|
if (recvBuf)
|
|
|
|
free(recvBuf);
|
2013-09-12 17:50:12 +04:00
|
|
|
if (ioSendPci.v)
|
|
|
|
free(ioSendPci.v);
|
|
|
|
if (ioRecvPci.v)
|
|
|
|
free(ioRecvPci.v);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_Control(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2012-11-21 20:56:40 +04:00
|
|
|
LONG status;
|
2011-10-15 19:30:10 +04:00
|
|
|
SCARDCONTEXT hContext;
|
|
|
|
SCARDHANDLE hCard;
|
2013-09-12 15:53:15 +04:00
|
|
|
UINT32 pvInBuffer, fpvOutBufferIsNULL;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 controlCode;
|
|
|
|
UINT32 controlFunction;
|
2012-11-21 20:56:40 +04:00
|
|
|
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;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CardHandle(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(irp->input) < 20)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("length violation %d [%d]", 20,
|
|
|
|
Stream_GetRemainingLength(irp->input));
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_F_INTERNAL_ERROR;
|
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
}
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, controlCode);
|
|
|
|
Stream_Read_UINT32(irp->input, recvLength);
|
2013-09-12 15:53:15 +04:00
|
|
|
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);
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirHandleRef(smartcard, irp, redirect, &hContext, &hCard);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
2013-09-12 15:53:15 +04:00
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2011-11-23 04:22:17 +04:00
|
|
|
/* Translate Windows SCARD_CTL_CODE's to corresponding local code */
|
2014-04-04 01:29:12 +04:00
|
|
|
if (DEVICE_TYPE_FROM_CTL_CODE(controlCode) == FILE_DEVICE_SMARTCARD)
|
2011-11-23 04:22:17 +04:00
|
|
|
{
|
2014-04-04 01:29:12 +04:00
|
|
|
controlFunction = FUNCTION_FROM_CTL_CODE(controlCode);
|
2011-11-23 04:22:17 +04:00
|
|
|
controlCode = SCARD_CTL_CODE(controlFunction);
|
|
|
|
}
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 15:53:15 +04:00
|
|
|
if (pvInBuffer)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-12 15:53:15 +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
|
|
|
|
2013-09-12 15:53:15 +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;
|
|
|
|
}
|
2012-10-09 07:21:26 +04:00
|
|
|
recvBuffer = malloc(recvLength);
|
2011-11-22 05:46:58 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read(irp->input, recvBuffer, recvLength);
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nBytesReturned = outBufferSize;
|
2012-10-09 07:21:26 +04:00
|
|
|
sendBuffer = malloc(outBufferSize);
|
2011-11-22 05:46:58 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
if (!check_handle_is_forwarded(smartcard, hCard, hContext))
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
|
2013-09-12 15:53:15 +04:00
|
|
|
status = SCARD_E_INVALID_TARGET;
|
|
|
|
goto finish;
|
2013-09-12 14:18:35 +04:00
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
status = SCardControl(hCard, (DWORD) controlCode, recvBuffer, (DWORD) recvLength,
|
2011-10-15 19:30:10 +04:00
|
|
|
sendBuffer, (DWORD) outBufferSize, &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);
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_repos(irp, nBytesReturned);
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 15:53:15 +04:00
|
|
|
finish:
|
|
|
|
if (recvBuffer)
|
|
|
|
free(recvBuffer);
|
|
|
|
if (sendBuffer)
|
|
|
|
free(sendBuffer);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_GetAttrib(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2012-11-21 20:56:40 +04:00
|
|
|
LONG status;
|
2011-10-15 19:30:10 +04:00
|
|
|
SCARDHANDLE hCard;
|
2013-09-12 14:18:35 +04:00
|
|
|
SCARDCONTEXT hContext;
|
2012-11-21 20:56:40 +04:00
|
|
|
DWORD dwAttrId = 0;
|
|
|
|
DWORD dwAttrLen = 0;
|
2011-10-15 19:30:10 +04:00
|
|
|
DWORD attrLen = 0;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* pbAttr = NULL;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CardHandle(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
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);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->input, 0x4);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, dwAttrLen);
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirHandleRef(smartcard, irp, redirect, &hContext, &hCard);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
if (!check_handle_is_forwarded(smartcard, hCard, hContext))
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("invalid handle %p [%p]", hCard, hContext);
|
|
|
|
return SCARD_E_INVALID_TARGET;
|
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
attrLen = (dwAttrLen == 0) ? 0 : SCARD_AUTOALLOCATE;
|
2012-11-21 20:56:40 +04:00
|
|
|
status = SCardGetAttrib(hCard, dwAttrId, attrLen == 0 ? NULL : (BYTE*) &pbAttr, &attrLen);
|
|
|
|
|
|
|
|
if (status != SCARD_S_SUCCESS)
|
|
|
|
{
|
2014-04-04 01:29:12 +04:00
|
|
|
attrLen = (dwAttrLen == 0) ? 0 : SCARD_AUTOALLOCATE;
|
2011-12-22 23:23:08 +04:00
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
if (dwAttrId == SCARD_ATTR_DEVICE_FRIENDLY_NAME_A && status == SCARD_E_UNSUPPORTED_FEATURE)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2012-11-21 20:56:40 +04:00
|
|
|
status = SCardGetAttrib(hCard, SCARD_ATTR_DEVICE_FRIENDLY_NAME_W,
|
2012-10-09 11:01:37 +04:00
|
|
|
attrLen == 0 ? NULL : (BYTE*) &pbAttr, &attrLen);
|
2012-11-21 20:56:40 +04:00
|
|
|
|
|
|
|
if (status != SCARD_S_SUCCESS)
|
|
|
|
{
|
2014-04-04 01:29:12 +04:00
|
|
|
attrLen = (dwAttrLen == 0) ? 0 : SCARD_AUTOALLOCATE;
|
2011-12-22 23:23:08 +04:00
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
2014-04-04 01:29:12 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
if (dwAttrId == SCARD_ATTR_DEVICE_FRIENDLY_NAME_W && status == SCARD_E_UNSUPPORTED_FEATURE)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2012-11-21 20:56:40 +04:00
|
|
|
status = SCardGetAttrib(hCard, SCARD_ATTR_DEVICE_FRIENDLY_NAME_A,
|
2012-10-09 11:01:37 +04:00
|
|
|
attrLen == 0 ? NULL : (BYTE*) &pbAttr, &attrLen);
|
2012-11-21 20:56:40 +04:00
|
|
|
|
|
|
|
if (status != SCARD_S_SUCCESS)
|
|
|
|
{
|
2014-04-04 01:29:12 +04:00
|
|
|
attrLen = (dwAttrLen == 0) ? 0 : SCARD_AUTOALLOCATE;
|
2011-12-22 23:23:08 +04:00
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
2012-11-21 20:56:40 +04:00
|
|
|
if (attrLen > dwAttrLen && pbAttr != NULL)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2012-11-21 20:56:40 +04:00
|
|
|
status = SCARD_E_INSUFFICIENT_BUFFER;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
dwAttrLen = attrLen;
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
if (status != SCARD_S_SUCCESS)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-10-01 15:09:52 +04:00
|
|
|
status = smartcard_output_return(irp, status);
|
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
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
|
|
|
}
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_repos(irp, dwAttrLen);
|
2011-11-22 05:46:58 +04:00
|
|
|
/* 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
|
|
|
}
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-10-01 15:09:52 +04:00
|
|
|
finish:
|
2013-09-12 14:18:35 +04:00
|
|
|
SCardFreeMemory(hContext, pbAttr);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_AccessStartedEvent(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-12 14:18:35 +04:00
|
|
|
if (Stream_GetRemainingLength(irp->input) < 4)
|
|
|
|
{
|
2013-10-24 23:34:14 +04:00
|
|
|
DEBUG_WARN("length violation %d [%d]", 4, Stream_GetRemainingLength(irp->input));
|
2013-09-12 14:18:35 +04:00
|
|
|
return SCARD_F_INTERNAL_ERROR;
|
|
|
|
}
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
Stream_Seek(irp->input, 4);
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2013-09-12 14:18:35 +04:00
|
|
|
|
2011-10-15 19:30:10 +04:00
|
|
|
return SCARD_S_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
void scard_error(SMARTCARD_DEVICE* smartcard, IRP* irp, UINT32 ntstatus)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2011-11-22 05:46:58 +04:00
|
|
|
/* [MS-RDPESC] 3.1.4.4 */
|
2013-09-12 14:18:35 +04:00
|
|
|
DEBUG_WARN("scard processing error %x", ntstatus);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(irp->output, 0); /* CHECKME */
|
2011-10-15 19:30:10 +04:00
|
|
|
irp->IoStatus = ntstatus;
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
static UINT32 handle_LocateCardsByATR(SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL wide)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-09-30 16:49:19 +04:00
|
|
|
int redirect = 0;
|
2012-11-21 20:56:40 +04:00
|
|
|
LONG status;
|
2014-04-05 00:45:11 +04:00
|
|
|
UINT32 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;
|
2014-04-05 00:45:11 +04:00
|
|
|
SCARD_READERSTATEA* cur = NULL;
|
|
|
|
SCARD_READERSTATEA* rsCur = NULL;
|
|
|
|
SCARD_READERSTATEA* readerStates = NULL;
|
2014-04-04 01:29:12 +04:00
|
|
|
SCARD_ATRMASK* curAtr = NULL;
|
|
|
|
SCARD_ATRMASK* pAtrMasks = NULL;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_CommonTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_PrivateTypeHeader(smartcard, irp);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_Context(smartcard, irp, &redirect);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
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);
|
2014-04-04 01:29:12 +04:00
|
|
|
status = handle_RedirContextRef(smartcard, irp, redirect, &hContext);
|
2013-10-24 23:34:14 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
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
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
pAtrMasks = malloc(atrMaskCount * sizeof(SCARD_ATRMASK));
|
2011-11-22 05:46:58 +04:00
|
|
|
|
2011-10-15 19:30:10 +04:00
|
|
|
if (!pAtrMasks)
|
2012-11-21 20:56:40 +04:00
|
|
|
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
|
|
|
|
2012-11-20 08:49:08 +04:00
|
|
|
readerStates = malloc(readerCount * sizeof(SCARD_READERSTATE));
|
|
|
|
ZeroMemory(readerStates, readerCount * sizeof(SCARD_READERSTATE));
|
2011-11-22 05:46:58 +04:00
|
|
|
|
2011-10-15 19:30:10 +04:00
|
|
|
for (i = 0; i < readerCount; i++)
|
|
|
|
{
|
|
|
|
cur = &readerStates[i];
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
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
|
|
|
|
2013-04-30 06:35:15 +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++)
|
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 dataLength;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-05 00:45:11 +04:00
|
|
|
cur = &readerStates[i];
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->input, 8);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, dataLength);
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_input_repos(irp, smartcard_input_string(irp, (char **) &cur->szReader, dataLength, wide));
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-08-30 18:30:27 +04:00
|
|
|
if (!cur->szReader)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("cur->szReader=%p", cur->szReader);
|
|
|
|
continue;
|
|
|
|
}
|
2014-04-05 00:45:11 +04:00
|
|
|
|
2011-10-15 19:30:10 +04:00
|
|
|
if (strcmp(cur->szReader, "\\\\?PnP?\\Notification") == 0)
|
|
|
|
cur->dwCurrentState |= SCARD_STATE_IGNORE;
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:57:28 +04:00
|
|
|
status = SCardGetStatusChangeA(hContext, 0x00000001, readerStates, readerCount);
|
2014-04-04 01:29:12 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
if (status != SCARD_S_SUCCESS)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-10-01 15:09:52 +04:00
|
|
|
status = smartcard_output_return(irp, status);
|
|
|
|
goto finish;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, curAtr = pAtrMasks; i < atrMaskCount; i++, curAtr++)
|
|
|
|
{
|
|
|
|
for (j = 0, rsCur = readerStates; j < readerCount; j++, rsCur++)
|
|
|
|
{
|
2012-10-09 10:38:39 +04:00
|
|
|
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)
|
|
|
|
{
|
2014-04-04 21:57:28 +04:00
|
|
|
rsCur->dwEventState |= SCARD_STATE_ATRMATCH;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-08-30 16:19:50 +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
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard_output_alignment(irp, 8);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-10-01 15:09:52 +04:00
|
|
|
finish:
|
|
|
|
for (i = 0, cur = readerStates; i < readerCount; i++, cur++)
|
|
|
|
{
|
|
|
|
if (cur->szReader)
|
|
|
|
free((void*) cur->szReader);
|
|
|
|
cur->szReader = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (readerStates)
|
|
|
|
free(readerStates);
|
|
|
|
|
|
|
|
if (pAtrMasks)
|
|
|
|
free(pAtrMasks);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
return status;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
void smartcard_device_control(SMARTCARD_DEVICE* smartcard, 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;
|
2014-04-04 08:56:24 +04:00
|
|
|
UINT32 outputBufferLength;
|
|
|
|
UINT32 inputBufferLength;
|
2014-04-04 02:47:18 +04:00
|
|
|
UINT32 ioControlCode;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 stream_len;
|
|
|
|
UINT32 irp_result_pos;
|
|
|
|
UINT32 output_len_pos;
|
2012-11-21 20:56:40 +04:00
|
|
|
const UINT32 header_lengths = 16;
|
|
|
|
|
|
|
|
/* MS-RPCE, Sections 2.2.6.1 and 2.2.6.2. */
|
2013-09-12 14:18:35 +04:00
|
|
|
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
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
Stream_Read_UINT32(irp->input, outputBufferLength); /* OutputBufferLength (4 bytes) */
|
|
|
|
Stream_Read_UINT32(irp->input, inputBufferLength); /* InputBufferLength (4 bytes) */
|
|
|
|
Stream_Read_UINT32(irp->input, ioControlCode); /* IoControlCode (4 bytes) */
|
|
|
|
Stream_Seek(irp->input, 20); /* Padding (20 bytes) */
|
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
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
irp_result_pos = Stream_GetPosition(irp->output);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
Stream_Write_UINT32(irp->output, 0); /* OutputBufferLength (4 bytes) */
|
|
|
|
|
2011-10-15 19:30:10 +04:00
|
|
|
/* [MS-RPCE] 2.2.6.1 */
|
2014-04-04 08:56:24 +04:00
|
|
|
Stream_Write_UINT8(irp->output, 1); /* Version (1 byte) */
|
|
|
|
Stream_Write_UINT8(irp->output, 0x10); /* Endianness (1 byte) */
|
|
|
|
Stream_Write_UINT16(irp->output, 8); /* CommonHeaderLength (2 bytes) */
|
|
|
|
Stream_Write_UINT32(irp->output, 0xCCCCCCCC); /* Filler (4 bytes), should be 0xCCCCCCCC */
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
output_len_pos = Stream_GetPosition(irp->output);
|
2014-04-04 08:56:24 +04:00
|
|
|
Stream_Seek(irp->output, 4); /* ObjectBufferLength (4 bytes) */
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
Stream_Write_UINT32(irp->output, 0x0); /* Filler (4 bytes), should be 0x00000000 */
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
result_pos = Stream_GetPosition(irp->output);
|
2014-04-04 08:56:24 +04:00
|
|
|
Stream_Seek(irp->output, 4); /* Result (4 bytes) */
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 14:18:35 +04:00
|
|
|
/* Ensure, that this package is fully available. */
|
2014-04-04 08:56:24 +04:00
|
|
|
if (Stream_GetRemainingLength(irp->input) < inputBufferLength)
|
2013-09-12 14:18:35 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("Invalid IRP of length %d received, expected %d, ignoring.",
|
2014-04-04 08:56:24 +04:00
|
|
|
Stream_GetRemainingLength(irp->input), inputBufferLength);
|
2013-09-12 14:18:35 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-04 08:56:24 +04:00
|
|
|
WLog_Print(smartcard->log, WLOG_WARN, "ioControlCode: %s (0x%08X)",
|
|
|
|
smartcard_get_ioctl_string(ioControlCode), ioControlCode);
|
|
|
|
|
|
|
|
if (Stream_Length(irp->input) != (Stream_GetPosition(irp->input) + inputBufferLength))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Input buffer length mismatch: Actual: %d Expected: %d\n",
|
|
|
|
Stream_Length(irp->input), Stream_GetPosition(irp->input) + inputBufferLength);
|
|
|
|
return;
|
|
|
|
}
|
2014-04-04 01:29:12 +04:00
|
|
|
|
2014-04-04 02:47:18 +04:00
|
|
|
switch (ioControlCode)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_ESTABLISHCONTEXT:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_EstablishContext(smartcard, irp);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_ISVALIDCONTEXT:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_IsValidContext(smartcard, irp);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_RELEASECONTEXT:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_ReleaseContext(smartcard, irp);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_LISTREADERSA:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_ListReaders(smartcard, irp, 0);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_LISTREADERSW:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_ListReaders(smartcard, irp, 1);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_LISTREADERGROUPSA:
|
2014-04-04 21:57:28 +04:00
|
|
|
result = SCARD_F_INTERNAL_ERROR;
|
|
|
|
break;
|
|
|
|
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_LISTREADERGROUPSW:
|
2011-10-15 19:30:10 +04:00
|
|
|
result = SCARD_F_INTERNAL_ERROR;
|
|
|
|
break;
|
|
|
|
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_GETSTATUSCHANGEA:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_GetStatusChange(smartcard, irp, 0);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_GETSTATUSCHANGEW:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_GetStatusChange(smartcard, irp, 1);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SCARD_IOCTL_CANCEL:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_Cancel(smartcard, irp);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_CONNECTA:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_Connect(smartcard, irp, 0);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_CONNECTW:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_Connect(smartcard, irp, 1);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SCARD_IOCTL_RECONNECT:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_Reconnect(smartcard, irp);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SCARD_IOCTL_DISCONNECT:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_Disconnect(smartcard, irp);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_BEGINTRANSACTION:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_BeginTransaction(smartcard, irp);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_ENDTRANSACTION:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_EndTransaction(smartcard, irp);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SCARD_IOCTL_STATE:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_State(smartcard, irp);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_STATUSA:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_Status(smartcard, irp, 0);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_STATUSW:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_Status(smartcard, irp, 1);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SCARD_IOCTL_TRANSMIT:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_Transmit(smartcard, irp);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SCARD_IOCTL_CONTROL:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_Control(smartcard, irp);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SCARD_IOCTL_GETATTRIB:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_GetAttrib(smartcard, irp);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_ACCESSSTARTEDEVENT:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_AccessStartedEvent(smartcard, irp);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_LOCATECARDSBYATRA:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_LocateCardsByATR(smartcard, irp, 0);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
2014-04-04 02:47:18 +04:00
|
|
|
case SCARD_IOCTL_LOCATECARDSBYATRW:
|
2014-04-04 01:29:12 +04:00
|
|
|
result = handle_LocateCardsByATR(smartcard, irp, 1);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-04-04 08:56:24 +04:00
|
|
|
result = STATUS_UNSUCCESSFUL;
|
2013-09-12 14:18:35 +04:00
|
|
|
DEBUG_WARN("scard unknown ioctl 0x%x [%d]\n",
|
2014-04-04 08:56:24 +04:00
|
|
|
ioControlCode, inputBufferLength);
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* look for NTSTATUS errors */
|
2014-04-04 08:56:24 +04:00
|
|
|
if ((result & 0xC0000000) == 0xC0000000)
|
2014-04-05 00:45:11 +04:00
|
|
|
scard_error(smartcard, irp, result);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
/* per Ludovic Rousseau, map different usage of this particular
|
|
|
|
* error code between pcsc-lite & windows */
|
|
|
|
if (result == 0x8010001F)
|
|
|
|
result = 0x80100022;
|
|
|
|
|
|
|
|
/* handle response packet */
|
2013-04-30 06:35:15 +04:00
|
|
|
pos = Stream_GetPosition(irp->output);
|
2012-08-02 02:05:45 +04:00
|
|
|
stream_len = pos - irp_result_pos - 4; /* Value of OutputBufferLength */
|
2013-04-30 06:35:15 +04:00
|
|
|
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
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(irp->output, output_len_pos);
|
2012-08-02 02:05:45 +04:00
|
|
|
/* 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
|
|
|
|
2013-04-30 06:35:15 +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
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(irp->output, pos);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
irp->IoStatus = 0;
|
|
|
|
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|