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