-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
checkpoint.go
196 lines (170 loc) · 4.83 KB
/
checkpoint.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found at
// https://go.googlesource.com/go/+/refs/heads/master/LICENSE.
package sunlight
import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"math"
"strconv"
"strings"
"unicode"
"unicode/utf8"
ct "github.com/google/certificate-transparency-go"
"golang.org/x/crypto/cryptobyte"
"golang.org/x/mod/sumdb/note"
"golang.org/x/mod/sumdb/tlog"
)
const maxCheckpointSize = 1e6
// A Checkpoint is a tree head to be formatted according to c2sp.org/checkpoint.
//
// A checkpoint looks like this:
//
// example.com/origin
// 923748
// nND/nri/U0xuHUrYSy0HtMeal2vzD9V4k/BO79C+QeI=
//
// It can be followed by extra extension lines.
type Checkpoint struct {
Origin string
tlog.Tree
// Extension is empty or a sequence of non-empty lines,
// each terminated by a newline character.
Extension string
}
func ParseCheckpoint(text string) (Checkpoint, error) {
// This is an extended version of tlog.ParseTree.
if strings.Count(text, "\n") < 3 || len(text) > maxCheckpointSize {
return Checkpoint{}, errors.New("malformed checkpoint")
}
if !strings.HasSuffix(text, "\n") {
return Checkpoint{}, errors.New("malformed checkpoint")
}
lines := strings.SplitN(text, "\n", 4)
n, err := strconv.ParseInt(lines[1], 10, 64)
if err != nil || n < 0 || lines[1] != strconv.FormatInt(n, 10) {
return Checkpoint{}, errors.New("malformed checkpoint")
}
h, err := base64.StdEncoding.DecodeString(lines[2])
if err != nil || len(h) != tlog.HashSize {
return Checkpoint{}, errors.New("malformed checkpoint")
}
rest := lines[3]
for rest != "" {
before, after, found := strings.Cut(rest, "\n")
if before == "" || !found {
return Checkpoint{}, errors.New("malformed checkpoint")
}
rest = after
}
var hash tlog.Hash
copy(hash[:], h)
return Checkpoint{lines[0], tlog.Tree{N: n, Hash: hash}, lines[3]}, nil
}
func FormatCheckpoint(c Checkpoint) string {
return fmt.Sprintf("%s\n%d\n%s\n%s",
c.Origin, c.N, base64.StdEncoding.EncodeToString(c.Hash[:]), c.Extension)
}
// NewRFC6962Verifier constructs a new [note.Verifier] that verifies a RFC 6962
// TreeHeadSignature formatted according to c2sp.org/sunlight.
func NewRFC6962Verifier(name string, key crypto.PublicKey) (note.Verifier, error) {
if !isValidName(name) {
return nil, fmt.Errorf("invalid name %q", name)
}
pkix, err := x509.MarshalPKIXPublicKey(key)
if err != nil {
return nil, err
}
keyID := sha256.Sum256(pkix)
v := &verifier{}
v.name = name
v.hash = keyHash(name, append([]byte{0x05}, keyID[:]...))
v.verify = func(msg, sig []byte) (ok bool) {
c, err := ParseCheckpoint(string(msg))
if err != nil {
return false
}
if c.Extension != "" {
return false
}
// Parse the RFC6962NoteSignature.
var timestamp uint64
var hashAlg, sigAlg uint8
var signature []byte
s := cryptobyte.String(sig)
if !s.ReadUint64(×tamp) ||
!s.ReadUint8(&hashAlg) || hashAlg != 4 || !s.ReadUint8(&sigAlg) ||
!s.ReadUint16LengthPrefixed((*cryptobyte.String)(&signature)) ||
!s.Empty() {
return false
}
sth := ct.SignedTreeHead{
Version: ct.V1,
TreeSize: uint64(c.N),
Timestamp: timestamp,
SHA256RootHash: ct.SHA256Hash(c.Hash),
}
sthBytes, err := ct.SerializeSTHSignatureInput(sth)
if err != nil {
return false
}
digest := sha256.Sum256(sthBytes)
switch key := key.(type) {
case *rsa.PublicKey:
if sigAlg != 1 {
return false
}
return rsa.VerifyPKCS1v15(key, crypto.SHA256, digest[:], sig) == nil
case *ecdsa.PublicKey:
if sigAlg != 3 {
return false
}
return ecdsa.VerifyASN1(key, digest[:], signature)
default:
return false
}
}
return v, nil
}
type verifier struct {
name string
hash uint32
verify func(msg, sig []byte) bool
}
func (v *verifier) Name() string { return v.name }
func (v *verifier) KeyHash() uint32 { return v.hash }
func (v *verifier) Verify(msg, sig []byte) bool { return v.verify(msg, sig) }
func isValidName(name string) bool {
return name != "" && utf8.ValidString(name) &&
strings.IndexFunc(name, unicode.IsSpace) < 0 &&
!strings.Contains(name, "+")
}
func keyHash(name string, key []byte) uint32 {
h := sha256.New()
h.Write([]byte(name))
h.Write([]byte("\n"))
h.Write(key)
sum := h.Sum(nil)
return binary.BigEndian.Uint32(sum)
}
func RFC6962SignatureTimestamp(sig note.Signature) (int64, error) {
sigBytes, err := base64.StdEncoding.DecodeString(sig.Base64)
if err != nil {
return 0, err
}
var timestamp uint64
s := cryptobyte.String(sigBytes)
if !s.Skip(4 /* key hash */) || !s.ReadUint64(×tamp) ||
timestamp > math.MaxInt64 {
return 0, errors.New("malformed RFC 6962 TreeHeadSignature")
}
return int64(timestamp), nil
}