1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "errno-util.h"
4 #include "format-table.h"
6 #include "homectl-pkcs11.h"
7 #include "libcrypt-util.h"
8 #include "memory-util.h"
9 #include "openssl-util.h"
10 #include "pkcs11-util.h"
13 static int add_pkcs11_encrypted_key(
16 const void *encrypted_key
, size_t encrypted_key_size
,
17 const void *decrypted_key
, size_t decrypted_key_size
) {
19 _cleanup_(json_variant_unrefp
) JsonVariant
*l
= NULL
, *w
= NULL
, *e
= NULL
;
20 _cleanup_(erase_and_freep
) char *base64_encoded
= NULL
, *hashed
= NULL
;
21 ssize_t base64_encoded_size
;
26 assert(encrypted_key
);
27 assert(encrypted_key_size
> 0);
28 assert(decrypted_key
);
29 assert(decrypted_key_size
> 0);
31 /* Before using UNIX hashing on the supplied key we base64 encode it, since crypt_r() and friends
32 * expect a NUL terminated string, and we use a binary key */
33 base64_encoded_size
= base64mem(decrypted_key
, decrypted_key_size
, &base64_encoded
);
34 if (base64_encoded_size
< 0)
35 return log_error_errno(base64_encoded_size
, "Failed to base64 encode secret key: %m");
37 r
= hash_password(base64_encoded
, &hashed
);
39 return log_error_errno(errno_or_else(EINVAL
), "Failed to UNIX hash secret key: %m");
41 r
= json_build(&e
, JSON_BUILD_OBJECT(
42 JSON_BUILD_PAIR("uri", JSON_BUILD_STRING(uri
)),
43 JSON_BUILD_PAIR("data", JSON_BUILD_BASE64(encrypted_key
, encrypted_key_size
)),
44 JSON_BUILD_PAIR("hashedPassword", JSON_BUILD_STRING(hashed
))));
46 return log_error_errno(r
, "Failed to build encrypted JSON key object: %m");
48 w
= json_variant_ref(json_variant_by_key(*v
, "privileged"));
49 l
= json_variant_ref(json_variant_by_key(w
, "pkcs11EncryptedKey"));
51 r
= json_variant_append_array(&l
, e
);
53 return log_error_errno(r
, "Failed append PKCS#11 encrypted key: %m");
55 r
= json_variant_set_field(&w
, "pkcs11EncryptedKey", l
);
57 return log_error_errno(r
, "Failed to set PKCS#11 encrypted key: %m");
59 r
= json_variant_set_field(v
, "privileged", w
);
61 return log_error_errno(r
, "Failed to update privileged field: %m");
66 static int add_pkcs11_token_uri(JsonVariant
**v
, const char *uri
) {
67 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
;
68 _cleanup_strv_free_
char **l
= NULL
;
74 w
= json_variant_ref(json_variant_by_key(*v
, "pkcs11TokenUri"));
76 r
= json_variant_strv(w
, &l
);
78 return log_error_errno(r
, "Failed to parse PKCS#11 token list: %m");
80 if (strv_contains(l
, uri
))
84 r
= strv_extend(&l
, uri
);
88 w
= json_variant_unref(w
);
89 r
= json_variant_new_array_strv(&w
, l
);
91 return log_error_errno(r
, "Failed to create PKCS#11 token URI JSON: %m");
93 r
= json_variant_set_field(v
, "pkcs11TokenUri", w
);
95 return log_error_errno(r
, "Failed to update PKCS#11 token URI list: %m");
100 int identity_add_token_pin(JsonVariant
**v
, const char *pin
) {
101 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
, *l
= NULL
;
102 _cleanup_strv_free_erase_
char **pins
= NULL
;
110 w
= json_variant_ref(json_variant_by_key(*v
, "secret"));
111 l
= json_variant_ref(json_variant_by_key(w
, "tokenPin"));
113 r
= json_variant_strv(l
, &pins
);
115 return log_error_errno(r
, "Failed to convert PIN array: %m");
117 if (strv_contains(pins
, pin
))
120 r
= strv_extend(&pins
, pin
);
126 l
= json_variant_unref(l
);
128 r
= json_variant_new_array_strv(&l
, pins
);
130 return log_error_errno(r
, "Failed to allocate new PIN array JSON: %m");
132 json_variant_sensitive(l
);
134 r
= json_variant_set_field(&w
, "tokenPin", l
);
136 return log_error_errno(r
, "Failed to update PIN field: %m");
138 r
= json_variant_set_field(v
, "secret", w
);
140 return log_error_errno(r
, "Failed to update secret object: %m");
145 static int acquire_pkcs11_certificate(
147 const char *askpw_friendly_name
,
148 const char *askpw_icon_name
,
150 char **ret_pin_used
) {
152 return pkcs11_acquire_certificate(uri
, askpw_friendly_name
, askpw_icon_name
, ret_cert
, ret_pin_used
);
154 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
155 "PKCS#11 tokens not supported on this build.");
159 int identity_add_pkcs11_key_data(JsonVariant
**v
, const char *uri
) {
160 _cleanup_(erase_and_freep
) void *decrypted_key
= NULL
, *saved_key
= NULL
;
161 _cleanup_(erase_and_freep
) char *pin
= NULL
;
162 size_t decrypted_key_size
, saved_key_size
;
163 _cleanup_(X509_freep
) X509
*cert
= NULL
;
168 r
= acquire_pkcs11_certificate(uri
, "home directory operation", "user-home", &cert
, &pin
);
172 r
= x509_generate_volume_keys(cert
, &decrypted_key
, &decrypted_key_size
, &saved_key
, &saved_key_size
);
174 return log_error_errno(r
, "Failed to generate volume keys: %m");
176 /* Add the token URI to the public part of the record. */
177 r
= add_pkcs11_token_uri(v
, uri
);
181 /* Include the encrypted version of the random key we just generated in the privileged part of the record */
182 r
= add_pkcs11_encrypted_key(
185 saved_key
, saved_key_size
,
186 decrypted_key
, decrypted_key_size
);
190 /* If we acquired the PIN also include it in the secret section of the record, so that systemd-homed
191 * can use it if it needs to, given that it likely needs to decrypt the key again to pass to LUKS or
193 r
= identity_add_token_pin(v
, pin
);