Debian Codegraph @

src/shared/creds-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "sd-id128.h"
5
6 #include "fd-util.h"
7 #include "shared-forward.h"
8
9 #define CREDENTIAL_NAME_MAX FDNAME_MAX
10
11 /* Put a size limit on the individual credential */
12 #define CREDENTIAL_SIZE_MAX (1U * U64_MB)
13
14 /* Refuse to store more than 1M per service, after all this is unswappable memory. Note that for now we put
15 * this to the same limit as the per-credential limit, i.e. if the user has n > 1 credentials instead of 1 it
16 * won't get them more space. */
17 #define CREDENTIALS_TOTAL_SIZE_MAX CREDENTIAL_SIZE_MAX
18
19 /* Put a size limit on encrypted credentials (which is the same as the unencrypted size plus a spacious 128K of extra
20 * space for headers, IVs, exported TPM2 key material and so on. */
21 #define CREDENTIAL_ENCRYPTED_SIZE_MAX (CREDENTIAL_SIZE_MAX + 128U * U64_KB)
22
23 bool credential_name_valid(const char *s);
24 bool credential_glob_valid(const char *s);
25
26 /* Where creds have been passed to the local execution context */
27 int get_credentials_dir(const char **ret);
28 int get_encrypted_credentials_dir(const char **ret);
29
30 int open_credentials_dir(void);
31
32 /* Where creds have been passed to the system */
33 #define SYSTEM_CREDENTIALS_DIRECTORY "/run/credentials/@system"
34 #define ENCRYPTED_SYSTEM_CREDENTIALS_DIRECTORY "/run/credentials/@encrypted"
35
36 /* Where system creds have been passed */
37 int get_system_credentials_dir(const char **ret);
38 int get_encrypted_system_credentials_dir(const char **ret);
39
40 int read_credential(const char *name, void **ret, size_t *ret_size); /* use in services! */
41 int read_credential_with_decryption(const char *name, void **ret, size_t *ret_size); /* use in generators + pid1! */
42
43 int read_credential_strings_many_internal(const char *first_name, char **first_value, ...);
44
45 #define read_credential_strings_many(first_name, first_value, ...) \
46 read_credential_strings_many_internal(first_name, first_value, __VA_ARGS__, NULL)
47
48 int read_credential_bool(const char *name);
49
50 typedef enum CredentialSecretFlags {
51 CREDENTIAL_SECRET_GENERATE = 1 << 0,
52 CREDENTIAL_SECRET_WARN_NOT_ENCRYPTED = 1 << 1,
53 CREDENTIAL_SECRET_FAIL_ON_TEMPORARY_FS = 1 << 2,
54 } CredentialSecretFlags;
55
56 int get_credential_host_secret(CredentialSecretFlags flags, struct iovec *ret);
57
58 int get_credential_user_password(const char *username, char **ret_password, bool *ret_is_hashed);
59
60 typedef enum CredentialFlags {
61 CREDENTIAL_ALLOW_NULL = 1 << 0, /* allow decryption with NULL key, even if TPM is around */
62 CREDENTIAL_REFUSE_NULL = 1 << 1, /* deny decryption with NULL key, even if SecureBoot is off */
63 CREDENTIAL_ANY_SCOPE = 1 << 2, /* allow decryption of both system and user credentials */
64
65 /* Only used by ipc_{encrypt,decrypt}_credential */
66 CREDENTIAL_IPC_ALLOW_INTERACTIVE = 1 << 3,
67 } CredentialFlags;
68
69 /* The four modes we support: keyed only by on-disk key, only by TPM2 HMAC key, and by the combination of
70 * both, as well as one with a fixed zero length key if TPM2 is missing (the latter of course provides no
71 * authenticity or confidentiality, but is still useful for integrity protection, and makes things simpler
72 * for us to handle). */
73 #define CRED_AES256_GCM_BY_HOST SD_ID128_MAKE(5a,1c,6a,86,df,9d,40,96,b1,d5,a6,5e,08,62,f1,9a)
74 #define CRED_AES256_GCM_BY_HOST_SCOPED SD_ID128_MAKE(55,b9,ed,1d,38,59,4d,43,a8,31,9d,2e,bb,33,2a,c6)
75 #define CRED_AES256_GCM_BY_TPM2_HMAC SD_ID128_MAKE(0c,7c,c0,7b,11,76,45,91,9c,4b,0b,ea,08,bc,20,fe)
76 #define CRED_AES256_GCM_BY_TPM2_HMAC_WITH_PK SD_ID128_MAKE(fa,f7,eb,93,41,e3,41,2c,a1,a4,36,f9,5a,29,36,2f)
77 #define CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC SD_ID128_MAKE(93,a8,94,09,48,74,44,90,90,ca,f2,fc,93,ca,b5,53)
78 #define CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_SCOPED \
79 SD_ID128_MAKE(ef,4a,c1,36,79,a9,48,0e,a7,db,68,89,7f,9f,16,5d)
80 #define CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_WITH_PK \
81 SD_ID128_MAKE(af,49,50,a8,49,13,4e,b1,a7,38,46,30,4f,f3,0c,05)
82 #define CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_WITH_PK_SCOPED \
83 SD_ID128_MAKE(ad,bc,4c,a3,ef,b6,42,01,ba,88,1b,6f,2e,40,95,ea)
84 #define CRED_AES256_GCM_BY_NULL SD_ID128_MAKE(05,84,69,da,f6,f5,43,24,80,05,49,da,0f,8e,a2,fb)
85
86 /* Five special IDs to pick a general automatic mode. These IDs will never be stored on disk, but are useful
87 * only internally while figuring out what precisely to write to disk. To mark that these aren't a "real"
88 * type, we'll prefix them with an underscore. */
89
90 /* Use TPM2 if available + host if available and on physical media. If neither are available, fail. */
91 #define _CRED_AUTO SD_ID128_MAKE(a2,19,cb,07,85,b2,4c,04,b1,6d,18,ca,b9,d2,ee,01)
92
93 /* Use best TPM2, and do not use host, and fail if no TPM */
94 #define _CRED_AUTO_TPM2 SD_ID128_MAKE(45,f3,a6,7e,0c,12,42,56,a4,ee,75,eb,44,c6,5a,6f)
95
96 /* Use TPM2 *and* host, and fail if one of the two isn't available. */
97 #define _CRED_AUTO_HOST_AND_TPM2 SD_ID128_MAKE(da,f6,7a,60,d3,eb,47,b3,a9,be,2f,d5,fe,c2,15,22)
98
99 /* Like _CRED_AUTO_TPM2, but uses "null" if not TPM is around */
100 #define _CRED_AUTO_INITRD SD_ID128_MAKE(02,dc,8e,de,3a,02,43,ab,a9,ec,54,9c,05,e6,a0,71)
101
102 /* Like _CRED_AUTO, but with per-UID scoping */
103 #define _CRED_AUTO_SCOPED SD_ID128_MAKE(23,88,96,85,6f,74,48,8a,9c,78,6f,6a,b0,e7,3b,6a)
104
105 #define CRED_KEY_IS_VALID(key) \
106 sd_id128_in_set((key), \
107 CRED_AES256_GCM_BY_HOST, \
108 CRED_AES256_GCM_BY_HOST_SCOPED, \
109 CRED_AES256_GCM_BY_TPM2_HMAC, \
110 CRED_AES256_GCM_BY_TPM2_HMAC_WITH_PK, \
111 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC, \
112 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_SCOPED, \
113 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_WITH_PK, \
114 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_WITH_PK_SCOPED, \
115 CRED_AES256_GCM_BY_NULL)
116 #define CRED_KEY_IS_AUTO(key) \
117 sd_id128_in_set((key), \
118 _CRED_AUTO, \
119 _CRED_AUTO_TPM2, \
120 _CRED_AUTO_HOST_AND_TPM2, \
121 _CRED_AUTO_INITRD, \
122 _CRED_AUTO_SCOPED)
123 #define CRED_KEY_IS_SCOPED(key) \
124 sd_id128_in_set((key), \
125 _CRED_AUTO_SCOPED, \
126 CRED_AES256_GCM_BY_HOST_SCOPED, \
127 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_SCOPED, \
128 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_WITH_PK_SCOPED)
129 #define CRED_KEY_REQUIRES_HOST(key) \
130 sd_id128_in_set((key), \
131 _CRED_AUTO_HOST_AND_TPM2, \
132 CRED_AES256_GCM_BY_HOST, \
133 CRED_AES256_GCM_BY_HOST_SCOPED, \
134 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC, \
135 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_SCOPED, \
136 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_WITH_PK, \
137 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_WITH_PK_SCOPED)
138 #define CRED_KEY_WANTS_HOST(key) \
139 sd_id128_in_set((key), \
140 _CRED_AUTO, \
141 _CRED_AUTO_SCOPED)
142 #define CRED_KEY_REQUIRES_TPM2(key) \
143 sd_id128_in_set((key), \
144 _CRED_AUTO_TPM2, \
145 _CRED_AUTO_HOST_AND_TPM2, \
146 CRED_AES256_GCM_BY_TPM2_HMAC, \
147 CRED_AES256_GCM_BY_TPM2_HMAC_WITH_PK, \
148 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC, \
149 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_SCOPED, \
150 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_WITH_PK, \
151 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_WITH_PK_SCOPED)
152 #define CRED_KEY_WANTS_TPM2(key) \
153 sd_id128_in_set((key), \
154 _CRED_AUTO, \
155 _CRED_AUTO_INITRD, \
156 _CRED_AUTO_SCOPED)
157 #define CRED_KEY_REQUIRES_TPM2_PK(key) \
158 sd_id128_in_set((key), \
159 CRED_AES256_GCM_BY_TPM2_HMAC_WITH_PK, \
160 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_WITH_PK, \
161 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC_WITH_PK_SCOPED)
162 #define CRED_KEY_WANTS_TPM2_PK(key) \
163 sd_id128_in_set((key), \
164 _CRED_AUTO, \
165 _CRED_AUTO_TPM2, \
166 _CRED_AUTO_HOST_AND_TPM2, \
167 _CRED_AUTO_INITRD, \
168 _CRED_AUTO_SCOPED)
169
170 int encrypt_credential_and_warn(sd_id128_t with_key, const char *name, usec_t timestamp, usec_t not_after, const char *tpm2_device, uint32_t tpm2_hash_pcr_mask, const char *tpm2_pubkey_path, uint32_t tpm2_pubkey_pcr_mask, uid_t uid, const struct iovec *input, CredentialFlags flags, struct iovec *ret);
171 int decrypt_credential_and_warn(const char *validate_name, usec_t validate_timestamp, const char *tpm2_device, const char *tpm2_signature_path, uid_t uid, const struct iovec *input, CredentialFlags flags, struct iovec *ret);
172
173 int ipc_encrypt_credential(const char *name, usec_t timestamp, usec_t not_after, uid_t uid, const struct iovec *input, CredentialFlags flags, struct iovec *ret);
174 int ipc_decrypt_credential(const char *validate_name, usec_t validate_timestamp, uid_t uid, const struct iovec *input, CredentialFlags flags, struct iovec *ret);
175
176 int get_global_boot_credentials_path(char **ret);
177
178 typedef struct PickUpCredential {
179 const char *credential_prefix;
180 const char *target_dir;
181 const char *filename_suffix;
182 } PickUpCredential;
183
184 int pick_up_credentials(const PickUpCredential *table, size_t n_table_entry);
185
186 typedef struct CredentialsVarlinkError {
187 const char *id;
188 int errnum;
189 const char *msg;
190 } CredentialsVarlinkError;
191
192 const CredentialsVarlinkError* credentials_varlink_error_by_id(const char *id) _pure_;
193 const CredentialsVarlinkError* credentials_varlink_error_by_errno(int errnum) _const_;