f2fae6929433f69c6f14da6268cf9954d4f87eb0
[oweals/openssl.git] / crypto / ts / ts_rsp_print.c
1 /* crypto/ts/ts_resp_print.c */
2 /*
3  * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
4  * 2002.
5  */
6 /* ====================================================================
7  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59
60 #include <stdio.h>
61 #include "internal/cryptlib.h"
62 #include <openssl/objects.h>
63 #include <openssl/bn.h>
64 #include <openssl/x509v3.h>
65 #include <openssl/ts.h>
66 #include "ts_lcl.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, const struct status_map_st *a,
76                                const 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 {
83     BIO_printf(bio, "Status info:\n");
84     TS_STATUS_INFO_print_bio(bio, a->status_info);
85
86     BIO_printf(bio, "\nTST info:\n");
87     if (a->tst_info != NULL)
88         TS_TST_INFO_print_bio(bio, a->tst_info);
89     else
90         BIO_printf(bio, "Not included.\n");
91
92     return 1;
93 }
94
95 int TS_STATUS_INFO_print_bio(BIO *bio, TS_STATUS_INFO *a)
96 {
97     static const char *status_map[] = {
98         "Granted.",
99         "Granted with modifications.",
100         "Rejected.",
101         "Waiting.",
102         "Revocation warning.",
103         "Revoked."
104     };
105     static const struct status_map_st failure_map[] = {
106         {TS_INFO_BAD_ALG,
107          "unrecognized or unsupported algorithm identifier"},
108         {TS_INFO_BAD_REQUEST,
109          "transaction not permitted or supported"},
110         {TS_INFO_BAD_DATA_FORMAT,
111          "the data submitted has the wrong format"},
112         {TS_INFO_TIME_NOT_AVAILABLE,
113          "the TSA's time source is not available"},
114         {TS_INFO_UNACCEPTED_POLICY,
115          "the requested TSA policy is not supported by the TSA"},
116         {TS_INFO_UNACCEPTED_EXTENSION,
117          "the requested extension is not supported by the TSA"},
118         {TS_INFO_ADD_INFO_NOT_AVAILABLE,
119          "the additional information requested could not be understood "
120          "or is not available"},
121         {TS_INFO_SYSTEM_FAILURE,
122          "the request cannot be handled due to system failure"},
123         {-1, NULL}
124     };
125     long status;
126     int i, lines = 0;
127
128     /* Printing status code. */
129     BIO_printf(bio, "Status: ");
130     status = ASN1_INTEGER_get(a->status);
131     if (0 <= status && status < (long)OSSL_NELEM(status_map))
132         BIO_printf(bio, "%s\n", status_map[status]);
133     else
134         BIO_printf(bio, "out of bounds\n");
135
136     /* Printing status description. */
137     BIO_printf(bio, "Status description: ");
138     for (i = 0; i < sk_ASN1_UTF8STRING_num(a->text); ++i) {
139         if (i > 0)
140             BIO_puts(bio, "\t");
141         ASN1_STRING_print_ex(bio, sk_ASN1_UTF8STRING_value(a->text, i), 0);
142         BIO_puts(bio, "\n");
143     }
144     if (i == 0)
145         BIO_printf(bio, "unspecified\n");
146
147     /* Printing failure information. */
148     BIO_printf(bio, "Failure info: ");
149     if (a->failure_info != NULL)
150         lines = ts_status_map_print(bio, failure_map, a->failure_info);
151     if (lines == 0)
152         BIO_printf(bio, "unspecified");
153     BIO_printf(bio, "\n");
154
155     return 1;
156 }
157
158 static int ts_status_map_print(BIO *bio, const struct status_map_st *a,
159                                const ASN1_BIT_STRING *v)
160 {
161     int lines = 0;
162
163     for (; a->bit >= 0; ++a) {
164         if (ASN1_BIT_STRING_get_bit(v, a->bit)) {
165             if (++lines > 1)
166                 BIO_printf(bio, ", ");
167             BIO_printf(bio, "%s", a->text);
168         }
169     }
170
171     return lines;
172 }
173
174 int TS_TST_INFO_print_bio(BIO *bio, TS_TST_INFO *a)
175 {
176     int v;
177
178     if (a == NULL)
179         return 0;
180
181     /* Print version. */
182     v = ASN1_INTEGER_get(a->version);
183     BIO_printf(bio, "Version: %d\n", v);
184
185     /* Print policy id. */
186     BIO_printf(bio, "Policy OID: ");
187     TS_OBJ_print_bio(bio, a->policy_id);
188
189     /* Print message imprint. */
190     TS_MSG_IMPRINT_print_bio(bio, a->msg_imprint);
191
192     /* Print serial number. */
193     BIO_printf(bio, "Serial number: ");
194     if (a->serial == NULL)
195         BIO_printf(bio, "unspecified");
196     else
197         TS_ASN1_INTEGER_print_bio(bio, a->serial);
198     BIO_write(bio, "\n", 1);
199
200     /* Print time stamp. */
201     BIO_printf(bio, "Time stamp: ");
202     ASN1_GENERALIZEDTIME_print(bio, a->time);
203     BIO_write(bio, "\n", 1);
204
205     /* Print accuracy. */
206     BIO_printf(bio, "Accuracy: ");
207     if (a->accuracy == NULL)
208         BIO_printf(bio, "unspecified");
209     else
210         ts_ACCURACY_print_bio(bio, a->accuracy);
211     BIO_write(bio, "\n", 1);
212
213     /* Print ordering. */
214     BIO_printf(bio, "Ordering: %s\n", a->ordering ? "yes" : "no");
215
216     /* Print nonce. */
217     BIO_printf(bio, "Nonce: ");
218     if (a->nonce == NULL)
219         BIO_printf(bio, "unspecified");
220     else
221         TS_ASN1_INTEGER_print_bio(bio, a->nonce);
222     BIO_write(bio, "\n", 1);
223
224     /* Print TSA name. */
225     BIO_printf(bio, "TSA: ");
226     if (a->tsa == NULL)
227         BIO_printf(bio, "unspecified");
228     else {
229         STACK_OF(CONF_VALUE) *nval;
230         if ((nval = i2v_GENERAL_NAME(NULL, a->tsa, NULL)))
231             X509V3_EXT_val_prn(bio, nval, 0, 0);
232         sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
233     }
234     BIO_write(bio, "\n", 1);
235
236     /* Print extensions. */
237     TS_ext_print_bio(bio, a->extensions);
238
239     return 1;
240 }
241
242 static int ts_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *a)
243 {
244     if (a->seconds != NULL)
245         TS_ASN1_INTEGER_print_bio(bio, a->seconds);
246     else
247         BIO_printf(bio, "unspecified");
248     BIO_printf(bio, " seconds, ");
249     if (a->millis != NULL)
250         TS_ASN1_INTEGER_print_bio(bio, a->millis);
251     else
252         BIO_printf(bio, "unspecified");
253     BIO_printf(bio, " millis, ");
254     if (a->micros != NULL)
255         TS_ASN1_INTEGER_print_bio(bio, a->micros);
256     else
257         BIO_printf(bio, "unspecified");
258     BIO_printf(bio, " micros");
259
260     return 1;
261 }