-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathverifier.go
More file actions
439 lines (360 loc) · 9.14 KB
/
verifier.go
File metadata and controls
439 lines (360 loc) · 9.14 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package keygen
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"fmt"
"io"
"net/http"
"strings"
"time"
)
const (
LicenseFileSigningPrefix = "license/"
MachineFileSigningPrefix = "machine/"
LicenseKeySigningPrefix = "key/"
)
type EncodingAlgorithm string
const (
EncodingAlgorithmAES256 EncodingAlgorithm = "aes-256-gcm"
EncodingAlgorithmBase64 EncodingAlgorithm = "base64"
)
type SigningAlgorithm string
const (
SigningAlgorithmEd25519 SigningAlgorithm = "ed25519"
SigningAlgorithmP256 SigningAlgorithm = "ecdsa-p256"
)
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
}
msg := []byte(LicenseFileSigningPrefix + cert.Enc)
sig, err := base64.StdEncoding.DecodeString(cert.Sig)
if err != nil {
return ErrLicenseFileNotGenuine
}
switch cert.SigningAlgorithm() {
case SigningAlgorithmEd25519:
publicKey, err := v.publicKeyBytes()
if err != nil {
return err
}
if ok := ed25519.Verify(publicKey, msg, sig); !ok {
return ErrLicenseFileNotGenuine
}
case SigningAlgorithmP256:
publicKey, err := v.publicKey()
if err != nil {
return err
}
ecdsaPubKey := publicKey.(*ecdsa.PublicKey)
hash := sha256.Sum256(msg)
if !ecdsa.VerifyASN1(ecdsaPubKey, hash[:], sig) {
return ErrLicenseFileNotGenuine
}
default:
return ErrLicenseFileNotSupported
}
return nil
}
// VerifyMachineFile checks if a license file is genuine.
func (v *verifier) VerifyMachineFile(lic *MachineFile) error {
cert, err := lic.certificate()
if err != nil {
return err
}
msg := []byte(MachineFileSigningPrefix + cert.Enc)
sig, err := base64.StdEncoding.DecodeString(cert.Sig)
if err != nil {
return ErrMachineFileNotGenuine
}
switch cert.SigningAlgorithm() {
case SigningAlgorithmEd25519:
publicKey, err := v.publicKeyBytes()
if err != nil {
return err
}
if ok := ed25519.Verify(publicKey, msg, sig); !ok {
return ErrMachineFileNotGenuine
}
case SigningAlgorithmP256:
publicKey, err := v.publicKey()
if err != nil {
return err
}
ecdsaPubKey := publicKey.(*ecdsa.PublicKey)
hash := sha256.Sum256(msg)
if !ecdsa.VerifyASN1(ecdsaPubKey, hash[:], sig) {
return ErrMachineFileNotGenuine
}
default:
return ErrMachineFileNotSupported
}
return nil
}
// 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.Scheme == "" {
return nil, ErrLicenseSchemeMissing
}
if license.Key == "" {
return nil, ErrLicenseKeyMissing
}
return v.verifyKey(license.Scheme, license.Key)
}
func (v *verifier) VerifyRequest(request *http.Request) error {
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)
alg := sigParams["algorithm"]
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
}
// we only support ed25519 and ecdsa p256 (nist p-256)
switch SigningAlgorithm(alg) {
case SigningAlgorithmEd25519:
publicKey, err := v.publicKeyBytes()
if err != nil {
return err
}
if ok := ed25519.Verify(publicKey, msgBytes, sigBytes); !ok {
return ErrResponseSignatureInvalid
}
case SigningAlgorithmP256:
publicKey, err := v.publicKey()
if err != nil {
return err
}
ecdsaPub := publicKey.(*ecdsa.PublicKey)
hash := sha256.Sum256(msgBytes)
if !ecdsa.VerifyASN1(ecdsaPub, hash[:], sigBytes) {
return ErrResponseSignatureInvalid
}
default:
return ErrResponseSignatureNotSupported
}
return nil
}
func (v *verifier) VerifyResponse(response *Response) error {
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)
alg := sigParams["algorithm"]
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
}
// we only support ed25519 and ecdsa p256 (nist p-256)
switch SigningAlgorithm(alg) {
case SigningAlgorithmEd25519:
publicKey, err := v.publicKeyBytes()
if err != nil {
return err
}
if ok := ed25519.Verify(publicKey, msgBytes, sigBytes); !ok {
return ErrResponseSignatureInvalid
}
case SigningAlgorithmP256:
publicKey, err := v.publicKey()
if err != nil {
return err
}
ecdsaPub := publicKey.(*ecdsa.PublicKey)
hash := sha256.Sum256(msgBytes)
if !ecdsa.VerifyASN1(ecdsaPub, hash[:], sigBytes) {
return ErrResponseSignatureInvalid
}
default:
return ErrResponseSignatureNotSupported
}
return nil
}
func (v *verifier) verifyKey(scheme SchemeCode, key string) ([]byte, error) {
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(LicenseKeySigningPrefix + 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
}
switch scheme {
case SchemeCodeEd25519:
publicKey, err := v.publicKeyBytes()
if err != nil {
return nil, err
}
if ok := ed25519.Verify(publicKey, msg, sig); !ok {
return nil, ErrLicenseKeyNotGenuine
}
case SchemeCodeP256:
publicKey, err := v.publicKey()
if err != nil {
return nil, err
}
ecdsaPub := publicKey.(*ecdsa.PublicKey)
hash := sha256.Sum256(msg)
if !ecdsa.VerifyASN1(ecdsaPub, hash[:], sig) {
return nil, ErrLicenseKeyNotGenuine
}
default:
return nil, ErrSchemeNotSupported
}
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 (v *verifier) publicKey() (crypto.PublicKey, error) {
if v.PublicKey == "" {
return nil, ErrPublicKeyMissing
}
block, _ := pem.Decode([]byte(v.PublicKey))
if block == nil {
return nil, ErrPublicKeyInvalid
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, ErrPublicKeyInvalid
}
if _, ok := pub.(*ecdsa.PublicKey); !ok {
return nil, ErrPublicKeyInvalid
}
return pub, 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
}