/src/openssl/crypto/ct/ct_prn.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2016-2025 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 <stdio.h> |
15 | | |
16 | | #include <openssl/asn1.h> |
17 | | #include <openssl/bio.h> |
18 | | |
19 | | #include "ct_local.h" |
20 | | |
21 | | static void SCT_signature_algorithms_print(const SCT *sct, BIO *out) |
22 | 0 | { |
23 | 0 | int nid = SCT_get_signature_nid(sct); |
24 | |
|
25 | 0 | if (nid == NID_undef) |
26 | 0 | BIO_printf(out, "%02X%02X", sct->hash_alg, sct->sig_alg); |
27 | 0 | else |
28 | 0 | BIO_printf(out, "%s", OBJ_nid2ln(nid)); |
29 | 0 | } |
30 | | |
31 | | static void timestamp_print(uint64_t timestamp, BIO *out) |
32 | 0 | { |
33 | 0 | ASN1_GENERALIZEDTIME *gen = ASN1_GENERALIZEDTIME_new(); |
34 | 0 | char genstr[20]; |
35 | |
|
36 | 0 | if (gen == NULL) |
37 | 0 | return; |
38 | 0 | ASN1_GENERALIZEDTIME_adj(gen, (time_t)0, |
39 | 0 | (int)(timestamp / 86400000), |
40 | 0 | (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 | 0 | snprintf(genstr, sizeof(genstr), "%.14s.%03dZ", |
46 | 0 | ASN1_STRING_get0_data(gen), (unsigned int)(timestamp % 1000)); |
47 | 0 | if (ASN1_GENERALIZEDTIME_set_string(gen, genstr)) |
48 | 0 | ASN1_GENERALIZEDTIME_print(out, gen); |
49 | 0 | ASN1_GENERALIZEDTIME_free(gen); |
50 | 0 | } |
51 | | |
52 | | const char *SCT_validation_status_string(const SCT *sct) |
53 | 0 | { |
54 | |
|
55 | 0 | switch (SCT_get_validation_status(sct)) { |
56 | 0 | case SCT_VALIDATION_STATUS_NOT_SET: |
57 | 0 | return "not set"; |
58 | 0 | case SCT_VALIDATION_STATUS_UNKNOWN_VERSION: |
59 | 0 | return "unknown version"; |
60 | 0 | case SCT_VALIDATION_STATUS_UNKNOWN_LOG: |
61 | 0 | return "unknown log"; |
62 | 0 | case SCT_VALIDATION_STATUS_UNVERIFIED: |
63 | 0 | return "unverified"; |
64 | 0 | case SCT_VALIDATION_STATUS_INVALID: |
65 | 0 | return "invalid"; |
66 | 0 | case SCT_VALIDATION_STATUS_VALID: |
67 | 0 | return "valid"; |
68 | 0 | } |
69 | 0 | return "unknown status"; |
70 | 0 | } |
71 | | |
72 | | void SCT_print(const SCT *sct, BIO *out, int indent, |
73 | | const CTLOG_STORE *log_store) |
74 | 0 | { |
75 | 0 | const CTLOG *log = NULL; |
76 | |
|
77 | 0 | if (log_store != NULL) { |
78 | 0 | log = CTLOG_STORE_get0_log_by_id(log_store, sct->log_id, |
79 | 0 | sct->log_id_len); |
80 | 0 | } |
81 | |
|
82 | 0 | BIO_printf(out, "%*sSigned Certificate Timestamp:", indent, ""); |
83 | 0 | BIO_printf(out, "\n%*sVersion : ", indent + 4, ""); |
84 | |
|
85 | 0 | if (sct->version != SCT_VERSION_V1) { |
86 | 0 | BIO_printf(out, "unknown\n%*s", indent + 16, ""); |
87 | 0 | BIO_hex_string(out, indent + 16, 16, sct->sct, (int)sct->sct_len); |
88 | 0 | return; |
89 | 0 | } |
90 | | |
91 | 0 | BIO_printf(out, "v1 (0x0)"); |
92 | |
|
93 | 0 | if (log != NULL) { |
94 | 0 | BIO_printf(out, "\n%*sLog : %s", indent + 4, "", |
95 | 0 | CTLOG_get0_name(log)); |
96 | 0 | } |
97 | |
|
98 | 0 | BIO_printf(out, "\n%*sLog ID : ", indent + 4, ""); |
99 | 0 | BIO_hex_string(out, indent + 16, 16, sct->log_id, (int)sct->log_id_len); |
100 | |
|
101 | 0 | BIO_printf(out, "\n%*sTimestamp : ", indent + 4, ""); |
102 | 0 | timestamp_print(sct->timestamp, out); |
103 | |
|
104 | 0 | BIO_printf(out, "\n%*sExtensions: ", indent + 4, ""); |
105 | 0 | if (sct->ext_len == 0) |
106 | 0 | BIO_printf(out, "none"); |
107 | 0 | else |
108 | 0 | BIO_hex_string(out, indent + 16, 16, sct->ext, (int)sct->ext_len); |
109 | |
|
110 | 0 | BIO_printf(out, "\n%*sSignature : ", indent + 4, ""); |
111 | 0 | SCT_signature_algorithms_print(sct, out); |
112 | 0 | BIO_printf(out, "\n%*s ", indent + 4, ""); |
113 | 0 | BIO_hex_string(out, indent + 16, 16, sct->sig, (int)sct->sig_len); |
114 | 0 | } |
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 | 0 | { |
119 | 0 | int sct_count = sk_SCT_num(sct_list); |
120 | 0 | int i; |
121 | |
|
122 | 0 | for (i = 0; i < sct_count; ++i) { |
123 | 0 | SCT *sct = sk_SCT_value(sct_list, i); |
124 | |
|
125 | 0 | SCT_print(sct, out, indent, log_store); |
126 | 0 | if (i < sk_SCT_num(sct_list) - 1) |
127 | 0 | BIO_printf(out, "%s", separator); |
128 | 0 | } |
129 | 0 | } |