Use fallthrough statement attribute
[heimdal.git] / lib / hx509 / file.c
blob877fb1d48a64e1f135b72df407585ce677ffe0e7
1 /*
2 * Copyright (c) 2005 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "hx_locl.h"
36 HX509_LIB_FUNCTION int HX509_LIB_CALL
37 _hx509_map_file_os(const char *fn, heim_octet_string *os)
39 size_t length;
40 void *data;
41 int ret;
43 ret = rk_undumpdata(fn, &data, &length);
45 os->data = data;
46 os->length = length;
48 return ret;
51 HX509_LIB_FUNCTION void HX509_LIB_CALL
52 _hx509_unmap_file_os(heim_octet_string *os)
54 rk_xfree(os->data);
57 HX509_LIB_FUNCTION int HX509_LIB_CALL
58 _hx509_write_file(const char *fn, const void *data, size_t length)
60 rk_dumpdata(fn, data, length);
61 return 0;
68 static void
69 print_pem_stamp(FILE *f, const char *type, const char *str)
71 fprintf(f, "-----%s %s-----\n", type, str);
74 HX509_LIB_FUNCTION int HX509_LIB_CALL
75 hx509_pem_write(hx509_context context, const char *type,
76 hx509_pem_header *headers, FILE *f,
77 const void *data, size_t size)
79 const char *p = data;
80 size_t length;
81 char *line;
83 #define ENCODE_LINE_LENGTH 54
85 print_pem_stamp(f, "BEGIN", type);
87 while (headers) {
88 fprintf(f, "%s: %s\n%s",
89 headers->header, headers->value,
90 headers->next ? "" : "\n");
91 headers = headers->next;
94 while (size > 0) {
95 ssize_t l;
97 length = size;
98 if (length > ENCODE_LINE_LENGTH)
99 length = ENCODE_LINE_LENGTH;
101 l = rk_base64_encode(p, length, &line);
102 if (l < 0) {
103 hx509_set_error_string(context, 0, ENOMEM,
104 "malloc - out of memory");
105 return ENOMEM;
107 size -= length;
108 fprintf(f, "%s\n", line);
109 p += length;
110 free(line);
113 print_pem_stamp(f, "END", type);
115 return 0;
122 HX509_LIB_FUNCTION int HX509_LIB_CALL
123 hx509_pem_add_header(hx509_pem_header **headers,
124 const char *header, const char *value)
126 hx509_pem_header *h;
128 h = calloc(1, sizeof(*h));
129 if (h == NULL)
130 return ENOMEM;
131 h->header = strdup(header);
132 if (h->header == NULL) {
133 free(h);
134 return ENOMEM;
136 h->value = strdup(value);
137 if (h->value == NULL) {
138 free(h->header);
139 free(h);
140 return ENOMEM;
143 h->next = *headers;
144 *headers = h;
146 return 0;
149 HX509_LIB_FUNCTION void HX509_LIB_CALL
150 hx509_pem_free_header(hx509_pem_header *headers)
152 hx509_pem_header *h;
153 while (headers) {
154 h = headers;
155 headers = headers->next;
156 free(h->header);
157 free(h->value);
158 free(h);
166 HX509_LIB_FUNCTION const char * HX509_LIB_CALL
167 hx509_pem_find_header(const hx509_pem_header *h, const char *header)
169 while(h) {
170 if (strcmp(header, h->header) == 0)
171 return h->value;
172 h = h->next;
174 return NULL;
182 HX509_LIB_FUNCTION int HX509_LIB_CALL
183 hx509_pem_read(hx509_context context,
184 FILE *f,
185 hx509_pem_read_func func,
186 void *ctx)
188 hx509_pem_header *headers = NULL;
189 char *type = NULL;
190 void *data = NULL;
191 size_t len = 0;
192 char buf[1024];
193 int ret = HX509_PARSING_KEY_FAILED;
195 enum { BEFORE, SEARCHHEADER, INHEADER, INDATA, DONE } where;
197 where = BEFORE;
199 while (fgets(buf, sizeof(buf), f) != NULL) {
200 char *p;
201 int i;
203 i = strcspn(buf, "\n");
204 if (buf[i] == '\n') {
205 buf[i] = '\0';
206 if (i > 0)
207 i--;
209 if (buf[i] == '\r') {
210 buf[i] = '\0';
211 if (i > 0)
212 i--;
215 switch (where) {
216 case BEFORE:
217 if (strncmp("-----BEGIN ", buf, 11) == 0) {
218 type = strdup(buf + 11);
219 if (type == NULL)
220 break;
221 p = strchr(type, '-');
222 if (p)
223 *p = '\0';
224 where = SEARCHHEADER;
226 break;
227 case SEARCHHEADER:
228 p = strchr(buf, ':');
229 if (p == NULL) {
230 where = INDATA;
231 goto indata;
233 fallthrough
234 /* FALLTHROUGH */
235 case INHEADER:
236 if (buf[0] == '\0') {
237 where = INDATA;
238 break;
240 p = strchr(buf, ':');
241 if (p) {
242 *p++ = '\0';
243 while (isspace((int)*p))
244 p++;
245 ret = hx509_pem_add_header(&headers, buf, p);
246 if (ret)
247 abort();
249 break;
250 case INDATA:
251 indata:
253 if (strncmp("-----END ", buf, 9) == 0) {
254 where = DONE;
255 break;
258 p = emalloc(i);
259 i = rk_base64_decode(buf, p);
260 if (i < 0) {
261 free(p);
262 goto out;
265 data = erealloc(data, len + i);
266 memcpy(((char *)data) + len, p, i);
267 free(p);
268 len += i;
269 break;
270 case DONE:
271 abort();
274 if (where == DONE) {
275 ret = (*func)(context, type, headers, data, len, ctx);
276 out:
277 free(data);
278 data = NULL;
279 len = 0;
280 free(type);
281 type = NULL;
282 where = BEFORE;
283 hx509_pem_free_header(headers);
284 headers = NULL;
285 if (ret)
286 break;
290 if (where != BEFORE) {
291 hx509_set_error_string(context, 0, HX509_PARSING_KEY_FAILED,
292 "File ends before end of PEM end tag");
293 ret = HX509_PARSING_KEY_FAILED;
295 if (data)
296 free(data);
297 if (type)
298 free(type);
299 if (headers)
300 hx509_pem_free_header(headers);
302 return ret;
306 * On modern systems there's no such thing as scrubbing a file. Not this way
307 * anyways. However, for now we'll cargo-cult this along just as in lib/krb5.
309 static int
310 scrub_file(int fd, ssize_t sz)
312 char buf[128];
314 memset(buf, 0, sizeof(buf));
315 while (sz > 0) {
316 ssize_t tmp;
317 size_t wr = sizeof(buf) > sz ? (size_t)sz : sizeof(buf);
319 tmp = write(fd, buf, wr);
320 if (tmp == -1)
321 return errno;
322 sz -= tmp;
324 #ifdef _MSC_VER
325 return _commit(fd);
326 #else
327 return fsync(fd);
328 #endif
332 _hx509_erase_file(hx509_context context, const char *fn)
334 struct stat sb1, sb2;
335 int ret;
336 int fd;
338 if (fn == NULL)
339 return 0;
341 /* This is based on _krb5_erase_file(), minus file locking */
342 ret = lstat(fn, &sb1);
343 if (ret == -1 && errno == ENOENT)
344 return 0;
345 if (ret == -1) {
346 hx509_set_error_string(context, 0, ret, "hx509_certs_destroy: "
347 "stat of \"%s\": %s", fn, strerror(ret));
348 return errno;
351 fd = open(fn, O_RDWR | O_BINARY | O_CLOEXEC | O_NOFOLLOW);
352 rk_cloexec(fd);
353 if (ret == -1 && errno == ENOENT)
354 return 0;
355 if (ret == -1)
356 return errno;
358 if (unlink(fn) < 0) {
359 ret = errno;
360 (void) close(fd);
361 hx509_set_error_string(context, 0, ret, "hx509_certs_destroy: "
362 "unlinking \"%s\": %s", fn, strerror(ret));
363 return ret;
366 /* check TOCTOU, symlinks */
367 ret = fstat(fd, &sb2);
368 if (ret < 0) {
369 ret = errno;
370 hx509_set_error_string(context, 0, ret, "hx509_certs_destroy: "
371 "fstat of %d, \"%s\": %s", fd, fn,
372 strerror(ret));
373 (void) close(fd);
374 return ret;
376 if (sb1.st_dev != sb2.st_dev || sb1.st_ino != sb2.st_ino) {
377 (void) close(fd);
378 return EPERM;
381 /* there are still hard links to this file */
382 if (sb2.st_nlink != 0) {
383 close(fd);
384 return 0;
387 ret = scrub_file(fd, sb2.st_size);
388 (void) close(fd);
389 return ret;