2 * Copyright (c) 2021 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 der_print_general_string(const heim_general_string *str, int flags)
48 der_print_boolean(const int *i, int flags)
50 return *i ? strdup("true") : strdup("false");
54 der_print_integer(const int *i, int flags)
58 if (asprintf(&s, "%d", *i) == -1 || s == NULL)
64 der_print_integer64(const int64_t *i, int flags)
68 if (asprintf(&s, "%lld", (long long)*i) == -1 || s == NULL)
74 der_print_unsigned(const unsigned *u, int flags)
78 if (asprintf(&s, "%u", *u) == -1 || s == NULL)
84 der_print_unsigned64(const uint64_t *u, int flags)
88 if (asprintf(&s, "%llu", (long long)*u) == -1 || s == NULL)
94 der_print_generalized_time(const time_t *t, int flags)
97 char str[sizeof("1970-01-01T00:00:00Z")];
100 if (gmtime_s(&tms, t) != 0 ||
101 strftime(str, sizeof(str), "%Y-%m-%dT%H:%M:%SZ", &tms) == 0)
104 if (strftime(str, sizeof(str), "%Y-%m-%dT%H:%M:%SZ", gmtime_r(t, &tms)) == 0)
111 der_print_utctime(const time_t *t, int flags)
114 char str[sizeof("1970-01-01T00:00:00Z")];
117 if (gmtime_s(&tms, t) != 0 ||
118 strftime(str, sizeof(str), "%Y-%m-%dT%H:%M:%SZ", &tms) == 0)
121 if (strftime(str, sizeof(str), "%Y-%m-%dT%H:%M:%SZ", gmtime_r(t, &tms)) == 0)
129 der_print_utf8string(const heim_utf8_string *str, int flags)
135 der_print_printable_string(const heim_printable_string *str, int flags)
137 return strndup(str->data, str->length);
141 der_print_ia5_string(const heim_ia5_string *str, int flags)
143 return strndup(str->data, str->length);
147 der_print_bmp_string(const heim_bmp_string *k, int flags)
149 return strdup("<BMPString-not-supported>");
153 der_print_universal_string(const heim_universal_string *k, int flags)
155 return strdup("<UniversalString-not-supported>");
159 der_print_visible_string(const heim_visible_string *str, int flags)
165 der_print_octet_string(const heim_octet_string *k, int flags)
169 (void) hex_encode(k->data, k->length, &s);
174 der_print_heim_integer(const heim_integer *k, int flags)
178 (void) der_print_hex_heim_integer(k, &s);
183 der_print_oid(const heim_oid *k, int flags)
185 struct rk_strpool *r = NULL;
186 const char *sym = NULL;
190 (void) der_print_heim_oid(k, '.', &s);
194 r = rk_strpoolprintf(r, "{\"_type\":\"OBJECT IDENTIFIER\","
199 for (i = 0; i < k->length; i++)
200 r = rk_strpoolprintf(r, "%s%u", i ? "," : "", k->components[i]);
202 r = rk_strpoolprintf(r, "]");
203 (void) der_find_heim_oid_by_oid(k, &sym);
205 if ((s = strdup(sym))) {
206 for (i = 0; s[i]; i++)
210 r = rk_strpoolprintf(r, ",\"name\":\"%s\"", s ? s : sym);
214 r = rk_strpoolprintf(r, "}");
215 return rk_strpoolcollect(r);
219 der_print_bit_string(const heim_bit_string *k, int flags)
224 (void) hex_encode(k->data, k->length / 8, &s);
225 if (asprintf(&s2, "%llu:%s", (unsigned long long)k->length, s) == -1 || !s2)