/[winpt]/trunk/Src/wptFileDisk.c
ViewVC logotype

Annotation of /trunk/Src/wptFileDisk.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 36 - (hide annotations)
Thu Oct 27 15:25:13 2005 UTC (19 years, 4 months ago) by werner
File MIME type: text/plain
File size: 9654 byte(s)
First set of changes to use autotools for building.
1 werner 36 /*
2     Control program for a virtual disk driver for Windows NT/2000/XP.
3     Copyright (C) 1999, 2000, 2001, 2002 Bo BrantĂ©n.
4    
5     Heavily modified for the use with Cryptdisk by Timo Schulz
6     (C) 2004 Timo Schulz
7    
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #ifdef HAVE_CONFIG_H
22     #include <config.h>
23     #endif
24    
25     #include <windows.h>
26     #include <windows.h>
27     #include <winioctl.h>
28     #include <stdio.h>
29     #include <sys/types.h>
30    
31     #include "openpgp.h"
32    
33     /* remember to change the values in wptErrors.h also */
34     #define CDISK_ERR_LOCK 201
35     #define CDISK_ERR_MOUNT 202
36     #define CDISK_ERR_UMOUNT 203
37     #define CDISK_ERR_OPEN 204
38     #define CDISK_ERR_BUSY 205
39     #define CDISK_ERR_QUERY 206
40    
41     #define MAX_KEYS 64
42     #define MAX_KEYHASH 32
43     #define MAX_KEY_LENGTH 50
44     #define CCVERSION 0x04
45    
46     #define DEVICE_BASE_NAME "\\FileDisk"
47     #define DEVICE_DIR_NAME "\\Device" DEVICE_BASE_NAME
48     #define DEVICE_NAME_PREFIX DEVICE_DIR_NAME DEVICE_BASE_NAME
49    
50     #define FILE_DEVICE_FILE_DISK 0x8000
51    
52     #define IOCTL_FILE_DISK_OPEN_FILE CTL_CODE(FILE_DEVICE_FILE_DISK, 0x800, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
53     #define IOCTL_FILE_DISK_CLOSE_FILE CTL_CODE(FILE_DEVICE_FILE_DISK, 0x801, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
54     #define IOCTL_FILE_DISK_QUERY_FILE CTL_CODE(FILE_DEVICE_FILE_DISK, 0x802, METHOD_BUFFERED, FILE_READ_ACCESS)
55    
56     typedef struct _OPEN_FILE_INFORMATION {
57     USHORT version;
58     LARGE_INTEGER FileSize;
59     BOOLEAN ReadOnly;
60     USHORT KeyType; //0 None 1 2Fish 2 AES256
61     USHORT KeyNum;
62     USHORT KeyLength;
63     USHORT FileNameLength;
64     UCHAR Key[MAX_KEYS][MAX_KEYHASH];
65     UCHAR DriveLetter;
66     UCHAR FileName[1];
67     } OPEN_FILE_INFORMATION, *POPEN_FILE_INFORMATION;
68    
69     static void
70     print_lasterr (char * prefix)
71     {
72     LPVOID msg;
73    
74     FormatMessage(
75     FORMAT_MESSAGE_ALLOCATE_BUFFER |
76     FORMAT_MESSAGE_FROM_SYSTEM |
77     FORMAT_MESSAGE_IGNORE_INSERTS,
78     NULL,
79     GetLastError(),
80     0,
81     (LPTSTR) &msg,
82     0,
83     NULL
84     );
85     MessageBox (NULL, (LPTSTR)msg, prefix, MB_ICONERROR|MB_OK);
86     LocalFree (msg);
87     }
88    
89    
90     static void
91     add_key (POPEN_FILE_INFORMATION ka, const char * key, size_t len)
92     {
93     gpg_md_t md;
94    
95     if (ka->KeyNum >= MAX_KEYS)
96     return;
97    
98     switch (ka->KeyType) {
99     case 1: /* 2fish */
100     md = gpg_md_open (MD_RMD160);
101     ka->KeyLength=20;
102     break;
103     case 2: /* AES256 */
104     md = gpg_md_open (MD_SHA512);
105     ka->KeyLength=32;
106     break;
107     case 3: /* AES128 */
108     md = gpg_md_open (MD_SHA256);
109     ka->KeyLength=16;
110     break;
111     case 4: /* AES192 */
112     ka->KeyLength=24;
113     md = gpg_md_open (MD_SHA384);
114     break;
115     }
116     gpg_md_write (md, (char *)key, len);
117     gpg_md_final (md);
118     memcpy (ka->Key[ka->KeyNum++], gpg_md_read (md), ka->KeyLength);
119     gpg_md_close (md);
120     }
121    
122    
123     int
124     filedisk_mount (int DeviceNumber,
125     POPEN_FILE_INFORMATION ofi,
126     char drivelet,
127     BOOLEAN CdImage)
128     {
129     char VolumeName[] = "\\\\.\\ :";
130     char DeviceName[255];
131     HANDLE Device;
132     DWORD BytesReturned;
133    
134     VolumeName[4] = drivelet;
135    
136     Device = CreateFile (VolumeName,
137     GENERIC_READ | GENERIC_WRITE,
138     FILE_SHARE_READ | FILE_SHARE_WRITE,
139     NULL,
140     OPEN_EXISTING,
141     FILE_FLAG_NO_BUFFERING,
142     NULL);
143     if (Device != INVALID_HANDLE_VALUE) {
144     SetLastError (ERROR_BUSY);
145     print_lasterr (&VolumeName[4]);
146     return CDISK_ERR_BUSY;
147     }
148    
149     if (CdImage)
150     sprintf (DeviceName, DEVICE_NAME_PREFIX "Cd" "%u", DeviceNumber);
151     else
152     sprintf (DeviceName, DEVICE_NAME_PREFIX "%u", DeviceNumber);
153    
154     if (!DefineDosDevice (DDD_RAW_TARGET_PATH,
155     &VolumeName[4],
156     DeviceName)) {
157     print_lasterr (&VolumeName[4]);
158     return CDISK_ERR_OPEN;
159     }
160    
161     Device = CreateFile (VolumeName,
162     GENERIC_READ | GENERIC_WRITE,
163     FILE_SHARE_READ | FILE_SHARE_WRITE,
164     NULL,
165     OPEN_EXISTING,
166     FILE_FLAG_NO_BUFFERING,
167     NULL);
168    
169     if (Device == INVALID_HANDLE_VALUE) {
170     print_lasterr (&VolumeName[4]);
171     DefineDosDevice (DDD_REMOVE_DEFINITION, &VolumeName[4], NULL);
172     return CDISK_ERR_OPEN;
173     }
174    
175     if (!DeviceIoControl (Device,
176     IOCTL_FILE_DISK_OPEN_FILE,
177     ofi,
178     sizeof(OPEN_FILE_INFORMATION) + ofi->FileNameLength - 1,
179     NULL, 0, &BytesReturned, NULL)) {
180     print_lasterr ("Cryptdisk Error");
181     DefineDosDevice (DDD_REMOVE_DEFINITION, &VolumeName[4], NULL);
182     return CDISK_ERR_MOUNT;
183     }
184    
185     return 0;
186     }
187    
188    
189     static int
190     filedisk_unmount2 (char drivelet, int flags, int forced)
191     {
192     char VolumeName[60] = "\\\\.\\ :";
193     HANDLE Device;
194     DWORD BytesReturned;
195    
196     if (!flags)
197     VolumeName[4] = drivelet;
198     else if (flags & 1)
199     sprintf(VolumeName,"\\\\.\\FileDisk%d", drivelet);
200     else if (flags & 2)
201     sprintf(VolumeName,"\\\\.\\FileDiskCd%d",drivelet);
202    
203     Device = CreateFile (VolumeName,
204     GENERIC_READ | GENERIC_WRITE,
205     FILE_SHARE_READ | FILE_SHARE_WRITE,
206     NULL,
207     OPEN_EXISTING,
208     FILE_FLAG_NO_BUFFERING,
209     NULL);
210     if (Device == INVALID_HANDLE_VALUE) {
211     if (forced)
212     print_lasterr(&VolumeName[4]);
213     return CDISK_ERR_OPEN;
214     }
215    
216     if (!DeviceIoControl (Device,
217     FSCTL_LOCK_VOLUME,
218     NULL, 0, NULL, 0,
219     &BytesReturned, NULL)) {
220     if (forced) {
221     print_lasterr(&VolumeName[4]);
222     return CDISK_ERR_LOCK;
223     }
224     }
225    
226     if (!DeviceIoControl (Device,
227     IOCTL_FILE_DISK_CLOSE_FILE,
228     NULL, 0, NULL, 0,
229     &BytesReturned, NULL )) {
230     if (forced)
231     print_lasterr ("Cryptdisk Error");
232     return CDISK_ERR_UMOUNT;
233     }
234    
235     if (!DeviceIoControl (
236     Device, FSCTL_DISMOUNT_VOLUME,
237     NULL, 0, NULL, 0,
238     &BytesReturned, NULL)) {
239     if (forced)
240     print_lasterr (&VolumeName[4]);
241     return CDISK_ERR_UMOUNT;
242     }
243    
244     if (!DeviceIoControl (Device,
245     FSCTL_UNLOCK_VOLUME,
246     NULL, 0, NULL, 0,
247     &BytesReturned, NULL)) {
248     if (forced) {
249     print_lasterr (&VolumeName[4]);
250     return CDISK_ERR_LOCK;
251     }
252     }
253    
254     CloseHandle (Device);
255    
256     if (!DefineDosDevice (
257     DDD_REMOVE_DEFINITION,
258     &VolumeName[4], NULL)) {
259     if (forced)
260     print_lasterr (&VolumeName[4]);
261     return CDISK_ERR_UMOUNT;
262     }
263    
264     return 0;
265     }
266    
267     int
268     filedisk_unmount (char drivelet, int forced)
269     {
270     char i;
271    
272     if (drivelet) {
273     int rc = filedisk_unmount2 (drivelet, 0, forced);
274     if (rc)
275     print_lasterr ("Unmount´");
276     return rc;
277     }
278     for (i=0; i<20;i++) {
279     if (filedisk_unmount2 (i, 1, 0) == CDISK_ERR_OPEN)
280     break;
281     }
282     for (i=0; i<20;i++) {
283     if (filedisk_unmount2 (i, 2, 0) == CDISK_ERR_OPEN)
284     break;
285     }
286     return 0;
287     }
288    
289    
290     int
291     filedisk_status (char drivelet, LARGE_INTEGER * size, int * rdonly)
292     {
293     char VolumeName[] = "\\\\.\\ :";
294     HANDLE Device;
295     POPEN_FILE_INFORMATION ofi;
296     DWORD BytesReturned;
297    
298     VolumeName[4] = drivelet;
299    
300     Device = CreateFile (VolumeName,
301     GENERIC_READ,
302     FILE_SHARE_READ | FILE_SHARE_WRITE,
303     NULL,
304     OPEN_EXISTING,
305     FILE_FLAG_NO_BUFFERING,
306     NULL);
307    
308     if (Device == INVALID_HANDLE_VALUE) {
309     print_lasterr(&VolumeName[4]);
310     return CDISK_ERR_OPEN;
311     }
312    
313     ofi = malloc (sizeof(OPEN_FILE_INFORMATION) + MAX_PATH);
314    
315     if (!DeviceIoControl (Device,
316     IOCTL_FILE_DISK_QUERY_FILE,
317     NULL,
318     0,
319     ofi,
320     sizeof(OPEN_FILE_INFORMATION) + MAX_PATH,
321     &BytesReturned, NULL)) {
322     print_lasterr (&VolumeName[4]);
323     return CDISK_ERR_QUERY;
324     }
325    
326     if (BytesReturned < sizeof(OPEN_FILE_INFORMATION)) {
327     SetLastError (ERROR_INSUFFICIENT_BUFFER);
328     print_lasterr (&VolumeName[4]);
329     return CDISK_ERR_QUERY;
330     }
331    
332     if (size)
333     *size = ofi->FileSize;
334     if (rdonly)
335     *rdonly = ofi->ReadOnly;
336    
337     return 0;
338     }
339    
340    
341     POPEN_FILE_INFORMATION
342     filedisk_init_info (int devnum, char drivelet, const char * file,
343     int ro, int cd_img, unsigned long size, const char * key,
344     int keyalgo)
345     {
346     POPEN_FILE_INFORMATION pfi;
347    
348     pfi = calloc (1, sizeof(OPEN_FILE_INFORMATION) + strlen (file) + 7);
349    
350     pfi->version = CCVERSION;
351     if (file[0] == '\\') {
352     if (file[1] == '\\' && file[2] != '.') {
353     /* \\server\share\path\filedisk.img */
354     strcpy(pfi->FileName, "\\??\\UNC");
355     strcat(pfi->FileName, file + 1);
356     }
357     else /* \Device\Harddisk0\Partition1\path\filedisk.img */
358     strcpy(pfi->FileName, file);
359     }
360     else { // c:\path\filedisk.img
361     strcpy (pfi->FileName, "\\??\\");
362     strcat (pfi->FileName, file);
363     }
364    
365     pfi->FileNameLength = (USHORT) strlen (pfi->FileName);
366     pfi->ReadOnly = ro? TRUE : FALSE;
367     if (cd_img)
368     pfi->ReadOnly = TRUE;
369     if (size)
370     pfi->FileSize.QuadPart = size * 1024;
371     if (key) {
372     if (keyalgo < 1 || keyalgo > 4)
373     pfi->KeyType = 1;
374     else
375     pfi->KeyType = keyalgo;
376     add_key (pfi, key, strlen (key));
377     }
378    
379     return pfi;
380     }
381    
382    
383     void
384     filedisk_free_info (POPEN_FILE_INFORMATION pfi)
385     {
386     if (!pfi)
387     return;
388     free (pfi);
389     }
390    

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26