1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
12e1051a39Sopenharmony_ci#include <openssl/x509.h>
13e1051a39Sopenharmony_ci#include <openssl/asn1.h>
14e1051a39Sopenharmony_ci#include <openssl/rsa.h>
15e1051a39Sopenharmony_ci#include <openssl/dsa.h>
16e1051a39Sopenharmony_ci#include <openssl/bn.h>
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_ci/* Print out an SPKI */
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_ciint NETSCAPE_SPKI_print(BIO *out, NETSCAPE_SPKI *spki)
21e1051a39Sopenharmony_ci{
22e1051a39Sopenharmony_ci    EVP_PKEY *pkey;
23e1051a39Sopenharmony_ci    ASN1_IA5STRING *chal;
24e1051a39Sopenharmony_ci    ASN1_OBJECT *spkioid;
25e1051a39Sopenharmony_ci    int i, n;
26e1051a39Sopenharmony_ci    char *s;
27e1051a39Sopenharmony_ci    BIO_printf(out, "Netscape SPKI:\n");
28e1051a39Sopenharmony_ci    X509_PUBKEY_get0_param(&spkioid, NULL, NULL, NULL, spki->spkac->pubkey);
29e1051a39Sopenharmony_ci    i = OBJ_obj2nid(spkioid);
30e1051a39Sopenharmony_ci    BIO_printf(out, "  Public Key Algorithm: %s\n",
31e1051a39Sopenharmony_ci               (i == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(i));
32e1051a39Sopenharmony_ci    pkey = X509_PUBKEY_get(spki->spkac->pubkey);
33e1051a39Sopenharmony_ci    if (pkey == NULL)
34e1051a39Sopenharmony_ci        BIO_printf(out, "  Unable to load public key\n");
35e1051a39Sopenharmony_ci    else {
36e1051a39Sopenharmony_ci        EVP_PKEY_print_public(out, pkey, 4, NULL);
37e1051a39Sopenharmony_ci        EVP_PKEY_free(pkey);
38e1051a39Sopenharmony_ci    }
39e1051a39Sopenharmony_ci    chal = spki->spkac->challenge;
40e1051a39Sopenharmony_ci    if (chal->length)
41e1051a39Sopenharmony_ci        BIO_printf(out, "  Challenge String: %.*s\n", chal->length, chal->data);
42e1051a39Sopenharmony_ci    i = OBJ_obj2nid(spki->sig_algor.algorithm);
43e1051a39Sopenharmony_ci    BIO_printf(out, "  Signature Algorithm: %s",
44e1051a39Sopenharmony_ci               (i == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(i));
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_ci    n = spki->signature->length;
47e1051a39Sopenharmony_ci    s = (char *)spki->signature->data;
48e1051a39Sopenharmony_ci    for (i = 0; i < n; i++) {
49e1051a39Sopenharmony_ci        if ((i % 18) == 0)
50e1051a39Sopenharmony_ci            BIO_write(out, "\n      ", 7);
51e1051a39Sopenharmony_ci        BIO_printf(out, "%02x%s", (unsigned char)s[i],
52e1051a39Sopenharmony_ci                   ((i + 1) == n) ? "" : ":");
53e1051a39Sopenharmony_ci    }
54e1051a39Sopenharmony_ci    BIO_write(out, "\n", 1);
55e1051a39Sopenharmony_ci    return 1;
56e1051a39Sopenharmony_ci}
57