Line data Source code
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 0 : der_print_general_string(const heim_general_string *str, int flags)
43 : {
44 0 : return strdup(*str);
45 : }
46 :
47 : char * ASN1CALL
48 0 : der_print_boolean(const int *i, int flags)
49 : {
50 0 : return *i ? strdup("true") : strdup("false");
51 : }
52 :
53 : char * ASN1CALL
54 0 : der_print_integer(const int *i, int flags)
55 : {
56 0 : char *s = NULL;
57 :
58 0 : if (asprintf(&s, "%d", *i) == -1 || s == NULL)
59 0 : return NULL;
60 0 : return s;
61 : }
62 :
63 : char * ASN1CALL
64 0 : der_print_integer64(const int64_t *i, int flags)
65 : {
66 0 : char *s = NULL;
67 :
68 0 : if (asprintf(&s, "%lld", (long long)*i) == -1 || s == NULL)
69 0 : return NULL;
70 0 : return s;
71 : }
72 :
73 : char * ASN1CALL
74 0 : der_print_unsigned(const unsigned *u, int flags)
75 : {
76 0 : char *s = NULL;
77 :
78 0 : if (asprintf(&s, "%u", *u) == -1 || s == NULL)
79 0 : return NULL;
80 0 : return s;
81 : }
82 :
83 : char * ASN1CALL
84 0 : der_print_unsigned64(const uint64_t *u, int flags)
85 : {
86 0 : char *s = NULL;
87 :
88 0 : if (asprintf(&s, "%llu", (long long)*u) == -1 || s == NULL)
89 0 : return NULL;
90 0 : return s;
91 : }
92 :
93 : char * ASN1CALL
94 0 : der_print_generalized_time(const time_t *t, int flags)
95 : {
96 0 : struct tm tms;
97 0 : 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 0 : if (strftime(str, sizeof(str), "%Y-%m-%dT%H:%M:%SZ", gmtime_r(t, &tms)) == 0)
105 0 : return NULL;
106 : #endif
107 0 : return strdup(str);
108 : }
109 :
110 : char * ASN1CALL
111 0 : der_print_utctime(const time_t *t, int flags)
112 : {
113 0 : struct tm tms;
114 0 : 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 0 : if (strftime(str, sizeof(str), "%Y-%m-%dT%H:%M:%SZ", gmtime_r(t, &tms)) == 0)
122 0 : return NULL;
123 : #endif
124 0 : return strdup(str);
125 : }
126 :
127 :
128 : char * ASN1CALL
129 0 : der_print_utf8string(const heim_utf8_string *str, int flags)
130 : {
131 0 : return strdup(*str);
132 : }
133 :
134 : char * ASN1CALL
135 0 : der_print_printable_string(const heim_printable_string *str, int flags)
136 : {
137 0 : return strndup(str->data, str->length);
138 : }
139 :
140 : char * ASN1CALL
141 0 : der_print_ia5_string(const heim_ia5_string *str, int flags)
142 : {
143 0 : return strndup(str->data, str->length);
144 : }
145 :
146 : char * ASN1CALL
147 0 : der_print_bmp_string(const heim_bmp_string *k, int flags)
148 : {
149 0 : return strdup("<BMPString-not-supported>");
150 : }
151 :
152 : char * ASN1CALL
153 0 : der_print_universal_string(const heim_universal_string *k, int flags)
154 : {
155 0 : return strdup("<UniversalString-not-supported>");
156 : }
157 :
158 : char * ASN1CALL
159 0 : der_print_visible_string(const heim_visible_string *str, int flags)
160 : {
161 0 : return strdup(*str);
162 : }
163 :
164 : char * ASN1CALL
165 0 : der_print_octet_string(const heim_octet_string *k, int flags)
166 : {
167 0 : char *s = NULL;
168 :
169 0 : (void) hex_encode(k->data, k->length, &s);
170 0 : return s;
171 : }
172 :
173 : char * ASN1CALL
174 0 : der_print_heim_integer(const heim_integer *k, int flags)
175 : {
176 0 : char *s = NULL;
177 :
178 0 : (void) der_print_hex_heim_integer(k, &s);
179 0 : return s;
180 : }
181 :
182 : char * ASN1CALL
183 0 : der_print_oid(const heim_oid *k, int flags)
184 : {
185 0 : struct rk_strpool *r = NULL;
186 0 : const char *sym = NULL;
187 0 : char *s = NULL;
188 0 : size_t i;
189 :
190 0 : (void) der_print_heim_oid(k, '.', &s);
191 :
192 0 : if (!s)
193 0 : return NULL;
194 0 : r = rk_strpoolprintf(r, "{\"_type\":\"OBJECT IDENTIFIER\","
195 : "\"oid\":\"%s\","
196 : "\"components\":[",
197 : s);
198 0 : free(s);
199 0 : for (i = 0; i < k->length; i++)
200 0 : r = rk_strpoolprintf(r, "%s%u", i ? "," : "", k->components[i]);
201 0 : if (r)
202 0 : r = rk_strpoolprintf(r, "]");
203 0 : (void) der_find_heim_oid_by_oid(k, &sym);
204 0 : if (sym && r) {
205 0 : if ((s = strdup(sym))) {
206 0 : for (i = 0; s[i]; i++)
207 0 : if (s[i] == '_')
208 0 : s[i] = '-';
209 : }
210 0 : r = rk_strpoolprintf(r, ",\"name\":\"%s\"", s ? s : sym);
211 0 : free(s);
212 : }
213 0 : if (r)
214 0 : r = rk_strpoolprintf(r, "}");
215 0 : return rk_strpoolcollect(r);
216 : }
217 :
218 : char * ASN1CALL
219 0 : der_print_bit_string(const heim_bit_string *k, int flags)
220 : {
221 0 : char *s2 = NULL;
222 0 : char *s = NULL;
223 :
224 0 : (void) hex_encode(k->data, k->length / 8, &s);
225 0 : if (asprintf(&s2, "%llu:%s", (unsigned long long)k->length, s) == -1 || !s2)
226 0 : s2 = NULL;
227 0 : free(s);
228 0 : return s2;
229 : }
|