Coverage Report

Created: 2024-06-12 07:07

/src/libbde/libbde/libbde_key_protector.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Key protector functions
3
 *
4
 * Copyright (C) 2011-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 <memory.h>
24
#include <types.h>
25
26
#include "libbde_key_protector.h"
27
#include "libbde_libcerror.h"
28
#include "libbde_types.h"
29
30
/* Creates a key protector
31
 * Make sure the value key_protector is referencing, is set to NULL
32
 * Returns 1 if successful or -1 on error
33
 */
34
int libbde_key_protector_initialize(
35
     libbde_key_protector_t **key_protector,
36
     libbde_volume_master_key_t *volume_master_key,
37
     libcerror_error_t **error )
38
0
{
39
0
  libbde_internal_key_protector_t *internal_key_protector = NULL;
40
0
  static char *function                                   = "libbde_key_protector_initialize";
41
42
0
  if( key_protector == NULL )
43
0
  {
44
0
    libcerror_error_set(
45
0
     error,
46
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
47
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
48
0
     "%s: invalid key protector.",
49
0
     function );
50
51
0
    return( -1 );
52
0
  }
53
0
  if( *key_protector != NULL )
54
0
  {
55
0
    libcerror_error_set(
56
0
     error,
57
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
58
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
59
0
     "%s: invalid key protector value already set.",
60
0
     function );
61
62
0
    return( -1 );
63
0
  }
64
0
  internal_key_protector = memory_allocate_structure(
65
0
                            libbde_internal_key_protector_t );
66
67
0
  if( internal_key_protector == NULL )
68
0
  {
69
0
    libcerror_error_set(
70
0
     error,
71
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
72
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
73
0
     "%s: unable to create key protector.",
74
0
     function );
75
76
0
    goto on_error;
77
0
  }
78
0
  if( memory_set(
79
0
       internal_key_protector,
80
0
       0,
81
0
       sizeof( libbde_internal_key_protector_t ) ) == NULL )
82
0
  {
83
0
    libcerror_error_set(
84
0
     error,
85
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
86
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
87
0
     "%s: unable to clear key protector.",
88
0
     function );
89
90
0
    memory_free(
91
0
     internal_key_protector );
92
93
0
    return( -1 );
94
0
  }
95
0
  internal_key_protector->volume_master_key = volume_master_key;
96
97
0
  *key_protector = (libbde_key_protector_t *) internal_key_protector;
98
99
0
  return( 1 );
100
101
0
on_error:
102
0
  if( internal_key_protector != NULL )
103
0
  {
104
0
    memory_free(
105
0
     internal_key_protector );
106
0
  }
107
0
  return( -1 );
108
0
}
109
110
/* Frees a key protector
111
 * Returns 1 if successful or -1 on error
112
 */
113
int libbde_key_protector_free(
114
     libbde_key_protector_t **key_protector,
115
     libcerror_error_t **error )
116
0
{
117
0
  libbde_internal_key_protector_t *internal_key_protector = NULL;
118
0
  static char *function                                   = "libbde_key_protector_free";
119
0
  int result                                              = 1;
120
121
0
  if( key_protector == NULL )
122
0
  {
123
0
    libcerror_error_set(
124
0
     error,
125
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
126
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
127
0
     "%s: invalid key protector.",
128
0
     function );
129
130
0
    return( -1 );
131
0
  }
132
0
  if( *key_protector != NULL )
133
0
  {
134
0
    internal_key_protector = (libbde_internal_key_protector_t *) *key_protector;
135
0
    *key_protector         = NULL;
136
137
0
    memory_free(
138
0
     internal_key_protector );
139
0
  }
140
0
  return( result );
141
0
}
142
143
/* Retrieves the identifier
144
 * The identifier is a GUID and is 16 bytes of size
145
 * Returns 1 if successful or -1 on error
146
 */
147
int libbde_key_protector_get_identifier(
148
     libbde_key_protector_t *key_protector,
149
     uint8_t *guid_data,
150
     size_t guid_data_size,
151
     libcerror_error_t **error )
152
0
{
153
0
  libbde_internal_key_protector_t *internal_key_protector = NULL;
154
0
  static char *function                                   = "libbde_key_protector_get_identifier";
155
156
0
  if( key_protector == 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 protector.",
163
0
     function );
164
165
0
    return( -1 );
166
0
  }
167
0
  internal_key_protector = (libbde_internal_key_protector_t *) key_protector;
168
169
0
  if( libbde_volume_master_key_get_identifier(
170
0
       internal_key_protector->volume_master_key,
171
0
       guid_data,
172
0
       guid_data_size,
173
0
       error ) != 1 )
174
0
  {
175
0
    libcerror_error_set(
176
0
     error,
177
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
178
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
179
0
     "%s: unable to retrieve identifier from volume master key.",
180
0
     function );
181
182
0
    return( -1 );
183
0
  }
184
0
  return( 1 );
185
0
}
186
187
/* Retrieves the type
188
 * Returns 1 if successful or -1 on error
189
 */
190
int libbde_key_protector_get_type(
191
     libbde_key_protector_t *key_protector,
192
     uint16_t *type,
193
     libcerror_error_t **error )
194
0
{
195
0
  libbde_internal_key_protector_t *internal_key_protector = NULL;
196
0
  static char *function                                   = "libbde_key_protector_get_type";
197
198
0
  if( key_protector == NULL )
199
0
  {
200
0
    libcerror_error_set(
201
0
     error,
202
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
203
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
204
0
     "%s: invalid key protector.",
205
0
     function );
206
207
0
    return( -1 );
208
0
  }
209
0
  internal_key_protector = (libbde_internal_key_protector_t *) key_protector;
210
211
0
  if( libbde_volume_master_key_get_protection_type(
212
0
       internal_key_protector->volume_master_key,
213
0
       type,
214
0
       error ) != 1 )
215
0
  {
216
0
    libcerror_error_set(
217
0
     error,
218
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
219
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
220
0
     "%s: unable to retrieve protection type from volume master key.",
221
0
     function );
222
223
0
    return( -1 );
224
0
  }
225
0
  return( 1 );
226
0
}
227