Debian Codegraph @

src/tls/credentials.c

raw
1 /*
2 * Copyright (C) 2019 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include "credentials.h"
9
10 #include <assert.h>
11 #include <err.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/stat.h>
18 #include <unistd.h>
19
20 #include <gnutls/x509.h>
21
22 #include "common/cockpitmemory.h"
23 #include "common/cockpitwebcertificate.h"
24
25 #include "utils.h"
26
27 struct _Credentials
28 {
29 gnutls_certificate_credentials_t creds;
30 int ref_count;
31 };
32
33 static Credentials *
34 credentials_new (gnutls_certificate_credentials_t creds)
35 {
36 Credentials *self = mallocx (sizeof (Credentials));
37 self->creds = creds;
38 self->ref_count = 1;
39
40 return self;
41 }
42
43 Credentials *
44 credentials_ref (Credentials *self)
45 {
46 self->ref_count++;
47
48 return self;
49 }
50
51 void
52 credentials_unref (Credentials *self)
53 {
54 if (--self->ref_count == 0)
55 {
56 gnutls_certificate_free_credentials (self->creds);
57 free (self);
58 }
59 }
60
61 gnutls_certificate_credentials_t
62 credentials_get (Credentials *self)
63 {
64 return self->creds;
65 }
66
67 /* Load a file into a gnutls_datum_t.
68 *
69 * Returns true on success, false if the file doesn't exist.
70 * Exits on any other failure.
71 * On success, data->data must be freed with free().
72 */
73 static bool
74 load_file (int dirfd, const char *filename, gnutls_datum_t *data)
75 {
76 int fd = openat (dirfd, filename, O_RDONLY | O_CLOEXEC | O_NOCTTY);
77 if (fd < 0)
78 {
79 if (errno == ENOENT)
80 return false;
81 err (EXIT_FAILURE, "Failed to open '%s'", filename);
82 }
83
84 struct stat st;
85 if (fstat (fd, &st) < 0)
86 err (EXIT_FAILURE, "Failed to stat '%s'", filename);
87
88 if (!S_ISREG (st.st_mode))
89 errx (EXIT_FAILURE, "'%s' is not a regular file", filename);
90
91 if (st.st_size <= 0)
92 errx (EXIT_FAILURE, "'%s' is empty", filename);
93
94 if (st.st_size > 640 * 1024) /* ought to be enough for anybody! */
95 errx (EXIT_FAILURE, "'%s' is too large", filename);
96
97 size_t file_size = (size_t) st.st_size;
98
99 unsigned char *buffer = mallocx (file_size + 1);
100 if (buffer == NULL)
101 errx (EXIT_FAILURE, "Failed to allocate memory for '%s'", filename);
102
103 ssize_t n;
104 do
105 n = read (fd, buffer, file_size);
106 while (n < 0 && errno == EINTR);
107
108 if (n < 0)
109 err (EXIT_FAILURE, "Failed to read '%s'", filename);
110
111 if (n != (ssize_t) file_size)
112 errx (EXIT_FAILURE, "Failed to read '%s': expected %zu bytes, got %zd", filename, file_size, n);
113
114 close (fd);
115
116 buffer[file_size] = '\0';
117
118 data->data = buffer;
119 data->size = (unsigned int) file_size; /* <= 640k */
120
121 return true;
122 }
123
124 /* Load credentials from `{n}.crt` and `{n}.key` files in dirfd. Files
125 * are deleted after loading to avoid leaving unnecessary copies of
126 * secrets lying around.
127 */
128 Credentials *
129 credentials_load (int dirfd)
130 {
131 gnutls_certificate_credentials_t creds;
132 int ret;
133
134 ret = gnutls_certificate_allocate_credentials (&creds);
135 assert (ret == GNUTLS_E_SUCCESS);
136
137 Credentials *self = credentials_new(creds);
138
139 /* Load files sequentially 0.{crt,key}, 1.{crt,key}, 2.{crt,key}, etc... */
140 int i;
141 for (i = 0; ; i++)
142 {
143 char crt_name[32];
144 snprintf (crt_name, sizeof crt_name, "%d.crt", i);
145
146 gnutls_datum_t crt_data;
147 if (!load_file (dirfd, crt_name, &crt_data))
148 break;
149
150 debug (SERVER, "Adding certificate %s", crt_name);
151
152 char key_name[32];
153 snprintf (key_name, sizeof key_name, "%d.key", i);
154
155 gnutls_datum_t key_data;
156 if (!load_file (dirfd, key_name, &key_data))
157 errx (EXIT_FAILURE, "Certificate '%s' exists but key '%s' is missing",
158 crt_name, key_name);
159
160 int ret = gnutls_certificate_set_x509_key_mem2 (self->creds,
161 &crt_data, &key_data,
162 GNUTLS_X509_FMT_PEM,
163 NULL, 0);
164 if (ret < 0)
165 errx (EXIT_FAILURE, "Failed to load keypair %s/%s: %s",
166 crt_name, key_name, gnutls_strerror (ret));
167
168 gnutls_memset (key_data.data, 0, key_data.size);
169 free (key_data.data);
170 free (crt_data.data);
171
172 /* Remove files after loading - secrets shouldn't sit on disk */
173 if (unlinkat (dirfd, key_name, 0) != 0)
174 err (EXIT_FAILURE, "Failed to remove '%s'", key_name);
175 if (unlinkat (dirfd, crt_name, 0) != 0)
176 err (EXIT_FAILURE, "Failed to remove '%s'", crt_name);
177 }
178
179 if (i == 0)
180 errx (EXIT_FAILURE, "No certificates found in directory");
181
182 debug (SERVER, "Loaded %d certificate(s)", i);
183 return self;
184 }