Coverage Report

Created: 2026-01-20 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsapfs/libfsapfs/libfsapfs_key_bag_entry.c
Line
Count
Source
1
/*
2
 * The key bag entry functions
3
 *
4
 * Copyright (C) 2018-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 "libfsapfs_debug.h"
28
#include "libfsapfs_key_bag_entry.h"
29
#include "libfsapfs_libcerror.h"
30
#include "libfsapfs_libcnotify.h"
31
#include "libfsapfs_libfguid.h"
32
33
#include "fsapfs_key_bag.h"
34
35
/* Creates key bag entry
36
 * Make sure the value key_bag_entry is referencing, is set to NULL
37
 * Returns 1 if successful or -1 on error
38
 */
39
int libfsapfs_key_bag_entry_initialize(
40
     libfsapfs_key_bag_entry_t **key_bag_entry,
41
     libcerror_error_t **error )
42
0
{
43
0
  static char *function = "libfsapfs_key_bag_entry_initialize";
44
45
0
  if( key_bag_entry == NULL )
46
0
  {
47
0
    libcerror_error_set(
48
0
     error,
49
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
50
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
51
0
     "%s: invalid key bag entry.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
0
  if( *key_bag_entry != NULL )
57
0
  {
58
0
    libcerror_error_set(
59
0
     error,
60
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
61
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
62
0
     "%s: invalid key bag entry value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
0
  *key_bag_entry = memory_allocate_structure(
68
0
                    libfsapfs_key_bag_entry_t );
69
70
0
  if( *key_bag_entry == NULL )
71
0
  {
72
0
    libcerror_error_set(
73
0
     error,
74
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
75
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
76
0
     "%s: unable to create key bag entry.",
77
0
     function );
78
79
0
    goto on_error;
80
0
  }
81
0
  if( memory_set(
82
0
       *key_bag_entry,
83
0
       0,
84
0
       sizeof( libfsapfs_key_bag_entry_t ) ) == NULL )
85
0
  {
86
0
    libcerror_error_set(
87
0
     error,
88
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
89
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
90
0
     "%s: unable to clear key bag entry.",
91
0
     function );
92
93
0
    goto on_error;
94
0
  }
95
0
  return( 1 );
96
97
0
on_error:
98
0
  if( *key_bag_entry != NULL )
99
0
  {
100
0
    memory_free(
101
0
     *key_bag_entry );
102
103
0
    *key_bag_entry = NULL;
104
0
  }
105
0
  return( -1 );
106
0
}
107
108
/* Frees key bag entry
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libfsapfs_key_bag_entry_free(
112
     libfsapfs_key_bag_entry_t **key_bag_entry,
113
     libcerror_error_t **error )
114
0
{
115
0
  static char *function = "libfsapfs_key_bag_entry_free";
116
117
0
  if( key_bag_entry == NULL )
118
0
  {
119
0
    libcerror_error_set(
120
0
     error,
121
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
122
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
123
0
     "%s: invalid key bag entry.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
0
  if( *key_bag_entry != NULL )
129
0
  {
130
0
    if( ( *key_bag_entry )->data != NULL )
131
0
    {
132
0
      memory_free(
133
0
       ( *key_bag_entry )->data );
134
0
    }
135
0
    memory_free(
136
0
     *key_bag_entry );
137
138
0
    *key_bag_entry = NULL;
139
0
  }
140
0
  return( 1 );
141
0
}
142
143
/* Reads the key bag entry
144
 * Returns 1 if successful or -1 on error
145
 */
146
int libfsapfs_key_bag_entry_read_data(
147
     libfsapfs_key_bag_entry_t *key_bag_entry,
148
     const uint8_t *data,
149
     size_t data_size,
150
     libcerror_error_t **error )
151
0
{
152
0
  static char *function = "libfsapfs_key_bag_entry_read_data";
153
154
#if defined( HAVE_DEBUG_OUTPUT )
155
  uint32_t value_32bit  = 0;
156
#endif
157
158
0
  if( key_bag_entry == NULL )
159
0
  {
160
0
    libcerror_error_set(
161
0
     error,
162
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
163
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
164
0
     "%s: invalid key bag entry.",
165
0
     function );
166
167
0
    return( -1 );
168
0
  }
169
0
  if( key_bag_entry->data != NULL )
170
0
  {
171
0
    libcerror_error_set(
172
0
     error,
173
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
174
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
175
0
     "%s: invalid key bag entry - data value already set.",
176
0
     function );
177
178
0
    return( -1 );
179
0
  }
180
0
  if( data == NULL )
181
0
  {
182
0
    libcerror_error_set(
183
0
     error,
184
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
185
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
186
0
     "%s: invalid data.",
187
0
     function );
188
189
0
    return( -1 );
190
0
  }
191
0
  if( ( data_size < sizeof( fsapfs_key_bag_entry_header_t ) )
192
0
   || ( data_size > (size_t) SSIZE_MAX ) )
193
0
  {
194
0
    libcerror_error_set(
195
0
     error,
196
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
197
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
198
0
     "%s: invalid data size value out of bounds.",
199
0
     function );
200
201
0
    return( -1 );
202
0
  }
203
#if defined( HAVE_DEBUG_OUTPUT )
204
  if( libcnotify_verbose != 0 )
205
  {
206
    libcnotify_printf(
207
     "%s: key bag entry header data:\n",
208
     function );
209
    libcnotify_print_data(
210
     data,
211
     sizeof( fsapfs_key_bag_entry_header_t ),
212
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
213
  }
214
#endif
215
0
  if( memory_copy(
216
0
       key_bag_entry->identifier,
217
0
       ( (fsapfs_key_bag_entry_header_t *) data )->identifier,
218
0
       16 ) == NULL )
219
0
  {
220
0
    libcerror_error_set(
221
0
     error,
222
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
223
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
224
0
     "%s: unable to copy identifier.",
225
0
     function );
226
227
0
    goto on_error;
228
0
  }
229
0
  byte_stream_copy_to_uint16_little_endian(
230
0
   ( (fsapfs_key_bag_entry_header_t *) data )->entry_type,
231
0
   key_bag_entry->type );
232
233
0
  byte_stream_copy_to_uint16_little_endian(
234
0
   ( (fsapfs_key_bag_entry_header_t *) data )->data_size,
235
0
   key_bag_entry->data_size );
236
237
#if defined( HAVE_DEBUG_OUTPUT )
238
  if( libcnotify_verbose != 0 )
239
  {
240
    if( libfsapfs_debug_print_guid_value(
241
         function,
242
         "identifier\t\t\t\t",
243
         ( (fsapfs_key_bag_entry_header_t *) data )->identifier,
244
         16,
245
         LIBFGUID_ENDIAN_BIG,
246
         LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE,
247
         error ) != 1 )
248
    {
249
      libcerror_error_set(
250
       error,
251
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
252
       LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
253
       "%s: unable to print UUID value.",
254
       function );
255
256
      goto on_error;
257
    }
258
    libcnotify_printf(
259
     "%s: entry type\t\t\t\t: %" PRIu16 "\n",
260
     function,
261
     key_bag_entry->type );
262
263
    libcnotify_printf(
264
     "%s: data size\t\t\t\t: %" PRIu16 "\n",
265
     function,
266
     key_bag_entry->data_size );
267
268
    byte_stream_copy_to_uint32_little_endian(
269
     ( (fsapfs_key_bag_entry_header_t *) data )->unknown1,
270
     value_32bit );
271
    libcnotify_printf(
272
     "%s: unknown1\t\t\t\t: 0x%08" PRIx32 "\n",
273
     function,
274
     value_32bit );
275
276
    libcnotify_printf(
277
     "\n" );
278
  }
279
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
280
281
0
  if( ( key_bag_entry->data_size == 0 )
282
0
   || ( key_bag_entry->data_size > ( data_size - sizeof( fsapfs_key_bag_entry_header_t ) ) ) )
283
0
  {
284
0
    libcerror_error_set(
285
0
     error,
286
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
287
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
288
0
     "%s: invalid key bag entry - data size value out of bounds.",
289
0
     function );
290
291
0
    goto on_error;
292
0
  }
293
0
  key_bag_entry->data = (uint8_t *) memory_allocate(
294
0
                                     sizeof( uint8_t ) * key_bag_entry->data_size );
295
296
0
  if( key_bag_entry->data == NULL )
297
0
  {
298
0
    libcerror_error_set(
299
0
     error,
300
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
301
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
302
0
     "%s: unable to create key bag entry data.",
303
0
     function );
304
305
0
    goto on_error;
306
0
  }
307
0
  if( memory_copy(
308
0
       key_bag_entry->data,
309
0
       &( data[ sizeof( fsapfs_key_bag_entry_header_t ) ] ),
310
0
       (size_t) key_bag_entry->data_size ) == NULL )
311
0
  {
312
0
    libcerror_error_set(
313
0
     error,
314
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
315
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
316
0
     "%s: unable to copy key bag entry data.",
317
0
     function );
318
319
0
    goto on_error;
320
0
  }
321
#if defined( HAVE_DEBUG_OUTPUT )
322
  if( libcnotify_verbose != 0 )
323
  {
324
    libcnotify_printf(
325
     "%s: key bag entry data:\n",
326
     function );
327
    libcnotify_print_data(
328
     key_bag_entry->data,
329
     (size_t) key_bag_entry->data_size,
330
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
331
  }
332
#endif
333
0
  key_bag_entry->size = sizeof( fsapfs_key_bag_entry_header_t ) + key_bag_entry->data_size;
334
335
0
  return( 1 );
336
337
0
on_error:
338
0
  if( key_bag_entry->data != NULL )
339
0
  {
340
0
    memory_free(
341
0
     key_bag_entry->data );
342
343
    key_bag_entry->data = NULL;
344
0
  }
345
0
  return( -1 );
346
0
}
347