GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/ocsp/ocsp_prn.c Lines: 89 110 80.9 %
Date: 2017-11-07 Branches: 46 97 47.4 %

Line Branch Exec Source
1
/* $OpenBSD: ocsp_prn.c,v 1.8 2015/07/16 02:16:19 miod Exp $ */
2
/* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
3
 * project. */
4
5
/* History:
6
   This file was originally part of ocsp.c and was transfered to Richard
7
   Levitte from CertCo by Kathy Weinhold in mid-spring 2000 to be included
8
   in OpenSSL or released as a patch kit. */
9
10
/* ====================================================================
11
 * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions
15
 * are met:
16
 *
17
 * 1. Redistributions of source code must retain the above copyright
18
 *    notice, this list of conditions and the following disclaimer.
19
 *
20
 * 2. Redistributions in binary form must reproduce the above copyright
21
 *    notice, this list of conditions and the following disclaimer in
22
 *    the documentation and/or other materials provided with the
23
 *    distribution.
24
 *
25
 * 3. All advertising materials mentioning features or use of this
26
 *    software must display the following acknowledgment:
27
 *    "This product includes software developed by the OpenSSL Project
28
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
29
 *
30
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
31
 *    endorse or promote products derived from this software without
32
 *    prior written permission. For written permission, please contact
33
 *    openssl-core@openssl.org.
34
 *
35
 * 5. Products derived from this software may not be called "OpenSSL"
36
 *    nor may "OpenSSL" appear in their names without prior written
37
 *    permission of the OpenSSL Project.
38
 *
39
 * 6. Redistributions of any form whatsoever must retain the following
40
 *    acknowledgment:
41
 *    "This product includes software developed by the OpenSSL Project
42
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
43
 *
44
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
45
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
48
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55
 * OF THE POSSIBILITY OF SUCH DAMAGE.
56
 * ====================================================================
57
 *
58
 * This product includes cryptographic software written by Eric Young
59
 * (eay@cryptsoft.com).  This product includes software written by Tim
60
 * Hudson (tjh@cryptsoft.com).
61
 *
62
 */
63
64
#include <openssl/bio.h>
65
#include <openssl/err.h>
66
#include <openssl/ocsp.h>
67
#include <openssl/pem.h>
68
69
static int
70
ocsp_certid_print(BIO *bp, OCSP_CERTID* a, int indent)
71
{
72
24
	BIO_printf(bp, "%*sCertificate ID:\n", indent, "");
73
12
	indent += 2;
74
12
	BIO_printf(bp, "%*sHash Algorithm: ", indent, "");
75
12
	i2a_ASN1_OBJECT(bp, a->hashAlgorithm->algorithm);
76
12
	BIO_printf(bp, "\n%*sIssuer Name Hash: ", indent, "");
77
12
	i2a_ASN1_STRING(bp, a->issuerNameHash, V_ASN1_OCTET_STRING);
78
12
	BIO_printf(bp, "\n%*sIssuer Key Hash: ", indent, "");
79
12
	i2a_ASN1_STRING(bp, a->issuerKeyHash, V_ASN1_OCTET_STRING);
80
12
	BIO_printf(bp, "\n%*sSerial Number: ", indent, "");
81
12
	i2a_ASN1_INTEGER(bp, a->serialNumber);
82
12
	BIO_printf(bp, "\n");
83
12
	return 1;
84
}
85
86
typedef struct {
87
	long t;
88
	const char *m;
89
} OCSP_TBLSTR;
90
91
static const char *
92
table2string(long s, const OCSP_TBLSTR *ts, int len)
93
{
94
	const OCSP_TBLSTR *p;
95
96
60
	for (p = ts; p < ts + len; p++)
97
22
		if (p->t == s)
98
16
			return p->m;
99
	return "(UNKNOWN)";
100
16
}
101
102
const char *
103
OCSP_response_status_str(long s)
104
{
105
	static const OCSP_TBLSTR rstat_tbl[] = {
106
		{ OCSP_RESPONSE_STATUS_SUCCESSFUL, "successful" },
107
		{ OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, "malformedrequest" },
108
		{ OCSP_RESPONSE_STATUS_INTERNALERROR, "internalerror" },
109
		{ OCSP_RESPONSE_STATUS_TRYLATER, "trylater" },
110
		{ OCSP_RESPONSE_STATUS_SIGREQUIRED, "sigrequired" },
111
		{ OCSP_RESPONSE_STATUS_UNAUTHORIZED, "unauthorized" }
112
	};
113
8
	return table2string(s, rstat_tbl, 6);
114
}
115
116
const char *
117
OCSP_cert_status_str(long s)
118
{
119
	static const OCSP_TBLSTR cstat_tbl[] = {
120
		{ V_OCSP_CERTSTATUS_GOOD, "good" },
121
		{ V_OCSP_CERTSTATUS_REVOKED, "revoked" },
122
		{ V_OCSP_CERTSTATUS_UNKNOWN, "unknown" }
123
	};
124
24
	return table2string(s, cstat_tbl, 3);
125
}
126
127
const char *
128
OCSP_crl_reason_str(long s)
129
{
130
	static const OCSP_TBLSTR reason_tbl[] = {
131
		{ OCSP_REVOKED_STATUS_UNSPECIFIED, "unspecified" },
132
		{ OCSP_REVOKED_STATUS_KEYCOMPROMISE, "keyCompromise" },
133
		{ OCSP_REVOKED_STATUS_CACOMPROMISE, "cACompromise" },
134
		{ OCSP_REVOKED_STATUS_AFFILIATIONCHANGED, "affiliationChanged" },
135
		{ OCSP_REVOKED_STATUS_SUPERSEDED, "superseded" },
136
		{ OCSP_REVOKED_STATUS_CESSATIONOFOPERATION, "cessationOfOperation" },
137
		{ OCSP_REVOKED_STATUS_CERTIFICATEHOLD, "certificateHold" },
138
		{ OCSP_REVOKED_STATUS_REMOVEFROMCRL, "removeFromCRL" }
139
	};
140
	return table2string(s, reason_tbl, 8);
141
}
142
143
int
144
OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST* o, unsigned long flags)
145
{
146
	int i;
147
	long l;
148
	OCSP_CERTID* cid = NULL;
149
	OCSP_ONEREQ *one = NULL;
150
4
	OCSP_REQINFO *inf = o->tbsRequest;
151
2
	OCSP_SIGNATURE *sig = o->optionalSignature;
152
153
2
	if (BIO_write(bp, "OCSP Request Data:\n", 19) <= 0)
154
		goto err;
155
2
	l = ASN1_INTEGER_get(inf->version);
156
2
	if (BIO_printf(bp, "    Version: %lu (0x%lx)", l+1, l) <= 0)
157
		goto err;
158
2
	if (inf->requestorName != NULL) {
159
		if (BIO_write(bp, "\n    Requestor Name: ", 21) <= 0)
160
			goto err;
161
		GENERAL_NAME_print(bp, inf->requestorName);
162
	}
163
2
	if (BIO_write(bp, "\n    Requestor List:\n", 21) <= 0)
164
		goto err;
165
12
	for (i = 0; i < sk_OCSP_ONEREQ_num(inf->requestList); i++) {
166
4
		one = sk_OCSP_ONEREQ_value(inf->requestList, i);
167
4
		cid = one->reqCert;
168
4
		ocsp_certid_print(bp, cid, 8);
169
4
		if (!X509V3_extensions_print(bp, "Request Single Extensions",
170
4
		    one->singleRequestExtensions, flags, 8))
171
			goto err;
172
	}
173
2
	if (!X509V3_extensions_print(bp, "Request Extensions",
174
2
	    inf->requestExtensions, flags, 4))
175
		goto err;
176
2
	if (sig) {
177
		if (X509_signature_print(bp, sig->signatureAlgorithm,
178
		    sig->signature) == 0)
179
			goto err;
180
		for (i = 0; i < sk_X509_num(sig->certs); i++) {
181
			if (X509_print(bp, sk_X509_value(sig->certs, i)) == 0)
182
				goto err;
183
			if (PEM_write_bio_X509(bp,
184
			    sk_X509_value(sig->certs, i)) == 0)
185
				goto err;
186
		}
187
	}
188
2
	return 1;
189
190
err:
191
	return 0;
192
2
}
193
194
int
195
OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE* o, unsigned long flags)
196
{
197
	int i, ret = 0;
198
	long l;
199
	OCSP_CERTID *cid = NULL;
200
	OCSP_BASICRESP *br = NULL;
201
	OCSP_RESPID *rid = NULL;
202
	OCSP_RESPDATA  *rd = NULL;
203
	OCSP_CERTSTATUS *cst = NULL;
204
	OCSP_REVOKEDINFO *rev = NULL;
205
	OCSP_SINGLERESP *single = NULL;
206
8
	OCSP_RESPBYTES *rb = o->responseBytes;
207
208
4
	if (BIO_puts(bp, "OCSP Response Data:\n") <= 0)
209
		goto err;
210
4
	l = ASN1_ENUMERATED_get(o->responseStatus);
211
8
	if (BIO_printf(bp, "    OCSP Response Status: %s (0x%lx)\n",
212
8
	    OCSP_response_status_str(l), l) <= 0)
213
		goto err;
214
4
	if (rb == NULL)
215
		return 1;
216
4
	if (BIO_puts(bp, "    Response Type: ") <= 0)
217
		goto err;
218
4
	if (i2a_ASN1_OBJECT(bp, rb->responseType) <= 0)
219
		goto err;
220
4
	if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
221
		BIO_puts(bp, " (unknown response type)\n");
222
		return 1;
223
	}
224
225
4
	i = ASN1_STRING_length(rb->response);
226
4
	if (!(br = OCSP_response_get1_basic(o)))
227
		goto err;
228
4
	rd = br->tbsResponseData;
229
4
	l = ASN1_INTEGER_get(rd->version);
230
4
	if (BIO_printf(bp, "\n    Version: %lu (0x%lx)\n", l+1, l) <= 0)
231
		goto err;
232
4
	if (BIO_puts(bp, "    Responder Id: ") <= 0)
233
		goto err;
234
235
8
	rid = rd->responderId;
236
8
	switch (rid->type) {
237
	case V_OCSP_RESPID_NAME:
238
4
		X509_NAME_print_ex(bp, rid->value.byName, 0, XN_FLAG_ONELINE);
239
4
		break;
240
	case V_OCSP_RESPID_KEY:
241
		i2a_ASN1_STRING(bp, rid->value.byKey, V_ASN1_OCTET_STRING);
242
		break;
243
	}
244
245
4
	if (BIO_printf(bp, "\n    Produced At: ")<=0)
246
		goto err;
247
4
	if (!ASN1_GENERALIZEDTIME_print(bp, rd->producedAt))
248
		goto err;
249
4
	if (BIO_printf(bp, "\n    Responses:\n") <= 0)
250
		goto err;
251
24
	for (i = 0; i < sk_OCSP_SINGLERESP_num(rd->responses); i++) {
252
8
		if (! sk_OCSP_SINGLERESP_value(rd->responses, i))
253
			continue;
254
8
		single = sk_OCSP_SINGLERESP_value(rd->responses, i);
255
8
		cid = single->certId;
256
8
		if (ocsp_certid_print(bp, cid, 4) <= 0)
257
			goto err;
258
8
		cst = single->certStatus;
259
16
		if (BIO_printf(bp, "    Cert Status: %s",
260
16
		    OCSP_cert_status_str(cst->type)) <= 0)
261
			goto err;
262
8
		if (cst->type == V_OCSP_CERTSTATUS_REVOKED) {
263
4
			rev = cst->value.revoked;
264
4
			if (BIO_printf(bp, "\n    Revocation Time: ") <= 0)
265
				goto err;
266
4
			if (!ASN1_GENERALIZEDTIME_print(bp,
267
4
			    rev->revocationTime))
268
				goto err;
269
4
			if (rev->revocationReason) {
270
				l = ASN1_ENUMERATED_get(rev->revocationReason);
271
				if (BIO_printf(bp,
272
				    "\n    Revocation Reason: %s (0x%lx)",
273
				    OCSP_crl_reason_str(l), l) <= 0)
274
					goto err;
275
			}
276
		}
277
8
		if (BIO_printf(bp, "\n    This Update: ") <= 0)
278
			goto err;
279
8
		if (!ASN1_GENERALIZEDTIME_print(bp, single->thisUpdate))
280
			goto err;
281
8
		if (single->nextUpdate) {
282
			if (BIO_printf(bp, "\n    Next Update: ") <= 0)
283
				goto err;
284
			if (!ASN1_GENERALIZEDTIME_print(bp, single->nextUpdate))
285
				goto err;
286
		}
287
8
		if (BIO_write(bp, "\n", 1) <= 0)
288
			goto err;
289
8
		if (!X509V3_extensions_print(bp, "Response Single Extensions",
290
8
		    single->singleExtensions, flags, 8))
291
			goto err;
292
8
		if (BIO_write(bp, "\n", 1) <= 0)
293
			goto err;
294
	}
295
4
	if (!X509V3_extensions_print(bp, "Response Extensions",
296
4
	    rd->responseExtensions, flags, 4))
297
		goto err;
298
4
	if (X509_signature_print(bp, br->signatureAlgorithm, br->signature) <=
299
	    0)
300
		goto err;
301
302
16
	for (i = 0; i < sk_X509_num(br->certs); i++) {
303
4
		X509_print(bp, sk_X509_value(br->certs, i));
304
4
		PEM_write_bio_X509(bp, sk_X509_value(br->certs, i));
305
	}
306
307
4
	ret = 1;
308
309
err:
310
4
	OCSP_BASICRESP_free(br);
311
4
	return ret;
312
4
}