2 * Copyright (c) 2005 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
38 _hx509_map_file_os(const char *fn, heim_octet_string *os)
44 ret = rk_undumpdata(fn, &data, &length);
53 _hx509_unmap_file_os(heim_octet_string *os)
59 _hx509_write_file(const char *fn, const void *data, size_t length)
61 rk_dumpdata(fn, data, length);
70 header(FILE *f, const char *type, const char *str)
72 fprintf(f, "-----%s %s-----\n", type, str);
76 hx509_pem_write(hx509_context context, const char *type,
77 hx509_pem_header *headers, FILE *f,
78 const void *data, size_t size)
84 #define ENCODE_LINE_LENGTH 54
86 header(f, "BEGIN", type);
89 fprintf(f, "%s: %s\n%s",
90 headers->header, headers->value,
91 headers->next ? "" : "\n");
92 headers = headers->next;
99 if (length > ENCODE_LINE_LENGTH)
100 length = ENCODE_LINE_LENGTH;
102 l = base64_encode(p, length, &line);
104 hx509_set_error_string(context, 0, ENOMEM,
105 "malloc - out of memory");
109 fprintf(f, "%s\n", line);
114 header(f, "END", type);
124 hx509_pem_add_header(hx509_pem_header **headers,
125 const char *header, const char *value)
129 h = calloc(1, sizeof(*h));
132 h->header = strdup(header);
133 if (h->header == NULL) {
137 h->value = strdup(value);
138 if (h->value == NULL) {
151 hx509_pem_free_header(hx509_pem_header *headers)
156 headers = headers->next;
168 hx509_pem_find_header(const hx509_pem_header *h, const char *header)
171 if (strcmp(header, h->header) == 0)
184 hx509_pem_read(hx509_context context,
186 hx509_pem_read_func func,
189 hx509_pem_header *headers = NULL;
194 int ret = HX509_PARSING_KEY_FAILED;
196 enum { BEFORE, SEARCHHEADER, INHEADER, INDATA, DONE } where;
200 while (fgets(buf, sizeof(buf), f) != NULL) {
204 i = strcspn(buf, "\n");
205 if (buf[i] == '\n') {
210 if (buf[i] == '\r') {
218 if (strncmp("-----BEGIN ", buf, 11) == 0) {
219 type = strdup(buf + 11);
222 p = strchr(type, '-');
225 where = SEARCHHEADER;
229 p = strchr(buf, ':');
236 if (buf[0] == '\0') {
240 p = strchr(buf, ':');
243 while (isspace((int)*p))
245 ret = hx509_pem_add_header(&headers, buf, p);
253 if (strncmp("-----END ", buf, 9) == 0) {
259 i = base64_decode(buf, p);
265 data = erealloc(data, len + i);
266 memcpy(((char *)data) + len, p, i);
275 ret = (*func)(context, type, headers, data, len, ctx);
283 hx509_pem_free_header(headers);
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;
300 hx509_pem_free_header(headers);