In OpenSSL builds, declare STACK for datatypes ...
[openssl.git] / crypto / ct / ct_prn.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #ifdef OPENSSL_NO_CT
11 # error "CT is disabled"
12 #endif
13
14 #include <openssl/asn1.h>
15 #include <openssl/bio.h>
16
17 #include "ct_local.h"
18
19 DEFINE_STACK_OF(SCT)
20
21 static void SCT_signature_algorithms_print(const SCT *sct, BIO *out)
22 {
23     int nid = SCT_get_signature_nid(sct);
24
25     if (nid == NID_undef)
26         BIO_printf(out, "%02X%02X", sct->hash_alg, sct->sig_alg);
27     else
28         BIO_printf(out, "%s", OBJ_nid2ln(nid));
29 }
30
31 static void timestamp_print(uint64_t timestamp, BIO *out)
32 {
33     ASN1_GENERALIZEDTIME *gen = ASN1_GENERALIZEDTIME_new();
34     char genstr[20];
35
36     if (gen == NULL)
37         return;
38     ASN1_GENERALIZEDTIME_adj(gen, (time_t)0,
39                              (int)(timestamp / 86400000),
40                              (timestamp % 86400000) / 1000);
41     /*
42      * Note GeneralizedTime from ASN1_GENERALIZETIME_adj is always 15
43      * characters long with a final Z. Update it with fractional seconds.
44      */
45     BIO_snprintf(genstr, sizeof(genstr), "%.14s.%03dZ",
46                  ASN1_STRING_get0_data(gen), (unsigned int)(timestamp % 1000));
47     if (ASN1_GENERALIZEDTIME_set_string(gen, genstr))
48         ASN1_GENERALIZEDTIME_print(out, gen);
49     ASN1_GENERALIZEDTIME_free(gen);
50 }
51
52 const char *SCT_validation_status_string(const SCT *sct)
53 {
54
55     switch (SCT_get_validation_status(sct)) {
56     case SCT_VALIDATION_STATUS_NOT_SET:
57         return "not set";
58     case SCT_VALIDATION_STATUS_UNKNOWN_VERSION:
59         return "unknown version";
60     case SCT_VALIDATION_STATUS_UNKNOWN_LOG:
61         return "unknown log";
62     case SCT_VALIDATION_STATUS_UNVERIFIED:
63         return "unverified";
64     case SCT_VALIDATION_STATUS_INVALID:
65         return "invalid";
66     case SCT_VALIDATION_STATUS_VALID:
67         return "valid";
68     }
69     return "unknown status";
70 }
71
72 void SCT_print(const SCT *sct, BIO *out, int indent,
73                const CTLOG_STORE *log_store)
74 {
75     const CTLOG *log = NULL;
76
77     if (log_store != NULL) {
78         log = CTLOG_STORE_get0_log_by_id(log_store, sct->log_id,
79                                          sct->log_id_len);
80     }
81
82     BIO_printf(out, "%*sSigned Certificate Timestamp:", indent, "");
83     BIO_printf(out, "\n%*sVersion   : ", indent + 4, "");
84
85     if (sct->version != SCT_VERSION_V1) {
86         BIO_printf(out, "unknown\n%*s", indent + 16, "");
87         BIO_hex_string(out, indent + 16, 16, sct->sct, sct->sct_len);
88         return;
89     }
90
91     BIO_printf(out, "v1 (0x0)");
92
93     if (log != NULL) {
94         BIO_printf(out, "\n%*sLog       : %s", indent + 4, "",
95                    CTLOG_get0_name(log));
96     }
97
98     BIO_printf(out, "\n%*sLog ID    : ", indent + 4, "");
99     BIO_hex_string(out, indent + 16, 16, sct->log_id, sct->log_id_len);
100
101     BIO_printf(out, "\n%*sTimestamp : ", indent + 4, "");
102     timestamp_print(sct->timestamp, out);
103
104     BIO_printf(out, "\n%*sExtensions: ", indent + 4, "");
105     if (sct->ext_len == 0)
106         BIO_printf(out, "none");
107     else
108         BIO_hex_string(out, indent + 16, 16, sct->ext, sct->ext_len);
109
110     BIO_printf(out, "\n%*sSignature : ", indent + 4, "");
111     SCT_signature_algorithms_print(sct, out);
112     BIO_printf(out, "\n%*s            ", indent + 4, "");
113     BIO_hex_string(out, indent + 16, 16, sct->sig, sct->sig_len);
114 }
115
116 void SCT_LIST_print(const STACK_OF(SCT) *sct_list, BIO *out, int indent,
117                     const char *separator, const CTLOG_STORE *log_store)
118 {
119     int sct_count = sk_SCT_num(sct_list);
120     int i;
121
122     for (i = 0; i < sct_count; ++i) {
123         SCT *sct = sk_SCT_value(sct_list, i);
124
125         SCT_print(sct, out, indent, log_store);
126         if (i < sk_SCT_num(sct_list) - 1)
127             BIO_printf(out, "%s", separator);
128     }
129 }