Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / lib / keyslot_context.c
1 /*
2  * LUKS - Linux Unified Key Setup, keyslot unlock helpers
3  *
4  * Copyright (C) 2022-2023 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2022-2023 Ondrej Kozina
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <errno.h>
23
24 #include "luks1/luks.h"
25 #include "luks2/luks2.h"
26 #include "keyslot_context.h"
27
28 static int get_luks2_key_by_passphrase(struct crypt_device *cd,
29         struct crypt_keyslot_context *kc,
30         int keyslot,
31         int segment,
32         struct volume_key **r_vk)
33 {
34         int r;
35
36         assert(cd);
37         assert(kc && kc->type == CRYPT_KC_TYPE_PASSPHRASE);
38         assert(r_vk);
39
40         r = LUKS2_keyslot_open(cd, keyslot, segment, kc->u.p.passphrase, kc->u.p.passphrase_size, r_vk);
41         if (r < 0)
42                 kc->error = r;
43
44         return r;
45 }
46
47 static int get_luks1_volume_key_by_passphrase(struct crypt_device *cd,
48         struct crypt_keyslot_context *kc,
49         int keyslot,
50         struct volume_key **r_vk)
51 {
52         int r;
53
54         assert(cd);
55         assert(kc && kc->type == CRYPT_KC_TYPE_PASSPHRASE);
56         assert(r_vk);
57
58         r = LUKS_open_key_with_hdr(keyslot, kc->u.p.passphrase, kc->u.p.passphrase_size,
59                                    crypt_get_hdr(cd, CRYPT_LUKS1), r_vk, cd);
60         if (r < 0)
61                 kc->error = r;
62
63         return r;
64 }
65
66 static int get_luks2_volume_key_by_passphrase(struct crypt_device *cd,
67         struct crypt_keyslot_context *kc,
68         int keyslot,
69         struct volume_key **r_vk)
70 {
71         return get_luks2_key_by_passphrase(cd, kc, keyslot, CRYPT_DEFAULT_SEGMENT, r_vk);
72 }
73
74 static int get_passphrase_by_passphrase(struct crypt_device *cd,
75         struct crypt_keyslot_context *kc,
76         const char **r_passphrase,
77         size_t *r_passphrase_size)
78 {
79         assert(cd);
80         assert(kc && kc->type == CRYPT_KC_TYPE_PASSPHRASE);
81         assert(r_passphrase);
82         assert(r_passphrase_size);
83
84         *r_passphrase = kc->u.p.passphrase;
85         *r_passphrase_size = kc->u.p.passphrase_size;
86
87         return 0;
88 }
89
90 static int get_passphrase_by_keyfile(struct crypt_device *cd,
91         struct crypt_keyslot_context *kc,
92         const char **r_passphrase,
93         size_t *r_passphrase_size)
94 {
95         int r;
96
97         assert(cd);
98         assert(kc && kc->type == CRYPT_KC_TYPE_KEYFILE);
99         assert(r_passphrase);
100         assert(r_passphrase_size);
101
102         if (!kc->i_passphrase) {
103                 r = crypt_keyfile_device_read(cd, kc->u.kf.keyfile,
104                                        &kc->i_passphrase, &kc->i_passphrase_size,
105                                        kc->u.kf.keyfile_offset, kc->u.kf.keyfile_size, 0);
106                 if (r < 0) {
107                         kc->error = r;
108                         return r;
109                 }
110         }
111
112         *r_passphrase = kc->i_passphrase;
113         *r_passphrase_size = kc->i_passphrase_size;
114
115         return 0;
116 }
117
118 static int get_luks2_key_by_keyfile(struct crypt_device *cd,
119         struct crypt_keyslot_context *kc,
120         int keyslot,
121         int segment,
122         struct volume_key **r_vk)
123 {
124         int r;
125         const char *passphrase;
126         size_t passphrase_size;
127
128         assert(cd);
129         assert(kc && kc->type == CRYPT_KC_TYPE_KEYFILE);
130         assert(r_vk);
131
132         r = get_passphrase_by_keyfile(cd, kc, &passphrase, &passphrase_size);
133         if (r)
134                 return r;
135
136         r = LUKS2_keyslot_open(cd, keyslot, segment, passphrase, passphrase_size, r_vk);
137         if (r < 0)
138                 kc->error = r;
139
140         return r;
141 }
142
143 static int get_luks2_volume_key_by_keyfile(struct crypt_device *cd,
144         struct crypt_keyslot_context *kc,
145         int keyslot,
146         struct volume_key **r_vk)
147 {
148         return get_luks2_key_by_keyfile(cd, kc, keyslot, CRYPT_DEFAULT_SEGMENT, r_vk);
149 }
150
151 static int get_luks1_volume_key_by_keyfile(struct crypt_device *cd,
152         struct crypt_keyslot_context *kc,
153         int keyslot,
154         struct volume_key **r_vk)
155 {
156         int r;
157         const char *passphrase;
158         size_t passphrase_size;
159
160         assert(cd);
161         assert(kc && kc->type == CRYPT_KC_TYPE_KEYFILE);
162         assert(r_vk);
163
164         r = get_passphrase_by_keyfile(cd, kc, &passphrase, &passphrase_size);
165         if (r)
166                 return r;
167
168         r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
169                                    crypt_get_hdr(cd, CRYPT_LUKS1), r_vk, cd);
170         if (r < 0)
171                 kc->error = r;
172
173         return r;
174 }
175
176 static int get_key_by_key(struct crypt_device *cd,
177         struct crypt_keyslot_context *kc,
178         int keyslot __attribute__((unused)),
179         int segment __attribute__((unused)),
180         struct volume_key **r_vk)
181 {
182         assert(kc && kc->type == CRYPT_KC_TYPE_KEY);
183         assert(r_vk);
184
185         if (!kc->u.k.volume_key) {
186                 kc->error = -ENOENT;
187                 return kc->error;
188         }
189
190         *r_vk = crypt_alloc_volume_key(kc->u.k.volume_key_size, kc->u.k.volume_key);
191         if (!*r_vk) {
192                 kc->error = -ENOMEM;
193                 return kc->error;
194         }
195
196         return 0;
197 }
198
199 static int get_volume_key_by_key(struct crypt_device *cd,
200         struct crypt_keyslot_context *kc,
201         int keyslot __attribute__((unused)),
202         struct volume_key **r_vk)
203 {
204         return get_key_by_key(cd, kc, -2 /* unused */, -2 /* unused */, r_vk);
205 }
206
207 static int get_luks2_key_by_token(struct crypt_device *cd,
208         struct crypt_keyslot_context *kc,
209         int keyslot __attribute__((unused)),
210         int segment,
211         struct volume_key **r_vk)
212 {
213         int r;
214
215         assert(cd);
216         assert(kc && kc->type == CRYPT_KC_TYPE_TOKEN);
217         assert(r_vk);
218
219         r = LUKS2_token_unlock_key(cd, crypt_get_hdr(cd, CRYPT_LUKS2), kc->u.t.id, kc->u.t.type,
220                                    kc->u.t.pin, kc->u.t.pin_size, segment, kc->u.t.usrptr, r_vk);
221         if (r < 0)
222                 kc->error = r;
223
224         return r;
225 }
226
227 static int get_luks2_volume_key_by_token(struct crypt_device *cd,
228         struct crypt_keyslot_context *kc,
229         int keyslot __attribute__((unused)),
230         struct volume_key **r_vk)
231 {
232         return get_luks2_key_by_token(cd, kc, -2 /* unused */, CRYPT_DEFAULT_SEGMENT, r_vk);
233 }
234
235 static int get_passphrase_by_token(struct crypt_device *cd,
236         struct crypt_keyslot_context *kc,
237         const char **r_passphrase,
238         size_t *r_passphrase_size)
239 {
240         int r;
241
242         assert(cd);
243         assert(kc && kc->type == CRYPT_KC_TYPE_TOKEN);
244         assert(r_passphrase);
245         assert(r_passphrase_size);
246
247         if (!kc->i_passphrase) {
248                 r = LUKS2_token_unlock_passphrase(cd, crypt_get_hdr(cd, CRYPT_LUKS2), kc->u.t.id,
249                                 kc->u.t.type, kc->u.t.pin, kc->u.t.pin_size,
250                                 kc->u.t.usrptr, &kc->i_passphrase, &kc->i_passphrase_size);
251                 if (r < 0) {
252                         kc->error = r;
253                         return r;
254                 }
255                 kc->u.t.id = r;
256         }
257
258         *r_passphrase = kc->i_passphrase;
259         *r_passphrase_size = kc->i_passphrase_size;
260
261         return kc->u.t.id;
262 }
263
264 static void unlock_method_init_internal(struct crypt_keyslot_context *kc)
265 {
266         assert(kc);
267
268         kc->error = 0;
269         kc->i_passphrase = NULL;
270         kc->i_passphrase_size = 0;
271 }
272
273 void crypt_keyslot_unlock_by_key_init_internal(struct crypt_keyslot_context *kc,
274         const char *volume_key,
275         size_t volume_key_size)
276 {
277         assert(kc);
278
279         kc->type = CRYPT_KC_TYPE_KEY;
280         kc->u.k.volume_key = volume_key;
281         kc->u.k.volume_key_size = volume_key_size;
282         kc->get_luks2_key = get_key_by_key;
283         kc->get_luks2_volume_key = get_volume_key_by_key;
284         kc->get_luks1_volume_key = get_volume_key_by_key;
285         kc->get_passphrase = NULL; /* keyslot key context does not provide passphrase */
286         unlock_method_init_internal(kc);
287 }
288
289 void crypt_keyslot_unlock_by_passphrase_init_internal(struct crypt_keyslot_context *kc,
290         const char *passphrase,
291         size_t passphrase_size)
292 {
293         assert(kc);
294
295         kc->type = CRYPT_KC_TYPE_PASSPHRASE;
296         kc->u.p.passphrase = passphrase;
297         kc->u.p.passphrase_size = passphrase_size;
298         kc->get_luks2_key = get_luks2_key_by_passphrase;
299         kc->get_luks2_volume_key = get_luks2_volume_key_by_passphrase;
300         kc->get_luks1_volume_key = get_luks1_volume_key_by_passphrase;
301         kc->get_passphrase = get_passphrase_by_passphrase;
302         unlock_method_init_internal(kc);
303 }
304
305 void crypt_keyslot_unlock_by_keyfile_init_internal(struct crypt_keyslot_context *kc,
306         const char *keyfile,
307         size_t keyfile_size,
308         uint64_t keyfile_offset)
309 {
310         assert(kc);
311
312         kc->type = CRYPT_KC_TYPE_KEYFILE;
313         kc->u.kf.keyfile = keyfile;
314         kc->u.kf.keyfile_size = keyfile_size;
315         kc->u.kf.keyfile_offset = keyfile_offset;
316         kc->get_luks2_key = get_luks2_key_by_keyfile;
317         kc->get_luks2_volume_key = get_luks2_volume_key_by_keyfile;
318         kc->get_luks1_volume_key = get_luks1_volume_key_by_keyfile;
319         kc->get_passphrase = get_passphrase_by_keyfile;
320         unlock_method_init_internal(kc);
321 }
322
323 void crypt_keyslot_unlock_by_token_init_internal(struct crypt_keyslot_context *kc,
324         int token,
325         const char *type,
326         const char *pin,
327         size_t pin_size,
328         void *usrptr)
329 {
330         assert(kc);
331
332         kc->type = CRYPT_KC_TYPE_TOKEN;
333         kc->u.t.id = token;
334         kc->u.t.type = type;
335         kc->u.t.pin = pin;
336         kc->u.t.pin_size = pin_size;
337         kc->u.t.usrptr = usrptr;
338         kc->get_luks2_key = get_luks2_key_by_token;
339         kc->get_luks2_volume_key = get_luks2_volume_key_by_token;
340         kc->get_luks1_volume_key = NULL; /* LUKS1 is not supported */
341         kc->get_passphrase = get_passphrase_by_token;
342         unlock_method_init_internal(kc);
343 }
344
345 void crypt_keyslot_context_destroy_internal(struct crypt_keyslot_context *kc)
346 {
347         if (!kc)
348                 return;
349
350         crypt_safe_free(kc->i_passphrase);
351         kc->i_passphrase = NULL;
352         kc->i_passphrase_size = 0;
353 }
354
355 void crypt_keyslot_context_free(struct crypt_keyslot_context *kc)
356 {
357         crypt_keyslot_context_destroy_internal(kc);
358         free(kc);
359 }
360
361 int crypt_keyslot_context_init_by_passphrase(struct crypt_device *cd,
362         const char *passphrase,
363         size_t passphrase_size,
364         struct crypt_keyslot_context **kc)
365 {
366         struct crypt_keyslot_context *tmp;
367
368         if (!kc || !passphrase)
369                 return -EINVAL;
370
371         tmp = malloc(sizeof(*tmp));
372         if (!tmp)
373                 return -ENOMEM;
374
375         crypt_keyslot_unlock_by_passphrase_init_internal(tmp, passphrase, passphrase_size);
376
377         *kc = tmp;
378
379         return 0;
380 }
381
382 int crypt_keyslot_context_init_by_keyfile(struct crypt_device *cd,
383         const char *keyfile,
384         size_t keyfile_size,
385         uint64_t keyfile_offset,
386         struct crypt_keyslot_context **kc)
387 {
388         struct crypt_keyslot_context *tmp;
389
390         if (!kc || !keyfile)
391                 return -EINVAL;
392
393         tmp = malloc(sizeof(*tmp));
394         if (!tmp)
395                 return -ENOMEM;
396
397         crypt_keyslot_unlock_by_keyfile_init_internal(tmp, keyfile, keyfile_size, keyfile_offset);
398
399         *kc = tmp;
400
401         return 0;
402 }
403
404 int crypt_keyslot_context_init_by_token(struct crypt_device *cd,
405         int token,
406         const char *type,
407         const char *pin, size_t pin_size,
408         void *usrptr,
409         struct crypt_keyslot_context **kc)
410 {
411         struct crypt_keyslot_context *tmp;
412
413         if (!kc || (token < 0 && token != CRYPT_ANY_TOKEN))
414                 return -EINVAL;
415
416         tmp = malloc(sizeof(*tmp));
417         if (!tmp)
418                 return -ENOMEM;
419
420         crypt_keyslot_unlock_by_token_init_internal(tmp, token, type, pin, pin_size, usrptr);
421
422         *kc = tmp;
423
424         return 0;
425 }
426
427 int crypt_keyslot_context_init_by_volume_key(struct crypt_device *cd,
428         const char *volume_key,
429         size_t volume_key_size,
430         struct crypt_keyslot_context **kc)
431 {
432         struct crypt_keyslot_context *tmp;
433
434         if (!kc)
435                 return -EINVAL;
436
437         tmp = malloc(sizeof(*tmp));
438         if (!tmp)
439                 return -ENOMEM;
440
441         crypt_keyslot_unlock_by_key_init_internal(tmp, volume_key, volume_key_size);
442
443         *kc = tmp;
444
445         return 0;
446 }
447
448 int crypt_keyslot_context_get_error(struct crypt_keyslot_context *kc)
449 {
450         return kc ? kc->error : -EINVAL;
451 }
452
453 int crypt_keyslot_context_set_pin(struct crypt_device *cd,
454         const char *pin, size_t pin_size,
455         struct crypt_keyslot_context *kc)
456 {
457         if (!kc || kc->type != CRYPT_KC_TYPE_TOKEN)
458                 return -EINVAL;
459
460         kc->u.t.pin = pin;
461         kc->u.t.pin_size = pin_size;
462         kc->error = 0;
463
464         return 0;
465 }
466
467 int crypt_keyslot_context_get_type(const struct crypt_keyslot_context *kc)
468 {
469         return kc ? kc->type : -EINVAL;
470 }
471
472 const char *keyslot_context_type_string(const struct crypt_keyslot_context *kc)
473 {
474         assert(kc);
475
476         switch (kc->type) {
477         case CRYPT_KC_TYPE_PASSPHRASE:
478                 return "passphrase";
479         case CRYPT_KC_TYPE_KEYFILE:
480                 return "keyfile";
481         case CRYPT_KC_TYPE_TOKEN:
482                 return "token";
483         case CRYPT_KC_TYPE_KEY:
484                 return "key";
485         default:
486                 return "<unknown>";
487         }
488 }