⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcsc_drv.cpp

📁 Skeleton for implementing a Windows PC/SC Smartcard Reader.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    USHORT      LibProtocol;
    KdPrint(("%sVdSetProtocol\n",HEAD));
    if ((status = SmartcardAcquireRemoveLock(pSmartcardExtension)) != STATUS_SUCCESS)
    {
        pSmartcardExtension->IoRequest.Information = 0;
        // We should immediately return the control back, because the driver 
        // is about to unload.
    }
    else
    {
        LibProtocol = (USHORT)(pSmartcardExtension->MinorIoControlCode);
        pSmartcardExtension->SmartcardRequest.BufferLength = 1;
        pSmartcardExtension->SmartcardReply.BufferLength = 0;
        LibProtocol = SCARD_PROTOCOL_T0;
        pSmartcardExtension->CardCapabilities.Protocol.Selected = LibProtocol;
        *(PULONG)(pSmartcardExtension->IoRequest.ReplyBuffer) = LibProtocol;
        *(pSmartcardExtension->IoRequest.Information) = sizeof(ULONG);
        SmartcardReleaseRemoveLock(pSmartcardExtension);
    }
    return status;
}

//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdTransmit</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="pSmartcardExtension">      				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdTransmit(PSMARTCARD_EXTENSION pSmartcardExtension)
{
    
    NTSTATUS status;
    KdPrint(("%sVdTransmit\n",HEAD));
    if ((status = SmartcardAcquireRemoveLock(pSmartcardExtension))!= STATUS_SUCCESS)
    {
        pSmartcardExtension->IoRequest.Information = 0;
        // We should immediately return the control back, because the driver 
        // is about to unload.
    }
    else
    {
        switch (pSmartcardExtension->CardCapabilities.Protocol.Selected)
        {
        case SCARD_PROTOCOL_T0 :
            status = VdTransmitT0(pSmartcardExtension);
            break;
        case SCARD_PROTOCOL_T1 :
            status = VdTransmitT1(pSmartcardExtension);
            break;
        case SCARD_PROTOCOL_RAW :
            status = VdTransmitRaw(pSmartcardExtension);
            break;
        }
        SmartcardReleaseRemoveLock(pSmartcardExtension);
    }
    return status;
}

//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdTransmitT0</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="pSmartcardExtension">       				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdTransmitT0(PSMARTCARD_EXTENSION pSmartcardExtension)
{
    
    NTSTATUS status = STATUS_SUCCESS;
    KdPrint(("%sVdTransmitT0\n",HEAD));

    if ((status = SmartcardT0Request(pSmartcardExtension))== STATUS_SUCCESS)
    {
    KdPrint(("\t%sSize of request: pSmartcardExtension->T0.Lc: %#X\n",HEAD, pSmartcardExtension->T0.Lc));
    KdPrint(("\t%sExpected size: pSmartcardExtension->T0.Le: %#X\n",HEAD, pSmartcardExtension->T0.Le + 2));
    KdPrint(("\t%sBUFFER: ",HEAD));
#ifdef _DEBUG
    for (UCHAR u = 0; u < pSmartcardExtension->SmartcardRequest.BufferLength; u++)
    {
        KdPrint(("%X;", pSmartcardExtension->SmartcardRequest.Buffer[u]));
    }
    KdPrint(("\n"));
#endif
    // Send command to emulator of reader
    status = SendCmdReader(pSmartcardExtension->SmartcardRequest.Buffer,
        pSmartcardExtension->SmartcardRequest.BufferLength,
        pSmartcardExtension->SmartcardReply.Buffer,
        pSmartcardExtension->SmartcardReply.BufferLength,
        (PDEVICE_OBJECT)  pSmartcardExtension->OsData->DeviceObject);
    }
    return SmartcardT0Reply(pSmartcardExtension);
}

//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdTransmitT1</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="pSmartcardExtension">      				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdTransmitT1(PSMARTCARD_EXTENSION pSmartcardExtension)
{
    KdPrint(("%sVdTransmitT1\n",HEAD));
    return (STATUS_INVALID_DEVICE_REQUEST);
}


//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdTransmitRaw</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="pSmartcardExtension">       				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdTransmitRaw(PSMARTCARD_EXTENSION pSmartcardExtension)
{
    KdPrint(("%sVdTransmitRaw\n",HEAD));
    return (STATUS_INVALID_DEVICE_REQUEST);
}

//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdCardTracking</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="pSmartcardExtension">      				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdCardTracking(PSMARTCARD_EXTENSION pSmartcardExtension)
{
    KIRQL CancelIrql;
    KdPrint(("%sVdCardTracking\n",HEAD));
    IoAcquireCancelSpinLock(&CancelIrql);
    IoSetCancelRoutine(pSmartcardExtension->OsData->NotificationIrp, VdCancel);
    IoReleaseCancelSpinLock(CancelIrql);
    return STATUS_PENDING;
}

//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdCancel</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="DeviceObject">             				       </param>
//	<param name="pIrp">                     				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdCancel(IN PDEVICE_OBJECT DeviceObject, IN PIRP pIrp)
{
    PDEVICE_EXTENSION       pDeviceExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
    PSMARTCARD_EXTENSION    pSmartcardExtension = &pDeviceExtension->SmartcardExtension;
    KdPrint(("%sVdCancel\n",HEAD));
    pSmartcardExtension->OsData->NotificationIrp = NULL;
    pIrp->IoStatus.Information = 0;
    pIrp->IoStatus.Status = STATUS_CANCELLED;
    IoReleaseCancelSpinLock(pIrp->CancelIrql);
    IoCompleteRequest(pIrp, IO_NO_INCREMENT);
    return STATUS_CANCELLED;
}


#ifdef USE_SYSTEM_THREAD
//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		Work_SystemThread</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="DeviceObject">             				       </param>
//	<param name="Context">                  				       </param>
//<returns> </returns>
// ======================================================================
void Work_SystemThread(IN PDEVICE_OBJECT DeviceObject, IN PVOID Context)
{
    PAGED_CODE();
    PDEVICE_EXTENSION pDeviceExtension = NULL;
    pDeviceExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
    KdPrint("%sWork_SystemThread\n",HEAD);
    KdPrint(("\t%sWork_File : Data %s\n",HEAD, (PUCHAR)Context));
    if (pDeviceExtension->uSizeOfData)
        if (Context != NULL)
            WorkWithFile(&(*(PUCHAR)Context), DeviceExtension->uSizeOfData); // Read Data
        else
            KdPrint(("\t%sWork_File : Wrong pointer to data\n",HEAD));
    else
        KdPrint(("\t%sWork_File : Size of data can't be equal 0\n",HEAD));
}
#endif


//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdVendorIOCTL</newpara>
//<newpara>	Comments :										
//Input parameters:
//  pSmartcardExtension->MajorIoControlCode: Contains a vendor - specific IOCTL code. 
//      Refer to the macro SCARD_CTL_CODE in winsmcrd.h for information 
//      how to define a vendor - specific IOCTL code. 
//      Note that the code must be between 2048 and 4095. 
//  pSmartcardExtension->IoRequest.RequestBuffer : Points to the user's input buffer. 
//  pSmartcardExtension->IoRequest.RequestBufferLength : The size of the user's input buffer. 
//  pSmartcardExtension->IoRequest.ReplyBuffer : Points to the user's output buffer. 
//  pSmartcardExtension->IoRequest.ReplyBufferLength : The size of the user's output buffer. 
//Output parameters:
//  pSmartcardExtension->IoRequest.Information : Must be set to the number of bytes returned. 
//</newpara>
//</summary>
//Parameters :
//	<param name="pSmartcardExtension">      				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdVendorIOCTL(PSMARTCARD_EXTENSION pSmartcardExtension)
{
    NTSTATUS        status      = STATUS_SUCCESS;
    static  char    answer[]    = "Vendor IOCTL CreateEvent and WORK_THREAD";   
    HANDLE          hThread     = NULL;
    KdPrint(("%sVdVendorIOCTL\n",HEAD));
    PAGED_CODE();
    if ((status = SmartcardAcquireRemoveLock(pSmartcardExtension)) != STATUS_SUCCESS)
    {
        pSmartcardExtension->IoRequest.Information = 0;
        // We should immediately return the control back, because the driver 
        // is about to unload.
    }
    else
    {
        switch (pSmartcardExtension->MajorIoControlCode)
        {
        case  IOCTL_CREATE_EVENT:
            {
                if (pSmartcardExtension->IoRequest.ReplyBuffer != NULL && 
                    pSmartcardExtension->IoRequest.ReplyBufferLength >= strlen(answer) + 1) 
                { 
                    strcpy((PCHAR)pSmartcardExtension->IoRequest.ReplyBuffer, answer);
                    *pSmartcardExtension->IoRequest.Information = strlen(answer);
                    //-----------------------------------------------------------------------------
#ifdef USE_WORK_THREAD
                    // Let's create Notification event for work thread or check and if
                    // one already exists , to miss this part of the code
                    if (READER_EXTENSION(pevWORK_THREAD_AWAKE))
                    {
                        status = STATUS_SUCCESS;
                        break;   
                    }
                    // let's create worker thread
                    // Start the worker thread
                    READER_EXTENSION(bThreadShouldStop) = false;
                    RtlInitUnicodeString(&READER_EXTENSION(uEventName), NOTIFICATIONEVENT_NAME);
                    READER_EXTENSION(pevWORK_THREAD_AWAKE) = 
                        (PKEVENT)IoCreateSynchronizationEvent(&READER_EXTENSION(uEventName), &READER_EXTENSION(hEvent));
                    if (!READER_EXTENSION(pevWORK_THREAD_AWAKE))
                    {
                       KdPrint(("\t%sVdVendorIOCTL Event was not created\n",HEAD));
                        *pSmartcardExtension->IoRequest.Information = 0;
                        status = STATUS_INSUFFICIENT_RESOURCES;
                        break;
                    }
                    else
                    {
                        KeClearEvent(READER_EXTENSION(pevWORK_THREAD_AWAKE));
                        if ((status = PsCreateSystemThread(&hThread, (ACCESS_MASK)0, NULL, (HANDLE)0,
                            NULL, WorkerThreadMain, READER_EXTENSION(pDeviceExtension))) != STATUS_SUCCESS)		// arg
                        {
                            // We didn't get the thread handle
                            *pSmartcardExtension->IoRequest.Information = 0;
                            status = STATUS_INSUFFICIENT_RESOURCES;
                            break;
                        }
                        else
                        {
                            KdPrint(("\t%sVdVendorIOCTL Event and WORK_THREAD were created\n",HEAD));
                            // Obtain real pointer to Thread object
                            ObReferenceObjectByHandle(hThread, THREAD_ALL_ACCESS, NULL, KernelMode,
                                (PVOID*)&READER_EXTENSION(pThreadObj), NULL);
                            ZwClose(hThread);	// don't need handle at all
                        }
                    }
#endif
                    //-----------------------------------------------------------------------------
                    break;
                }
                else
                {
                    status = STATUS_BUFFER_TOO_SMALL;
                    break;
                }
        default:
            status = STATUS_NOT_IMPLEMENTED;
            }
        }
        SmartcardReleaseRemoveLock(pSmartcardExtension);
    }
    KdPrint(("\t%sVdVendorIOCTL status %#X\n",HEAD,status));
    return status;
}
#pragma warning(pop) 
#else
#error Modification required for this type of OS
#endif
//----------------------------------------------------------------------------------------------
#endif // DRIVER_CPP

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -