Debian Codegraph @

cmd/certpicker/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "context"
5 "encoding/json"
6 "flag"
7 "fmt"
8 "log"
9 "net/http"
10 "strings"
11 "time"
12
13 "github.com/d-Rickyy-b/certstream-server-go/internal/certificatetransparency"
14 "github.com/d-Rickyy-b/certstream-server-go/internal/config"
15 "github.com/d-Rickyy-b/certstream-server-go/internal/models"
16
17 ct "github.com/google/certificate-transparency-go"
18 "github.com/google/certificate-transparency-go/client"
19 "github.com/google/certificate-transparency-go/jsonclient"
20 "github.com/google/certificate-transparency-go/scanner"
21 )
22
23 var userAgent = fmt.Sprintf("Certstream v%s (github.com/d-Rickyy-b/certstream-server-go)", config.Version)
24
25 func main() {
26 ctLogFlag := flag.String("log", "", "URL of the CT log - e.g. ct.googleapis.com/logs/eu1/xenon2025h2")
27 certIDFlag := flag.Int64("cert", 0, "ID of the certificate to fetch from the CT log")
28 chainFlag := flag.Bool("chain", false, "Include full chain for the certificate")
29 asDERFlag := flag.Bool("asder", false, "Include DER encoding of the certificate")
30 flag.Parse()
31
32 ctLog := *ctLogFlag
33 certID := *certIDFlag
34
35 if ctLog == "" {
36 log.Fatalln("CT log URL is required")
37 }
38 if !strings.HasPrefix(ctLog, "https://") {
39 ctLog = "https://" + ctLog
40 }
41
42 // Initialize the http client and json client provided by the ct library
43 hc := http.Client{Timeout: 30 * time.Second}
44 jsonClient, e := client.New(ctLog, &hc, jsonclient.Options{UserAgent: userAgent})
45 if e != nil {
46 log.Fatalln("Error creating JSON client:", e)
47 }
48
49 // Get entries from CT log
50 c, _ := context.WithTimeout(context.Background(), 10*time.Second)
51 entries, getEntriesErr := jsonClient.GetRawEntries(c, certID, certID)
52 if getEntriesErr != nil {
53 log.Fatalln("Error getting entries from CT log: ", getEntriesErr)
54 }
55
56 // Loop over entries and pars each one.
57 for _, leafEntry := range entries.Entries {
58 rawLogEntry, err := ct.RawLogEntryFromLeaf(certID, &leafEntry)
59 if err != nil {
60 log.Fatalln("Error creating raw log entry: ", err)
61 }
62
63 entry, parseErr := certificatetransparency.ParseCertstreamEntry(rawLogEntry, "N/A", "N/A", ctLog, models.SourceIsRFC6962)
64 if parseErr != nil {
65 log.Fatalln("Error parsing certstream entry: ", parseErr)
66 }
67
68 // Check if the entry is a certificate or precertificate
69 if logEntry, toLogEntryErr := rawLogEntry.ToLogEntry(); toLogEntryErr != nil {
70 log.Println("Error converting rawLogEntry to logEntry: ", toLogEntryErr)
71 } else {
72 matcher := scanner.MatchAll{}
73 if logEntry.X509Cert != nil && matcher.CertificateMatches(logEntry.X509Cert) {
74 entry.Data.UpdateType = "X509LogEntry"
75 }
76 if logEntry.Precert != nil && matcher.PrecertificateMatches(logEntry.Precert) {
77 entry.Data.UpdateType = "PrecertLogEntry"
78 }
79 }
80
81 // Remove DER encoding and chain if not requested
82 if !*asDERFlag {
83 entry.Data.LeafCert.AsDER = ""
84 for i := range entry.Data.Chain {
85 entry.Data.Chain[i].AsDER = ""
86 }
87 }
88
89 // Remove chain if not requested
90 if !*chainFlag {
91 entry.Data.Chain = nil
92 }
93
94 // Marshal the certificate entry to JSON and pretty print it
95 result, marshalErr := json.MarshalIndent(entry, "", " ")
96 if marshalErr != nil {
97 return
98 }
99
100 fmt.Println(string(result))
101 }
102 }