|
9 | 9 |
|
10 | 10 | #include <assert.h> |
11 | 11 | #include <err.h> |
| 12 | +#include <errno.h> |
| 13 | +#include <fcntl.h> |
| 14 | +#include <stdio.h> |
12 | 15 | #include <stdlib.h> |
13 | 16 | #include <string.h> |
| 17 | +#include <sys/stat.h> |
| 18 | +#include <unistd.h> |
14 | 19 |
|
15 | 20 | #include <gnutls/x509.h> |
16 | 21 |
|
@@ -59,24 +64,111 @@ credentials_get (Credentials *self) |
59 | 64 | return self->creds; |
60 | 65 | } |
61 | 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 gnutls_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 | + |
62 | 124 | Credentials * |
63 | | -credentials_load (const char *certificate_filename, |
64 | | - const char *key_filename) |
| 125 | +credentials_load_directory (int dirfd) |
65 | 126 | { |
66 | 127 | gnutls_certificate_credentials_t creds; |
67 | 128 | int ret; |
68 | 129 |
|
69 | | - debug (SERVER, "Using certificate %s", certificate_filename); |
70 | | - |
71 | 130 | ret = gnutls_certificate_allocate_credentials (&creds); |
72 | 131 | assert (ret == GNUTLS_E_SUCCESS); |
73 | 132 |
|
74 | | - ret = gnutls_certificate_set_x509_key_file (creds, |
75 | | - certificate_filename, key_filename, |
76 | | - GNUTLS_X509_FMT_PEM); |
| 133 | + Credentials *self = credentials_new(creds); |
| 134 | + |
| 135 | + /* Load files sequentially 0.{crt,key}, 1.{crt,key}, 2.{crt,key}, etc... */ |
| 136 | + int i; |
| 137 | + for (i = 0; ; i++) |
| 138 | + { |
| 139 | + char crt_name[32]; |
| 140 | + snprintf (crt_name, sizeof crt_name, "%d.crt", i); |
| 141 | + |
| 142 | + gnutls_datum_t crt_data; |
| 143 | + if (!load_file (dirfd, crt_name, &crt_data)) |
| 144 | + break; |
| 145 | + |
| 146 | + debug (SERVER, "Adding certificate %s", cert_name); |
| 147 | + |
| 148 | + char key_name[32]; |
| 149 | + snprintf (key_name, sizeof key_name, "%d.key", i); |
| 150 | + |
| 151 | + gnutls_datum_t key_data; |
| 152 | + if (!load_file (dirfd, key_name, &key_data)) |
| 153 | + errx (EXIT_FAILURE, "Certificate '%s' exists but key '%s' is missing", |
| 154 | + crt_name, key_name); |
| 155 | + |
| 156 | + int ret = gnutls_certificate_set_x509_key_mem2 (self->creds, |
| 157 | + &crt_data, &key_data, |
| 158 | + GNUTLS_X509_FMT_PEM, |
| 159 | + NULL, 0); |
| 160 | + if (ret < 0) |
| 161 | + errx (EXIT_FAILURE, "Failed to load keypair %s/%s: %s", |
| 162 | + crt_name, key_name, gnutls_strerror (ret)); |
| 163 | + |
| 164 | + gnutls_memset (key_data.data, 0, key_data.size); |
| 165 | + free (key_data.data); |
| 166 | + free (crt_data.data); |
| 167 | + } |
77 | 168 |
|
78 | | - if (ret != GNUTLS_E_SUCCESS) |
79 | | - errx (EXIT_FAILURE, "Failed to initialize server certificate: %s", gnutls_strerror (ret)); |
| 169 | + if (i == 0) |
| 170 | + errx (EXIT_FAILURE, "No certificates found in directory"); |
80 | 171 |
|
81 | | - return credentials_new (creds); |
| 172 | + debug (SERVER, "Loaded %d certificate(s)", i); |
| 173 | + return self; |
82 | 174 | } |
0 commit comments