summaryrefslogtreecommitdiff
path: root/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c
blob: f0286ec1bf4f76864962a93e6fed17859c9b4c5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "alloc-util.h"
#include "ask-password-api.h"
#include "env-util.h"
#include "hexdecoct.h"
#include "json.h"
#include "log.h"
#include "luks2-tpm2.h"
#include "parse-util.h"
#include "random-util.h"
#include "strv.h"
#include "tpm2-util.h"

int acquire_luks2_key(
                uint32_t pcr_mask,
                uint16_t pcr_bank,
                uint16_t primary_alg,
                const char *device,
                const void *key_data,
                size_t key_data_size,
                const void *policy_hash,
                size_t policy_hash_size,
                TPM2Flags flags,
                const char *pin,
                void **ret_decrypted_key,
                size_t *ret_decrypted_key_size) {

        _cleanup_free_ char *auto_device = NULL;
        int r;

        assert(ret_decrypted_key);
        assert(ret_decrypted_key_size);

        if (!device) {
                r = tpm2_find_device_auto(LOG_DEBUG, &auto_device);
                if (r == -ENODEV)
                        return -EAGAIN; /* Tell the caller to wait for a TPM2 device to show up */
                if (r < 0)
                        return r;

                device = auto_device;
        }

        if ((flags & TPM2_FLAGS_USE_PIN) && !pin)
                return -ENOANO;

        return tpm2_unseal(
                        device,
                        pcr_mask, pcr_bank,
                        primary_alg,
                        key_data, key_data_size,
                        policy_hash, policy_hash_size, pin,
                        ret_decrypted_key, ret_decrypted_key_size);
}

/* this function expects valid "systemd-tpm2" in json */
int parse_luks2_tpm2_data(
                const char *json,
                uint32_t search_pcr_mask,
                uint32_t *ret_pcr_mask,
                uint16_t *ret_pcr_bank,
                uint16_t *ret_primary_alg,
                char **ret_base64_blob,
                char **ret_hex_policy_hash,
                TPM2Flags *ret_flags) {

        int r;
        JsonVariant *w, *e;
        uint32_t pcr_mask = 0;
        uint16_t pcr_bank = UINT16_MAX, primary_alg = TPM2_ALG_ECC;
        _cleanup_free_ char *base64_blob = NULL, *hex_policy_hash = NULL;
        _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
        TPM2Flags flags = 0;

        assert(json);
        assert(ret_pcr_mask);
        assert(ret_pcr_bank);
        assert(ret_primary_alg);
        assert(ret_base64_blob);
        assert(ret_hex_policy_hash);

        r = json_parse(json, 0, &v, NULL, NULL);
        if (r < 0)
                return -EINVAL;

        w = json_variant_by_key(v, "tpm2-pcrs");
        if (!w || !json_variant_is_array(w))
                return -EINVAL;

        JSON_VARIANT_ARRAY_FOREACH(e, w) {
                uint64_t u;

                if (!json_variant_is_number(e))
                        return -EINVAL;

                u = json_variant_unsigned(e);
                if (u >= TPM2_PCRS_MAX)
                        return -EINVAL;

                pcr_mask |= UINT32_C(1) << u;
        }

        if (search_pcr_mask != UINT32_MAX &&
            search_pcr_mask != pcr_mask)
                return -ENXIO;

        w = json_variant_by_key(v, "tpm2-pcr-bank");
        if (w) {
                /* The PCR bank field is optional */

                if (!json_variant_is_string(w))
                        return -EINVAL;

                r = tpm2_pcr_bank_from_string(json_variant_string(w));
                if (r < 0)
                        return r;

                pcr_bank = r;
        }

        w = json_variant_by_key(v, "tpm2-primary-alg");
        if (w) {
                /* The primary key algorithm is optional */

                if (!json_variant_is_string(w))
                        return -EINVAL;

                r = tpm2_primary_alg_from_string(json_variant_string(w));
                if (r < 0)
                        return r;

                primary_alg = r;
        }

        w = json_variant_by_key(v, "tpm2-blob");
        if (!w || !json_variant_is_string(w))
                return -EINVAL;

        base64_blob = strdup(json_variant_string(w));
        if (!base64_blob)
                return -ENOMEM;

        w = json_variant_by_key(v, "tpm2-policy-hash");
        if (!w || !json_variant_is_string(w))
                return -EINVAL;

        hex_policy_hash = strdup(json_variant_string(w));
        if (!hex_policy_hash)
                return -ENOMEM;

        w = json_variant_by_key(v, "tpm2-pin");
        if (w) {
                if (!json_variant_is_boolean(w))
                        return -EINVAL;

                if (json_variant_boolean(w))
                        flags |= TPM2_FLAGS_USE_PIN;
        }

        *ret_pcr_mask = pcr_mask;
        *ret_pcr_bank = pcr_bank;
        *ret_primary_alg = primary_alg;
        *ret_base64_blob = TAKE_PTR(base64_blob);
        *ret_hex_policy_hash = TAKE_PTR(hex_policy_hash);
        *ret_flags = flags;

        return 0;
}