Fix safestack issues in asn1.h
[openssl.git] / crypto / ts / ts_rsp_print.c
1 /*
2  * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/objects.h>
13 #include <openssl/bn.h>
14 #include <openssl/x509v3.h>
15 #include <openssl/ts.h>
16 #include "ts_local.h"
17
18 DEFINE_STACK_OF(CONF_VALUE)
19
20 struct status_map_st {
21     int bit;
22     const char *text;
23 };
24
25 static int ts_status_map_print(BIO *bio, const struct status_map_st *a,
26                                const ASN1_BIT_STRING *v);
27 static int ts_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *accuracy);
28
29
30 int TS_RESP_print_bio(BIO *bio, TS_RESP *a)
31 {
32     BIO_printf(bio, "Status info:\n");
33     TS_STATUS_INFO_print_bio(bio, a->status_info);
34
35     BIO_printf(bio, "\nTST info:\n");
36     if (a->tst_info != NULL)
37         TS_TST_INFO_print_bio(bio, a->tst_info);
38     else
39         BIO_printf(bio, "Not included.\n");
40
41     return 1;
42 }
43
44 int TS_STATUS_INFO_print_bio(BIO *bio, TS_STATUS_INFO *a)
45 {
46     static const char *status_map[] = {
47         "Granted.",
48         "Granted with modifications.",
49         "Rejected.",
50         "Waiting.",
51         "Revocation warning.",
52         "Revoked."
53     };
54     static const struct status_map_st failure_map[] = {
55         {TS_INFO_BAD_ALG,
56          "unrecognized or unsupported algorithm identifier"},
57         {TS_INFO_BAD_REQUEST,
58          "transaction not permitted or supported"},
59         {TS_INFO_BAD_DATA_FORMAT,
60          "the data submitted has the wrong format"},
61         {TS_INFO_TIME_NOT_AVAILABLE,
62          "the TSA's time source is not available"},
63         {TS_INFO_UNACCEPTED_POLICY,
64          "the requested TSA policy is not supported by the TSA"},
65         {TS_INFO_UNACCEPTED_EXTENSION,
66          "the requested extension is not supported by the TSA"},
67         {TS_INFO_ADD_INFO_NOT_AVAILABLE,
68          "the additional information requested could not be understood "
69          "or is not available"},
70         {TS_INFO_SYSTEM_FAILURE,
71          "the request cannot be handled due to system failure"},
72         {-1, NULL}
73     };
74     long status;
75     int i, lines = 0;
76
77     BIO_printf(bio, "Status: ");
78     status = ASN1_INTEGER_get(a->status);
79     if (0 <= status && status < (long)OSSL_NELEM(status_map))
80         BIO_printf(bio, "%s\n", status_map[status]);
81     else
82         BIO_printf(bio, "out of bounds\n");
83
84     BIO_printf(bio, "Status description: ");
85     for (i = 0; i < sk_ASN1_UTF8STRING_num(a->text); ++i) {
86         if (i > 0)
87             BIO_puts(bio, "\t");
88         ASN1_STRING_print_ex(bio, sk_ASN1_UTF8STRING_value(a->text, i), 0);
89         BIO_puts(bio, "\n");
90     }
91     if (i == 0)
92         BIO_printf(bio, "unspecified\n");
93
94     BIO_printf(bio, "Failure info: ");
95     if (a->failure_info != NULL)
96         lines = ts_status_map_print(bio, failure_map, a->failure_info);
97     if (lines == 0)
98         BIO_printf(bio, "unspecified");
99     BIO_printf(bio, "\n");
100
101     return 1;
102 }
103
104 static int ts_status_map_print(BIO *bio, const struct status_map_st *a,
105                                const ASN1_BIT_STRING *v)
106 {
107     int lines = 0;
108
109     for (; a->bit >= 0; ++a) {
110         if (ASN1_BIT_STRING_get_bit(v, a->bit)) {
111             if (++lines > 1)
112                 BIO_printf(bio, ", ");
113             BIO_printf(bio, "%s", a->text);
114         }
115     }
116
117     return lines;
118 }
119
120 int TS_TST_INFO_print_bio(BIO *bio, TS_TST_INFO *a)
121 {
122     int v;
123
124     if (a == NULL)
125         return 0;
126
127     v = ASN1_INTEGER_get(a->version);
128     BIO_printf(bio, "Version: %d\n", v);
129
130     BIO_printf(bio, "Policy OID: ");
131     TS_OBJ_print_bio(bio, a->policy_id);
132
133     TS_MSG_IMPRINT_print_bio(bio, a->msg_imprint);
134
135     BIO_printf(bio, "Serial number: ");
136     if (a->serial == NULL)
137         BIO_printf(bio, "unspecified");
138     else
139         TS_ASN1_INTEGER_print_bio(bio, a->serial);
140     BIO_write(bio, "\n", 1);
141
142     BIO_printf(bio, "Time stamp: ");
143     ASN1_GENERALIZEDTIME_print(bio, a->time);
144     BIO_write(bio, "\n", 1);
145
146     BIO_printf(bio, "Accuracy: ");
147     if (a->accuracy == NULL)
148         BIO_printf(bio, "unspecified");
149     else
150         ts_ACCURACY_print_bio(bio, a->accuracy);
151     BIO_write(bio, "\n", 1);
152
153     BIO_printf(bio, "Ordering: %s\n", a->ordering ? "yes" : "no");
154
155     BIO_printf(bio, "Nonce: ");
156     if (a->nonce == NULL)
157         BIO_printf(bio, "unspecified");
158     else
159         TS_ASN1_INTEGER_print_bio(bio, a->nonce);
160     BIO_write(bio, "\n", 1);
161
162     BIO_printf(bio, "TSA: ");
163     if (a->tsa == NULL)
164         BIO_printf(bio, "unspecified");
165     else {
166         STACK_OF(CONF_VALUE) *nval;
167         if ((nval = i2v_GENERAL_NAME(NULL, a->tsa, NULL)))
168             X509V3_EXT_val_prn(bio, nval, 0, 0);
169         sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
170     }
171     BIO_write(bio, "\n", 1);
172
173     TS_ext_print_bio(bio, a->extensions);
174
175     return 1;
176 }
177
178 static int ts_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *a)
179 {
180     if (a->seconds != NULL)
181         TS_ASN1_INTEGER_print_bio(bio, a->seconds);
182     else
183         BIO_printf(bio, "unspecified");
184     BIO_printf(bio, " seconds, ");
185     if (a->millis != NULL)
186         TS_ASN1_INTEGER_print_bio(bio, a->millis);
187     else
188         BIO_printf(bio, "unspecified");
189     BIO_printf(bio, " millis, ");
190     if (a->micros != NULL)
191         TS_ASN1_INTEGER_print_bio(bio, a->micros);
192     else
193         BIO_printf(bio, "unspecified");
194     BIO_printf(bio, " micros");
195
196     return 1;
197 }