118334Speter/* $OpenBSD: ts_rsp_print.c,v 1.7 2023/07/07 07:25:21 beck Exp $ */
290075Sobrien/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3169689Skan * project 2002.
418334Speter */
518334Speter/* ====================================================================
618334Speter * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
790075Sobrien *
818334Speter * Redistribution and use in source and binary forms, with or without
990075Sobrien * modification, are permitted provided that the following conditions
1090075Sobrien * are met:
1190075Sobrien *
1290075Sobrien * 1. Redistributions of source code must retain the above copyright
1318334Speter *    notice, this list of conditions and the following disclaimer.
1490075Sobrien *
1590075Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1690075Sobrien *    notice, this list of conditions and the following disclaimer in
1790075Sobrien *    the documentation and/or other materials provided with the
1818334Speter *    distribution.
1918334Speter *
2090075Sobrien * 3. All advertising materials mentioning features or use of this
21169689Skan *    software must display the following acknowledgment:
22169689Skan *    "This product includes software developed by the OpenSSL Project
2318334Speter *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2418334Speter *
2518334Speter * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2618334Speter *    endorse or promote products derived from this software without
2718334Speter *    prior written permission. For written permission, please contact
2818334Speter *    licensing@OpenSSL.org.
2918334Speter *
3018334Speter * 5. Products derived from this software may not be called "OpenSSL"
3118334Speter *    nor may "OpenSSL" appear in their names without prior written
3218334Speter *    permission of the OpenSSL Project.
3318334Speter *
3418334Speter * 6. Redistributions of any form whatsoever must retain the following
3518334Speter *    acknowledgment:
3618334Speter *    "This product includes software developed by the OpenSSL Project
3718334Speter *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3818334Speter *
39169689Skan * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4018334Speter * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4118334Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4218334Speter * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4318334Speter * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4418334Speter * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4518334Speter * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4618334Speter * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4718334Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4818334Speter * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4918334Speter * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5018334Speter * OF THE POSSIBILITY OF SUCH DAMAGE.
5190075Sobrien * ====================================================================
5218334Speter *
5318334Speter * This product includes cryptographic software written by Eric Young
5418334Speter * (eay@cryptsoft.com).  This product includes software written by Tim
5518334Speter * Hudson (tjh@cryptsoft.com).
5618334Speter *
5752284Sobrien */
5852284Sobrien
5952284Sobrien#include <stdio.h>
6052284Sobrien
6118334Speter#include <openssl/bn.h>
6218334Speter#include <openssl/objects.h>
6318334Speter#include <openssl/ts.h>
6418334Speter#include <openssl/x509v3.h>
6518334Speter
6618334Speter#include "ts_local.h"
6718334Speter
6818334Speterstruct status_map_st {
6918334Speter	int bit;
7018334Speter	const char *text;
7118334Speter};
7218334Speter
7318334Speter/* Local function declarations. */
7418334Speter
7518334Speterstatic int TS_status_map_print(BIO *bio, struct status_map_st *a,
7618334Speter    ASN1_BIT_STRING *v);
7718334Speterstatic int TS_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *accuracy);
7818334Speter
79169689Skan/* Function definitions. */
8018334Speter
8118334Speterint
8218334SpeterTS_RESP_print_bio(BIO *bio, TS_RESP *a)
8318334Speter{
8418334Speter	TS_TST_INFO *tst_info;
8518334Speter
8618334Speter	BIO_printf(bio, "Status info:\n");
8718334Speter	TS_STATUS_INFO_print_bio(bio, TS_RESP_get_status_info(a));
8818334Speter
8918334Speter	BIO_printf(bio, "\nTST info:\n");
9018334Speter	tst_info = TS_RESP_get_tst_info(a);
9118334Speter	if (tst_info != NULL)
9218334Speter		TS_TST_INFO_print_bio(bio, TS_RESP_get_tst_info(a));
9318334Speter	else
9418334Speter		BIO_printf(bio, "Not included.\n");
9518334Speter
9618334Speter	return 1;
9718334Speter}
9818334SpeterLCRYPTO_ALIAS(TS_RESP_print_bio);
9918334Speter
10018334Speterint
10118334SpeterTS_STATUS_INFO_print_bio(BIO *bio, TS_STATUS_INFO *a)
10218334Speter{
10318334Speter	static const char *status_map[] = {
10418334Speter		"Granted.",
10518334Speter		"Granted with modifications.",
10618334Speter		"Rejected.",
10718334Speter		"Waiting.",
10818334Speter		"Revocation warning.",
10918334Speter		"Revoked."
11018334Speter	};
11118334Speter	static struct status_map_st failure_map[] = {
11290075Sobrien		{
11318334Speter			TS_INFO_BAD_ALG,
11418334Speter			"unrecognized or unsupported algorithm identifier"
11550397Sobrien		},
116132718Skan		{
117132718Skan			TS_INFO_BAD_REQUEST,
11852284Sobrien			"transaction not permitted or supported"
11918334Speter		},
12090075Sobrien		{
12150397Sobrien			TS_INFO_BAD_DATA_FORMAT,
12290075Sobrien			"the data submitted has the wrong format"
12318334Speter		},
12418334Speter		{
12518334Speter			TS_INFO_TIME_NOT_AVAILABLE,
12618334Speter			"the TSA's time source is not available"
12718334Speter		},
12818334Speter		{
12918334Speter			TS_INFO_UNACCEPTED_POLICY,
13018334Speter			"the requested TSA policy is not supported by the TSA"
13118334Speter		},
13218334Speter		{
13352284Sobrien			TS_INFO_UNACCEPTED_EXTENSION,
13490075Sobrien			"the requested extension is not supported by the TSA"
13590075Sobrien		},
136169689Skan		{
137169689Skan			TS_INFO_ADD_INFO_NOT_AVAILABLE,
138169689Skan			"the additional information requested could not be understood "
13918334Speter			"or is not available"
14018334Speter		},
14118334Speter		{
14218334Speter			TS_INFO_SYSTEM_FAILURE,
14318334Speter			"the request cannot be handled due to system failure"
14418334Speter		},
14518334Speter		{ -1, NULL }
14618334Speter	};
14718334Speter	long status;
14818334Speter	int i, lines = 0;
14918334Speter
15018334Speter	/* Printing status code. */
15118334Speter	BIO_printf(bio, "Status: ");
15218334Speter	status = ASN1_INTEGER_get(a->status);
15318334Speter	if (0 <= status &&
15418334Speter	    status < (long)(sizeof(status_map) / sizeof(status_map[0])))
15518334Speter		BIO_printf(bio, "%s\n", status_map[status]);
15618334Speter	else
15718334Speter		BIO_printf(bio, "out of bounds\n");
15818334Speter
15918334Speter	/* Printing status description. */
16018334Speter	BIO_printf(bio, "Status description: ");
16118334Speter	for (i = 0; i < sk_ASN1_UTF8STRING_num(a->text); ++i) {
16218334Speter		if (i > 0)
16318334Speter			BIO_puts(bio, "\t");
16418334Speter		ASN1_STRING_print_ex(bio, sk_ASN1_UTF8STRING_value(a->text, i),
16518334Speter		    0);
16618334Speter		BIO_puts(bio, "\n");
16718334Speter	}
16818334Speter	if (i == 0)
16918334Speter		BIO_printf(bio, "unspecified\n");
17018334Speter
17118334Speter	/* Printing failure information. */
17218334Speter	BIO_printf(bio, "Failure info: ");
17318334Speter	if (a->failure_info != NULL)
174132718Skan		lines = TS_status_map_print(bio, failure_map, a->failure_info);
175132718Skan	if (lines == 0)
176132718Skan		BIO_printf(bio, "unspecified");
177132718Skan	BIO_printf(bio, "\n");
178132718Skan
179132718Skan	return 1;
180132718Skan}
181132718SkanLCRYPTO_ALIAS(TS_STATUS_INFO_print_bio);
182132718Skan
183132718Skanstatic int
18490075SobrienTS_status_map_print(BIO *bio, struct status_map_st *a, ASN1_BIT_STRING *v)
185132718Skan{
18690075Sobrien	int lines = 0;
187132718Skan
188132718Skan	for (; a->bit >= 0; ++a) {
189132718Skan		if (ASN1_BIT_STRING_get_bit(v, a->bit)) {
190132718Skan			if (++lines > 1)
191132718Skan				BIO_printf(bio, ", ");
192132718Skan			BIO_printf(bio, "%s", a->text);
193132718Skan		}
194132718Skan	}
195132718Skan
196132718Skan	return lines;
197132718Skan}
198132718Skan
199132718Skanint
200132718SkanTS_TST_INFO_print_bio(BIO *bio, TS_TST_INFO *a)
201132718Skan{
202132718Skan	int v;
203132718Skan	ASN1_OBJECT *policy_id;
204132718Skan	const ASN1_INTEGER *serial;
205132718Skan	const ASN1_GENERALIZEDTIME *gtime;
206132718Skan	TS_ACCURACY *accuracy;
207132718Skan	const ASN1_INTEGER *nonce;
208132718Skan	GENERAL_NAME *tsa_name;
209132718Skan
210132718Skan	if (a == NULL)
211132718Skan		return 0;
212132718Skan
213132718Skan	/* Print version. */
214132718Skan	v = TS_TST_INFO_get_version(a);
215132718Skan	BIO_printf(bio, "Version: %d\n", v);
216132718Skan
217132718Skan	/* Print policy id. */
21890075Sobrien	BIO_printf(bio, "Policy OID: ");
219132718Skan	policy_id = TS_TST_INFO_get_policy_id(a);
22090075Sobrien	TS_OBJ_print_bio(bio, policy_id);
22118334Speter
22218334Speter	/* Print message imprint. */
22318334Speter	TS_MSG_IMPRINT_print_bio(bio, TS_TST_INFO_get_msg_imprint(a));
22418334Speter
22518334Speter	/* Print serial number. */
22618334Speter	BIO_printf(bio, "Serial number: ");
227132718Skan	serial = TS_TST_INFO_get_serial(a);
22818334Speter	if (serial == NULL)
22918334Speter		BIO_printf(bio, "unspecified");
23018334Speter	else
23118334Speter		TS_ASN1_INTEGER_print_bio(bio, serial);
232122180Skan	BIO_write(bio, "\n", 1);
233122180Skan
234122180Skan	/* Print time stamp. */
235122180Skan	BIO_printf(bio, "Time stamp: ");
236122180Skan	gtime = TS_TST_INFO_get_time(a);
237122180Skan	ASN1_GENERALIZEDTIME_print(bio, gtime);
23818334Speter	BIO_write(bio, "\n", 1);
23918334Speter
24018334Speter	/* Print accuracy. */
24118334Speter	BIO_printf(bio, "Accuracy: ");
24218334Speter	accuracy = TS_TST_INFO_get_accuracy(a);
24318334Speter	if (accuracy == NULL)
24418334Speter		BIO_printf(bio, "unspecified");
24518334Speter	else
24618334Speter		TS_ACCURACY_print_bio(bio, accuracy);
24718334Speter	BIO_write(bio, "\n", 1);
24818334Speter
24918334Speter	/* Print ordering. */
25018334Speter	BIO_printf(bio, "Ordering: %s\n",
25118334Speter	    TS_TST_INFO_get_ordering(a) ? "yes" : "no");
25218334Speter
25318334Speter	/* Print nonce. */
25418334Speter	BIO_printf(bio, "Nonce: ");
25518334Speter	nonce = TS_TST_INFO_get_nonce(a);
25618334Speter	if (nonce == NULL)
25718334Speter		BIO_printf(bio, "unspecified");
25818334Speter	else
259169689Skan		TS_ASN1_INTEGER_print_bio(bio, nonce);
26018334Speter	BIO_write(bio, "\n", 1);
26118334Speter
26218334Speter	/* Print TSA name. */
26318334Speter	BIO_printf(bio, "TSA: ");
26418334Speter	tsa_name = TS_TST_INFO_get_tsa(a);
26518334Speter	if (tsa_name == NULL)
26618334Speter		BIO_printf(bio, "unspecified");
267132718Skan	else {
26818334Speter		STACK_OF(CONF_VALUE) *nval;
26918334Speter		if ((nval = i2v_GENERAL_NAME(NULL, tsa_name, NULL)))
27018334Speter			X509V3_EXT_val_prn(bio, nval, 0, 0);
27118334Speter		sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
27218334Speter	}
27318334Speter	BIO_write(bio, "\n", 1);
27418334Speter
27518334Speter	/* Print extensions. */
27618334Speter	TS_ext_print_bio(bio, TS_TST_INFO_get_exts(a));
27718334Speter
27818334Speter	return 1;
27918334Speter}
28018334SpeterLCRYPTO_ALIAS(TS_TST_INFO_print_bio);
28118334Speter
28218334Speterstatic int
28318334SpeterTS_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *accuracy)
28418334Speter{
28518334Speter	const ASN1_INTEGER *seconds = TS_ACCURACY_get_seconds(accuracy);
28618334Speter	const ASN1_INTEGER *millis = TS_ACCURACY_get_millis(accuracy);
28718334Speter	const ASN1_INTEGER *micros = TS_ACCURACY_get_micros(accuracy);
28818334Speter
28950397Sobrien	if (seconds != NULL)
29018334Speter		TS_ASN1_INTEGER_print_bio(bio, seconds);
29118334Speter	else
29218334Speter		BIO_printf(bio, "unspecified");
29318334Speter	BIO_printf(bio, " seconds, ");
29418334Speter	if (millis != NULL)
29518334Speter		TS_ASN1_INTEGER_print_bio(bio, millis);
29618334Speter	else
29718334Speter		BIO_printf(bio, "unspecified");
298132718Skan	BIO_printf(bio, " millis, ");
299132718Skan	if (micros != NULL)
30018334Speter		TS_ASN1_INTEGER_print_bio(bio, micros);
30118334Speter	else
30218334Speter		BIO_printf(bio, "unspecified");
30318334Speter	BIO_printf(bio, " micros");
30418334Speter
30518334Speter	return 1;
30618334Speter}
30718334Speter