File: fvdewipekey.c

package info (click to toggle)
libfvde 20190104-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,884 kB
  • sloc: ansic: 206,296; sh: 6,111; makefile: 1,444; python: 487; yacc: 356; lex: 247; sed: 16
file content (323 lines) | stat: -rw-r--r-- 7,000 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * Decrypts a EncryptedRoot.plist.wipekey file
 *
 * Copyright (C) 2011-2019, Omar Choudary <choudary.omar@gmail.com>
 *                          Joachim Metz <joachim.metz@gmail.com>
 *
 * Refer to AUTHORS for acknowledgements.
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <common.h>
#include <file_stream.h>
#include <memory.h>
#include <system_string.h>
#include <types.h>

#if defined( HAVE_UNISTD_H )
#include <unistd.h>
#endif

#if defined( HAVE_STDLIB_H ) || defined( WINAPI )
#include <stdlib.h>
#endif

#include "fvdetools_getopt.h"
#include "fvdetools_libcerror.h"
#include "fvdetools_libclocale.h"
#include "fvdetools_libcnotify.h"
#include "fvdetools_libfvde.h"
#include "fvdetools_output.h"
#include "fvdetools_signal.h"
#include "fvdetools_unused.h"
#include "wipekey_handle.h"

wipekey_handle_t *fvdewipekey_wipekey_handle = NULL;
int fvdewipekey_abort                        = 0;

/* Prints the executable usage information
 */
void usage_fprint(
      FILE *stream )
{
	if( stream == NULL )
	{
		return;
	}
	fprintf( stream, "Use fvdewipkey to decrypt an EncryptedRoot.plist.wipekey file\n\n" );

	fprintf( stream, "Usage: fvdewipeky filename volume_key_data [ -hvV ]\n" );

	fprintf( stream, "\tfilename:        the name of the EncryptedRoot.plist.wipekey file\n" );
	fprintf( stream, "\tvolume_key_data: the volume key data formatted in base16\n\n" );

	fprintf( stream, "\t-h:     shows this help\n" );
	fprintf( stream, "\t-v:     verbose output to stderr\n" );
	fprintf( stream, "\t-V:     print version\n" );
}

/* Signal handler for fvdewipekey
 */
void fvdewipekey_signal_handler(
      fvdetools_signal_t signal FVDETOOLS_ATTRIBUTE_UNUSED )
{
	libcerror_error_t *error = NULL;
	static char *function   = "fvdewipekey_signal_handler";

	FVDETOOLS_UNREFERENCED_PARAMETER( signal )

	fvdewipekey_abort = 1;

	if( fvdewipekey_wipekey_handle != NULL )
	{
		if( wipekey_handle_signal_abort(
		     fvdewipekey_wipekey_handle,
		     &error ) != 1 )
		{
			libcnotify_printf(
			 "%s: unable to signal wipekey handle to abort.\n",
			 function );

			libcnotify_print_error_backtrace(
			 error );
			libcerror_error_free(
			 &error );
		}
	}
	/* Force stdin to close otherwise any function reading it will remain blocked
	 */
#if defined( WINAPI ) && !defined( __CYGWIN__ )
	if( _close(
	     0 ) != 0 )
#else
	if( close(
	     0 ) != 0 )
#endif
	{
		libcnotify_printf(
		 "%s: unable to close stdin.\n",
		 function );
	}
}

/* The main program
 */
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
int wmain( int argc, wchar_t * const argv[] )
#else
int main( int argc, char * const argv[] )
#endif
{
	libfvde_error_t *error                            = NULL;
	system_character_t *encrypted_root_plist_filename = NULL;
	system_character_t *volume_key_data               = NULL;
	char *program                                     = "fvdewipekey";
	system_integer_t option                           = 0;
	int result                                        = 0;
	int verbose                                       = 0;

	libcnotify_stream_set(
	 stderr,
	 NULL );
	libcnotify_verbose_set(
	 1 );

	if( libclocale_initialize(
             "fvdetools",
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to initialize locale values.\n" );

		goto on_error;
	}
        if( fvdetools_output_initialize(
             _IONBF,
             &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to initialize output settings.\n" );

		goto on_error;
	}
	fvdetools_output_version_fprint(
	 stdout,
	 program );

	while( ( option = fvdetools_getopt(
	                   argc,
	                   argv,
	                   _SYSTEM_STRING( "hvV" ) ) ) != (system_integer_t) -1 )
	{
		switch( option )
		{
			case (system_integer_t) '?':
			default:
				fprintf(
				 stderr,
				 "Invalid argument: %" PRIs_SYSTEM "\n",
				 argv[ optind - 1 ] );

				usage_fprint(
				 stdout );

				return( EXIT_FAILURE );

			case (system_integer_t) 'h':
				usage_fprint(
				 stdout );

				return( EXIT_SUCCESS );

			case (system_integer_t) 'v':
				verbose = 1;

				break;

			case (system_integer_t) 'V':
				fvdetools_output_copyright_fprint(
				 stdout );

				return( EXIT_SUCCESS );
		}
	}
	if( optind == argc )
	{
		fprintf(
		 stderr,
		 "Missing name of the EncryptedRoot.plist.wipekey file.\n" );

		usage_fprint(
		 stdout );

		return( EXIT_FAILURE );
	}
	encrypted_root_plist_filename = argv[ optind++ ];

	if( optind == argc )
	{
		fprintf(
		 stderr,
		 "Missing volume key data.\n" );

		usage_fprint(
		 stdout );

		return( EXIT_FAILURE );
	}
	volume_key_data = argv[ optind ];

	libcnotify_verbose_set(
	 verbose );
	libfvde_notify_set_stream(
	 stderr,
	 NULL );
	libfvde_notify_set_verbose(
	 verbose );

	if( wipekey_handle_initialize(
	     &fvdewipekey_wipekey_handle,
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to initialize wipekey handle.\n" );

		goto on_error;
	}
	if( wipekey_handle_set_volume_key_data(
	     fvdewipekey_wipekey_handle,
	     volume_key_data,
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to set volume key data.\n" );

		goto on_error;
	}
	result = wipekey_handle_open_input(
	          fvdewipekey_wipekey_handle,
	          encrypted_root_plist_filename,
	          &error );

	if( result == -1 )
	{
		fprintf(
		 stderr,
		 "Unable to open: %" PRIs_SYSTEM ".\n",
		 encrypted_root_plist_filename );

		goto on_error;
	}
	else if( result == 0 )
	{
		fprintf(
		 stderr,
		 "Unable to unlock EncryptedRoot.plist.wipekey.\n" );

		goto on_error;
	}
	if( wipekey_handle_wipekey_fprint(
	     fvdewipekey_wipekey_handle,
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to print decrypted EncryptedRoot.plist.wipekey.\n" );

		goto on_error;
	}
	if( wipekey_handle_close_input(
	     fvdewipekey_wipekey_handle,
	     &error ) != 0 )
	{
		fprintf(
		 stderr,
		 "Unable to close wipekey handle.\n" );

		goto on_error;
	}
	if( wipekey_handle_free(
	     &fvdewipekey_wipekey_handle,
	     &error ) != 1 )
	{
		fprintf(
		 stderr,
		 "Unable to free wipekey handle.\n" );

		goto on_error;
	}
	return( EXIT_SUCCESS );

on_error:
	if( error != NULL )
	{
		libcnotify_print_error_backtrace(
		 error );
		libcerror_error_free(
		 &error );
	}
	if( fvdewipekey_wipekey_handle != NULL )
	{
		wipekey_handle_free(
		 &fvdewipekey_wipekey_handle,
		 NULL );
	}
	return( EXIT_FAILURE );
}