-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
verifier.go
321 lines (261 loc) · 6.53 KB
/
verifier.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package keygen
import (
"bytes"
"crypto/ed25519"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"net/http"
"strings"
"time"
)
type verifier struct {
PublicKey string
}
// VerifyLicenseFile checks if a license file is genuine.
func (v *verifier) VerifyLicenseFile(lic *LicenseFile) error {
cert, err := lic.certificate()
if err != nil {
return err
}
switch {
case cert.Alg == "aes-256-gcm+ed25519" || cert.Alg == "base64+ed25519":
publicKey, err := v.publicKeyBytes()
if err != nil {
return err
}
msg := []byte("license/" + cert.Enc)
sig, err := base64.StdEncoding.DecodeString(cert.Sig)
if err != nil {
return ErrLicenseFileNotGenuine
}
if ok := ed25519.Verify(publicKey, msg, sig); !ok {
return ErrLicenseFileNotGenuine
}
return nil
default:
return ErrLicenseFileNotSupported
}
}
// VerifyMachineFile checks if a license file is genuine.
func (v *verifier) VerifyMachineFile(lic *MachineFile) error {
cert, err := lic.certificate()
if err != nil {
return err
}
switch {
case cert.Alg == "aes-256-gcm+ed25519" || cert.Alg == "base64+ed25519":
publicKey, err := v.publicKeyBytes()
if err != nil {
return err
}
msg := []byte("machine/" + cert.Enc)
sig, err := base64.StdEncoding.DecodeString(cert.Sig)
if err != nil {
return ErrMachineFileNotGenuine
}
if ok := ed25519.Verify(publicKey, msg, sig); !ok {
return ErrMachineFileNotGenuine
}
return nil
default:
return ErrMachineFileNotSupported
}
}
// Verify checks if a license key is genuine by cryptographically verifying the
// key using your PublicKey. If the key is genuine, the decoded dataset from the
// key will be returned. An error will be returned if the key is not genuine or
// otherwise invalid, e.g. ErrLicenseNotGenuine.
func (v *verifier) VerifyLicense(license *License) ([]byte, error) {
if license.Key == "" {
return nil, ErrLicenseKeyMissing
}
if license.Scheme == "" {
return nil, ErrLicenseSchemeMissing
}
switch {
case license.Scheme == SchemeCodeEd25519:
dataset, err := v.verifyKey(license.Key)
return dataset, err
default:
return nil, ErrLicenseSchemeNotSupported
}
}
func (v *verifier) VerifyRequest(request *http.Request) error {
publicKey, err := v.publicKeyBytes()
if err != nil {
return err
}
digestHeader := request.Header.Get("Digest")
if digestHeader == "" {
return ErrRequestDigestMissing
}
body, err := io.ReadAll(request.Body)
if err != nil {
return err
}
// We need to close and replace the body so that others can read
request.Body.Close()
request.Body = io.NopCloser(bytes.NewBuffer(body))
shasum := sha256.Sum256(body)
digest := "sha-256=" + base64.StdEncoding.EncodeToString(shasum[:])
if digest != digestHeader {
return ErrRequestDigestInvalid
}
date := request.Header.Get("Date")
if date == "" {
return ErrRequestDateMissing
}
t, err := time.Parse(time.RFC1123, date)
if err != nil {
return err
}
if MaxClockDrift >= 0 && time.Since(t) > MaxClockDrift {
return ErrRequestDateTooOld
}
method := strings.ToLower(request.Method)
host := request.Host
url := request.URL
path := url.EscapedPath()
if path == "" {
path = "/"
}
if url.RawQuery != "" {
path += "?" + url.RawQuery
}
sigHeader := request.Header.Get("Keygen-Signature")
if sigHeader == "" {
return ErrRequestSignatureMissing
}
sigParams := parseSignatureHeader(sigHeader)
sig := sigParams["signature"]
msg := fmt.Sprintf(
"(request-target): %s %s\nhost: %s\ndate: %s\ndigest: %s",
method,
path,
host,
date,
digest,
)
msgBytes := []byte(msg)
sigBytes, err := base64.StdEncoding.DecodeString(sig)
if err != nil {
return err
}
if ok := ed25519.Verify(publicKey, msgBytes, sigBytes); !ok {
return ErrRequestSignatureInvalid
}
return nil
}
func (v *verifier) VerifyResponse(response *Response) error {
publicKey, err := v.publicKeyBytes()
if err != nil {
return err
}
digestHeader := response.Headers.Get("Digest")
if digestHeader == "" {
return ErrResponseDigestMissing
}
shasum := sha256.Sum256(response.Body)
digest := "sha-256=" + base64.StdEncoding.EncodeToString(shasum[:])
if digest != digestHeader {
return ErrResponseDigestInvalid
}
date := response.Headers.Get("Date")
if date == "" {
return ErrResponseDateMissing
}
t, err := time.Parse(time.RFC1123, date)
if err != nil {
return err
}
if MaxClockDrift >= 0 && time.Since(t) > MaxClockDrift {
return ErrResponseDateTooOld
}
method := strings.ToLower(response.Request.Method)
url := response.Request.URL
host := url.Host
path := url.EscapedPath()
if path == "" {
path = "/"
}
if url.RawQuery != "" {
path += "?" + url.RawQuery
}
sigHeader := response.Headers.Get("Keygen-Signature")
if sigHeader == "" {
return ErrResponseSignatureMissing
}
sigParams := parseSignatureHeader(sigHeader)
sig := sigParams["signature"]
msg := fmt.Sprintf(
"(request-target): %s %s\nhost: %s\ndate: %s\ndigest: %s",
method,
path,
host,
date,
digest,
)
msgBytes := []byte(msg)
sigBytes, err := base64.StdEncoding.DecodeString(sig)
if err != nil {
return ErrResponseSignatureInvalid
}
if ok := ed25519.Verify(publicKey, msgBytes, sigBytes); !ok {
return ErrResponseSignatureInvalid
}
return nil
}
func (v *verifier) verifyKey(key string) ([]byte, error) {
publicKey, err := v.publicKeyBytes()
if err != nil {
return nil, err
}
parts := strings.SplitN(key, ".", 2)
signingData := parts[0]
encSig := parts[1]
parts = strings.SplitN(signingData, "/", 2)
signingPrefix := parts[0]
encDataset := parts[1]
if signingPrefix != "key" {
return nil, ErrLicenseKeyNotGenuine
}
msg := []byte("key/" + encDataset)
sig, err := base64.URLEncoding.DecodeString(encSig)
if err != nil {
return nil, ErrLicenseKeyNotGenuine
}
dataset, err := base64.URLEncoding.DecodeString(encDataset)
if err != nil {
return nil, ErrLicenseKeyNotGenuine
}
if ok := ed25519.Verify(publicKey, msg, sig); !ok {
return nil, ErrLicenseKeyNotGenuine
}
return dataset, nil
}
func (v *verifier) publicKeyBytes() ([]byte, error) {
if v.PublicKey == "" {
return nil, ErrPublicKeyMissing
}
key, err := hex.DecodeString(v.PublicKey)
if err != nil {
return nil, ErrPublicKeyInvalid
}
if l := len(key); l != ed25519.PublicKeySize {
return nil, ErrPublicKeyInvalid
}
return key, nil
}
func parseSignatureHeader(header string) map[string]string {
params := make(map[string]string)
for _, param := range strings.Split(header, ",") {
kv := strings.SplitN(param, "=", 2)
k := strings.TrimLeft(kv[0], " ")
v := strings.Trim(kv[1], `"`)
params[k] = v
}
return params
}