-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
statement.go
248 lines (218 loc) · 6.02 KB
/
statement.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package keys
import (
"bytes"
"fmt"
"time"
"github.com/keys-pub/keys/encoding"
"github.com/keys-pub/keys/json"
"github.com/keys-pub/keys/tsutil"
"github.com/pkg/errors"
)
// Statement with signature.
// Use NewSigchainStatement to create a signed Sigchain Statement.
type Statement struct {
// Sig is the signature bytes.
Sig []byte
// KID is the key that signed.
KID ID
// Data (optional).
Data []byte
// Seq in a sigchain (1 is root, optional).
Seq int
// Prev is a hash of the previous item in the sigchain (optional).
Prev []byte
// Revoke refers to a previous signed seq to revoke (optional).
Revoke int
// Type (optional).
Type string
// Timestamp (optional).
Timestamp time.Time
// Nonce (optional).
Nonce []byte
}
// StatementPublicKey describes a public key for a Statement.
type StatementPublicKey interface {
ID() ID
Verify(b []byte) ([]byte, error)
VerifyDetached(sig []byte, b []byte) error
}
// StatementPublicKeyFromID converts ID to StatementPublicKey.
// TODO: Support other key types.
func StatementPublicKeyFromID(id ID) (StatementPublicKey, error) {
return NewEdX25519PublicKeyFromID(id)
}
// Sign the statement.
// Returns an error if already signed.
func (s *Statement) Sign(signKey *EdX25519Key) error {
if s.Sig != nil {
return errors.Errorf("signature already set")
}
if s.KID != signKey.ID() {
return errors.Errorf("sign failed: key id mismatch")
}
b := s.BytesToSign()
s.Sig = signKey.SignDetached(b)
return nil
}
// StatementID returns and identifier for a Statement as kid-seq.
// If seq is <= 0, returns kid.
// The idenfifier looks like "kex1a4yj333g68pvd6hfqvufqkv4vy54jfe6t33ljd3kc9rpfty8xlgsfte2sn-000000000000001".
func StatementID(kid ID, seq int) string {
if seq <= 0 {
return kid.String()
}
return kid.WithSeq(seq)
}
// URL returns path string for a Statement in the HTTP API.
// If Seq is not set, then there is no path.
// Path looks like "/kex1a4yj333g68pvd6hfqvufqkv4vy54jfe6t33ljd3kc9rpfty8xlgsfte2sn/1".
func (s *Statement) URL() string {
if s.Seq == 0 {
return ""
}
return "/" + s.KID.String() + "/" + fmt.Sprintf("%d", s.Seq)
}
type statementFormat struct {
Sig []byte `json:".sig"`
Data []byte `json:"data"`
KID string `json:"kid"`
Nonce []byte `json:"nonce"`
Prev []byte `json:"prev"`
Revoke int `json:"revoke"`
Seq int `json:"seq"`
Timestamp int64 `json:"ts"`
Type string `json:"type"`
}
// Verify statement.
// If you have the original bytes use VerifySpecific.
func (s *Statement) Verify() error {
spk, err := StatementPublicKeyFromID(s.KID)
if err != nil {
return err
}
if len(s.Sig) == 0 {
return errors.Errorf("missing signature")
}
b := s.BytesToSign()
if err := spk.VerifyDetached(s.Sig, b); err != nil {
return err
}
return nil
}
// VerifySpecific and check that bytesToSign match the statement's
// BytesToSign, to verify the original bytes match the specific
// serialization.
func (s *Statement) VerifySpecific(bytesToSign []byte) error {
serialized := s.BytesToSign()
// We want to verify the bytes we get before unmarshalling match the same
// bytes used to sign/verify after marshalling.
// https://latacora.micro.blog/2019/07/24/how-not-to.html
if !bytes.Equal(bytesToSign, serialized) {
return errors.Errorf("statement bytes failed to match specific serialization")
}
return s.Verify()
}
// MarshalJSON marshals statement to JSON.
func (s *Statement) MarshalJSON() ([]byte, error) {
return s.Bytes()
}
// UnmarshalJSON unmarshals a statement from JSON.
func (s *Statement) UnmarshalJSON(b []byte) error {
st, err := unmarshalJSON(b)
if err != nil {
return err
}
s.Sig = st.Sig
s.Data = st.Data
s.KID = st.KID
s.Seq = st.Seq
s.Prev = st.Prev
s.Revoke = st.Revoke
s.Timestamp = st.Timestamp
s.Type = st.Type
s.Nonce = st.Nonce
return nil
}
// Bytes is the serialized Statement.
func (s *Statement) Bytes() ([]byte, error) {
if err := s.Verify(); err != nil {
return nil, err
}
return statementBytes(s, s.Sig), nil
}
// BytesToSign returns bytes to sign.
func (s *Statement) BytesToSign() []byte {
return statementBytes(s, nil)
}
func statementBytes(st *Statement, sig []byte) []byte {
mes := []encoding.TextMarshaler{
json.String(".sig", encoding.MustEncode(sig, encoding.Base64)),
}
if len(st.Data) != 0 {
mes = append(mes, json.String("data", encoding.MustEncode(st.Data, encoding.Base64)))
}
mes = append(mes, json.String("kid", st.KID.String()))
if len(st.Nonce) != 0 {
mes = append(mes, json.String("nonce", encoding.MustEncode(st.Nonce, encoding.Base64)))
}
if len(st.Prev) != 0 {
mes = append(mes, json.String("prev", encoding.MustEncode(st.Prev, encoding.Base64)))
}
if st.Revoke != 0 {
mes = append(mes, json.Int("revoke", st.Revoke))
}
if st.Seq != 0 {
mes = append(mes, json.Int("seq", st.Seq))
}
if !st.Timestamp.IsZero() {
mes = append(mes, json.Int("ts", int(tsutil.Millis(st.Timestamp))))
}
if st.Type != "" {
mes = append(mes, json.String("type", st.Type))
}
b, err := json.Marshal(mes...)
if err != nil {
panic(err)
}
return b
}
// unmarshalJSON returns a Statement from JSON bytes.
func unmarshalJSON(b []byte) (*Statement, error) {
if len(b) < 97 {
return nil, errors.Errorf("not enough bytes for statement")
}
// Extract sig directly from bytes.
sig := b[9:97]
bytesToSign := bytesJoin(b[0:9], b[97:])
sigBytes, err := encoding.Decode(string(sig), encoding.Base64)
if err != nil {
return nil, err
}
var stf statementFormat
if err := json.Unmarshal(b, &stf); err != nil {
return nil, errors.Errorf("statement not valid JSON")
}
kid, err := ParseID(stf.KID)
if err != nil {
return nil, err
}
ts := tsutil.ParseMillis(stf.Timestamp)
if !bytes.Equal(stf.Sig, sigBytes) {
return nil, errors.Errorf("sig bytes mismatch")
}
st := &Statement{
Sig: sigBytes,
Data: stf.Data,
KID: kid,
Nonce: stf.Nonce,
Prev: stf.Prev,
Revoke: stf.Revoke,
Seq: stf.Seq,
Timestamp: ts,
Type: stf.Type,
}
if err := st.VerifySpecific(bytesToSign); err != nil {
return nil, err
}
return st, nil
}