Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsapfs/libfsapfs/libfsapfs_key_encrypted_key.c
Line
Count
Source
1
/*
2
 * The key encrypted key (KEK) functions
3
 *
4
 * Copyright (C) 2018-2026, 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_encryption_context.h"
29
#include "libfsapfs_key_bag_value.h"
30
#include "libfsapfs_key_encrypted_key.h"
31
#include "libfsapfs_libcerror.h"
32
#include "libfsapfs_libcnotify.h"
33
#include "libfsapfs_libfguid.h"
34
#include "libfsapfs_libhmac.h"
35
#include "libfsapfs_password.h"
36
37
#include "fsapfs_key_bag.h"
38
39
const uint8_t libfsapfs_key_encrypted_key_wrapped_kek_initialization_vector[ 8 ] = {
40
       0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6 };
41
42
/* Creates a key encrypted key
43
 * Make sure the value key_encrypted_key is referencing, is set to NULL
44
 * Returns 1 if successful or -1 on error
45
 */
46
int libfsapfs_key_encrypted_key_initialize(
47
     libfsapfs_key_encrypted_key_t **key_encrypted_key,
48
     libcerror_error_t **error )
49
0
{
50
0
  static char *function = "libfsapfs_key_encrypted_key_initialize";
51
52
0
  if( key_encrypted_key == NULL )
53
0
  {
54
0
    libcerror_error_set(
55
0
     error,
56
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
57
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
58
0
     "%s: invalid key encrypted key.",
59
0
     function );
60
61
0
    return( -1 );
62
0
  }
63
0
  if( *key_encrypted_key != NULL )
64
0
  {
65
0
    libcerror_error_set(
66
0
     error,
67
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
68
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
69
0
     "%s: invalid key encrypted key value already set.",
70
0
     function );
71
72
0
    return( -1 );
73
0
  }
74
0
  *key_encrypted_key = memory_allocate_structure(
75
0
                        libfsapfs_key_encrypted_key_t );
76
77
0
  if( *key_encrypted_key == NULL )
78
0
  {
79
0
    libcerror_error_set(
80
0
     error,
81
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
82
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
83
0
     "%s: unable to create key encrypted key.",
84
0
     function );
85
86
0
    goto on_error;
87
0
  }
88
0
  if( memory_set(
89
0
       *key_encrypted_key,
90
0
       0,
91
0
       sizeof( libfsapfs_key_encrypted_key_t ) ) == NULL )
92
0
  {
93
0
    libcerror_error_set(
94
0
     error,
95
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
96
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
97
0
     "%s: unable to clear key encrypted key.",
98
0
     function );
99
100
0
    goto on_error;
101
0
  }
102
0
  return( 1 );
103
104
0
on_error:
105
0
  if( *key_encrypted_key != NULL )
106
0
  {
107
0
    memory_free(
108
0
     *key_encrypted_key );
109
110
0
    *key_encrypted_key = NULL;
111
0
  }
112
0
  return( -1 );
113
0
}
114
115
/* Frees key encrypted key
116
 * Returns 1 if successful or -1 on error
117
 */
118
int libfsapfs_key_encrypted_key_free(
119
     libfsapfs_key_encrypted_key_t **key_encrypted_key,
120
     libcerror_error_t **error )
121
0
{
122
0
  static char *function = "libfsapfs_key_encrypted_key_free";
123
124
0
  if( key_encrypted_key == NULL )
125
0
  {
126
0
    libcerror_error_set(
127
0
     error,
128
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
129
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
130
0
     "%s: invalid key encrypted key.",
131
0
     function );
132
133
0
    return( -1 );
134
0
  }
135
0
  if( *key_encrypted_key != NULL )
136
0
  {
137
0
    memory_free(
138
0
     *key_encrypted_key );
139
140
0
    *key_encrypted_key = NULL;
141
0
  }
142
0
  return( 1 );
143
0
}
144
145
/* Reads the key encrypted key
146
 * Returns 1 if successful or -1 on error
147
 */
148
int libfsapfs_key_encrypted_key_read_data(
149
     libfsapfs_key_encrypted_key_t *key_encrypted_key,
150
     const uint8_t *data,
151
     size_t data_size,
152
     libcerror_error_t **error )
153
0
{
154
0
  fsapfs_key_bag_kek_metadata_header_t *kek_metadata_header = NULL;
155
0
  libfsapfs_key_bag_value_t *packed_value                   = NULL;
156
0
  const uint8_t *wrapped_kek_object_data                    = NULL;
157
0
  static char *function                                     = "libfsapfs_key_encrypted_key_read_data";
158
0
  size_t data_offset                                        = 0;
159
0
  size_t object_value_data_end                              = 0;
160
0
  size_t wrapped_kek_object_header_size                     = 0;
161
0
  uint16_t kek_metadata_value_data_size                     = 0;
162
0
  uint16_t wrapped_kek_object_data_size                     = 0;
163
0
  uint16_t remaining_value_data_size                        = 0;
164
165
#if defined( HAVE_DEBUG_OUTPUT )
166
  uint16_t value_16bit                                      = 0;
167
#endif
168
169
0
  if( key_encrypted_key == NULL )
170
0
  {
171
0
    libcerror_error_set(
172
0
     error,
173
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
174
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
175
0
     "%s: invalid key encrypted key.",
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_ARGUMENTS,
185
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
186
0
     "%s: invalid data.",
187
0
     function );
188
189
0
    return( -1 );
190
0
  }
191
0
  if( data_size > (size_t) SSIZE_MAX )
192
0
  {
193
0
    libcerror_error_set(
194
0
     error,
195
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
196
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
197
0
     "%s: invalid data size value out of bounds.",
198
0
     function );
199
200
0
    return( -1 );
201
0
  }
202
#if defined( HAVE_DEBUG_OUTPUT )
203
  if( libcnotify_verbose != 0 )
204
  {
205
    libcnotify_printf(
206
     "%s: KEK object data:\n",
207
     function );
208
    libcnotify_print_data(
209
     data,
210
     data_size,
211
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
212
  }
213
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
214
215
0
  if( libfsapfs_key_bag_value_initialize(
216
0
       &packed_value,
217
0
       error ) != 1 )
218
0
  {
219
0
    libcerror_error_set(
220
0
     error,
221
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
222
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
223
0
     "%s: unable to create packed value.",
224
0
     function );
225
226
0
    goto on_error;
227
0
  }
228
0
  if( libfsapfs_key_bag_value_read_data(
229
0
       packed_value,
230
0
       data,
231
0
       data_size,
232
0
       error ) != 1 )
233
0
  {
234
0
    libcerror_error_set(
235
0
     error,
236
0
     LIBCERROR_ERROR_DOMAIN_IO,
237
0
     LIBCERROR_IO_ERROR_READ_FAILED,
238
0
     "%s: unable to read KEK object packed value at offset: %" PRIzd ".",
239
0
     function,
240
0
     data_offset );
241
242
0
    goto on_error;
243
0
  }
244
0
  data_offset += 2 + packed_value->extended_size;
245
246
0
  if( packed_value->data_size > ( data_size - data_offset ) )
247
0
  {
248
0
    libcerror_error_set(
249
0
     error,
250
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
251
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
252
0
     "%s: invalid KEK object value data size value out of bounds.",
253
0
     function );
254
255
0
    goto on_error;
256
0
  }
257
0
  object_value_data_end = data_offset + packed_value->data_size;
258
259
#if defined( HAVE_DEBUG_OUTPUT )
260
  if( libcnotify_verbose != 0 )
261
  {
262
    libcnotify_printf(
263
     "%s: object value data:\n",
264
     function );
265
    libcnotify_print_data(
266
     &( data[ data_offset ] ),
267
     (size_t) packed_value->data_size,
268
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
269
  }
270
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
271
272
0
  if( packed_value->tag != 0x30 )
273
0
  {
274
0
    libcerror_error_set(
275
0
     error,
276
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
277
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
278
0
     "%s: unsupported KEK object value tag: 0x02%" PRIx8 ".",
279
0
     function,
280
0
     packed_value->tag );
281
282
0
    goto on_error;
283
0
  }
284
  /* The attributes are bound by the object value data, not by the data,
285
   * so that trailing data is not parsed as an attribute.
286
   */
287
0
  while( data_offset < object_value_data_end )
288
0
  {
289
0
    if( ( object_value_data_end - data_offset ) < 2 )
290
0
    {
291
0
      libcerror_error_set(
292
0
       error,
293
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
294
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
295
0
       "%s: invalid KEK attribute value data size value out of bounds.",
296
0
       function );
297
298
0
      goto on_error;
299
0
    }
300
0
    if( ( data[ data_offset ] == 0 )
301
0
     && ( data[ data_offset + 1 ] == 0 ) )
302
0
    {
303
0
      break;
304
0
    }
305
0
    if( libfsapfs_key_bag_value_read_data(
306
0
         packed_value,
307
0
         &( data[ data_offset ] ),
308
0
         data_size - data_offset,
309
0
         error ) != 1 )
310
0
    {
311
0
      libcerror_error_set(
312
0
       error,
313
0
       LIBCERROR_ERROR_DOMAIN_IO,
314
0
       LIBCERROR_IO_ERROR_READ_FAILED,
315
0
       "%s: unable to read KEK attribute packed value at offset: %" PRIzd ".",
316
0
       function,
317
0
       data_offset );
318
319
0
      goto on_error;
320
0
    }
321
0
    data_offset += 2 + packed_value->extended_size;
322
323
0
    if( packed_value->data_size > ( object_value_data_end - data_offset ) )
324
0
    {
325
0
      libcerror_error_set(
326
0
       error,
327
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
328
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
329
0
       "%s: invalid KEK attribute value data size value out of bounds.",
330
0
       function );
331
332
0
      goto on_error;
333
0
    }
334
#if defined( HAVE_DEBUG_OUTPUT )
335
    if( libcnotify_verbose != 0 )
336
    {
337
      libcnotify_printf(
338
       "%s: KEK attribute value data:\n",
339
       function );
340
      libcnotify_print_data(
341
       &( data[ data_offset ] ),
342
       (size_t) packed_value->data_size,
343
       LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
344
    }
345
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
346
347
0
    switch( packed_value->tag )
348
0
    {
349
0
      case 0x81:
350
0
        if( packed_value->data_size != 32 )
351
0
        {
352
0
          libcerror_error_set(
353
0
           error,
354
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
355
0
           LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
356
0
           "%s: unsupported HMAC attribute value data size: %" PRIu16 ".",
357
0
           function,
358
0
           packed_value->data_size );
359
360
0
          goto on_error;
361
0
        }
362
0
        if( memory_copy(
363
0
             key_encrypted_key->hmac,
364
0
             &( data[ data_offset ] ),
365
0
             32 ) == NULL )
366
0
        {
367
0
          libcerror_error_set(
368
0
           error,
369
0
           LIBCERROR_ERROR_DOMAIN_MEMORY,
370
0
           LIBCERROR_MEMORY_ERROR_COPY_FAILED,
371
0
           "%s: unable to copy HMAC.",
372
0
           function );
373
374
0
          goto on_error;
375
0
        }
376
0
        break;
377
378
0
      case 0x82:
379
0
        if( packed_value->data_size != 8 )
380
0
        {
381
0
          libcerror_error_set(
382
0
           error,
383
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
384
0
           LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
385
0
           "%s: unsupported KEK attribute value data size: %" PRIu16 ".",
386
0
           function,
387
0
           packed_value->data_size );
388
389
0
          goto on_error;
390
0
        }
391
0
        break;
392
393
0
      case 0xa3:
394
0
        if( packed_value->data_size == 0 )
395
0
        {
396
0
          libcerror_error_set(
397
0
           error,
398
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
399
0
           LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
400
0
           "%s: unsupported wrapped KEK packed object attribute value data size: %" PRIu16 ".",
401
0
           function,
402
0
           packed_value->data_size );
403
404
0
          goto on_error;
405
0
        }
406
0
        wrapped_kek_object_header_size = 2 + packed_value->extended_size;
407
408
0
        wrapped_kek_object_data      = &( data[ data_offset - wrapped_kek_object_header_size ] );
409
0
        wrapped_kek_object_data_size = (uint16_t) ( wrapped_kek_object_header_size + packed_value->data_size );
410
0
        break;
411
412
0
      default:
413
0
        break;
414
0
    }
415
0
    data_offset += packed_value->data_size;
416
0
  }
417
0
  if( wrapped_kek_object_data == NULL )
418
0
  {
419
0
    libcerror_error_set(
420
0
     error,
421
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
422
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
423
0
     "%s: missing wrapped KEK packed object.",
424
0
     function );
425
426
0
    goto on_error;
427
0
  }
428
#if defined( HAVE_DEBUG_OUTPUT )
429
  if( libcnotify_verbose != 0 )
430
  {
431
    libcnotify_printf(
432
     "%s: wrapped KEK packed object data:\n",
433
     function );
434
    libcnotify_print_data(
435
     wrapped_kek_object_data,
436
     wrapped_kek_object_data_size,
437
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
438
  }
439
#endif
440
0
  if( libfsapfs_key_bag_value_read_data(
441
0
       packed_value,
442
0
       wrapped_kek_object_data,
443
0
       wrapped_kek_object_data_size,
444
0
       error ) != 1 )
445
0
  {
446
0
    libcerror_error_set(
447
0
     error,
448
0
     LIBCERROR_ERROR_DOMAIN_IO,
449
0
     LIBCERROR_IO_ERROR_READ_FAILED,
450
0
     "%s: unable to read KEK packed value at offset: %" PRIzd ".",
451
0
     function,
452
0
     data_offset );
453
454
0
    goto on_error;
455
0
  }
456
0
  data_offset = 2 + packed_value->extended_size;
457
458
#if defined( HAVE_DEBUG_OUTPUT )
459
  if( libcnotify_verbose != 0 )
460
  {
461
    libcnotify_printf(
462
     "%s: wrapped KEK object value data:\n",
463
     function );
464
    libcnotify_print_data(
465
     &( wrapped_kek_object_data[ data_offset ] ),
466
     (size_t) packed_value->data_size,
467
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
468
  }
469
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
470
471
0
  if( packed_value->tag != 0xa3 )
472
0
  {
473
0
    libcerror_error_set(
474
0
     error,
475
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
476
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
477
0
     "%s: unsupported wrapped KEK object value tag: 0x02%" PRIx8 ".",
478
0
     function,
479
0
     packed_value->tag );
480
481
0
    goto on_error;
482
0
  }
483
0
  if( packed_value->data_size > ( wrapped_kek_object_data_size - 2 ) )
484
0
  {
485
0
    libcerror_error_set(
486
0
     error,
487
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
488
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
489
0
     "%s: invalid wrapped KEK object value data size value out of bounds.",
490
0
     function );
491
492
0
    goto on_error;
493
0
  }
494
0
  while( data_offset < (size_t) wrapped_kek_object_data_size )
495
0
  {
496
0
    if( ( wrapped_kek_object_data_size - data_offset ) < 2 )
497
0
    {
498
0
      libcerror_error_set(
499
0
       error,
500
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
501
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
502
0
       "%s: invalid wrapped KEK attribute value data size value out of bounds.",
503
0
       function );
504
505
0
      goto on_error;
506
0
    }
507
0
    if( ( wrapped_kek_object_data[ data_offset ] == 0 )
508
0
     && ( wrapped_kek_object_data[ data_offset + 1 ] == 0 ) )
509
0
    {
510
0
      break;
511
0
    }
512
0
    if( libfsapfs_key_bag_value_read_data(
513
0
         packed_value,
514
0
         &( wrapped_kek_object_data[ data_offset ] ),
515
0
         wrapped_kek_object_data_size - data_offset,
516
0
         error ) != 1 )
517
0
    {
518
0
      libcerror_error_set(
519
0
       error,
520
0
       LIBCERROR_ERROR_DOMAIN_IO,
521
0
       LIBCERROR_IO_ERROR_READ_FAILED,
522
0
       "%s: unable to read wrapped KEK packed value at offset: %" PRIzd ".",
523
0
       function,
524
0
       data_offset );
525
526
0
      goto on_error;
527
0
    }
528
0
    data_offset += 2 + packed_value->extended_size;
529
530
0
    if( packed_value->data_size > ( (size_t) wrapped_kek_object_data_size - data_offset ) )
531
0
    {
532
0
      libcerror_error_set(
533
0
       error,
534
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
535
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
536
0
       "%s: invalid wrapped KEK packed value data size value out of bounds.",
537
0
       function );
538
539
0
      goto on_error;
540
0
    }
541
#if defined( HAVE_DEBUG_OUTPUT )
542
    if( libcnotify_verbose != 0 )
543
    {
544
      libcnotify_printf(
545
       "%s: wrapped KEK attribute value data:\n",
546
       function );
547
      libcnotify_print_data(
548
       &( wrapped_kek_object_data[ data_offset ] ),
549
       (size_t) packed_value->data_size,
550
       LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
551
    }
552
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
553
554
0
    switch( packed_value->tag )
555
0
    {
556
0
      case 0x81:
557
0
        if( packed_value->data_size != 16 )
558
0
        {
559
0
          libcerror_error_set(
560
0
           error,
561
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
562
0
           LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
563
0
           "%s: unsupported identifier wrapped KEK attribute value data size: %" PRIu16 ".",
564
0
           function,
565
0
           packed_value->data_size );
566
567
0
          goto on_error;
568
0
        }
569
0
        if( memory_copy(
570
0
             key_encrypted_key->identifier,
571
0
             &( wrapped_kek_object_data[ data_offset ] ),
572
0
             16 ) == NULL )
573
0
        {
574
0
          libcerror_error_set(
575
0
           error,
576
0
           LIBCERROR_ERROR_DOMAIN_MEMORY,
577
0
           LIBCERROR_MEMORY_ERROR_COPY_FAILED,
578
0
           "%s: unable to copy identifier.",
579
0
           function );
580
581
0
          goto on_error;
582
0
        }
583
#if defined( HAVE_DEBUG_OUTPUT )
584
        if( libcnotify_verbose != 0 )
585
        {
586
          if( libfsapfs_debug_print_guid_value(
587
               function,
588
               "identifier\t\t\t",
589
               &( wrapped_kek_object_data[ data_offset ] ),
590
               16,
591
               LIBFGUID_ENDIAN_BIG,
592
               LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE,
593
               error ) != 1 )
594
          {
595
            libcerror_error_set(
596
             error,
597
             LIBCERROR_ERROR_DOMAIN_RUNTIME,
598
             LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
599
             "%s: unable to print UUID value.",
600
             function );
601
602
            goto on_error;
603
          }
604
          libcnotify_printf(
605
           "\n" );
606
        }
607
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
608
609
0
        break;
610
611
0
      case 0x82:
612
        /* The metadata can be 8 or 22 bytes in size
613
         */
614
0
        if( ( packed_value->data_size != 8 )
615
0
         && ( packed_value->data_size != 22 ) )
616
0
        {
617
0
          libcerror_error_set(
618
0
           error,
619
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
620
0
           LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
621
0
           "%s: unsupported wrapped KEK metadata attribute value data size: %" PRIu16 ".",
622
0
           function,
623
0
           packed_value->data_size );
624
625
0
          goto on_error;
626
0
        }
627
0
        kek_metadata_value_data_size = packed_value->data_size;
628
0
        kek_metadata_header          = (fsapfs_key_bag_kek_metadata_header_t *) &( wrapped_kek_object_data[ data_offset ] );
629
630
0
        byte_stream_copy_to_uint32_little_endian(
631
0
         kek_metadata_header->flags,
632
0
         key_encrypted_key->flags );
633
634
#if defined( HAVE_DEBUG_OUTPUT )
635
        if( libcnotify_verbose != 0 )
636
        {
637
          libcnotify_printf(
638
           "%s: flags\t\t\t\t: 0x%08" PRIx32 "\n",
639
           function,
640
           key_encrypted_key->flags );
641
642
          byte_stream_copy_to_uint16_little_endian(
643
           kek_metadata_header->unknown1,
644
           value_16bit );
645
          libcnotify_printf(
646
           "%s: unknown1\t\t\t\t: 0x%04" PRIx16 "\n",
647
           function,
648
           value_16bit );
649
650
          if( packed_value->data_size == 8 )
651
          {
652
            byte_stream_copy_to_uint16_little_endian(
653
             &( wrapped_kek_object_data[ data_offset + 6 ] ),
654
             value_16bit );
655
            libcnotify_printf(
656
             "%s: identifier\t\t\t: 0x%04" PRIx16 "\n",
657
             function,
658
             value_16bit );
659
          }
660
          else if( packed_value->data_size == 22 )
661
          {
662
            if( libfsapfs_debug_print_guid_value(
663
                 function,
664
                 "identifier\t\t\t",
665
                 &( wrapped_kek_object_data[ data_offset + 6 ] ),
666
                 16,
667
                 LIBFGUID_ENDIAN_BIG,
668
                 LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE,
669
                 error ) != 1 )
670
            {
671
              libcerror_error_set(
672
               error,
673
               LIBCERROR_ERROR_DOMAIN_RUNTIME,
674
               LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
675
               "%s: unable to print UUID value.",
676
               function );
677
678
              goto on_error;
679
            }
680
          }
681
          libcnotify_printf(
682
           "\n" );
683
        }
684
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
685
686
        /* In the 22-byte variant the first 4 bytes are not the
687
         * encryption method. The 40-byte wrapped KEK corresponds with
688
         * AES-256.
689
         */
690
0
        if( packed_value->data_size == 22 )
691
0
        {
692
0
          key_encrypted_key->flags = 0;
693
0
        }
694
0
        break;
695
696
0
      case 0x83:
697
0
        if( packed_value->data_size != 40 )
698
0
        {
699
0
          libcerror_error_set(
700
0
           error,
701
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
702
0
           LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
703
0
           "%s: unsupported wrapped KEK attribute value data size: %" PRIu16 ".",
704
0
           function,
705
0
           packed_value->data_size );
706
707
0
          goto on_error;
708
0
        }
709
0
        if( memory_copy(
710
0
             key_encrypted_key->wrapped_kek,
711
0
             &( wrapped_kek_object_data[ data_offset ] ),
712
0
             40 ) == NULL )
713
0
        {
714
0
          libcerror_error_set(
715
0
           error,
716
0
           LIBCERROR_ERROR_DOMAIN_MEMORY,
717
0
           LIBCERROR_MEMORY_ERROR_COPY_FAILED,
718
0
           "%s: unable to copy wrapped KEK.",
719
0
           function );
720
721
0
          goto on_error;
722
0
        }
723
0
        break;
724
725
0
      case 0x84:
726
        /* A password-protected KEK entry stores a <= 8-byte PBKDF2
727
         * iteration count here. A macOS 26 container key bag VEK
728
         * entry, which is unwrapped with a key instead of a password,
729
         * carries a 16-byte value that is not an iteration count. The
730
         * 22-byte KEK metadata attribute distinguishes the two, so
731
         * that a corrupt legacy entry is still rejected.
732
         */
733
0
        if( ( packed_value->data_size == 0 )
734
0
         || ( ( packed_value->data_size > 8 )
735
0
          &&  ( kek_metadata_value_data_size != 22 ) ) )
736
0
        {
737
0
          libcerror_error_set(
738
0
           error,
739
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
740
0
           LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
741
0
           "%s: unsupported number of wrapped KEK iteration attribute value data size: %" PRIu16 ".",
742
0
           function,
743
0
           packed_value->data_size );
744
745
0
          goto on_error;
746
0
        }
747
0
        if( packed_value->data_size > 8 )
748
0
        {
749
0
          break;
750
0
        }
751
0
        key_encrypted_key->number_of_iterations = 0;
752
753
0
        remaining_value_data_size = packed_value->data_size;
754
755
0
        while( remaining_value_data_size > 0 )
756
0
        {
757
0
          key_encrypted_key->number_of_iterations <<= 8;
758
0
          key_encrypted_key->number_of_iterations  |= wrapped_kek_object_data[ data_offset++ ];
759
760
0
          remaining_value_data_size--;
761
0
        }
762
0
        data_offset -= packed_value->data_size;
763
764
#if defined( HAVE_DEBUG_OUTPUT )
765
        if( libcnotify_verbose != 0 )
766
        {
767
          libcnotify_printf(
768
           "%s: number of iterations\t\t: %" PRIu64 "\n",
769
           function,
770
           key_encrypted_key->number_of_iterations );
771
772
          libcnotify_printf(
773
           "\n" );
774
        }
775
#endif
776
0
        break;
777
778
0
      case 0x85:
779
        /* A password-protected KEK entry stores a 16-byte PBKDF2 salt
780
         * here. A macOS 26 container key bag VEK entry carries an
781
         * unrelated 3-byte value that is not a salt. The 22-byte KEK
782
         * metadata attribute distinguishes the two, so that a corrupt
783
         * legacy entry is still rejected.
784
         */
785
0
        if( ( packed_value->data_size != 16 )
786
0
         && ( kek_metadata_value_data_size != 22 ) )
787
0
        {
788
0
          libcerror_error_set(
789
0
           error,
790
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
791
0
           LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
792
0
           "%s: unsupported wrapped KEK salt attribute value data size: %" PRIu16 ".",
793
0
           function,
794
0
           packed_value->data_size );
795
796
0
          goto on_error;
797
0
        }
798
0
        if( packed_value->data_size != 16 )
799
0
        {
800
0
          break;
801
0
        }
802
0
        if( memory_copy(
803
0
             key_encrypted_key->salt,
804
0
             &( wrapped_kek_object_data[ data_offset ] ),
805
0
             16 ) == NULL )
806
0
        {
807
0
          libcerror_error_set(
808
0
           error,
809
0
           LIBCERROR_ERROR_DOMAIN_MEMORY,
810
0
           LIBCERROR_MEMORY_ERROR_COPY_FAILED,
811
0
           "%s: unable to copy salt.",
812
0
           function );
813
814
0
          goto on_error;
815
0
        }
816
0
        break;
817
818
0
      default:
819
0
        break;
820
0
    }
821
0
    data_offset += packed_value->data_size;
822
0
  }
823
0
  if( libfsapfs_key_bag_value_free(
824
0
       &packed_value,
825
0
       error ) != 1 )
826
0
  {
827
0
    libcerror_error_set(
828
0
     error,
829
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
830
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
831
0
     "%s: unable to free packed value.",
832
0
     function );
833
834
0
    goto on_error;
835
0
  }
836
0
  return( 1 );
837
838
0
on_error:
839
0
  if( packed_value != NULL )
840
0
  {
841
0
    libfsapfs_key_bag_value_free(
842
0
     &packed_value,
843
0
     NULL );
844
0
  }
845
0
  return( -1 );
846
0
}
847
848
/* Unlocks the key encrypted key with a key
849
 * Returns 1 if successful, 0 if not or -1 on error
850
 */
851
int libfsapfs_key_encrypted_key_unlock_with_key(
852
     libfsapfs_key_encrypted_key_t *key_encrypted_key,
853
     const uint8_t *key,
854
     size_t key_size,
855
     uint8_t *unlocked_key,
856
     size_t unlocked_key_size,
857
     libcerror_error_t **error )
858
0
{
859
0
  uint8_t hash_buffer[ LIBHMAC_SHA256_HASH_SIZE ];
860
0
  uint8_t wrapped_kek[ 40 ];
861
862
0
  static char *function     = "libfsapfs_key_encrypted_key_unlock_with_key";
863
0
  size_t used_kek_data_size = 0;
864
0
  size_t used_key_size      = 0;
865
0
  int result                = 0;
866
867
0
  if( key_encrypted_key == NULL )
868
0
  {
869
0
    libcerror_error_set(
870
0
     error,
871
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
872
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
873
0
     "%s: invalid key encrypted key.",
874
0
     function );
875
876
0
    return( -1 );
877
0
  }
878
0
  if( key_encrypted_key->flags == 0 )
879
0
  {
880
0
    used_kek_data_size = 40;
881
0
    used_key_size      = 32;
882
0
  }
883
0
  else if( key_encrypted_key->flags == 2 )
884
0
  {
885
0
    used_kek_data_size = 24;
886
0
    used_key_size      = 16;
887
0
  }
888
0
  else
889
0
  {
890
0
    libcerror_error_set(
891
0
     error,
892
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
893
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
894
0
     "%s: unsupported encryption method.",
895
0
     function );
896
897
0
    return( -1 );
898
0
  }
899
0
  if( key == NULL )
900
0
  {
901
0
    libcerror_error_set(
902
0
     error,
903
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
904
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
905
0
     "%s: invalid key.",
906
0
     function );
907
908
0
    return( -1 );
909
0
  }
910
0
  if( key_size != 256 )
911
0
  {
912
0
    libcerror_error_set(
913
0
     error,
914
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
915
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
916
0
     "%s: invalid key size value out of bounds.",
917
0
     function );
918
919
0
    return( -1 );
920
0
  }
921
0
  if( unlocked_key == NULL )
922
0
  {
923
0
    libcerror_error_set(
924
0
     error,
925
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
926
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
927
0
     "%s: invalid unlocked key.",
928
0
     function );
929
930
0
    return( -1 );
931
0
  }
932
0
  if( unlocked_key_size != 256 )
933
0
  {
934
0
    libcerror_error_set(
935
0
     error,
936
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
937
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
938
0
     "%s: invalid unlocked key size value out of bounds.",
939
0
     function );
940
941
0
    return( -1 );
942
0
  }
943
#if defined( HAVE_DEBUG_OUTPUT )
944
  if( libcnotify_verbose != 0 )
945
  {
946
    libcnotify_printf(
947
     "%s: key:\n",
948
     function );
949
    libcnotify_print_data(
950
     key,
951
     32,
952
     0 );
953
  }
954
#endif
955
0
  if( libfsapfs_encryption_aes_key_unwrap(
956
0
       key,
957
0
       used_key_size * 8,
958
0
       key_encrypted_key->wrapped_kek,
959
0
       used_kek_data_size,
960
0
       wrapped_kek,
961
0
       used_kek_data_size,
962
0
       error ) != 1 )
963
0
  {
964
0
    libcerror_error_set(
965
0
     error,
966
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
967
0
     LIBCERROR_RUNTIME_ERROR_GENERIC,
968
0
     "%s: unable to unwrap wrapped KEK with key.",
969
0
     function );
970
971
0
    goto on_error;
972
0
  }
973
0
  if( memory_compare(
974
0
       wrapped_kek,
975
0
       libfsapfs_key_encrypted_key_wrapped_kek_initialization_vector,
976
0
       8 ) == 0 )
977
0
  {
978
0
    if( memory_copy(
979
0
         unlocked_key,
980
0
         &( wrapped_kek[ 8 ] ),
981
0
         used_key_size ) == NULL )
982
0
    {
983
0
      libcerror_error_set(
984
0
       error,
985
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
986
0
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
987
0
       "%s: unable to copy unlocked key.",
988
0
       function );
989
990
0
      goto on_error;
991
0
    }
992
0
    if( key_encrypted_key->flags == 2 )
993
0
    {
994
0
      if( memory_copy(
995
0
           &( unlocked_key[ 16 ] ),
996
0
           key_encrypted_key->identifier,
997
0
           16 ) == NULL )
998
0
      {
999
0
        libcerror_error_set(
1000
0
         error,
1001
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
1002
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1003
0
         "%s: unable to copy identifier to unlocked key.",
1004
0
         function );
1005
1006
0
        goto on_error;
1007
0
      }
1008
0
      if( libhmac_sha256_calculate(
1009
0
           unlocked_key,
1010
0
           32,
1011
0
           hash_buffer,
1012
0
           LIBHMAC_SHA256_HASH_SIZE,
1013
0
           error ) != 1 )
1014
0
      {
1015
0
        libcerror_error_set(
1016
0
         error,
1017
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1018
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1019
0
         "%s: unable to calculate SHA-256 of tweak key data.",
1020
0
         function );
1021
1022
0
        goto on_error;
1023
0
      }
1024
0
      if( memory_copy(
1025
0
           &( unlocked_key[ 16 ] ),
1026
0
           hash_buffer,
1027
0
           16 ) == NULL )
1028
0
      {
1029
0
        libcerror_error_set(
1030
0
         error,
1031
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
1032
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1033
0
         "%s: unable to copy SHA-256 hash to unlocked key.",
1034
0
         function );
1035
1036
0
        goto on_error;
1037
0
      }
1038
0
      memory_set(
1039
0
       hash_buffer,
1040
0
       0,
1041
0
       LIBHMAC_SHA256_HASH_SIZE );
1042
0
    }
1043
#if defined( HAVE_DEBUG_OUTPUT )
1044
    if( libcnotify_verbose != 0 )
1045
    {
1046
      libcnotify_printf(
1047
       "%s: unlocked key:\n",
1048
       function );
1049
      libcnotify_print_data(
1050
       unlocked_key,
1051
       unlocked_key_size / 8,
1052
       0 );
1053
    }
1054
#endif
1055
0
    result = 1;
1056
0
  }
1057
0
  memory_set(
1058
0
   wrapped_kek,
1059
0
   0,
1060
0
   40 );
1061
1062
0
  return( result );
1063
1064
0
on_error:
1065
0
  memory_set(
1066
0
   wrapped_kek,
1067
0
   0,
1068
0
   40 );
1069
1070
0
  memory_set(
1071
0
   hash_buffer,
1072
0
   0,
1073
0
   LIBHMAC_SHA256_HASH_SIZE );
1074
1075
0
  return( -1 );
1076
0
}
1077
1078
/* Unlocks the key encrypted key with a password
1079
 * Returns 1 if successful, 0 if not or -1 on error
1080
 */
1081
int libfsapfs_key_encrypted_key_unlock_with_password(
1082
     libfsapfs_key_encrypted_key_t *key_encrypted_key,
1083
     const uint8_t *password,
1084
     size_t password_length,
1085
     uint8_t *unlocked_key,
1086
     size_t unlocked_key_size,
1087
     libcerror_error_t **error )
1088
0
{
1089
0
  uint8_t password_key[ 32 ];
1090
0
  uint8_t wrapped_kek[ 40 ];
1091
1092
0
  static char *function     = "libfsapfs_key_encrypted_key_unlock_with_password";
1093
0
  size_t password_key_size  = 0;
1094
0
  size_t used_kek_data_size = 0;
1095
0
  int result                = 0;
1096
1097
0
  if( key_encrypted_key == NULL )
1098
0
  {
1099
0
    libcerror_error_set(
1100
0
     error,
1101
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1102
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1103
0
     "%s: invalid key encrypted key.",
1104
0
     function );
1105
1106
0
    return( -1 );
1107
0
  }
1108
0
  if( ( key_encrypted_key->flags == 0 )
1109
0
   || ( key_encrypted_key->flags == 16 ) )
1110
0
  {
1111
0
    password_key_size  = 32;
1112
0
    used_kek_data_size = 40;
1113
0
  }
1114
0
  else if( key_encrypted_key->flags == 2 )
1115
0
  {
1116
0
    password_key_size  = 16;
1117
0
    used_kek_data_size = 24;
1118
0
  }
1119
0
  else
1120
0
  {
1121
0
    libcerror_error_set(
1122
0
     error,
1123
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1124
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1125
0
     "%s: unsupported encryption method.",
1126
0
     function );
1127
1128
0
    return( -1 );
1129
0
  }
1130
0
  if( unlocked_key == NULL )
1131
0
  {
1132
0
    libcerror_error_set(
1133
0
     error,
1134
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1135
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1136
0
     "%s: invalid unlocked key.",
1137
0
     function );
1138
1139
0
    return( -1 );
1140
0
  }
1141
0
  if( unlocked_key_size != 256 )
1142
0
  {
1143
0
    libcerror_error_set(
1144
0
     error,
1145
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1146
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1147
0
     "%s: invalid key size value out of bounds.",
1148
0
     function );
1149
1150
0
    return( -1 );
1151
0
  }
1152
0
  if( libfsapfs_password_pbkdf2(
1153
0
       password,
1154
0
       password_length,
1155
0
       key_encrypted_key->salt,
1156
0
       16,
1157
0
       (uint32_t) key_encrypted_key->number_of_iterations,
1158
0
       password_key,
1159
0
       password_key_size,
1160
0
       error ) != 1 )
1161
0
  {
1162
0
    libcerror_error_set(
1163
0
     error,
1164
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1165
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1166
0
     "%s: unable to determine password key.",
1167
0
     function );
1168
1169
0
    goto on_error;
1170
0
  }
1171
#if defined( HAVE_DEBUG_OUTPUT )
1172
  if( libcnotify_verbose != 0 )
1173
  {
1174
    libcnotify_printf(
1175
     "%s: password key:\n",
1176
     function );
1177
    libcnotify_print_data(
1178
     password_key,
1179
     password_key_size,
1180
     0 );
1181
  }
1182
#endif
1183
0
  if( libfsapfs_encryption_aes_key_unwrap(
1184
0
       password_key,
1185
0
       password_key_size * 8,
1186
0
       key_encrypted_key->wrapped_kek,
1187
0
       used_kek_data_size,
1188
0
       wrapped_kek,
1189
0
       used_kek_data_size,
1190
0
       error ) != 1 )
1191
0
  {
1192
0
    libcerror_error_set(
1193
0
     error,
1194
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1195
0
     LIBCERROR_RUNTIME_ERROR_GENERIC,
1196
0
     "%s: unable to unwrap wrapped KEK with password.",
1197
0
     function );
1198
1199
0
    goto on_error;
1200
0
  }
1201
0
  memory_set(
1202
0
   password_key,
1203
0
   0,
1204
0
   32 );
1205
1206
0
  if( memory_compare(
1207
0
       wrapped_kek,
1208
0
       libfsapfs_key_encrypted_key_wrapped_kek_initialization_vector,
1209
0
       8 ) == 0 )
1210
0
  {
1211
0
    if( memory_copy(
1212
0
         unlocked_key,
1213
0
         &( wrapped_kek[ 8 ] ),
1214
0
         password_key_size ) == NULL )
1215
0
    {
1216
0
      libcerror_error_set(
1217
0
       error,
1218
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1219
0
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1220
0
       "%s: unable to copy unlocked key.",
1221
0
       function );
1222
1223
0
      goto on_error;
1224
0
    }
1225
#if defined( HAVE_DEBUG_OUTPUT )
1226
    if( libcnotify_verbose != 0 )
1227
    {
1228
      libcnotify_printf(
1229
       "%s: unlocked key:\n",
1230
       function );
1231
      libcnotify_print_data(
1232
       unlocked_key,
1233
       unlocked_key_size / 8,
1234
       0 );
1235
    }
1236
#endif
1237
0
    result = 1;
1238
0
  }
1239
0
  memory_set(
1240
0
   wrapped_kek,
1241
0
   0,
1242
0
   40 );
1243
1244
0
  return( result );
1245
1246
0
on_error:
1247
0
  memory_set(
1248
0
   wrapped_kek,
1249
0
   0,
1250
0
   40 );
1251
1252
0
  memory_set(
1253
0
   password_key,
1254
0
   0,
1255
0
   32 );
1256
1257
0
  return( -1 );
1258
0
}
1259