freeotfe.c

来自「文件驱动加密,功能强大,可产生加密分区,支持AES,MD2,MD4,MD5MD2」· C语言 代码 · 共 1,966 行 · 第 1/5 页

C
1,966
字号
    // ...any key
    if (devExt->MasterKey != NULL)
        {
        SecZeroMemory(
                    devExt->MasterKey,
                    (devExt->MasterKeyLength / 8)
                    );
        ExFreePool(devExt->MasterKey);
        devExt->MasterKey = NULL;
        }
    if (devExt->MasterKeyASCII != NULL)
        {
        SecZeroMemory(
                    devExt->MasterKeyASCII,
                    ((devExt->MasterKeyLength / 8) * 2)
                    );
        ExFreePool(devExt->MasterKeyASCII);
        devExt->MasterKeyASCII = NULL;
        }
    devExt->MasterKeyLength = 0;

    // ...any ESSIV key
    if (devExt->ESSIVKey != NULL)
        {
        SecZeroMemory(
                    devExt->ESSIVKey,
                    (devExt->ESSIVKeyLength / 8)
                    );
        ExFreePool(devExt->ESSIVKey);
        devExt->ESSIVKey = NULL;
        }
    if (devExt->ESSIVKeyASCII != NULL)
        {
        SecZeroMemory(
                    devExt->ESSIVKeyASCII,
                    ((devExt->ESSIVKeyLength / 8) * 2)
                    );
        ExFreePool(devExt->ESSIVKeyASCII);
        devExt->ESSIVKeyASCII = NULL;
        }
    devExt->ESSIVKeyLength = 0;


    // ...any volume IV
    if (devExt->VolumeIV != NULL)
        {
        SecZeroMemory(
                    devExt->VolumeIV,
                    (devExt->VolumeIVLength / 8)
                    );
        ExFreePool(devExt->VolumeIV);
        devExt->VolumeIV = NULL;
        }
    devExt->VolumeIVLength = 0;

    // ...any volume flags
    devExt->VolumeFlags = 0;

    // ...any sector IV generation method
    devExt->SectorIVGenMethod = SCTRIVGEN_UNKNOWN;


    // ...any metadata
    if (devExt->MetaData != NULL)
        {
        SecZeroMemory(
                    devExt->MetaData,
                    devExt->MetaDataLength
                    );
        ExFreePool(devExt->MetaData);
        devExt->MetaData = NULL;
        }
    devExt->MetaDataLength = 0;


    DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("OK.\n"));   

    DEBUGOUTMAINDRV(DEBUGLEV_EXIT, ("OverwriteDeviceSensitive\n"));
    
    return status;
}


// =========================================================================
// Destroy the specified device object, regardless of it's state
// Note: This can be called to destroy *either* the main device, *or* a
//       disk device
// Returns: The next device object
PDEVICE_OBJECT
DestroyDevice(
    IN PDEVICE_OBJECT devObj    
)
{
    NTSTATUS status;
    PDEVICE_EXTENSION devExt;
    PDEVICE_OBJECT nextDevObj;

    DEBUGOUTMAINDRV(DEBUGLEV_ENTER, ("DestroyDevice\n"));
        
    nextDevObj = devObj->NextDevice;
    
    devExt = (PDEVICE_EXTENSION)devObj->DeviceExtension;    

    // Signal the device's thread to terminate, and wait until it does.
    devExt->TerminateThread = TRUE;
    CancelAllQueuedIRPs(devObj);
    if (devExt->ThreadObject != NULL) 
        {    
        // Make sure the thread wakes up, so it can see that it should terminate
        KeReleaseSemaphore(&devExt->IRPQueueSemaphore,
                            0,  // No priority boost
                            1,  // Increment semaphore by 1
                            TRUE );// WaitForXxx after this call

        // Wait for the thread to terminate
        KeWaitForSingleObject(devExt->ThreadObject,
                            Executive,
                            KernelMode,
                            FALSE,
                            NULL );

        DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Wait completed; thread terminated.\n"));
        
        // Release the thread object ref
        DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Dereferencing ThreadObject...\n"));
        ObDereferenceObject(devExt->ThreadObject);
        devExt->ThreadObject = NULL;
        }
    

    // Main device specific
    if (devExt->IsMainDevice) 
        {
        // Tear down any symbolic device name link
        DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Tearing down main device specific...\n"));
        if (devExt->zzSymbolicLinkName.Buffer != NULL)
            {
            DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Tearing down symbolic device link...\n"));
            DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("About to delete symlink: %ls\n", devExt->zzSymbolicLinkName.Buffer));
            status = IoDeleteSymbolicLink(&devExt->zzSymbolicLinkName);
            if (!(NT_SUCCESS(status)))
                {
                DEBUGOUTMAINDRV(DEBUGLEV_ERROR, ("Status NOT OK\n"));
                }
            
            DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Freeing off symlink unicode buffer...\n")); 
            SecZeroMemory(
                          devExt->zzSymbolicLinkName.Buffer,
                          sizeof(devExt->zzSymbolicLinkName.MaximumLength)
                         );
            ExFreePool(devExt->zzSymbolicLinkName.Buffer);
            devExt->zzSymbolicLinkName.Buffer = NULL;
            DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("OK.\n"));   
            }
        }

    
    // Tear down any device name
    if (devExt->zzDeviceName.Buffer != NULL)
        {
        DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Freeing off devname unicode buffer...\n"));
        SecZeroMemory(
                      devExt->zzDeviceName.Buffer,
                      sizeof(devExt->zzDeviceName.MaximumLength)
                     );
        ExFreePool(devExt->zzDeviceName.Buffer);
        devExt->zzDeviceName.Buffer = NULL;
        }
    

    DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("OK.\n"));   

    // Nuke any disk-related information
    OverwriteDeviceSensitive(devObj);
    
    // Belt & braces - overwrite the whole device extension
    DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Overwriting device extension memory...\n"));   
    SecZeroMemory(devExt, sizeof(DEVICE_EXTENSION));
    DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("OK.\n"));   

    // Get rid of the device object...
    DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Deleting device...\n"));   
    IoDeleteDevice(devObj);
    DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("OK.\n"));   
    

    DEBUGOUTMAINDRV(DEBUGLEV_EXIT, ("DestroyDevice\n"));
    
    return nextDevObj;
}


// =========================================================================
// Handle create IRPs
NTSTATUS
FreeOTFE_MF_DispatchCreate(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
)
{
    DEBUGOUTMAINDRV(DEBUGLEV_ENTER, ("FreeOTFE_MF_DispatchCreate\n"));
    
    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = FILE_OPENED;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);

    DEBUGOUTMAINDRV(DEBUGLEV_EXIT, ("FreeOTFE_MF_DispatchCreate\n"));
    
    return STATUS_SUCCESS;
}


// =========================================================================
// Handle close IRPs
NTSTATUS
FreeOTFE_MF_DispatchClose(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
)
{
    DEBUGOUTMAINDRV(DEBUGLEV_ENTER, ("FreeOTFE_MF_DispatchClose\n"));
    
    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = FILE_OPENED;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);

    DEBUGOUTMAINDRV(DEBUGLEV_EXIT, ("FreeOTFE_MF_DispatchClose\n"));
    
    return STATUS_SUCCESS;
}


// =========================================================================
// Handle device control IRPs
NTSTATUS
FreeOTFE_MF_DispatchDeviceControl(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
)
{
    PDEVICE_EXTENSION devExt;
    PIO_STACK_LOCATION irpSp;
    NTSTATUS status;
    PDEVICE_OBJECT queueDeviceObject = NULL;
    
    DEBUGOUTMAINDRV(DEBUGLEV_ENTER, ("FreeOTFE_MF_DispatchDeviceControl\n"));
    
    
    devExt = DeviceObject->DeviceExtension;
    irpSp = IoGetCurrentIrpStackLocation(Irp);

    status = STATUS_SUCCESS;
    Irp->IoStatus.Information = 0;


    // If the request is to be queued for later processing, set "queueDeviceObject" to the
    // device object which is to have the IRP added to it's thread queue.
    // Otherwise:
    //   On success, set "status" to a success code, and "Irp->IoStatus.Information" to the
    //   number of bytes being sent back (returned) to the caller
    //   On failure, set "status" to a failure code; "Irp->IoStatus.Information" will be set
    //   to 0 before it's completed


    switch (irpSp->Parameters.DeviceIoControl.IoControlCode)
        {
        case IOCTL_FREEOTFE_VERSION:
            {
            status = IOCTL_FreeOTFEIOCTL_Version(DeviceObject, Irp);
            break;
            }


        case IOCTL_FREEOTFE_CREATE:
            {
            status = IOCTL_FreeOTFEIOCTL_Create(DeviceObject, Irp);
            break;
            }
            

        case IOCTL_FREEOTFE_DESTROY:
            {
            status = IOCTL_FreeOTFEIOCTL_Destroy(DeviceObject, Irp);
            break;
            }


        case IOCTL_FREEOTFE_GET_RAW:
            {
            status = IOCTL_FreeOTFEIOCTL_GetRaw(DeviceObject, Irp);
            break;
            }


        case IOCTL_FREEOTFE_SET_RAW:
            {
            status = IOCTL_FreeOTFEIOCTL_SetRaw(DeviceObject, Irp);
            break;
            }

        case IOCTL_FREEOTFE_DERIVE_KEY:
            {
            status = IOCTL_FreeOTFEIOCTL_DeriveKey(DeviceObject, Irp);
            break;
            }

        case IOCTL_FREEOTFE_GENERATE_MAC:
            {
            status = IOCTL_FreeOTFEIOCTL_GenerateMAC(DeviceObject, Irp);
            break;
            }

        case IOCTL_FREEOTFE_MOUNT:
            {
            status = IOCTL_FreeOTFEIOCTL_Mount(
                                               DeviceObject,
                                               Irp,
                                               &queueDeviceObject
                                              );
            break;
            }


        case IOCTL_FREEOTFE_DOS_MOUNTPOINT_CREATE:
            {
            status = IOCTL_FreeOTFEIOCTL_DOSMountpointCreate(DeviceObject, Irp);
            break;
            }

        case IOCTL_FREEOTFE_DOS_MOUNTPOINT_DELETE:
            {
            status = IOCTL_FreeOTFEIOCTL_DOSMountpointDelete(DeviceObject, Irp);
            break;
            }

        case IOCTL_FREEOTFE_LDREU:
            {
            status = IOCTL_FreeOTFEIOCTL_LDREU(DeviceObject, Irp);
            break;
            }

        case IOCTL_FREEOTFE_DISMOUNT:
            {
            status = IOCTL_FreeOTFEIOCTL_Dismount(
                                                  DeviceObject,
                                                  Irp,
                                                  &queueDeviceObject
                                                 );
            break;
            }


        case IOCTL_FREEOTFE_GET_DISK_DEVICE_COUNT:
            {
            status = IOCTL_FreeOTFEIOCTL_GetDiskDeviceCount(DeviceObject, Irp);
            break;
            }


        case IOCTL_FREEOTFE_GET_DISK_DEVICE_LIST:
            {
            status = IOCTL_FreeOTFEIOCTL_GetDiskDeviceList(DeviceObject, Irp);
            break;
            }


        case IOCTL_FREEOTFE_GET_DISK_DEVICE_STATUS:
            {
            status = IOCTL_FreeOTFEIOCTL_GetDiskDeviceStatus(DeviceObject, Irp);        
            break;
            }

        case IOCTL_FREEOTFE_GET_DISK_DEVICE_METADATA:
            {
            status = IOCTL_FreeOTFEIOCTL_GetDiskDeviceMetaData(DeviceObject, Irp);        
            break;
            }


        //  --------------------------------------------------------------
        //  -- MS Windows standard disk IOCTL handling below this point --
        //  --------------------------------------------------------------
        
       
        case IOCTL_DISK_FORMAT_TRACKS:
        case IOCTL_DISK_FORMAT_TRACKS_EX:
            {
            status = IOCTL_Std_DiskFormatTracks(DeviceObject, Irp);
            break;
            }


        case IOCTL_DISK_IS_WRITABLE:
            {

⌨️ 快捷键说明

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