Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / lib / luks2 / luks2_token_keyring.c
1 /*
2  * LUKS - Linux Unified Key Setup v2, kernel keyring token
3  *
4  * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2016-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 "luks2_internal.h"
23
24 int keyring_open(struct crypt_device *cd,
25                                 int token,
26                                 char **buffer,
27                                 size_t *buffer_len,
28                                 void *usrptr __attribute__((unused)))
29 {
30         json_object *jobj_token, *jobj_key;
31         struct luks2_hdr *hdr;
32         int r;
33
34         if (!(hdr = crypt_get_hdr(cd, CRYPT_LUKS2)))
35                 return -EINVAL;
36
37         jobj_token = LUKS2_get_token_jobj(hdr, token);
38         if (!jobj_token)
39                 return -EINVAL;
40
41         json_object_object_get_ex(jobj_token, "key_description", &jobj_key);
42
43         r = keyring_get_passphrase(json_object_get_string(jobj_key), buffer, buffer_len);
44         if (r == -ENOTSUP) {
45                 log_dbg(cd, "Kernel keyring features disabled.");
46                 return -ENOENT;
47         } else if (r < 0) {
48                 log_dbg(cd, "keyring_get_passphrase failed (error %d)", r);
49                 return -EPERM;
50         }
51
52         return 0;
53 }
54
55 int keyring_validate(struct crypt_device *cd __attribute__((unused)),
56                                     const char *json)
57 {
58         enum json_tokener_error jerr;
59         json_object *jobj_token, *jobj_key;
60         int r = 1;
61
62         log_dbg(cd, "Validating keyring token json");
63
64         jobj_token = json_tokener_parse_verbose(json, &jerr);
65         if (!jobj_token) {
66                 log_dbg(cd, "Keyring token JSON parse failed.");
67                 return r;
68         }
69
70         if (json_object_object_length(jobj_token) != 3) {
71                 log_dbg(cd, "Keyring token is expected to have exactly 3 fields.");
72                 goto out;
73         }
74
75         if (!json_object_object_get_ex(jobj_token, "key_description", &jobj_key)) {
76                 log_dbg(cd, "missing key_description field.");
77                 goto out;
78         }
79
80         if (!json_object_is_type(jobj_key, json_type_string)) {
81                 log_dbg(cd, "key_description is not a string.");
82                 goto out;
83         }
84
85         /* TODO: perhaps check that key description is in '%s:%s'
86          * format where both strings are not empty */
87         r = !strlen(json_object_get_string(jobj_key));
88 out:
89         json_object_put(jobj_token);
90         return r;
91 }
92
93 void keyring_dump(struct crypt_device *cd, const char *json)
94 {
95         enum json_tokener_error jerr;
96         json_object *jobj_token, *jobj_key;
97
98         jobj_token = json_tokener_parse_verbose(json, &jerr);
99         if (!jobj_token)
100                 return;
101
102         if (!json_object_object_get_ex(jobj_token, "key_description", &jobj_key)) {
103                 json_object_put(jobj_token);
104                 return;
105         }
106
107         log_std(cd, "\tKey description: %s\n", json_object_get_string(jobj_key));
108
109         json_object_put(jobj_token);
110 }
111
112 int LUKS2_token_keyring_json(char *buffer, size_t buffer_size,
113         const struct crypt_token_params_luks2_keyring *keyring_params)
114 {
115         int r;
116
117         r = snprintf(buffer, buffer_size, "{ \"type\": \"%s\", \"keyslots\":[],\"key_description\":\"%s\"}",
118                  LUKS2_TOKEN_KEYRING, keyring_params->key_description);
119         if (r < 0 || (size_t)r >= buffer_size)
120                 return -EINVAL;
121
122         return 0;
123 }
124
125 int LUKS2_token_keyring_get(struct luks2_hdr *hdr,
126         int token, struct crypt_token_params_luks2_keyring *keyring_params)
127 {
128         json_object *jobj_token, *jobj;
129
130         jobj_token = LUKS2_get_token_jobj(hdr, token);
131         json_object_object_get_ex(jobj_token, "type", &jobj);
132         assert(!strcmp(json_object_get_string(jobj), LUKS2_TOKEN_KEYRING));
133
134         json_object_object_get_ex(jobj_token, "key_description", &jobj);
135
136         keyring_params->key_description = json_object_get_string(jobj);
137
138         return token;
139 }
140
141 void keyring_buffer_free(void *buffer, size_t buffer_len __attribute__((unused)))
142 {
143         crypt_safe_free(buffer);
144 }