Coverage Report

Created: 2025-06-22 07:35

/src/libfsapfs/libfsapfs/libfsapfs_key_bag_header.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The key bag header functions
3
 *
4
 * Copyright (C) 2018-2024, 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_header.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 container physical_map_header
36
 * Make sure the value key_bag_header is referencing, is set to NULL
37
 * Returns 1 if successful or -1 on error
38
 */
39
int libfsapfs_key_bag_header_initialize(
40
     libfsapfs_key_bag_header_t **key_bag_header,
41
     libcerror_error_t **error )
42
0
{
43
0
  static char *function = "libfsapfs_key_bag_header_initialize";
44
45
0
  if( key_bag_header == 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 header.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
0
  if( *key_bag_header != 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 header value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
0
  *key_bag_header = memory_allocate_structure(
68
0
                     libfsapfs_key_bag_header_t );
69
70
0
  if( *key_bag_header == 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 header.",
77
0
     function );
78
79
0
    goto on_error;
80
0
  }
81
0
  if( memory_set(
82
0
       *key_bag_header,
83
0
       0,
84
0
       sizeof( libfsapfs_key_bag_header_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 header.",
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_header != NULL )
99
0
  {
100
0
    memory_free(
101
0
     *key_bag_header );
102
103
0
    *key_bag_header = NULL;
104
0
  }
105
0
  return( -1 );
106
0
}
107
108
/* Frees key bag header
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libfsapfs_key_bag_header_free(
112
     libfsapfs_key_bag_header_t **key_bag_header,
113
     libcerror_error_t **error )
114
0
{
115
0
  static char *function = "libfsapfs_key_bag_header_free";
116
117
0
  if( key_bag_header == 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 header.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
0
  if( *key_bag_header != NULL )
129
0
  {
130
0
    memory_free(
131
0
     *key_bag_header );
132
133
0
    *key_bag_header = NULL;
134
0
  }
135
0
  return( 1 );
136
0
}
137
138
/* Reads the key bag header
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libfsapfs_key_bag_header_read_data(
142
     libfsapfs_key_bag_header_t *key_bag_header,
143
     const uint8_t *data,
144
     size_t data_size,
145
     libcerror_error_t **error )
146
0
{
147
0
  static char *function   = "libfsapfs_key_bag_header_read_data";
148
0
  uint16_t format_version = 0;
149
150
#if defined( HAVE_DEBUG_OUTPUT )
151
  uint64_t value_64bit    = 0;
152
#endif
153
154
0
  if( key_bag_header == NULL )
155
0
  {
156
0
    libcerror_error_set(
157
0
     error,
158
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
159
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
160
0
     "%s: invalid key bag header.",
161
0
     function );
162
163
0
    return( -1 );
164
0
  }
165
0
  if( data == NULL )
166
0
  {
167
0
    libcerror_error_set(
168
0
     error,
169
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
170
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
171
0
     "%s: invalid data.",
172
0
     function );
173
174
0
    return( -1 );
175
0
  }
176
0
  if( ( data_size < sizeof( fsapfs_key_bag_header_t ) )
177
0
   || ( data_size > (size_t) SSIZE_MAX ) )
178
0
  {
179
0
    libcerror_error_set(
180
0
     error,
181
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
182
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
183
0
     "%s: invalid data size value out of bounds.",
184
0
     function );
185
186
0
    return( -1 );
187
0
  }
188
#if defined( HAVE_DEBUG_OUTPUT )
189
  if( libcnotify_verbose != 0 )
190
  {
191
    libcnotify_printf(
192
     "%s: key bag header data:\n",
193
     function );
194
    libcnotify_print_data(
195
     data,
196
     sizeof( fsapfs_key_bag_header_t ),
197
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
198
  }
199
#endif
200
0
  byte_stream_copy_to_uint16_little_endian(
201
0
   ( (fsapfs_key_bag_header_t *) data )->format_version,
202
0
   format_version );
203
204
0
  byte_stream_copy_to_uint16_little_endian(
205
0
   ( (fsapfs_key_bag_header_t *) data )->number_of_entries,
206
0
   key_bag_header->number_of_entries );
207
208
0
  byte_stream_copy_to_uint32_little_endian(
209
0
   ( (fsapfs_key_bag_header_t *) data )->data_size,
210
0
   key_bag_header->data_size );
211
212
#if defined( HAVE_DEBUG_OUTPUT )
213
  if( libcnotify_verbose != 0 )
214
  {
215
    libcnotify_printf(
216
     "%s: format version\t\t\t: %" PRIu16 "\n",
217
     function,
218
     format_version );
219
220
    libcnotify_printf(
221
     "%s: number of entries\t\t\t: %" PRIu16 "\n",
222
     function,
223
     key_bag_header->number_of_entries );
224
225
    libcnotify_printf(
226
     "%s: data size\t\t\t\t: %" PRIu32 "\n",
227
     function,
228
     key_bag_header->data_size );
229
230
    byte_stream_copy_to_uint64_little_endian(
231
     ( (fsapfs_key_bag_header_t *) data )->unknown1,
232
     value_64bit );
233
    libcnotify_printf(
234
     "%s: unknown1\t\t\t\t: 0x%08" PRIx64 "\n",
235
     function,
236
     value_64bit );
237
238
    libcnotify_printf(
239
     "\n" );
240
  }
241
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
242
243
0
  if( format_version != 2 )
244
0
  {
245
0
    libcerror_error_set(
246
0
     error,
247
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
248
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
249
0
     "%s: unsupported format version: %" PRIu16 ".",
250
0
     function,
251
0
     format_version );
252
253
0
    return( -1 );
254
0
  }
255
0
  return( 1 );
256
0
}
257