s4:torture: Adapt KDC canon test to Heimdal upstream changes
[Samba.git] / source4 / heimdal / lib / asn1 / der_print.c
blob1d459856a616deec8142820ce7a186ae08510a77
1 /*
2 * Copyright (c) 2021 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "der_locl.h"
37 #include "hex.h"
39 RCSID("$Id$");
41 char * ASN1CALL
42 der_print_general_string(const heim_general_string *str, int flags)
44 return strdup(*str);
47 char * ASN1CALL
48 der_print_boolean(const int *i, int flags)
50 return *i ? strdup("true") : strdup("false");
53 char * ASN1CALL
54 der_print_integer(const int *i, int flags)
56 char *s = NULL;
58 if (asprintf(&s, "%d", *i) == -1 || s == NULL)
59 return NULL;
60 return s;
63 char * ASN1CALL
64 der_print_integer64(const int64_t *i, int flags)
66 char *s = NULL;
68 if (asprintf(&s, "%lld", (long long)*i) == -1 || s == NULL)
69 return NULL;
70 return s;
73 char * ASN1CALL
74 der_print_unsigned(const unsigned *u, int flags)
76 char *s = NULL;
78 if (asprintf(&s, "%u", *u) == -1 || s == NULL)
79 return NULL;
80 return s;
83 char * ASN1CALL
84 der_print_unsigned64(const uint64_t *u, int flags)
86 char *s = NULL;
88 if (asprintf(&s, "%llu", (long long)*u) == -1 || s == NULL)
89 return NULL;
90 return s;
93 char * ASN1CALL
94 der_print_generalized_time(const time_t *t, int flags)
96 struct tm tms;
97 char str[sizeof("1970-01-01T00:00:00Z")];
99 #ifdef WIN32
100 if (gmtime_s(&tms, t) != 0 ||
101 strftime(str, sizeof(str), "%Y-%m-%dT%H:%M:%SZ", &tms) == 0)
102 return NULL;
103 #else
104 if (strftime(str, sizeof(str), "%Y-%m-%dT%H:%M:%SZ", gmtime_r(t, &tms)) == 0)
105 return NULL;
106 #endif
107 return strdup(str);
110 char * ASN1CALL
111 der_print_utctime(const time_t *t, int flags)
113 struct tm tms;
114 char str[sizeof("1970-01-01T00:00:00Z")];
116 #ifdef WIN32
117 if (gmtime_s(&tms, t) != 0 ||
118 strftime(str, sizeof(str), "%Y-%m-%dT%H:%M:%SZ", &tms) == 0)
119 return NULL;
120 #else
121 if (strftime(str, sizeof(str), "%Y-%m-%dT%H:%M:%SZ", gmtime_r(t, &tms)) == 0)
122 return NULL;
123 #endif
124 return strdup(str);
128 char * ASN1CALL
129 der_print_utf8string(const heim_utf8_string *str, int flags)
131 return strdup(*str);
134 char * ASN1CALL
135 der_print_printable_string(const heim_printable_string *str, int flags)
137 return strndup(str->data, str->length);
140 char * ASN1CALL
141 der_print_ia5_string(const heim_ia5_string *str, int flags)
143 return strndup(str->data, str->length);
146 char * ASN1CALL
147 der_print_bmp_string(const heim_bmp_string *k, int flags)
149 return strdup("<BMPString-not-supported>");
152 char * ASN1CALL
153 der_print_universal_string(const heim_universal_string *k, int flags)
155 return strdup("<UniversalString-not-supported>");
158 char * ASN1CALL
159 der_print_visible_string(const heim_visible_string *str, int flags)
161 return strdup(*str);
164 char * ASN1CALL
165 der_print_octet_string(const heim_octet_string *k, int flags)
167 char *s = NULL;
169 (void) hex_encode(k->data, k->length, &s);
170 return s;
173 char * ASN1CALL
174 der_print_heim_integer(const heim_integer *k, int flags)
176 char *s = NULL;
178 (void) der_print_hex_heim_integer(k, &s);
179 return s;
182 char * ASN1CALL
183 der_print_oid(const heim_oid *k, int flags)
185 struct rk_strpool *r = NULL;
186 const char *sym = NULL;
187 char *s = NULL;
188 size_t i;
190 (void) der_print_heim_oid(k, '.', &s);
192 if (!s)
193 return NULL;
194 r = rk_strpoolprintf(r, "{\"_type\":\"OBJECT IDENTIFIER\","
195 "\"oid\":\"%s\","
196 "\"components\":[",
198 free(s);
199 for (i = 0; i < k->length; i++)
200 r = rk_strpoolprintf(r, "%s%u", i ? "," : "", k->components[i]);
201 if (r)
202 r = rk_strpoolprintf(r, "]");
203 (void) der_find_heim_oid_by_oid(k, &sym);
204 if (sym && r) {
205 if ((s = strdup(sym))) {
206 for (i = 0; s[i]; i++)
207 if (s[i] == '_')
208 s[i] = '-';
210 r = rk_strpoolprintf(r, ",\"name\":\"%s\"", s ? s : sym);
211 free(s);
213 if (r)
214 r = rk_strpoolprintf(r, "}");
215 return rk_strpoolcollect(r);
218 char * ASN1CALL
219 der_print_bit_string(const heim_bit_string *k, int flags)
221 char *s2 = NULL;
222 char *s = NULL;
224 (void) hex_encode(k->data, k->length / 8, &s);
225 if (asprintf(&s2, "%llu:%s", (unsigned long long)k->length, s) == -1 || !s2)
226 return NULL;
227 free(s);
228 return s2;