|
23 | 23 |
|
24 | 24 | #include <assert.h> |
25 | 25 | #include <err.h> |
| 26 | +#include <errno.h> |
| 27 | +#include <fcntl.h> |
| 28 | +#include <stdio.h> |
26 | 29 | #include <stdlib.h> |
27 | 30 | #include <string.h> |
| 31 | +#include <sys/stat.h> |
| 32 | +#include <unistd.h> |
28 | 33 |
|
29 | 34 | #include <gnutls/x509.h> |
30 | 35 |
|
@@ -73,24 +78,111 @@ credentials_get (Credentials *self) |
73 | 78 | return self->creds; |
74 | 79 | } |
75 | 80 |
|
| 81 | +/* Load a file into a gnutls_datum_t. |
| 82 | + * |
| 83 | + * Returns true on success, false if the file doesn't exist. |
| 84 | + * Exits on any other failure. |
| 85 | + * On success, data->data must be freed with gnutls_free(). |
| 86 | + */ |
| 87 | +static bool |
| 88 | +load_file (int dirfd, const char *filename, gnutls_datum_t *data) |
| 89 | +{ |
| 90 | + int fd = openat (dirfd, filename, O_RDONLY | O_CLOEXEC | O_NOCTTY); |
| 91 | + if (fd < 0) |
| 92 | + { |
| 93 | + if (errno == ENOENT) |
| 94 | + return false; |
| 95 | + err (EXIT_FAILURE, "Failed to open '%s'", filename); |
| 96 | + } |
| 97 | + |
| 98 | + struct stat st; |
| 99 | + if (fstat (fd, &st) < 0) |
| 100 | + err (EXIT_FAILURE, "Failed to stat '%s'", filename); |
| 101 | + |
| 102 | + if (!S_ISREG (st.st_mode)) |
| 103 | + errx (EXIT_FAILURE, "'%s' is not a regular file", filename); |
| 104 | + |
| 105 | + if (st.st_size <= 0) |
| 106 | + errx (EXIT_FAILURE, "'%s' is empty", filename); |
| 107 | + |
| 108 | + if (st.st_size > 640 * 1024) /* ought to be enough for anybody! */ |
| 109 | + errx (EXIT_FAILURE, "'%s' is too large", filename); |
| 110 | + |
| 111 | + size_t file_size = (size_t) st.st_size; |
| 112 | + |
| 113 | + unsigned char *buffer = mallocx (file_size + 1); |
| 114 | + if (buffer == NULL) |
| 115 | + errx (EXIT_FAILURE, "Failed to allocate memory for '%s'", filename); |
| 116 | + |
| 117 | + ssize_t n; |
| 118 | + do |
| 119 | + n = read (fd, buffer, file_size); |
| 120 | + while (n < 0 && errno == EINTR); |
| 121 | + |
| 122 | + if (n < 0) |
| 123 | + err (EXIT_FAILURE, "Failed to read '%s'", filename); |
| 124 | + |
| 125 | + if (n != (ssize_t) file_size) |
| 126 | + errx (EXIT_FAILURE, "Failed to read '%s': expected %zu bytes, got %zd", filename, file_size, n); |
| 127 | + |
| 128 | + close (fd); |
| 129 | + |
| 130 | + buffer[file_size] = '\0'; |
| 131 | + |
| 132 | + data->data = buffer; |
| 133 | + data->size = (unsigned int) file_size; /* <= 640k */ |
| 134 | + |
| 135 | + return true; |
| 136 | +} |
| 137 | + |
76 | 138 | Credentials * |
77 | | -credentials_load (const char *certificate_filename, |
78 | | - const char *key_filename) |
| 139 | +credentials_load_directory (int dirfd) |
79 | 140 | { |
80 | 141 | gnutls_certificate_credentials_t creds; |
81 | 142 | int ret; |
82 | 143 |
|
83 | | - debug (SERVER, "Using certificate %s", certificate_filename); |
84 | | - |
85 | 144 | ret = gnutls_certificate_allocate_credentials (&creds); |
86 | 145 | assert (ret == GNUTLS_E_SUCCESS); |
87 | 146 |
|
88 | | - ret = gnutls_certificate_set_x509_key_file (creds, |
89 | | - certificate_filename, key_filename, |
90 | | - GNUTLS_X509_FMT_PEM); |
| 147 | + Credentials *self = credentials_new(creds); |
| 148 | + |
| 149 | + /* Load files sequentially 0.{crt,key}, 1.{crt,key}, 2.{crt,key}, etc... */ |
| 150 | + int i; |
| 151 | + for (i = 0; ; i++) |
| 152 | + { |
| 153 | + char crt_name[32]; |
| 154 | + snprintf (crt_name, sizeof crt_name, "%d.crt", i); |
| 155 | + |
| 156 | + gnutls_datum_t crt_data; |
| 157 | + if (!load_file (dirfd, crt_name, &crt_data)) |
| 158 | + break; |
| 159 | + |
| 160 | + debug (SERVER, "Adding certificate %s", cert_name); |
| 161 | + |
| 162 | + char key_name[32]; |
| 163 | + snprintf (key_name, sizeof key_name, "%d.key", i); |
| 164 | + |
| 165 | + gnutls_datum_t key_data; |
| 166 | + if (!load_file (dirfd, key_name, &key_data)) |
| 167 | + errx (EXIT_FAILURE, "Certificate '%s' exists but key '%s' is missing", |
| 168 | + crt_name, key_name); |
| 169 | + |
| 170 | + int ret = gnutls_certificate_set_x509_key_mem2 (self->creds, |
| 171 | + &crt_data, &key_data, |
| 172 | + GNUTLS_X509_FMT_PEM, |
| 173 | + NULL, 0); |
| 174 | + if (ret < 0) |
| 175 | + errx (EXIT_FAILURE, "Failed to load keypair %s/%s: %s", |
| 176 | + crt_name, key_name, gnutls_strerror (ret)); |
| 177 | + |
| 178 | + gnutls_memset (key_data.data, 0, key_data.size); |
| 179 | + free (key_data.data); |
| 180 | + free (crt_data.data); |
| 181 | + } |
91 | 182 |
|
92 | | - if (ret != GNUTLS_E_SUCCESS) |
93 | | - errx (EXIT_FAILURE, "Failed to initialize server certificate: %s", gnutls_strerror (ret)); |
| 183 | + if (i == 0) |
| 184 | + errx (EXIT_FAILURE, "No certificates found in directory"); |
94 | 185 |
|
95 | | - return credentials_new (creds); |
| 186 | + debug (SERVER, "Loaded %d certificate(s)", i); |
| 187 | + return self; |
96 | 188 | } |
0 commit comments