xref: /openbsd-src/lib/libcrypto/ts/ts_lib.c (revision 6d253f95424ee0054c798f493d12377911cd3668)
1*6d253f95Stb /* $OpenBSD: ts_lib.c,v 1.15 2025/01/07 14:22:19 tb Exp $ */
2f1535dc8Sdjm /* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3f1535dc8Sdjm  * project 2002.
4f1535dc8Sdjm  */
5f1535dc8Sdjm /* ====================================================================
6f1535dc8Sdjm  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7f1535dc8Sdjm  *
8f1535dc8Sdjm  * Redistribution and use in source and binary forms, with or without
9f1535dc8Sdjm  * modification, are permitted provided that the following conditions
10f1535dc8Sdjm  * are met:
11f1535dc8Sdjm  *
12f1535dc8Sdjm  * 1. Redistributions of source code must retain the above copyright
13f1535dc8Sdjm  *    notice, this list of conditions and the following disclaimer.
14f1535dc8Sdjm  *
15f1535dc8Sdjm  * 2. Redistributions in binary form must reproduce the above copyright
16f1535dc8Sdjm  *    notice, this list of conditions and the following disclaimer in
17f1535dc8Sdjm  *    the documentation and/or other materials provided with the
18f1535dc8Sdjm  *    distribution.
19f1535dc8Sdjm  *
20f1535dc8Sdjm  * 3. All advertising materials mentioning features or use of this
21f1535dc8Sdjm  *    software must display the following acknowledgment:
22f1535dc8Sdjm  *    "This product includes software developed by the OpenSSL Project
23f1535dc8Sdjm  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24f1535dc8Sdjm  *
25f1535dc8Sdjm  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26f1535dc8Sdjm  *    endorse or promote products derived from this software without
27f1535dc8Sdjm  *    prior written permission. For written permission, please contact
28f1535dc8Sdjm  *    licensing@OpenSSL.org.
29f1535dc8Sdjm  *
30f1535dc8Sdjm  * 5. Products derived from this software may not be called "OpenSSL"
31f1535dc8Sdjm  *    nor may "OpenSSL" appear in their names without prior written
32f1535dc8Sdjm  *    permission of the OpenSSL Project.
33f1535dc8Sdjm  *
34f1535dc8Sdjm  * 6. Redistributions of any form whatsoever must retain the following
35f1535dc8Sdjm  *    acknowledgment:
36f1535dc8Sdjm  *    "This product includes software developed by the OpenSSL Project
37f1535dc8Sdjm  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38f1535dc8Sdjm  *
39f1535dc8Sdjm  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40f1535dc8Sdjm  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41f1535dc8Sdjm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42f1535dc8Sdjm  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43f1535dc8Sdjm  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44f1535dc8Sdjm  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45f1535dc8Sdjm  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46f1535dc8Sdjm  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47f1535dc8Sdjm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48f1535dc8Sdjm  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49f1535dc8Sdjm  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50f1535dc8Sdjm  * OF THE POSSIBILITY OF SUCH DAMAGE.
51f1535dc8Sdjm  * ====================================================================
52f1535dc8Sdjm  *
53f1535dc8Sdjm  * This product includes cryptographic software written by Eric Young
54f1535dc8Sdjm  * (eay@cryptsoft.com).  This product includes software written by Tim
55f1535dc8Sdjm  * Hudson (tjh@cryptsoft.com).
56f1535dc8Sdjm  *
57f1535dc8Sdjm  */
58f1535dc8Sdjm 
59f1535dc8Sdjm #include <stdio.h>
60a8913c44Sjsing #include <string.h>
61a8913c44Sjsing 
62f1535dc8Sdjm #include <openssl/bn.h>
63b6ab114eSjsing #include <openssl/objects.h>
646d6bb189Sderaadt #include <openssl/ts.h>
65b6ab114eSjsing #include <openssl/x509v3.h>
66f1535dc8Sdjm 
67c9675a23Stb #include "bn_local.h"
68c9675a23Stb #include "x509_local.h"
69838f0b6dStb 
70f1535dc8Sdjm /* Local function declarations. */
71f1535dc8Sdjm 
72f1535dc8Sdjm /* Function definitions. */
73f1535dc8Sdjm 
74ec7cdc12Sjsing int
75ec7cdc12Sjsing TS_ASN1_INTEGER_print_bio(BIO *bio, const ASN1_INTEGER *num)
76f1535dc8Sdjm {
77*6d253f95Stb 	BIGNUM *bn = NULL;
78*6d253f95Stb 	char *hex = NULL;
79*6d253f95Stb 	int ret = 0;
80f1535dc8Sdjm 
81*6d253f95Stb 	/* XXX - OpenSSL decided to return -1 here for some stupid reason. */
82*6d253f95Stb 	if ((bn = ASN1_INTEGER_to_BN(num, NULL)) == NULL)
83*6d253f95Stb 		goto err;
84*6d253f95Stb 	if ((hex = BN_bn2hex(bn)) == NULL)
85*6d253f95Stb 		goto err;
86*6d253f95Stb 	if (BIO_printf(bio, "0x%s", hex) <= 0)
87*6d253f95Stb 		goto err;
88*6d253f95Stb 
89*6d253f95Stb 	ret = 1;
90*6d253f95Stb 
91*6d253f95Stb  err:
92*6d253f95Stb 	BN_free(bn);
936f3a6cb1Sbeck 	free(hex);
94f1535dc8Sdjm 
95*6d253f95Stb 	return ret;
96f1535dc8Sdjm }
97bb933e2fSbeck LCRYPTO_ALIAS(TS_ASN1_INTEGER_print_bio);
98f1535dc8Sdjm 
99ec7cdc12Sjsing int
100ec7cdc12Sjsing TS_OBJ_print_bio(BIO *bio, const ASN1_OBJECT *obj)
101f1535dc8Sdjm {
102f1535dc8Sdjm 	char obj_txt[128];
103f1535dc8Sdjm 
104f1535dc8Sdjm 	int len = OBJ_obj2txt(obj_txt, sizeof(obj_txt), obj, 0);
10515586887Sbeck 	if (len >= sizeof(obj_txt))
10615586887Sbeck 		len = sizeof(obj_txt) - 1;
107f1535dc8Sdjm 	BIO_write(bio, obj_txt, len);
108f1535dc8Sdjm 	BIO_write(bio, "\n", 1);
109f1535dc8Sdjm 	return 1;
110f1535dc8Sdjm }
111bb933e2fSbeck LCRYPTO_ALIAS(TS_OBJ_print_bio);
112f1535dc8Sdjm 
113ec7cdc12Sjsing int
114ec7cdc12Sjsing TS_ext_print_bio(BIO *bio, const STACK_OF(X509_EXTENSION) *extensions)
115f1535dc8Sdjm {
116f1535dc8Sdjm 	int i, critical, n;
117f1535dc8Sdjm 	X509_EXTENSION *ex;
118f1535dc8Sdjm 	ASN1_OBJECT *obj;
119f1535dc8Sdjm 
120f1535dc8Sdjm 	BIO_printf(bio, "Extensions:\n");
121f1535dc8Sdjm 	n = X509v3_get_ext_count(extensions);
122ec7cdc12Sjsing 	for (i = 0; i < n; i++) {
123f1535dc8Sdjm 		ex = X509v3_get_ext(extensions, i);
124f1535dc8Sdjm 		obj = X509_EXTENSION_get_object(ex);
125f1535dc8Sdjm 		i2a_ASN1_OBJECT(bio, obj);
126f1535dc8Sdjm 		critical = X509_EXTENSION_get_critical(ex);
127f1535dc8Sdjm 		BIO_printf(bio, ": %s\n", critical ? "critical" : "");
128ec7cdc12Sjsing 		if (!X509V3_EXT_print(bio, ex, 0, 4)) {
129f1535dc8Sdjm 			BIO_printf(bio, "%4s", "");
1303e1a72eaSjsing 			ASN1_STRING_print(bio, ex->value);
131f1535dc8Sdjm 		}
132f1535dc8Sdjm 		BIO_write(bio, "\n", 1);
133f1535dc8Sdjm 	}
134f1535dc8Sdjm 
135f1535dc8Sdjm 	return 1;
136f1535dc8Sdjm }
137bb933e2fSbeck LCRYPTO_ALIAS(TS_ext_print_bio);
138f1535dc8Sdjm 
139ec7cdc12Sjsing int
140ec7cdc12Sjsing TS_X509_ALGOR_print_bio(BIO *bio, const X509_ALGOR *alg)
141f1535dc8Sdjm {
142f1535dc8Sdjm 	int i = OBJ_obj2nid(alg->algorithm);
143ec7cdc12Sjsing 
144f1535dc8Sdjm 	return BIO_printf(bio, "Hash Algorithm: %s\n",
145f1535dc8Sdjm 	    (i == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(i));
146f1535dc8Sdjm }
147bb933e2fSbeck LCRYPTO_ALIAS(TS_X509_ALGOR_print_bio);
148f1535dc8Sdjm 
149ec7cdc12Sjsing int
150ec7cdc12Sjsing TS_MSG_IMPRINT_print_bio(BIO *bio, TS_MSG_IMPRINT *a)
151f1535dc8Sdjm {
1528035488aSjsing 	ASN1_OCTET_STRING *msg;
153f1535dc8Sdjm 
154f1535dc8Sdjm 	TS_X509_ALGOR_print_bio(bio, TS_MSG_IMPRINT_get_algo(a));
155f1535dc8Sdjm 
156f1535dc8Sdjm 	BIO_printf(bio, "Message data:\n");
157f1535dc8Sdjm 	msg = TS_MSG_IMPRINT_get_msg(a);
1588035488aSjsing 	BIO_dump_indent(bio, (const char *)ASN1_STRING_data(msg),
1598035488aSjsing 	    ASN1_STRING_length(msg), 4);
160f1535dc8Sdjm 
161f1535dc8Sdjm 	return 1;
162f1535dc8Sdjm }
163bb933e2fSbeck LCRYPTO_ALIAS(TS_MSG_IMPRINT_print_bio);
164