Coverage Report

Created: 2025-12-05 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libbde/libbde/libbde_key.c
Line
Count
Source
1
/*
2
 * Key metadata entry functions
3
 *
4
 * Copyright (C) 2011-2025, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libbde_debug.h"
28
#include "libbde_definitions.h"
29
#include "libbde_key.h"
30
#include "libbde_libcerror.h"
31
#include "libbde_libcnotify.h"
32
#include "libbde_metadata_entry.h"
33
34
#include "bde_metadata.h"
35
36
/* Creates a key
37
 * Make sure the value key is referencing, is set to NULL
38
 * Returns 1 if successful or -1 on error
39
 */
40
int libbde_key_initialize(
41
     libbde_key_t **key,
42
     libcerror_error_t **error )
43
1.81k
{
44
1.81k
  static char *function = "libbde_key_initialize";
45
46
1.81k
  if( key == NULL )
47
0
  {
48
0
    libcerror_error_set(
49
0
     error,
50
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
51
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
52
0
     "%s: invalid key.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
1.81k
  if( *key != NULL )
58
0
  {
59
0
    libcerror_error_set(
60
0
     error,
61
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
62
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
63
0
     "%s: invalid key value already set.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
1.81k
  *key = memory_allocate_structure(
69
1.81k
          libbde_key_t );
70
71
1.81k
  if( *key == NULL )
72
0
  {
73
0
    libcerror_error_set(
74
0
     error,
75
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
76
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
77
0
     "%s: unable to create key.",
78
0
     function );
79
80
0
    goto on_error;
81
0
  }
82
1.81k
  if( memory_set(
83
1.81k
       *key,
84
1.81k
       0,
85
1.81k
       sizeof( libbde_key_t ) ) == NULL )
86
0
  {
87
0
    libcerror_error_set(
88
0
     error,
89
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
90
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
91
0
     "%s: unable to clear key.",
92
0
     function );
93
94
0
    goto on_error;
95
0
  }
96
1.81k
  return( 1 );
97
98
0
on_error:
99
0
  if( *key != NULL )
100
0
  {
101
0
    memory_free(
102
0
     *key );
103
104
0
    *key = NULL;
105
0
  }
106
0
  return( -1 );
107
1.81k
}
108
109
/* Frees a key
110
 * Returns 1 if successful or -1 on error
111
 */
112
int libbde_key_free(
113
     libbde_key_t **key,
114
     libcerror_error_t **error )
115
1.81k
{
116
1.81k
  static char *function = "libbde_key_free";
117
118
1.81k
  if( key == NULL )
119
0
  {
120
0
    libcerror_error_set(
121
0
     error,
122
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
123
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
124
0
     "%s: invalid key.",
125
0
     function );
126
127
0
    return( -1 );
128
0
  }
129
1.81k
  if( *key != NULL )
130
1.81k
  {
131
1.81k
    if( ( *key )->data != NULL )
132
1.81k
    {
133
1.81k
      memory_free(
134
1.81k
       ( *key )->data );
135
1.81k
    }
136
1.81k
    memory_free(
137
1.81k
     *key );
138
139
1.81k
    *key = NULL;
140
1.81k
  }
141
1.81k
  return( 1 );
142
1.81k
}
143
144
/* Reads a key from the metadata entry
145
 * Returns 1 if successful or -1 on error
146
 */
147
int libbde_key_read(
148
     libbde_key_t *key,
149
     libbde_metadata_entry_t *metadata_entry,
150
     libcerror_error_t **error )
151
1.81k
{
152
1.81k
  uint8_t *value_data    = NULL;
153
1.81k
  static char *function  = "libbde_key_read";
154
1.81k
  size_t value_data_size = 0;
155
156
1.81k
  if( key == NULL )
157
0
  {
158
0
    libcerror_error_set(
159
0
     error,
160
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
161
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
162
0
     "%s: invalid key.",
163
0
     function );
164
165
0
    return( -1 );
166
0
  }
167
1.81k
  if( metadata_entry == NULL )
168
0
  {
169
0
    libcerror_error_set(
170
0
     error,
171
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
172
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
173
0
     "%s: invalid metadata entry.",
174
0
     function );
175
176
0
    return( -1 );
177
0
  }
178
1.81k
  if( metadata_entry->value_data == NULL )
179
0
  {
180
0
    libcerror_error_set(
181
0
     error,
182
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
183
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
184
0
     "%s: invalid metadata entry - missing value data.",
185
0
     function );
186
187
0
    return( -1 );
188
0
  }
189
1.81k
  if( metadata_entry->value_type != LIBBDE_VALUE_TYPE_KEY )
190
0
  {
191
0
    libcerror_error_set(
192
0
     error,
193
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
194
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
195
0
     "%s: invalid metadata entry - unsupported value type: 0x%04" PRIx16 ".",
196
0
     function,
197
0
     metadata_entry->value_type );
198
199
0
    return( -1 );
200
0
  }
201
1.81k
  value_data      = metadata_entry->value_data;
202
1.81k
  value_data_size = metadata_entry->value_data_size;
203
204
1.81k
  if( ( value_data_size < sizeof( bde_metadata_entry_key_header_t ) )
205
1.81k
   || ( value_data_size > MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
206
3
  {
207
3
    libcerror_error_set(
208
3
     error,
209
3
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
210
3
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
211
3
     "%s: invalid metadata entry - value data size value out of bounds.",
212
3
     function );
213
214
3
    return( -1 );
215
3
  }
216
1.81k
  byte_stream_copy_to_uint32_little_endian(
217
1.81k
   ( (bde_metadata_entry_key_header_t *) value_data )->encryption_method,
218
1.81k
   key->encryption_method );
219
220
#if defined( HAVE_DEBUG_OUTPUT )
221
  if( libcnotify_verbose != 0 )
222
  {
223
    libcnotify_printf(
224
     "%s: encryption method\t\t\t\t\t: 0x%08" PRIx32 " (%s)\n",
225
     function,
226
     key->encryption_method,
227
     libbde_debug_print_encryption_method(
228
      key->encryption_method ) );
229
  }
230
#endif
231
1.81k
  value_data      += sizeof( bde_metadata_entry_key_header_t );
232
1.81k
  value_data_size -= sizeof( bde_metadata_entry_key_header_t );
233
234
#if defined( HAVE_DEBUG_OUTPUT )
235
  if( libcnotify_verbose != 0 )
236
  {
237
    libcnotify_printf(
238
     "%s: key data:\n",
239
     function );
240
    libcnotify_print_data(
241
     value_data,
242
     value_data_size,
243
     0 );
244
  }
245
#endif
246
1.81k
  key->data = (uint8_t *) memory_allocate(
247
1.81k
                           sizeof( uint8_t ) * value_data_size );
248
249
1.81k
  if( key->data == NULL )
250
0
  {
251
0
    libcerror_error_set(
252
0
     error,
253
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
254
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
255
0
     "%s: unable to create data.",
256
0
     function );
257
258
0
    goto on_error;
259
0
  }
260
1.81k
  if( memory_copy(
261
1.81k
       key->data,
262
1.81k
       value_data,
263
1.81k
       value_data_size ) == NULL )
264
0
  {
265
0
    libcerror_error_set(
266
0
     error,
267
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
268
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
269
0
     "%s: unable to copy data to key.",
270
0
     function );
271
272
0
    goto on_error;
273
0
  }
274
1.81k
  key->data_size = value_data_size;
275
  
276
1.81k
  return( 1 );
277
278
0
on_error:
279
0
  if( key->data != NULL )
280
0
  {
281
0
    memory_free(
282
0
     key->data );
283
284
    key->data = NULL;
285
0
  }
286
0
  return( -1 );
287
1.81k
}
288