xref: /openbsd/lib/libcrypto/ts/ts_rsp_print.c (revision bb933e2f)
1 /* $OpenBSD: ts_rsp_print.c,v 1.7 2023/07/07 07:25:21 beck Exp $ */
2 /* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3  * project 2002.
4  */
5 /* ====================================================================
6  * Copyright (c) 2006 The OpenSSL Project.  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
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 
59 #include <stdio.h>
60 
61 #include <openssl/bn.h>
62 #include <openssl/objects.h>
63 #include <openssl/ts.h>
64 #include <openssl/x509v3.h>
65 
66 #include "ts_local.h"
67 
68 struct status_map_st {
69 	int bit;
70 	const char *text;
71 };
72 
73 /* Local function declarations. */
74 
75 static int TS_status_map_print(BIO *bio, struct status_map_st *a,
76     ASN1_BIT_STRING *v);
77 static int TS_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *accuracy);
78 
79 /* Function definitions. */
80 
81 int
TS_RESP_print_bio(BIO * bio,TS_RESP * a)82 TS_RESP_print_bio(BIO *bio, TS_RESP *a)
83 {
84 	TS_TST_INFO *tst_info;
85 
86 	BIO_printf(bio, "Status info:\n");
87 	TS_STATUS_INFO_print_bio(bio, TS_RESP_get_status_info(a));
88 
89 	BIO_printf(bio, "\nTST info:\n");
90 	tst_info = TS_RESP_get_tst_info(a);
91 	if (tst_info != NULL)
92 		TS_TST_INFO_print_bio(bio, TS_RESP_get_tst_info(a));
93 	else
94 		BIO_printf(bio, "Not included.\n");
95 
96 	return 1;
97 }
98 LCRYPTO_ALIAS(TS_RESP_print_bio);
99 
100 int
TS_STATUS_INFO_print_bio(BIO * bio,TS_STATUS_INFO * a)101 TS_STATUS_INFO_print_bio(BIO *bio, TS_STATUS_INFO *a)
102 {
103 	static const char *status_map[] = {
104 		"Granted.",
105 		"Granted with modifications.",
106 		"Rejected.",
107 		"Waiting.",
108 		"Revocation warning.",
109 		"Revoked."
110 	};
111 	static struct status_map_st failure_map[] = {
112 		{
113 			TS_INFO_BAD_ALG,
114 			"unrecognized or unsupported algorithm identifier"
115 		},
116 		{
117 			TS_INFO_BAD_REQUEST,
118 			"transaction not permitted or supported"
119 		},
120 		{
121 			TS_INFO_BAD_DATA_FORMAT,
122 			"the data submitted has the wrong format"
123 		},
124 		{
125 			TS_INFO_TIME_NOT_AVAILABLE,
126 			"the TSA's time source is not available"
127 		},
128 		{
129 			TS_INFO_UNACCEPTED_POLICY,
130 			"the requested TSA policy is not supported by the TSA"
131 		},
132 		{
133 			TS_INFO_UNACCEPTED_EXTENSION,
134 			"the requested extension is not supported by the TSA"
135 		},
136 		{
137 			TS_INFO_ADD_INFO_NOT_AVAILABLE,
138 			"the additional information requested could not be understood "
139 			"or is not available"
140 		},
141 		{
142 			TS_INFO_SYSTEM_FAILURE,
143 			"the request cannot be handled due to system failure"
144 		},
145 		{ -1, NULL }
146 	};
147 	long status;
148 	int i, lines = 0;
149 
150 	/* Printing status code. */
151 	BIO_printf(bio, "Status: ");
152 	status = ASN1_INTEGER_get(a->status);
153 	if (0 <= status &&
154 	    status < (long)(sizeof(status_map) / sizeof(status_map[0])))
155 		BIO_printf(bio, "%s\n", status_map[status]);
156 	else
157 		BIO_printf(bio, "out of bounds\n");
158 
159 	/* Printing status description. */
160 	BIO_printf(bio, "Status description: ");
161 	for (i = 0; i < sk_ASN1_UTF8STRING_num(a->text); ++i) {
162 		if (i > 0)
163 			BIO_puts(bio, "\t");
164 		ASN1_STRING_print_ex(bio, sk_ASN1_UTF8STRING_value(a->text, i),
165 		    0);
166 		BIO_puts(bio, "\n");
167 	}
168 	if (i == 0)
169 		BIO_printf(bio, "unspecified\n");
170 
171 	/* Printing failure information. */
172 	BIO_printf(bio, "Failure info: ");
173 	if (a->failure_info != NULL)
174 		lines = TS_status_map_print(bio, failure_map, a->failure_info);
175 	if (lines == 0)
176 		BIO_printf(bio, "unspecified");
177 	BIO_printf(bio, "\n");
178 
179 	return 1;
180 }
181 LCRYPTO_ALIAS(TS_STATUS_INFO_print_bio);
182 
183 static int
TS_status_map_print(BIO * bio,struct status_map_st * a,ASN1_BIT_STRING * v)184 TS_status_map_print(BIO *bio, struct status_map_st *a, ASN1_BIT_STRING *v)
185 {
186 	int lines = 0;
187 
188 	for (; a->bit >= 0; ++a) {
189 		if (ASN1_BIT_STRING_get_bit(v, a->bit)) {
190 			if (++lines > 1)
191 				BIO_printf(bio, ", ");
192 			BIO_printf(bio, "%s", a->text);
193 		}
194 	}
195 
196 	return lines;
197 }
198 
199 int
TS_TST_INFO_print_bio(BIO * bio,TS_TST_INFO * a)200 TS_TST_INFO_print_bio(BIO *bio, TS_TST_INFO *a)
201 {
202 	int v;
203 	ASN1_OBJECT *policy_id;
204 	const ASN1_INTEGER *serial;
205 	const ASN1_GENERALIZEDTIME *gtime;
206 	TS_ACCURACY *accuracy;
207 	const ASN1_INTEGER *nonce;
208 	GENERAL_NAME *tsa_name;
209 
210 	if (a == NULL)
211 		return 0;
212 
213 	/* Print version. */
214 	v = TS_TST_INFO_get_version(a);
215 	BIO_printf(bio, "Version: %d\n", v);
216 
217 	/* Print policy id. */
218 	BIO_printf(bio, "Policy OID: ");
219 	policy_id = TS_TST_INFO_get_policy_id(a);
220 	TS_OBJ_print_bio(bio, policy_id);
221 
222 	/* Print message imprint. */
223 	TS_MSG_IMPRINT_print_bio(bio, TS_TST_INFO_get_msg_imprint(a));
224 
225 	/* Print serial number. */
226 	BIO_printf(bio, "Serial number: ");
227 	serial = TS_TST_INFO_get_serial(a);
228 	if (serial == NULL)
229 		BIO_printf(bio, "unspecified");
230 	else
231 		TS_ASN1_INTEGER_print_bio(bio, serial);
232 	BIO_write(bio, "\n", 1);
233 
234 	/* Print time stamp. */
235 	BIO_printf(bio, "Time stamp: ");
236 	gtime = TS_TST_INFO_get_time(a);
237 	ASN1_GENERALIZEDTIME_print(bio, gtime);
238 	BIO_write(bio, "\n", 1);
239 
240 	/* Print accuracy. */
241 	BIO_printf(bio, "Accuracy: ");
242 	accuracy = TS_TST_INFO_get_accuracy(a);
243 	if (accuracy == NULL)
244 		BIO_printf(bio, "unspecified");
245 	else
246 		TS_ACCURACY_print_bio(bio, accuracy);
247 	BIO_write(bio, "\n", 1);
248 
249 	/* Print ordering. */
250 	BIO_printf(bio, "Ordering: %s\n",
251 	    TS_TST_INFO_get_ordering(a) ? "yes" : "no");
252 
253 	/* Print nonce. */
254 	BIO_printf(bio, "Nonce: ");
255 	nonce = TS_TST_INFO_get_nonce(a);
256 	if (nonce == NULL)
257 		BIO_printf(bio, "unspecified");
258 	else
259 		TS_ASN1_INTEGER_print_bio(bio, nonce);
260 	BIO_write(bio, "\n", 1);
261 
262 	/* Print TSA name. */
263 	BIO_printf(bio, "TSA: ");
264 	tsa_name = TS_TST_INFO_get_tsa(a);
265 	if (tsa_name == NULL)
266 		BIO_printf(bio, "unspecified");
267 	else {
268 		STACK_OF(CONF_VALUE) *nval;
269 		if ((nval = i2v_GENERAL_NAME(NULL, tsa_name, NULL)))
270 			X509V3_EXT_val_prn(bio, nval, 0, 0);
271 		sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
272 	}
273 	BIO_write(bio, "\n", 1);
274 
275 	/* Print extensions. */
276 	TS_ext_print_bio(bio, TS_TST_INFO_get_exts(a));
277 
278 	return 1;
279 }
280 LCRYPTO_ALIAS(TS_TST_INFO_print_bio);
281 
282 static int
TS_ACCURACY_print_bio(BIO * bio,const TS_ACCURACY * accuracy)283 TS_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *accuracy)
284 {
285 	const ASN1_INTEGER *seconds = TS_ACCURACY_get_seconds(accuracy);
286 	const ASN1_INTEGER *millis = TS_ACCURACY_get_millis(accuracy);
287 	const ASN1_INTEGER *micros = TS_ACCURACY_get_micros(accuracy);
288 
289 	if (seconds != NULL)
290 		TS_ASN1_INTEGER_print_bio(bio, seconds);
291 	else
292 		BIO_printf(bio, "unspecified");
293 	BIO_printf(bio, " seconds, ");
294 	if (millis != NULL)
295 		TS_ASN1_INTEGER_print_bio(bio, millis);
296 	else
297 		BIO_printf(bio, "unspecified");
298 	BIO_printf(bio, " millis, ");
299 	if (micros != NULL)
300 		TS_ASN1_INTEGER_print_bio(bio, micros);
301 	else
302 		BIO_printf(bio, "unspecified");
303 	BIO_printf(bio, " micros");
304 
305 	return 1;
306 }
307