|
|
1.1.1.3 root 1: /* The source code contained in this file has been derived from the source code
2: of Encryption for the Masses 2.02a by Paul Le Roux. Modifications and
3: additions to that source code contained in this file are Copyright (c) 2004
1.1.1.4 ! root 4: TrueCrypt Foundation and Copyright (c) 2004 TrueCrypt Team. Unmodified
1.1.1.3 root 5: parts are Copyright (c) 1998-99 Paul Le Roux. This is a TrueCrypt Foundation
6: release. Please see the file license.txt for full license details. */
1.1 root 7:
8: #include "TCdefs.h"
9:
10: #include "crypto.h"
11: #include "fat.h"
12: #include "format.h"
13: #include "volumes.h"
14: #include "password.h"
15: #include "apidrvr.h"
16: #include "dlgcode.h"
17: #include "pkcs5.h"
18: #include "endian.h"
1.1.1.4 ! root 19: #include "resource.h"
1.1 root 20:
21: #include <io.h>
22:
23: void
24: VerifyPasswordAndUpdate (HWND hwndDlg, HWND hButton, HWND hPassword,
25: HWND hVerify, char *szPassword,
26: char *szVerify)
27: {
28: char szTmp1[MAX_PASSWORD + 1];
29: char szTmp2[MAX_PASSWORD + 1];
30: int k = GetWindowTextLength (hPassword);
31: BOOL bEnable = FALSE;
32:
33: if (hwndDlg); /* Remove warning */
34:
35: GetWindowText (hPassword, szTmp1, sizeof (szTmp1));
36: GetWindowText (hVerify, szTmp2, sizeof (szTmp2));
37:
38: if (strcmp (szTmp1, szTmp2) != 0)
39: bEnable = FALSE;
40: else
41: {
42: if (k >= MIN_PASSWORD)
43: bEnable = TRUE;
44: else
45: bEnable = FALSE;
46: }
47:
48: if (szPassword != NULL)
49: memcpy (szPassword, szTmp1, sizeof (szTmp1));
50:
51: if (szVerify != NULL)
52: memcpy (szVerify, szTmp2, sizeof (szTmp2));
53:
54: burn (szTmp1, sizeof (szTmp1));
55: burn (szTmp2, sizeof (szTmp2));
56:
57: EnableWindow (hButton, bEnable);
58: }
59:
60: int
1.1.1.4 ! root 61: ChangePwd (char *lpszVolume, char *lpszOldPassword, char *lpszPassword, int pkcs5, HWND hwndDlg)
1.1 root 62: {
63: int nDosLinkCreated = 0, nStatus;
64: char szDiskFile[TC_MAX_PATH], szCFDevice[TC_MAX_PATH];
65: char szDosDevice[TC_MAX_PATH];
1.1.1.4 ! root 66: char buffer[HEADER_SIZE], bufferHiddenVolume[HEADER_SIZE];
1.1 root 67: PCRYPTO_INFO cryptoInfo = NULL, ci = NULL;
68: void *dev = INVALID_HANDLE_VALUE;
69: OPEN_TEST_STRUCT driver;
70: diskio_f write, read;
71: DWORD dwError;
72: BOOL bDevice;
1.1.1.4 ! root 73: unsigned __int64 volSize = 0;
! 74: FILETIME ftCreationTime;
! 75: FILETIME ftLastWriteTime;
! 76: FILETIME ftLastAccessTime;
! 77: BOOL bTimeStampValid = FALSE;
1.1 root 78:
79: if (Randinit ()) return 1;
80:
81: CreateFullVolumePath (szDiskFile, lpszVolume, &bDevice);
82:
1.1.1.4 ! root 83: write = (diskio_f) _lwrite;
! 84: read = (diskio_f) _lread;
! 85:
! 86: if (bDevice == FALSE)
! 87: {
! 88: strcpy (szCFDevice, szDiskFile);
! 89: }
! 90: else
1.1 root 91: {
1.1.1.4 ! root 92: nDosLinkCreated = FakeDosNameForDevice (szDiskFile, szDosDevice, szCFDevice, FALSE);
! 93: if (nDosLinkCreated != 0)
! 94: {
! 95: return nDosLinkCreated;
! 96: }
! 97: }
1.1 root 98:
1.1.1.4 ! root 99: dev = CreateFile (szCFDevice, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
! 100:
! 101: if (bDevice == TRUE)
! 102: {
! 103: /* necessary to determine the hidden volume header offset */
! 104:
! 105: if (dev == INVALID_HANDLE_VALUE)
1.1 root 106: {
1.1.1.4 ! root 107: return ERR_OS_ERROR;
1.1 root 108: }
109: else
110: {
1.1.1.4 ! root 111: DISK_GEOMETRY driveInfo;
! 112: DWORD dwResult;
! 113: int nStatus;
! 114: BOOL bResult;
! 115:
! 116: bResult = DeviceIoControl (dev, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
! 117: &driveInfo, sizeof (driveInfo), &dwResult, NULL);
! 118:
! 119: if (driveInfo.MediaType == FixedMedia)
1.1 root 120: {
1.1.1.4 ! root 121: PARTITION_INFORMATION diskInfo;
! 122:
! 123: bResult = DeviceIoControl (dev, IOCTL_DISK_GET_PARTITION_INFO, NULL, 0,
! 124: &diskInfo, sizeof (diskInfo), &dwResult, NULL);
! 125:
! 126: if (bResult == TRUE)
! 127: {
! 128: volSize = diskInfo.PartitionLength.QuadPart;
! 129:
! 130: if (volSize == 0)
! 131: {
! 132: CloseHandle (dev);
! 133: return ERR_VOL_SIZE_WRONG;
! 134: }
! 135: }
! 136: else
! 137: {
! 138: CloseHandle (dev);
! 139: return ERR_OS_ERROR;
! 140: }
! 141: }
! 142: else
! 143: {
! 144: volSize = driveInfo.Cylinders.QuadPart * driveInfo.BytesPerSector *
! 145: driveInfo.SectorsPerTrack * driveInfo.TracksPerCylinder;
1.1 root 146: }
147: }
148: }
1.1.1.4 ! root 149:
! 150: if (dev == INVALID_HANDLE_VALUE) return ERR_OS_ERROR;
! 151:
! 152: WaitCursor ();
! 153:
! 154: if (!bDevice)
1.1 root 155: {
1.1.1.4 ! root 156: /* Remember the container modification/creation date and time, (used to reset file date and time of
! 157: file-hosted containers after password change (or attempt to), in order preserve plausible deniability
! 158: of hidden volumes (last password change time is stored in the volume header). */
1.1 root 159:
1.1.1.4 ! root 160: if (GetFileTime ((HANDLE) dev, &ftCreationTime, &ftLastAccessTime, &ftLastWriteTime) == 0)
1.1 root 161: {
1.1.1.4 ! root 162: bTimeStampValid = FALSE;
! 163: MessageBox (hwndDlg, getstr (IDS_GETFILETIME_FAILED_PW), "TrueCrypt", MB_OK | MB_ICONEXCLAMATION);
1.1 root 164: }
1.1.1.4 ! root 165: else
! 166: bTimeStampValid = TRUE;
1.1 root 167: }
168:
1.1.1.4 ! root 169: /* Read in volume header */
! 170:
! 171: nStatus = (*read) ((HFILE) dev, buffer, sizeof (buffer));
! 172: if (nStatus != sizeof (buffer))
1.1 root 173: {
1.1.1.4 ! root 174: nStatus = ERR_VOL_SIZE_WRONG;
! 175: goto error;
1.1 root 176: }
177:
178:
1.1.1.4 ! root 179: /* Read in possible hidden volume header */
1.1 root 180:
1.1.1.4 ! root 181: if (!SeekHiddenVolHeader ((HFILE) dev, volSize, bDevice))
! 182: return ERR_VOL_SEEKING;
1.1 root 183:
1.1.1.4 ! root 184: nStatus = (*read) ((HFILE) dev, bufferHiddenVolume, sizeof (bufferHiddenVolume));
! 185: if (nStatus != sizeof (bufferHiddenVolume))
1.1 root 186: {
187: nStatus = ERR_VOL_SIZE_WRONG;
188: goto error;
189: }
190:
191:
1.1.1.4 ! root 192: /* Try to decrypt either of the headers */
! 193:
! 194: nStatus = VolumeReadHeader (buffer, bufferHiddenVolume, lpszOldPassword, &cryptoInfo);
1.1 root 195: if (nStatus != 0)
196: {
197: cryptoInfo = NULL;
198: goto error;
199: }
200:
201: /* Change password now */
202:
1.1.1.4 ! root 203: if (cryptoInfo->hiddenVolume)
! 204: {
! 205: if (!SeekHiddenVolHeader ((HFILE) dev, volSize, bDevice))
! 206: return ERR_VOL_SEEKING;
! 207: }
! 208: else
1.1 root 209: {
210: nStatus = _llseek ((HFILE) dev, 0, FILE_BEGIN);
211:
212: if (nStatus != 0)
213: {
214: nStatus = ERR_VOL_SEEKING;
215: goto error;
216: }
217: }
218:
1.1.1.4 ! root 219: // Change PRF if requested by user
1.1.1.3 root 220: if (pkcs5 != 0)
221: cryptoInfo->pkcs5 = pkcs5;
222:
1.1.1.4 ! root 223: VolumeWriteHeader (cryptoInfo->hiddenVolume ? buffer : bufferHiddenVolume,
! 224: cryptoInfo->ea,
1.1 root 225: lpszPassword,
226: cryptoInfo->pkcs5,
227: cryptoInfo->master_decrypted_key,
228: cryptoInfo->volume_creation_time,
1.1.1.4 ! root 229: &ci,
! 230: cryptoInfo->hiddenVolume ? cryptoInfo->hiddenVolumeSize : 0);
1.1 root 231:
232: crypto_close (ci);
233:
234: /* Write out new encrypted key + key check */
235:
1.1.1.4 ! root 236: nStatus = (*write) ((HFILE) dev, cryptoInfo->hiddenVolume ? buffer : bufferHiddenVolume, HEADER_SIZE);
! 237:
! 238: if (nStatus != HEADER_SIZE)
1.1 root 239: {
240: nStatus = ERR_VOL_WRITING;
241: goto error;
242: }
243:
244: /* That's it done... */
1.1.1.4 ! root 245:
1.1 root 246: nStatus = 0;
247:
248: error:
249:
250: burn (buffer, sizeof (buffer));
1.1.1.4 ! root 251: burn (bufferHiddenVolume, sizeof (bufferHiddenVolume));
1.1 root 252:
253: if (cryptoInfo != NULL)
254: crypto_close (cryptoInfo);
255:
256: dwError = GetLastError ();
257:
1.1.1.4 ! root 258: if (bTimeStampValid)
1.1 root 259: {
1.1.1.4 ! root 260: // Restore the container timestamp (to preserve plausible deniability of possible hidden volume).
! 261: if (SetFileTime (dev, &ftCreationTime, &ftLastAccessTime, &ftLastWriteTime) == 0)
! 262: MessageBox (hwndDlg, getstr (IDS_SETFILETIME_FAILED_PW), "TrueCrypt", MB_OK | MB_ICONEXCLAMATION);
! 263: }
! 264:
! 265: CloseHandle ((HANDLE) dev);
1.1 root 266:
1.1.1.4 ! root 267: if (bDevice == TRUE && nDosLinkCreated != 0)
! 268: {
! 269: int x = RemoveFakeDosName (szDiskFile, szDosDevice);
! 270: if (x != 0)
1.1 root 271: {
1.1.1.4 ! root 272: dwError = GetLastError ();
! 273: nStatus = x;
1.1 root 274: }
275: }
276:
277: SetLastError (dwError);
278:
279: NormalCursor ();
280:
281: return nStatus;
282: }
1.1.1.4 ! root 283:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.