Some more cleanups I missed last time.
parent
bb3c2172b8
commit
0eb4cfbb42
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* decrypt.c - $EFS decryption routined. Part of the Linux-NTFS project.
|
||||
* decrypt.c - $EFS decryption routines. Part of the Linux-NTFS project.
|
||||
*
|
||||
* Copyright (c) 2005 Yuval Fledel
|
||||
* Copyright (c) 2005 Anton Altaparmakov
|
||||
|
|
|
@ -56,9 +56,9 @@ GEN_PRINTF(Vprintf, stderr, &opts.verbose, TRUE)
|
|||
GEN_PRINTF(Qprintf, stderr, &opts.quiet, FALSE)
|
||||
static GEN_PRINTF(Printf, stderr, NULL, FALSE)
|
||||
|
||||
static ntfschar EFS[5] = { const_cpu_to_le16('$'), const_cpu_to_le16('E'),
|
||||
const_cpu_to_le16('F'), const_cpu_to_le16('S'),
|
||||
const_cpu_to_le16('\0')
|
||||
static ntfschar EFS[5] = {
|
||||
const_cpu_to_le16('$'), const_cpu_to_le16('E'), const_cpu_to_le16('F'),
|
||||
const_cpu_to_le16('S'), const_cpu_to_le16('\0')
|
||||
};
|
||||
static const int EFS_name_length = 4;
|
||||
|
||||
|
@ -71,8 +71,8 @@ static const int EFS_name_length = 4;
|
|||
*/
|
||||
static void version(void)
|
||||
{
|
||||
Printf("\n%s v%s - Decrypt and concatenate files and print on the "
|
||||
"standard output.\n\n", EXEC_NAME, VERSION);
|
||||
Printf("\n%s v%s - Decrypt files and print on the standard output.\n\n",
|
||||
EXEC_NAME, VERSION);
|
||||
Printf("Copyright (c) 2005 Yuval Fledel\n");
|
||||
Printf("Copyright (c) 2005 Anton Altaparmakov\n");
|
||||
Printf("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home);
|
||||
|
@ -133,11 +133,11 @@ static int parse_options(int argc, char **argv)
|
|||
while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != (char)-1) {
|
||||
switch (c) {
|
||||
case 1: /* A non-option argument */
|
||||
if (!opts.device) {
|
||||
if (!opts.device)
|
||||
opts.device = argv[optind - 1];
|
||||
} else if (!opts.file) {
|
||||
else if (!opts.file)
|
||||
opts.file = argv[optind - 1];
|
||||
} else {
|
||||
else {
|
||||
Eprintf("You must specify exactly one file.\n");
|
||||
err++;
|
||||
}
|
||||
|
@ -151,8 +151,8 @@ static int parse_options(int argc, char **argv)
|
|||
break;
|
||||
case 'i':
|
||||
if (opts.inode != -1)
|
||||
Eprintf
|
||||
("You must specify exactly one inode.\n");
|
||||
Eprintf("You must specify exactly one "
|
||||
"inode.\n");
|
||||
else if (utils_parse_size(optarg, &opts.inode, FALSE))
|
||||
break;
|
||||
else
|
||||
|
@ -183,8 +183,8 @@ static int parse_options(int argc, char **argv)
|
|||
err++;
|
||||
|
||||
} else if (opts.file == NULL && opts.inode == -1) {
|
||||
Eprintf("You must specify a file or inode "
|
||||
"with the -i option.\n");
|
||||
Eprintf("You must specify a file or inode with the -i "
|
||||
"option.\n");
|
||||
err++;
|
||||
|
||||
} else if (opts.file != NULL && opts.inode != -1) {
|
||||
|
@ -194,7 +194,7 @@ static int parse_options(int argc, char **argv)
|
|||
|
||||
if (opts.quiet && opts.verbose) {
|
||||
Eprintf("You may not use --quiet and --verbose at the "
|
||||
"same time.\n");
|
||||
"same time.\n");
|
||||
err++;
|
||||
}
|
||||
}
|
||||
|
@ -210,7 +210,7 @@ static int parse_options(int argc, char **argv)
|
|||
/**
|
||||
* cat
|
||||
*/
|
||||
static int cat_decrypt(ntfs_inode * inode, decrypt_key * fek)
|
||||
static int cat_decrypt(ntfs_inode *inode, decrypt_key *fek)
|
||||
{
|
||||
int bufsize = 512;
|
||||
char *buffer;
|
||||
|
@ -221,14 +221,12 @@ static int cat_decrypt(ntfs_inode * inode, decrypt_key * fek)
|
|||
buffer = malloc(bufsize);
|
||||
if (!buffer)
|
||||
return 1;
|
||||
|
||||
attr = ntfs_attr_open(inode, AT_DATA, NULL, 0);
|
||||
if (!attr) {
|
||||
Eprintf("Cannot cat a directory.\n");
|
||||
free(buffer);
|
||||
return 1;
|
||||
}
|
||||
|
||||
total = attr->data_size;
|
||||
|
||||
// hack: make sure attr will not be commited to disk if you use this.
|
||||
|
@ -246,7 +244,6 @@ static int cat_decrypt(ntfs_inode * inode, decrypt_key * fek)
|
|||
}
|
||||
if (!bytes_read)
|
||||
break;
|
||||
|
||||
if ((i = decrypt_decrypt_sector(fek, buffer, offset)) <
|
||||
bytes_read) {
|
||||
perror("ERROR: Couldn't decrypt all data!");
|
||||
|
@ -256,7 +253,6 @@ static int cat_decrypt(ntfs_inode * inode, decrypt_key * fek)
|
|||
}
|
||||
if (bytes_read > total)
|
||||
bytes_read = total;
|
||||
|
||||
written = fwrite(buffer, 1, bytes_read, stdout);
|
||||
if (written != bytes_read) {
|
||||
perror("ERROR: Couldn't output all data!");
|
||||
|
@ -265,7 +261,6 @@ static int cat_decrypt(ntfs_inode * inode, decrypt_key * fek)
|
|||
offset += bytes_read;
|
||||
total -= bytes_read;
|
||||
}
|
||||
|
||||
ntfs_attr_close(attr);
|
||||
free(buffer);
|
||||
return 0;
|
||||
|
@ -290,13 +285,11 @@ static decrypt_key *get_fek(ntfs_inode * inode)
|
|||
perror("Error");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
efs_buffer = malloc(na->data_size);
|
||||
if (!efs_buffer) {
|
||||
perror("malloc failed");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (ntfs_attr_pread(na, 0, na->data_size, efs_buffer) !=
|
||||
na->data_size) {
|
||||
perror("ntfs_attr_pread failed");
|
||||
|
@ -305,31 +298,29 @@ static decrypt_key *get_fek(ntfs_inode * inode)
|
|||
}
|
||||
ntfs_attr_close(na);
|
||||
|
||||
/* init the CryptoAPI */
|
||||
/* Init the CryptoAPI. */
|
||||
if (!(session = decrypt_open())) {
|
||||
perror("Could not init the cryptoAPI.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* iterate through the DDFs & DRFs until you obtain a key */
|
||||
|
||||
ddf = efs_buffer + le32_to_cpu(*(u32 *)(efs_buffer + 0x40));
|
||||
ddf_count = le32_to_cpu(*(u32 *)ddf);
|
||||
/* Iterate through the DDFs & DRFs until you obtain a key. */
|
||||
ddf = efs_buffer + le32_to_cpu(*(u32*)(efs_buffer + 0x40));
|
||||
ddf_count = le32_to_cpu(*(u32*)ddf);
|
||||
|
||||
ddf = ddf + 0x04;
|
||||
for (i = 0; i < ddf_count; i++) {
|
||||
//Eprintf("ddf #%u.\n", i);
|
||||
if (*(u32 *) (ddf + 0x18))
|
||||
if (*(u32*)(ddf + 0x18))
|
||||
certificate = (ddf + 0x30 +
|
||||
le32_to_cpu(*(u32 *)(ddf + 0x18)));
|
||||
le32_to_cpu(*(u32*)(ddf + 0x18)));
|
||||
else
|
||||
certificate = (ddf + 0x30);
|
||||
|
||||
hash_size = (unsigned int)le32_to_cpu(*(u32 *)certificate);
|
||||
hash_size = (unsigned int)le32_to_cpu(*(u32*)certificate);
|
||||
hash_data = certificate + (unsigned int)
|
||||
le32_to_cpu(*(u32 *)(certificate + 0x04));
|
||||
fek_size = (unsigned int)le32_to_cpu(*(u32 *)(ddf + 0x08));
|
||||
fek_buf = ddf + (unsigned int)le32_to_cpu(*(u32 *)(ddf + 0x0c));
|
||||
le32_to_cpu(*(u32*)(certificate + 0x04));
|
||||
fek_size = (unsigned int)le32_to_cpu(*(u32*)(ddf + 0x08));
|
||||
fek_buf = ddf + (unsigned int)le32_to_cpu(*(u32*)(ddf + 0x0c));
|
||||
|
||||
if ((key = decrypt_user_key_open(session, hash_size,
|
||||
hash_data))) {
|
||||
|
@ -346,10 +337,9 @@ static decrypt_key *get_fek(ntfs_inode * inode)
|
|||
} else
|
||||
Eprintf("Could not open key.\n");
|
||||
|
||||
ddf = ddf + le32_to_cpu(*(u32 *)(ddf + 0x08)) +
|
||||
le32_to_cpu(*(u32 *)(ddf + 0x0c));
|
||||
ddf = ddf + le32_to_cpu(*(u32*)(ddf + 0x08)) +
|
||||
le32_to_cpu(*(u32*)(ddf + 0x0c));
|
||||
}
|
||||
|
||||
decrypt_close(session);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -371,7 +361,6 @@ int main(int argc, char *argv[])
|
|||
|
||||
if (!parse_options(argc, argv))
|
||||
return 1;
|
||||
|
||||
utils_set_locale();
|
||||
|
||||
//XXX quieten errors, temporarily
|
||||
|
@ -381,17 +370,14 @@ int main(int argc, char *argv[])
|
|||
perror("ERROR: couldn't mount volume");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (opts.inode != -1)
|
||||
inode = ntfs_inode_open(vol, opts.inode);
|
||||
else
|
||||
inode = ntfs_pathname_to_inode(vol, NULL, opts.file);
|
||||
|
||||
if (!inode) {
|
||||
perror("ERROR: Couldn't open inode");
|
||||
return 1;
|
||||
}
|
||||
|
||||
fek = get_fek(inode);
|
||||
if (fek) {
|
||||
result = cat_decrypt(inode, fek);
|
||||
|
@ -400,14 +386,7 @@ int main(int argc, char *argv[])
|
|||
Eprintf("Could not obtain FEK.\n");
|
||||
result = 1;
|
||||
}
|
||||
|
||||
ntfs_inode_close(inode);
|
||||
ntfs_umount(vol, FALSE);
|
||||
#if 0
|
||||
if (result)
|
||||
Printf("failed\n");
|
||||
else
|
||||
Printf("success\n");
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue