| 1 |
/* |
| 2 |
* Copyright (C) 2021 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
/* |
| 7 |
* This file deals with authentication based on client certificates. It |
| 8 |
* contains a peer certificate verification function and a series of |
| 9 |
* functions for dealing with the session-scoped client certificate file |
| 10 |
* we store on the disk. |
| 11 |
* |
| 12 |
* This file is responsible for determining the cockpit-ws instance |
| 13 |
* identifiers: the client certificate files are limited in scope to a |
| 14 |
* particular cgroup, which is determined based on the instance |
| 15 |
* identifier: that logic is also in this file. |
| 16 |
* |
| 17 |
* Higher layers (cockpit-tls → cockpit-ws → cockpit-session) are |
| 18 |
* responsible for transporting the client certificate filename from |
| 19 |
* here to the counterpart of this file which performs the actual |
| 20 |
* checks: src/session/client-certificate.c. The filename is |
| 21 |
* required information for authentication, but it's not sufficient: |
| 22 |
* the cgroup of the wsinstance must also match the one found in the |
| 23 |
* client certificate file. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
|
| 28 |
#include "client-certificate.h" |
| 29 |
|
| 30 |
#include "common/cockpitmemory.h" |
| 31 |
#include "common/cockpithex.h" |
| 32 |
#include "tls/utils.h" /* for SHA256_NIL */ |
| 33 |
|
| 34 |
#include <assert.h> |
| 35 |
#include <err.h> |
| 36 |
#include <fcntl.h> |
| 37 |
#include <limits.h> |
| 38 |
#include <stdio.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <sys/random.h> |
| 41 |
#include <unistd.h> |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* client_certificate_verify: Custom client certificate validation function |
| 46 |
* |
| 47 |
* cockpit-tls ignores CA/trusted owner and leaves that to e. g. sssd. But |
| 48 |
* validate the other properties such as expiry, unsafe algorithms, etc. |
| 49 |
* This combination cannot be done with gnutls_session_set_verify_cert(). |
| 50 |
*/ |
| 51 |
int |
| 52 |
client_certificate_verify (gnutls_session_t session) |
| 53 |
{ |
| 54 |
unsigned status; |
| 55 |
int ret; |
| 56 |
|
| 57 |
do |
| 58 |
ret = gnutls_certificate_verify_peers2 (session, &status); |
| 59 |
while (ret == GNUTLS_E_INTERRUPTED); |
| 60 |
|
| 61 |
if (ret == 0) |
| 62 |
{ |
| 63 |
/* ignore CA/trusted owner and leave that to e. g. sssd */ |
| 64 |
status &= ~(GNUTLS_CERT_INVALID | GNUTLS_CERT_SIGNER_NOT_FOUND | GNUTLS_CERT_SIGNER_NOT_CA); |
| 65 |
if (status != 0) |
| 66 |
{ |
| 67 |
gnutls_datum_t msg; |
| 68 |
ret = gnutls_certificate_verification_status_print (status, gnutls_certificate_type_get (session), &msg, 0); |
| 69 |
if (ret != GNUTLS_E_SUCCESS) |
| 70 |
errx (EXIT_FAILURE, "Failed to print verification status: %s", gnutls_strerror (ret)); |
| 71 |
warnx ("Invalid TLS peer certificate: %s", msg.data); |
| 72 |
gnutls_free (msg.data); |
| 73 |
#ifdef GNUTLS_E_CERTIFICATE_VERIFICATION_ERROR |
| 74 |
return GNUTLS_E_CERTIFICATE_VERIFICATION_ERROR; |
| 75 |
#else /* fallback for GnuTLS < 3.4.4 */ |
| 76 |
return GNUTLS_E_CERTIFICATE_ERROR; |
| 77 |
#endif |
| 78 |
} |
| 79 |
} |
| 80 |
else if (ret != GNUTLS_E_NO_CERTIFICATE_FOUND) |
| 81 |
{ |
| 82 |
warnx ("Verifying TLS peer failed: %s", gnutls_strerror (ret)); |
| 83 |
return ret; |
| 84 |
} |
| 85 |
|
| 86 |
return GNUTLS_E_SUCCESS; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* client_certificate_get_wsinstance: |
| 91 |
* @certificate: the certificate presented by the peer |
| 92 |
* |
| 93 |
* Determines the correct cockpit-ws instance for handling connections |
| 94 |
* for this @certificate (must be non-%NULL). |
| 95 |
* |
| 96 |
* Currently, the full SHA256 fingerprint of the peer certificate is |
| 97 |
* used. This is a pure design decision that nothing else depends on, |
| 98 |
* and it could be changed to something else. |
| 99 |
* |
| 100 |
* This function never fails. Any internal failure will abort the |
| 101 |
* program. |
| 102 |
* |
| 103 |
* The return value of this function needs to be free()d. |
| 104 |
*/ |
| 105 |
static char * |
| 106 |
client_certificate_get_wsinstance (const gnutls_datum_t *certificate) |
| 107 |
{ |
| 108 |
unsigned char digest_data[256 / 8]; |
| 109 |
size_t digest_size = sizeof digest_data; |
| 110 |
int r = gnutls_fingerprint (GNUTLS_DIG_SHA256, certificate, digest_data, &digest_size); |
| 111 |
if (r != GNUTLS_E_SUCCESS) |
| 112 |
errx (EXIT_FAILURE, "Could not generate fingerprint of peer certificate: %s", |
| 113 |
gnutls_strerror (r)); |
| 114 |
assert (digest_size == sizeof digest_data); |
| 115 |
|
| 116 |
return cockpit_hex_encode (digest_data, digest_size); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* client_certificate_random_filename: |
| 121 |
* |
| 122 |
* Generates a (high quality) random hexadecimal string to use as a |
| 123 |
* client certificate filename. |
| 124 |
* |
| 125 |
* Currently, the filename will be 64 characters in length. This is a |
| 126 |
* pure design decision that nothing else depends on, and it could be |
| 127 |
* changed to something else. |
| 128 |
* |
| 129 |
* This function never fails. Any internal failure will abort the |
| 130 |
* program. |
| 131 |
* |
| 132 |
* The return value of this function needs to be free()d. |
| 133 |
*/ |
| 134 |
static char * |
| 135 |
client_certificate_random_filename (void) |
| 136 |
{ |
| 137 |
/* This is guaranteed to succeed, but we check it anyway. */ |
| 138 |
unsigned char random_data[256 / 8]; |
| 139 |
ssize_t s = getrandom (random_data, sizeof random_data, 0); |
| 140 |
if (s != sizeof random_data) |
| 141 |
err (EXIT_FAILURE, "Could not read random data from the kernel"); |
| 142 |
|
| 143 |
return cockpit_hex_encode (random_data, sizeof random_data); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* client_certificate_write_data: |
| 148 |
* @fd: a writable file descriptor |
| 149 |
* @data: data to write |
| 150 |
* @size: the length of @data |
| 151 |
* @description: description of @data, added to error messages |
| 152 |
* |
| 153 |
* Writes @data to @fd. |
| 154 |
* |
| 155 |
* There's no EINTR handling or support for partial writes. Any kind of |
| 156 |
* result other than a complete success on the first try is treated as |
| 157 |
* an error — this is tmpfs, after all. |
| 158 |
* |
| 159 |
* In case of failure, a message will be written to stderr. |
| 160 |
*/ |
| 161 |
static bool |
| 162 |
client_certificate_write_data (int fd, |
| 163 |
const void *data, |
| 164 |
size_t size, |
| 165 |
const char *description) |
| 166 |
{ |
| 167 |
ssize_t s = write (fd, data, size); |
| 168 |
|
| 169 |
if (s == size) |
| 170 |
return true; |
| 171 |
|
| 172 |
if (s == -1) |
| 173 |
warn ("Couldn't write %s to certificate file", description); |
| 174 |
else |
| 175 |
warnx ("Partial write of %s to certificate file: %zu of %zu", description, s, size); |
| 176 |
|
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
static bool |
| 181 |
client_certificate_write_cgroup_header (int fd, |
| 182 |
const char *wsinstance) |
| 183 |
{ |
| 184 |
char header[200]; |
| 185 |
int s = snprintf (header, sizeof header, |
| 186 |
"0::/system.slice/system-cockpithttps.slice/cockpit-wsinstance-https@%s.service\n", |
| 187 |
wsinstance); |
| 188 |
assert (s < sizeof header); |
| 189 |
|
| 190 |
return client_certificate_write_data (fd, header, s, "cgroup header"); |
| 191 |
} |
| 192 |
|
| 193 |
static bool |
| 194 |
client_certificate_write_pem (int fd, |
| 195 |
const gnutls_datum_t *der) |
| 196 |
{ |
| 197 |
gnutls_datum_t pem = { NULL, 0 }; |
| 198 |
int r = gnutls_pem_base64_encode2 ("CERTIFICATE", der, &pem); |
| 199 |
if (r != GNUTLS_E_SUCCESS) |
| 200 |
{ |
| 201 |
warnx ("Couldn't base64 encode certificate: %s", gnutls_strerror (r)); |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
bool result = client_certificate_write_data (fd, pem.data, pem.size, "PEM data"); |
| 206 |
|
| 207 |
/* Make sure we get the function version and not the weird |
| 208 |
* side-effecting macro version. |
| 209 |
*/ |
| 210 |
(gnutls_free) (pem.data); |
| 211 |
|
| 212 |
return result; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* client_certificate_link_fd_to_random_name: |
| 217 |
* @dirfd: the directory to link the file |
| 218 |
* @fd: the file created with O_TMPFILE |
| 219 |
* @out_filename: the filename that was chosen |
| 220 |
* |
| 221 |
* Links the O_TMPFILE referred to by @fd to a random filename in |
| 222 |
* @dirfd. |
| 223 |
* |
| 224 |
* On success, %true is returned and @out_filename is set to the |
| 225 |
* filename that was used. It must be free()d. |
| 226 |
* |
| 227 |
* On failure, %false is returned and a message will have been logged. |
| 228 |
*/ |
| 229 |
static bool |
| 230 |
client_certificate_link_fd_to_random_name (int dirfd, |
| 231 |
int fd, |
| 232 |
char **out_filename) |
| 233 |
{ |
| 234 |
char *filename = client_certificate_random_filename (); |
| 235 |
|
| 236 |
/* "the usual tricks" — see openat(2) and linkat(2) */ |
| 237 |
char path[PATH_MAX]; |
| 238 |
snprintf(path, PATH_MAX, "/proc/self/fd/%d", fd); |
| 239 |
|
| 240 |
if (linkat (AT_FDCWD, path, dirfd, filename, AT_SYMLINK_FOLLOW) == 0) |
| 241 |
{ |
| 242 |
*out_filename = filename; |
| 243 |
return true; |
| 244 |
} |
| 245 |
else |
| 246 |
{ |
| 247 |
warn ("Unable to link client certificate file to /run/cockpit/tls/%s", filename); |
| 248 |
free (filename); |
| 249 |
return false; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* client_certificate_create_tmpfile: |
| 255 |
* @dirfd: the directory that will eventually contain the file |
| 256 |
* @out_fd: a write-mode fd for the created file |
| 257 |
*/ |
| 258 |
static bool |
| 259 |
client_certificate_create_tmpfile (int dirfd, |
| 260 |
int *out_fd) |
| 261 |
{ |
| 262 |
int fd = openat (dirfd, ".", O_TMPFILE | O_WRONLY, 0400); |
| 263 |
if (fd == -1) |
| 264 |
{ |
| 265 |
warn ("Couldn't create temporary file for client certificate"); |
| 266 |
return false; |
| 267 |
} |
| 268 |
|
| 269 |
*out_fd = fd; |
| 270 |
return true; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* client_certificate_accept: |
| 275 |
* @session: a post-handshake gnutls session |
| 276 |
* @dirfd: the directory for session-scoped client certificates |
| 277 |
* @out_wsinstance: the instance of cockpit-ws to connect to |
| 278 |
* @out_filename: the filename where the client certificate was written |
| 279 |
* |
| 280 |
* Called immediately after completing the handshake with an incoming |
| 281 |
* HTTPS connection. |
| 282 |
* |
| 283 |
* If no client certificate was presented, this function writes %NULL to |
| 284 |
* @out_filename, but still provides a (hard-coded) instance identifier |
| 285 |
* to @out_wsinstance. |
| 286 |
* |
| 287 |
* If a client certificate was presented, the @out_wsinstance will |
| 288 |
* correspond to the SHA256 of the peer certificate. In this case, a |
| 289 |
* file with a random filename will be written to the directory |
| 290 |
* referenced by @dirfd. This file will contain the expected cgroup of |
| 291 |
* the cockpit-ws instance in question, plus the client certificate. |
| 292 |
* That data is interpreted by the counterpart to this code, living in |
| 293 |
* src/session/client-certificate.c. |
| 294 |
* |
| 295 |
* In any case, %true will be returned in case of success, and %false |
| 296 |
* will be returned in case of an error. In case of success, any values |
| 297 |
* returned in @out_wsinstance or @out_filename need to be free()d. In |
| 298 |
* case of error, the connection should be terminated: a message will |
| 299 |
* already have been logged. |
| 300 |
*/ |
| 301 |
bool |
| 302 |
client_certificate_accept (gnutls_session_t session, |
| 303 |
int dirfd, |
| 304 |
char **out_wsinstance, |
| 305 |
char **out_filename) |
| 306 |
{ |
| 307 |
const gnutls_datum_t *peer_certificate = gnutls_certificate_get_peers (session, NULL); |
| 308 |
|
| 309 |
if (peer_certificate == NULL) |
| 310 |
{ |
| 311 |
*out_wsinstance = strdupx (SHA256_NIL); |
| 312 |
*out_filename = NULL; |
| 313 |
|
| 314 |
return true; |
| 315 |
} |
| 316 |
|
| 317 |
char *wsinstance = client_certificate_get_wsinstance (peer_certificate); |
| 318 |
|
| 319 |
int fd = -1; |
| 320 |
bool success = |
| 321 |
client_certificate_create_tmpfile (dirfd, &fd) && |
| 322 |
client_certificate_write_cgroup_header (fd, wsinstance) && |
| 323 |
client_certificate_write_pem (fd, peer_certificate) && |
| 324 |
client_certificate_link_fd_to_random_name (dirfd, fd, out_filename); |
| 325 |
|
| 326 |
if (fd != -1) |
| 327 |
close (fd); |
| 328 |
|
| 329 |
if (success) |
| 330 |
*out_wsinstance = wsinstance; |
| 331 |
else |
| 332 |
free (wsinstance); |
| 333 |
|
| 334 |
if (!success) |
| 335 |
warnx ("Disconnecting client due to above failure."); |
| 336 |
|
| 337 |
return success; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* client_certificate_unlink_and_free: |
| 342 |
* @dirfd: the directory for session-scoped client certificates |
| 343 |
* @inout_filename: the name of the client certificate file |
| 344 |
* |
| 345 |
* Glorified wrapper around unlinkat(). |
| 346 |
* |
| 347 |
* Frees @inout_filename. |
| 348 |
* |
| 349 |
* If the operation fails, the program will be aborted. |
| 350 |
*/ |
| 351 |
void |
| 352 |
client_certificate_unlink_and_free (int dirfd, |
| 353 |
char *filename) |
| 354 |
{ |
| 355 |
if (unlinkat (dirfd, filename, 0) != 0) |
| 356 |
{ |
| 357 |
/* We can't leave stale certificate files hanging around after |
| 358 |
* they should have been deleted, and we're really not expecting a |
| 359 |
* failure here, so let's abort the entire service. This should |
| 360 |
* cause any running -ws instances to be terminated, and will |
| 361 |
* cause systemd to delete the entire runtime directory as well. |
| 362 |
*/ |
| 363 |
err (EXIT_FAILURE, "Failed to unlink client certificate file %s", filename); |
| 364 |
} |
| 365 |
|
| 366 |
free (filename); |
| 367 |
} |