1238384Sjkim/* crypto/ts/ts_resp_print.c */
2296341Sdelphij/*
3296341Sdelphij * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
4296341Sdelphij * 2002.
5238384Sjkim */
6238384Sjkim/* ====================================================================
7238384Sjkim * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
8238384Sjkim *
9238384Sjkim * Redistribution and use in source and binary forms, with or without
10238384Sjkim * modification, are permitted provided that the following conditions
11238384Sjkim * are met:
12238384Sjkim *
13238384Sjkim * 1. Redistributions of source code must retain the above copyright
14296341Sdelphij *    notice, this list of conditions and the following disclaimer.
15238384Sjkim *
16238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
17238384Sjkim *    notice, this list of conditions and the following disclaimer in
18238384Sjkim *    the documentation and/or other materials provided with the
19238384Sjkim *    distribution.
20238384Sjkim *
21238384Sjkim * 3. All advertising materials mentioning features or use of this
22238384Sjkim *    software must display the following acknowledgment:
23238384Sjkim *    "This product includes software developed by the OpenSSL Project
24238384Sjkim *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25238384Sjkim *
26238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27238384Sjkim *    endorse or promote products derived from this software without
28238384Sjkim *    prior written permission. For written permission, please contact
29238384Sjkim *    licensing@OpenSSL.org.
30238384Sjkim *
31238384Sjkim * 5. Products derived from this software may not be called "OpenSSL"
32238384Sjkim *    nor may "OpenSSL" appear in their names without prior written
33238384Sjkim *    permission of the OpenSSL Project.
34238384Sjkim *
35238384Sjkim * 6. Redistributions of any form whatsoever must retain the following
36238384Sjkim *    acknowledgment:
37238384Sjkim *    "This product includes software developed by the OpenSSL Project
38238384Sjkim *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39238384Sjkim *
40238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43238384Sjkim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE.
52238384Sjkim * ====================================================================
53238384Sjkim *
54238384Sjkim * This product includes cryptographic software written by Eric Young
55238384Sjkim * (eay@cryptsoft.com).  This product includes software written by Tim
56238384Sjkim * Hudson (tjh@cryptsoft.com).
57238384Sjkim *
58238384Sjkim */
59238384Sjkim
60238384Sjkim#include <stdio.h>
61238384Sjkim#include "cryptlib.h"
62238384Sjkim#include <openssl/objects.h>
63238384Sjkim#include <openssl/bn.h>
64238384Sjkim#include <openssl/x509v3.h>
65238384Sjkim#include "ts.h"
66238384Sjkim
67296341Sdelphijstruct status_map_st {
68296341Sdelphij    int bit;
69296341Sdelphij    const char *text;
70296341Sdelphij};
71238384Sjkim
72238384Sjkim/* Local function declarations. */
73238384Sjkim
74238384Sjkimstatic int TS_status_map_print(BIO *bio, struct status_map_st *a,
75296341Sdelphij                               ASN1_BIT_STRING *v);
76238384Sjkimstatic int TS_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *accuracy);
77238384Sjkim
78238384Sjkim/* Function definitions. */
79238384Sjkim
80238384Sjkimint TS_RESP_print_bio(BIO *bio, TS_RESP *a)
81296341Sdelphij{
82296341Sdelphij    TS_TST_INFO *tst_info;
83238384Sjkim
84296341Sdelphij    BIO_printf(bio, "Status info:\n");
85296341Sdelphij    TS_STATUS_INFO_print_bio(bio, TS_RESP_get_status_info(a));
86238384Sjkim
87296341Sdelphij    BIO_printf(bio, "\nTST info:\n");
88296341Sdelphij    tst_info = TS_RESP_get_tst_info(a);
89296341Sdelphij    if (tst_info != NULL)
90296341Sdelphij        TS_TST_INFO_print_bio(bio, TS_RESP_get_tst_info(a));
91296341Sdelphij    else
92296341Sdelphij        BIO_printf(bio, "Not included.\n");
93238384Sjkim
94296341Sdelphij    return 1;
95296341Sdelphij}
96296341Sdelphij
97238384Sjkimint TS_STATUS_INFO_print_bio(BIO *bio, TS_STATUS_INFO *a)
98296341Sdelphij{
99296341Sdelphij    static const char *status_map[] = {
100296341Sdelphij        "Granted.",
101296341Sdelphij        "Granted with modifications.",
102296341Sdelphij        "Rejected.",
103296341Sdelphij        "Waiting.",
104296341Sdelphij        "Revocation warning.",
105296341Sdelphij        "Revoked."
106296341Sdelphij    };
107296341Sdelphij    static struct status_map_st failure_map[] = {
108296341Sdelphij        {TS_INFO_BAD_ALG,
109296341Sdelphij         "unrecognized or unsupported algorithm identifier"},
110296341Sdelphij        {TS_INFO_BAD_REQUEST,
111296341Sdelphij         "transaction not permitted or supported"},
112296341Sdelphij        {TS_INFO_BAD_DATA_FORMAT,
113296341Sdelphij         "the data submitted has the wrong format"},
114296341Sdelphij        {TS_INFO_TIME_NOT_AVAILABLE,
115296341Sdelphij         "the TSA's time source is not available"},
116296341Sdelphij        {TS_INFO_UNACCEPTED_POLICY,
117296341Sdelphij         "the requested TSA policy is not supported by the TSA"},
118296341Sdelphij        {TS_INFO_UNACCEPTED_EXTENSION,
119296341Sdelphij         "the requested extension is not supported by the TSA"},
120296341Sdelphij        {TS_INFO_ADD_INFO_NOT_AVAILABLE,
121296341Sdelphij         "the additional information requested could not be understood "
122296341Sdelphij         "or is not available"},
123296341Sdelphij        {TS_INFO_SYSTEM_FAILURE,
124296341Sdelphij         "the request cannot be handled due to system failure"},
125296341Sdelphij        {-1, NULL}
126296341Sdelphij    };
127296341Sdelphij    long status;
128296341Sdelphij    int i, lines = 0;
129238384Sjkim
130296341Sdelphij    /* Printing status code. */
131296341Sdelphij    BIO_printf(bio, "Status: ");
132296341Sdelphij    status = ASN1_INTEGER_get(a->status);
133296341Sdelphij    if (0 <= status
134296341Sdelphij        && status < (long)(sizeof(status_map) / sizeof(status_map[0])))
135296341Sdelphij        BIO_printf(bio, "%s\n", status_map[status]);
136296341Sdelphij    else
137296341Sdelphij        BIO_printf(bio, "out of bounds\n");
138238384Sjkim
139296341Sdelphij    /* Printing status description. */
140296341Sdelphij    BIO_printf(bio, "Status description: ");
141296341Sdelphij    for (i = 0; i < sk_ASN1_UTF8STRING_num(a->text); ++i) {
142296341Sdelphij        if (i > 0)
143296341Sdelphij            BIO_puts(bio, "\t");
144296341Sdelphij        ASN1_STRING_print_ex(bio, sk_ASN1_UTF8STRING_value(a->text, i), 0);
145296341Sdelphij        BIO_puts(bio, "\n");
146296341Sdelphij    }
147296341Sdelphij    if (i == 0)
148296341Sdelphij        BIO_printf(bio, "unspecified\n");
149238384Sjkim
150296341Sdelphij    /* Printing failure information. */
151296341Sdelphij    BIO_printf(bio, "Failure info: ");
152296341Sdelphij    if (a->failure_info != NULL)
153296341Sdelphij        lines = TS_status_map_print(bio, failure_map, a->failure_info);
154296341Sdelphij    if (lines == 0)
155296341Sdelphij        BIO_printf(bio, "unspecified");
156296341Sdelphij    BIO_printf(bio, "\n");
157238384Sjkim
158296341Sdelphij    return 1;
159296341Sdelphij}
160296341Sdelphij
161238384Sjkimstatic int TS_status_map_print(BIO *bio, struct status_map_st *a,
162296341Sdelphij                               ASN1_BIT_STRING *v)
163296341Sdelphij{
164296341Sdelphij    int lines = 0;
165238384Sjkim
166296341Sdelphij    for (; a->bit >= 0; ++a) {
167296341Sdelphij        if (ASN1_BIT_STRING_get_bit(v, a->bit)) {
168296341Sdelphij            if (++lines > 1)
169296341Sdelphij                BIO_printf(bio, ", ");
170296341Sdelphij            BIO_printf(bio, "%s", a->text);
171296341Sdelphij        }
172296341Sdelphij    }
173238384Sjkim
174296341Sdelphij    return lines;
175296341Sdelphij}
176238384Sjkim
177238384Sjkimint TS_TST_INFO_print_bio(BIO *bio, TS_TST_INFO *a)
178296341Sdelphij{
179296341Sdelphij    int v;
180296341Sdelphij    ASN1_OBJECT *policy_id;
181296341Sdelphij    const ASN1_INTEGER *serial;
182296341Sdelphij    const ASN1_GENERALIZEDTIME *gtime;
183296341Sdelphij    TS_ACCURACY *accuracy;
184296341Sdelphij    const ASN1_INTEGER *nonce;
185296341Sdelphij    GENERAL_NAME *tsa_name;
186238384Sjkim
187296341Sdelphij    if (a == NULL)
188296341Sdelphij        return 0;
189238384Sjkim
190296341Sdelphij    /* Print version. */
191296341Sdelphij    v = TS_TST_INFO_get_version(a);
192296341Sdelphij    BIO_printf(bio, "Version: %d\n", v);
193238384Sjkim
194296341Sdelphij    /* Print policy id. */
195296341Sdelphij    BIO_printf(bio, "Policy OID: ");
196296341Sdelphij    policy_id = TS_TST_INFO_get_policy_id(a);
197296341Sdelphij    TS_OBJ_print_bio(bio, policy_id);
198238384Sjkim
199296341Sdelphij    /* Print message imprint. */
200296341Sdelphij    TS_MSG_IMPRINT_print_bio(bio, TS_TST_INFO_get_msg_imprint(a));
201238384Sjkim
202296341Sdelphij    /* Print serial number. */
203296341Sdelphij    BIO_printf(bio, "Serial number: ");
204296341Sdelphij    serial = TS_TST_INFO_get_serial(a);
205296341Sdelphij    if (serial == NULL)
206296341Sdelphij        BIO_printf(bio, "unspecified");
207296341Sdelphij    else
208296341Sdelphij        TS_ASN1_INTEGER_print_bio(bio, serial);
209296341Sdelphij    BIO_write(bio, "\n", 1);
210238384Sjkim
211296341Sdelphij    /* Print time stamp. */
212296341Sdelphij    BIO_printf(bio, "Time stamp: ");
213296341Sdelphij    gtime = TS_TST_INFO_get_time(a);
214296341Sdelphij    ASN1_GENERALIZEDTIME_print(bio, gtime);
215296341Sdelphij    BIO_write(bio, "\n", 1);
216238384Sjkim
217296341Sdelphij    /* Print accuracy. */
218296341Sdelphij    BIO_printf(bio, "Accuracy: ");
219296341Sdelphij    accuracy = TS_TST_INFO_get_accuracy(a);
220296341Sdelphij    if (accuracy == NULL)
221296341Sdelphij        BIO_printf(bio, "unspecified");
222296341Sdelphij    else
223296341Sdelphij        TS_ACCURACY_print_bio(bio, accuracy);
224296341Sdelphij    BIO_write(bio, "\n", 1);
225238384Sjkim
226296341Sdelphij    /* Print ordering. */
227296341Sdelphij    BIO_printf(bio, "Ordering: %s\n",
228296341Sdelphij               TS_TST_INFO_get_ordering(a) ? "yes" : "no");
229238384Sjkim
230296341Sdelphij    /* Print nonce. */
231296341Sdelphij    BIO_printf(bio, "Nonce: ");
232296341Sdelphij    nonce = TS_TST_INFO_get_nonce(a);
233296341Sdelphij    if (nonce == NULL)
234296341Sdelphij        BIO_printf(bio, "unspecified");
235296341Sdelphij    else
236296341Sdelphij        TS_ASN1_INTEGER_print_bio(bio, nonce);
237296341Sdelphij    BIO_write(bio, "\n", 1);
238238384Sjkim
239296341Sdelphij    /* Print TSA name. */
240296341Sdelphij    BIO_printf(bio, "TSA: ");
241296341Sdelphij    tsa_name = TS_TST_INFO_get_tsa(a);
242296341Sdelphij    if (tsa_name == NULL)
243296341Sdelphij        BIO_printf(bio, "unspecified");
244296341Sdelphij    else {
245296341Sdelphij        STACK_OF(CONF_VALUE) *nval;
246296341Sdelphij        if ((nval = i2v_GENERAL_NAME(NULL, tsa_name, NULL)))
247296341Sdelphij            X509V3_EXT_val_prn(bio, nval, 0, 0);
248296341Sdelphij        sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
249296341Sdelphij    }
250296341Sdelphij    BIO_write(bio, "\n", 1);
251238384Sjkim
252296341Sdelphij    /* Print extensions. */
253296341Sdelphij    TS_ext_print_bio(bio, TS_TST_INFO_get_exts(a));
254238384Sjkim
255296341Sdelphij    return 1;
256296341Sdelphij}
257238384Sjkim
258238384Sjkimstatic int TS_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *accuracy)
259296341Sdelphij{
260296341Sdelphij    const ASN1_INTEGER *seconds = TS_ACCURACY_get_seconds(accuracy);
261296341Sdelphij    const ASN1_INTEGER *millis = TS_ACCURACY_get_millis(accuracy);
262296341Sdelphij    const ASN1_INTEGER *micros = TS_ACCURACY_get_micros(accuracy);
263238384Sjkim
264296341Sdelphij    if (seconds != NULL)
265296341Sdelphij        TS_ASN1_INTEGER_print_bio(bio, seconds);
266296341Sdelphij    else
267296341Sdelphij        BIO_printf(bio, "unspecified");
268296341Sdelphij    BIO_printf(bio, " seconds, ");
269296341Sdelphij    if (millis != NULL)
270296341Sdelphij        TS_ASN1_INTEGER_print_bio(bio, millis);
271296341Sdelphij    else
272296341Sdelphij        BIO_printf(bio, "unspecified");
273296341Sdelphij    BIO_printf(bio, " millis, ");
274296341Sdelphij    if (micros != NULL)
275296341Sdelphij        TS_ASN1_INTEGER_print_bio(bio, micros);
276296341Sdelphij    else
277296341Sdelphij        BIO_printf(bio, "unspecified");
278296341Sdelphij    BIO_printf(bio, " micros");
279238384Sjkim
280296341Sdelphij    return 1;
281296341Sdelphij}
282