Imported Upstream version 2.3.3
[platform/upstream/cryptsetup.git] / lib / luks2 / luks2_token.c
1 /*
2  * LUKS - Linux Unified Key Setup v2, token handling
3  *
4  * Copyright (C) 2016-2020 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2016-2020 Milan Broz
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 <assert.h>
23
24 #include "luks2_internal.h"
25
26 /* Builtin tokens */
27 extern const crypt_token_handler keyring_handler;
28
29 static token_handler token_handlers[LUKS2_TOKENS_MAX] = {
30         /* keyring builtin token */
31         {
32           .get = token_keyring_get,
33           .set = token_keyring_set,
34           .h = &keyring_handler
35         },
36 };
37
38 static int is_builtin_candidate(const char *type)
39 {
40         return !strncmp(type, LUKS2_BUILTIN_TOKEN_PREFIX, LUKS2_BUILTIN_TOKEN_PREFIX_LEN);
41 }
42
43 int crypt_token_register(const crypt_token_handler *handler)
44 {
45         int i;
46
47         if (is_builtin_candidate(handler->name)) {
48                 log_dbg(NULL, "'" LUKS2_BUILTIN_TOKEN_PREFIX "' is reserved prefix for builtin tokens.");
49                 return -EINVAL;
50         }
51
52         for (i = 0; i < LUKS2_TOKENS_MAX && token_handlers[i].h; i++) {
53                 if (!strcmp(token_handlers[i].h->name, handler->name)) {
54                         log_dbg(NULL, "Keyslot handler %s is already registered.", handler->name);
55                         return -EINVAL;
56                 }
57         }
58
59         if (i == LUKS2_TOKENS_MAX)
60                 return -EINVAL;
61
62         token_handlers[i].h = handler;
63         return 0;
64 }
65
66 static const token_handler
67 *LUKS2_token_handler_type_internal(struct crypt_device *cd, const char *type)
68 {
69         int i;
70
71         for (i = 0; i < LUKS2_TOKENS_MAX && token_handlers[i].h; i++)
72                 if (!strcmp(token_handlers[i].h->name, type))
73                         return token_handlers + i;
74
75         return NULL;
76 }
77
78 static const crypt_token_handler
79 *LUKS2_token_handler_type(struct crypt_device *cd, const char *type)
80 {
81         const token_handler *th = LUKS2_token_handler_type_internal(cd, type);
82
83         return th ? th->h : NULL;
84 }
85
86 static const token_handler
87 *LUKS2_token_handler_internal(struct crypt_device *cd, int token)
88 {
89         struct luks2_hdr *hdr;
90         json_object *jobj1, *jobj2;
91
92         if (token < 0)
93                 return NULL;
94
95         if (!(hdr = crypt_get_hdr(cd, CRYPT_LUKS2)))
96                 return NULL;
97
98         if (!(jobj1 = LUKS2_get_token_jobj(hdr, token)))
99                 return NULL;
100
101         if (!json_object_object_get_ex(jobj1, "type", &jobj2))
102                 return NULL;
103
104         return LUKS2_token_handler_type_internal(cd, json_object_get_string(jobj2));
105 }
106
107 static const crypt_token_handler
108 *LUKS2_token_handler(struct crypt_device *cd, int token)
109 {
110         const token_handler *th = LUKS2_token_handler_internal(cd, token);
111
112         return th ? th->h : NULL;
113 }
114
115 static int LUKS2_token_find_free(struct luks2_hdr *hdr)
116 {
117         int i;
118
119         for (i = 0; i < LUKS2_TOKENS_MAX; i++)
120                 if (!LUKS2_get_token_jobj(hdr, i))
121                         return i;
122
123         return -EINVAL;
124 }
125
126 int LUKS2_token_create(struct crypt_device *cd,
127         struct luks2_hdr *hdr,
128         int token,
129         const char *json,
130         int commit)
131 {
132         const crypt_token_handler *h;
133         const token_handler *th;
134         json_object *jobj_tokens, *jobj_type, *jobj;
135         enum json_tokener_error jerr;
136         char num[16];
137
138         if (token == CRYPT_ANY_TOKEN) {
139                 if (!json)
140                         return -EINVAL;
141                 token = LUKS2_token_find_free(hdr);
142         }
143
144         if (token < 0 || token >= LUKS2_TOKENS_MAX)
145                 return -EINVAL;
146
147         if (!json_object_object_get_ex(hdr->jobj, "tokens", &jobj_tokens))
148                 return -EINVAL;
149
150         snprintf(num, sizeof(num), "%d", token);
151
152         /* Remove token */
153         if (!json)
154                 json_object_object_del(jobj_tokens, num);
155         else {
156
157                 jobj = json_tokener_parse_verbose(json, &jerr);
158                 if (!jobj) {
159                         log_dbg(cd, "Token JSON parse failed.");
160                         return -EINVAL;
161                 }
162
163                 if (LUKS2_token_validate(cd, hdr->jobj, jobj, num)) {
164                         json_object_put(jobj);
165                         return -EINVAL;
166                 }
167
168                 json_object_object_get_ex(jobj, "type", &jobj_type);
169                 if (is_builtin_candidate(json_object_get_string(jobj_type))) {
170                         th = LUKS2_token_handler_type_internal(cd, json_object_get_string(jobj_type));
171                         if (!th || !th->set) {
172                                 log_dbg(cd, "%s is builtin token candidate with missing handler", json_object_get_string(jobj_type));
173                                 json_object_put(jobj);
174                                 return -EINVAL;
175                         }
176                         h = th->h;
177                 } else
178                         h = LUKS2_token_handler_type(cd, json_object_get_string(jobj_type));
179
180                 if (h && h->validate && h->validate(cd, json)) {
181                         json_object_put(jobj);
182                         log_dbg(cd, "Token type %s validation failed.", h->name);
183                         return -EINVAL;
184                 }
185
186                 json_object_object_add(jobj_tokens, num, jobj);
187                 if (LUKS2_check_json_size(cd, hdr)) {
188                         log_dbg(cd, "Not enough space in header json area for new token.");
189                         json_object_object_del(jobj_tokens, num);
190                         return -ENOSPC;
191                 }
192         }
193
194         if (commit)
195                 return LUKS2_hdr_write(cd, hdr) ?: token;
196
197         return token;
198 }
199
200 crypt_token_info LUKS2_token_status(struct crypt_device *cd,
201         struct luks2_hdr *hdr,
202         int token,
203         const char **type)
204 {
205         const char *tmp;
206         const token_handler *th;
207         json_object *jobj_type, *jobj_token;
208
209         if (token < 0 || token >= LUKS2_TOKENS_MAX)
210                 return CRYPT_TOKEN_INVALID;
211
212         if (!(jobj_token = LUKS2_get_token_jobj(hdr, token)))
213                 return CRYPT_TOKEN_INACTIVE;
214
215         json_object_object_get_ex(jobj_token, "type", &jobj_type);
216         tmp = json_object_get_string(jobj_type);
217
218         if ((th = LUKS2_token_handler_type_internal(cd, tmp))) {
219                 if (type)
220                         *type = th->h->name;
221                 return th->set ? CRYPT_TOKEN_INTERNAL : CRYPT_TOKEN_EXTERNAL;
222         }
223
224         if (type)
225                 *type = tmp;
226
227         return is_builtin_candidate(tmp) ? CRYPT_TOKEN_INTERNAL_UNKNOWN : CRYPT_TOKEN_EXTERNAL_UNKNOWN;
228 }
229
230 int LUKS2_builtin_token_get(struct crypt_device *cd,
231         struct luks2_hdr *hdr,
232         int token,
233         const char *type,
234         void *params)
235 {
236         const token_handler *th = LUKS2_token_handler_type_internal(cd, type);
237
238         // internal error
239         assert(th && th->get);
240
241         return th->get(LUKS2_get_token_jobj(hdr, token), params) ?: token;
242 }
243
244 int LUKS2_builtin_token_create(struct crypt_device *cd,
245         struct luks2_hdr *hdr,
246         int token,
247         const char *type,
248         const void *params,
249         int commit)
250 {
251         const token_handler *th;
252         int r;
253         json_object *jobj_token, *jobj_tokens;
254
255         th = LUKS2_token_handler_type_internal(cd, type);
256
257         // at this point all builtin handlers must exist and have validate fn defined
258         assert(th && th->set && th->h->validate);
259
260         if (token == CRYPT_ANY_TOKEN) {
261                 if ((token = LUKS2_token_find_free(hdr)) < 0)
262                         log_err(cd, _("No free token slot."));
263         }
264         if (token < 0 || token >= LUKS2_TOKENS_MAX)
265                 return -EINVAL;
266
267         r = th->set(&jobj_token, params);
268         if (r) {
269                 log_err(cd, _("Failed to create builtin token %s."), type);
270                 return r;
271         }
272
273         // builtin tokens must produce valid json
274         r = LUKS2_token_validate(cd, hdr->jobj, jobj_token, "new");
275         assert(!r);
276         r = th->h->validate(cd, json_object_to_json_string_ext(jobj_token,
277                 JSON_C_TO_STRING_PLAIN | JSON_C_TO_STRING_NOSLASHESCAPE));
278         assert(!r);
279
280         json_object_object_get_ex(hdr->jobj, "tokens", &jobj_tokens);
281         json_object_object_add_by_uint(jobj_tokens, token, jobj_token);
282         if (LUKS2_check_json_size(cd, hdr)) {
283                 log_dbg(cd, "Not enough space in header json area for new %s token.", type);
284                 json_object_object_del_by_uint(jobj_tokens, token);
285                 return -ENOSPC;
286         }
287
288         if (commit)
289                 return LUKS2_hdr_write(cd, hdr) ?: token;
290
291         return token;
292 }
293
294 static int LUKS2_token_open(struct crypt_device *cd,
295         struct luks2_hdr *hdr,
296         int token,
297         char **buffer,
298         size_t *buffer_len,
299         void *usrptr)
300 {
301         const char *json;
302         const crypt_token_handler *h;
303         int r;
304
305         if (!(h = LUKS2_token_handler(cd, token)))
306                 return -ENOENT;
307
308         if (h->validate) {
309                 if (LUKS2_token_json_get(cd, hdr, token, &json))
310                         return -EINVAL;
311
312                 if (h->validate(cd, json)) {
313                         log_dbg(cd, "Token %d (%s) validation failed.", token, h->name);
314                         return -EINVAL;
315                 }
316         }
317
318         r = h->open(cd, token, buffer, buffer_len, usrptr);
319         if (r < 0)
320                 log_dbg(cd, "Token %d (%s) open failed with %d.", token, h->name, r);
321
322         return r;
323 }
324
325 static void LUKS2_token_buffer_free(struct crypt_device *cd,
326                 int token,
327                 void *buffer,
328                 size_t buffer_len)
329 {
330         const crypt_token_handler *h = LUKS2_token_handler(cd, token);
331
332         if (h->buffer_free)
333                 h->buffer_free(buffer, buffer_len);
334         else {
335                 crypt_safe_memzero(buffer, buffer_len);
336                 free(buffer);
337         }
338 }
339
340 static int LUKS2_keyslot_open_by_token(struct crypt_device *cd,
341         struct luks2_hdr *hdr,
342         int token,
343         int segment,
344         const char *buffer,
345         size_t buffer_len,
346         struct volume_key **vk)
347 {
348         const crypt_token_handler *h;
349         json_object *jobj_token, *jobj_token_keyslots, *jobj;
350         unsigned int num = 0;
351         int i, r;
352
353         if (!(h = LUKS2_token_handler(cd, token)))
354                 return -ENOENT;
355
356         jobj_token = LUKS2_get_token_jobj(hdr, token);
357         if (!jobj_token)
358                 return -EINVAL;
359
360         json_object_object_get_ex(jobj_token, "keyslots", &jobj_token_keyslots);
361         if (!jobj_token_keyslots)
362                 return -EINVAL;
363
364         /* Try to open keyslot referenced in token */
365         r = -EINVAL;
366         for (i = 0; i < (int) json_object_array_length(jobj_token_keyslots) && r < 0; i++) {
367                 jobj = json_object_array_get_idx(jobj_token_keyslots, i);
368                 num = atoi(json_object_get_string(jobj));
369                 log_dbg(cd, "Trying to open keyslot %u with token %d (type %s).", num, token, h->name);
370                 r = LUKS2_keyslot_open(cd, num, segment, buffer, buffer_len, vk);
371         }
372
373         if (r < 0)
374                 return r;
375
376         return num;
377 }
378
379 int LUKS2_token_open_and_activate(struct crypt_device *cd,
380                 struct luks2_hdr *hdr,
381                 int token,
382                 const char *name,
383                 uint32_t flags,
384                 void *usrptr)
385 {
386         int keyslot, r;
387         char *buffer;
388         size_t buffer_len;
389         struct volume_key *vk = NULL;
390
391         r = LUKS2_token_open(cd, hdr, token, &buffer, &buffer_len, usrptr);
392         if (r < 0)
393                 return r;
394
395         r = LUKS2_keyslot_open_by_token(cd, hdr, token,
396                                         (flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY) ?
397                                         CRYPT_ANY_SEGMENT : CRYPT_DEFAULT_SEGMENT,
398                                         buffer, buffer_len, &vk);
399
400         LUKS2_token_buffer_free(cd, token, buffer, buffer_len);
401
402         if (r < 0)
403                 return r;
404
405         keyslot = r;
406
407         if ((name || (flags & CRYPT_ACTIVATE_KEYRING_KEY)) && crypt_use_keyring_for_vk(cd)) {
408                 if (!(r = LUKS2_volume_key_load_in_keyring_by_keyslot(cd, hdr, vk, keyslot)))
409                         flags |= CRYPT_ACTIVATE_KEYRING_KEY;
410         }
411
412         if (r >= 0 && name)
413                 r = LUKS2_activate(cd, name, vk, flags);
414
415         if (r < 0)
416                 crypt_drop_keyring_key(cd, vk);
417         crypt_free_volume_key(vk);
418
419         return r < 0 ? r : keyslot;
420 }
421
422 int LUKS2_token_open_and_activate_any(struct crypt_device *cd,
423         struct luks2_hdr *hdr,
424         const char *name,
425         uint32_t flags)
426 {
427         char *buffer;
428         json_object *tokens_jobj;
429         size_t buffer_len;
430         int keyslot, token, r = -EINVAL;
431         struct volume_key *vk = NULL;
432
433         json_object_object_get_ex(hdr->jobj, "tokens", &tokens_jobj);
434
435         json_object_object_foreach(tokens_jobj, slot, val) {
436                 UNUSED(val);
437                 token = atoi(slot);
438
439                 r = LUKS2_token_open(cd, hdr, token, &buffer, &buffer_len, NULL);
440                 if (r < 0)
441                         continue;
442
443                 r = LUKS2_keyslot_open_by_token(cd, hdr, token,
444                                                 (flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY) ?
445                                                 CRYPT_ANY_SEGMENT : CRYPT_DEFAULT_SEGMENT,
446                                                 buffer, buffer_len, &vk);
447                 LUKS2_token_buffer_free(cd, token, buffer, buffer_len);
448                 if (r >= 0)
449                         break;
450         }
451
452         keyslot = r;
453
454         if (r >= 0 && (name || (flags & CRYPT_ACTIVATE_KEYRING_KEY)) && crypt_use_keyring_for_vk(cd)) {
455                 if (!(r = LUKS2_volume_key_load_in_keyring_by_keyslot(cd, hdr, vk, keyslot)))
456                         flags |= CRYPT_ACTIVATE_KEYRING_KEY;
457         }
458
459         if (r >= 0 && name)
460                 r = LUKS2_activate(cd, name, vk, flags);
461
462         if (r < 0)
463                 crypt_drop_keyring_key(cd, vk);
464         crypt_free_volume_key(vk);
465
466         return r < 0 ? r : keyslot;
467 }
468
469 void LUKS2_token_dump(struct crypt_device *cd, int token)
470 {
471         const crypt_token_handler *h;
472         json_object *jobj_token;
473
474         h = LUKS2_token_handler(cd, token);
475         if (h && h->dump) {
476                 jobj_token = LUKS2_get_token_jobj(crypt_get_hdr(cd, CRYPT_LUKS2), token);
477                 if (jobj_token)
478                         h->dump(cd, json_object_to_json_string_ext(jobj_token,
479                                 JSON_C_TO_STRING_PLAIN | JSON_C_TO_STRING_NOSLASHESCAPE));
480         }
481 }
482
483 int LUKS2_token_json_get(struct crypt_device *cd, struct luks2_hdr *hdr,
484                            int token, const char **json)
485 {
486         json_object *jobj_token;
487
488         jobj_token = LUKS2_get_token_jobj(hdr, token);
489         if (!jobj_token)
490                 return -EINVAL;
491
492         *json = json_object_to_json_string_ext(jobj_token,
493                 JSON_C_TO_STRING_PLAIN | JSON_C_TO_STRING_NOSLASHESCAPE);
494         return 0;
495 }
496
497 static int assign_one_keyslot(struct crypt_device *cd, struct luks2_hdr *hdr,
498                               int token, int keyslot, int assign)
499 {
500         json_object *jobj1, *jobj_token, *jobj_token_keyslots;
501         char num[16];
502
503         log_dbg(cd, "Keyslot %i %s token %i.", keyslot, assign ? "assigned to" : "unassigned from", token);
504
505         jobj_token = LUKS2_get_token_jobj(hdr, token);
506         if (!jobj_token)
507                 return -EINVAL;
508
509         json_object_object_get_ex(jobj_token, "keyslots", &jobj_token_keyslots);
510         if (!jobj_token_keyslots)
511                 return -EINVAL;
512
513         snprintf(num, sizeof(num), "%d", keyslot);
514         if (assign) {
515                 jobj1 = LUKS2_array_jobj(jobj_token_keyslots, num);
516                 if (!jobj1)
517                         json_object_array_add(jobj_token_keyslots, json_object_new_string(num));
518         } else {
519                 jobj1 = LUKS2_array_remove(jobj_token_keyslots, num);
520                 if (jobj1)
521                         json_object_object_add(jobj_token, "keyslots", jobj1);
522         }
523
524         return 0;
525 }
526
527 static int assign_one_token(struct crypt_device *cd, struct luks2_hdr *hdr,
528                             int keyslot, int token, int assign)
529 {
530         json_object *jobj_keyslots;
531         int r = 0;
532
533         if (!LUKS2_get_token_jobj(hdr, token))
534                 return -EINVAL;
535
536         if (keyslot == CRYPT_ANY_SLOT) {
537                 json_object_object_get_ex(hdr->jobj, "keyslots", &jobj_keyslots);
538
539                 json_object_object_foreach(jobj_keyslots, key, val) {
540                         UNUSED(val);
541                         r = assign_one_keyslot(cd, hdr, token, atoi(key), assign);
542                         if (r < 0)
543                                 break;
544                 }
545         } else
546                 r = assign_one_keyslot(cd, hdr, token, keyslot, assign);
547
548         return r;
549 }
550
551 int LUKS2_token_assign(struct crypt_device *cd, struct luks2_hdr *hdr,
552                         int keyslot, int token, int assign, int commit)
553 {
554         json_object *jobj_tokens;
555         int r = 0;
556
557         if (token == CRYPT_ANY_TOKEN) {
558                 json_object_object_get_ex(hdr->jobj, "tokens", &jobj_tokens);
559
560                 json_object_object_foreach(jobj_tokens, key, val) {
561                         UNUSED(val);
562                         r = assign_one_token(cd, hdr, keyslot, atoi(key), assign);
563                         if (r < 0)
564                                 break;
565                 }
566         } else
567                 r = assign_one_token(cd, hdr, keyslot, token, assign);
568
569         if (r < 0)
570                 return r;
571
572         // FIXME: do not write header in nothing changed
573         if (commit)
574                 return LUKS2_hdr_write(cd, hdr) ?: token;
575
576         return token;
577 }
578
579 int LUKS2_token_is_assigned(struct crypt_device *cd, struct luks2_hdr *hdr,
580                             int keyslot, int token)
581 {
582         int i;
583         json_object *jobj_token, *jobj_token_keyslots, *jobj;
584
585         if (keyslot < 0 || keyslot >= LUKS2_KEYSLOTS_MAX || token < 0 || token >= LUKS2_TOKENS_MAX)
586                 return -EINVAL;
587
588         jobj_token = LUKS2_get_token_jobj(hdr, token);
589         if (!jobj_token)
590                 return -ENOENT;
591
592         json_object_object_get_ex(jobj_token, "keyslots", &jobj_token_keyslots);
593
594         for (i = 0; i < (int) json_object_array_length(jobj_token_keyslots); i++) {
595                 jobj = json_object_array_get_idx(jobj_token_keyslots, i);
596                 if (keyslot == atoi(json_object_get_string(jobj)))
597                         return 0;
598         }
599
600         return -ENOENT;
601 }
602
603 int LUKS2_tokens_count(struct luks2_hdr *hdr)
604 {
605         json_object *jobj_tokens = LUKS2_get_tokens_jobj(hdr);
606         if (!jobj_tokens)
607                 return -EINVAL;
608
609         return json_object_object_length(jobj_tokens);
610 }