forked from google/certificate-transparency-go
/
get_entries.go
128 lines (116 loc) · 3.41 KB
/
get_entries.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2022 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"context"
"encoding/pem"
"fmt"
"os"
ct "github.com/RarimoVoting/certificate-transparency-go"
"github.com/RarimoVoting/certificate-transparency-go/x509"
"github.com/RarimoVoting/certificate-transparency-go/x509util"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
)
var (
getFirst int64
getLast int64
chainOut bool
textOut bool
)
func init() {
cmd := cobra.Command{
Use: fmt.Sprintf("get-entries %s --first=idx [--last=idx]", connectionFlags),
Aliases: []string{"getentries", "entries"},
Short: "Fetch a range of entries in the log",
Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, _ []string) {
runGetEntries(cmd.Context())
},
}
cmd.Flags().Int64Var(&getFirst, "first", -1, "First entry to get")
cmd.Flags().Int64Var(&getLast, "last", -1, "Last entry to get")
cmd.Flags().BoolVar(&chainOut, "chain", false, "Display entire certificate chain")
cmd.Flags().BoolVar(&textOut, "text", true, "Display certificates as text")
rootCmd.AddCommand(&cmd)
}
// runGetEntries runs the get-entries command.
func runGetEntries(ctx context.Context) {
logClient := connect(ctx)
if getFirst == -1 {
klog.Exit("No -first option supplied")
}
if getLast == -1 {
getLast = getFirst
}
rsp, err := logClient.GetRawEntries(ctx, getFirst, getLast)
if err != nil {
exitWithDetails(err)
}
for i, rawEntry := range rsp.Entries {
index := getFirst + int64(i)
rle, err := ct.RawLogEntryFromLeaf(index, &rawEntry)
if err != nil {
fmt.Printf("Index=%d Failed to unmarshal leaf entry: %v", index, err)
continue
}
showRawLogEntry(rle)
}
}
func showRawLogEntry(rle *ct.RawLogEntry) {
ts := rle.Leaf.TimestampedEntry
when := ct.TimestampToTime(ts.Timestamp)
fmt.Printf("Index=%d Timestamp=%d (%v) ", rle.Index, ts.Timestamp, when)
switch ts.EntryType {
case ct.X509LogEntryType:
fmt.Printf("X.509 certificate:\n")
showRawCert(*ts.X509Entry)
case ct.PrecertLogEntryType:
fmt.Printf("pre-certificate from issuer with keyhash %x:\n", ts.PrecertEntry.IssuerKeyHash)
showRawCert(rle.Cert) // As-submitted: with signature and poison.
default:
fmt.Printf("Unhandled log entry type %d\n", ts.EntryType)
}
if chainOut {
for _, c := range rle.Chain {
showRawCert(c)
}
}
}
func showRawCert(cert ct.ASN1Cert) {
if textOut {
c, err := x509.ParseCertificate(cert.Data)
if err != nil {
klog.Errorf("Error parsing certificate: %q", err.Error())
}
if c == nil {
return
}
showParsedCert(c)
} else {
showPEMData(cert.Data)
}
}
func showParsedCert(cert *x509.Certificate) {
if textOut {
fmt.Printf("%s\n", x509util.CertificateToString(cert))
} else {
showPEMData(cert.Raw)
}
}
func showPEMData(data []byte) {
if err := pem.Encode(os.Stdout, &pem.Block{Type: "CERTIFICATE", Bytes: data}); err != nil {
klog.Errorf("Failed to PEM encode cert: %q", err.Error())
}
}