1238384Sjkim/* crypto/ts/ts_lib.c */
2280304Sjkim/*
3280304Sjkim * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
4280304Sjkim * 2002.
5238384Sjkim */
6238384Sjkim/* ====================================================================
7238384Sjkim * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
8238384Sjkim *
9238384Sjkim * Redistribution and use in source and binary forms, with or without
10238384Sjkim * modification, are permitted provided that the following conditions
11238384Sjkim * are met:
12238384Sjkim *
13238384Sjkim * 1. Redistributions of source code must retain the above copyright
14280304Sjkim *    notice, this list of conditions and the following disclaimer.
15238384Sjkim *
16238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
17238384Sjkim *    notice, this list of conditions and the following disclaimer in
18238384Sjkim *    the documentation and/or other materials provided with the
19238384Sjkim *    distribution.
20238384Sjkim *
21238384Sjkim * 3. All advertising materials mentioning features or use of this
22238384Sjkim *    software must display the following acknowledgment:
23238384Sjkim *    "This product includes software developed by the OpenSSL Project
24238384Sjkim *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25238384Sjkim *
26238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27238384Sjkim *    endorse or promote products derived from this software without
28238384Sjkim *    prior written permission. For written permission, please contact
29238384Sjkim *    licensing@OpenSSL.org.
30238384Sjkim *
31238384Sjkim * 5. Products derived from this software may not be called "OpenSSL"
32238384Sjkim *    nor may "OpenSSL" appear in their names without prior written
33238384Sjkim *    permission of the OpenSSL Project.
34238384Sjkim *
35238384Sjkim * 6. Redistributions of any form whatsoever must retain the following
36238384Sjkim *    acknowledgment:
37238384Sjkim *    "This product includes software developed by the OpenSSL Project
38238384Sjkim *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39238384Sjkim *
40238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43238384Sjkim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE.
52238384Sjkim * ====================================================================
53238384Sjkim *
54238384Sjkim * This product includes cryptographic software written by Eric Young
55238384Sjkim * (eay@cryptsoft.com).  This product includes software written by Tim
56238384Sjkim * Hudson (tjh@cryptsoft.com).
57238384Sjkim *
58238384Sjkim */
59238384Sjkim
60238384Sjkim#include <stdio.h>
61238384Sjkim#include "cryptlib.h"
62238384Sjkim#include <openssl/objects.h>
63238384Sjkim#include <openssl/bn.h>
64238384Sjkim#include <openssl/x509v3.h>
65238384Sjkim#include "ts.h"
66238384Sjkim
67238384Sjkim/* Local function declarations. */
68238384Sjkim
69238384Sjkim/* Function definitions. */
70238384Sjkim
71238384Sjkimint TS_ASN1_INTEGER_print_bio(BIO *bio, const ASN1_INTEGER *num)
72280304Sjkim{
73280304Sjkim    BIGNUM num_bn;
74280304Sjkim    int result = 0;
75280304Sjkim    char *hex;
76238384Sjkim
77280304Sjkim    BN_init(&num_bn);
78280304Sjkim    ASN1_INTEGER_to_BN(num, &num_bn);
79280304Sjkim    if ((hex = BN_bn2hex(&num_bn))) {
80280304Sjkim        result = BIO_write(bio, "0x", 2) > 0;
81280304Sjkim        result = result && BIO_write(bio, hex, strlen(hex)) > 0;
82280304Sjkim        OPENSSL_free(hex);
83280304Sjkim    }
84280304Sjkim    BN_free(&num_bn);
85238384Sjkim
86280304Sjkim    return result;
87280304Sjkim}
88238384Sjkim
89238384Sjkimint TS_OBJ_print_bio(BIO *bio, const ASN1_OBJECT *obj)
90280304Sjkim{
91280304Sjkim    char obj_txt[128];
92238384Sjkim
93306196Sjkim    OBJ_obj2txt(obj_txt, sizeof(obj_txt), obj, 0);
94306196Sjkim    BIO_printf(bio, "%s\n", obj_txt);
95238384Sjkim
96280304Sjkim    return 1;
97280304Sjkim}
98238384Sjkim
99238384Sjkimint TS_ext_print_bio(BIO *bio, const STACK_OF(X509_EXTENSION) *extensions)
100280304Sjkim{
101280304Sjkim    int i, critical, n;
102280304Sjkim    X509_EXTENSION *ex;
103280304Sjkim    ASN1_OBJECT *obj;
104238384Sjkim
105280304Sjkim    BIO_printf(bio, "Extensions:\n");
106280304Sjkim    n = X509v3_get_ext_count(extensions);
107280304Sjkim    for (i = 0; i < n; i++) {
108280304Sjkim        ex = X509v3_get_ext(extensions, i);
109280304Sjkim        obj = X509_EXTENSION_get_object(ex);
110280304Sjkim        i2a_ASN1_OBJECT(bio, obj);
111280304Sjkim        critical = X509_EXTENSION_get_critical(ex);
112280304Sjkim        BIO_printf(bio, ": %s\n", critical ? "critical" : "");
113280304Sjkim        if (!X509V3_EXT_print(bio, ex, 0, 4)) {
114280304Sjkim            BIO_printf(bio, "%4s", "");
115280304Sjkim            M_ASN1_OCTET_STRING_print(bio, ex->value);
116280304Sjkim        }
117280304Sjkim        BIO_write(bio, "\n", 1);
118280304Sjkim    }
119238384Sjkim
120280304Sjkim    return 1;
121280304Sjkim}
122238384Sjkim
123238384Sjkimint TS_X509_ALGOR_print_bio(BIO *bio, const X509_ALGOR *alg)
124280304Sjkim{
125280304Sjkim    int i = OBJ_obj2nid(alg->algorithm);
126280304Sjkim    return BIO_printf(bio, "Hash Algorithm: %s\n",
127280304Sjkim                      (i == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(i));
128280304Sjkim}
129238384Sjkim
130238384Sjkimint TS_MSG_IMPRINT_print_bio(BIO *bio, TS_MSG_IMPRINT *a)
131280304Sjkim{
132280304Sjkim    const ASN1_OCTET_STRING *msg;
133238384Sjkim
134280304Sjkim    TS_X509_ALGOR_print_bio(bio, TS_MSG_IMPRINT_get_algo(a));
135238384Sjkim
136280304Sjkim    BIO_printf(bio, "Message data:\n");
137280304Sjkim    msg = TS_MSG_IMPRINT_get_msg(a);
138280304Sjkim    BIO_dump_indent(bio, (const char *)M_ASN1_STRING_data(msg),
139280304Sjkim                    M_ASN1_STRING_length(msg), 4);
140238384Sjkim
141280304Sjkim    return 1;
142280304Sjkim}
143