github.com/zmap/zcrypto@v0.0.0-20240512203510-0fef58d9a9db/ct/client/main/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"log"
     7  
     8  	"encoding/json"
     9  	ct "github.com/zmap/zcrypto/ct"
    10  	"github.com/zmap/zcrypto/ct/client"
    11  	"github.com/zmap/zcrypto/x509"
    12  )
    13  
    14  // Processes the given entry in the specified log.
    15  func processEntry(entry ct.LogEntry) (*x509.Certificate, error) {
    16  	cert := &x509.Certificate{}
    17  	switch entry.Leaf.TimestampedEntry.EntryType {
    18  	case ct.X509LogEntryType:
    19  		innerCert, err := x509.ParseCertificate(entry.Leaf.TimestampedEntry.X509Entry)
    20  		if err != nil {
    21  			return nil, err
    22  		}
    23  		cert = innerCert
    24  	case ct.PrecertLogEntryType:
    25  		innerCert, err := x509.ParseCertificate(entry.Leaf.TimestampedEntry.PrecertEntry.TBSCertificate)
    26  		if err != nil {
    27  			return nil, err
    28  		}
    29  		cert = innerCert
    30  	}
    31  	return cert, nil
    32  }
    33  
    34  func main() {
    35  	var logURI = flag.String("log_uri", "http://ct.googleapis.com/aviator", "CT log base URI")
    36  	var indexToParse = flag.Int64("index", 1, "Index to parse")
    37  	flag.Parse()
    38  	logClient := client.New(*logURI)
    39  	entries, err := logClient.GetEntries(*indexToParse, *indexToParse)
    40  	if err != nil {
    41  		log.Fatal(err)
    42  	}
    43  	for _, entry := range entries {
    44  		cert, err := processEntry(entry)
    45  		if err != nil {
    46  			fmt.Printf("%d %s\n", entry.Index, err.Error())
    47  			continue
    48  		}
    49  		finalJSON, _ := json.Marshal(cert)
    50  		fmt.Printf("%d %s\n", entry.Index, string(finalJSON))
    51  	}
    52  }