s4:heimdal: import lorikeet-heimdal-202201172009 (commit 5a0b45cd723628b3690ea848548b...
[samba.git] / source4 / heimdal / lib / asn1 / der_print.c
1 /*
2  * Copyright (c) 2021 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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.
34  */
35
36 #include "der_locl.h"
37 #include "hex.h"
38
39 RCSID("$Id$");
40
41 char * ASN1CALL
42 der_print_general_string(const heim_general_string *str, int flags)
43 {
44     return strdup(*str);
45 }
46
47 char * ASN1CALL
48 der_print_boolean(const int *i, int flags)
49 {
50     return *i ? strdup("true") : strdup("false");
51 }
52
53 char * ASN1CALL
54 der_print_integer(const int *i, int flags)
55 {
56     char *s = NULL;
57
58     if (asprintf(&s, "%d", *i) == -1 || s == NULL)
59         return NULL;
60     return s;
61 }
62
63 char * ASN1CALL
64 der_print_integer64(const int64_t *i, int flags)
65 {
66     char *s = NULL;
67
68     if (asprintf(&s, "%lld", (long long)*i) == -1 || s == NULL)
69         return NULL;
70     return s;
71 }
72
73 char * ASN1CALL
74 der_print_unsigned(const unsigned *u, int flags)
75 {
76     char *s = NULL;
77
78     if (asprintf(&s, "%u", *u) == -1 || s == NULL)
79         return NULL;
80     return s;
81 }
82
83 char * ASN1CALL
84 der_print_unsigned64(const uint64_t *u, int flags)
85 {
86     char *s = NULL;
87
88     if (asprintf(&s, "%llu", (long long)*u) == -1 || s == NULL)
89         return NULL;
90     return s;
91 }
92
93 char * ASN1CALL
94 der_print_generalized_time(const time_t *t, int flags)
95 {
96     struct tm tms;
97     char str[sizeof("1970-01-01T00:00:00Z")];
98
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);
108 }
109
110 char * ASN1CALL
111 der_print_utctime(const time_t *t, int flags)
112 {
113     struct tm tms;
114     char str[sizeof("1970-01-01T00:00:00Z")];
115
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);
125 }
126
127
128 char * ASN1CALL
129 der_print_utf8string(const heim_utf8_string *str, int flags)
130 {
131     return strdup(*str);
132 }
133
134 char * ASN1CALL
135 der_print_printable_string(const heim_printable_string *str, int flags)
136 {
137     return strndup(str->data, str->length);
138 }
139
140 char * ASN1CALL
141 der_print_ia5_string(const heim_ia5_string *str, int flags)
142 {
143     return strndup(str->data, str->length);
144 }
145
146 char * ASN1CALL
147 der_print_bmp_string(const heim_bmp_string *k, int flags)
148 {
149     return strdup("<BMPString-not-supported>");
150 }
151
152 char * ASN1CALL
153 der_print_universal_string(const heim_universal_string *k, int flags)
154 {
155     return strdup("<UniversalString-not-supported>");
156 }
157
158 char * ASN1CALL
159 der_print_visible_string(const heim_visible_string *str, int flags)
160 {
161     return strdup(*str);
162 }
163
164 char * ASN1CALL
165 der_print_octet_string(const heim_octet_string *k, int flags)
166 {
167     char *s = NULL;
168
169     (void) hex_encode(k->data, k->length, &s);
170     return s;
171 }
172
173 char * ASN1CALL
174 der_print_heim_integer(const heim_integer *k, int flags)
175 {
176     char *s = NULL;
177
178     (void) der_print_hex_heim_integer(k, &s);
179     return s;
180 }
181
182 char * ASN1CALL
183 der_print_oid(const heim_oid *k, int flags)
184 {
185     struct rk_strpool *r = NULL;
186     const char *sym = NULL;
187     char *s = NULL;
188     size_t i;
189
190     (void) der_print_heim_oid(k, '.', &s);
191
192     if (!s)
193         return NULL;
194     r = rk_strpoolprintf(r, "{\"_type\":\"OBJECT IDENTIFIER\","
195                          "\"oid\":\"%s\","
196                          "\"components\":[",
197                          s);
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] = '-';
209         }
210         r = rk_strpoolprintf(r, ",\"name\":\"%s\"", s ? s : sym);
211         free(s);
212     }
213     if (r)
214         r = rk_strpoolprintf(r, "}");
215     return rk_strpoolcollect(r);
216 }
217
218 char * ASN1CALL
219 der_print_bit_string(const heim_bit_string *k, int flags)
220 {
221     char *s2 = NULL;
222     char *s = NULL;
223
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;
229 }