File: main.go

package info (click to toggle)
golang-github-zmap-zcrypto 0.0~git20240512.0fef58d-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: python: 567; sh: 124; makefile: 9
file content (52 lines) | stat: -rw-r--r-- 1,314 bytes parent folder | download
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
package main

import (
	"flag"
	"fmt"
	"log"

	"encoding/json"
	ct "github.com/zmap/zcrypto/ct"
	"github.com/zmap/zcrypto/ct/client"
	"github.com/zmap/zcrypto/x509"
)

// Processes the given entry in the specified log.
func processEntry(entry ct.LogEntry) (*x509.Certificate, error) {
	cert := &x509.Certificate{}
	switch entry.Leaf.TimestampedEntry.EntryType {
	case ct.X509LogEntryType:
		innerCert, err := x509.ParseCertificate(entry.Leaf.TimestampedEntry.X509Entry)
		if err != nil {
			return nil, err
		}
		cert = innerCert
	case ct.PrecertLogEntryType:
		innerCert, err := x509.ParseCertificate(entry.Leaf.TimestampedEntry.PrecertEntry.TBSCertificate)
		if err != nil {
			return nil, err
		}
		cert = innerCert
	}
	return cert, nil
}

func main() {
	var logURI = flag.String("log_uri", "http://ct.googleapis.com/aviator", "CT log base URI")
	var indexToParse = flag.Int64("index", 1, "Index to parse")
	flag.Parse()
	logClient := client.New(*logURI)
	entries, err := logClient.GetEntries(*indexToParse, *indexToParse)
	if err != nil {
		log.Fatal(err)
	}
	for _, entry := range entries {
		cert, err := processEntry(entry)
		if err != nil {
			fmt.Printf("%d %s\n", entry.Index, err.Error())
			continue
		}
		finalJSON, _ := json.Marshal(cert)
		fmt.Printf("%d %s\n", entry.Index, string(finalJSON))
	}
}