...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package submission
16
17 import (
18 "context"
19 "crypto/sha256"
20 "fmt"
21
22 ct "github.com/google/certificate-transparency-go"
23 "github.com/google/certificate-transparency-go/client"
24 "github.com/google/certificate-transparency-go/loglist3"
25 "github.com/google/certificate-transparency-go/tls"
26 "github.com/google/certificate-transparency-go/x509util"
27 )
28
29 type rootInfo struct {
30 raw []byte
31 filename string
32 }
33
34
35 type stubLogClient struct {
36 logURL string
37 rootsCerts map[string][]rootInfo
38 }
39
40 func (m stubLogClient) AddChain(ctx context.Context, chain []ct.ASN1Cert) (*ct.SignedCertificateTimestamp, error) {
41 if _, ok := m.rootsCerts[m.logURL]; ok {
42 return testSCT(m.logURL), nil
43 }
44 return nil, fmt.Errorf("log %q has no roots", m.logURL)
45 }
46
47 func (m stubLogClient) AddPreChain(ctx context.Context, chain []ct.ASN1Cert) (*ct.SignedCertificateTimestamp, error) {
48 if _, ok := m.rootsCerts[m.logURL]; ok {
49 return testSCT(m.logURL), nil
50 }
51 return nil, fmt.Errorf("log %q has no roots", m.logURL)
52 }
53
54 func (m stubLogClient) GetAcceptedRoots(ctx context.Context) ([]ct.ASN1Cert, error) {
55 roots := []ct.ASN1Cert{}
56 certInfos, ok := m.rootsCerts[m.logURL]
57 if !ok {
58 return roots, nil
59 }
60 for _, certInfo := range certInfos {
61 if len(certInfo.raw) > 0 {
62 roots = append(roots, ct.ASN1Cert{Data: certInfo.raw})
63 continue
64 }
65 roots = append(roots, ct.ASN1Cert{Data: readCertFile(certInfo.filename)})
66 }
67 return roots, nil
68 }
69
70
71 func readCertFile(filename string) []byte {
72 data, err := x509util.ReadPossiblePEMFile(filename, "CERTIFICATE")
73 if err != nil {
74 return nil
75 }
76 return data[0]
77 }
78
79
80 func testSCT(logURL string) *ct.SignedCertificateTimestamp {
81 var keyID [sha256.Size]byte
82 copy(keyID[:], logURL)
83 return &ct.SignedCertificateTimestamp{
84 SCTVersion: ct.V1,
85 LogID: ct.LogID{KeyID: keyID},
86 Timestamp: 1234,
87 Extensions: []byte{},
88 Signature: ct.DigitallySigned{
89 Algorithm: tls.SignatureAndHashAlgorithm{
90 Hash: tls.SHA256,
91 Signature: tls.ECDSA,
92 },
93 },
94 }
95 }
96
97 func newRootedStubLogClient(log *loglist3.Log, rCerts map[string][]rootInfo) (client.AddLogClient, error) {
98 return stubLogClient{logURL: log.URL, rootsCerts: rCerts}, nil
99 }
100
101 func newEmptyStubLogClient(log *loglist3.Log) (client.AddLogClient, error) {
102 return newRootedStubLogClient(log, map[string][]rootInfo{})
103 }
104
105
106
107 func NewStubLogClient(log *loglist3.Log) (client.AddLogClient, error) {
108 return stubLogClient{logURL: log.URL, rootsCerts: map[string][]rootInfo{log.URL: {}}}, nil
109 }
110
View as plain text