Fix length checks in X509_cmp_time to avoid out-of-bounds reads.
[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
67 struct status_map_st {
68     int bit;
69     const char *text;
70 };
71
72 /* Local function declarations. */
73
74 static int ts_status_map_print(BIO *bio, const struct status_map_st *a,
75                                const ASN1_BIT_STRING *v);
76 static int ts_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *accuracy);
77
78 /* Function definitions. */
79
80 int TS_RESP_print_bio(BIO *bio, TS_RESP *a)
81 {
82     TS_TST_INFO *tst_info;
83
84     BIO_printf(bio, "Status info:\n");
85     TS_STATUS_INFO_print_bio(bio, TS_RESP_get_status_info(a));
86
87     BIO_printf(bio, "\nTST info:\n");
88     tst_info = TS_RESP_get_tst_info(a);
89     if (tst_info != NULL)
90         TS_TST_INFO_print_bio(bio, TS_RESP_get_tst_info(a));
91     else
92         BIO_printf(bio, "Not included.\n");
93
94     return 1;
95 }
96
97 int TS_STATUS_INFO_print_bio(BIO *bio, TS_STATUS_INFO *a)
98 {
99     static const char *status_map[] = {
100         "Granted.",
101         "Granted with modifications.",
102         "Rejected.",
103         "Waiting.",
104         "Revocation warning.",
105         "Revoked."
106     };
107     static const struct status_map_st failure_map[] = {
108         {TS_INFO_BAD_ALG,
109          "unrecognized or unsupported algorithm identifier"},
110         {TS_INFO_BAD_REQUEST,
111          "transaction not permitted or supported"},
112         {TS_INFO_BAD_DATA_FORMAT,
113          "the data submitted has the wrong format"},
114         {TS_INFO_TIME_NOT_AVAILABLE,
115          "the TSA's time source is not available"},
116         {TS_INFO_UNACCEPTED_POLICY,
117          "the requested TSA policy is not supported by the TSA"},
118         {TS_INFO_UNACCEPTED_EXTENSION,
119          "the requested extension is not supported by the TSA"},
120         {TS_INFO_ADD_INFO_NOT_AVAILABLE,
121          "the additional information requested could not be understood "
122          "or is not available"},
123         {TS_INFO_SYSTEM_FAILURE,
124          "the request cannot be handled due to system failure"},
125         {-1, NULL}
126     };
127     long status;
128     int i, lines = 0;
129
130     /* Printing status code. */
131     BIO_printf(bio, "Status: ");
132     status = ASN1_INTEGER_get(a->status);
133     if (0 <= status && status < (long)OSSL_NELEM(status_map))
134         BIO_printf(bio, "%s\n", status_map[status]);
135     else
136         BIO_printf(bio, "out of bounds\n");
137
138     /* Printing status description. */
139     BIO_printf(bio, "Status description: ");
140     for (i = 0; i < sk_ASN1_UTF8STRING_num(a->text); ++i) {
141         if (i > 0)
142             BIO_puts(bio, "\t");
143         ASN1_STRING_print_ex(bio, sk_ASN1_UTF8STRING_value(a->text, i), 0);
144         BIO_puts(bio, "\n");
145     }
146     if (i == 0)
147         BIO_printf(bio, "unspecified\n");
148
149     /* Printing failure information. */
150     BIO_printf(bio, "Failure info: ");
151     if (a->failure_info != NULL)
152         lines = ts_status_map_print(bio, failure_map, a->failure_info);
153     if (lines == 0)
154         BIO_printf(bio, "unspecified");
155     BIO_printf(bio, "\n");
156
157     return 1;
158 }
159
160 static int ts_status_map_print(BIO *bio, const struct status_map_st *a,
161                                const ASN1_BIT_STRING *v)
162 {
163     int lines = 0;
164
165     for (; a->bit >= 0; ++a) {
166         if (ASN1_BIT_STRING_get_bit(v, a->bit)) {
167             if (++lines > 1)
168                 BIO_printf(bio, ", ");
169             BIO_printf(bio, "%s", a->text);
170         }
171     }
172
173     return lines;
174 }
175
176 int TS_TST_INFO_print_bio(BIO *bio, TS_TST_INFO *a)
177 {
178     int v;
179     ASN1_OBJECT *policy_id;
180     const ASN1_INTEGER *serial;
181     const ASN1_GENERALIZEDTIME *gtime;
182     TS_ACCURACY *accuracy;
183     const ASN1_INTEGER *nonce;
184     GENERAL_NAME *tsa_name;
185
186     if (a == NULL)
187         return 0;
188
189     /* Print version. */
190     v = TS_TST_INFO_get_version(a);
191     BIO_printf(bio, "Version: %d\n", v);
192
193     /* Print policy id. */
194     BIO_printf(bio, "Policy OID: ");
195     policy_id = TS_TST_INFO_get_policy_id(a);
196     TS_OBJ_print_bio(bio, policy_id);
197
198     /* Print message imprint. */
199     TS_MSG_IMPRINT_print_bio(bio, TS_TST_INFO_get_msg_imprint(a));
200
201     /* Print serial number. */
202     BIO_printf(bio, "Serial number: ");
203     serial = TS_TST_INFO_get_serial(a);
204     if (serial == NULL)
205         BIO_printf(bio, "unspecified");
206     else
207         TS_ASN1_INTEGER_print_bio(bio, serial);
208     BIO_write(bio, "\n", 1);
209
210     /* Print time stamp. */
211     BIO_printf(bio, "Time stamp: ");
212     gtime = TS_TST_INFO_get_time(a);
213     ASN1_GENERALIZEDTIME_print(bio, gtime);
214     BIO_write(bio, "\n", 1);
215
216     /* Print accuracy. */
217     BIO_printf(bio, "Accuracy: ");
218     accuracy = TS_TST_INFO_get_accuracy(a);
219     if (accuracy == NULL)
220         BIO_printf(bio, "unspecified");
221     else
222         ts_ACCURACY_print_bio(bio, accuracy);
223     BIO_write(bio, "\n", 1);
224
225     /* Print ordering. */
226     BIO_printf(bio, "Ordering: %s\n",
227                TS_TST_INFO_get_ordering(a) ? "yes" : "no");
228
229     /* Print nonce. */
230     BIO_printf(bio, "Nonce: ");
231     nonce = TS_TST_INFO_get_nonce(a);
232     if (nonce == NULL)
233         BIO_printf(bio, "unspecified");
234     else
235         TS_ASN1_INTEGER_print_bio(bio, nonce);
236     BIO_write(bio, "\n", 1);
237
238     /* Print TSA name. */
239     BIO_printf(bio, "TSA: ");
240     tsa_name = TS_TST_INFO_get_tsa(a);
241     if (tsa_name == NULL)
242         BIO_printf(bio, "unspecified");
243     else {
244         STACK_OF(CONF_VALUE) *nval;
245         if ((nval = i2v_GENERAL_NAME(NULL, tsa_name, NULL)))
246             X509V3_EXT_val_prn(bio, nval, 0, 0);
247         sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
248     }
249     BIO_write(bio, "\n", 1);
250
251     /* Print extensions. */
252     TS_ext_print_bio(bio, TS_TST_INFO_get_exts(a));
253
254     return 1;
255 }
256
257 static int ts_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *accuracy)
258 {
259     const ASN1_INTEGER *seconds = TS_ACCURACY_get_seconds(accuracy);
260     const ASN1_INTEGER *millis = TS_ACCURACY_get_millis(accuracy);
261     const ASN1_INTEGER *micros = TS_ACCURACY_get_micros(accuracy);
262
263     if (seconds != NULL)
264         TS_ASN1_INTEGER_print_bio(bio, seconds);
265     else
266         BIO_printf(bio, "unspecified");
267     BIO_printf(bio, " seconds, ");
268     if (millis != NULL)
269         TS_ASN1_INTEGER_print_bio(bio, millis);
270     else
271         BIO_printf(bio, "unspecified");
272     BIO_printf(bio, " millis, ");
273     if (micros != NULL)
274         TS_ASN1_INTEGER_print_bio(bio, micros);
275     else
276         BIO_printf(bio, "unspecified");
277     BIO_printf(bio, " micros");
278
279     return 1;
280 }