11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
51cb0ef41Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
61cb0ef41Sopenharmony_ci * in the file LICENSE in the source distribution or at
71cb0ef41Sopenharmony_ci * https://www.openssl.org/source/license.html
81cb0ef41Sopenharmony_ci */
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include <stdio.h>
111cb0ef41Sopenharmony_ci#include "internal/cryptlib.h"
121cb0ef41Sopenharmony_ci#include <openssl/objects.h>
131cb0ef41Sopenharmony_ci#include <openssl/bn.h>
141cb0ef41Sopenharmony_ci#include <openssl/x509.h>
151cb0ef41Sopenharmony_ci#include <openssl/x509v3.h>
161cb0ef41Sopenharmony_ci#include <openssl/ts.h>
171cb0ef41Sopenharmony_ci#include "ts_local.h"
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciint TS_ASN1_INTEGER_print_bio(BIO *bio, const ASN1_INTEGER *num)
201cb0ef41Sopenharmony_ci{
211cb0ef41Sopenharmony_ci    BIGNUM *num_bn;
221cb0ef41Sopenharmony_ci    int result = 0;
231cb0ef41Sopenharmony_ci    char *hex;
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci    num_bn = ASN1_INTEGER_to_BN(num, NULL);
261cb0ef41Sopenharmony_ci    if (num_bn == NULL)
271cb0ef41Sopenharmony_ci        return -1;
281cb0ef41Sopenharmony_ci    if ((hex = BN_bn2hex(num_bn))) {
291cb0ef41Sopenharmony_ci        result = BIO_write(bio, "0x", 2) > 0;
301cb0ef41Sopenharmony_ci        result = result && BIO_write(bio, hex, strlen(hex)) > 0;
311cb0ef41Sopenharmony_ci        OPENSSL_free(hex);
321cb0ef41Sopenharmony_ci    }
331cb0ef41Sopenharmony_ci    BN_free(num_bn);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci    return result;
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciint TS_OBJ_print_bio(BIO *bio, const ASN1_OBJECT *obj)
391cb0ef41Sopenharmony_ci{
401cb0ef41Sopenharmony_ci    char obj_txt[128];
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    OBJ_obj2txt(obj_txt, sizeof(obj_txt), obj, 0);
431cb0ef41Sopenharmony_ci    BIO_printf(bio, "%s\n", obj_txt);
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci    return 1;
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciint TS_ext_print_bio(BIO *bio, const STACK_OF(X509_EXTENSION) *extensions)
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci    int i, critical, n;
511cb0ef41Sopenharmony_ci    X509_EXTENSION *ex;
521cb0ef41Sopenharmony_ci    ASN1_OBJECT *obj;
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    BIO_printf(bio, "Extensions:\n");
551cb0ef41Sopenharmony_ci    n = X509v3_get_ext_count(extensions);
561cb0ef41Sopenharmony_ci    for (i = 0; i < n; i++) {
571cb0ef41Sopenharmony_ci        ex = X509v3_get_ext(extensions, i);
581cb0ef41Sopenharmony_ci        obj = X509_EXTENSION_get_object(ex);
591cb0ef41Sopenharmony_ci        if (i2a_ASN1_OBJECT(bio, obj) < 0)
601cb0ef41Sopenharmony_ci            return 0;
611cb0ef41Sopenharmony_ci        critical = X509_EXTENSION_get_critical(ex);
621cb0ef41Sopenharmony_ci        BIO_printf(bio, ":%s\n", critical ? " critical" : "");
631cb0ef41Sopenharmony_ci        if (!X509V3_EXT_print(bio, ex, 0, 4)) {
641cb0ef41Sopenharmony_ci            BIO_printf(bio, "%4s", "");
651cb0ef41Sopenharmony_ci            ASN1_STRING_print(bio, X509_EXTENSION_get_data(ex));
661cb0ef41Sopenharmony_ci        }
671cb0ef41Sopenharmony_ci        BIO_write(bio, "\n", 1);
681cb0ef41Sopenharmony_ci    }
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci    return 1;
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ciint TS_X509_ALGOR_print_bio(BIO *bio, const X509_ALGOR *alg)
741cb0ef41Sopenharmony_ci{
751cb0ef41Sopenharmony_ci    int i = OBJ_obj2nid(alg->algorithm);
761cb0ef41Sopenharmony_ci    return BIO_printf(bio, "Hash Algorithm: %s\n",
771cb0ef41Sopenharmony_ci                      (i == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(i));
781cb0ef41Sopenharmony_ci}
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ciint TS_MSG_IMPRINT_print_bio(BIO *bio, TS_MSG_IMPRINT *a)
811cb0ef41Sopenharmony_ci{
821cb0ef41Sopenharmony_ci    ASN1_OCTET_STRING *msg;
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci    TS_X509_ALGOR_print_bio(bio, a->hash_algo);
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci    BIO_printf(bio, "Message data:\n");
871cb0ef41Sopenharmony_ci    msg = a->hashed_msg;
881cb0ef41Sopenharmony_ci    BIO_dump_indent(bio, (const char *)ASN1_STRING_get0_data(msg),
891cb0ef41Sopenharmony_ci                    ASN1_STRING_length(msg), 4);
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci    return 1;
921cb0ef41Sopenharmony_ci}
93