Debian Codegraph @

src/cryptenroll/cryptenroll-pkcs11.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "cryptenroll-pkcs11.h"
5 #include "crypto-util.h"
6 #include "cryptsetup-util.h"
7 #include "hexdecoct.h"
8 #include "json-util.h"
9 #include "pkcs11-util.h"
10
11 #if HAVE_P11KIT && HAVE_OPENSSL
12 static int uri_set_private_class(const char *uri, char **ret_uri) {
13 _cleanup_(p11_kit_uri_freep) P11KitUri *p11kit_uri = NULL;
14 _cleanup_free_ char *private_uri = NULL;
15 int r;
16
17 assert(ret_uri);
18
19 r = uri_from_string(uri, &p11kit_uri);
20 if (r < 0)
21 return log_error_errno(r, "Failed to parse PKCS#11 URI '%s': %m", uri);
22
23 if (sym_p11_kit_uri_get_attribute(p11kit_uri, CKA_CLASS)) {
24 CK_OBJECT_CLASS class = CKO_PRIVATE_KEY;
25 CK_ATTRIBUTE attribute = { CKA_CLASS, &class, sizeof(class) };
26
27 if (sym_p11_kit_uri_set_attribute(p11kit_uri, &attribute) != P11_KIT_URI_OK)
28 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to set class for URI '%s'.", uri);
29
30 if (sym_p11_kit_uri_format(p11kit_uri, P11_KIT_URI_FOR_ANY, &private_uri) != P11_KIT_URI_OK)
31 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to format PKCS#11 URI.");
32 }
33
34 *ret_uri = TAKE_PTR(private_uri);
35 return 0;
36 }
37 #endif
38
39 int enroll_pkcs11(struct crypt_device *cd, const struct iovec *volume_key,const char *uri) {
40 #if HAVE_P11KIT && HAVE_OPENSSL
41 _cleanup_(erase_and_freep) void *decrypted_key = NULL;
42 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
43 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
44 _cleanup_free_ char *keyslot_as_string = NULL, *private_uri = NULL;
45 size_t decrypted_key_size, saved_key_size;
46 _cleanup_free_ void *saved_key = NULL;
47 _cleanup_(EVP_PKEY_freep) EVP_PKEY *pkey = NULL;
48 Pkcs11RsaPadding rsa_padding = _PKCS11_RSA_PADDING_INVALID;
49 ssize_t base64_encoded_size;
50 const char *node;
51 int r;
52
53 assert_se(cd);
54 assert_se(iovec_is_set(volume_key));
55 assert_se(uri);
56
57 assert_se(node = sym_crypt_get_device_name(cd));
58
59 r = pkcs11_acquire_public_key(
60 uri,
61 "volume enrollment operation",
62 "drive-harddisk",
63 "cryptenroll.pkcs11-pin",
64 /* askpw_flags= */ 0,
65 &pkey,
66 &rsa_padding,
67 /* ret_pin_used= */ NULL);
68 if (r < 0)
69 return r;
70
71 r = pkey_generate_volume_keys(pkey,
72 pkcs11_rsa_padding_to_oaep_hash(rsa_padding),
73 &decrypted_key, &decrypted_key_size, &saved_key, &saved_key_size);
74 if (r < 0)
75 return log_error_errno(r, "Failed to generate volume keys: %m");
76
77 /* Let's base64 encode the key to use, for compat with homed (and it's easier to type it in by
78 * keyboard, if that might ever end up being necessary.) */
79 base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
80 if (base64_encoded_size < 0)
81 return log_error_errno(base64_encoded_size, "Failed to base64 encode secret key: %m");
82
83 r = cryptsetup_set_minimal_pbkdf(cd);
84 if (r < 0)
85 return log_error_errno(r, "Failed to set minimal PBKDF: %m");
86
87 int keyslot = sym_crypt_keyslot_add_by_volume_key(
88 cd,
89 CRYPT_ANY_SLOT,
90 volume_key->iov_base,
91 volume_key->iov_len,
92 base64_encoded,
93 base64_encoded_size);
94 if (keyslot < 0)
95 return log_error_errno(keyslot, "Failed to add new PKCS#11 key to %s: %m", node);
96
97 if (asprintf(&keyslot_as_string, "%i", keyslot) < 0)
98 return log_oom();
99
100 /* Change 'type=cert' or 'type=public' in the provided URI to 'type=private' before storing in
101 a LUKS2 header. This allows users to use output of some PKCS#11 tools directly without
102 modifications. */
103 r = uri_set_private_class(uri, &private_uri);
104 if (r < 0)
105 return r;
106
107 r = sd_json_buildo(&v,
108 SD_JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-pkcs11")),
109 SD_JSON_BUILD_PAIR("keyslots", SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_STRING(keyslot_as_string))),
110 SD_JSON_BUILD_PAIR_STRING("pkcs11-uri", private_uri ?: uri),
111 SD_JSON_BUILD_PAIR_BASE64("pkcs11-key", saved_key, saved_key_size),
112 SD_JSON_BUILD_PAIR_CONDITION(rsa_padding > PKCS11_RSA_PADDING_PKCS1V15,
113 "pkcs11-padding", SD_JSON_BUILD_STRING(pkcs11_rsa_padding_to_string(rsa_padding))));
114 if (r < 0)
115 return log_error_errno(r, "Failed to prepare PKCS#11 JSON token object: %m");
116
117 r = cryptsetup_add_token_json(cd, v);
118 if (r < 0)
119 return log_error_errno(r, "Failed to add PKCS#11 JSON token to LUKS2 header: %m");
120
121 log_info("New PKCS#11 token enrolled as key slot %i.", keyslot);
122 return keyslot;
123 #else
124 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "PKCS#11 key enrollment not supported.");
125 #endif
126 }